Skip to content

Commit 0ff37f2

Browse files
committed
Begin building libc++ and libc++abi runtimes on demand
This builds on the previous work to flesh out more on-demand runtimes building. It adds building of the `libc++.a` archive runtime. A number of changes are required for this to work: - The runtimes build infrastructure needs to support building sources from multiple parts of LLVM rather than a single part. We do this by lifting the root of the runtimes source paths up a level to a common runtimes tree, and installing the runtimes sources below this directory. - Both libc++ and libc++abi runtimes sources need to be installed, and we even need to install some interesting parts of llvm-libc that are used in the build of libc++. - We need to generate the site configuration header file for libc++ from the CMake template. This includes both setting up a set of platform-independent defines and introducing some basic Bazel support for processing the CMake template itself. Doing all of this also exposed some missing features and limitations of the runtimes building infrastructure that are addressed here. One note is that all of this just adds libc++ to the explicit `build-runtimes` command for testing. It doesn't yet trigger automatically building these prior to linking, or configuring any of the other subcommands to automatically use these runtimes. All of that will come in follow-up PRs. Also, this makes the `clang_runtimes_test` ... _very_ slow in our default build configuration. Compiling libc++, even with many threads on a large Linux server requires up to 50 seconds. I'm open to any suggestions on how to handle this, including disabling the test in non-optimized builds. I have some ideas to speed this up, but fundamentally building libc++ is... not cheap. I did look at some of the existing Bazel tools to process the CMake template, but they all seemed significantly more complex than what we need and didn't have broad adoption. Given that, it seemed slightly better to just roll our own given the simple format. The second new LLVM patch is currently under review upstream and so hopefully temporary: llvm/llvm-project#169155
1 parent 28017b6 commit 0ff37f2

17 files changed

+766
-70
lines changed

MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ http_archive(
118118
"@carbon//bazel/llvm_project:0002_Added_Bazel_build_for_compiler_rt_fuzzer.patch",
119119
"@carbon//bazel/llvm_project:0003_Comment_out_unloaded_proto_library_dependencies.patch",
120120
"@carbon//bazel/llvm_project:0004_Introduce_basic_sources_exporting_for_libunwind.patch",
121+
"@carbon//bazel/llvm_project:0005_Introduce_basic_sources_exporting_for_libcxx_and_libcxxabi.patch",
122+
"@carbon//bazel/llvm_project:0006_Add_a_filegroup_containing__all__sources_to_the_libc_build_rules.patch",
123+
"@carbon//bazel/llvm_project:0007_Remove_target_compatibility_restrictions_for_float128.patch",
121124
],
122125
strip_prefix = "llvm-project-{0}".format(llvm_project_version),
123126
urls = ["https://github.com/llvm/llvm-project/archive/{0}.tar.gz".format(llvm_project_version)],

