gccdeps.py 7.19 KB
Newer Older
Thomas Nagy's avatar
Thomas Nagy committed
1
2
3
4
5
6
#!/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
7
and prepare the dependency calculation for the next run.
8
This affects the cxx class, so make sure to load Qt5 after this tool.
9

10
11
12
13
Usage::

	def options(opt):
		opt.load('compiler_cxx')
14
	def configure(conf):
15
		conf.load('compiler_cxx gccdeps')
Thomas Nagy's avatar
Thomas Nagy committed
16
17
"""

18
import os, re, threading
Thomas Nagy's avatar
Thomas Nagy committed
19
20
from waflib import Task, Logs, Utils, Errors
from waflib.Tools import c_preproc
Thomas Nagy's avatar
Thomas Nagy committed
21
from waflib.TaskGen import before_method, feature
Thomas Nagy's avatar
Thomas Nagy committed
22
23
24

lock = threading.Lock()

25
26
27
gccdeps_flags = ['-MD']
if not c_preproc.go_absolute:
	gccdeps_flags = ['-MMD']
Thomas Nagy's avatar
Thomas Nagy committed
28

29
# Third-party tools are allowed to add extra names in here with append()
Michael Vincent's avatar
Michael Vincent committed
30
supported_compilers = ['gas', 'gcc', 'icc', 'clang']
31

32
re_o = re.compile(r"\.o$")
ita's avatar
ita committed
33
re_splitter = re.compile(r'(?<!\\)\s+') # split by space, except when spaces are escaped
Thomas Nagy's avatar
Thomas Nagy committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47

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

48
49
50
51
52
53
54
55
56
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))
Michael Vincent's avatar
Michael Vincent committed
57

58
59
60
	try:
		node = cached_nodes[node_lookup_key]
	except KeyError:
Michael Vincent's avatar
Michael Vincent committed
61
62
63
64
65
66
67
		# retry with lock on cache miss
		with lock:
			try:
				node = cached_nodes[node_lookup_key]
			except KeyError:
				node = cached_nodes[node_lookup_key] = base_node.find_resource(path)

68
69
	return node

Thomas Nagy's avatar
Thomas Nagy committed
70
def post_run(self):
71
	if not self.__class__.__name__ in self.env.ENABLE_GCCDEPS:
72
		return super(self.derived_gccdeps, self).post_run()
ita's avatar
ita committed
73

74
75
	deps_filename = self.outputs[0].abspath()
	deps_filename = re_o.sub('.d', deps_filename)
76
	try:
77
		deps_txt = Utils.readf(deps_filename)
78
79
80
	except EnvironmentError:
		Logs.error('Could not find a .d dependency file, are cflags/cxxflags overwritten?')
		raise
Thomas Nagy's avatar
Thomas Nagy committed
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	# 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]
100
	deps_txt = '\n'.join([remove_makefile_rule_lhs(line) for line in deps_txt.splitlines()])
101
102

	# Now join all the lines together
103
	deps_txt = deps_txt.replace('\\\n', '')
Thomas Nagy's avatar
Thomas Nagy committed
104

105
106
	dep_paths = deps_txt.strip()
	dep_paths = [x.replace('\\ ', ' ') for x in re_splitter.split(dep_paths) if x]
Thomas Nagy's avatar
Thomas Nagy committed
107

108
109
	resolved_nodes = []
	unresolved_names = []
Thomas Nagy's avatar
Thomas Nagy committed
110
111
	bld = self.generator.bld

112
113
114
115
116
117
	# Dynamically bind to the cache
	try:
		cached_nodes = bld.cached_nodes
	except AttributeError:
		cached_nodes = bld.cached_nodes = {}

118
	for path in dep_paths:
Thomas Nagy's avatar
Thomas Nagy committed
119
120

		node = None
121
122
		if os.path.isabs(path):
			node = path_to_node(bld.root, path, cached_nodes)
Thomas Nagy's avatar
Thomas Nagy committed
123
		else:
124
			# TODO waf 1.9 - single cwd value
125
			base_node = getattr(bld, 'cwdx', bld.bldnode)
126
			# when calling find_resource, make sure the path does not contain '..'
127
128
129
			path = [k for k in Utils.split_path(path) if k and k != '.']
			while '..' in path:
				idx = path.index('..')
130
				if idx == 0:
131
132
					path = path[1:]
					base_node = base_node.parent
133
				else:
134
135
					del path[idx]
					del path[idx-1]
136

137
			node = path_to_node(base_node, path, cached_nodes)
Thomas Nagy's avatar
Thomas Nagy committed
138
139

		if not node:
140
141
			raise ValueError('could not find %r for %r' % (path, self))

142
143
144
145
		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
Thomas Nagy's avatar
Thomas Nagy committed
146

147
148
149
		resolved_nodes.append(node)

	Logs.debug('deps: gccdeps for %s returned %s', self, resolved_nodes)
Thomas Nagy's avatar
Thomas Nagy committed
150

151
152
	bld.node_deps[self.uid()] = resolved_nodes
	bld.raw_deps[self.uid()] = unresolved_names
Thomas Nagy's avatar
Thomas Nagy committed
153
154
155

	try:
		del self.cache_sig
156
	except AttributeError:
Thomas Nagy's avatar
Thomas Nagy committed
157
158
159
160
		pass

	Task.Task.post_run(self)

Michael Vincent's avatar
Michael Vincent committed
161
162
163
164
165
166
167
168
def scan(self):
	if not self.__class__.__name__ in self.env.ENABLE_GCCDEPS:
		return super(self.derived_gccdeps, self).scan()

	resolved_nodes = self.generator.bld.node_deps.get(self.uid(), [])
	unresolved_names = []
	return (resolved_nodes, unresolved_names)

Thomas Nagy's avatar
Thomas Nagy committed
169
def sig_implicit_deps(self):
170
	if not self.__class__.__name__ in self.env.ENABLE_GCCDEPS:
171
		return super(self.derived_gccdeps, self).sig_implicit_deps()
172
173
	bld = self.generator.bld

Thomas Nagy's avatar
Thomas Nagy committed
174
	try:
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
		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
Thomas Nagy's avatar
Thomas Nagy committed
191

192
193
194
195
196
197
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
ita's avatar
ita committed
198

Michael Vincent's avatar
Michael Vincent committed
199
for k in ('asm', 'c', 'cxx'):
200
201
	if k in Task.classes:
		wrap_compiled_task(k)
Thomas Nagy's avatar
Thomas Nagy committed
202

203
204
205
@before_method('process_source')
@feature('force_gccdeps')
def force_gccdeps(self):
Michael Vincent's avatar
Michael Vincent committed
206
	self.env.ENABLE_GCCDEPS = ['asm', 'c', 'cxx']
207
208

def configure(conf):
209
210
211
212
	# in case someone provides a --enable-gccdeps command-line option
	if not getattr(conf.options, 'enable_gccdeps', True):
		return

213
214
	global gccdeps_flags
	flags = conf.env.GCCDEPS_FLAGS or gccdeps_flags
Michael Vincent's avatar
Michael Vincent committed
215
216
217
218
219
220
221
222
223
	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')

224
225
226
227
228
229
	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:
230
			conf.env.append_value('CFLAGS', flags)
231
232
233
234
235
236
237
238
			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:
239
			conf.env.append_value('CXXFLAGS', flags)
240
241
			conf.env.append_unique('ENABLE_GCCDEPS', 'cxx')

242
243
244
def options(opt):
	raise ValueError('Do not load gccdeps options')