Skip to content

Commit

Permalink
Heuristically path map copts and defines
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmeum authored and bazel-io committed Oct 2, 2024
1 parent 00ed3e2 commit 68ca4bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ default ExceptionlessMapFn<Object> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public ExceptionlessMapFn<Object> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ public ImmutableList<VariableValue> getSequenceValue(
ImmutableList.Builder<VariableValue> sequences =
ImmutableList.builderWithExpectedSize(values.size());
for (String value : values) {
sequences.add(new StringValue(value));
sequences.add(new StringValue(pathMapper.mapHeuristically(value)));
}
return sequences.build();
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/shell/bazel/path_mapping_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -659,7 +660,13 @@ EOF
cat > "$pkg/common/utils/utils.cc.tpl" <<'EOF'
#include "utils.h"
#include <cstdlib>
#include <iostream>
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
Expand Down

0 comments on commit 68ca4bd

Please sign in to comment.