Skip to content

Commit abde801

Browse files
committed
Fix _cc_toolchain attribute values
1 parent 5e6c76b commit abde801

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

MODULE.bazel

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ non_module_deps = use_extension("//swift:extensions.bzl", "non_module_deps")
1818
use_repo(
1919
non_module_deps,
2020
"build_bazel_rules_swift_index_import",
21-
"build_bazel_rules_swift_local_config",
2221
"build_bazel_rules_swift_local_cc_config",
22+
"build_bazel_rules_swift_local_config",
2323
"com_github_apple_swift_log",
2424
"com_github_apple_swift_nio",
2525
"com_github_apple_swift_nio_extras",
@@ -35,4 +35,4 @@ use_repo(apple_cc_configure, "local_config_apple_cc")
3535
# Dev dependencies
3636
bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True, repo_name = "io_bazel_stardoc")
3737

38-
register_toolchains("@build_bazel_rules_swift_cc_toolchain//:all")
38+
register_toolchains("@build_bazel_rules_swift_local_cc_config//:all")

swift/internal/swift_autoconfiguration.bzl

+49-17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Skylib.
2525
"""
2626

2727
load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain")
28+
load("@bazel_tools//tools/cpp:windows_cc_configure.bzl", "configure_windows_toolchain")
2829
load(
2930
"@bazel_tools//tools/cpp:lib_cc_configure.bzl",
3031
"get_cpu_value",
@@ -261,12 +262,35 @@ def _normalized_linux_cpu(cpu):
261262
return "x86_64"
262263
return cpu
263264

264-
def _create_linux_cc_toolchain(repository_ctx):
265+
def _toolchain_root(repository_ctx):
265266
path_to_swiftc = repository_ctx.which("swiftc")
266267
if not path_to_swiftc:
267268
fail("No 'swiftc' executable found in $PATH")
269+
return path_to_swiftc.dirname
270+
271+
def _create_xcode_cc_toolchain(repository_ctx):
272+
"""Creates BUILD alias for the C++ toolchain provided by apple_support
268273
269-
toolchain_root = path_to_swiftc.dirname
274+
Args:
275+
repository_ctx: The repository rule context.
276+
"""
277+
278+
repository_ctx.file("BUILD", """
279+
alias(
280+
name = "toolchain",
281+
actual = "@local_config_apple_cc//:toolchain",
282+
visibility = ["//visibility:public"]
283+
)
284+
""")
285+
286+
def _create_linux_cc_toolchain(repository_ctx):
287+
"""Creates BUILD targets for the Swift-provided C++ toolchain on Linux.
288+
289+
Args:
290+
repository_ctx: The repository rule context.
291+
"""
292+
293+
toolchain_root = _toolchain_root(repository_ctx)
270294
cpu = get_cpu_value(repository_ctx)
271295
configure_unix_toolchain(repository_ctx, cpu, overriden_tools = {
272296
"ar": toolchain_root.get_child("llvm-ar"),
@@ -277,6 +301,23 @@ def _create_linux_cc_toolchain(repository_ctx):
277301
"gcc": toolchain_root.get_child("clang"),
278302
})
279303

304+
def _create_windows_cc_toolchain(repository_ctx):
305+
"""Creates BUILD targets for the Swift-provided C++ toolchain on Windows.
306+
307+
Args:
308+
repository_ctx: The repository rule context.
309+
"""
310+
311+
toolchain_root = _toolchain_root(repository_ctx)
312+
configure_windows_toolchain(repository_ctx, overriden_tools = {
313+
"ar": toolchain_root.get_child("llvm-ar.exe"),
314+
"ld": toolchain_root.get_child("lld.exe"),
315+
"llvm-cov": toolchain_root.get_child("llvm-cov.exe"),
316+
"llvm-profdata": toolchain_root.get_child("llvm-profdata.exe"),
317+
"cpp": toolchain_root.get_child("clang-cpp.exe"),
318+
"gcc": toolchain_root.get_child("clang.exe"),
319+
})
320+
280321
def _create_linux_toolchain(repository_ctx):
281322
"""Creates BUILD targets for the Swift toolchain on Linux.
282323
@@ -287,7 +328,7 @@ def _create_linux_toolchain(repository_ctx):
287328
if not path_to_swiftc:
288329
fail("No 'swiftc' executable found in $PATH")
289330

290-
toolchain_root = path_to_swiftc.dirname
331+
toolchain_root = _toolchain_root(repository_ctx)
291332
root = path_to_swiftc.dirname.dirname
292333
feature_values = _compute_feature_values(repository_ctx, path_to_swiftc)
293334
version_file = _write_swift_version(repository_ctx, path_to_swiftc)
@@ -313,11 +354,6 @@ load(
313354
314355
package(default_visibility = ["//visibility:public"])
315356
316-
alias(
317-
name = "swift_cc_toolchain",
318-
actual = "@build_bazel_rules_swift_local_cc_config//:toolchain"
319-
)
320-
321357
swift_toolchain(
322358
name = "toolchain",
323359
arch = "{cpu}",
@@ -427,12 +463,6 @@ load(
427463
428464
package(default_visibility = ["//visibility:public"])
429465
430-
# Use the system C++ toolchain
431-
alias(
432-
name = "swift_cc_toolchain",
433-
actual = "@bazel_tools//tools/cpp:current_cc_toolchain"
434-
)
435-
436466
swift_toolchain(
437467
name = "toolchain",
438468
arch = "x86_64",
@@ -457,10 +487,12 @@ swift_toolchain(
457487

458488
def _swift_cc_autoconfiguration_impl(repository_ctx):
459489
os_name = repository_ctx.os.name.lower()
460-
if os_name.startswith("linux"):
461-
_create_linux_cc_toolchain(repository_ctx)
490+
if os_name.startswith("mac os"):
491+
_create_xcode_cc_toolchain(repository_ctx)
492+
elif os_name.startswith("windows"):
493+
_create_windows_cc_toolchain(repository_ctx)
462494
else:
463-
fail("cc_toolchain detection for Swift is currently only supported on Linux")
495+
_create_linux_cc_toolchain(repository_ctx)
464496

465497
def _swift_autoconfiguration_impl(repository_ctx):
466498
# TODO(allevato): This is expedient and fragile. Use the

swift/internal/swift_import.bzl

+1-1
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("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"),
175+
default = Label("@build_bazel_rules_swift_local_cc_config//: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

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ configuration options that are applied to targets on a per-package basis.
432432
allow_single_file = True,
433433
),
434434
"_cc_toolchain": attr.label(
435-
default = Label("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"),
435+
default = Label("@build_bazel_rules_swift_local_cc_config//:toolchain"),
436436
doc = """\
437437
The C++ toolchain from which other tools needed by the Swift toolchain (such as
438438
`clang` and `ar`) will be retrieved.

swift/internal/xcode_swift_toolchain.bzl

+1-1
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("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"),
782+
default = Label("@build_bazel_rules_swift_local_cc_config//: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.

0 commit comments

Comments
 (0)