-
Notifications
You must be signed in to change notification settings - Fork 7
/
builders.py
executable file
·1238 lines (1045 loc) · 48.9 KB
/
builders.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Builder instances for various targets."""
from pathlib import Path
from typing import cast, Dict, Iterator, List, Optional, Set
import contextlib
import os
import re
import shutil
import textwrap
import timer
import base_builders
import configs
import constants
import hosts
import mapfile
import multiprocessing
import paths
import utils
class SanitizerMapFileBuilder(base_builders.Builder):
name: str = 'sanitizer-mapfile'
config_list: List[configs.Config] = configs.android_configs()
def _build_config(self) -> None:
arch = self._config.target_arch
lib_dir = self.output_toolchain.clang_lib_dir / 'lib' / 'linux'
self._build_sanitizer_map_file('asan', arch, lib_dir, 'ASAN')
self._build_sanitizer_map_file('ubsan_standalone', arch, lib_dir, 'ASAN')
if super()._is_64bit():
self._build_sanitizer_map_file('tsan', arch, lib_dir, 'TSAN')
if arch == hosts.Arch.AARCH64:
self._build_sanitizer_map_file('hwasan', arch, lib_dir, 'ASAN')
@staticmethod
def _build_sanitizer_map_file(san: str, arch: hosts.Arch, lib_dir: Path, section_name: str) -> None:
lib_file = lib_dir / f'libclang_rt.{san}-{arch.llvm_arch}-android.so'
map_file = lib_dir / f'libclang_rt.{san}-{arch.llvm_arch}-android.map.txt'
mapfile.create_map_file(lib_file, map_file, section_name)
class Stage1Builder(base_builders.LLVMBuilder):
name: str = 'stage1'
install_dir: Path = paths.OUT_DIR / 'stage1-install'
build_android_targets: bool = False
build_extra_tools: bool = False
@property
def llvm_targets(self) -> Set[str]:
if self.build_android_targets:
return constants.HOST_TARGETS | constants.ANDROID_TARGETS
else:
if self._config.target_os.is_darwin:
return constants.DARWIN_HOST_TARGETS
else:
return constants.HOST_TARGETS
@property
def llvm_projects(self) -> Set[str]:
proj = {'clang', 'lld'}
# Need tools like clang-pseudo-gen and clang-tidy-confusable-chars-gen
# for Linux when cross-compiling for Windows.
proj.add('clang-tools-extra')
if self.build_lldb:
proj.add('lldb')
return proj
@property
def llvm_runtime_projects(self) -> Set[str]:
proj = {'compiler-rt', 'libcxx', 'libcxxabi'}
if isinstance(self._config, configs.LinuxMuslConfig):
# libcxx builds against libunwind when building for musl
proj.add('libunwind')
return proj
@property
def ldflags(self) -> List[str]:
ldflags = super().ldflags
# Use -static-libstdc++ to statically link the c++ runtime [1]. This
# avoids specifying self.toolchain.lib_dirs in rpath to find libc++ at
# runtime.
# [1] libc++ in our case, despite the flag saying -static-libstdc++.
ldflags.append('-static-libstdc++')
return ldflags
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['CLANG_ENABLE_ARCMT'] = 'OFF'
if not self.build_extra_tools:
defines['CLANG_ENABLE_STATIC_ANALYZER'] = 'OFF'
defines['LLVM_BUILD_TOOLS'] = 'ON'
# Do not build compiler-rt for Darwin. We don't ship host (or any
# prebuilt) runtimes for Darwin anyway. Attempting to build these will
# fail compilation of lib/builtins/atomic_*.c that only get built for
# Darwin and fail compilation due to us using the bionic version of
# stdatomic.h.
if self._config.target_os.is_darwin:
defines['LLVM_BUILD_EXTERNAL_COMPILER_RT'] = 'ON'
# Don't build libfuzzer as part of the first stage build.
defines['COMPILER_RT_BUILD_LIBFUZZER'] = 'OFF'
return defines
def test(self) -> None:
with timer.Timer(f'stage1_test'):
self._ninja(['check-clang', 'check-llvm', 'check-clang-tools'])
# stage1 cannot run check-cxx yet
class Stage2Builder(base_builders.LLVMBuilder):
name: str = 'stage2'
install_dir: Path = paths.OUT_DIR / 'stage2-install'
remove_install_dir: bool = True
debug_build: bool = False
build_instrumented: bool = False
bolt_optimize: bool = False
bolt_instrument: bool = False
profdata_file: Optional[Path] = None
lto: bool = False
@property
def llvm_targets(self) -> Set[str]:
return constants.ANDROID_TARGETS
@property
def llvm_projects(self) -> Set[str]:
proj = {'clang', 'lld', 'clang-tools-extra', 'polly', 'bolt'}
if self.build_lldb:
proj.add('lldb')
return proj
@property
def llvm_runtime_projects(self) -> Set[str]:
proj = {'compiler-rt', 'libcxx', 'libcxxabi'}
if isinstance(self._config, configs.LinuxMuslConfig):
# libcxx builds against libunwind when building for musl
proj.add('libunwind')
return proj
@property
def env(self) -> Dict[str, str]:
env = super().env
# Point CMake to the libc++ from stage1. It is possible that once built,
# the newly-built libc++ may override this because of the rpath pointing to
# $ORIGIN/../lib. That'd be fine because both libraries are built from
# the same sources.
# Newer compilers put lib files in lib/x86_64-unknown-linux-gnu.
# Include the path to the libc++.so.1 in stage2-install,
# to run unittests/.../*Tests programs.
env['LD_LIBRARY_PATH'] = (
':'.join([str(item) for item in self.toolchain.lib_dirs])
+ f':{self.install_dir}/lib')
return env
@property
def ldflags(self) -> List[str]:
ldflags = super().ldflags
if self._config.target_os.is_linux:
if isinstance(self._config, configs.LinuxMuslConfig):
ldflags.append('-Wl,-rpath,\$ORIGIN/../lib/x86_64-unknown-linux-musl')
else:
ldflags.append('-Wl,-rpath,\$ORIGIN/../lib/x86_64-unknown-linux-gnu')
# '$ORIGIN/../lib' is added by llvm's CMake rules.
if self.bolt_optimize or self.bolt_instrument:
ldflags.append('-Wl,-q')
# TODO: Turn on ICF for Darwin once it can be built with LLD.
if not self._config.target_os.is_darwin:
ldflags.append('-Wl,--icf=safe')
if self.lto and self.enable_mlgo:
ldflags.append('-Wl,-mllvm,-regalloc-enable-advisor=release')
return ldflags
@property
def cflags(self) -> List[str]:
cflags = super().cflags
if self.profdata_file:
cflags.append('-Wno-profile-instr-out-of-date')
cflags.append('-Wno-profile-instr-unprofiled')
if not self.lto and self.enable_mlgo:
cflags.append('-mllvm -regalloc-enable-advisor=release')
return cflags
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['CLANG_PYTHON_BINDINGS_VERSIONS'] = '3'
if (self.lto and
not self._config.target_os.is_darwin and
not self.build_instrumented and
not self.debug_build):
defines['LLVM_ENABLE_LTO'] = 'Thin'
# Increase the ThinLTO link jobs limit to improve build speed.
defines['LLVM_PARALLEL_LINK_JOBS'] = min(int(multiprocessing.cpu_count() / 2), 16)
# Build libFuzzer here to be exported for the host fuzzer builds. libFuzzer
# is not currently supported on Darwin.
if self._config.target_os.is_darwin:
defines['COMPILER_RT_BUILD_LIBFUZZER'] = 'OFF'
else:
defines['COMPILER_RT_BUILD_LIBFUZZER'] = 'ON'
if self.debug_build:
defines['CMAKE_BUILD_TYPE'] = 'Debug'
if self.build_instrumented:
defines['LLVM_BUILD_INSTRUMENTED'] = 'ON'
# llvm-profdata is only needed to finish CMake configuration
# (tools/clang/utils/perf-training/CMakeLists.txt) and not needed for
# build
llvm_profdata = self.toolchain.path / 'bin' / 'llvm-profdata'
defines['LLVM_PROFDATA'] = str(llvm_profdata)
elif self.profdata_file:
defines['LLVM_PROFDATA_FILE'] = str(self.profdata_file)
# Do not build compiler-rt for Darwin. We don't ship host (or any
# prebuilt) runtimes for Darwin anyway. Attempting to build these will
# fail compilation of lib/builtins/atomic_*.c that only get built for
# Darwin and fail compilation due to us using the bionic version of
# stdatomic.h.
if self._config.target_os.is_darwin:
defines['LLVM_BUILD_EXTERNAL_COMPILER_RT'] = 'ON'
return defines
def install_config(self) -> None:
super().install_config()
lldb_wrapper_path = self.install_dir / 'bin' / 'lldb.sh'
lib_path_env = 'LD_LIBRARY_PATH' if self._config.target_os.is_linux else 'DYLD_LIBRARY_PATH'
lldb_wrapper_path.write_text(textwrap.dedent(f"""\
#!/bin/bash
CURDIR=$(cd $(dirname $0) && pwd)
export PYTHONHOME="$CURDIR/../python3"
export {lib_path_env}="$CURDIR/../python3/lib:${lib_path_env}"
"$CURDIR/lldb" "$@"
"""))
lldb_wrapper_path.chmod(0o755)
def test(self) -> None:
if isinstance(self._config, configs.LinuxMuslConfig):
# musl cannot run check-cxx yet
with timer.Timer('stage2_test'):
self._ninja(['check-clang', 'check-llvm'])
# TUSchedulerTests.PreambleThrottle is flaky on buildbots for musl build.
# So disable it.
self._ninja(['check-clang-tools'],
{'GTEST_FILTER': '-TUSchedulerTests.PreambleThrottle'})
else:
super().test()
class BuiltinsBuilder(base_builders.LLVMRuntimeBuilder):
name: str = 'builtins'
src_dir: Path = paths.LLVM_PATH / 'compiler-rt' / 'lib' / 'builtins'
# Only target the NDK, not the platform. The NDK copy is sufficient for the
# platform builders, and both NDK+platform builders use the same toolchain,
# which can only have a single copy installed into its resource directory.
@property
def config_list(self) -> List[configs.Config]:
result = configs.android_configs(platform=False)
# There is no NDK for riscv64, use the platform config instead.
riscv64 = configs.AndroidRiscv64Config()
riscv64.platform = True
result.append(riscv64)
result.append(configs.BaremetalAArch64Config())
# For arm32 and x86, build a special version of the builtins library
# where the symbols are exported, not hidden. This version is needed
# to continue exporting builtins from libc.so and libm.so.
for arch in [configs.AndroidARMConfig(), configs.AndroidI386Config()]:
arch.platform = False
arch.extra_config = {'is_exported': True}
result.append(arch)
result.append(configs.LinuxMuslConfig(hosts.Arch.AARCH64))
result.append(configs.LinuxMuslConfig(hosts.Arch.ARM))
return result
@property
def is_exported(self) -> bool:
return self._config.extra_config and self._config.extra_config.get('is_exported', False)
@property
def output_dir(self) -> Path:
old_path = super().output_dir
suffix = '-exported' if self.is_exported else ''
return old_path.parent / (old_path.name + suffix)
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
arch = self._config.target_arch
defines['COMPILER_RT_BUILTINS_HIDE_SYMBOLS'] = \
'TRUE' if not self.is_exported else 'FALSE'
# Most builders use COMPILER_RT_DEFAULT_TARGET_TRIPLE, but that cause
# a problem for non-Android arm. compiler-rt autodetects arm, armhf
# and armv6m in compiler-rt/cmake/base-config-ix.cmake, but
# set_output_name in compiler-rt/cmake/Modules/AddCompilerRT.cmake
# uses the name libclang_rt.builtins-arm for both arm and armv6m.
# Use CMAKE_C_COMPILER_TARGET + COMPILER_RT_DEFAULT_TARGET_ONLY
# instead to only build for armhf.
defines['CMAKE_C_COMPILER_TARGET'] = self._config.llvm_triple
defines['CMAKE_CXX_COMPILER_TARGET'] = self._config.llvm_triple
defines['COMPILER_RT_DEFAULT_TARGET_ONLY'] = 'TRUE'
# For CMake feature testing, create an archive instead of an executable,
# because we can't link an executable until builtins have been built.
defines['CMAKE_TRY_COMPILE_TARGET_TYPE'] = 'STATIC_LIBRARY'
defines['COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN'] = 'OFF'
defines['COMPILER_RT_OS_DIR'] = self._config.target_os.crt_dir
return defines
def install_config(self) -> None:
# Copy the library into the toolchain resource directory (lib/linux) and
# runtimes_ndk_cxx.
arch = self._config.target_arch
sarch = 'i686' if arch == hosts.Arch.I386 else arch.value
if isinstance(self._config, configs.LinuxMuslConfig) and arch == hosts.Arch.ARM:
sarch = 'armhf'
filename = 'libclang_rt.builtins-' + sarch
filename += '-android.a' if self._config.target_os.is_android else '.a'
filename_exported = 'libclang_rt.builtins-' + sarch + '-android-exported.a'
src_path = self.output_dir / 'lib' / self._config.target_os.crt_dir / filename
self.output_resource_dir.mkdir(parents=True, exist_ok=True)
if self.is_exported:
# This special copy exports its symbols and is only intended for use
# in Bionic's libc.so.
shutil.copy2(src_path, self.output_resource_dir / filename_exported)
else:
shutil.copy2(src_path, self.output_resource_dir / filename)
# Also install to self.resource_dir, if it's different,
# for use when building target libraries.
if self.resource_dir != self.output_resource_dir:
self.resource_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, self.resource_dir / filename)
# Make a copy for the NDK.
if self._config.target_os.is_android:
dst_dir = self.output_toolchain.path / 'runtimes_ndk_cxx'
dst_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, dst_dir / filename)
class CompilerRTBuilder(base_builders.LLVMRuntimeBuilder):
name: str = 'compiler-rt'
src_dir: Path = paths.LLVM_PATH / 'compiler-rt'
config_list: List[configs.Config] = (
configs.android_configs(platform=True) +
configs.android_configs(platform=False)
)
@property
def install_dir(self) -> Path:
if self._config.platform:
return self.output_toolchain.clang_lib_dir
# Installs to a temporary dir and copies to runtimes_ndk_cxx manually.
output_dir = self.output_dir
return output_dir.parent / (output_dir.name + '-install')
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['COMPILER_RT_BUILD_BUILTINS'] = 'OFF'
defines['COMPILER_RT_USE_BUILTINS_LIBRARY'] = 'ON'
# FIXME: Disable WError build until upstream fixed the compiler-rt
# personality routine warnings caused by r309226.
# defines['COMPILER_RT_ENABLE_WERROR'] = 'ON'
defines['COMPILER_RT_TEST_COMPILER_CFLAGS'] = defines['CMAKE_C_FLAGS']
defines['COMPILER_RT_DEFAULT_TARGET_TRIPLE'] = self._config.llvm_triple
defines['COMPILER_RT_INCLUDE_TESTS'] = 'OFF'
defines['SANITIZER_CXX_ABI'] = 'libcxxabi'
# With CMAKE_SYSTEM_NAME='Android', compiler-rt will be installed to
# lib/android instead of lib/linux.
del defines['CMAKE_SYSTEM_NAME']
libs: List[str] = []
if self._config.api_level < 21:
libs += ['-landroid_support']
# Currently, -rtlib=compiler-rt (even with -unwindlib=libunwind) does
# not automatically link libunwind.a on Android.
libs += ['-lunwind']
defines['SANITIZER_COMMON_LINK_LIBS'] = ' '.join(libs)
# compiler-rt's CMakeLists.txt file deletes -Wl,-z,defs from
# CMAKE_SHARED_LINKER_FLAGS when COMPILER_RT_USE_BUILTINS_LIBRARY is
# set. We want this flag on instead to catch unresolved references
# early.
defines['SANITIZER_COMMON_LINK_FLAGS'] = '-Wl,-z,defs'
if self._config.platform:
defines['COMPILER_RT_HWASAN_WITH_INTERCEPTORS'] = 'OFF'
return defines
@property
def cflags(self) -> List[str]:
cflags = super().cflags
cflags.append('-funwind-tables')
return cflags
def install_config(self) -> None:
# Still run `ninja install`.
super().install_config()
lib_dir = self.install_dir / 'lib' / 'linux'
# Install the fuzzer library to the old {arch}/libFuzzer.a path for
# backwards compatibility.
arch = self._config.target_arch
sarch = 'i686' if arch == hosts.Arch.I386 else arch.value
static_lib_filename = 'libclang_rt.fuzzer-' + sarch + '-android.a'
arch_dir = lib_dir / arch.value
arch_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(lib_dir / static_lib_filename, arch_dir / 'libFuzzer.a')
if not self._config.platform:
dst_dir = self.output_toolchain.path / 'runtimes_ndk_cxx'
shutil.copytree(lib_dir, dst_dir, dirs_exist_ok=True)
def install(self) -> None:
# Install libfuzzer headers once for all configs.
header_src = self.src_dir / 'lib' / 'fuzzer'
header_dst = self.output_toolchain.path / 'prebuilt_include' / 'llvm' / 'lib' / 'Fuzzer'
header_dst.mkdir(parents=True, exist_ok=True)
for f in header_src.iterdir():
if f.suffix in ('.h', '.def'):
shutil.copy2(f, header_dst)
symlink_path = self.output_resource_dir / 'libclang_rt.hwasan_static-aarch64-android.a'
symlink_path.unlink(missing_ok=True)
os.symlink('libclang_rt.hwasan-aarch64-android.a', symlink_path)
class MuslHostRuntimeBuilder(base_builders.LLVMRuntimeBuilder):
name: str = 'compiler-rt-linux-musl'
src_dir: Path = paths.LLVM_PATH / 'runtimes'
config_list: List[configs.Config] = [
configs.LinuxMuslConfig(hosts.Arch.X86_64),
configs.LinuxMuslConfig(hosts.Arch.I386),
configs.LinuxMuslConfig(hosts.Arch.AARCH64),
configs.LinuxMuslConfig(hosts.Arch.ARM),
]
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['LLVM_ENABLE_RUNTIMES'] = 'compiler-rt;libunwind'
# compiler-rt CMake defines
# ORC JIT fails to build with MUSL.
defines['COMPILER_RT_BUILD_ORC'] = 'OFF'
# libunwind CMake defines
if self.enable_assertions:
defines['LIBUNWIND_ENABLE_ASSERTIONS'] = 'TRUE'
else:
defines['LIBUNWIND_ENABLE_ASSERTIONS'] = 'FALSE'
defines['LIBUNWIND_ENABLE_SHARED'] = 'FALSE'
defines['LIBUNWIND_TARGET_TRIPLE'] = self._config.llvm_triple
defines['COMPILER_RT_HAS_LIBSTDCXX'] = 'FALSE'
defines['COMPILER_RT_HAS_LIBCXX'] = 'TRUE'
defines['SANITIZER_CXX_ABI'] = 'libcxxabi'
defines['COMPILER_RT_USE_BUILTINS_LIBRARY'] = 'TRUE'
# Most builders use COMPILER_RT_DEFAULT_TARGET_TRIPLE, but that cause
# a problem for non-Android arm. compiler-rt autodetects arm, armhf
# and armv6m in compiler-rt/cmake/base-config-ix.cmake, but
# set_output_name in compiler-rt/cmake/Modules/AddCompilerRT.cmake
# uses the name libclang_rt.builtins-arm for both arm and armv6m.
# Use CMAKE_C_COMPILER_TARGET + COMPILER_RT_DEFAULT_TARGET_ONLY
# instead to only build for armhf.
# CMAKE_CXX_COMPILER_TARGET is also necessary for the libcxx embedded
# in libclang_rt.fuzzer.
defines['CMAKE_C_COMPILER_TARGET'] = self._config.llvm_triple
defines['CMAKE_CXX_COMPILER_TARGET'] = self._config.llvm_triple
defines['COMPILER_RT_DEFAULT_TARGET_ONLY'] = 'TRUE'
return defines
@property
def cflags(self) -> List[str]:
# Use the stage2 toolchain's resource-dir where libclang_rt.builtins
# gets installed. This is only needed in debug and instrumented builds
# (where the stage1 toolchain is used to build runtimes) and a no-op
# elsewhere.
return super().cflags + ['-resource-dir', f'{self.output_toolchain.clang_lib_dir}']
@property
def install_dir(self) -> Path:
return self.output_resource_dir / self._config.llvm_triple
class LibUnwindBuilder(base_builders.LLVMRuntimeBuilder):
name: str = 'libunwind'
src_dir: Path = paths.LLVM_PATH / 'runtimes'
# Build two copies of the builtins library:
# - A copy targeting the NDK with hidden symbols.
# - A copy targeting the platform with exported symbols.
# Bionic's libc.so exports the unwinder, so it needs a copy with exported
# symbols. Everything else uses the NDK copy.
@property
def config_list(self) -> List[configs.Config]:
result = configs.android_configs(platform=False)
for arch in configs.android_configs(platform=True):
arch.extra_config = {'is_exported': True}
result.append(arch)
# riscv64 needs a copy with hidden symbols for use while building
# the runtimes, but doesn't have an NDK sysroot. Make a copy
# targeting the platform with hidden symbols.
riscv64 = configs.AndroidRiscv64Config()
riscv64.platform = True
riscv64.extra_config = {'is_exported': False}
result.append(riscv64)
return result
@property
def is_exported(self) -> bool:
return self._config.extra_config and self._config.extra_config.get('is_exported', False)
@property
def output_dir(self) -> Path:
old_path = super().output_dir
suffix = '-exported' if self.is_exported else '-hermetic'
return old_path.parent / (old_path.name + suffix)
@property
def cflags(self) -> List[str]:
return super().cflags + ['-D_LIBUNWIND_USE_DLADDR=0']
@property
def ldflags(self) -> List[str]:
# Override the default -unwindlib=libunwind. libunwind.a doesn't exist
# when libunwind is built, and libunwind can't use
# CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY because
# LIBUNWIND_HAS_PTHREAD_LIB must be set to false.
return super().ldflags + ['-unwindlib=none']
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['LLVM_ENABLE_RUNTIMES'] = 'libunwind'
defines['LIBUNWIND_HIDE_SYMBOLS'] = 'TRUE' if not self.is_exported else 'FALSE'
defines['LIBUNWIND_ENABLE_SHARED'] = 'FALSE'
if self.enable_assertions:
defines['LIBUNWIND_ENABLE_ASSERTIONS'] = 'TRUE'
else:
defines['LIBUNWIND_ENABLE_ASSERTIONS'] = 'FALSE'
# Enable the FrameHeaderCache for the libc.so unwinder only. It can't be
# enabled generally for Android because it needs the
# dlpi_adds/dlpi_subs fields, which were only added to Bionic in
# Android R. See llvm.org/pr46743.
defines['LIBUNWIND_USE_FRAME_HEADER_CACHE'] = 'TRUE' if self.is_exported else 'FALSE'
defines['LIBUNWIND_TARGET_TRIPLE'] = self._config.llvm_triple
return defines
def install_config(self) -> None:
# We need to install libunwind manually.
arch = self._config.target_arch
src_path = self.output_dir / 'lib' / 'libunwind.a'
out_res_dir = self.output_resource_dir / arch.value
out_res_dir.mkdir(parents=True, exist_ok=True)
if self.is_exported:
# This special copy exports its symbols and is only intended for use
# in Bionic's libc.so.
shutil.copy2(src_path, out_res_dir / 'libunwind-exported.a')
else:
shutil.copy2(src_path, out_res_dir / 'libunwind.a')
# Also install to self.resource_dir, if it's different, for
# use when building runtimes.
if self.resource_dir != self.output_resource_dir:
res_dir = self.resource_dir / arch.value
res_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, res_dir / 'libunwind.a')
# Make a copy for the NDK.
ndk_dir = self.output_toolchain.path / 'runtimes_ndk_cxx' / arch.value
ndk_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, ndk_dir / 'libunwind.a')
class LibOMPBuilder(base_builders.LLVMRuntimeBuilder):
name: str = 'libomp'
src_dir: Path = paths.LLVM_PATH / 'openmp'
config_list: List[configs.Config] = (
configs.android_configs(platform=True, extra_config={'is_shared': False}) +
configs.android_configs(platform=False, extra_config={'is_shared': False}) +
configs.android_configs(platform=False, extra_config={'is_shared': True})
)
@property
def is_shared(self) -> bool:
return cast(Dict[str, bool], self._config.extra_config)['is_shared']
@property
def output_dir(self) -> Path:
old_path = super().output_dir
suffix = '-shared' if self.is_shared else '-static'
return old_path.parent / (old_path.name + suffix)
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['OPENMP_ENABLE_LIBOMPTARGET'] = 'FALSE'
defines['OPENMP_ENABLE_OMPT_TOOLS'] = 'FALSE'
defines['LIBOMP_ENABLE_SHARED'] = 'TRUE' if self.is_shared else 'FALSE'
return defines
@property
def ldflags(self) -> List[str]:
# Workaround for undefined version symbols in libomp.
# https://reviews.llvm.org/D135402
# http://b/258377285
return ["-Wl,--undefined-version"]
def install_config(self) -> None:
# We need to install libomp manually.
libname = 'libomp.' + ('so' if self.is_shared else 'a')
src_lib = self.output_dir / 'runtime' / 'src' / libname
dst_dir = self.install_dir
dst_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_lib, dst_dir / libname)
# install omp.h, omp-tools.h (it's enough to do for just one config).
if self._config.target_arch == hosts.Arch.AARCH64:
for header in ['omp.h', 'omp-tools.h']:
shutil.copy2(self.output_dir / 'runtime' / 'src' / header,
self.output_toolchain.clang_builtin_header_dir)
class LibNcursesBuilder(base_builders.AutoconfBuilder, base_builders.LibInfo):
name: str = 'libncurses'
src_dir: Path = paths.LIBNCURSES_SRC_DIR
@property
def config_flags(self) -> List[str]:
return super().config_flags + [
'--with-shared',
]
@property
def _lib_names(self) -> List[str]:
return ['libncurses', 'libform', 'libpanel']
class LibEditBuilder(base_builders.AutoconfBuilder, base_builders.LibInfo):
name: str = 'libedit'
src_dir: Path = paths.LIBEDIT_SRC_DIR
@property
def ldflags(self) -> List[str]:
return [
] + super().ldflags
@property
def cflags(self) -> List[str]:
flags = []
return flags + super().cflags
def build(self) -> None:
files: List[Path] = []
super().build()
class SwigBuilder(base_builders.AutoconfBuilder):
name: str = 'swig'
src_dir: Path = paths.SWIG_SRC_DIR
@property
def config_flags(self) -> List[str]:
flags = super().config_flags
flags.append('--without-pcre')
return flags
@property
def ldflags(self) -> List[str]:
ldflags = super().ldflags
# Point to the libc++.so from the toolchain.
for lib_dir in self.toolchain.lib_dirs:
ldflags.append(f'-Wl,-rpath,{lib_dir}')
return ldflags
class XzBuilder(base_builders.CMakeBuilder, base_builders.LibInfo):
name: str = 'liblzma'
src_dir: Path = paths.XZ_SRC_DIR
static_lib: bool = True
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
# CMake actually generates a malformed archive command. llvm-ranlib does
# not accept it, but the Apple ranlib accepts this. Workaround to use
# the system ranlib until either CMake fixes this or llvm-ranlib also
# supports this common malformed input.
# See LIBTOOL(1).
if self._config.target_os.is_darwin:
defines['CMAKE_RANLIB'] = '/usr/bin/ranlib'
return defines
class ZstdBuilder(base_builders.CMakeBuilder, base_builders.LibInfo):
name: str = 'libzstd'
src_dir: Path = paths.ZSTD_SRC_DIR / 'build' / 'cmake'
with_lib_version: bool = False
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['ZSTD_BUILD_PROGRAMS'] = 'OFF'
# See XzBuilder above for reasoning.
if self._config.target_os.is_darwin:
defines['CMAKE_RANLIB'] = '/usr/bin/ranlib'
return defines
@property
def link_libraries(self) -> List[Path]:
# LLVM requires both dynamic and static libzstd.
if self._config.target_os.is_windows:
libs = [self.install_dir / 'bin' / 'libzstd.dll']
else:
libs = super().link_libraries
libs.append(self.install_dir / 'lib' / 'libzstd.a')
return libs
class LibXml2Builder(base_builders.CMakeBuilder, base_builders.LibInfo):
name: str = 'libxml2'
src_dir: Path = paths.LIBXML2_SRC_DIR
@contextlib.contextmanager
def _backup_file(self, file_to_backup: Path) -> Iterator[None]:
backup_file = file_to_backup.parent / (file_to_backup.name + '.bak')
if file_to_backup.exists():
file_to_backup.rename(backup_file)
try:
yield
finally:
if backup_file.exists():
backup_file.rename(file_to_backup)
def build(self) -> None:
# The src dir contains configure files for Android platform. Rename them
# so that they will not be used during our build.
# We don't delete them here because the same libxml2 may be used to build
# Android platform later.
with self._backup_file(self.src_dir / 'include' / 'libxml' / 'xmlversion.h'):
with self._backup_file(self.src_dir / 'config.h'):
super().build()
@property
def ldflags(self) -> List[str]:
if self._config.target_os.is_linux:
# We do not enable all libxml2 features. Allow undefined symbols in the version script.
return super().ldflags + ['-Wl,--undefined-version']
return super().ldflags
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
defines['LIBXML2_WITH_PYTHON'] = 'OFF'
defines['LIBXML2_WITH_PROGRAMS'] = 'ON'
defines['LIBXML2_WITH_LZMA'] = 'OFF'
defines['LIBXML2_WITH_ICONV'] = 'OFF'
defines['LIBXML2_WITH_ZLIB'] = 'OFF'
return defines
@property
def include_dir(self) -> Path:
return self.install_dir / 'include' / 'libxml2'
@property
def symlinks(self) -> List[Path]:
if self._config.target_os.is_windows:
return []
ext = 'so' if self._config.target_os.is_linux else 'dylib'
return [self.install_dir / 'lib' / f'libxml2.{ext}']
@property
def install_tools(self) -> List[Path]:
return [self.install_dir / 'bin' / 'xmllint']
class LldbServerBuilder(base_builders.LLVMRuntimeBuilder):
name: str = 'lldb-server'
src_dir: Path = paths.LLVM_PATH / 'llvm'
config_list: List[configs.Config] = configs.android_configs(platform=False, static=True)
ninja_targets: List[str] = ['lldb-server']
@property
def cflags(self) -> List[str]:
cflags: List[str] = super().cflags
# The build system will add '-stdlib=libc++' automatically. Since we
# have -nostdinc++ here, -stdlib is useless. Adds a flag to avoid the
# warnings.
cflags.append('-Wno-unused-command-line-argument')
return cflags
@property
def ldflags(self) -> List[str]:
# Currently, -rtlib=compiler-rt (even with -unwindlib=libunwind) does
# not automatically link libunwind.a on Android.
return super().ldflags + ['-lunwind']
@property
def _llvm_target(self) -> str:
return {
hosts.Arch.ARM: 'ARM',
hosts.Arch.AARCH64: 'AArch64',
hosts.Arch.I386: 'X86',
hosts.Arch.X86_64: 'X86',
}[self._config.target_arch]
@property
def cmake_defines(self) -> Dict[str, str]:
defines = super().cmake_defines
# lldb depends on support libraries.
defines['LLVM_ENABLE_PROJECTS'] = 'clang;lldb'
defines['LLVM_TARGETS_TO_BUILD'] = self._llvm_target
defines['LLVM_TABLEGEN'] = str(self.toolchain.build_path / 'bin' / 'llvm-tblgen')
defines['CLANG_TABLEGEN'] = str(self.toolchain.build_path / 'bin' / 'clang-tblgen')
defines['LLDB_TABLEGEN'] = str(self.toolchain.build_path / 'bin' / 'lldb-tblgen')
triple = self._config.llvm_triple
defines['LLVM_HOST_TRIPLE'] = triple.replace('i686', 'i386')
defines['LLDB_ENABLE_LUA'] = 'OFF'
return defines
def install_config(self) -> None:
src_path = self.output_dir / 'bin' / 'lldb-server'
install_dir = self.install_dir
install_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, install_dir)
class HostSysrootsBuilder(base_builders.Builder):
name: str = 'host-sysroots'
config_list: List[configs.Config] = (configs.MinGWConfig(),)
def _build_config(self) -> None:
config = self._config
sysroot = config.sysroot
sysroot_lib = sysroot / 'lib'
if sysroot.exists():
shutil.rmtree(sysroot)
sysroot.parent.mkdir(parents=True, exist_ok=True)
# copy sysroot and add libgcc* to it.
shutil.copytree(config.gcc_root / config.gcc_triple,
sysroot, symlinks=True)
shutil.copytree(config.gcc_lib_dir, sysroot_lib, dirs_exist_ok=True)
# b/237425904 cleanup: uncomment to remove libstdc++ after toolchain defaults to
# libc++
# (sysroot_lib / 'libstdc++.a').unlink()
# shutil.rmtree(sysroot / 'include' / 'c++' / '4.8.3')
# copy libc++ libs and headers from bootstrap prebuilts. This is needed
# for the libcxx builder to pass CMake configuration. The libcxx
# builder will subsequently overwrite these.
shutil.copy(paths.WINDOWS_CLANG_PREBUILT_DIR / 'lib' / 'libc++.a', sysroot_lib)
shutil.copy(paths.WINDOWS_CLANG_PREBUILT_DIR / 'lib' / 'libc++abi.a', sysroot_lib)
shutil.copytree(paths.WINDOWS_CLANG_PREBUILT_DIR / 'include' / 'c++' / 'v1',
sysroot / 'include' / 'c++' / 'v1')
class DeviceSysrootsBuilder(base_builders.Builder):
name: str = 'device-sysroots'
config_list: List[configs.Config] = (
configs.android_configs(platform=True) +
configs.android_configs(platform=False)
)
def _build_config(self) -> None:
config: configs.AndroidConfig = cast(configs.AndroidConfig, self._config)
arch = config.target_arch
platform = config.platform
sysroot = config.sysroot
if sysroot.exists():
shutil.rmtree(sysroot)
sysroot.mkdir(parents=True, exist_ok=True)
# Copy the NDK prebuilt's sysroot, but for the platform variant, omit
# the STL and android_support headers and libraries.
if arch == hosts.Arch.RISCV64:
src_sysroot = paths.RISCV64_ANDROID_SYSROOT
else:
src_sysroot = paths.NDK_BASE / 'toolchains' / 'llvm' / 'prebuilt' / 'linux-x86_64' / 'sysroot'
# Copy over usr/include.
shutil.copytree(src_sysroot / 'usr' / 'include',
sysroot / 'usr' / 'include', symlinks=True)
if platform:
if arch != hosts.Arch.RISCV64:
# Remove the STL headers.
shutil.rmtree(sysroot / 'usr' / 'include' / 'c++')
else:
# Add the android_support headers from usr/local/include.
shutil.copytree(src_sysroot / 'usr' / 'local' / 'include',
sysroot / 'usr' / 'local' / 'include', symlinks=True)
# Copy over usr/lib/$TRIPLE.
src_lib = src_sysroot / 'usr' / 'lib' / config.ndk_sysroot_triple
dest_lib = sysroot / 'usr' / 'lib' / config.ndk_sysroot_triple
shutil.copytree(src_lib, dest_lib, symlinks=True)
# Remove the NDK's libcompiler_rt-extras. For the platform, also remove
# the NDK libc++, except for the riscv64 sysroot which doesn't have
# these files.
(dest_lib / 'libcompiler_rt-extras.a').unlink()
if platform and arch != hosts.Arch.RISCV64:
(dest_lib / 'libc++abi.a').unlink()
(dest_lib / 'libc++_static.a').unlink()
(dest_lib / 'libc++_shared.so').unlink()
# Each per-API-level directory has libc++.so and libc++.a.
for subdir in dest_lib.iterdir():
if subdir.is_symlink() or not subdir.is_dir():
continue
if not re.match(r'\d+$', subdir.name):
continue
if platform and arch != hosts.Arch.RISCV64:
(subdir / 'libc++.a').unlink()
(subdir / 'libc++.so').unlink()
# Verify that there aren't any extra copies somewhere else in the
# directory hierarchy.
verify_gone = ['libcompiler_rt-extras.a', 'libunwind.a']
if platform:
verify_gone += [
'libc++abi.a',
'libc++_static.a',
'libc++_shared.so',
'libc++.a',
'libc++.so',
]
for (parent, _, files) in os.walk(sysroot):
for f in files:
if f in verify_gone:
raise RuntimeError('sysroot file should have been ' +
f'removed: {os.path.join(parent, f)}')
if platform:
# Create a stub library for the platform's libc++.
platform_stubs = paths.OUT_DIR / 'platform_stubs' / config.ndk_arch
platform_stubs.mkdir(parents=True, exist_ok=True)
libdir = sysroot / 'usr' / 'lib'
libdir.mkdir(parents=True, exist_ok=True)
with (platform_stubs / 'libc++.c').open('w') as f:
f.write(textwrap.dedent("""\
void __cxa_atexit() {}
void __cxa_demangle() {}
void __cxa_finalize() {}
void __dynamic_cast() {}
void _ZTIN10__cxxabiv117__class_type_infoE() {}
void _ZTIN10__cxxabiv120__si_class_type_infoE() {}
void _ZTIN10__cxxabiv121__vmi_class_type_infoE() {}
void _ZTISt9type_info() {}
"""))
utils.check_call([self.toolchain.cc,
f'--target={config.llvm_triple}',
'-fuse-ld=lld', '-nostdlib', '-shared',
'-Wl,-soname,libc++.so',
'-o{}'.format(libdir / 'libc++.so'),
str(platform_stubs / 'libc++.c')])
class PlatformLibcxxAbiBuilder(base_builders.LLVMRuntimeBuilder):
name = 'platform-libcxxabi'
src_dir: Path = paths.LLVM_PATH / 'runtimes'
config_list: List[configs.Config] = configs.android_configs(
platform=True, suppress_libcxx_headers=True)