2011-09-10 11:13:51 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
# Thomas Nagy, 2008-2010 (ita)
|
|
|
|
|
|
|
|
"""
|
|
|
|
Execute the tasks with gcc -MD, read the dependencies from the .d file
|
2015-03-02 16:54:56 +01:00
|
|
|
and prepare the dependency calculation for the next run.
|
2016-07-26 19:41:57 +02:00
|
|
|
This affects the cxx class, so make sure to load Qt5 after this tool.
|
2015-03-02 16:54:56 +01:00
|
|
|
|
2017-09-21 17:30:08 +02:00
|
|
|
Usage::
|
|
|
|
|
|
|
|
def options(opt):
|
|
|
|
opt.load('compiler_cxx')
|
2015-03-02 16:54:56 +01:00
|
|
|
def configure(conf):
|
2017-09-21 17:30:08 +02:00
|
|
|
conf.load('compiler_cxx gccdeps')
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
|
2020-06-25 00:55:46 +02:00
|
|
|
import os, re, threading
|
2011-09-10 11:13:51 +02:00
|
|
|
from waflib import Task, Logs, Utils, Errors
|
|
|
|
from waflib.Tools import c_preproc
|
2012-07-13 19:56:38 +02:00
|
|
|
from waflib.TaskGen import before_method, feature
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
lock = threading.Lock()
|
|
|
|
|
2015-03-02 16:54:56 +01:00
|
|
|
gccdeps_flags = ['-MD']
|
|
|
|
if not c_preproc.go_absolute:
|
|
|
|
gccdeps_flags = ['-MMD']
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2012-07-12 23:11:15 +02:00
|
|
|
# Third-party tools are allowed to add extra names in here with append()
|
2020-02-24 18:27:30 +01:00
|
|
|
supported_compilers = ['gas', 'gcc', 'icc', 'clang']
|
2012-07-12 23:11:15 +02:00
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def scan(self):
|
2015-03-02 16:54:56 +01:00
|
|
|
if not self.__class__.__name__ in self.env.ENABLE_GCCDEPS:
|
2016-07-26 19:41:57 +02:00
|
|
|
return super(self.derived_gccdeps, self).scan()
|
2011-09-10 11:13:51 +02:00
|
|
|
nodes = self.generator.bld.node_deps.get(self.uid(), [])
|
|
|
|
names = []
|
|
|
|
return (nodes, names)
|
|
|
|
|
2019-01-05 12:02:42 +01:00
|
|
|
re_o = re.compile(r"\.o$")
|
2012-05-04 14:32:25 +02:00
|
|
|
re_splitter = re.compile(r'(?<!\\)\s+') # split by space, except when spaces are escaped
|
2012-07-13 19:56:38 +02:00
|
|
|
|
|
|
|
def remove_makefile_rule_lhs(line):
|
|
|
|
# Splitting on a plain colon would accidentally match inside a
|
|
|
|
# Windows absolute-path filename, so we must search for a colon
|
|
|
|
# followed by whitespace to find the divider between LHS and RHS
|
|
|
|
# of the Makefile rule.
|
|
|
|
rulesep = ': '
|
|
|
|
|
|
|
|
sep_idx = line.find(rulesep)
|
|
|
|
if sep_idx >= 0:
|
|
|
|
return line[sep_idx + 2:]
|
|
|
|
else:
|
|
|
|
return line
|
|
|
|
|
2012-12-28 22:45:49 +01:00
|
|
|
def path_to_node(base_node, path, cached_nodes):
|
|
|
|
# Take the base node and the path and return a node
|
|
|
|
# Results are cached because searching the node tree is expensive
|
|
|
|
# The following code is executed by threads, it is not safe, so a lock is needed...
|
|
|
|
if getattr(path, '__hash__'):
|
|
|
|
node_lookup_key = (base_node, path)
|
|
|
|
else:
|
|
|
|
# Not hashable, assume it is a list and join into a string
|
|
|
|
node_lookup_key = (base_node, os.path.sep.join(path))
|
|
|
|
try:
|
|
|
|
lock.acquire()
|
|
|
|
node = cached_nodes[node_lookup_key]
|
|
|
|
except KeyError:
|
|
|
|
node = base_node.find_resource(path)
|
|
|
|
cached_nodes[node_lookup_key] = node
|
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
return node
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def post_run(self):
|
2015-03-02 16:54:56 +01:00
|
|
|
if not self.__class__.__name__ in self.env.ENABLE_GCCDEPS:
|
2016-07-26 19:41:57 +02:00
|
|
|
return super(self.derived_gccdeps, self).post_run()
|
2012-05-04 09:25:48 +02:00
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
name = self.outputs[0].abspath()
|
|
|
|
name = re_o.sub('.d', name)
|
2017-02-14 22:25:43 +01:00
|
|
|
try:
|
|
|
|
txt = Utils.readf(name)
|
|
|
|
except EnvironmentError:
|
|
|
|
Logs.error('Could not find a .d dependency file, are cflags/cxxflags overwritten?')
|
|
|
|
raise
|
2013-05-18 19:51:45 +02:00
|
|
|
#os.remove(name)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2012-07-12 23:11:15 +02:00
|
|
|
# Compilers have the choice to either output the file's dependencies
|
|
|
|
# as one large Makefile rule:
|
|
|
|
#
|
|
|
|
# /path/to/file.o: /path/to/dep1.h \
|
|
|
|
# /path/to/dep2.h \
|
|
|
|
# /path/to/dep3.h \
|
|
|
|
# ...
|
|
|
|
#
|
|
|
|
# or as many individual rules:
|
|
|
|
#
|
|
|
|
# /path/to/file.o: /path/to/dep1.h
|
|
|
|
# /path/to/file.o: /path/to/dep2.h
|
|
|
|
# /path/to/file.o: /path/to/dep3.h
|
|
|
|
# ...
|
|
|
|
#
|
|
|
|
# So the first step is to sanitize the input by stripping out the left-
|
|
|
|
# hand side of all these lines. After that, whatever remains are the
|
|
|
|
# implicit dependencies of task.outputs[0]
|
|
|
|
txt = '\n'.join([remove_makefile_rule_lhs(line) for line in txt.splitlines()])
|
|
|
|
|
|
|
|
# Now join all the lines together
|
2011-09-10 11:13:51 +02:00
|
|
|
txt = txt.replace('\\\n', '')
|
|
|
|
|
2012-07-12 23:11:15 +02:00
|
|
|
val = txt.strip()
|
2012-05-04 19:43:13 +02:00
|
|
|
val = [x.replace('\\ ', ' ') for x in re_splitter.split(val) if x]
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
nodes = []
|
|
|
|
bld = self.generator.bld
|
|
|
|
|
2012-12-28 22:45:49 +01:00
|
|
|
# Dynamically bind to the cache
|
|
|
|
try:
|
|
|
|
cached_nodes = bld.cached_nodes
|
|
|
|
except AttributeError:
|
|
|
|
cached_nodes = bld.cached_nodes = {}
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
for x in val:
|
|
|
|
|
|
|
|
node = None
|
|
|
|
if os.path.isabs(x):
|
2012-12-28 22:45:49 +01:00
|
|
|
node = path_to_node(bld.root, x, cached_nodes)
|
2011-09-10 11:13:51 +02:00
|
|
|
else:
|
2015-11-08 11:48:53 +01:00
|
|
|
# TODO waf 1.9 - single cwd value
|
|
|
|
path = getattr(bld, 'cwdx', bld.bldnode)
|
2015-07-27 18:38:03 +02:00
|
|
|
# when calling find_resource, make sure the path does not contain '..'
|
2011-09-10 11:13:51 +02:00
|
|
|
x = [k for k in Utils.split_path(x) if k and k != '.']
|
2015-07-27 18:38:03 +02:00
|
|
|
while '..' in x:
|
|
|
|
idx = x.index('..')
|
|
|
|
if idx == 0:
|
|
|
|
x = x[1:]
|
|
|
|
path = path.parent
|
|
|
|
else:
|
|
|
|
del x[idx]
|
|
|
|
del x[idx-1]
|
|
|
|
|
2012-12-28 22:45:49 +01:00
|
|
|
node = path_to_node(path, x, cached_nodes)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
if not node:
|
|
|
|
raise ValueError('could not find %r for %r' % (x, self))
|
2015-03-02 16:54:56 +01:00
|
|
|
if id(node) == id(self.inputs[0]):
|
|
|
|
# ignore the source file, it is already in the dependencies
|
|
|
|
# this way, successful config tests may be retrieved from the cache
|
|
|
|
continue
|
|
|
|
nodes.append(node)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2016-05-28 16:18:51 +02:00
|
|
|
Logs.debug('deps: gccdeps for %s returned %s', self, nodes)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
bld.node_deps[self.uid()] = nodes
|
|
|
|
bld.raw_deps[self.uid()] = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
del self.cache_sig
|
2016-05-28 16:18:51 +02:00
|
|
|
except AttributeError:
|
2011-09-10 11:13:51 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
Task.Task.post_run(self)
|
|
|
|
|
|
|
|
def sig_implicit_deps(self):
|
2015-03-02 16:54:56 +01:00
|
|
|
if not self.__class__.__name__ in self.env.ENABLE_GCCDEPS:
|
2016-07-26 19:41:57 +02:00
|
|
|
return super(self.derived_gccdeps, self).sig_implicit_deps()
|
2020-06-25 00:55:46 +02:00
|
|
|
bld = self.generator.bld
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
2020-06-25 00:55:46 +02:00
|
|
|
return self.compute_sig_implicit_deps()
|
|
|
|
except Errors.TaskNotReady:
|
|
|
|
raise ValueError("Please specify the build order precisely with gccdeps (asm/c/c++ tasks)")
|
|
|
|
except EnvironmentError:
|
|
|
|
# If a file is renamed, assume the dependencies are stale and must be recalculated
|
|
|
|
for x in bld.node_deps.get(self.uid(), []):
|
|
|
|
if not x.is_bld() and not x.exists():
|
|
|
|
try:
|
|
|
|
del x.parent.children[x.name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
key = self.uid()
|
|
|
|
bld.node_deps[key] = []
|
|
|
|
bld.raw_deps[key] = []
|
|
|
|
return Utils.SIG_NIL
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2016-07-26 19:41:57 +02:00
|
|
|
def wrap_compiled_task(classname):
|
|
|
|
derived_class = type(classname, (Task.classes[classname],), {})
|
|
|
|
derived_class.derived_gccdeps = derived_class
|
|
|
|
derived_class.post_run = post_run
|
|
|
|
derived_class.scan = scan
|
|
|
|
derived_class.sig_implicit_deps = sig_implicit_deps
|
2012-05-04 09:25:48 +02:00
|
|
|
|
2020-02-24 18:27:30 +01:00
|
|
|
for k in ('asm', 'c', 'cxx'):
|
2016-07-26 19:41:57 +02:00
|
|
|
if k in Task.classes:
|
|
|
|
wrap_compiled_task(k)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2015-03-02 16:54:56 +01:00
|
|
|
@before_method('process_source')
|
|
|
|
@feature('force_gccdeps')
|
|
|
|
def force_gccdeps(self):
|
2020-02-24 18:27:30 +01:00
|
|
|
self.env.ENABLE_GCCDEPS = ['asm', 'c', 'cxx']
|
2015-03-02 16:54:56 +01:00
|
|
|
|
|
|
|
def configure(conf):
|
2015-10-31 11:24:53 +01:00
|
|
|
# in case someone provides a --enable-gccdeps command-line option
|
|
|
|
if not getattr(conf.options, 'enable_gccdeps', True):
|
|
|
|
return
|
|
|
|
|
2015-03-02 16:54:56 +01:00
|
|
|
global gccdeps_flags
|
|
|
|
flags = conf.env.GCCDEPS_FLAGS or gccdeps_flags
|
2020-02-24 18:27:30 +01:00
|
|
|
if conf.env.ASM_NAME in supported_compilers:
|
|
|
|
try:
|
|
|
|
conf.check(fragment='', features='asm force_gccdeps', asflags=flags, compile_filename='test.S', msg='Checking for asm flags %r' % ''.join(flags))
|
|
|
|
except Errors.ConfigurationError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
conf.env.append_value('ASFLAGS', flags)
|
|
|
|
conf.env.append_unique('ENABLE_GCCDEPS', 'asm')
|
|
|
|
|
2015-03-02 16:54:56 +01:00
|
|
|
if conf.env.CC_NAME in supported_compilers:
|
|
|
|
try:
|
|
|
|
conf.check(fragment='int main() { return 0; }', features='c force_gccdeps', cflags=flags, msg='Checking for c flags %r' % ''.join(flags))
|
|
|
|
except Errors.ConfigurationError:
|
|
|
|
pass
|
|
|
|
else:
|
2018-12-19 17:18:19 +01:00
|
|
|
conf.env.append_value('CFLAGS', flags)
|
2015-03-02 16:54:56 +01:00
|
|
|
conf.env.append_unique('ENABLE_GCCDEPS', 'c')
|
|
|
|
|
|
|
|
if conf.env.CXX_NAME in supported_compilers:
|
|
|
|
try:
|
|
|
|
conf.check(fragment='int main() { return 0; }', features='cxx force_gccdeps', cxxflags=flags, msg='Checking for cxx flags %r' % ''.join(flags))
|
|
|
|
except Errors.ConfigurationError:
|
|
|
|
pass
|
|
|
|
else:
|
2018-12-19 17:18:19 +01:00
|
|
|
conf.env.append_value('CXXFLAGS', flags)
|
2015-03-02 16:54:56 +01:00
|
|
|
conf.env.append_unique('ENABLE_GCCDEPS', 'cxx')
|
|
|
|
|
2017-09-21 17:30:08 +02:00
|
|
|
def options(opt):
|
|
|
|
raise ValueError('Do not load gccdeps options')
|
|
|
|
|