forked from conda-forge/cdt-builds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_cdt_recipes.py
438 lines (382 loc) · 14.5 KB
/
gen_cdt_recipes.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import os
import subprocess
from concurrent.futures import ThreadPoolExecutor, as_completed
import shutil
import collections
import tqdm
import click
from ruamel.yaml import YAML
from cdt_config import (
LEGACY_CDT_PATH,
LEGACY_CUSTOM_CDT_PATH,
CDT_PATH,
CUSTOM_CDT_PATH,
)
from render_readme import render_readme
def _is_changed_or_not_tracked(pth):
ctracked = subprocess.run(
"git ls-files --error-unmatch %s" % pth,
shell=True,
capture_output=True,
)
if ctracked.returncode != 0:
return True
else:
cdiff = subprocess.run(
"git diff --exit-code -s %s" % pth,
shell=True,
capture_output=True,
)
if cdiff.returncode != 0:
return True
else:
return False
def _gen_dist_arch_str(arch, dist):
return "%s-%s" % (dist.replace("ent", ""), arch)
def _make_cdt_recipes(*, extra, cdt_path, arch_dist_tuples, cdts, exec, force):
futures = {}
for arch, dist in arch_dist_tuples:
for cdt, cfg in cdts.items():
if cfg["custom"]:
continue
if (
"skipped_cdts" in cfg
and _gen_dist_arch_str(arch, dist) in cfg["skipped_cdts"]
):
continue
if cfg.get("recursive", True):
_extra = extra + " --recursive"
else:
_extra = extra
_pth = os.path.join(
cdt_path,
cdt.lower() + "-" + _gen_dist_arch_str(arch, dist),
)
if not force and os.path.exists(_pth):
continue
futures[exec.submit(
subprocess.run,
(
f"python rpm.py {cdt} --output-dir={cdt_path} "
+ f"--architecture={arch} --distro={dist} "
+ _extra
),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
shell=True
)] = {"cdt": cdt, "arch": arch, "dist": dist}
return futures
def _cleanup_custom_cdt_overlaps(*, cdt_path, arch_dist_tuples, cdts):
for arch, dist in arch_dist_tuples:
for cdt, cfg in cdts.items():
if not cfg["custom"]:
continue
pth = os.path.join(
cdt_path,
cdt.lower() + "-" + dist.replace("ent", "") + "-" + arch,
)
if os.path.exists(pth):
try:
subprocess.run(
"git rm -r -f --ignore-unmatch " + pth,
shell=True,
capture_output=True,
check=True,
)
subprocess.run(
"rm -rf " + pth,
shell=True,
capture_output=True,
check=True,
)
except subprocess.CalledProcessError as e:
print(
"WARNING: error removing autogenerated "
"recipe for custom CDT %s: %s" % (cdt, repr(e))
)
def _clear_gen_cdts(pth):
try:
subprocess.run(
"git rm -r -f --ignore-unmatch " + pth + "/*-*-*",
shell=True,
capture_output=True,
check=True,
)
subprocess.run(
"rm -rf " + pth + "/*-*-*",
shell=True,
capture_output=True,
check=True,
)
except subprocess.CalledProcessError as e:
print("WARNING: error removing autogenerated CDTs at %s: %s" % (pth, repr(e)))
def _fix_cdt_licenses(*, cdts, arch_dist_tuples, cdt_path):
print("fixing CDT licenses for path '%s'..." % cdt_path, flush=True)
for arch, dist in arch_dist_tuples:
for cdt, cfg in cdts.items():
pth = os.path.join(
cdt_path,
cdt.lower() + "-" + dist.replace("ent", "") + "-" + arch,
)
if 'license_file' in cfg and os.path.exists(pth):
if cfg["license_file"] is None:
pass
elif isinstance(cfg["license_file"], collections.abc.MutableSequence):
for lf in cfg['license_file']:
shutil.copy2(lf, os.path.join(pth, "."))
else:
shutil.copy2(cfg['license_file'], os.path.join(pth, "."))
try:
yaml = YAML(typ="jinja2")
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.width = 320
with open(os.path.join(pth, "meta.yaml"), "r") as fp:
meta = yaml.load(fp)
except Exception:
print("ERROR: could not adjust license for %s" % pth)
continue
if cfg["license_file"] is None:
if "license_file" in meta["about"]:
meta["about"].pop("license_file")
elif isinstance(cfg["license_file"], collections.abc.MutableSequence):
meta["about"]["license_file"] = [
os.path.basename(lf)
for lf in cfg["license_file"]
]
else:
meta["about"]["license_file"] = os.path.basename(
cfg["license_file"]
)
with open(os.path.join(pth, "meta.yaml"), "w") as fp:
meta = yaml.dump(meta, fp)
def _fix_cdt_deps(*, cdts, arch_dist_tuples, cdt_path):
print("adjusting CDT deps for path '%s'..." % cdt_path, flush=True)
for arch, dist in arch_dist_tuples:
for cdt, cfg in cdts.items():
pth = os.path.join(
cdt_path,
cdt.lower() + "-" + dist.replace("ent", "") + "-" + arch,
)
if 'dep_remove' in cfg and os.path.exists(pth):
try:
yaml = YAML(typ="jinja2")
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.width = 320
with open(os.path.join(pth, "meta.yaml"), "r") as fp:
meta = yaml.load(fp)
except Exception:
print("ERROR: could not adjust license for %s" % pth)
continue
if "requirements" in meta:
for sec in ["build", "host", "run"]:
if sec in meta["requirements"]:
new_deps = []
for dep in meta["requirements"][sec]:
if not any(
dep.startswith(d + "-cos")
for d in cfg["dep_remove"]
):
new_deps.append(dep)
meta["requirements"][sec] = new_deps
with open(os.path.join(pth, "meta.yaml"), "w") as fp:
meta = yaml.dump(meta, fp)
def _fix_cdt_builds(*, cdts, arch_dist_tuples, cdt_path):
print("adjusting CDT builds for path '%s'..." % cdt_path, flush=True)
for arch, dist in arch_dist_tuples:
shortdist = dist.replace("ent", "")
distarch = dist.replace("ent", "") + "-" + arch
for cdt, cfg in cdts.items():
pth = os.path.join(
cdt_path,
cdt.lower() + "-" + distarch,
)
build_pth = os.path.join(pth, "build.sh")
if (
'build_append' in cfg
and os.path.exists(pth)
and (
distarch in cfg["build_append"]
or shortdist in cfg["build_append"]
or arch in cfg["build_append"]
or "all" in cfg["build_append"]
)
):
if distarch in cfg["build_append"]:
extra_build = cfg["build_append"][distarch]
elif shortdist in cfg["build_append"]:
extra_build = cfg["build_append"][shortdist]
elif arch in cfg["build_append"]:
extra_build = cfg["build_append"][arch]
elif "all" in cfg["build_append"]:
extra_build = cfg["build_append"]["all"]
else:
raise RuntimeError("could not get build append for %s!" % cdt)
with open(build_pth, "r") as fp:
build_str = fp.read()
if (
"# CONDA-FORGE BUILD APPEND" not in build_str
and not build_str.strip().endswith("# CDT BUILD APPENDED")
):
with open(build_pth, "w") as fp:
for line in build_str.splitlines():
fp.write(line + "\n")
if "# START OF INSERTED BUILD APPENDS" == line:
fp.write("\n\n# CONDA-FORGE BUILD APPEND\n")
fp.write(extra_build + "\n")
@click.command()
@click.option(
"--force", default=False, is_flag=True,
help="Forcibly regenerate all CDT recipes."
)
@click.option(
"--no-legacy", default=False, is_flag=True,
help="Do not generate the old-style, legacy CDTs in the legacy_* folders."
)
@click.option(
"--fast", default=False, is_flag=True,
help="Use a global src cache. May fail due to race conditions."
)
def _main(force, no_legacy, fast):
"""
Generate all CDT recipes.
"""
yaml = YAML()
with open("cdt_slugs.yaml", "r") as fp:
cdts = yaml.load(fp)
if not no_legacy:
os.makedirs(LEGACY_CDT_PATH, exist_ok=True)
os.makedirs(LEGACY_CUSTOM_CDT_PATH, exist_ok=True)
os.makedirs(CDT_PATH, exist_ok=True)
os.makedirs(CUSTOM_CDT_PATH, exist_ok=True)
print("generating CDT recipes...")
futures = {}
with ThreadPoolExecutor(max_workers=16) as exec:
if not no_legacy:
# legacy CDTs for the old compiler sysroots
# if force:
# _clear_gen_cdts(LEGACY_CDT_PATH)
extra = "--conda-forge-style"
if fast:
extra += " --use-global-cache"
arch_dist_tuples = [
("x86_64", "centos6"),
("aarch64", "centos7"),
("ppc64le", "centos7")
]
futures.update(
_make_cdt_recipes(
extra=extra,
cdt_path=LEGACY_CDT_PATH,
arch_dist_tuples=arch_dist_tuples,
cdts=cdts,
exec=exec,
force=force)
)
# new CDTs for the new compilers with a single sysroot
# if force:
# _clear_gen_cdts(CDT_PATH)
extra = "--conda-forge-style --single-sysroot"
if fast:
extra += " --use-global-cache"
arch_dist_tuples = [
("x86_64", "centos6"), ("x86_64", "centos7"),
("aarch64", "centos7"), ("ppc64le", "centos7")
]
futures.update(
_make_cdt_recipes(
extra=extra,
cdt_path=CDT_PATH,
arch_dist_tuples=arch_dist_tuples,
cdts=cdts,
exec=exec,
force=force)
)
for fut in tqdm.tqdm(as_completed(futures), total=len(futures)):
c = fut.result()
pkg = futures[fut]
nm = "-".join([pkg["cdt"], pkg["dist"].replace("ent", ""), pkg["arch"]])
if (
c.returncode != 0
or "WARNING: Did not find package called (or another one providing)" in c.stdout # noqa
):
tqdm.tqdm.write("WARNING: making CDT recipe %s failed!" % nm)
tqdm.tqdm.write(c.stdout)
if (
"WARNING: could not find a suitable license " in c.stdout
):
for line in c.stdout.splitlines():
if "WARNING: could not find a suitable license " in line:
_found_cdt = None
for _cdt in cdts:
if _cdt.lower() in line.lower():
if _found_cdt is None:
_found_cdt = _cdt
elif len(_cdt) > len(_found_cdt):
_found_cdt = _cdt
if _found_cdt is not None:
if "license_file" not in cdts[_found_cdt]:
tqdm.tqdm.write(line.strip())
else:
tqdm.tqdm.write(line.strip())
# finally, we have to clean up any CDTs marked as custom that happened to be
# made by the templates again
if not no_legacy:
# legacy CDTs for the old compiler sysroots
arch_dist_tuples = [
("x86_64", "centos6"),
("aarch64", "centos7"),
("ppc64le", "centos7")
]
_cleanup_custom_cdt_overlaps(
cdt_path=LEGACY_CDT_PATH,
arch_dist_tuples=arch_dist_tuples,
cdts=cdts)
_fix_cdt_licenses(
cdts=cdts,
arch_dist_tuples=arch_dist_tuples,
cdt_path=LEGACY_CDT_PATH
)
_fix_cdt_deps(
cdts=cdts,
arch_dist_tuples=arch_dist_tuples,
cdt_path=LEGACY_CDT_PATH
)
_fix_cdt_builds(
cdts=cdts,
arch_dist_tuples=arch_dist_tuples,
cdt_path=LEGACY_CDT_PATH
)
# new CDTs for the new compilers with a single sysroot
arch_dist_tuples = [
("x86_64", "centos6"), ("x86_64", "centos7"),
("aarch64", "centos7"), ("ppc64le", "centos7")
]
_cleanup_custom_cdt_overlaps(
cdt_path=CDT_PATH,
arch_dist_tuples=arch_dist_tuples,
cdts=cdts)
_fix_cdt_licenses(
cdts=cdts,
arch_dist_tuples=arch_dist_tuples,
cdt_path=CDT_PATH
)
_fix_cdt_deps(
cdts=cdts,
arch_dist_tuples=arch_dist_tuples,
cdt_path=CDT_PATH
)
_fix_cdt_builds(
cdts=cdts,
arch_dist_tuples=arch_dist_tuples,
cdt_path=CDT_PATH
)
# make the readme
render_readme()
print(
"finished generating CDTs - make sure to add any changes in the repo "
"via 'git add *' before making a commit!",
flush=True,
)
if __name__ == "__main__":
_main()