Skip to content

Commit 65a2f70

Browse files
committed
Use source directories for toolchain includes and wrapper tools
1 parent edc127e commit 65a2f70

File tree

6 files changed

+52
-63
lines changed

6 files changed

+52
-63
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @rrbutani @jsharpe @fmeum @helly25
1+
* @rrbutani @jsharpe @fmeum @helly25 @dzbarsky

toolchain/BUILD.llvm_repo renamed to toolchain/BUILD.llvm_repo.tpl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,30 @@ filegroup(
6161
),
6262
)
6363

64+
filegroup(
65+
name = "cxx_builtin_include",
66+
srcs = [
67+
"include/c++",
68+
"lib/clang/{LLVM_VERSION}/include",
69+
],
70+
)
71+
6472
filegroup(
6573
name = "bin",
6674
srcs = glob(["bin/**"]),
6775
)
6876

6977
filegroup(
7078
name = "lib",
71-
srcs = glob(
72-
[
73-
"lib/**/libc++*.a",
74-
"lib/**/libunwind.a",
75-
# TODO(zbarsky): `lib/clang/<VERSION>` can be a source directory
76-
"lib/clang/*/lib/**",
77-
# clang_rt.*.o supply crtbegin and crtend sections.
78-
"lib/**/clang_rt.*.o",
79-
],
80-
allow_empty = True,
81-
),
82-
# Include the .dylib files in the linker sandbox even though they will
83-
# not be available at runtime to allow sanitizers to work locally.
84-
# Any library linked from the toolchain to be released should be linked statically.
79+
srcs = [
80+
# Include the .dylib files in the linker sandbox even though they will
81+
# not be available at runtime to allow sanitizers to work locally.
82+
# Any library linked from the toolchain to be released should be linked statically.
83+
"lib/clang/{LLVM_VERSION}/lib",
84+
] + glob([
85+
"lib/**/libc++*.a",
86+
"lib/**/libunwind.a",
87+
]),
8588
)
8689

