diff --git a/MODULE.bazel b/MODULE.bazel index a2bbdb073..601c7f5bb 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -18,8 +18,8 @@ non_module_deps = use_extension("//swift:extensions.bzl", "non_module_deps") use_repo( non_module_deps, "build_bazel_rules_swift_index_import", - "build_bazel_rules_swift_local_config", "build_bazel_rules_swift_local_cc_config", + "build_bazel_rules_swift_local_config", "com_github_apple_swift_log", "com_github_apple_swift_nio", "com_github_apple_swift_nio_extras", @@ -35,4 +35,4 @@ 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") +register_toolchains("@build_bazel_rules_swift_local_cc_config//:all") diff --git a/swift/internal/swift_autoconfiguration.bzl b/swift/internal/swift_autoconfiguration.bzl index c5567e5c0..05593d3eb 100644 --- a/swift/internal/swift_autoconfiguration.bzl +++ b/swift/internal/swift_autoconfiguration.bzl @@ -25,6 +25,7 @@ Skylib. """ load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain") +load("@bazel_tools//tools/cpp:windows_cc_configure.bzl", "configure_windows_toolchain") load( "@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value", @@ -261,12 +262,35 @@ def _normalized_linux_cpu(cpu): return "x86_64" return cpu -def _create_linux_cc_toolchain(repository_ctx): +def _toolchain_root(repository_ctx): path_to_swiftc = repository_ctx.which("swiftc") if not path_to_swiftc: fail("No 'swiftc' executable found in $PATH") + return path_to_swiftc.dirname + +def _create_xcode_cc_toolchain(repository_ctx): + """Creates BUILD alias for the C++ toolchain provided by apple_support - toolchain_root = path_to_swiftc.dirname + Args: + repository_ctx: The repository rule context. + """ + + repository_ctx.file("BUILD", """ +alias( + name = "toolchain", + actual = "@local_config_apple_cc//:toolchain", + visibility = ["//visibility:public"] +) + """) + +def _create_linux_cc_toolchain(repository_ctx): + """Creates BUILD targets for the Swift-provided C++ toolchain on Linux. + + Args: + repository_ctx: The repository rule context. + """ + + toolchain_root = _toolchain_root(repository_ctx) cpu = get_cpu_value(repository_ctx) configure_unix_toolchain(repository_ctx, cpu, overriden_tools = { "ar": toolchain_root.get_child("llvm-ar"), @@ -277,6 +301,23 @@ def _create_linux_cc_toolchain(repository_ctx): "gcc": toolchain_root.get_child("clang"), }) +def _create_windows_cc_toolchain(repository_ctx): + """Creates BUILD targets for the Swift-provided C++ toolchain on Windows. + + Args: + repository_ctx: The repository rule context. + """ + + toolchain_root = _toolchain_root(repository_ctx) + configure_windows_toolchain(repository_ctx, overriden_tools = { + "ar": toolchain_root.get_child("llvm-ar.exe"), + "ld": toolchain_root.get_child("lld.exe"), + "llvm-cov": toolchain_root.get_child("llvm-cov.exe"), + "llvm-profdata": toolchain_root.get_child("llvm-profdata.exe"), + "cpp": toolchain_root.get_child("clang-cpp.exe"), + "gcc": toolchain_root.get_child("clang.exe"), + }) + def _create_linux_toolchain(repository_ctx): """Creates BUILD targets for the Swift toolchain on Linux. @@ -287,7 +328,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 + toolchain_root = _toolchain_root(repository_ctx) 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) @@ -313,11 +354,6 @@ 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}", @@ -427,12 +463,6 @@ 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", @@ -457,10 +487,12 @@ 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) + if os_name.startswith("mac os"): + _create_xcode_cc_toolchain(repository_ctx) + elif os_name.startswith("windows"): + _create_windows_cc_toolchain(repository_ctx) else: - fail("cc_toolchain detection for Swift is currently only supported on Linux") + _create_linux_cc_toolchain(repository_ctx) def _swift_autoconfiguration_impl(repository_ctx): # TODO(allevato): This is expedient and fragile. Use the diff --git a/swift/internal/swift_import.bzl b/swift/internal/swift_import.bzl index 917bc6661..cff6d7e1f 100644 --- a/swift/internal/swift_import.bzl +++ b/swift/internal/swift_import.bzl @@ -172,7 +172,7 @@ The `.swiftmodule` file provided to Swift targets that depend on this target. mandatory = False, ), "_cc_toolchain": attr.label( - default = Label("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"), + default = Label("@build_bazel_rules_swift_local_cc_config//:toolchain"), doc = """\ The C++ toolchain from which linking flags and other tools needed by the Swift toolchain (such as `clang`) will be retrieved. diff --git a/swift/internal/swift_toolchain.bzl b/swift/internal/swift_toolchain.bzl index 88ad1d407..a92fc4e38 100644 --- a/swift/internal/swift_toolchain.bzl +++ b/swift/internal/swift_toolchain.bzl @@ -432,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("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"), + default = Label("@build_bazel_rules_swift_local_cc_config//:toolchain"), doc = """\ The C++ toolchain from which other tools needed by the Swift toolchain (such as `clang` and `ar`) will be retrieved. diff --git a/swift/internal/xcode_swift_toolchain.bzl b/swift/internal/xcode_swift_toolchain.bzl index 21955de05..3bc38811c 100644 --- a/swift/internal/xcode_swift_toolchain.bzl +++ b/swift/internal/xcode_swift_toolchain.bzl @@ -779,7 +779,7 @@ configuration options that are applied to targets on a per-package basis. providers = [[SwiftPackageConfigurationInfo]], ), "_cc_toolchain": attr.label( - default = Label("@build_bazel_rules_swift_local_config//:swift_cc_toolchain"), + default = Label("@build_bazel_rules_swift_local_cc_config//:toolchain"), doc = """\ The C++ toolchain from which linking flags and other tools needed by the Swift toolchain (such as `clang`) will be retrieved.