Skip to content

Commit 15698bf

Browse files
committed
RFC: Add --features=swift.static_stdlib
This PR added the ability to compile Swift binary with stdlib in Linux. It added `-static-stdlib` to `swiftc` invocation and added `static-stdlib-args.lnk` to linker flag. However, there is an issue with Bazel cache: ``` bazel build examples:ddpg --features=swift.static_stdlib ``` It compiles. Then: ``` bazel build examples:ddpg ``` See error: ``` /usr/bin/ld: cannot find -lDispatchStubs /usr/bin/ld: cannot find -lCoreFoundation ``` It seems reused the previous autolink extract results. Disable incremental build when -static-stdlib is presented.
1 parent 3bc7bc1 commit 15698bf

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

swift/internal/compiling.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ load(
6666
"SWIFT_FEATURE_USE_OLD_DRIVER",
6767
"SWIFT_FEATURE_USE_PCH_OUTPUT_DIR",
6868
"SWIFT_FEATURE_VFSOVERLAY",
69+
"SWIFT_FEATURE_STATIC_STDLIB",
6970
"SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS",
7071
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS",
7172
)
@@ -1037,6 +1038,16 @@ def compile_action_configs(
10371038
],
10381039
configurators = [_conditional_compilation_flag_configurator],
10391040
),
1041+
1042+
# Enable the built modules to reference static Swift standard libraries.
1043+
swift_toolchain_config.action_config(
1044+
actions = [
1045+
swift_action_names.COMPILE,
1046+
swift_action_names.DERIVE_FILES,
1047+
],
1048+
configurators = [swift_toolchain_config.add_arg("-static-stdlib")],
1049+
features = [SWIFT_FEATURE_STATIC_STDLIB],
1050+
),
10401051
]
10411052

10421053
# NOTE: The positions of these action configs in the list are important,

swift/internal/feature_names.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES = "swift.skip_function_bodies_for_deri
291291
# swift.coverage_prefix_map also remap the path in coverage data.
292292
SWIFT_FEATURE_REMAP_XCODE_PATH = "swift.remap_xcode_path"
293293

294+
# If enabled the built binary will statically link Swift standard libraries.
295+
# This requires Swift 5.3.1
296+
SWIFT_FEATURE_STATIC_STDLIB = "swift.static_stdlib"
297+
294298
# A private feature that is set by the toolchain if a flag enabling WMO was
295299
# passed on the command line using `--swiftcopt`. Users should never manually
296300
# enable, disable, or query this feature.

swift/internal/swift_toolchain.bzl

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ load(
3434
"SWIFT_FEATURE_USE_AUTOLINK_EXTRACT",
3535
"SWIFT_FEATURE_USE_MODULE_WRAP",
3636
"SWIFT_FEATURE_USE_RESPONSE_FILES",
37+
"SWIFT_FEATURE_STATIC_STDLIB",
3738
)
3839
load(":features.bzl", "features_for_build_modes")
3940
load(
@@ -202,7 +203,8 @@ def _swift_unix_linkopts_cc_info(
202203
cpu,
203204
os,
204205
toolchain_label,
205-
toolchain_root):
206+
toolchain_root,
207+
static_stdlib):
206208
"""Returns a `CcInfo` containing flags that should be passed to the linker.
207209
208210
The provider returned by this function will be used as an implicit
@@ -216,27 +218,43 @@ def _swift_unix_linkopts_cc_info(
216218
toolchain_label: The label of the Swift toolchain that will act as the
217219
owner of the linker input propagating the flags.
218220
toolchain_root: The toolchain's root directory.
221+
static_stdlib: Whether to statically link Swift standard libraries.
219222
220223
Returns:
221224
A `CcInfo` provider that will provide linker flags to binaries that
222225
depend on Swift targets.
223226
"""
224227

225-
# TODO(#8): Support statically linking the Swift runtime.
226-
platform_lib_dir = "{toolchain_root}/lib/swift/{os}".format(
227-
os = os,
228-
toolchain_root = toolchain_root,
229-
)
228+
if static_stdlib:
229+
platform_lib_dir = "{toolchain_root}/lib/swift_static/{os}".format(
230+
os = os,
231+
toolchain_root = toolchain_root,
232+
)
233+
else:
234+
platform_lib_dir = "{toolchain_root}/lib/swift/{os}".format(
235+
os = os,
236+
toolchain_root = toolchain_root,
237+
)
238+
239+
linkopts = [
240+
"-pie",
241+
"-L{}".format(platform_lib_dir),
242+
"-Wl,-rpath,{}".format(platform_lib_dir),
243+
]
244+
245+
# Appending generic linker args from Swift runtime.
246+
if static_stdlib:
247+
static_stdlib_args = "{platform_lib_dir}/static-stdlib-args.lnk".format(
248+
platform_lib_dir = platform_lib_dir,
249+
)
250+
linkopts.append("@{}".format(static_stdlib_args))
230251

231252
runtime_object_path = "{platform_lib_dir}/{cpu}/swiftrt.o".format(
232253
cpu = cpu,
233254
platform_lib_dir = platform_lib_dir,
234255
)
235256

236-
linkopts = [
237-
"-pie",
238-
"-L{}".format(platform_lib_dir),
239-
"-Wl,-rpath,{}".format(platform_lib_dir),
257+
linkopts += [
240258
"-lm",
241259
"-lstdc++",
242260
"-lrt",
@@ -273,6 +291,7 @@ def _swift_toolchain_impl(ctx):
273291
ctx.attr.os,
274292
ctx.label,
275293
toolchain_root,
294+
SWIFT_FEATURE_STATIC_STDLIB in ctx.features,
276295
)
277296

278297
# Combine build mode features, autoconfigured features, and required

tools/worker/work_processor.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ void WorkProcessor::ProcessWorkRequest(
8181
std::string output_file_map_path;
8282
std::string emit_module_path;
8383
bool is_wmo = false;
84+
bool is_static_stdlib = false;
8485
bool is_dump_ast = false;
8586

8687
std::string prev_arg;
@@ -97,6 +98,8 @@ void WorkProcessor::ProcessWorkRequest(
9798
arg.clear();
9899
} else if (prev_arg == "-emit-module-path") {
99100
emit_module_path = arg;
101+
} else if (arg == "-static-stdlib") {
102+
is_static_stdlib = true;
100103
} else if (ArgumentEnablesWMO(arg)) {
101104
is_wmo = true;
102105
}
@@ -108,7 +111,7 @@ void WorkProcessor::ProcessWorkRequest(
108111
prev_arg = original_arg;
109112
}
110113

111-
bool is_incremental = !is_wmo && !is_dump_ast;
114+
bool is_incremental = !is_wmo && !is_dump_ast && !is_static_stdlib;
112115

113116
if (!output_file_map_path.empty()) {
114117
if (is_incremental) {

0 commit comments

Comments
 (0)