-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-ci.py
647 lines (586 loc) · 21.2 KB
/
run-ci.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import argparse
import json
import os
import shutil
import sys
from pathlib import Path
from typing import List, Optional
import cli_ui as ui # noqa
import tankerci
import tankerci.conan
import tankerci.cpp
import tankerci.git
import tankerci.gitlab
from tankerci.build_info import DepsConfig
from tankerci.conan import Profile, TankerSource
TARGET_LIST = [
"armv7-linux-androideabi",
"aarch64-linux-android",
"x86_64-linux-android",
"i686-linux-android",
"aarch64-apple-ios",
"aarch64-apple-ios-sim",
"aarch64-apple-darwin",
"x86_64-apple-ios",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
"x86_64-pc-windows-msvc",
# special one, it is the same as msvc, but the import lib is renamed to libctanker.a
"x86_64-pc-windows-gnu",
]
NDK_ARCH_TARGETS = {
"armv7": "arm-linux-androideabi",
"armv8": "aarch64-linux-android",
"x86_64": "x86_64-linux-android",
"x86": "i686-linux-android",
}
CLANG_RT_ARCH_TARGETS = {
"armv7": "arm-android",
"armv8": "aarch64-android",
"x86_64": "x86_64-android",
"x86": "i686-android",
}
def profile_to_rust_target(platform: str, arch: str, sdk: Optional[str]) -> str:
if platform == "Android":
if arch == "armv7":
return "armv7-linux-androideabi"
elif arch == "armv8":
return "aarch64-linux-android"
elif arch == "x86_64":
return "x86_64-linux-android"
elif arch == "x86":
return "i686-linux-android"
elif platform == "Macos":
if arch == "x86_64":
return "x86_64-apple-darwin"
elif arch == "armv8":
return "aarch64-apple-darwin"
elif platform == "iOS":
if arch == "armv8":
if sdk == "iphonesimulator":
return "aarch64-apple-ios-sim"
else:
return "aarch64-apple-ios"
elif arch == "x86_64":
return "x86_64-apple-ios"
elif platform == "Linux":
return "x86_64-unknown-linux-gnu"
elif platform == "Windows":
return "x86_64-pc-windows-msvc"
raise Exception(f"Unsupported target architecture: {platform}-{arch}")
def get_android_bin_path() -> Path:
tankerci.run(
"conan",
"install",
"android-ndk/r26@",
"--profile:host",
"linux-x86_64",
"--profile:build",
str(tankerci.conan.get_build_profile()),
)
_, out = tankerci.run_captured(
"conan",
"info",
"android-ndk/r26@",
"--profile",
"linux-x86_64",
"--profile:build",
str(tankerci.conan.get_build_profile()),
"--json",
"--paths",
)
try:
info = json.loads(out)
package_path = Path(info[0]["package_folder"])
bin_path = package_path / "bin/toolchains/llvm/prebuilt/linux-x86_64/bin"
return bin_path
except (json.JSONDecodeError, KeyError, IndexError):
if out:
ui.error(f"Failed to parse output: {out}")
raise
def bind_gen(
*, header_source: Path, output_file: Path, include_path: Path, dynamic_loading: bool
) -> None:
args = []
if dynamic_loading:
args += [
"--dynamic-loading",
"ctanker_api",
]
tankerci.run(
"bindgen",
*args,
"--no-layout-tests",
str(header_source),
"-o",
str(output_file),
"--",
"-I",
str(include_path),
)
class Builder:
def __init__(
self, *, src_path: Path, build_profile: Profile, host_profile: Profile
):
self.src_path = src_path
self.host_profile = host_profile
self.build_profile = build_profile
self.platform = tankerci.conan.get_profile_key(
"settings.os", str(host_profile[0])
)
self.sdk = None
if self.platform == "iOS":
self.sdk = tankerci.conan.get_profile_key(
"settings.os.sdk", str(host_profile[0])
)
self.arch = tankerci.conan.get_profile_key(
"settings.arch", str(host_profile[0])
)
self.target_triplet = profile_to_rust_target(self.platform, self.arch, self.sdk)
@property
def _is_android_target(self) -> bool:
return self.platform == "Android"
@property
def _is_ios_target(self) -> bool:
return self.platform == "iOS"
@property
def _is_windows_target(self) -> bool:
return self.platform == "Windows"
@property
def _is_host_target(self) -> bool:
return not (self._is_android_target or self._is_ios_target)
def _copy_includes(self, package_path: Path, depsConfig: DepsConfig) -> None:
package_include = package_path / "include"
if package_include.exists():
shutil.rmtree(package_include)
package_include.mkdir(parents=True)
include_path = Path(depsConfig["tanker"].include_dirs[0])
dest_include_path = package_include / "ctanker"
for header in include_path.glob("**/*"):
if header.is_dir():
continue
rel_dir = header.parent.relative_to(include_path)
header_dest_dir = dest_include_path / rel_dir
header_dest_dir.mkdir(parents=True, exist_ok=True)
ui.info_2(header, "->", header_dest_dir)
shutil.copy(header, header_dest_dir)
# Import only soft float128 builtins from compiler-rt
# This is necessary on 64bit android archs, as Clang doesn't build them by default,
# and Google's NDK distribution doesn't take care of that either...
@staticmethod
def _armerge_soft_float128_compiler_rt_builtins(
compiler_rt_lib: Path, output_path: Path, env: dict[str, str]
) -> None:
f128_builtins = [
"__addtf3",
"__subtf3",
"__multf3",
"__divtf3",
"__negtf2",
"__extenddftf2",
"__extendsftf2",
"__trunctfdf2",
"__trunctfsf2",
"__fixdfti",
"__fixsfti",
"__fixunsdfti",
"__fixunssfti",
"__fixtfdi",
"__fixtfsi",
"__fixtfti",
"__fixunstfdi",
"__fixunstfsi",
"__fixunstfti",
"__floattidf",
"__floattisf",
"__floatuntidf",
"__floatuntisf",
"__floatditf",
"__floatsitf",
"__floattitf",
"__floatunditf",
"__floatunsitf",
"__floatuntitf",
"__cmptf2",
"__unordtf2",
"__eqtf2",
"__getf2",
"__gttf2",
"__letf2",
"__lttf2",
"__netf2",
"__powitf2",
"__multc3",
"__divtc3",
]
keep_symbol_args = [e for sym_name in f128_builtins for e in ["-k", sym_name]]
tankerci.run(
"armerge",
*keep_symbol_args,
"--output",
str(output_path.relative_to(Path.cwd())),
str(compiler_rt_lib),
shell=False,
env=env,
)
def _merge_all_libs(
self, depsConfig: DepsConfig, package_path: Path, native_path: Path
) -> None:
with tankerci.working_directory(package_path):
env = os.environ.copy()
if self._is_android_target:
android_bin_path = get_android_bin_path()
env["LD"] = str(android_bin_path / "ld.lld")
env["OBJCOPY"] = str(android_bin_path / "llvm-objcopy")
ui.info(f'Using {env["LD"]}')
ui.info(f'Using {env["OBJCOPY"]}')
if self._is_ios_target:
env["ARMERGE_LDFLAGS"] = "-bitcode_bundle"
libctanker_a = Path("libctanker.a")
if libctanker_a.exists():
libctanker_a.unlink()
cxx_package_libs = package_path / "cxx_deplibs"
cxx_package_libs.mkdir(parents=True, exist_ok=True)
if self._is_android_target:
ndk_arch = NDK_ARCH_TARGETS[self.arch]
android_lib_path = android_bin_path / f"../sysroot/usr/lib/{ndk_arch}"
# Starting with NDK r23, Google in its infinite wisdom has decided to make things
# more interesting
# libgcc is gone, and now we use clang's libcxx and compiler-rt.
# Unfortunately, the libcxx_static.a is currently missing soft float128 builtins
# for 64bit archs (See https://reviews.llvm.org/D53608 and
# https://github.com/llvm/llvm-project/issues/51395)
# It is possible to find those symbols in the separate libclang_rt.builtins libs
# However, we can't pull in all of rt.builtins, or we will have duplicate symbols
# and fail linking
if self.arch in ["x86_64", "armv8"]:
compiler_rt_arch = CLANG_RT_ARCH_TARGETS[self.arch]
compiler_rt_dir = android_bin_path / "../lib/clang/17/lib/linux/"
compiler_rt_lib = (
compiler_rt_dir / f"libclang_rt.builtins-{compiler_rt_arch}.a"
)
out_path = (
cxx_package_libs
/ f"libclang_rt.builtins.float128-{compiler_rt_arch}.a"
)
self._armerge_soft_float128_compiler_rt_builtins(
compiler_rt_lib, out_path, env
)
for lib in android_lib_path.glob("*.a"):
# Rust already links some (non-C++) NDK libs, skip to avoid duplicate symbols
# Also we already link rt builtins above, so we don't need/can't have these
skipped = [
"libc.a",
"libm.a",
"libdl.a",
"libz.a",
"libstdc++.a",
"libunwind.a",
"libcompiler_rt-extras.a",
]
if lib.is_dir() or lib.name in skipped:
continue
ui.info_2(
"Android NDK",
ndk_arch,
"sysroot",
lib.name,
"->",
cxx_package_libs,
)
shutil.copy(lib, cxx_package_libs)
package_libs = package_path / "deplibs"
package_libs.mkdir(parents=True, exist_ok=True)
for lib_path in depsConfig.all_lib_paths():
ios_libcxx = ["libc++.a", "libc++abi.a"]
if self._is_ios_target and lib_path.name in ios_libcxx:
dest_dir = cxx_package_libs
else:
dest_dir = package_libs
ui.info_1("copying", lib_path, "to", dest_dir)
shutil.copy(lib_path, dest_dir)
if self._is_android_target or self._is_ios_target:
tankerci.run(
"armerge --keep-symbols '.*' --output libcxx_vendored.a cxx_deplibs/*.a",
shell=True,
env=env,
)
shutil.copy("libcxx_vendored.a", native_path)
# Apple prefixes symbols with '_'
tankerci.run(
"armerge --keep-symbols '^_?tanker_.*' --output libctanker.a"
" deplibs/*.a",
shell=True,
env=env,
)
if self._is_android_target:
llvm_strip = android_bin_path / "llvm-strip"
# HACK: Android forces debug symbols, we need to patch the
# toolchain to remove them. Until then, strip them here.
tankerci.run(
str(llvm_strip), "--strip-debug", "--strip-unneeded", "libctanker.a"
)
shutil.copy("libctanker.a", native_path)
def _prepare_profile(self) -> None:
conan_out = self.src_path / "conan" / "out" / str(self.host_profile)
package_path = conan_out / "package"
depsConfig = DepsConfig(
self.src_path / "conan" / "out" / str(self.host_profile)
)
self._copy_includes(package_path, depsConfig)
native_path = self.src_path / "native" / self.target_triplet
if native_path.exists():
shutil.rmtree(native_path)
native_path.mkdir(parents=True)
if self._is_windows_target:
for lib_path in depsConfig.all_lib_paths():
ui.info_1("copying", lib_path, "to", native_path)
shutil.copy(lib_path, native_path)
# handle mingw target
mingw_path = self.src_path / "native" / "x86_64-pc-windows-gnu"
# prepare is called twice, so ignore when dirs exists
shutil.copytree(native_path, mingw_path, dirs_exist_ok=True)
else:
self._merge_all_libs(depsConfig, package_path, native_path)
include_path = package_path / "include" / "ctanker"
bind_gen(
header_source=include_path / "ctanker.h",
output_file=native_path / "ctanker.rs",
include_path=include_path,
dynamic_loading=self._is_windows_target,
)
if self._is_windows_target:
shutil.copyfile(native_path / "ctanker.rs", mingw_path / "ctanker.rs")
def prepare(
self,
update: bool,
tanker_source: TankerSource,
tanker_ref: Optional[str] = None,
) -> None:
tanker_deployed_ref = tanker_ref
if tanker_source == TankerSource.DEPLOYED and not tanker_ref:
tanker_deployed_ref = "tanker/latest-stable@"
tankerci.conan.install_tanker_source(
tanker_source,
output_path=Path("conan") / "out",
host_profiles=[self.host_profile],
build_profile=self.build_profile,
update=update,
tanker_deployed_ref=tanker_deployed_ref,
)
self._prepare_profile()
def _cargo(self, subcommand: str, *extra_args: str) -> None:
env = os.environ.copy()
if self._is_android_target:
android_bin_path = get_android_bin_path()
env["CC"] = str(android_bin_path / "clang")
env["AR"] = str(android_bin_path / "llvm-ar")
ui.info(f'Using {env["CC"]}')
ui.info(f'Using {env["AR"]}')
tankerci.run(
"cargo",
subcommand,
"--target",
self.target_triplet,
*extra_args,
cwd=self.src_path,
env=env,
)
if self._is_windows_target:
tankerci.run(
"cargo",
subcommand,
"--target",
"x86_64-pc-windows-gnu",
*extra_args,
cwd=self.src_path,
)
def build(self) -> None:
if not self._is_host_target:
if self.target_triplet == "aarch64-apple-ios-sim":
tankerci.run(
"cargo",
"+nightly",
"build",
"-Z",
"build-std",
"--target",
self.target_triplet,
cwd=self.src_path,
)
return
self._cargo("build")
def test(self) -> None:
self.build()
if not self._is_host_target:
ui.info(
str(self.host_profile), "is a cross-compiled target, skipping tests"
)
return
tankerci.run("cargo", "fmt", "--", "--check", cwd=self.src_path)
tankerci.run(
"cargo",
"clippy",
"--all-targets",
"--",
"--deny",
"warnings",
"--allow",
"unknown-lints",
cwd=self.src_path,
)
if self._is_windows_target:
shutil.copy(
Path("native") / self.target_triplet / "ctanker.dll",
Path("target") / "debug/deps",
)
self._cargo("test")
self._cargo(
"test", "--no-default-features"
) # Also test without HTTP reverse bindings on desktops
def prepare(
tanker_source: TankerSource,
*,
profiles: List[Profile],
update: bool = False,
tanker_ref: Optional[str] = None,
) -> None:
build_profile = tankerci.conan.get_build_profile()
for host_profile in profiles:
builder = Builder(
src_path=Path.cwd(), host_profile=host_profile, build_profile=build_profile
)
builder.prepare(update, tanker_source, tanker_ref)
def build(
*,
profiles: List[Profile],
test: bool = False,
) -> None:
build_profile = tankerci.conan.get_build_profile()
if os.environ.get("CI"):
os.environ["RUSTFLAGS"] = "-D warnings"
for host_profile in profiles:
builder = Builder(
src_path=Path.cwd(), host_profile=host_profile, build_profile=build_profile
)
# build is implied with test
if test:
builder.test()
else:
builder.build()
def deploy(args: argparse.Namespace) -> None:
compiled_targets = [p.name for p in Path("native").iterdir() if p.is_dir()]
missing_targets = [
target for target in TARGET_LIST if target not in compiled_targets
]
if missing_targets:
ui.fatal("Aborting deploy because of missing targets:", *missing_targets)
version = args.version
registry = args.registry
tankerci.bump_files(version)
tankerci.run("cargo", "publish", "--allow-dirty", f"--registry={registry}")
def matching_downstream_branch(repo: str) -> str:
current_ref = os.environ.get(
"UPSTREAM_COMMIT_REF_NAME", os.environ["CI_COMMIT_REF_NAME"]
)
if tankerci.git.remote_branch_exists(current_ref, repo):
return current_ref
else:
return "master"
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--isolate-conan-user-home",
action="store_true",
dest="home_isolation",
default=False,
)
parser.add_argument("--remote", default="artifactory")
subparsers = parser.add_subparsers(title="subcommands", dest="command")
build_parser = subparsers.add_parser("build")
build_parser.add_argument(
"--profile",
dest="profiles",
action="append",
required=True,
nargs="+",
type=str,
)
build_parser.add_argument("--test", action="store_true")
prepare_parser = subparsers.add_parser("prepare")
prepare_parser.add_argument(
"--profile",
dest="profiles",
action="append",
required=True,
nargs="+",
type=str,
)
prepare_parser.add_argument("--tanker-ref")
prepare_parser.add_argument(
"--use-tanker",
type=TankerSource,
default=TankerSource.EDITABLE,
dest="tanker_source",
)
prepare_parser.add_argument(
"--update", action="store_true", default=False, dest="update"
)
download_artifacts_parser = subparsers.add_parser("download-artifacts")
download_artifacts_parser.add_argument("--project-id", required=True)
download_artifacts_parser.add_argument("--pipeline-id", required=True)
download_artifacts_parser.add_argument("--job-name", required=True)
deploy_parser = subparsers.add_parser("deploy")
deploy_parser.add_argument("--version", required=True)
deploy_parser.add_argument("--registry", required=True)
write_bridge_dotenv = subparsers.add_parser("write-bridge-dotenv")
write_bridge_dotenv.add_argument(
"--downstream", dest="downstreams", action="append", required=True
)
args = parser.parse_args()
user_home = None
if args.home_isolation:
user_home = Path.cwd() / ".cache" / "conan" / args.remote
if args.command == "build":
profiles = [Profile(p) for p in args.profiles]
with tankerci.conan.ConanContextManager(
[args.remote, "conancenter"], conan_home=user_home
):
build(
profiles=profiles,
test=args.test,
)
elif args.command == "deploy":
deploy(args)
elif args.command == "prepare":
with tankerci.conan.ConanContextManager(
[args.remote, "conancenter"], conan_home=user_home
):
profiles = [Profile(p) for p in args.profiles]
prepare(
args.tanker_source,
profiles=profiles,
update=args.update,
tanker_ref=args.tanker_ref,
)
elif args.command == "download-artifacts":
tankerci.gitlab.download_artifacts(
project_id=args.project_id,
pipeline_id=args.pipeline_id,
job_name=args.job_name,
)
elif args.command == "write-bridge-dotenv":
branches = [matching_downstream_branch(repo) for repo in args.downstreams]
keys = [
repo.replace("-", "_").upper() + "_BRIDGE_BRANCH"
for repo in args.downstreams
]
env_list = "\n".join([f"{k}={v}" for k, v in zip(keys, branches)])
with open("bridge.env", "a+") as f:
f.write(env_list)
ui.info(env_list)
else:
parser.print_help()
sys.exit(1)
if __name__ == "__main__":
main()