Skip to content

Commit d2bbe5b

Browse files
author
Siddhartha Bagaria
committed
Sync with GRAIL's toolchain configuration
1 parent f9aada9 commit d2bbe5b

File tree

6 files changed

+85
-191
lines changed

6 files changed

+85
-191
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,30 @@ Notes:
3131

3232
- The LLVM toolchain archive is downloaded and extracted in the named
3333
repository. People elsewhere have used wrapper scripts to avoid symlinking
34-
and get better control of the environment in which the toolchain binaries are
35-
run.
34+
and get better control of the environment in which the toolchain binaries are
35+
run.
3636

3737
- Sandboxing the toolchain introduces a significant overhead (100ms per
38-
action). To overcome this, one can use `--experimental_sandbox_fs=/dev/shm`.
39-
However, not all environments might have enough shared memory available to load
40-
all the files in memory. In order to avoid sandboxing the toolchain entirely,
41-
we can use absolute paths. When running bazel actions, these paths will be
42-
available from inside the sandbox as part of the / read-only mount.
38+
action). To overcome this, one can use
39+
`--experimental_sandbox_base=/dev/shm`. However, not all environments might
40+
have enough shared memory available to load all the files in memory. That is
41+
why we have templated the paths to the toolchain as absolute paths. When
42+
running bazel actions, these paths will be available from inside the sandbox
43+
as part of the / read-only mount.
4344

44-
- While the C++ toolchain itself works well with relative paths (except the
45-
performance hit because of sandboxing), the go toolchain needs the linker and
46-
the linked libraries as absolute. This is a limitation of the cpp configuration
47-
fragment provided by skylark and used by the go toolchain.
45+
- The toolchain is known to also work with `rules_go`, both with absolute and
46+
relative paths.
4847

4948
- There is no R toolchain yet, so the Makevars file has been repurposed to
50-
provide the right configuration, but only when the configure script for
51-
packages do not override the variables set in the Makevars.
49+
provide the right configuration, but only when using `cc_deps` attribute of
50+
`r_pkg`. All packages not using `cc_deps` are free to configure themselves.
51+
The Makevars file provided will always use absolute paths. This implies that
52+
the build actions are not reproducible across multiple workspaces, although
53+
the build outputs can be.
5254

5355
- The toolchain is almost hermetic but borrows system headers and libraries
54-
from the user's system. If needed, one can package a sysroot for their build
55-
environment and set `builtin_sysroot` in CROSSTOOL. If using relative paths, be
56-
sure to add your sysroot files to the `all_files` attribute of the toolchain.
56+
from the user's system. If needed, we can package a sysroot for our build
57+
environment and set `builtin_sysroot` in CROSSTOOL.
5758

5859
Other examples of toolchain configuration:
5960

