Skip to content

Commit

Permalink
Use cc_toolchain_* for clang shipped with Swift toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Sep 11, 2023
1 parent f4f0ebf commit 8e3023c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
3 changes: 3 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use_repo(
non_module_deps,
"build_bazel_rules_swift_index_import",
"build_bazel_rules_swift_local_config",
"build_bazel_rules_swift_cc_toolchain",
"com_github_apple_swift_log",
"com_github_apple_swift_nio",
"com_github_apple_swift_nio_extras",
Expand All @@ -33,3 +34,5 @@ use_repo(apple_cc_configure, "local_config_apple_cc")

# Dev dependencies
bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True, repo_name = "io_bazel_stardoc")

register_toolchains("@build_bazel_rules_swift_cc_toolchain//:all")
2 changes: 1 addition & 1 deletion swift/internal/linking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ into the binary. Possible values are:
# Do not add references; temporary attribute for C++ toolchain
# Starlark migration.
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
default = Label("@build_bazel_rules_swift_local_config//:current_swift_cc_toolchain"),
),
# A late-bound attribute denoting the value of the `--custom_malloc`
# command line flag (or None if the flag is not provided).
Expand Down
109 changes: 109 additions & 0 deletions swift/internal/swift_autoconfiguration.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ should be loaded here. Do not load anything else, even common libraries like
Skylib.
"""

load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain")
load(
"@build_bazel_rules_swift//swift/internal:feature_names.bzl",
"SWIFT_FEATURE_CODEVIEW_DEBUG_INFO",
Expand Down Expand Up @@ -241,6 +242,7 @@ def _create_linux_toolchain(repository_ctx):
if not path_to_swiftc:
fail("No 'swiftc' executable found in $PATH")

toolchain_root = path_to_swiftc.dirname
root = path_to_swiftc.dirname.dirname
feature_values = _compute_feature_values(repository_ctx, path_to_swiftc)
version_file = _write_swift_version(repository_ctx, path_to_swiftc)
Expand All @@ -255,13 +257,105 @@ def _create_linux_toolchain(repository_ctx):
repository_ctx.file(
"BUILD",
"""
load("@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl", "cc_toolchain_config")
load(
"@build_bazel_rules_swift//swift/internal:swift_toolchain.bzl",
"swift_toolchain",
)
package(default_visibility = ["//visibility:public"])
cc_toolchain_config(
name = "swift_cc_toolchain_config",
cpu = "{cpu}",
compiler = "clang",
toolchain_identifier = "swift-cc-local",
host_system_name = "local",
target_system_name = "local",
target_libc = "local",
abi_version = "local",
abi_libc_version = "local",
cxx_builtin_include_directories = [],
tool_paths = {{
"ar": "{toolchain_root}/llvm-ar",
"ld": "{toolchain_root}/lld",
"llvm-cov": "{toolchain_root}/bin/llvm-cov",
"llvm-profdata": "{toolchain_root}/bin/llvm-profdata",
"cpp": "{toolchain_root}/clang-cpp",
"gcc": "{toolchain_root}/clang",
"dwp": "/usr/bin/dwp",
"gcov": "/usr/bin/gcov",
"nm": "/usr/bin/nm",
"objcopy": "/usr/bin/objcopy",
"objdump": "/usr/bin/objdump",
"strip": "/usr/bin/strip",
}},
compile_flags = [
"-fstack-protector",
"-Wall",
"-Wthread-safety",
"-Wself-assign",
"-Wunused-but-set-parameter",
"-Wno-free-nonheap-object",
"-fcolor-diagnostics",
"-fno-omit-frame-pointer",
],
opt_compile_flags = [
"-g0",
"-O2",
"-D_FORTIFY_SOURCE=1",
"-DNDEBUG",
"-ffunction-sections",
"-fdata-sections",
],
dbg_compile_flags = ["-g"],
cxx_flags = ["-std=c++0x"],
link_flags = [
"-Wl,-no-as-needed",
"-Wl,-z,relro,-z,now",
"-B/usr/local",
],
link_libs = [
"-lstdc++",
"-lm",
],
opt_link_flags = ["-Wl,--gc-sections"],
unfiltered_compile_flags = [
"-fno-canonical-system-headers",
"-Wno-builtin-macro-redefined",
"-D__DATE__=redacted",
"-D__TIMESTAMP__=redacted",
"-D__TIME__=redacted",
],
coverage_compile_flags = ["--coverage"],
coverage_link_flags = ["--coverage"],
supports_start_end_lib = False,
)
filegroup(
name = "empty"
)
cc_toolchain(
name = "swift_cc_toolchain",
toolchain_identifier = "swift-cc-toolchain",
toolchain_config = ":swift_cc_toolchain_config",
all_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
)
cc_toolchain_suite(
name = "current_swift_cc_toolchain",
toolchains = {{
"k8": ":swift_cc_toolchain",
}}
)
swift_toolchain(
name = "toolchain",
arch = "{cpu}",
Expand All @@ -277,6 +371,7 @@ swift_toolchain(
for feature in feature_values
]),
root = root,
toolchain_root = toolchain_root,
version_file = version_file,
),
)
Expand Down Expand Up @@ -392,6 +487,20 @@ swift_toolchain(
),
)