bazel/check_deps/check_non_test_cc_deps.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@
4141
# Other packages in the LLVM project shouldn't be accidentally used
4242
# in Carbon. We can expand the above list if use cases emerge.
4343
if package not in (
44-
"llvm",
45-
"lld",
4644
"clang",
4745
"clang-tools-extra/clangd",
46+
"libc",
47+
"libcxx",
48+
"libcxxabi",
4849
"libunwind",
50+
"lld",
51+
"llvm",
4952
# While this is in a `third_party` directory, its code is documented
5053
# as part of LLVM and for use in compiler-rt.
5154
"third-party/siphash",
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Commit ID: e4ff7299fe7e35e70ba79f5d8e2c58658cfba678
2+
Change ID: mstnwoqruyypnoouksnyqssllrsozpos
3+
Bookmarks: bz-libcxx bz-libcxx@git bz-libcxx@origin
4+
Author : Chandler Carruth <[email protected]> (2025-09-25 22:55:26)
5+
Committer: Chandler Carruth <[email protected]> (2025-11-22 09:23:52)
6+
7+
Introduce basic sources exporting for libcxx and libcxxabi
8+
9+
This exports the source files directly so that they can be used to build
10+
a libcxx runtime library on demand.
11+
12+
diff --git a/utils/bazel/llvm-project-overlay/libcxx/BUILD.bazel b/utils/bazel/llvm-project-overlay/libcxx/BUILD.bazel
13+
new file mode 100644
14+
index 0000000000..a81a64c649
15+
--- /dev/null
16+
+++ b/utils/bazel/llvm-project-overlay/libcxx/BUILD.bazel
17+
@@ -0,0 +1,49 @@
18+
+# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
19+
+# See https://llvm.org/LICENSE.txt for license information.
20+
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
21+
+
22+
+licenses(["notice"])
23+
+
24+
+package(
25+
+ default_visibility = ["//visibility:public"],
26+
+)
27+
+
28+
+exports_files(["include/__config_site.in"])
29+
+
30+
+exports_files(["vendor/llvm/default_assertion_handler.in"])
31+
+
32+
+filegroup(
33+
+ name = "libcxx_hdrs",
34+
+ srcs = glob(
35+
+ [
36+
+ # Top level includes and those in `experimental` and `ext` sometimes
37+
+ # have no extension.
38+
+ "include/*",
39+
+ "include/experimental/*",
40+
+ "include/ext/*",
41+
+
42+
+ # Implementation detail headers all use `.h` extensions
43+
+ "include/**/*.h",
44+
+ ],
45+
+ exclude = [
46+
+ # Omit CMake and CMake-configured files that get caught by the
47+
+ # extension-less patterns.
48+
+ "**/*.in",
49+
+ "**/CMakeLists.txt",
50+
+
51+
+ # Omit C++03 compatibility headers as current users don't need them.
52+
+ "include/__cxx03/**",
53+
+ ],
54+
+ ),
55+
+)
56+
+
57+
+filegroup(
58+
+ name = "libcxx_srcs",
59+
+ srcs = glob(
60+
+ [
61+
+ "src/**/*.cpp",
62+
+ "src/**/*.h",
63+
+ "src/**/*.ipp",
64+
+ ],
65+
+ ),
66+
+)
67+
diff --git a/utils/bazel/llvm-project-overlay/libcxxabi/BUILD.bazel b/utils/bazel/llvm-project-overlay/libcxxabi/BUILD.bazel
68+
new file mode 100644
69+
index 0000000000..cf491e4e46
70+
--- /dev/null
71+
+++ b/utils/bazel/llvm-project-overlay/libcxxabi/BUILD.bazel
72+
@@ -0,0 +1,24 @@
73+
+# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
74+
+# See https://llvm.org/LICENSE.txt for license information.
75+
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76+
+
77+
+licenses(["notice"])
78+
+
79+
+package(
80+
+ default_visibility = ["//visibility:public"],
81+
+)
82+
+
83+
+filegroup(
84+
+ name = "libcxxabi_hdrs",
85+
+ srcs = glob(["include/*.h"]),
86+
+)
87+
+
88+
+filegroup(
89+
+ name = "libcxxabi_srcs",
90+
+ srcs = glob([
91+
+ "src/**/*.cpp",
92+
+ "src/**/*.def",
93+
+ "src/**/*.inc",
94+
+ "src/**/*.h",
95+
+ ]),
96+
+)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Commit ID: e524424ed32e945bdced8c19ca480f461d04397a
2+
Change ID: lslousqwyovpkqlxvyrzmmypxkxxulwn
3+
Bookmarks: push-lslousqwyovp push-lslousqwyovp@git push-lslousqwyovp@origin
4+
Author : Chandler Carruth <[email protected]> (2025-11-20 03:05:36)
5+
Committer: Chandler Carruth <[email protected]> (2025-11-22 09:23:52)
6+
7+
Add a filegroup containing _all_ sources to the libc build rules
8+
9+
These rules already expose a filegroup containing the _dependencies_,
10+
but that misses the source files directly in the top level library.
11+
12+
Without this filegroup, there isn't a way to access the source files
13+
used by libcxx when building it, etc.
14+
15+
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
16+
index 0f2965369c..c871334dd9 100644
17+
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
18+
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
19+
@@ -247,6 +247,12 @@
20+
**kwargs
21+
)
22+
23+
+ _libc_srcs_filegroup(
24+
+ name = name + "_hdrs",
25+
+ libs = [":" + name],
26+
+ enforce_headers_only = True,
27+
+ )
28+
+
29+
def libc_generated_header(name, hdr, yaml_template, other_srcs = []):
30+
"""Generates a libc header file from YAML template.
31+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Commit ID: 94325bae07213d2dd8263945f5362720dee7bcb9
2+
Change ID: kskkxwvnyqwuzkswkuxpnmyovqxoypnz
3+
Bookmarks: push-kskkxwvnyqwu push-kskkxwvnyqwu@git push-kskkxwvnyqwu@origin
4+
Author : Chandler Carruth <[email protected]> (2025-11-24 07:26:06)
5+
Committer: Chandler Carruth <[email protected]> (2025-11-24 07:40:58)
6+
7+
Remove target compatibility restrictions for float128
8+
9+
The restrictions here aren't nearly as much about the OS as the compiler
10+
and architecture, but the Bazel restriction was OS-based. Everything
11+
seems to work well on even Arm64 macOS, and I would expect most BSDs and
12+
other OSes to work well with Clang's support on x86-64.
13+
14+
The source code here already handles detecting when there is compiler
15+
support for the type. And the users of this don't `select` or do
16+
anything else to conditionally include the header, so it seems better to
17+
not restrict access to the header from the build system, and instead
18+
continue making the source code compatible or a no-op on relevant
19+
configurations.
20+
21+
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
22+
index bd48222856..e3962d9b5f 100644
23+
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
24+
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
25+
@@ -171,11 +171,6 @@
26+
libc_support_library(
27+
name = "llvm_libc_types_float128",
28+
hdrs = ["include/llvm-libc-types/float128.h"],
29+
- target_compatible_with = select({
30+
- "@platforms//os:linux": [],
31+
- "@platforms//os:windows": [],
32+
- "//conditions:default": ["@platforms//:incompatible"],
33+
- }),
34+
deps = [":llvm_libc_macros_float_macros"],
35+
)
36+

toolchain/base/runtime_sources.bzl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ BUILTINS_FILEGROUPS = {
3838
}
3939

4040
RUNTIMES_FILEGROUPS = {
41-
"libunwind_srcs": "@llvm-project//libunwind:libunwind_srcs",
41+
"libcxx": "@llvm-project//libcxx:libcxx_srcs",
42+
"libcxxabi": "@llvm-project//libcxxabi:libcxxabi_srcs",
43+
"libunwind": "@llvm-project//libunwind:libunwind_srcs",
4244
}
4345

4446
_TEMPLATE = """
@@ -89,8 +91,16 @@ inline constexpr llvm::StringLiteral BuiltinsI386Srcs[] = {{
8991
{i386_srcs}
9092
}};
9193
94+
constexpr inline llvm::StringLiteral LibcxxSrcs[] = {{
95+
{libcxx}
96+
}};
97+
98+
constexpr inline llvm::StringLiteral LibcxxabiSrcs[] = {{
99+
{libcxxabi}
100+
}};
101+
92102
constexpr inline llvm::StringLiteral LibunwindSrcs[] = {{
93-
{libunwind_srcs}
103+
{libunwind}
94104
}};
95105
96106
}} // namespace Carbon::RuntimeSources
@@ -118,14 +128,14 @@ def _get_path(file_attr, to_path_fn):
118128

