Skip to content

Commit b3f6f2e

Browse files
authored
Merge pull request #22413 from github/jketema/swift-static
Unified: Make Linux build hermetic
2 parents ec63c66 + 0f1fd7f commit b3f6f2e

9 files changed

Lines changed: 308 additions & 76 deletions

File tree

BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ exports_files([
33
"Cargo.lock",
44
"Cargo.toml",
55
])
6+
7+
constraint_setting(name = "swift_runtime_linkage")
8+
9+
constraint_value(
10+
name = "static_swift_runtime",
11+
constraint_setting = ":swift_runtime_linkage",
12+
visibility = ["//visibility:public"],
13+
)

MODULE.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ use_repo(
228228
# `unified/swift-syntax-rs` package is not loadable in that context. Keep this
229229
# in sync with `unified/swift-syntax-rs/.swift-version` (used by the `cargo`
230230
# build) and the `swift-syntax` release in `swift/Package.swift`.
231-
swift = use_extension("@rules_swift//swift:extensions.bzl", "swift")
231+
swift = use_extension("@rules_swift//swift:extensions.bzl", "swift", dev_dependency = True)
232232
swift.toolchain(
233233
name = "swift_toolchain",
234234
swift_version = "6.3.3",
@@ -243,6 +243,7 @@ use_repo(
243243
register_toolchains(
244244
"@swift_toolchain//:swift_toolchain_exec_ubuntu22.04",
245245
"@swift_toolchain//:swift_toolchain_exec_xcode",
246+
dev_dependency = True,
246247
)
247248

248249
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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

misc/bazel/registry/modules/rules_swift/4.0.0-rc5-codeql.1/source.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"integrity": "sha256-Ly2lS4AlZJMiyqAxKHyCB+2pGeP5aYkQ6zv8QheeSTI=",
33
"url": "https://github.com/bazelbuild/rules_swift/releases/download/4.0.0-rc5/rules_swift.4.0.0-rc5.tar.gz",
44
"patches": {
5-
"register_downloaded_macos_toolchain.patch": "sha256-249WJ4YEH2HmxlvGftl4eD53792joKJx9Dh0j5eHAgo="
5+
"register_downloaded_macos_toolchain.patch": "sha256-249WJ4YEH2HmxlvGftl4eD53792joKJx9Dh0j5eHAgo=",
6+
"external_static_runtime.patch": "sha256-VY8UPZjCdlLjDB4taEpRfENRlkw4pGNYD2MAgNFITC4="
67
},
78
"patch_strip": 1
89
}

unified/BUILD.bazel

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ codeql_pkg_files(
4242
codeql_pkg_files(
4343
name = "extractor-arch",
4444
exes = codeql_platform_select(
45-
linux64 = [
46-
"//unified/extractor",
47-
"//unified/swift-syntax-rs:swift_runtime_libs",
48-
],
45+
linux64 = ["//unified/extractor"],
4946
linux_arm64 = ["//unified/extractor-unsupported-os:extractor"],
5047
osx64 = ["//unified/extractor"],
5148
win64 = ["//unified/extractor-unsupported-os:extractor"],

unified/extractor/BUILD.bazel

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ codeql_rust_binary(
1313
"ast_types.yml",
1414
"swift_node_types.yml",
1515
],
16-
data = select({
17-
"@platforms//os:linux": ["//unified/swift-syntax-rs:swift_runtime_libs"],
18-
"//conditions:default": [],
19-
}),
16+
data = ["//unified/swift-syntax-rs:swift_runtime"],
2017
proc_macro_deps = all_crate_deps(
2118
proc_macro = True,
2219
),
@@ -56,10 +53,7 @@ _TESTS = {
5653
"swift_node_types.yml",
5754
] + spec["compile_data"],
5855
crate_root = "tests/%s.rs" % test_name,
59-
data = spec["data"] + select({
60-
"@platforms//os:linux": ["//unified/swift-syntax-rs:swift_runtime_libs"],
61-
"//conditions:default": [],
62-
}),
56+
data = spec["data"] + ["//unified/swift-syntax-rs:swift_runtime"],
6357
edition = "2024",
6458
proc_macro_deps = all_crate_deps(
6559
proc_macro = True,

0 commit comments

Comments
 (0)