toolchain/BUILD.tpl

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ filegroup(
2323

2424
filegroup(
2525
name = "cc_wrapper",
26-
srcs = ["cc_wrapper.sh"],
26+
srcs = ["bin/cc_wrapper.sh"],
2727
)
2828

2929
cc_toolchain_suite(
@@ -39,6 +39,9 @@ load("@com_grail_bazel_toolchain//toolchain:configure.bzl", "conditional_cc_tool
3939
conditional_cc_toolchain("cc-clang-linux", "k8", False, %{absolute_paths})
4040
conditional_cc_toolchain("cc-clang-darwin", "darwin", True, %{absolute_paths})
4141

42+
## LLVM toolchain files
43+
# Needed when not using absolute paths.
44+
4245
filegroup(
4346
name = "clang",
4447
srcs = [
@@ -52,25 +55,32 @@ filegroup(
5255
name = "ld",
5356
srcs = [
5457
"bin/ld.lld",
55-
"bin/ld64.lld"
58+
"bin/ld",
59+
"bin/ld.gold", # Dummy file on non-linux.
5660
],
5761
)
5862

5963
filegroup(
6064
name = "include",
6165
srcs = glob([
6266
"include/c++/**",
63-
"lib/clang/*/include/**",
67+
"lib/clang/%{llvm_version}/include/**",
6468
]),
6569
)
6670

6771
filegroup(
6872
name = "lib",
69-
srcs = [
70-
"lib/libc++.a",
71-
"lib/libc++abi.a",
72-
"lib/libunwind.a",
73-
],
73+
srcs = glob(
74+
[
75+
"lib/lib*.a",
76+
"lib/clang/%{llvm_version}/lib/**/*.a",
77+
],
78+
exclude = [
79+
"lib/libLLVM*.a",
80+
"lib/libclang*.a",
81+
"lib/liblld*.a",
82+
],
83+
),
7484
)
7585

7686
filegroup(
@@ -128,18 +138,7 @@ filegroup(
128138

129139
filegroup(
130140
name = "binutils_components",
131-
srcs = [
132-
":ar",
133-
":as",
134-
":dwp",
135-
":ld",
136-
":nm",
137-
":objcopy",
138-
":objdump",
139-
":profdata",
140-
":ranlib",
141-
":readelf",
142-
],
141+
srcs = glob(["bin/*"]),
143142
)
144143

145144
filegroup(

toolchain/CROSSTOOL.tpl

Lines changed: 18 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ toolchain {
3333
compiler: "clang"
3434
host_system_name: "local"
3535
needsPic: true
36-
supports_gold_linker: false
3736
supports_incremental_linker: false
3837
supports_fission: false
3938
supports_interface_shared_objects: false
4039
supports_normalizing_ar: false
4140
supports_start_end_lib: true
41+
supports_gold_linker: true
4242
target_libc: "local"
4343
target_cpu: "k8"
4444
target_system_name: "local"
@@ -54,6 +54,7 @@ toolchain {
5454
unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
5555
unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
5656
unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
57+
unfiltered_cxx_flag: "-fdebug-prefix-map=%{toolchain_path_prefix}=%{debug_toolchain_path_prefix}"
5758
5859
# Security
5960
compiler_flag: "-U_FORTIFY_SOURCE"
@@ -65,20 +66,20 @@ toolchain {
6566
compiler_flag: "-Wall"
6667
6768
# C++
68-
cxx_flag: "-std=c++11"
69+
cxx_flag: "-std=c++17"
6970
cxx_flag: "-stdlib=libc++"
7071
# The linker has no way of knowing if there are C++ objects; so we always link C++ libraries.
71-
linker_flag: "%{toolchain_path_prefix}lib/libc++.a"
72-
linker_flag: "%{toolchain_path_prefix}lib/libc++abi.a"
73-
linker_flag: "%{toolchain_path_prefix}lib/libunwind.a"
72+
linker_flag: "-l:libc++.a"
73+
linker_flag: "-l:libc++abi.a"
74+
linker_flag: "-l:libunwind.a"
7475
cxx_flag: "-DLIBCXX_USE_COMPILER_RT=YES"
7576
linker_flag: "-rtlib=compiler-rt"
76-
linker_flag: "-lpthread"
77-
linker_flag: "-ldl" # For libunwind
77+
linker_flag: "-lpthread" # For libunwind
78+
linker_flag: "-ldl" # For libunwind
7879
7980
# Linker
8081
linker_flag: "-lm"
81-
linker_flag: "-fuse-ld=lld"
82+
linker_flag: "-fuse-ld=gold"
8283
linker_flag: "-Wl,--build-id=md5"
8384
linker_flag: "-Wl,--hash-style=gnu"
8485
@@ -89,14 +90,11 @@ toolchain {
8990
cxx_builtin_include_directory: "/include"
9091
cxx_builtin_include_directory: "/usr/include"
9192
cxx_builtin_include_directory: "/usr/local/include"
92-
compiler_flag: "-isystem%{toolchain_path_prefix}include/c++/v1"
93-
compiler_flag: "-isystem%{toolchain_path_prefix}lib/clang/%{llvm_version}/include"
9493
9594
objcopy_embed_flag: "-I"
9695
objcopy_embed_flag: "binary"
9796
98-
compiler_flag: "-B%{toolchain_path_prefix}bin"
99-
tool_path {name: "ld" path: "%{tools_path_prefix}bin/ld.lld" }
97+
tool_path {name: "ld" path: "%{tools_path_prefix}bin/ld.gold" } # TODO: Switch to lld after https://reviews.llvm.org/D41978
10098
tool_path {name: "cpp" path: "%{tools_path_prefix}bin/clang-cpp" }
10199
tool_path {name: "dwp" path: "%{tools_path_prefix}bin/llvm-dwp" }
102100
tool_path {name: "gcov" path: "%{tools_path_prefix}bin/llvm-profdata" }
@@ -124,60 +122,6 @@ toolchain {
124122
}
125123

126124
linking_mode_flags { mode: DYNAMIC }
127-
128-
feature {
129-
name: "coverage"
130-
}
131-
feature {
132-
name: "llvm_coverage_map_format"
133-
flag_set {
134-
action: "preprocess-assemble"
135-
action: "c-compile"
136-
action: "c++-compile"
137-
action: "c++-module-compile"
138-
flag_group {
139-
flag: "-fprofile-instr-generate"
140-
flag: "-fcoverage-mapping"
141-
flag: "-g"
142-
}
143-
}
144-
flag_set {
145-
action: "c++-link-dynamic-library"
146-
action: "c++-link-nodeps-dynamic-library"
147-
action: "c++-link-executable"
148-
flag_group {
149-
flag: "-fprofile-instr-generate"
150-
}
151-
}
152-
requires {
153-
feature: "coverage"
154-
}
155-
}
156-
feature {
157-
name: "gcc_coverage_map_format"
158-
flag_set {
159-
action: "preprocess-assemble"
160-
action: "c-compile"
161-
action: "c++-compile"
162-
action: "c++-module-compile"
163-
flag_group {
164-
flag: "-fprofile-arcs"
165-
flag: "-ftest-coverage"
166-
flag: "-g"
167-
}
168-
}
169-
flag_set {
170-
action: "c++-link-dynamic-library"
171-
action: "c++-link-nodeps-dynamic-library"
172-
action: "c++-link-executable"
173-
flag_group {
174-
flag: "-lgcov"
175-
}
176-
}
177-
requires {
178-
feature: "coverage"
179-
}
180-
}
181125
}
182126

183127
toolchain {
@@ -189,7 +133,7 @@ toolchain {
189133
compiler: "clang"
190134
abi_version: "darwin_x86_64"
191135
abi_libc_version: "darwin_x86_64"
192-
needsPic: true
136+
needsPic: false
193137
194138
builtin_sysroot: ""
195139
@@ -202,6 +146,7 @@ toolchain {
202146
unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
203147
unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
204148
unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
149+
unfiltered_cxx_flag: "-fdebug-prefix-map=%{toolchain_path_prefix}=%{debug_toolchain_path_prefix}"
205150
206151
# Security
207152
compiler_flag: "-D_FORTIFY_SOURCE=1"
@@ -215,16 +160,11 @@ toolchain {
215160
compiler_flag: "-Wall"
216161
217162
# C++
218-
cxx_flag: "-std=c++11"
163+
cxx_flag: "-std=c++17"
219164
cxx_flag: "-stdlib=libc++"
220165
# The linker has no way of knowing if there are C++ objects; so we always link C++ libraries.
221-
linker_flag: "%{toolchain_path_prefix}lib/libc++.a"
222-
linker_flag: "%{toolchain_path_prefix}lib/libc++abi.a"
223-
linker_flag: "%{toolchain_path_prefix}lib/libunwind.a"
224-
cxx_flag: "-DLIBCXX_USE_COMPILER_RT=YES"
225-
linker_flag: "-rtlib=compiler-rt"
226-
linker_flag: "-lpthread"
227-
linker_flag: "-ldl" # For libunwind
166+
linker_flag: "-lc++"
167+
linker_flag: "-lc++abi"
228168
229169
# Linker
230170
linker_flag: "-lm"
@@ -235,27 +175,22 @@ toolchain {
235175
cxx_builtin_include_directory: "%{toolchain_path_prefix}include/c++/v1"
236176
cxx_builtin_include_directory: "%{toolchain_path_prefix}lib/clang/%{llvm_version}/include"
237177
cxx_builtin_include_directory: "/usr/include"
238-
cxx_builtin_include_directory: "/usr/include/linux"
239178
cxx_builtin_include_directory: "/System/Library/Frameworks"
240179
cxx_builtin_include_directory: "/Library/Frameworks"
241-
#cxx_builtin_include_directory: "/Applications/Xcode.app/Contents/Developer"
242-
compiler_flag: "-isystem%{toolchain_path_prefix}include/c++/v1"
243-
compiler_flag: "-isystem%{toolchain_path_prefix}lib/clang/%{llvm_version}/include"
244180
245181
objcopy_embed_flag: "-I"
246182
objcopy_embed_flag: "binary"
247183
248-
compiler_flag: "-B%{toolchain_path_prefix}bin"
249-
tool_path {name: "ld" path: "/usr/bin/ld" } # lld is not ready for macOS.
184+
tool_path {name: "ld" path: "%{tools_path_prefix}bin/ld" } # lld is not ready for macOS.
250185
tool_path {name: "cpp" path: "%{tools_path_prefix}bin/clang-cpp" }
251186
tool_path {name: "dwp" path: "%{tools_path_prefix}bin/llvm-dwp" }
252187
tool_path {name: "gcov" path: "%{tools_path_prefix}bin/llvm-profdata" }
253188
tool_path {name: "nm" path: "%{tools_path_prefix}bin/llvm-nm" }
254189
tool_path {name: "objcopy" path: "%{tools_path_prefix}bin/llvm-objcopy" }
255190
tool_path {name: "objdump" path: "%{tools_path_prefix}bin/llvm-objdump" }
256191
tool_path {name: "strip" path: "/usr/bin/strip" }
257-
tool_path {name: "gcc" path: "%{tools_path_prefix}cc_wrapper.sh" }
258-
tool_path {name: "ar" path: "/usr/bin/libtool" } # default archiver flags configuration is different for macOS.
192+
tool_path {name: "gcc" path: "%{tools_path_prefix}bin/cc_wrapper.sh" }
193+
tool_path {name: "ar" path: "/usr/bin/libtool" } # system provided libtool is preferred.
259194

260195
compilation_mode_flags {
261196
mode: FASTBUILD
@@ -304,64 +239,4 @@ toolchain {
304239
}
305240
}
306241
}
307-
308-
feature {
309-
name: "coverage"
310-
}
311-
feature {
312-
name: "llvm_coverage_map_format"
313-
flag_set {
314-
action: "preprocess-assemble"
315-
action: "c-compile"
316-
action: "c++-compile"
317-
action: "c++-module-compile"
318-
action: "objc-compile"
319-
action: "objc++-compile"
320-
flag_group {
321-
flag: "-fprofile-instr-generate"
322-
flag: "-fcoverage-mapping"
323-
flag: "-g"
324-
}
325-
}
326-
flag_set {
327-
action: "c++-link-dynamic-library"
328-
action: "c++-link-nodeps-dynamic-library"
329-
action: "c++-link-executable"
330-
action: "objc-executable"
331-
action: "objc++-executable"
332-
flag_group {
333-
flag: "-fprofile-instr-generate"
334-
}
335-
}
336-
requires {
337-
feature: "coverage"
338-
}
339-
}
340-
feature {
341-
name: "gcc_coverage_map_format"
342-
flag_set {
343-
action: "preprocess-assemble"
344-
action: "c-compile"
345-
action: "c++-compile"
346-
action: "c++-module-compile"
347-
action: "objc-compile"
348-
action: "objc++-compile"
349-
flag_group {
350-
flag: "-fprofile-arcs"
351-
flag: "-ftest-coverage"
352-
flag: "-g"
353-
}
354-
}
355-
flag_set {
356-
action: "c++-link-dynamic-library"
357-
action: "c++-link-nodeps-dynamic-library"
358-
action: "c++-link-executable"
359-
flag_group {
360-
flag: "-lgcov"
361-
}
362-
}
363-
requires {
364-
feature: "coverage"
365-
}
366-
}
367242
}

0 commit comments

Comments
 (0)