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 18, 2023
1 parent f4f0ebf commit 36ad5aa
Show file tree
Hide file tree
Showing 7 changed files with 60 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_local_cc_config",
"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_cc_config//: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
47 changes: 47 additions & 0 deletions swift/internal/swift_autoconfiguration.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ 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(
"@bazel_tools//tools/cpp:lib_cc_configure.bzl",
"get_cpu_value",
)
load(
"@build_bazel_rules_swift//swift/internal:feature_names.bzl",
"SWIFT_FEATURE_CODEVIEW_DEBUG_INFO",
Expand Down Expand Up @@ -231,6 +236,22 @@ def _normalized_linux_cpu(cpu):
return "x86_64"
return cpu

def _create_linux_cc_toolchain(repository_ctx):
path_to_swiftc = repository_ctx.which("swiftc")
if not path_to_swiftc:
fail("No 'swiftc' executable found in $PATH")

toolchain_root = path_to_swiftc.dirname
cpu = get_cpu_value(repository_ctx)
configure_unix_toolchain(repository_ctx, cpu, overriden_tools = {
"ar": toolchain_root.get_child("llvm-ar"),
"ld": toolchain_root.get_child("lld"),
"llvm-cov": toolchain_root.get_child("llvm-cov"),
"llvm-profdata": toolchain_root.get_child("llvm-profdata"),
"cpp": toolchain_root.get_child("clang-cpp"),
"gcc": toolchain_root.get_child("clang"),
})

def _create_linux_toolchain(repository_ctx):
"""Creates BUILD targets for the Swift toolchain on Linux.
Expand All @@ -241,6 +262,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 @@ -262,6 +284,11 @@ load(
package(default_visibility = ["//visibility:public"])
alias(
name = "swift_cc_toolchain",
actual = "@build_bazel_rules_swift_local_cc_config//:toolchain"
)
swift_toolchain(
name = "toolchain",
arch = "{cpu}",
Expand All @@ -277,6 +304,7 @@ swift_toolchain(
for feature in feature_values
]),
root = root,
toolchain_root = toolchain_root,
version_file = version_file,
),
)
Expand Down Expand Up @@ -370,6 +398,12 @@ load(
package(default_visibility = ["//visibility:public"])
# Use the system C++ toolchain
alias(
name = "swift_cc_toolchain",
actual = "@bazel_tools//tools/cpp:current_cc_toolchain"
)
swift_toolchain(
name = "toolchain",
arch = "x86_64",
Expand All @@ -392,6 +426,13 @@ swift_toolchain(
),
)

def _swift_cc_autoconfiguration_impl(repository_ctx):
os_name = repository_ctx.os.name.lower()
if os_name.startswith("linux"):
_create_linux_cc_toolchain(repository_ctx)
else:
fail("cc_toolchain detection for Swift is currently only supported on Linux")

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 All @@ -404,6 +445,12 @@ def _swift_autoconfiguration_impl(repository_ctx):
else:
_create_linux_toolchain(repository_ctx)

swift_cc_autoconfiguration = repository_rule(
environ = ["PATH"],
implementation = _swift_cc_autoconfiguration_impl,
local = True,
)

swift_autoconfiguration = repository_rule(
environ = ["CC", "PATH", "ProgramData", "Path"],
implementation = _swift_autoconfiguration_impl,
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//: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//: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//: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
6 changes: 6 additions & 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_autoconfiguration",
)

def _maybe(repo_rule, name, **kwargs):
Expand Down Expand Up @@ -183,6 +184,11 @@ def swift_rules_dependencies(include_bzlmod_ready_dependencies = True):
sha256 = "28c1ffa39d99e74ed70623899b207b41f79214c498c603915aef55972a851a15",
)

_maybe(
swift_cc_autoconfiguration,
name = "build_bazel_rules_swift_local_cc_config",
)

_maybe(
swift_autoconfiguration,
name = "build_bazel_rules_swift_local_config",
Expand Down

0 comments on commit 36ad5aa

Please sign in to comment.