diff --git a/swift/internal/compiling.bzl b/swift/internal/compiling.bzl index 13fb43cb3..a752d4147 100644 --- a/swift/internal/compiling.bzl +++ b/swift/internal/compiling.bzl @@ -32,6 +32,7 @@ load( "SWIFT_FEATURE_CACHEABLE_SWIFTMODULES", "SWIFT_FEATURE_COMPILE_STATS", "SWIFT_FEATURE_COVERAGE", + "SWIFT_FEATURE_COVERAGE_PREFIX_MAP", "SWIFT_FEATURE_DBG", "SWIFT_FEATURE_DEBUG_PREFIX_MAP", "SWIFT_FEATURE_EMIT_C_MODULE", @@ -293,6 +294,19 @@ def compile_action_configs(): [SWIFT_FEATURE_DEBUG_PREFIX_MAP, SWIFT_FEATURE_FULL_DEBUG_INFO], ], ), + + # Make paths written into coverage info workspace-relative. + swift_toolchain_config.action_config( + actions = [swift_action_names.COMPILE], + configurators = [ + swift_toolchain_config.add_arg( + "-Xwrapped-swift=-coverage-prefix-pwd-is-dot", + ), + ], + features = [ + [SWIFT_FEATURE_COVERAGE_PREFIX_MAP, SWIFT_FEATURE_COVERAGE], + ], + ), ] #### Coverage and sanitizer instrumentation flags diff --git a/swift/internal/feature_names.bzl b/swift/internal/feature_names.bzl index da282aeef..ad3ed8969 100644 --- a/swift/internal/feature_names.bzl +++ b/swift/internal/feature_names.bzl @@ -61,6 +61,11 @@ SWIFT_FEATURE_COMPILE_STATS = "swift.compile_stats" # builds. SWIFT_FEATURE_DEBUG_PREFIX_MAP = "swift.debug_prefix_map" +# If enabled, coverage builds will use the `-coverage-prefix-map` feature to +# remap the current working directory to `.`, which increases reproducibility +# of remote builds. +SWIFT_FEATURE_COVERAGE_PREFIX_MAP = "swift.coverage_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 diff --git a/test/BUILD b/test/BUILD index 38d7e1de4..ea685d048 100644 --- a/test/BUILD +++ b/test/BUILD @@ -1,5 +1,6 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load(":debug_settings_tests.bzl", "debug_settings_test_suite") +load(":coverage_settings_tests.bzl", "coverage_settings_test_suite") load(":generated_header_tests.bzl", "generated_header_test_suite") load(":private_deps_tests.bzl", "private_deps_test_suite") load(":swift_through_non_swift_tests.bzl", "swift_through_non_swift_test_suite") @@ -8,6 +9,8 @@ licenses(["notice"]) debug_settings_test_suite() +coverage_settings_test_suite() + generated_header_test_suite() private_deps_test_suite() diff --git a/test/coverage_settings_tests.bzl b/test/coverage_settings_tests.bzl new file mode 100644 index 000000000..e16d7da10 --- /dev/null +++ b/test/coverage_settings_tests.bzl @@ -0,0 +1,53 @@ +"""Tests for coverage-related command line flags under various configs.""" + +load( + "@build_bazel_rules_swift//test/rules:action_command_line_test.bzl", + "make_action_command_line_test_rule", +) + +default_coverage_test = make_action_command_line_test_rule( + config_settings = { + "//command_line_option:collect_code_coverage": "true", + }, +) + +coverage_prefix_map_test = make_action_command_line_test_rule( + config_settings = { + "//command_line_option:collect_code_coverage": "true", + "//command_line_option:features": [ + "swift.coverage_prefix_map", + ], + }, +) + +def coverage_settings_test_suite(name = "coverage_settings"): + """Test suite for coverage options. + + Args: + name: The name prefix for all the nested tests + """ + default_coverage_test( + name = "{}_default_coverage".format(name), + tags = [name], + expected_argv = [ + "-profile-generate", + "-profile-coverage-mapping", + ], + not_expected_argv = [ + "-Xwrapped-swift=-coverage-prefix-pwd-is-dot", + ], + mnemonic = "SwiftCompile", + target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple", + ) + + coverage_prefix_map_test( + name = "{}_prefix_map".format(name), + tags = [name], + expected_argv = [ + "-profile-generate", + "-profile-coverage-mapping", + "-Xwrapped-swift=-coverage-prefix-pwd-is-dot", + ], + mnemonic = "SwiftCompile", + target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple", + ) diff --git a/tools/worker/swift_runner.cc b/tools/worker/swift_runner.cc index eca0f2993..c1a387f66 100644 --- a/tools/worker/swift_runner.cc +++ b/tools/worker/swift_runner.cc @@ -187,6 +187,12 @@ bool SwiftRunner::ProcessArgument( consumer("-debug-prefix-map"); consumer(GetCurrentDirectory() + "=."); changed = true; + } else if (arg == "-Xwrapped-swift=-coverage-prefix-pwd-is-dot") { + // Get the actual current working directory (the workspace root), which we + // didn't know at analysis time. + consumer("-coverage-prefix-map"); + consumer(GetCurrentDirectory() + "=."); + changed = true; } else if (arg == "-Xwrapped-swift=-ephemeral-module-cache") { // Create a temporary directory to hold the module cache, which will be // deleted after compilation is finished.