|
|
@@ -4,20 +4,23 @@
|
|
|
|
|
|
"""Provides variables and rules to work with Clang's runtime library sources.
|
|
|
|
|
|
-These are organized into groups based on the runtime functionality:
|
|
|
+These are organized into groups based on the Clang runtimes providing them and
|
|
|
+how they are built:
|
|
|
- CRT: The C language runtimes not provided by the C standard library, currently
|
|
|
just infrastructure for global initialization and teardown.
|
|
|
- Builtins: The compiler builtins library mirroring `libgcc` that provides
|
|
|
function definitions for operations not reliably available in hardware but
|
|
|
needed by Clang.
|
|
|
+- Libc++ and libc++abi: The C++ standard library and its ABI components.
|
|
|
+- Libunwind: The unwinding library.
|
|
|
|
|
|
Future runtimes we plan to add support for but not yet included:
|
|
|
-- Libunwind
|
|
|
-- Libc++ and libc++abi
|
|
|
- Sanitizers
|
|
|
- Profiling runtimes
|
|
|
"""
|
|
|
|
|
|
+load("@llvm-project//libcxx:libcxx_library.bzl", "libcxx_and_abi_copts")
|
|
|
+load("@llvm-project//libunwind:libunwind_library.bzl", "libunwind_copts")
|
|
|
load("//bazel/cc_rules:defs.bzl", "cc_library")
|
|
|
|
|
|
CRT_FILES = {
|
|
|
@@ -38,78 +41,29 @@ BUILTINS_FILEGROUPS = {
|
|
|
}
|
|
|
|
|
|
RUNTIMES_FILEGROUPS = {
|
|
|
- "libcxx": "@llvm-project//libcxx:libcxx_srcs",
|
|
|
+ "libcxx_linux": "@llvm-project//libcxx:libcxx_linux_srcs",
|
|
|
+ "libcxx_macos": "@llvm-project//libcxx:libcxx_macos_srcs",
|
|
|
+ "libcxx_win32": "@llvm-project//libcxx:libcxx_win32_srcs",
|
|
|
"libcxxabi": "@llvm-project//libcxxabi:libcxxabi_srcs",
|
|
|
"libunwind": "@llvm-project//libunwind:libunwind_srcs",
|
|
|
}
|
|
|
|
|
|
-_TEMPLATE = """
|
|
|
-// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
|
-// Exceptions. See /LICENSE for license information.
|
|
|
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
-//
|
|
|
-// Generated header file of strings describing the Clang runtime library source
|
|
|
-// files.
|
|
|
-//
|
|
|
-// See toolchain/driver/runtime_sources.bzl for more details.
|
|
|
-
|
|
|
-#ifndef CARBON_TOOLCHAIN_BASE_RUNTIME_SOURCES_H_
|
|
|
-#define CARBON_TOOLCHAIN_BASE_RUNTIME_SOURCES_H_
|
|
|
-
|
|
|
-#include "llvm/ADT/StringRef.h"
|
|
|
-
|
|
|
-namespace Carbon::RuntimeSources {{
|
|
|
-
|
|
|
-inline constexpr llvm::StringLiteral CrtBegin = {crtbegin_src};
|
|
|
-inline constexpr llvm::StringLiteral CrtEnd = {crtend_src};
|
|
|
-
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsGenericSrcs[] = {{
|
|
|
-{generic_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsMacosSrcs[] = {{
|
|
|
-{macos_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsBf16Srcs[] = {{
|
|
|
-{bf16_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsTfSrcs[] = {{
|
|
|
-{tf_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsX86ArchSrcs[] = {{
|
|
|
-{x86_arch_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsX86Fp80Srcs[] = {{
|
|
|
-{x86_fp80_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsAarch64Srcs[] = {{
|
|
|
-{aarch64_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsX86_64Srcs[] = {{
|
|
|
-{x86_64_srcs}
|
|
|
-}};
|
|
|
-inline constexpr llvm::StringLiteral BuiltinsI386Srcs[] = {{
|
|
|
-{i386_srcs}
|
|
|
-}};
|
|
|
-
|
|
|
-constexpr inline llvm::StringLiteral LibcxxSrcs[] = {{
|
|
|
-{libcxx}
|
|
|
-}};
|
|
|
-
|
|
|
-constexpr inline llvm::StringLiteral LibcxxabiSrcs[] = {{
|
|
|
-{libcxxabi}
|
|
|
-}};
|
|
|
-
|
|
|
-constexpr inline llvm::StringLiteral LibunwindSrcs[] = {{
|
|
|
-{libunwind}
|
|
|
-}};
|
|
|
-
|
|
|
-}} // namespace Carbon::RuntimeSources
|
|
|
-
|
|
|
-#endif // CARBON_TOOLCHAIN_BASE_RUNTIME_SOURCES_H_
|
|
|
-"""
|
|
|
+RUNTIMES_PREFIXES = {
|
|
|
+ "libcxx_linux": "libcxx/",
|
|
|
+ "libcxx_macos": "libcxx/",
|
|
|
+ "libcxx_win32": "libcxx/",
|
|
|
+ "libcxxabi": "libcxxabi/",
|
|
|
+ "libunwind": "libunwind/",
|
|
|
+}
|
|
|
+
|
|
|
+def _format_one_per_line(list):
|
|
|
+ return "\n" + "\n".join([
|
|
|
+ ' "{0}",'.format(item)
|
|
|
+ for item in list
|
|
|
+ ]) + "\n"
|
|
|
|
|
|
def _builtins_path(file):
|
|
|
- """Returns the runtime install path for a file in CompilerRT's builtins library."""
|
|
|
+ """Returns the install path for a file in CompilerRT's builtins library."""
|
|
|
|
|
|
# The CompilerRT package has the builtins runtime sources in the
|
|
|
# "lib/builtins/" subdirectory, and we install into a "builtins/"
|
|
|
@@ -118,13 +72,13 @@ def _builtins_path(file):
|
|
|
return file.owner.name.removeprefix("lib/")
|
|
|
|
|
|
def _runtimes_path(file):
|
|
|
- """Returns the runtime install path for a file in a normal runtimes library."""
|
|
|
+ """Returns the install path for a file in a normal runtimes library."""
|
|
|
return file.owner.name
|
|
|
|
|
|
def _get_path(file_attr, to_path_fn):
|
|
|
files = file_attr[DefaultInfo].files.to_list()
|
|
|
if len(files) > 1:
|
|
|
- fail(msg = "Expected a single file and got {0} files.".format(len(files)))
|
|
|
+ fail("Expected a single file and got {0} files.".format(len(files)))
|
|
|
|
|
|
return '"{0}"'.format(to_path_fn(files[0]))
|
|
|
|
|
|
@@ -134,29 +88,44 @@ def _get_paths(files_attr, to_path_fn, prefix = ""):
|
|
|
files.extend(src[DefaultInfo].files.to_list())
|
|
|
files.extend(src[DefaultInfo].default_runfiles.files.to_list())
|
|
|
|
|
|
- return "\n".join([
|
|
|
- ' "{0}{1}",'.format(prefix, to_path_fn(f))
|
|
|
+ return _format_one_per_line([
|
|
|
+ "{0}{1}".format(prefix, to_path_fn(f))
|
|
|
for f in files
|
|
|
])
|
|
|
|
|
|
-def _generate_runtime_sources_h_rule(ctx):
|
|
|
- h_file = ctx.actions.declare_file(ctx.label.name)
|
|
|
- ctx.actions.write(h_file, _TEMPLATE.format(**({
|
|
|
- k: _get_path(getattr(ctx.attr, "_" + k), _builtins_path)
|
|
|
+def _get_substitutions(ctx):
|
|
|
+ key_attr = lambda k: getattr(ctx.attr, "_" + k)
|
|
|
+ return {
|
|
|
+ "LIBCXX_AND_ABI_COPTS": _format_one_per_line(libcxx_and_abi_copts),
|
|
|
+ "LIBUNWIND_COPTS": _format_one_per_line(libunwind_copts),
|
|
|
+ } | {
|
|
|
+ k.upper(): _get_path(key_attr(k), _builtins_path)
|
|
|
for k in CRT_FILES.keys()
|
|
|
} | {
|
|
|
- k: _get_paths(getattr(ctx.attr, "_" + k), _builtins_path)
|
|
|
+ "BUILTINS_" + k.upper(): _get_paths(key_attr(k), _builtins_path)
|
|
|
for k in BUILTINS_FILEGROUPS.keys()
|
|
|
} | {
|
|
|
- # Other runtimes are installed under separate directories named the same
|
|
|
- # as their key.
|
|
|
- k: _get_paths(getattr(ctx.attr, "_" + k), _runtimes_path, k + "/")
|
|
|
+ # Other runtimes are installed under separate directories named the
|
|
|
+ # same as their key.
|
|
|
+ k.upper() + "_SRCS": _get_paths(
|
|
|
+ key_attr(k),
|
|
|
+ _runtimes_path,
|
|
|
+ RUNTIMES_PREFIXES[k],
|
|
|
+ )
|
|
|
for k in RUNTIMES_FILEGROUPS.keys()
|
|
|
- })))
|
|
|
+ }
|
|
|
+
|
|
|
+def _generate_runtimes_build_info_h_rule(ctx):
|
|
|
+ h_file = ctx.actions.declare_file(ctx.label.name)
|
|
|
+ ctx.actions.expand_template(
|
|
|
+ template = ctx.file._template_file,
|
|
|
+ output = h_file,
|
|
|
+ substitutions = _get_substitutions(ctx),
|
|
|
+ )
|
|
|
return [DefaultInfo(files = depset([h_file]))]
|
|
|
|
|
|
-generate_runtime_sources_h = rule(
|
|
|
- implementation = _generate_runtime_sources_h_rule,
|
|
|
+generate_runtimes_build_info_h = rule(
|
|
|
+ implementation = _generate_runtimes_build_info_h_rule,
|
|
|
attrs = {
|
|
|
"_" + k: attr.label(default = v, allow_single_file = True)
|
|
|
for k, v in CRT_FILES.items()
|
|
|
@@ -164,23 +133,27 @@ generate_runtime_sources_h = rule(
|
|
|
"_" + k: attr.label_list(default = [v], allow_files = True)
|
|
|
for k, v in BUILTINS_FILEGROUPS.items() + RUNTIMES_FILEGROUPS.items()
|
|
|
} | {
|
|
|
+ "_template_file": attr.label(
|
|
|
+ default = "runtimes_build_info.tpl.h",
|
|
|
+ allow_single_file = True,
|
|
|
+ ),
|
|
|
},
|
|
|
)
|
|
|
|
|
|
-def generate_runtime_sources_cc_library(name, deps = [], **kwargs):
|
|
|
- """Generates a `runtime_sources.h` header and a `cc_library` rule for it.
|
|
|
+def generate_runtimes_build_info_cc_library(name, deps = [], **kwargs):
|
|
|
+ """Generates a `runtimes_build_info.h` header and a `cc_library` rule.
|
|
|
|
|
|
- This first generates the header file with variables describing the runtime
|
|
|
- sources from Clang, and then a `cc_library` that exports that header.
|
|
|
+ This first generates the header file with variables describing the runtimes
|
|
|
+ build info from Clang, and then a `cc_library` that exports that header.
|
|
|
|
|
|
The `cc_library` rule name is the provided `name` and should be depended on
|
|
|
by code that includes the generated header. The `kwargs` are expanded into
|
|
|
the `cc_library` in case other attributes need to be configured there.
|
|
|
"""
|
|
|
- generate_runtime_sources_h(name = "runtime_sources.h")
|
|
|
+ generate_runtimes_build_info_h(name = "runtimes_build_info.h")
|
|
|
cc_library(
|
|
|
name = name,
|
|
|
- hdrs = ["runtime_sources.h"],
|
|
|
+ hdrs = ["runtimes_build_info.h"],
|
|
|
deps = [
|
|
|
# For StringRef.h
|
|
|
"@llvm-project//llvm:Support",
|