Skip to content

Commit

Permalink
Add support for -file-prefix-map $PWD=. (#904)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
keith authored Sep 15, 2022
1 parent 00b6224 commit 20d7d9d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 4 deletions.
15 changes: 15 additions & 0 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ load(
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_ENABLE_TESTING",
"SWIFT_FEATURE_FASTBUILD",
"SWIFT_FEATURE_FILE_PREFIX_MAP",
"SWIFT_FEATURE_FULL_DEBUG_INFO",
"SWIFT_FEATURE_GLOBAL_MODULE_CACHE_USES_TMPDIR",
"SWIFT_FEATURE_INDEX_WHILE_BUILDING",
Expand Down Expand Up @@ -609,6 +610,20 @@ def compile_action_configs(
[SWIFT_FEATURE_COVERAGE_PREFIX_MAP, SWIFT_FEATURE_COVERAGE],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
),
],
features = [
[SWIFT_FEATURE_FILE_PREFIX_MAP],
],
),
]

#### Coverage and sanitizer instrumentation flags
Expand Down
8 changes: 8 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ SWIFT_FEATURE_DEBUG_PREFIX_MAP = "swift.debug_prefix_map"
# of remote builds.
SWIFT_FEATURE_COVERAGE_PREFIX_MAP = "swift.coverage_prefix_map"

# If enabled, builds will use the `-file-prefix-map` feature to remap the
# current working directory to `.`, which avoids embedding non-hermetic
# absolute path information in build artifacts. Specifically what this flag
# does is subject to change in Swift, but it should imply all other
# `-*-prefix-map` flags. How those flags compose is potentially complicated, so
# using only this flag, or the same values for each flag, is recommended.
SWIFT_FEATURE_FILE_PREFIX_MAP = "swift.file_prefix_map"

# If enabled, C and Objective-C libraries that are direct or transitive
# dependencies of a Swift library will emit explicit precompiled modules that
# are compatible with Swift's ClangImporter and propagate them up the build
Expand Down
19 changes: 19 additions & 0 deletions swift/internal/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ load(
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_FILE_PREFIX_MAP",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_REMAP_XCODE_PATH",
"SWIFT_FEATURE_SUPPORTS_BARE_SLASH_REGEX",
Expand Down Expand Up @@ -397,6 +398,24 @@ def _all_action_configs(
],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [
swift_toolchain_config.add_arg(
"-file-prefix-map",
"__BAZEL_XCODE_DEVELOPER_DIR__=DEVELOPER_DIR",
),
],
features = [
[
SWIFT_FEATURE_REMAP_XCODE_PATH,
SWIFT_FEATURE_FILE_PREFIX_MAP,
],
],
),
])

if needs_resource_directory:
Expand Down
3 changes: 3 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":ast_file_tests.bzl", "ast_file_test_suite")
load(":coverage_settings_tests.bzl", "coverage_settings_test_suite")
load(":debug_settings_tests.bzl", "debug_settings_test_suite")
load(":features_tests.bzl", "features_test_suite")
load(":generated_header_tests.bzl", "generated_header_test_suite")
load(":linking_tests.bzl", "linking_test_suite")
load(":module_cache_settings_tests.bzl", "module_cache_settings_test_suite")
Expand All @@ -21,6 +22,8 @@ coverage_settings_test_suite(name = "coverage_settings")

debug_settings_test_suite(name = "debug_settings")

features_test_suite(name = "features")

generated_header_test_suite(name = "generated_header")

linking_test_suite(name = "linking")
Expand Down
66 changes: 66 additions & 0 deletions test/features_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Tests for various features that aren't large enough to need their own tests file."""

load(
"@build_bazel_rules_swift//test/rules:action_command_line_test.bzl",
"make_action_command_line_test_rule",
)

default_test = make_action_command_line_test_rule()

file_prefix_map_test = make_action_command_line_test_rule(
config_settings = {
"//command_line_option:features": [
"swift.file_prefix_map",
],
},
)

file_prefix_xcode_remap_test = make_action_command_line_test_rule(
config_settings = {
"//command_line_option:features": [
"swift.file_prefix_map",
"swift.remap_xcode_path",
],
},
)

def features_test_suite(name):
"""Test suite for various features.
Args:
name: the base name to be used in things created by this macro
"""
default_test(
name = "{}_default_test".format(name),
tags = [name],
expected_argv = ["-emit-object"],
not_expected_argv = [
"-file-prefix-map",
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
],
mnemonic = "SwiftCompile",
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
)

file_prefix_map_test(
name = "{}_file_prefix_map_test".format(name),
tags = [name],
expected_argv = [
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
],
mnemonic = "SwiftCompile",
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
)

file_prefix_xcode_remap_test(
name = "{}_file_prefix_xcode_remap_test".format(name),
tags = [name],
expected_argv = [
"-Xwrapped-swift=-file-prefix-pwd-is-dot",
"-file-prefix-map",
"__BAZEL_XCODE_DEVELOPER_DIR__=DEVELOPER_DIR",
],
target_compatible_with = ["@platforms//os:macos"],
mnemonic = "SwiftCompile",
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
)
14 changes: 10 additions & 4 deletions tools/worker/swift_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,23 @@ bool SwiftRunner::ProcessArgument(
std::string new_arg = arg;
if (StripPrefix("-Xwrapped-swift=", new_arg)) {
if (new_arg == "-debug-prefix-pwd-is-dot") {
// Get the actual current working directory (the workspace root), which
// we didn't know at analysis time.
// Replace the $PWD with . to make the paths relative to the workspace
// without breaking hermiticity.
consumer("-debug-prefix-map");
consumer(std::filesystem::current_path().string() + "=.");
changed = true;
} else if (new_arg == "-coverage-prefix-pwd-is-dot") {
// Get the actual current working directory (the workspace root), which
// we didn't know at analysis time.
// Replace the $PWD with . to make the paths relative to the workspace
// without breaking hermiticity.
consumer("-coverage-prefix-map");
consumer(std::filesystem::current_path().string() + "=.");
changed = true;
} else if (new_arg == "-file-prefix-pwd-is-dot") {
// Replace the $PWD with . to make the paths relative to the workspace
// without breaking hermiticity.
consumer("-file-prefix-map");
consumer(std::filesystem::current_path().string() + "=.");
changed = true;
} else if (new_arg == "-ephemeral-module-cache") {
// Create a temporary directory to hold the module cache, which will be
// deleted after compilation is finished.
Expand Down

0 comments on commit 20d7d9d

Please sign in to comment.