8790
filegroup(

toolchain/BUILD.toolchain.tpl

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,13 @@ load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
1919
load("@toolchains_llvm//toolchain/internal:system_module_map.bzl", "system_module_map")
2020
load("%{cc_toolchain_config_bzl}", "cc_toolchain_config")
2121

22-
# Following filegroup targets are used when not using absolute paths and shared
22+
# This filegroup target is used when not using absolute paths and shared
2323
# between different toolchains.
2424

25-
# Tools symlinked through this repo. This target is for internal use in the toolchain only.
25+
# Tools wrapped and symlinked through this repo. This target is for internal use in the toolchain only.
2626
filegroup(
27-
name = "internal-use-symlinked-tools",
28-
srcs = [%{symlinked_tools}
29-
],
30-
visibility = ["//visibility:private"],
31-
)
32-
33-
# Tools wrapped through this repo. This target is for internal use in the toolchain only.
34-
filegroup(
35-
name = "internal-use-wrapped-tools",
36-
srcs = [
37-
"%{wrapper_bin_prefix}cc_wrapper.sh",
38-
],
39-
visibility = ["//visibility:private"],
40-
)
41-
42-
# All internal use files.
43-
filegroup(
44-
name = "internal-use-files",
45-
srcs = [
46-
":internal-use-symlinked-tools",
47-
":internal-use-wrapped-tools",
48-
],
27+
name = "internal-use-tools",
28+
srcs = ["%{tools_dir}"],
4929
visibility = ["//visibility:private"],
5030
)
5131

toolchain/internal/configure.bzl

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ def llvm_config_impl(rctx):
135135
tools = _toolchain_tools(os)
136136
for tool_name, symlink_name in tools.items():
137137
rctx.symlink(llvm_dist_rel_path + "bin/" + tool_name, tools_path_prefix + symlink_name)
138-
symlinked_tools_str = "".join([
139-
"\n" + (" " * 8) + "\"" + tools_path_prefix + symlink_name + "\","
140-
for symlink_name in tools.values()
141-
])
142138
else:
143139
llvm_dist_rel_path = llvm_dist_path_prefix
144140
llvm_dist_label_prefix = llvm_dist_path_prefix
@@ -147,7 +143,6 @@ def llvm_config_impl(rctx):
147143
# No symlinking necessary when using absolute paths.
148144
wrapper_bin_prefix = "bin/"
149145
tools_path_prefix = llvm_dist_path_prefix + "bin/"
150-
symlinked_tools_str = ""
151146

152147
sysroot_paths_dict, sysroot_labels_dict = _sysroot_paths_dict(
153148
rctx,
@@ -230,8 +225,7 @@ def llvm_config_impl(rctx):
230225
{
231226
"%{cc_toolchain_config_bzl}": str(rctx.attr._cc_toolchain_config_bzl),
232227
"%{cc_toolchains}": cc_toolchains_str,
233-
"%{symlinked_tools}": symlinked_tools_str,
234-
"%{wrapper_bin_prefix}": wrapper_bin_prefix,
228+
"%{tools_dir}": wrapper_bin_prefix.removesuffix("/"),
235229
"%{convenience_targets}": convenience_targets_str,
236230
},
237231
)
@@ -335,7 +329,7 @@ def _cc_toolchain_str(
335329
# TODO: Are there other situations where we can continue?
336330
return ""
337331

338-
extra_files_str = "\":internal-use-files\""
332+
extra_files_str = repr(":internal-use-tools")
339333

340334
# C++ built-in include directories.
341335
# This contains both the includes shipped with the compiler as well as the sysroot (or host)
@@ -360,15 +354,12 @@ def _cc_toolchain_str(
360354
"wasip1-wasm32": "wasm32-wasip1",
361355
"wasip1-wasm64": "wasm64-wasip1",
362356
}[target_pair]
357+
363358
cxx_builtin_include_directories = [
364359
toolchain_path_prefix + "include/c++/v1",
365-
toolchain_path_prefix + "include/{}/c++/v1".format(target_system_name),
366-
toolchain_path_prefix + "lib/clang/{}/include".format(llvm_version),
367-
toolchain_path_prefix + "lib/clang/{}/share".format(llvm_version),
368-
toolchain_path_prefix + "lib64/clang/{}/include".format(llvm_version),
369-
toolchain_path_prefix + "lib/clang/{}/include".format(major_llvm_version),
370-
toolchain_path_prefix + "lib/clang/{}/share".format(major_llvm_version),
371-
toolchain_path_prefix + "lib64/clang/{}/include".format(major_llvm_version),
360+
toolchain_path_prefix + "lib/clang/{}/include".format(
361+
major_llvm_version if major_llvm_version >= 16 else llvm_version,
362+
),
372363
]
373364

374365
sysroot_prefix = ""
@@ -505,17 +496,15 @@ filegroup(name = "strip-files-{suffix}", srcs = [{extra_files_str}])
505496
template = template + """
506497
filegroup(
507498
name = "cxx_builtin_include_files-{suffix}",
508-
srcs = [
509-
"{llvm_dist_label_prefix}clang",
510-
"{llvm_dist_label_prefix}include",
511-
],
499+
srcs = ["{llvm_dist_label_prefix}cxx_builtin_include"],
512500
)
513501
514502
filegroup(
515503
name = "compiler-components-{suffix}",
516504
srcs = [
517505
":cxx_builtin_include_files-{suffix}",
518506
":sysroot-components-{suffix}",
507+
"{llvm_dist_label_prefix}clang",
519508
{extra_compiler_files}
520509
],
521510
)

toolchain/internal/repo.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
load(
1515
"//toolchain/internal:common.bzl",
1616
_attr_dict = "attr_dict",
17+
_exec_os_arch_dict_value = "exec_os_arch_dict_value",
1718
_os = "os",
1819
_supported_os_arch_keys = "supported_os_arch_keys",
1920
)
@@ -422,9 +423,16 @@ def llvm_repo_impl(rctx):
422423
rctx.file("BUILD.bazel", executable = False)
423424
return None
424425

426+
(_key, llvm_version) = _exec_os_arch_dict_value(rctx, "llvm_versions")
427+
428+
major_llvm_version = int(llvm_version.split(".")[0])
429+
425430
rctx.file(
426431
"BUILD.bazel",
427-
content = rctx.read(Label("//toolchain:BUILD.llvm_repo")),
432+
content = rctx.read(Label("//toolchain:BUILD.llvm_repo.tpl")).format(
433+
# The versioning changed.
434+
LLVM_VERSION = major_llvm_version if major_llvm_version >= 16 else llvm_version,
435+
),
428436
executable = False,
429437
)
430438

toolchain/internal/system_module_map.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,29 @@ def _system_module_map(ctx):
5555
execroot_prefix = execroot_prefix,
5656
)
5757

58+
umbrella_submodule_closure = lambda file: _umbrella_submodule(
59+
execroot_prefix + paths.normalize(file.path).replace("//", "/"),
60+
)
61+
5862
template_dict = ctx.actions.template_dict()
63+
64+
cxx_builtin_files = ctx.attr.cxx_builtin_include_files[DefaultInfo].files
65+
if len(cxx_builtin_files.to_list()) == 2:
66+
cxx_builtin_include_files_closure = umbrella_submodule_closure
67+
else:
68+
cxx_builtin_include_files_closure = textual_header_closure
5969
template_dict.add_joined(
6070
"%cxx_builtin_include_files%",
61-
ctx.attr.cxx_builtin_include_files[DefaultInfo].files,
71+
cxx_builtin_files,
6272
join_with = "\n",
63-
map_each = textual_header_closure,
73+
map_each = cxx_builtin_include_files_closure,
6474
allow_closure = True,
6575
)
6676

6777
# We don't have a good way to detect a source directory, so check if it's a single File...
6878
sysroot_files = ctx.attr.sysroot_files[DefaultInfo].files.to_list()
6979
if len(sysroot_files) == 1:
70-
path = paths.normalize(sysroot_files[0].path).replace("//", "/")
71-
template_dict.add("%sysroot%", _umbrella_submodule(execroot_prefix + path))
80+
template_dict.add("%sysroot%", umbrella_submodule_closure(sysroot_files[0]))
7281
else:
7382
if sysroot_files:
7483
# buildifier: disable=print

0 commit comments

Comments
 (0)