forked from dspeterson/dory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSConscript
More file actions
384 lines (304 loc) · 14.2 KB
/
SConscript
File metadata and controls
384 lines (304 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python3
# -----------------------------------------------------------------------------
# The copyright notice below is for the SCons build scripts included with this
# software, which were initially developed by Michael Park
# (see https://github.com/mpark/bob) and customized at if(we). Many thanks to
# Michael for this useful contribution.
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# The MIT License (MIT)
# Copyright (c) 2014 Michael Park
# Copyright (c) 2014 if(we)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -----------------------------------------------------------------------------
import functools
import os
import os.path
import re
import subprocess
import sys
import SCons.Scanner.C
# Import variables.
Import(['env', 'src', 'out'])
# Our scanner.
scanner = SCons.Scanner.C.CScanner()
# Current working directory.
cwd = Dir(Dir(GetLaunchDir()).get_path(src))
# Set of supported extensions.
exts = {env[suffix] for suffix in {'PROGSUFFIX',
'LIBSUFFIX',
'SHLIBSUFFIX',
'OBJSUFFIX',
'SHOBJSUFFIX',
'TESTSUFFIX'}}
lib_header_map = {}
generated_source_map = {}
# Input parameter 'target_path' is a filename which may represent a header
# file, object file, or library. For instance, some possibilities are:
#
# 'foo/bar.h'
# 'foo/bar.o'
# 'foo/bar.a'
# 'foo/bar.so'
#
# If 'target_path' is a header file for a library that we must build (as
# specified by a call to specify_lib_headers() below), then return a File
# object representing the library to build. Otherwise look for a corresponding
# non-header C or C++ source file, where the possible file suffixes are defined
# by input list 'dep_suffixes'. For instance, if dep_suffixes is
# [ '.cc', '.cpp', '.c' ] then the possibilities are:
#
# 'foo/bar.cc'
# 'foo/bar.cpp'
# 'foo/bar.c'
#
# If no such non-header is found, return None. If exactly one such non-header
# is found, return a File object representing it. If for some crazy reason,
# multiple such non-headers are found (for instance, both 'foo/bar.cc' and
# 'foo/bar.c'), halt with an error message. If parameter 'cwd_rel' is True,
# then 'target_path' is interpreted as relative to the current working
# directory. Otherwise 'target_path' is interpreted as relative to 'src'.
def get_one_dep_source(target_path, dep_suffixes, cwd_rel, dep_libs):
(prefix, suffix) = os.path.splitext(target_path)
if target_path in lib_header_map:
# Handle case where 'target_path' is a header for a library that we
# must build. Here, the library gets added to a set of libraries that
# will become additional targets to build.
dep_libs.add(lib_header_map[target_path])
return None
if target_path in generated_source_map:
# Handle case where corresponding non-header is a generated file.
result = generated_source_map[target_path]
else:
result = None
for elem in dep_suffixes:
if cwd_rel:
dep_source = cwd.File(prefix + elem)
else:
dep_source = File(prefix + elem)
if dep_source.srcnode().exists():
if result is not None:
path1 = prefix + os.path.splitext(result.get_path())[1]
path2 = prefix + os.path.splitext(dep_source.get_path())[1]
sys.stderr.write('Ambiguous sources for ' + target_path +
': ' + path1 + ' ' + path2 + '\n')
sys.exit(1)
result = dep_source
return result
# This is a workaround for a change in the signature of method
# 'get_implicit_deps' of SCons class 'Node' that occurred in version 2.5.0.
#
# TODO: Look for a better workaround.
def get_implicit_deps_wrapper(source):
def path_fn(scanner_param):
return (src,)
try:
# First try calling the method using the parameters it expects in SCons
# version 2.5.0 or later.
result = source.get_implicit_deps(env, scanner, path_fn)
except TypeError as err:
if str(err) != "path_fn() takes exactly 1 argument (0 given)":
raise
# If we get here, we saw the failure that occurs when using an older
# version of SCons. Therefore try the old invocation style.
result = source.get_implicit_deps(env, scanner, (src,))
return result
# If parameter 'main' is None, return None. Otherwise, recursively find all of
# the object files that main depends on, and return the result as a list of
# File objects. 'dep_libs' is an initially empty set of libraries. On return,
# it contains all libraries that the current target depends on, and that we
# must build. These libraries will become additional targets to build.
def get_dep_sources(main, dep_libs):
def impl(source_set, result, dep_libs):
if not source_set:
return result
result |= source_set
dep_source_set = set()
for source in source_set:
if not source.srcnode().exists():
continue # source file is generated during build
for dep_header in get_implicit_deps_wrapper(source):
dep_source = get_one_dep_source(dep_header.get_path(src),
env['DEP_SUFFIXES'], False,
dep_libs)
if dep_source is not None and dep_source not in result:
dep_source_set.add(dep_source)
return impl(dep_source_set, result, dep_libs)
if main is None:
return None
dep_source_set = impl({main}, set(), dep_libs)
# Sort the dependencies so their order is consistent. If the order changes
# from one build to the next, SCons will rebuild the target unnecessarily
# because the dependency order changed.
result = [elem for elem in dep_source_set]
result.sort(key=lambda elem: str(elem))
return result
def remove_self_dependency(target, source):
new_source = []
for s in source:
if s.get_abspath() != target.get_abspath():
new_source.append(s)
return new_source
# Execute the test and check that the return code is 0.
def run_test(target, source, env):
for test in source:
try:
subprocess.check_call(test.get_path())
except subprocess.CalledProcessError:
sys.stderr.write('Test failed\n')
sys.exit(1)
def specify_generated_file(in_node, out_node, header_node, script_node,
script_abs_node):
# Tell SCons how to generate file.
out_path = out_node.get_path()
in_path = in_node.get_path()
script_path = script_node.get_path()
script_abs_path = script_abs_node.get_path()
env.Command(out_path, [in_path, script_path],
script_abs_path + " -g < $SOURCE > $TARGET")
# Specify that header depends on file generated during the build. For
# instance, 'dory/build_id.h' depends on 'dory/build_id.c'. This
# information is taken into account when dependencies are computed.
generated_source_map[header_node.get_path()] = out_node
# Input parameter 'headers' is a list of header files for a library 'lib' that
# is built by our build system. The effect we want to achieve here is as
# follows. If source file foo.cc #includes one of the headers for 'lib', then
# 'lib' will be added as a dependency of foo.cc.
def specify_lib_headers(lib, headers):
for h in headers:
lib_header_map[h.get_path()] = lib
def third_party_build_dir():
return Dir('#').Dir('out').Dir('third_party_build')
def third_party_out_dir(dir_name):
return Dir(third_party_build_dir().get_path()).Dir(dir_name)
def make_third_party_build_cmd(dir_name):
root = Dir('#')
dir_1 = third_party_build_dir()
dir_2 = root.Dir('src').Dir('third_party').Dir(dir_name)
dir_3 = third_party_out_dir(dir_name)
script = dir_3.File('build_' + dir_name)
return 'mkdir -p ' + dir_1.get_path() + ' && cp -a ' + dir_2.get_path() + \
' ' + dir_1.get_path() + ' && cd ' + dir_3.get_path() + \
' && ' + script.get_path()
# remove duplicates from list 'a' while preserving ordering of remaining items
def dedupe(a):
result = []
for x in a:
if x not in result:
result.append(x)
return result
def build_ext_lib_matchers(ext_lib_deps):
result = []
for dep in ext_lib_deps:
result.append([re.compile(dep[0]), dep[1]])
return result
def compute_ext_dep_libs(path, matchers):
libs = []
for m in matchers:
if m[0].match(path) is not None:
libs += m[1]
return dedupe(libs)
specify_generated_file(Dir('dory').File('build_id.c.in'),
Dir('dory').File('build_id.c'), Dir('dory').File('build_id.h'),
Dir('dory').Dir('scripts').File('gen_version'),
src.Dir('dory').Dir('scripts').File('gen_version'))
specify_generated_file(Dir('dory').Dir('client').File('build_id.c.in'),
Dir('dory').Dir('client').File('build_id.c'),
Dir('dory').Dir('client').File('build_id.h'),
Dir('dory').Dir('scripts').File('gen_version'),
src.Dir('dory').Dir('scripts').File('gen_version'))
specify_lib_headers(Dir('dory').Dir('client').File('libdory_client.a'),
[Dir('dory').Dir('client').File('dory_client.h')])
googletest_dir = 'googletest'
googletest_lib_1 = third_party_out_dir(googletest_dir).File('libgtest.a')
googletest_lib_2 = third_party_out_dir(googletest_dir).File('libgtest_main.a')
env.Command(googletest_lib_1, "", make_third_party_build_cmd(googletest_dir))
env.Command(googletest_lib_2, "", make_third_party_build_cmd(googletest_dir))
lz4_dir = 'lz4'
lz4_lib = third_party_out_dir(lz4_dir).File('liblz4.a')
env.Command(lz4_lib, "", make_third_party_build_cmd(lz4_dir))
common_prog_libs = env['PROG_LIBS'] + [lz4_lib]
common_test_libs = dedupe(
[googletest_lib_1, googletest_lib_2, lz4_lib, 'pthread'] + \
common_prog_libs)
ext_lib_matchers = build_ext_lib_matchers(env['EXT_LIB_DEPS'])
# For each given target path, try to build it.
for target_path in BUILD_TARGETS:
target = cwd.File(target_path)
target_rel_path = target.get_path(out)
ext = os.path.splitext(target_path)[1]
# Validate that the file kind is supported.
if ext not in exts:
sys.stderr.write(
'Invalid extension: "{ext}" (choose from {exts})'.format(
ext=ext,
exts=', '.join(['"{ext}"'.format(ext=ext) for ext in exts])) +
'\n')
sys.exit(1)
dep_libs = set([])
source = {env['PROGSUFFIX'] : lambda:
get_dep_sources(get_one_dep_source(target_path,
env['DEP_SUFFIXES'], True, dep_libs), dep_libs),
env['LIBSUFFIX'] : lambda:
get_dep_sources(get_one_dep_source(target_path,
env['DEP_SUFFIXES'], True, dep_libs), dep_libs),
env['SHLIBSUFFIX']: lambda:
get_dep_sources(get_one_dep_source(target_path,
env['DEP_SUFFIXES'], True, dep_libs), dep_libs),
env['OBJSUFFIX'] : lambda: get_one_dep_source(
target_path, env['DEP_SUFFIXES'], True, dep_libs),
env['SHOBJSUFFIX']: lambda: get_one_dep_source(
target_path, env['DEP_SUFFIXES'], True, dep_libs),
env['TESTSUFFIX'] : lambda:
get_dep_sources(get_one_dep_source(target_path, ['.test.cc'],
True, dep_libs), dep_libs)
}[ext]()
if source is None:
sys.stderr.write('Source for target ' + target_path + ' not found\n')
sys.exit(1)
# When computing dependencies for a library to be built such as libfoo.so,
# if libfoo.c #includes a header for the library being built (as specified
# by specify_lib_headers() above) then this will cause libfoo.so to be
# mistakenly listed as a dependency of itself. Here we fix that problem.
if type(source) is list:
source = remove_self_dependency(target, source)
# Add to BUILD_TARGETS all libraries that the current target depends on.
for d in dep_libs:
dep_path = d.get_path(cwd)
if dep_path not in BUILD_TARGETS:
BUILD_TARGETS.append(dep_path)
ext_dep_libs = compute_ext_dep_libs(target_rel_path, ext_lib_matchers)
dep_lib_array = [lib for lib in dep_libs]
prog_libs = common_prog_libs + ext_dep_libs + dep_lib_array
test_libs = common_test_libs + ext_dep_libs + dep_lib_array
# Choose the correct builder based on the target_path's extension.
{env['PROGSUFFIX'] : functools.partial(env.Program, LIBS=prog_libs),
env['LIBSUFFIX'] : env.Library,
env['SHLIBSUFFIX']: env.SharedLibrary,
env['OBJSUFFIX'] : env.Object,
env['SHOBJSUFFIX']: env.SharedObject,
env['TESTSUFFIX'] : functools.partial(env.Program, LIBS=test_libs)
}[ext](target, source)
# If target is a unit test, execute it.
test = None
if GetOption('test') and ext == env['TESTSUFFIX']:
test = env.Command(None, target,
Action(run_test, 'Running test: $SOURCE'))
env.Alias(target_path, [target, test])