-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·404 lines (326 loc) · 10.4 KB
/
build.py
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
import subprocess
import os
import json
import functools
import argparse
import yaml
import itertools
def detectTarget(current_dir, special_targets, normal_targets, test_targets):
all_targets = special_targets + normal_targets + test_targets
def removePrefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix) :]
else:
return text
def check(target):
# First check if target is symbolic
if target in all_targets:
return target
# For non-symbolic targets, try to match with the path
target_dir = os.path.join(os.getcwd(), target)
if os.path.isdir(target_dir):
for normal in normal_targets:
normal_target_dir = os.path.join(current_dir, normal)
if os.path.samefile(target_dir, normal_target_dir):
return normal
for test in test_targets:
unprefixed_test = removePrefix(test, "test-")
test_target_dir = os.path.join(
current_dir, os.path.join(".jenkins", "tests"), unprefixed_test
)
if os.path.samefile(target_dir, test_target_dir):
return test
return None
return check
class TargetNormalizer:
def __init__(self):
self.targets_contain_creative = False
self.targets_contain_destructive = False
def run(self, targetCheck, creative_set, destructive_set, normal_set):
def allowed(target):
normalized = targetCheck(target)
if normalized:
if normalized in creative_set or normalized in normal_set:
self.targets_contain_creative = True
if self.targets_contain_destructive:
raise argparse.ArgumentTypeError(
"invalid choice: {} (mix of creative and destructive targets)".format(
normalized
)
)
if normalized in destructive_set:
self.targets_contain_destructive = True
if self.targets_contain_creative:
raise argparse.ArgumentTypeError(
"invalid choice: {} (mix of creative and destructive targets)".format(
normalized
)
)
return normalized
targetList = list(creative_set) + list(destructive_set) + list(normal_set)
args = {"value": target, "choices": ", ".join(map(repr, targetList))}
msg = "invalid choice: %(value)r (choose from %(choices)s)"
raise argparse.ArgumentTypeError(msg % args)
return allowed
target_normalizer = TargetNormalizer()
current_dir = os.path.dirname(os.path.abspath(__file__))
available_config = json.loads(
subprocess.check_output(
["./conan/parse-compilers.py", "--show-all-json"],
cwd=current_dir,
stderr=sys.stderr,
)
)
default_compiler = available_config["DEFAULT_COMPILER"]
available_compilers = available_config["AVAILABLE_COMPILERS"]
available_build_types = available_config["AVAILABLE_BUILD_TYPES"]
normal_targets = {}
test_targets = {}
special_creative_targets = {"all": [], "tests": [], "everything": []}
special_destructive_targets = {
"clean": [],
"buildclean": [],
"tidyclean": [],
"distclean": [],
}
special_targets = {**special_creative_targets, **special_destructive_targets}
target_dict = yaml.load(
open(os.path.join(current_dir, "targets.yaml")), Loader=yaml.FullLoader
)
for target_name, target_deps in target_dict.items():
if target_name.startswith("test-"):
test_targets[target_name] = target_deps
else:
normal_targets[target_name] = target_deps
def sorted_targets(targets):
return sorted(targets.keys())
def flatten(lst):
return functools.reduce(lambda a, b: a + b, lst, [])
make_list = list(map(sorted_targets, [special_targets, normal_targets, test_targets]))
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument(
"--verbose",
required=False,
dest="verbose",
help="Show verbose compilation information",
action="store_true",
)
parser.add_argument(
"--serial-build",
required=False,
dest="serial",
help="Build one package at a time",
action="store_true",
)
parser.add_argument(
"-b",
"--build-type",
choices=available_build_types,
default="relwithdebinfo",
nargs="?",
dest="build_type",
help="Type of build (default: relwithdebinfo)",
)
descr_compiler = """Type of the compiler (default: {})
Possible options:
{}
""".format(
default_compiler, "\n".join(available_compilers)
)
parser.add_argument(
"-c",
"--compiler",
choices=available_compilers,
default=default_compiler,
nargs="?",
dest="compiler",
metavar="COMPILER",
help=descr_compiler,
)
parser.add_argument(
"--available-options",
required=False,
dest="available_options",
action="store_true",
help=argparse.SUPPRESS,
)
parser.add_argument(
"--deps-only",
required=False,
dest="deps_only",
action="store_true",
help="Consider as target(s) the dependencies of the specified target(s)",
)
descr_target = """What targets to build (default: all)
Possible targets:
{}
""".format(
"\n\n".join(["\n".join(make_list_group) for make_list_group in make_list])
)
parser.add_argument(
"TARGET",
default="all",
nargs="*",
# choices=flatten(make_list),
metavar="TARGET",
type=target_normalizer.run(
detectTarget(
current_dir,
sorted_targets(special_targets),
sorted_targets(normal_targets),
sorted_targets(test_targets),
),
set(special_creative_targets.keys()),
set(special_destructive_targets.keys()),
set(normal_targets.keys()).union(set(test_targets.keys())),
),
help=descr_target,
)
parser.add_argument(
"--gen-tidy-info",
required=False,
dest="gen_tidy_info",
help="Generate information to be used by clang-tidy",
action="store_true",
)
parser.add_argument(
"--test-packages",
required=False,
dest="test_packages",
help="Run the unit-test suite for packages",
action="store_true",
)
results = parser.parse_args()
if results.available_options:
print(
json.dumps(
{
"DEFAULT_COMPILER": default_compiler,
"AVAILABLE_COMPILERS": available_compilers,
"AVAILABLE_BUILD_TYPES": available_build_types,
}
)
)
exit(0)
compiler = results.compiler
build_type = results.build_type
if isinstance(results.TARGET, str):
targets = [results.TARGET]
else:
targets = list(results.TARGET)
# Remove duplicates
targets = list(set(targets))
env = os.environ.copy()
env["DORY_INVOKED_FROM_BUILDSCRIPT"] = "1"
make_opt = []
make_opt_stderr = None
if not results.serial:
make_opt.append("-j")
if not results.verbose:
make_opt.append("-s")
make_opt_stderr = subprocess.DEVNULL
else:
env["DORY_BUILD_VERBOSITY"] = "1"
if not set(special_destructive_targets.keys()).isdisjoint(set(targets)):
cmd_destructive_targets = "make {} -C conan {}".format(
" ".join(make_opt), " ".join(targets)
)
ret = subprocess.call(
cmd_destructive_targets,
cwd=current_dir,
env=env,
shell=True,
)
exit(ret)
if "all" in targets:
targets.remove("all")
targets.extend(normal_targets.keys())
if "tests" in targets:
targets.remove("tests")
targets.extend(test_targets.keys())
if "everything" in targets:
targets.remove("everything")
targets.extend(normal_targets.keys())
targets.extend(test_targets.keys())
# Remove duplicates again
targets = list(set(targets))
if results.deps_only:
targets = list(set(flatten([target_dict[target] for target in targets])))
config = json.loads(
subprocess.check_output(
["./conan/parse-compilers.py", "-c", compiler, "-b", build_type, "--json"],
cwd=current_dir,
stderr=subprocess.DEVNULL,
)
)
CC = config["CC"]
env["CC"] = CC
CXX = config["CXX"]
env["CXX"] = CXX
COMPILER_PATH = config["PATH"]
if COMPILER_PATH:
env["PATH"] = "{}:".format(COMPILER_PATH) + env["PATH"]
def convert_target(target, compiler, buildtype, gen_tidy, test_pkg):
prefix = os.path.join(".deps", compiler, buildtype)
if gen_tidy and test_pkg:
return os.path.join(prefix, "{}.conantesttidy".format(target))
if gen_tidy and not test_pkg:
return os.path.join(prefix, "{}.conantidy".format(target))
if not gen_tidy and test_pkg:
return os.path.join(prefix, "{}.conantest".format(target))
return os.path.join(prefix, "{}.conandep".format(target))
def check_targets(targets):
def gather_check_targets(target, store):
if not target:
return
store.add(target)
[gather_check_targets(dep, store) for dep in target_dict[target]]
checks = set(["compiler-options"])
[gather_check_targets(target, checks) for target in targets]
return list(checks)
cmd_check_target_changes = "make {} -C conan".format(" ".join(make_opt))
ret = subprocess.call(
cmd_check_target_changes,
cwd=current_dir,
stderr=make_opt_stderr,
env=env,
shell=True,
)
if ret > 0:
exit(ret)
cmd_check_package_changes = "make {} -f .deps/Makefile.generated {}".format(
" ".join(make_opt),
" ".join(map(lambda trgt: "{}.check".format(trgt), check_targets(targets))),
)
cmd_build = "make {} -f .deps/Makefile.generated {}".format(
" ".join(make_opt),
" ".join(
map(
lambda trgt: convert_target(
trgt,
results.compiler,
results.build_type,
results.gen_tidy_info,
results.test_packages,
),
targets,
)
),
)
# The compilers need to be explicity set, as conan won't interfere with the
# build system. Therefore, make sure the CC/CXX flags match the conan profile.
ret = subprocess.call(
"{} && {}".format(
cmd_check_package_changes,
cmd_build,
),
cwd=current_dir,
env=env,
shell=True,
)
exit(ret)