Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bazel_dep(name = "gazelle", version = "0.54.0")
bazel_dep(name = "rules_dotnet", version = "0.22.2-codeql.1")
bazel_dep(name = "googletest", version = "1.18.0.bcr.1")
bazel_dep(name = "rules_rust", version = "0.75.0")
bazel_dep(name = "rules_swift", version = "4.0.0-rc5-codeql.3")
bazel_dep(name = "rules_swift", version = "4.2.2-codeql.1")
bazel_dep(name = "swift-syntax", version = "604.0.0")
bazel_dep(name = "zstd", version = "1.5.7.bcr.3")

Expand Down
495 changes: 495 additions & 0 deletions misc/bazel/registry/modules/rules_swift/4.2.2-codeql.1/MODULE.bazel

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
diff --git a/swift/extensions.bzl b/swift/extensions.bzl
index 9a4ffd5..b42d7dc 100644
--- a/swift/extensions.bzl
+++ b/swift/extensions.bzl
@@ -186,6 +186,7 @@ def _standalone_toolchain_impl(module_ctx):
name = repository_name,
sha256 = sha256,
platform = platform,
+ static_runtime = toolchain.static_runtime if platform != "xcode" else None,
swift_version = swift_version,
)
toolchains_build_file_content += toolchains_for_platform(
@@ -261,6 +262,9 @@ their hashes. For instance:
`bazel run @rules_swift//tools/swift-releases -- list 6.2.4`
""",
),
+ "static_runtime": attr.label(
+ doc = "External static runtime package to attach to the generated execution toolchains.",
+ ),
"swift_version": attr.string(doc = "Version of the swift toolchain to be installed. Cannot be used concurrently with `swift_version_file`"),
"swift_version_file": attr.label(doc = "A label to the .swift_version file to use. Cannot be used concurrently with `swift_version`"),
})
diff --git a/swift/internal/extensions/standalone_toolchain.bzl b/swift/internal/extensions/standalone_toolchain.bzl
index e3e621d..3fd890a 100644
--- a/swift/internal/extensions/standalone_toolchain.bzl
+++ b/swift/internal/extensions/standalone_toolchain.bzl
@@ -110,6 +110,9 @@ def _standalone_toolchain_impl(repository_ctx):
"BUILD.bazel",
repository_ctx.attr._build_template,
substitutions = {
+ "{external_static_runtime}": (
+ ' external_static_runtime = "{}",'.format(repository_ctx.attr.static_runtime) if repository_ctx.attr.static_runtime else ""
+ ),
"{linux_sysroot_version}": linux_sysroot_version,
"{swift_version}": repository_ctx.attr.swift_version,
},
@@ -125,6 +128,9 @@ standalone_toolchain = repository_rule(
"sha256": attr.string(
doc = "The expected SHA-256 of the file downloaded. This must match the SHA-256 of the file downloaded.",
),
+ "static_runtime": attr.label(
+ doc = "External static runtime package used by this execution toolchain.",
+ ),
"swift_version": attr.string(
doc = "Version of the swift toolchain to be installed.",
mandatory = True,
diff --git a/swift/internal/extensions/toolchain.BUILD b/swift/internal/extensions/toolchain.BUILD
index 5f950a4..6981a02 100644
--- a/swift/internal/extensions/toolchain.BUILD
+++ b/swift/internal/extensions/toolchain.BUILD
@@ -377,6 +377,7 @@ swift_toolchain(
),
"//conditions:default": [],
}),
+{external_static_runtime}
swift_tools = "tools",
version_file = ".swift-version",
)
diff --git a/swift/toolchains/BUILD b/swift/toolchains/BUILD
index 1a8db31..fff0879 100644
--- a/swift/toolchains/BUILD
+++ b/swift/toolchains/BUILD
@@ -155,6 +155,7 @@ bzl_library(
"//swift/internal:features",
"//swift/internal:providers",
"//swift/internal:target_triples",
+ "//swift/internal:toolchain_utils",
"//swift/internal:utils",
"//swift/internal:wmo",
"//swift/toolchains/config:action_config",
diff --git a/swift/toolchains/swift_toolchain.bzl b/swift/toolchains/swift_toolchain.bzl
index 071b278..3579131 100644
--- a/swift/toolchains/swift_toolchain.bzl
+++ b/swift/toolchains/swift_toolchain.bzl
@@ -71,6 +71,11 @@ load(
"SwiftModuleAliasesInfo",
)
load("//swift/internal:target_triples.bzl", "target_triples")
+load(
+ "//swift/internal:toolchain_utils.bzl",
+ "get_swift_toolchain",
+ "use_swift_toolchain",
+)
load(
"//swift/internal:utils.bzl",
"collect_cross_import_overlays",
@@ -103,6 +108,64 @@ load(
)
load("//swift/toolchains/config:tool_config.bzl", "ToolConfigInfo")

+SwiftStaticRuntimeInfo = provider(
+ fields = {
+ "copts": "Compiler options required to use the runtime.",
+ "files": "Runtime and SDK files required by compile and link actions.",
+ "linkopts": "Linker options required to link the runtime.",
+ "root": "Execution-root-relative path to the runtime SDK.",
+ },
+)
+
+def _swift_static_runtime_impl(ctx):
+ root = ctx.label.workspace_root
+ files = depset(ctx.files.files)
+
+ def expand_root(options):
+ return [option.replace("{root}", root) for option in options]
+
+ return [
+ DefaultInfo(files = files),
+ SwiftStaticRuntimeInfo(
+ copts = expand_root(ctx.attr.copts),
+ files = files,
+ linkopts = expand_root(ctx.attr.linkopts),
+ root = root,
+ ),
+ ]
+
+swift_static_runtime = rule(
+ implementation = _swift_static_runtime_impl,
+ attrs = {
+ "copts": attr.string_list(),
+ "files": attr.label_list(allow_files = True, mandatory = True),
+ "linkopts": attr.string_list(),
+ },
+)
+
+def _swift_runtime_impl(ctx):
+ runtime_cc_info = get_swift_toolchain(ctx).dynamic_runtime_cc_info
+ files = []
+ if runtime_cc_info:
+ for linker_input in runtime_cc_info.linking_context.linker_inputs.to_list():
+ for library in linker_input.libraries:
+ dynamic_library = getattr(library, "dynamic_library", None)
+ if dynamic_library:
+ files.append(dynamic_library)
+ runtime = depset(files)
+ providers = [DefaultInfo(
+ files = runtime,
+ runfiles = ctx.runfiles(transitive_files = runtime),
+ )]
+ if runtime_cc_info:
+ providers.append(runtime_cc_info)
+ return providers
+
+swift_runtime = rule(
+ implementation = _swift_runtime_impl,
+ toolchains = use_swift_toolchain(),
+)
+
def _swift_compile_resource_set(_os, inputs_size):
# The `os` argument is unused, but the Starlark API requires both
# positional arguments.
@@ -674,6 +737,7 @@ def _resolve_sdkroot(ctx, cc_toolchain):

def _swift_toolchain_impl(ctx):
toolchain_root = ctx.attr.root
+ external_static_runtime = ctx.attr.external_static_runtime[SwiftStaticRuntimeInfo] if ctx.attr.external_static_runtime else None
cc_toolchain = find_cc_toolchain(ctx)
target_system_name = _parse_target_system_name(
arch = ctx.attr.arch,
@@ -696,7 +760,7 @@ def _swift_toolchain_impl(ctx):
"before invoking Bazel, or configure a Bazel LLVM CC toolchain. " +
"The current CC toolchain is configured to use '{}'.".format(cc_toolchain.compiler))

- sdkroot = _resolve_sdkroot(ctx, cc_toolchain)
+ sdkroot = external_static_runtime.root if external_static_runtime else _resolve_sdkroot(ctx, cc_toolchain)

if ctx.attr.swift_tools:
if ctx.attr.swift_executable:
@@ -741,6 +805,18 @@ def _swift_toolchain_impl(ctx):
ctx.attr.linkopts,
ctx.files.linker_inputs,
)
+ elif external_static_runtime:
+ swift_linkopts_cc_info = CcInfo(
+ linking_context = cc_common.create_linking_context(
+ linker_inputs = depset([
+ cc_common.create_linker_input(
+ owner = ctx.label,
+ user_link_flags = depset(external_static_runtime.linkopts),
+ additional_inputs = external_static_runtime.files,
+ ),
+ ]),
+ ),
+ )
else:
(
swift_linkopts_cc_info,
@@ -768,6 +844,8 @@ def _swift_toolchain_impl(ctx):
swiftcopts.extend(ctx.attr._exec_copts[BuildSettingInfo].value)
else:
swiftcopts.extend(ctx.attr._copts[BuildSettingInfo].value)
+ if external_static_runtime:
+ swiftcopts.extend(external_static_runtime.copts)

# Combine build mode features, autoconfigured features, and required
# features.
@@ -779,7 +857,7 @@ def _swift_toolchain_impl(ctx):
target_triple = target_triple,
))

- if apple_common.dotted_version(ctx.attr.parsed_version) >= apple_common.dotted_version("6.3"):
+ if not external_static_runtime and apple_common.dotted_version(ctx.attr.parsed_version) >= apple_common.dotted_version("6.3"):
requested_features.append(SWIFT_FEATURE__SUPPORTS_HERMETIC_SWIFTMODULE)

if apple_common.dotted_version(ctx.attr.parsed_version) >= apple_common.dotted_version("6.4"):
@@ -796,6 +874,8 @@ def _swift_toolchain_impl(ctx):
additional_tools = [ctx.file.version_file]
if ctx.attr.swift_tools:
additional_tools += ctx.attr.swift_tools[SwiftToolsInfo].additional_inputs
+ if external_static_runtime:
+ additional_tools += external_static_runtime.files.to_list()

all_tool_configs = _all_tool_configs(
env = ctx.attr.env,
@@ -883,7 +963,7 @@ def _swift_toolchain_impl(ctx):
implicit_system_modules = collect_implicit_deps_providers([]),
swift_worker = ctx.attr._worker[DefaultInfo].files_to_run,
swift_worker_protocol = "proto" if ctx.attr._use_proto_worker[BuildSettingInfo].value else "json",
- const_protocols_to_gather = ctx.file.const_protocols_to_gather,
+ const_protocols_to_gather = None if external_static_runtime else ctx.file.const_protocols_to_gather,
test_configuration = struct(
binary_name = "{name}",
env = env,
@@ -1013,6 +1093,10 @@ The root of a SDK to be used for building the target.
""",
mandatory = False,
),
+ "external_static_runtime": attr.label(
+ doc = "External static Swift runtime and SDK to use instead of the compiler's runtimes.",
+ providers = [[SwiftStaticRuntimeInfo]],
+ ),
"static_runtime": attr.label_list(
doc = """\
Static Swift runtime archives and supporting linker files that are passed to
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -5 +5 @@
- version = "0",
+ version = "4.2.2-codeql.1",
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
diff --git a/swift/internal/extensions/standalone_toolchain.bzl b/swift/internal/extensions/standalone_toolchain.bzl
index 3fd890a..dfa6c39 100644
--- a/swift/internal/extensions/standalone_toolchain.bzl
+++ b/swift/internal/extensions/standalone_toolchain.bzl
@@ -100,6 +100,11 @@ def _standalone_toolchain_impl(repository_ctx):
if not repository_ctx.path("usr/bin/llvm-strip").exists:
repository_ctx.symlink("usr/bin/llvm-objcopy", "usr/bin/llvm-strip")

+ macos_sdkroot = ""
+ if repository_ctx.attr.platform == "xcode":
+ macos_sdkroot = _run(repository_ctx, ["xcrun", "--sdk", "macosx", "--show-sdk-path"])
+ repository_ctx.watch(macos_sdkroot)
+
repository_ctx.file(".swift-version", repository_ctx.attr.swift_version)

linux_sysroot_version = "22.04"
@@ -114,6 +119,7 @@ def _standalone_toolchain_impl(repository_ctx):
' external_static_runtime = "{}",'.format(repository_ctx.attr.static_runtime) if repository_ctx.attr.static_runtime else ""
),
"{linux_sysroot_version}": linux_sysroot_version,
+ "{macos_sdkroot}": macos_sdkroot,
"{swift_version}": repository_ctx.attr.swift_version,
},
)
diff --git a/swift/internal/extensions/toolchain.BUILD b/swift/internal/extensions/toolchain.BUILD
index 6981a02..7ea996c 100644
--- a/swift/internal/extensions/toolchain.BUILD
+++ b/swift/internal/extensions/toolchain.BUILD
@@ -351,19 +351,24 @@ swift_toolchain(
features = [
"swift._supports_upcoming_features",
"swift.no_embed_debug_module",
- "swift.use_autolink_extract",
- "swift.lld_gc_workaround",
- "swift.use_module_wrap",
# TODO: This should be removed so that private headers can be used with
# explicit modules, but the build targets for CgRPC need to be cleaned up
# first because they contain C++ code.
"swift.module_map_no_private_headers",
- ],
+ ] + select({
+ "@platforms//os:linux": [
+ "swift.lld_gc_workaround",
+ "swift.use_autolink_extract",
+ "swift.use_module_wrap",
+ ],
+ "//conditions:default": [],
+ }),
os = select({
"@platforms//os:linux": "linux",
"@platforms//os:macos": "macos",
}),
parsed_version = "{swift_version}",
+ sdkroot = "{macos_sdkroot}",
static_runtime = select({
"@platforms//os:linux": glob(
[
diff --git a/swift/internal/extensions/toolchains.bzl b/swift/internal/extensions/toolchains.bzl
index c969e31..faf4547 100644
--- a/swift/internal/extensions/toolchains.bzl
+++ b/swift/internal/extensions/toolchains.bzl
@@ -125,12 +125,11 @@ def toolchains_for_platform(platform, toolchain_repository):
toolchain_repository = toolchain_repository,
)

- if platform != "xcode":
- content += _EXEC_TOOLCHAIN_PLATFORM.format(
- exec_compatible_with = _exec_compatible_with_for_platform(platform),
- platform = platform,
- toolchain_repository = toolchain_repository,
- )
+ content += _EXEC_TOOLCHAIN_PLATFORM.format(
+ exec_compatible_with = _exec_compatible_with_for_platform(platform),
+ platform = platform,
+ toolchain_repository = toolchain_repository,
+ )

if platform in (
"ubuntu22.04",
diff --git a/swift/toolchains/swift_toolchain.bzl b/swift/toolchains/swift_toolchain.bzl
index 3579131..afb9ac1 100644
--- a/swift/toolchains/swift_toolchain.bzl
+++ b/swift/toolchains/swift_toolchain.bzl
@@ -669,10 +669,14 @@ def _parse_target_system_name(*, arch, os, target_system_name):
"""Returns the target system name set by the CC toolchain or attempts to create one based on the OS and arch."""

if target_system_name and target_system_name != "local":
+ if os == "macos" and target_system_name.endswith(("-macos", "-macosx")):
+ return target_system_name + "10.15"
return target_system_name

if os == "linux":
return "%s-unknown-linux-gnu" % arch
+ elif os == "macos":
+ return "%s-apple-macos10.15" % arch
elif os == "windows":
# The MSVC cc toolchain reports a `target_gnu_system_name` of "local",
# so synthesize the triple.
@@ -785,6 +789,17 @@ def _swift_toolchain_impl(ctx):
)
elif ctx.attr.os == "none":
swift_linkopts_cc_info = CcInfo()
+ elif ctx.attr.os == "macos":
+ macos_linkopts = ["-L{}".format(paths.join(toolchain_root, "lib/swift/macosx"))]
+ if sdkroot:
+ macos_linkopts.append("-L{}".format(paths.join(sdkroot, "usr/lib/swift")))
+ swift_linkopts_cc_info = _swift_sdk_linkopts_cc_info(
+ ctx.label,
+ macos_linkopts,
+ ctx.attr.swift_tools[SwiftToolsInfo].additional_inputs if ctx.attr.swift_tools else [],
+ )
+ dynamic_runtime_cc_info = None
+ static_runtime_cc_info = None
elif ctx.attr.os == "android":
if not sdkroot:
fail("Android toolchain requires a sysroot to be set, either via the `sdkroot` attribute or by using a CC toolchain that provides one.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"integrity": "sha256-0qlu7XFoSHcFHojdLCp3LtvbdU3kiM7gZt1yzOJAz8Y=",
"url": "https://github.com/bazelbuild/rules_swift/releases/download/4.2.2/rules_swift.4.2.2.tar.gz",
"patches": {
"external_static_runtime.patch": "sha256-kFxCR0aeygkmsSWQur8JpStRepc9sqI+WENUd61wEOo=",
"module_dot_bazel_version.patch": "sha256-9hq60gxavLu+lHxu15eHjUgWXjIRvkbStMWTC5PiZ7k=",
"register_downloaded_macos_toolchain.patch": "sha256-JWvdIt3rWsbS5zZQ1lZJv08uEaTa1OJPs5t/lMGi6fc="
},
"patch_strip": 1
}
1 change: 1 addition & 0 deletions misc/bazel/registry/modules/rules_swift/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"github:bazelbuild/rules_swift"
],
"versions": [
"4.2.2-codeql.1",
"4.0.0-rc5-codeql.3"
],
"yanked_versions": {}
Expand Down
Loading