Skip to content

Commit

Permalink
Fix _cc_toolchain attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Sep 21, 2023
1 parent 5e6c76b commit abde801
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
66 changes: 49 additions & 17 deletions swift/internal/swift_autoconfiguration.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"),
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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}",
Expand Down Expand Up @@ -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",
Expand All @@ -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
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("@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.
Expand Down
2 changes: 1 addition & 1 deletion swift/internal/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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("@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.
Expand Down

0 comments on commit abde801

Please sign in to comment.