Skip to content

Commit 36ad5aa

Browse files
committed
Use cc_toolchain_* for clang shipped with Swift toolchain
1 parent f4f0ebf commit 36ad5aa

File tree

7 files changed

+60
-9
lines changed

7 files changed

+60
-9
lines changed

MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use_repo(
1919
non_module_deps,
2020
"build_bazel_rules_swift_index_import",
2121
"build_bazel_rules_swift_local_config",
22+
"build_bazel_rules_swift_local_cc_config",
2223
"com_github_apple_swift_log",
2324
"com_github_apple_swift_nio",
2425
"com_github_apple_swift_nio_extras",
@@ -33,3 +34,5 @@ use_repo(apple_cc_configure, "local_config_apple_cc")
3334

3435
# Dev dependencies
3536
bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True, repo_name = "io_bazel_stardoc")
37+
38+
register_toolchains("@build_bazel_rules_swift_cc_toolchain//:all")

swift/internal/linking.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ into the binary. Possible values are:
107107
# Do not add references; temporary attribute for C++ toolchain
108108
# Starlark migration.
109109
"_cc_toolchain": attr.label(
110-
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
110+
default = Label("@build_bazel_rules_swift_local_cc_config//:toolchain"),
111111
),
112112
# A late-bound attribute denoting the value of the `--custom_malloc`
113113
# command line flag (or None if the flag is not provided).

swift/internal/swift_autoconfiguration.bzl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ should be loaded here. Do not load anything else, even common libraries like
2424
Skylib.
2525
"""
2626

27+
load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain")
28+
load(
29+
"@bazel_tools//tools/cpp:lib_cc_configure.bzl",
30+
"get_cpu_value",
31+
)
2732
load(
2833
"@build_bazel_rules_swift//swift/internal:feature_names.bzl",
2934
"SWIFT_FEATURE_CODEVIEW_DEBUG_INFO",
@@ -231,6 +236,22 @@ def _normalized_linux_cpu(cpu):
231236
return "x86_64"
232237
return cpu
233238

239+
def _create_linux_cc_toolchain(repository_ctx):
240+
path_to_swiftc = repository_ctx.which("swiftc")
241+
if not path_to_swiftc:
242+
fail("No 'swiftc' executable found in $PATH")
243+
244+
toolchain_root = path_to_swiftc.dirname
245+
cpu = get_cpu_value(repository_ctx)
246+
configure_unix_toolchain(repository_ctx, cpu, overriden_tools = {
247+
"ar": toolchain_root.get_child("llvm-ar"),
248+
"ld": toolchain_root.get_child("lld"),
249+
"llvm-cov": toolchain_root.get_child("llvm-cov"),
250+
"llvm-profdata": toolchain_root.get_child("llvm-profdata"),
251+
"cpp": toolchain_root.get_child("clang-cpp"),
252+
"gcc": toolchain_root.get_child("clang"),
253+
})
254+
234255
def _create_linux_toolchain(repository_ctx):
235256
"""Creates BUILD targets for the Swift toolchain on Linux.
236257
@@ -241,6 +262,7 @@ def _create_linux_toolchain(repository_ctx):
241262
if not path_to_swiftc:
242263
fail("No 'swiftc' executable found in $PATH")
243264

