Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ SWIFT_FEATURE_DEBUG_PREFIX_MAP = "swift.debug_prefix_map"
# of remote builds.
SWIFT_FEATURE_COVERAGE_PREFIX_MAP = "swift.coverage_prefix_map"

# A private feature that is used to embed absolute source paths in coverage builds.
#
# If enabled, coverage builds will use a `-coverage-prefix-map` that remaps
# the current working directory to a canonical location, which permits
# coverage representation in non-sandboxed builds with tools that expect
# absolute paths such as Xcode.
#
# This feature should only be used with non-sandboxed builds inside tools such as
# Xcode, and enabling it effectively breaks Bazel's ability to rely on the
# remote cache those builds. It should not be enabled by users of the toolchain.
SWIFT_FEATURE__COVERAGE_PREFIX_MAP_ABSOLUTE_SOURCES_NON_HERMETIC = "swift._coverage_prefix_map_absolute_sources_non_hermetic"

# 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
12 changes: 12 additions & 0 deletions swift/toolchains/config/compile_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ load(
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE_USE_PCH_OUTPUT_DIR",
"SWIFT_FEATURE_VFSOVERLAY",
"SWIFT_FEATURE__COVERAGE_PREFIX_MAP_ABSOLUTE_SOURCES_NON_HERMETIC",
"SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS",
"SWIFT_FEATURE__SUPPORTS_UPCOMING_FEATURES",
"SWIFT_FEATURE__SUPPORTS_V6",
Expand Down Expand Up @@ -558,6 +559,17 @@ def compile_action_configs(
features = [
[SWIFT_FEATURE_COVERAGE_PREFIX_MAP, SWIFT_FEATURE_COVERAGE],
],
not_features = [SWIFT_FEATURE__COVERAGE_PREFIX_MAP_ABSOLUTE_SOURCES_NON_HERMETIC],
),
ActionConfigInfo(
actions = all_compile_action_names(),
configurators = [
add_arg("-Xwrapped-swift=-coverage-prefix-pwd-is-canonical"),
],
features = [
[SWIFT_FEATURE__COVERAGE_PREFIX_MAP_ABSOLUTE_SOURCES_NON_HERMETIC, SWIFT_FEATURE_COVERAGE],
],
not_features = [SWIFT_FEATURE_COVERAGE_PREFIX_MAP, SWIFT_FEATURE_FILE_PREFIX_MAP],
),

# Ensure that .swiftsourceinfo files are tracked and not deleted by the worker
Expand Down
18 changes: 15 additions & 3 deletions tools/worker/swift_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,12 @@ bool SwiftRunner::ProcessArgument(

// Helper function for adding path remapping flags that depend on information
// only known at execution time.
auto add_prefix_map_flags = [&](const std::string &flag) {
auto add_prefix_map_flags = [&](const std::string &flag,
const std::string &new_path = ".") {
// Get the actual current working directory (the execution root), which
// we didn't know at analysis time.
consumer(flag);
consumer(std::filesystem::current_path().string() + "=.");
consumer(std::filesystem::current_path().string() + "=" + new_path);

#if __APPLE__
std::string developer_dir = "__BAZEL_XCODE_DEVELOPER_DIR__";
Expand All @@ -316,7 +317,18 @@ bool SwiftRunner::ProcessArgument(
} else if (new_arg == "-coverage-prefix-pwd-is-dot") {
// Replace the $PWD with . to make the paths relative to the workspace
// without breaking hermiticity.
add_prefix_map_flags("-file-prefix-map");
add_prefix_map_flags("-coverage-prefix-map");
changed = true;
} else if (new_arg == "-coverage-prefix-pwd-is-canonical") {
// Replace the $PWD with . to make the paths relative to the workspace
// without breaking hermiticity.
auto cwd = std::filesystem::current_path();
// The bazel execroot is a normal directory, but inside of it there are
// symlinks to our source tree. This fetches the true path of a known
// directory in order to get the actual source root of the project. This
// should only work with sandboxing disabled.
auto target_path = std::filesystem::canonical(cwd / "BUILD.bazel").parent_path();
add_prefix_map_flags("-coverage-prefix-map", target_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
Expand Down