From c44d6a6c6a0d1edf8b7e177be1b756744bae9819 Mon Sep 17 00:00:00 2001 From: "bazel.build machine account" Date: Wed, 9 Oct 2024 20:33:43 +0200 Subject: [PATCH] [7.4.0] Heuristically path map copts and defines (#23853) This allows path mapping to apply to actions that reference execpaths in custom compiler options via location expansion. Work towards #6526 Closes #23630. PiperOrigin-RevId: 680941133 Change-Id: Ia10e2df481dcfe4480cbf9dfb1e12ec3b07d8ab2 Commit https://github.com/bazelbuild/bazel/commit/b4b35af91283709689ffca37c4d1eb4f5eee0c30 Co-authored-by: Fabian Meumertzheim --- .../com/google/devtools/build/lib/actions/PathMapper.java | 5 +++++ .../build/lib/analysis/actions/StrippingPathMapper.java | 5 +++++ .../devtools/build/lib/rules/cpp/CcToolchainVariables.java | 2 +- src/test/shell/bazel/path_mapping_test.sh | 7 +++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/devtools/build/lib/actions/PathMapper.java b/src/main/java/com/google/devtools/build/lib/actions/PathMapper.java index 23ee0fbbfa992c..c3dcae4f68b854 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/PathMapper.java +++ b/src/main/java/com/google/devtools/build/lib/actions/PathMapper.java @@ -128,6 +128,11 @@ default ExceptionlessMapFn getMapFn(@Nullable String previousFlag) { return MapFn.DEFAULT; } + /** Heuristically maps all path-like strings in the given argument. */ + default String mapHeuristically(String arg) { + return arg; + } + /** * Returns a {@link FileRootApi} representing the new root of the given artifact after mapping. * diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/StrippingPathMapper.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/StrippingPathMapper.java index 558a401634f440..74522e4aed4f51 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/actions/StrippingPathMapper.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/StrippingPathMapper.java @@ -201,6 +201,11 @@ public ExceptionlessMapFn getMapFn(@Nullable String previousFlag) { return MapFn.DEFAULT; } + @Override + public String mapHeuristically(String arg) { + return argStripper.strip(arg); + } + @Override public FileRootApi mapRoot(Artifact artifact) { if (Objects.equals(artifact.getRoot(), outputArtifactRoot)) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainVariables.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainVariables.java index 9987a1d4343a12..2df796874cf9c8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainVariables.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainVariables.java @@ -1050,7 +1050,7 @@ public ImmutableList getSequenceValue( ImmutableList.Builder sequences = ImmutableList.builderWithExpectedSize(values.size()); for (String value : values) { - sequences.add(new StringValue(value)); + sequences.add(new StringValue(pathMapper.mapHeuristically(value))); } return sequences.build(); } diff --git a/src/test/shell/bazel/path_mapping_test.sh b/src/test/shell/bazel/path_mapping_test.sh index 60892c69fb63a0..b5cf054a62f777 100755 --- a/src/test/shell/bazel/path_mapping_test.sh +++ b/src/test/shell/bazel/path_mapping_test.sh @@ -542,6 +542,7 @@ cc_library( name = "utils", srcs = ["dir/utils.cc"], hdrs = ["dir/utils.h"], + defines = ["MY_FILE=\\\"+$(execpath dir/utils.cc)+\\\""], include_prefix = "other_dir", strip_include_prefix = "dir", visibility = ["//visibility:public"], @@ -659,7 +660,13 @@ EOF cat > "$pkg/common/utils/utils.cc.tpl" <<'EOF' #include "utils.h" +#include +#include + std::string AsGreeting(const std::string& name) { + if (std::string(MY_FILE).find("-out/cfg/") == std::string::npos) { + std::cerr << "Expected path to contain '-out/cfg/'" << std::endl; + } return "{GREETING}, " + name + "!"; } EOF