265+
toolchain_root = path_to_swiftc.dirname
244266
root = path_to_swiftc.dirname.dirname
245267
feature_values = _compute_feature_values(repository_ctx, path_to_swiftc)
246268
version_file = _write_swift_version(repository_ctx, path_to_swiftc)
@@ -262,6 +284,11 @@ load(
262284
263285
package(default_visibility = ["//visibility:public"])
264286
287+
alias(
288+
name = "swift_cc_toolchain",
289+
actual = "@build_bazel_rules_swift_local_cc_config//:toolchain"
290+
)
291+
265292
swift_toolchain(
266293
name = "toolchain",
267294
arch = "{cpu}",
@@ -277,6 +304,7 @@ swift_toolchain(
277304
for feature in feature_values
278305
]),
279306
root = root,
307+
toolchain_root = toolchain_root,
280308
version_file = version_file,
281309
),
282310
)
@@ -370,6 +398,12 @@ load(
370398
371399
package(default_visibility = ["//visibility:public"])
372400
401+
# Use the system C++ toolchain
402+
alias(
403+
name = "swift_cc_toolchain",
404+
actual = "@bazel_tools//tools/cpp:current_cc_toolchain"
405+
)
406+
373407
swift_toolchain(
374408
name = "toolchain",
375409
arch = "x86_64",
@@ -392,6 +426,13 @@ swift_toolchain(
392426
),
393427
)
394428

429+
def _swift_cc_autoconfiguration_impl(repository_ctx):
430+
os_name = repository_ctx.os.name.lower()
431+
if os_name.startswith("linux"):
432+
_create_linux_cc_toolchain(repository_ctx)
433+
else:
434+
fail("cc_toolchain detection for Swift is currently only supported on Linux")
435+
395436
def _swift_autoconfiguration_impl(repository_ctx):
396437
# TODO(allevato): This is expedient and fragile. Use the
397438
# platforms/toolchains APIs instead to define proper toolchains, and make it
@@ -404,6 +445,12 @@ def _swift_autoconfiguration_impl(repository_ctx):
404445
else:
405446
_create_linux_toolchain(repository_ctx)
406447

448+
swift_cc_autoconfiguration = repository_rule(
449+
environ = ["PATH"],
450+
implementation = _swift_cc_autoconfiguration_impl,
451+
local = True,
452+
)
453+
407454
swift_autoconfiguration = repository_rule(
408455
environ = ["CC", "PATH", "ProgramData", "Path"],
409456
implementation = _swift_autoconfiguration_impl,

swift/internal/swift_import.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ The `.swiftmodule` file provided to Swift targets that depend on this target.
172172
mandatory = False,
173173
),
174174
"_cc_toolchain": attr.label(
175-
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
175+
default = Label("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"),
176176
doc = """\
177177
The C++ toolchain from which linking flags and other tools needed by the Swift
178178
toolchain (such as `clang`) will be retrieved.

swift/internal/swift_toolchain.bzl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,6 @@ def _swift_toolchain_impl(ctx):
272272
toolchain_root = ctx.attr.root
273273
cc_toolchain = find_cpp_toolchain(ctx)
274274

275-
if cc_toolchain.compiler != "clang":
276-
fail("Swift requires the configured CC toolchain to be LLVM (clang). " +
277-
"Either use the locally installed LLVM by setting `CC=clang` in your environment " +
278-
"before invoking Bazel, or configure a Bazel LLVM CC toolchain.")
279-
280275
if ctx.attr.os == "windows":
281276
swift_linkopts_cc_info = _swift_windows_linkopts_cc_info(
282277
ctx.attr.arch,
@@ -437,7 +432,7 @@ configuration options that are applied to targets on a per-package basis.
437432
allow_single_file = True,
438433
),
439434
"_cc_toolchain": attr.label(
440-
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
435+
default = Label("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"),
441436
doc = """\
442437
The C++ toolchain from which other tools needed by the Swift toolchain (such as
443438
`clang` and `ar`) will be retrieved.

swift/internal/xcode_swift_toolchain.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ configuration options that are applied to targets on a per-package basis.
779779
providers = [[SwiftPackageConfigurationInfo]],
780780
),
781781
"_cc_toolchain": attr.label(
782-
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
782+
default = Label("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"),
783783
doc = """\
784784
The C++ toolchain from which linking flags and other tools needed by the Swift
785785
toolchain (such as `clang`) will be retrieved.

swift/repositories.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1818
load(
1919
"@build_bazel_rules_swift//swift/internal:swift_autoconfiguration.bzl",
2020
"swift_autoconfiguration",
21+
"swift_cc_autoconfiguration",
2122
)
2223

2324
def _maybe(repo_rule, name, **kwargs):
@@ -183,6 +184,11 @@ def swift_rules_dependencies(include_bzlmod_ready_dependencies = True):
183184
sha256 = "28c1ffa39d99e74ed70623899b207b41f79214c498c603915aef55972a851a15",
184185
)
185186

187+
_maybe(
188+
swift_cc_autoconfiguration,
189+
name = "build_bazel_rules_swift_local_cc_config",
190+
)
191+
186192
_maybe(
187193
swift_autoconfiguration,
188194
name = "build_bazel_rules_swift_local_config",

0 commit comments

Comments
 (0)