Skip to content

Commit f315011

Browse files
committed
Add support for -file-prefix-map $PWD=.
This is a new flag in Swift 5.7, that mirrors the flag with the same name in clang, meaning that it implies both -debug-prefix-map, and -coverage-prefix map. Currently it is also the only flag that applies to indexing info if you want to produce hermetic indexes. Ideally going forward users should prefer this flag if they want their builds to be hermetic instead of picking the other flags as needed, since this will potentially also apply to new absolute paths in the future. swiftlang/swift#58946
1 parent a9c2190 commit f315011

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

swift/internal/compiling.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ load(
4545
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
4646
"SWIFT_FEATURE_ENABLE_TESTING",
4747
"SWIFT_FEATURE_FASTBUILD",
48+
"SWIFT_FEATURE_FILE_PREFIX_MAP",
4849
"SWIFT_FEATURE_FULL_DEBUG_INFO",
4950
"SWIFT_FEATURE_GLOBAL_MODULE_CACHE_USES_TMPDIR",
5051
"SWIFT_FEATURE_INDEX_WHILE_BUILDING",
@@ -609,6 +610,20 @@ def compile_action_configs(
609610
[SWIFT_FEATURE_COVERAGE_PREFIX_MAP, SWIFT_FEATURE_COVERAGE],
610611
],
611612
),
613+
swift_toolchain_config.action_config(
614+
actions = [
615+
swift_action_names.COMPILE,
616+
swift_action_names.DERIVE_FILES,
617+
],
618+
configurators = [
619+
swift_toolchain_config.add_arg(
620+
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
621+
),
622+
],
623+
features = [
624+
[SWIFT_FEATURE_FILE_PREFIX_MAP],
625+
],
626+
),
612627
]
613628

614629
#### Coverage and sanitizer instrumentation flags

swift/internal/feature_names.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ SWIFT_FEATURE_DEBUG_PREFIX_MAP = "swift.debug_prefix_map"
5757
# of remote builds.
5858
SWIFT_FEATURE_COVERAGE_PREFIX_MAP = "swift.coverage_prefix_map"
5959

60+
# If enabled, builds will use the `-file-prefix-map` feature to remap the
61+
# current working directory to `.`, which avoids embedding non-hermetic
62+
# absolute path information in build artifacts. Specifically what this flag
63+
# does is subject to change in Swift, but it should imply all other
64+
# `-*-prefix-map` flags. How those flags compose is potentially complicated, so
65+
# using only this flag, or the same values for each flag, is recommended.
66+
SWIFT_FEATURE_FILE_PREFIX_MAP = "swift.file_prefix_map"
67+
6068
# If enabled, C and Objective-C libraries that are direct or transitive
6169
# dependencies of a Swift library will emit explicit precompiled modules that
6270
# are compatible with Swift's ClangImporter and propagate them up the build

swift/internal/xcode_swift_toolchain.bzl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ load(
3636
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
3737
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
3838
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
39+
"SWIFT_FEATURE_FILE_PREFIX_MAP",
3940
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
4041
"SWIFT_FEATURE_REMAP_XCODE_PATH",
4142
"SWIFT_FEATURE_SUPPORTS_BARE_SLASH_REGEX",
@@ -397,6 +398,24 @@ def _all_action_configs(
397398
],
398399
],
399400
),
401+
swift_toolchain_config.action_config(
402+
actions = [
403+
swift_action_names.COMPILE,
404+
swift_action_names.DERIVE_FILES,
405+
],
406+
configurators = [
407+
swift_toolchain_config.add_arg(
408+
"-file-prefix-map",
409+
"__BAZEL_XCODE_DEVELOPER_DIR__=DEVELOPER_DIR",
410+
),
411+
],
412+
features = [
413+
[
414+
SWIFT_FEATURE_REMAP_XCODE_PATH,
415+
SWIFT_FEATURE_FILE_PREFIX_MAP,
416+
],
417+
],
418+
),
400419
])
401420

402421
if needs_resource_directory:

test/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load(":ast_file_tests.bzl", "ast_file_test_suite")
33
load(":coverage_settings_tests.bzl", "coverage_settings_test_suite")
44
load(":debug_settings_tests.bzl", "debug_settings_test_suite")
5+
load(":features_tests.bzl", "features_test_suite")
56
load(":generated_header_tests.bzl", "generated_header_test_suite")
67
load(":linking_tests.bzl", "linking_test_suite")
78
load(":module_cache_settings_tests.bzl", "module_cache_settings_test_suite")
@@ -21,6 +22,8 @@ coverage_settings_test_suite(name = "coverage_settings")
2122

2223
debug_settings_test_suite(name = "debug_settings")
2324

25+
features_test_suite(name = "features")
26+
2427
generated_header_test_suite(name = "generated_header")
2528

2629
linking_test_suite(name = "linking")

test/features_tests.bzl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Tests for various features that aren't large enough to need their own tests file."""
2+
3+
load(
4+
"@build_bazel_rules_swift//test/rules:action_command_line_test.bzl",
5+
"make_action_command_line_test_rule",
6+
)
7+
8+
default_test = make_action_command_line_test_rule()
9+
10+
file_prefix_map_test = make_action_command_line_test_rule(
11+
config_settings = {
12+
"//command_line_option:features": [
13+
"swift.file_prefix_map",
14+
],
15+
},
16+
)
17+
18+
file_prefix_xcode_remap_test = make_action_command_line_test_rule(
19+
config_settings = {
20+
"//command_line_option:features": [
21+
"swift.file_prefix_map",
22+
"swift.remap_xcode_path",
23+
],
24+
},
25+
)
26+
27+
def features_test_suite(name):
28+
"""Test suite for various features.
29+
30+
Args:
31+
name: the base name to be used in things created by this macro
32+
"""
33+
default_test(
34+
name = "{}_default_test".format(name),
35+
tags = [name],
36+
expected_argv = ["-sdk"],
37+
not_expected_argv = [
38+
"-file-prefix-map",
39+
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
40+
],
41+
mnemonic = "SwiftCompile",
42+
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
43+
)
44+
45+
file_prefix_map_test(
46+
name = "{}_file_prefix_map_test".format(name),
47+
tags = [name],
48+
expected_argv = [
49+
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
50+
],
51+
mnemonic = "SwiftCompile",
52+
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
53+
)
54+
55+
file_prefix_xcode_remap_test(
56+
name = "{}_file_prefix_xcode_remap_test".format(name),
57+
tags = [name],
58+
expected_argv = [
59+
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
60+
"-file-prefix-map",
61+
"__BAZEL_XCODE_DEVELOPER_DIR__=DEVELOPER_DIR",
62+
],
63+
target_compatible_with = ["@platforms//os:macos"],
64+
mnemonic = "SwiftCompile",
65+
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
66+
)

tools/worker/swift_runner.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ bool SwiftRunner::ProcessArgument(
284284
consumer("-coverage-prefix-map");
285285
consumer(std::filesystem::current_path().string() + "=.");
286286
changed = true;
287+
} else if (new_arg == "-file-prefix-pwd-is-dot") {
288+
// Get the actual current working directory (the workspace root), which
289+
// we didn't know at analysis time.
290+
consumer("-file-prefix-map");
291+
consumer(std::filesystem::current_path().string() + "=.");
292+
changed = true;
287293
} else if (new_arg == "-ephemeral-module-cache") {
288294
// Create a temporary directory to hold the module cache, which will be
289295
// deleted after compilation is finished.

0 commit comments

Comments
 (0)