|
| 1 | +diff --git a/swift/extensions.bzl b/swift/extensions.bzl |
| 2 | +index b77f636..1255ba7 100644 |
| 3 | +--- a/swift/extensions.bzl |
| 4 | ++++ b/swift/extensions.bzl |
| 5 | +@@ -191,6 +191,7 @@ def _standalone_toolchain_impl(module_ctx): |
| 6 | + name = repository_name, |
| 7 | + sha256 = sha256, |
| 8 | + platform = platform, |
| 9 | ++ static_runtime = toolchain.static_runtime if platform != "xcode" else None, |
| 10 | + swift_version = swift_version, |
| 11 | + ) |
| 12 | + toolchains_build_file_content += toolchains_for_platform( |
| 13 | +@@ -266,6 +267,9 @@ their hashes. For instance: |
| 14 | + `bazel run @rules_swift//tools/swift-releases -- list 6.2.4` |
| 15 | + """, |
| 16 | + ), |
| 17 | ++ "static_runtime": attr.label( |
| 18 | ++ doc = "External static runtime package to attach to the generated execution toolchains.", |
| 19 | ++ ), |
| 20 | + "swift_version": attr.string(doc = "Version of the swift toolchain to be installed. Cannot be used concurrently with `swift_version_file`"), |
| 21 | + "swift_version_file": attr.label(doc = "A label to the .swift_version file to use. Cannot be used concurrently with `swift_version`"), |
| 22 | + }) |
| 23 | +diff --git a/swift/internal/extensions/standalone_toolchain.bzl b/swift/internal/extensions/standalone_toolchain.bzl |
| 24 | +index a1e09ea..1fcf56d 100644 |
| 25 | +--- a/swift/internal/extensions/standalone_toolchain.bzl |
| 26 | ++++ b/swift/internal/extensions/standalone_toolchain.bzl |
| 27 | +@@ -105,6 +105,9 @@ def _standalone_toolchain_impl(repository_ctx): |
| 28 | + "BUILD.bazel", |
| 29 | + repository_ctx.attr._build_template, |
| 30 | + substitutions = { |
| 31 | ++ "{external_static_runtime}": ( |
| 32 | ++ ' external_static_runtime = "{}",'.format(repository_ctx.attr.static_runtime) if repository_ctx.attr.static_runtime else "" |
| 33 | ++ ), |
| 34 | + "{macos_sdkroot}": macos_sdkroot, |
| 35 | + "{swift_version}": repository_ctx.attr.swift_version, |
| 36 | + }, |
| 37 | +@@ -121,6 +124,9 @@ _STANDALONE_TOOLCHAIN_ATTRS = { |
| 38 | + "sha256": attr.string( |
| 39 | + doc = "The expected SHA-256 of the file downloaded. This must match the SHA-256 of the file downloaded.", |
| 40 | + ), |
| 41 | ++ "static_runtime": attr.label( |
| 42 | ++ doc = "External static runtime package used by this execution toolchain.", |
| 43 | ++ ), |
| 44 | + "swift_version": attr.string( |
| 45 | + doc = "Version of the swift toolchain to be installed.", |
| 46 | + mandatory = True, |
| 47 | +diff --git a/swift/internal/extensions/toolchain.BUILD b/swift/internal/extensions/toolchain.BUILD |
| 48 | +index 1304dc2..ee77866 100644 |
| 49 | +--- a/swift/internal/extensions/toolchain.BUILD |
| 50 | ++++ b/swift/internal/extensions/toolchain.BUILD |
| 51 | +@@ -364,6 +364,7 @@ swift_toolchain( |
| 52 | + ), |
| 53 | + "//conditions:default": [], |
| 54 | + }), |
| 55 | ++{external_static_runtime} |
| 56 | + swift_tools = "tools", |
| 57 | + version_file = ".swift-version", |
| 58 | + ) |
| 59 | +diff --git a/swift/toolchains/BUILD b/swift/toolchains/BUILD |
| 60 | +index 1a8db31..fff0879 100644 |
| 61 | +--- a/swift/toolchains/BUILD |
| 62 | ++++ b/swift/toolchains/BUILD |
| 63 | +@@ -155,6 +155,7 @@ bzl_library( |
| 64 | + "//swift/internal:features", |
| 65 | + "//swift/internal:providers", |
| 66 | + "//swift/internal:target_triples", |
| 67 | ++ "//swift/internal:toolchain_utils", |
| 68 | + "//swift/internal:utils", |
| 69 | + "//swift/internal:wmo", |
| 70 | + "//swift/toolchains/config:action_config", |
| 71 | +diff --git a/swift/toolchains/swift_toolchain.bzl b/swift/toolchains/swift_toolchain.bzl |
| 72 | +index 9fc2a49..6f10573 100644 |
| 73 | +--- a/swift/toolchains/swift_toolchain.bzl |
| 74 | ++++ b/swift/toolchains/swift_toolchain.bzl |
| 75 | +@@ -69,6 +69,11 @@ load( |
| 76 | + "SwiftModuleAliasesInfo", |
| 77 | + ) |
| 78 | + load("//swift/internal:target_triples.bzl", "target_triples") |
| 79 | ++load( |
| 80 | ++ "//swift/internal:toolchain_utils.bzl", |
| 81 | ++ "get_swift_toolchain", |
| 82 | ++ "use_swift_toolchain", |
| 83 | ++) |
| 84 | + load( |
| 85 | + "//swift/internal:utils.bzl", |
| 86 | + "collect_cross_import_overlays", |
| 87 | +@@ -101,6 +106,64 @@ load( |
| 88 | + ) |
| 89 | + load("//swift/toolchains/config:tool_config.bzl", "ToolConfigInfo") |
| 90 | +- |
| 91 | ++ |
| 92 | ++SwiftStaticRuntimeInfo = provider( |
| 93 | ++ fields = { |
| 94 | ++ "copts": "Compiler options required to use the runtime.", |
| 95 | ++ "files": "Runtime and SDK files required by compile and link actions.", |
| 96 | ++ "linkopts": "Linker options required to link the runtime.", |
| 97 | ++ "root": "Execution-root-relative path to the runtime SDK.", |
| 98 | ++ }, |
| 99 | ++) |
| 100 | ++ |
| 101 | ++def _swift_static_runtime_impl(ctx): |
| 102 | ++ root = ctx.label.workspace_root |
| 103 | ++ files = depset(ctx.files.files) |
| 104 | ++ |
| 105 | ++ def expand_root(options): |
| 106 | ++ return [option.replace("{root}", root) for option in options] |
| 107 | ++ |
| 108 | ++ return [ |
| 109 | ++ DefaultInfo(files = files), |
| 110 | ++ SwiftStaticRuntimeInfo( |
| 111 | ++ copts = expand_root(ctx.attr.copts), |
| 112 | ++ files = files, |
| 113 | ++ linkopts = expand_root(ctx.attr.linkopts), |
| 114 | ++ root = root, |
| 115 | ++ ), |
| 116 | ++ ] |
| 117 | ++ |
| 118 | ++swift_static_runtime = rule( |
| 119 | ++ implementation = _swift_static_runtime_impl, |
| 120 | ++ attrs = { |
| 121 | ++ "copts": attr.string_list(), |
| 122 | ++ "files": attr.label_list(allow_files = True, mandatory = True), |
| 123 | ++ "linkopts": attr.string_list(), |
| 124 | ++ }, |
| 125 | ++) |
| 126 | ++ |
| 127 | ++def _swift_runtime_impl(ctx): |
| 128 | ++ runtime_cc_info = get_swift_toolchain(ctx).dynamic_runtime_cc_info |
| 129 | ++ files = [] |
| 130 | ++ if runtime_cc_info: |
| 131 | ++ for linker_input in runtime_cc_info.linking_context.linker_inputs.to_list(): |
| 132 | ++ for library in linker_input.libraries: |
| 133 | ++ dynamic_library = getattr(library, "dynamic_library", None) |
| 134 | ++ if dynamic_library: |
| 135 | ++ files.append(dynamic_library) |
| 136 | ++ runtime = depset(files) |
| 137 | ++ providers = [DefaultInfo( |
| 138 | ++ files = runtime, |
| 139 | ++ runfiles = ctx.runfiles(transitive_files = runtime), |
| 140 | ++ )] |
| 141 | ++ if runtime_cc_info: |
| 142 | ++ providers.append(runtime_cc_info) |
| 143 | ++ return providers |
| 144 | ++ |
| 145 | ++swift_runtime = rule( |
| 146 | ++ implementation = _swift_runtime_impl, |
| 147 | ++ toolchains = use_swift_toolchain(), |
| 148 | ++) |
| 149 | ++ |
| 150 | + def _swift_compile_resource_set(_os, inputs_size): |
| 151 | + # The `os` argument is unused, but the Starlark API requires both |
| 152 | + # positional arguments. |
| 153 | +@@ -662,6 +725,7 @@ def _resolve_sdkroot(ctx, cc_toolchain): |
| 154 | +- |
| 155 | ++ |
| 156 | + def _swift_toolchain_impl(ctx): |
| 157 | + toolchain_root = ctx.attr.root |
| 158 | ++ external_static_runtime = ctx.attr.external_static_runtime[SwiftStaticRuntimeInfo] if ctx.attr.external_static_runtime else None |
| 159 | + cc_toolchain = find_cc_toolchain(ctx) |
| 160 | + target_system_name = _parse_target_system_name( |
| 161 | + arch = ctx.attr.arch, |
| 162 | +@@ -684,7 +748,7 @@ def _swift_toolchain_impl(ctx): |
| 163 | + "before invoking Bazel, or configure a Bazel LLVM CC toolchain. " + |
| 164 | + "The current CC toolchain is configured to use '{}'.".format(cc_toolchain.compiler)) |
| 165 | +- |
| 166 | ++ |
| 167 | +- sdkroot = _resolve_sdkroot(ctx, cc_toolchain) |
| 168 | ++ sdkroot = external_static_runtime.root if external_static_runtime else _resolve_sdkroot(ctx, cc_toolchain) |
| 169 | +- |
| 170 | ++ |
| 171 | + if ctx.attr.swift_tools: |
| 172 | + if ctx.attr.swift_executable: |
| 173 | +@@ -740,6 +804,18 @@ def _swift_toolchain_impl(ctx): |
| 174 | + ctx.attr.linkopts, |
| 175 | + ctx.files.linker_inputs, |
| 176 | + ) |
| 177 | ++ elif external_static_runtime: |
| 178 | ++ swift_linkopts_cc_info = CcInfo( |
| 179 | ++ linking_context = cc_common.create_linking_context( |
| 180 | ++ linker_inputs = depset([ |
| 181 | ++ cc_common.create_linker_input( |
| 182 | ++ owner = ctx.label, |
| 183 | ++ user_link_flags = depset(external_static_runtime.linkopts), |
| 184 | ++ additional_inputs = external_static_runtime.files, |
| 185 | ++ ), |
| 186 | ++ ]), |
| 187 | ++ ), |
| 188 | ++ ) |
| 189 | + else: |
| 190 | + ( |
| 191 | + swift_linkopts_cc_info, |
| 192 | +@@ -763,6 +839,8 @@ def _swift_toolchain_impl(ctx): |
| 193 | + swiftcopts.extend(ctx.attr._exec_copts[BuildSettingInfo].value) |
| 194 | + else: |
| 195 | + swiftcopts.extend(ctx.attr._copts[BuildSettingInfo].value) |
| 196 | ++ if external_static_runtime: |
| 197 | ++ swiftcopts.extend(external_static_runtime.copts) |
| 198 | +- |
| 199 | ++ |
| 200 | + # Combine build mode features, autoconfigured features, and required |
| 201 | + # features. |
| 202 | +@@ -774,7 +852,7 @@ def _swift_toolchain_impl(ctx): |
| 203 | + target_triple = target_triple, |
| 204 | + )) |
| 205 | +- |
| 206 | ++ |
| 207 | +- if apple_common.dotted_version(ctx.attr.parsed_version) >= apple_common.dotted_version("6.3"): |
| 208 | ++ if not external_static_runtime and apple_common.dotted_version(ctx.attr.parsed_version) >= apple_common.dotted_version("6.3"): |
| 209 | + requested_features.append(SWIFT_FEATURE__SUPPORTS_HERMETIC_SWIFTMODULE) |
| 210 | +- |
| 211 | ++ |
| 212 | + requested_features.extend(ctx.features) |
| 213 | +@@ -787,6 +865,8 @@ def _swift_toolchain_impl(ctx): |
| 214 | + additional_tools = [ctx.file.version_file] |
| 215 | + if ctx.attr.swift_tools: |
| 216 | + additional_tools += ctx.attr.swift_tools[SwiftToolsInfo].additional_inputs |
| 217 | ++ if external_static_runtime: |
| 218 | ++ additional_tools += external_static_runtime.files.to_list() |
| 219 | +- |
| 220 | ++ |
| 221 | + all_tool_configs = _all_tool_configs( |
| 222 | + env = ctx.attr.env, |
| 223 | +@@ -873,7 +953,7 @@ def _swift_toolchain_impl(ctx): |
| 224 | + system_modules = collect_implicit_deps_providers([]), |
| 225 | + implicit_system_modules = collect_implicit_deps_providers([]), |
| 226 | + swift_worker = ctx.attr._worker[DefaultInfo].files_to_run, |
| 227 | +- const_protocols_to_gather = ctx.file.const_protocols_to_gather, |
| 228 | ++ const_protocols_to_gather = None if external_static_runtime else ctx.file.const_protocols_to_gather, |
| 229 | + test_configuration = struct( |
| 230 | + binary_name = "{name}", |
| 231 | + env = env, |
| 232 | +@@ -952,6 +1032,10 @@ context. |
| 233 | + """, |
| 234 | + allow_files = True, |
| 235 | + ), |
| 236 | ++ "external_static_runtime": attr.label( |
| 237 | ++ doc = "External static Swift runtime and SDK to use instead of the compiler's runtimes.", |
| 238 | ++ providers = [[SwiftStaticRuntimeInfo]], |
| 239 | ++ ), |
| 240 | + "static_runtime": attr.label_list( |
| 241 | + doc = """\ |
| 242 | + Static Swift runtime archives and supporting linker files that are passed to |
0 commit comments