def _swift_cc_toolchain_impl(repository_ctx):
cpu = _normalized_linux_cpu(repository_ctx.os.arch)
path_to_swiftc = repository_ctx.which("swiftc")
if not path_to_swiftc:
fail("No 'swiftc' executable found in $PATH")

root = path_to_swiftc.dirname.dirname

swift_cc_toolchain = repository_rule(
environ = ["PATH"],
implementation = _swift_cc_toolchain_impl,
local = True,
)

def _swift_autoconfiguration_impl(repository_ctx):
# TODO(allevato): This is expedient and fragile. Use the
# platforms/toolchains APIs instead to define proper toolchains, and make it
Expand Down
2 changes: 1 addition & 1 deletion swift/internal/swift_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ The `.swiftmodule` file provided to Swift targets that depend on this target.
mandatory = False,
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
default = Label("@build_bazel_rules_swift_local_config//:current_swift_cc_toolchain"),
doc = """\
The C++ toolchain from which linking flags and other tools needed by the Swift
toolchain (such as `clang`) will be retrieved.
Expand Down
7 changes: 1 addition & 6 deletions swift/internal/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ def _swift_toolchain_impl(ctx):
toolchain_root = ctx.attr.root
cc_toolchain = find_cpp_toolchain(ctx)

if cc_toolchain.compiler != "clang":
fail("Swift requires the configured CC toolchain to be LLVM (clang). " +
"Either use the locally installed LLVM by setting `CC=clang` in your environment " +
"before invoking Bazel, or configure a Bazel LLVM CC toolchain.")

if ctx.attr.os == "windows":
swift_linkopts_cc_info = _swift_windows_linkopts_cc_info(
ctx.attr.arch,
Expand Down Expand Up @@ -437,7 +432,7 @@ configuration options that are applied to targets on a per-package basis.
allow_single_file = True,
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
default = Label("@build_bazel_rules_swift_local_config//:current_swift_cc_toolchain"),
doc = """\
The C++ toolchain from which other tools needed by the Swift toolchain (such as
`clang` and `ar`) will be retrieved.
Expand Down
2 changes: 1 addition & 1 deletion swift/internal/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ configuration options that are applied to targets on a per-package basis.
providers = [[SwiftPackageConfigurationInfo]],
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
default = Label("@build_bazel_rules_swift_local_config//:current_swift_cc_toolchain"),
doc = """\
The C++ toolchain from which linking flags and other tools needed by the Swift
toolchain (such as `clang`) will be retrieved.
Expand Down
1 change: 1 addition & 0 deletions swift/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(
"@build_bazel_rules_swift//swift/internal:swift_autoconfiguration.bzl",
"swift_autoconfiguration",
"swift_cc_toolchain",
)

def _maybe(repo_rule, name, **kwargs):
Expand Down

0 comments on commit 8e3023c

Please sign in to comment.