119129
return '"{0}"'.format(to_path_fn(files[0]))
120130

121-
def _get_paths(files_attr, to_path_fn):
131+
def _get_paths(files_attr, to_path_fn, prefix = ""):
122132
files = []
123133
for src in files_attr:
124134
files.extend(src[DefaultInfo].files.to_list())
125135
files.extend(src[DefaultInfo].default_runfiles.files.to_list())
126136

127137
return "\n".join([
128-
' "{0}",'.format(to_path_fn(f))
138+
' "{0}{1}",'.format(prefix, to_path_fn(f))
129139
for f in files
130140
])
131141

@@ -138,7 +148,9 @@ def _generate_runtime_sources_h_rule(ctx):
138148
k: _get_paths(getattr(ctx.attr, "_" + k), _builtins_path)
139149
for k in BUILTINS_FILEGROUPS.keys()
140150
} | {
141-
k: _get_paths(getattr(ctx.attr, "_" + k), _runtimes_path)
151+
# Other runtimes are installed under separate directories named the same
152+
# as their key.
153+
k: _get_paths(getattr(ctx.attr, "_" + k), _runtimes_path, k + "/")
142154
for k in RUNTIMES_FILEGROUPS.keys()
143155
})))
144156
return [DefaultInfo(files = depset([h_file]))]

toolchain/driver/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ cc_test(
8282

8383
cc_test(
8484
name = "clang_runtimes_test",
85-
size = "small",
85+
size = "medium",
8686
srcs = ["clang_runtimes_test.cpp"],
8787
data = ["//toolchain/install:install_data"],
8888
deps = [

toolchain/driver/build_runtimes_subcommand.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ auto BuildRuntimesSubcommand::RunInternal(DriverEnv& driver_env)
9797
ClangArchiveRuntimesBuilder<Runtimes::LibUnwind> lib_unwind_builder(
9898
&runner, driver_env.thread_pool, llvm::Triple(features.target),
9999
&runtimes);
100+
ClangArchiveRuntimesBuilder<Runtimes::Libcxx> libcxx_builder(
101+
&runner, driver_env.thread_pool, llvm::Triple(features.target),
102+
&runtimes);
100103

101104
CARBON_RETURN_IF_ERROR(std::move(resource_dir_builder).Wait());
102105
CARBON_RETURN_IF_ERROR(std::move(lib_unwind_builder).Wait());
106+
CARBON_RETURN_IF_ERROR(std::move(libcxx_builder).Wait());
103107

104108
return runtimes.base_path();
105109
}

0 commit comments

Comments
 (0)