Skip to content

Commit

Permalink
RFC: Add --features=swift.static_stdlib
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
liuliu committed Jun 3, 2022
1 parent 3bc7bc1 commit 15698bf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
11 changes: 11 additions & 0 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ load(
"SWIFT_FEATURE_USE_OLD_DRIVER",
"SWIFT_FEATURE_USE_PCH_OUTPUT_DIR",
"SWIFT_FEATURE_VFSOVERLAY",
"SWIFT_FEATURE_STATIC_STDLIB",
"SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS",
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS",
)
Expand Down Expand Up @@ -1037,6 +1038,16 @@ def compile_action_configs(
],
configurators = [_conditional_compilation_flag_configurator],
),

# Enable the built modules to reference static Swift standard libraries.
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [swift_toolchain_config.add_arg("-static-stdlib")],
features = [SWIFT_FEATURE_STATIC_STDLIB],
),
]

# NOTE: The positions of these action configs in the list are important,
Expand Down
4 changes: 4 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES = "swift.skip_function_bodies_for_deri
# swift.coverage_prefix_map also remap the path in coverage data.
SWIFT_FEATURE_REMAP_XCODE_PATH = "swift.remap_xcode_path"

# If enabled the built binary will statically link Swift standard libraries.
# This requires Swift 5.3.1
SWIFT_FEATURE_STATIC_STDLIB = "swift.static_stdlib"

# A private feature that is set by the toolchain if a flag enabling WMO was
# passed on the command line using `--swiftcopt`. Users should never manually
# enable, disable, or query this feature.
Expand Down
39 changes: 29 additions & 10 deletions swift/internal/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ load(
"SWIFT_FEATURE_USE_AUTOLINK_EXTRACT",
"SWIFT_FEATURE_USE_MODULE_WRAP",
"SWIFT_FEATURE_USE_RESPONSE_FILES",
"SWIFT_FEATURE_STATIC_STDLIB",
)
load(":features.bzl", "features_for_build_modes")
load(
Expand Down Expand Up @@ -202,7 +203,8 @@ def _swift_unix_linkopts_cc_info(
cpu,
os,
toolchain_label,
toolchain_root):
toolchain_root,
static_stdlib):
"""Returns a `CcInfo` containing flags that should be passed to the linker.
The provider returned by this function will be used as an implicit
Expand All @@ -216,27 +218,43 @@ def _swift_unix_linkopts_cc_info(
toolchain_label: The label of the Swift toolchain that will act as the
owner of the linker input propagating the flags.
toolchain_root: The toolchain's root directory.
static_stdlib: Whether to statically link Swift standard libraries.
Returns:
A `CcInfo` provider that will provide linker flags to binaries that
depend on Swift targets.
"""

# TODO(#8): Support statically linking the Swift runtime.
platform_lib_dir = "{toolchain_root}/lib/swift/{os}".format(
os = os,
toolchain_root = toolchain_root,
)
if static_stdlib:
platform_lib_dir = "{toolchain_root}/lib/swift_static/{os}".format(
os = os,
toolchain_root = toolchain_root,
)
else:
platform_lib_dir = "{toolchain_root}/lib/swift/{os}".format(
os = os,
toolchain_root = toolchain_root,
)

linkopts = [
"-pie",
"-L{}".format(platform_lib_dir),
"-Wl,-rpath,{}".format(platform_lib_dir),
]

# Appending generic linker args from Swift runtime.
if static_stdlib:
static_stdlib_args = "{platform_lib_dir}/static-stdlib-args.lnk".format(
platform_lib_dir = platform_lib_dir,
)
linkopts.append("@{}".format(static_stdlib_args))

runtime_object_path = "{platform_lib_dir}/{cpu}/swiftrt.o".format(
cpu = cpu,
platform_lib_dir = platform_lib_dir,
)

linkopts = [
"-pie",
"-L{}".format(platform_lib_dir),
"-Wl,-rpath,{}".format(platform_lib_dir),
linkopts += [
"-lm",
"-lstdc++",
"-lrt",
Expand Down Expand Up @@ -273,6 +291,7 @@ def _swift_toolchain_impl(ctx):
ctx.attr.os,
ctx.label,
toolchain_root,
SWIFT_FEATURE_STATIC_STDLIB in ctx.features,
)

# Combine build mode features, autoconfigured features, and required
Expand Down
5 changes: 4 additions & 1 deletion tools/worker/work_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void WorkProcessor::ProcessWorkRequest(
std::string output_file_map_path;
std::string emit_module_path;
bool is_wmo = false;
bool is_static_stdlib = false;
bool is_dump_ast = false;

std::string prev_arg;
Expand All @@ -97,6 +98,8 @@ void WorkProcessor::ProcessWorkRequest(
arg.clear();
} else if (prev_arg == "-emit-module-path") {
emit_module_path = arg;
} else if (arg == "-static-stdlib") {
is_static_stdlib = true;
} else if (ArgumentEnablesWMO(arg)) {
is_wmo = true;
}
Expand All @@ -108,7 +111,7 @@ void WorkProcessor::ProcessWorkRequest(
prev_arg = original_arg;
}

bool is_incremental = !is_wmo && !is_dump_ast;
bool is_incremental = !is_wmo && !is_dump_ast && !is_static_stdlib;

if (!output_file_map_path.empty()) {
if (is_incremental) {
Expand Down

0 comments on commit 15698bf

Please sign in to comment.