Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public ImmutableMap<String, String> getCoverageEnvironment() {
*
* <p>This is useful for virtual include paths in C++, which get reported at the include location
* and not the real source path. For example, the reported include source file can be
* "bazel-out/k8-fastbuild/bin/include/common/_virtual_includes/strategy/strategy.h", but its
* actual source path is "include/common/strategy.h".
* "bazel-out/k8-fastbuild/_virtual_includes/XXXXXXXX/strategy.h", but its actual source path is
* "include/common/strategy.h".
*/
NestedSet<Tuple> getReportedToActualSources() {
return reportedToActualSources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ public final class CcCompilationContext implements CcCompilationContextApi<Artif
// Each pair maps the Bazel generated paths of virtual include headers back to their original path
// relative to the workspace directory.
// For example it can map
// "bazel-out/k8-fastbuild/bin/include/common/_virtual_includes/strategy/strategy.h"
// "bazel-out/k8-fastbuild/_virtual_includes/XXXXXXXX/strategy.h"
// back to the path of the header in the workspace directory "include/common/strategy.h".
// This is needed only when code coverage collection is enabled, to report the actual source file
// name in the coverage output file.
private final NestedSet<Tuple> virtualToOriginalHeaders;

/**
* Caches the actual number of transitive headers reachable through transitiveHeaderInfos. We need
* to create maps to store these and so caching this count can substantially help with memory
* allocations.
*/
private int transitiveHeaderCount;

/** Aftifacts generated by the header validation actions. */
private final NestedSet<Artifact> headerTokens;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _compute_public_headers(

module_map_headers = []
virtual_to_original_headers_list = []
virtual_include_dir = paths.join(paths.join(package_source_root(label.workspace_name, label.package, is_sibling_repository_layout), _VIRTUAL_INCLUDES_DIR), label.name)
virtual_include_dir = paths.join(_VIRTUAL_INCLUDES_DIR, "%x" % hash(paths.join(package_source_root(label.workspace_name, label.package, is_sibling_repository_layout), label.name)))
for original_header in public_headers_artifacts:
repo_relative_path = _repo_relative_path(original_header)
if not repo_relative_path.startswith(strip_prefix):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,6 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
"//src/main/java/com/google/devtools/build/lib/skyframe:bzl_load_value",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/net/starlark/java/eval",
"//src/main/protobuf:crosstool_config_java_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.baseArtifactNames;
import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.baseNamesOf;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.truth.IterableSubject;
import com.google.devtools.build.lib.actions.Action;
Expand Down Expand Up @@ -1071,12 +1072,12 @@ public void testIncludeManglingSmoke() throws Exception {
ConfiguredTarget lib = getConfiguredTarget("//third_party/a");
CcCompilationContext ccCompilationContext = lib.get(CcInfo.PROVIDER).getCcCompilationContext();
assertThat(ActionsTestUtil.prettyArtifactNames(ccCompilationContext.getDeclaredIncludeSrcs()))
.containsExactly("third_party/a/_virtual_includes/a/lib/b/c.h", "third_party/a/v1/b/c.h");
.containsExactly("_virtual_includes/207132b2/lib/b/c.h", "third_party/a/v1/b/c.h");
assertThat(ccCompilationContext.getIncludeDirs())
.containsExactly(
getTargetConfiguration()
.getBinFragment(RepositoryName.MAIN)
.getRelative("third_party/a/_virtual_includes/a"));
.getRelative("_virtual_includes/207132b2"));
}

@Test
Expand Down Expand Up @@ -1148,10 +1149,9 @@ public void testAbsoluteAndRelativeStripPrefix() throws Exception {
.getCcCompilationContext();

assertThat(ActionsTestUtil.prettyArtifactNames(relative.getDeclaredIncludeSrcs()))
.containsExactly("third_party/a/_virtual_includes/relative/b.h", "third_party/a/v1/b.h");
.containsExactly("_virtual_includes/6665bb5b/b.h", "third_party/a/v1/b.h");
assertThat(ActionsTestUtil.prettyArtifactNames(absolute.getDeclaredIncludeSrcs()))
.containsExactly(
"third_party/a/_virtual_includes/absolute/a/v1/b.h", "third_party/a/v1/b.h");
.containsExactly("_virtual_includes/ee72ce06/a/v1/b.h", "third_party/a/v1/b.h");
}

@Test
Expand Down Expand Up @@ -1205,8 +1205,12 @@ public void testSymlinkActionIsNotRegisteredWhenIncludePrefixDoesntChangePath()

CcCompilationContext ccCompilationContext =
getConfiguredTarget("//third_party:a").get(CcInfo.PROVIDER).getCcCompilationContext();
assertThat(ActionsTestUtil.prettyArtifactNames(ccCompilationContext.getDeclaredIncludeSrcs()))
.doesNotContain("third_party/_virtual_includes/a/third_party/a.h");
assertThat(
Joiner.on(" ")
.join(
ActionsTestUtil.prettyArtifactNames(
ccCompilationContext.getDeclaredIncludeSrcs())))
.doesNotContainMatch("_virtual_includes/[a-z0-9]{8}/third_party/a.h");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public void testExternalIncludePathsVariable() throws Exception {
ImmutableList.Builder<String> entries =
ImmutableList.<String>builder()
.add(
"/k8-fastbuild/bin/external/pkg+/_virtual_includes/foo2",
"/k8-fastbuild/bin/_virtual_includes/8a060f18",
"external/pkg+",
"/k8-fastbuild/bin/external/pkg+");
if (analysisMock.isThisBazel()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5826,8 +5826,7 @@ public void testIncludePrefix() throws Exception {
assertThat(target).isNotNull();
CcInfo ccInfo = target.get(CcInfo.PROVIDER);
assertThat(artifactsToStrings(ccInfo.getCcCompilationContext().getDirectPublicHdrs()))
.contains(
"bin third_party/bar/_virtual_includes/starlark_lib_suffix/prefix/starlark_lib.h");
.contains("bin _virtual_includes/c203db2d/prefix/starlark_lib.h");
}

@Test
Expand All @@ -5850,7 +5849,7 @@ public void testStripIncludePrefix() throws Exception {
assertThat(target).isNotNull();
CcInfo ccInfo = target.get(CcInfo.PROVIDER);
assertThat(artifactsToStrings(ccInfo.getCcCompilationContext().getDirectPublicHdrs()))
.contains("bin third_party/bar/_virtual_includes/starlark_lib_suffix/starlark_lib.h");
.contains("bin _virtual_includes/c203db2d/starlark_lib.h");
}

@Test
Expand All @@ -5877,7 +5876,7 @@ public void testStripIncludePrefixIncludePath() throws Exception {
.containsExactly(
getTargetConfiguration()
.getBinFragment(RepositoryName.MAIN)
.getRelative("third_party/bar/_virtual_includes/starlark_lib_suffix"));
.getRelative("_virtual_includes/c203db2d"));
}

@Test
Expand All @@ -5902,8 +5901,7 @@ public void testStripIncludePrefixAndIncludePrefix() throws Exception {
assertThat(target).isNotNull();
CcInfo ccInfo = target.get(CcInfo.PROVIDER);
assertThat(artifactsToStrings(ccInfo.getCcCompilationContext().getDirectPublicHdrs()))
.contains(
"bin third_party/bar/_virtual_includes/starlark_lib_suffix/prefix/starlark_lib.h");
.contains("bin _virtual_includes/c203db2d/prefix/starlark_lib.h");
}

@Test
Expand All @@ -5928,7 +5926,7 @@ public void testStripIncludePrefixAndIncludePrefixIncludePath() throws Exception
.containsExactly(
getTargetConfiguration()
.getBinFragment(RepositoryName.MAIN)
.getRelative("third_party/bar/_virtual_includes/starlark_lib_suffix"));
.getRelative("_virtual_includes/c203db2d"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.prettyArtifactNames;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.eventbus.EventBus;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
import com.google.devtools.build.lib.packages.util.MockProtoSupport;
Expand Down Expand Up @@ -246,6 +248,44 @@ public void outputDirectoryForProtoCompileAction() throws Exception {
"--cpp_out=%s", getTargetConfiguration().getGenfilesFragment(RepositoryName.MAIN)));
}

@Test
public void outputDirectoryForProtoCompileAction_externalRepos() throws Exception {
setBuildLanguageOptions("--experimental_builtins_injection_override=+cc_proto_library");
scratch.file(
"x/BUILD",
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['@bla//foo:bar_proto'])");

scratch.file("/bla/REPO.bazel");
// Create the rule '@bla//foo:bar_proto'.
scratch.file(
"/bla/foo/BUILD",
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
"package(default_visibility=['//visibility:public'])",
"proto_library(name = 'bar_proto', srcs = ['bar.proto'])");
scratch.appendFile(
"MODULE.bazel",
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\","
+ " \"local_repository\")",
"local_repository(name = 'bla', path = '/bla/')");
invalidatePackages(); // A dash of magic to re-evaluate the MODULE.bazel file.

ConfiguredTarget target = getConfiguredTarget("//x:foo_cc_proto");
Artifact hFile = getFirstArtifactEndingWith(getFilesToBuild(target), "bar.pb.h");
SpawnAction protoCompileAction = getGeneratingSpawnAction(hFile);

assertThat(protoCompileAction.getArguments())
.contains(
String.format(
"--cpp_out=%s/external/+local_repository+bla",
getTargetConfiguration().getBinFragment(RepositoryName.MAIN)));

CcCompilationContext ccCompilationContext =
target.get(CcInfo.PROVIDER).getCcCompilationContext();
assertThat(prettyArtifactNames(ccCompilationContext.getDeclaredIncludeSrcs()))
.containsExactly("external/+local_repository+bla/foo/bar.pb.h");
}

@Test
public void commandLineControlsOutputFileSuffixes() throws Exception {
getAnalysisMock()
Expand Down Expand Up @@ -292,6 +332,106 @@ public void generatedSourcesNotCoverageInstrumented() throws Exception {
assertThat(options).doesNotContain("-ftest-coverage");
}

@Test
public void importPrefixWorksWithRepositories() throws Exception {
scratch.appendFile(
"MODULE.bazel",
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\","
+ " \"local_repository\")",
"local_repository(name = 'yolo_repo', path = '/yolo_repo/')");
invalidatePackages();

scratch.file("/yolo_repo/REPO.bazel");
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
scratch.file(
"/yolo_repo/yolo_pkg/BUILD",
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"proto_library(",
" name = 'yolo_proto',",
" srcs = ['yolo.proto'],",
" import_prefix = 'bazel.build/yolo',",
")",
"cc_proto_library(",
" name = 'yolo_cc_proto',",
" deps = [':yolo_proto'],",
")");
assertThat(getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto")).isNotNull();
assertThat(getProtoHeaderExecPath())
.endsWith("_virtual_includes/f25ac60e/bazel.build/yolo/yolo_pkg/yolo.pb.h");
}

@Test
public void stripImportPrefixWorksWithRepositories() throws Exception {
scratch.appendFile(
"MODULE.bazel",
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\","
+ " \"local_repository\")",
"local_repository(name = 'yolo_repo', path = '/yolo_repo/')");
invalidatePackages();

scratch.file("/yolo_repo/REPO.bazel");
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
scratch.file(
"/yolo_repo/yolo_pkg/BUILD",
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"proto_library(",
" name = 'yolo_proto',",
" srcs = ['yolo.proto'],",
" strip_import_prefix = '/yolo_pkg',",
")",
"cc_proto_library(",
" name = 'yolo_cc_proto',",
" deps = [':yolo_proto'],",
")");
assertThat(getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto")).isNotNull();
assertThat(getProtoHeaderExecPath()).endsWith("_virtual_includes/f25ac60e/yolo.pb.h");
}

@Test
public void importPrefixAndStripImportPrefixWorksWithRepositories() throws Exception {
scratch.appendFile(
"MODULE.bazel",
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\","
+ " \"local_repository\")",
"local_repository(name = 'yolo_repo', path = '/yolo_repo/')");
invalidatePackages();

scratch.file("/yolo_repo/REPO.bazel");
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
scratch.file(
"/yolo_repo/yolo_pkg/BUILD",
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"proto_library(",
" name = 'yolo_proto',",
" srcs = ['yolo.proto'],",
" import_prefix = 'bazel.build/yolo',",
" strip_import_prefix = '/yolo_pkg'",
")",
"cc_proto_library(",
" name = 'yolo_cc_proto',",
" deps = [':yolo_proto'],",
")");
getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto");

assertThat(getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto")).isNotNull();
assertThat(getProtoHeaderExecPath())
.endsWith("_virtual_includes/f25ac60e/bazel.build/yolo/yolo.pb.h");
}

private String getProtoHeaderExecPath() throws LabelSyntaxException {
ConfiguredTarget configuredTarget =
getConfiguredTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto");
CcInfo ccInfo = configuredTarget.get(CcInfo.PROVIDER);
ImmutableList<Artifact> headers =
ccInfo.getCcCompilationContext().getDeclaredIncludeSrcs().toList();
// TODO(b/364873432): cc_proto_library returns headers in both _virtual_includes and
// _virtual_imports
return Iterables.getFirst(headers, null).getExecPathString();
}

@Test
public void testCcProtoLibraryLoadedThroughMacro() throws Exception {
if (!analysisMock.isThisBazel()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1443,8 +1443,7 @@ public void testIncludesDirsOfTransitiveCcIncDepsGetPassedToCompileAction() thro
CommandAction compileAction = compileAction("//package:objc_lib", "b.o");
// We remove spaces, since the crosstool rules do not use spaces for include paths.
String compileActionArgs = Joiner.on("").join(compileAction.getArguments()).replace(" ", "");
assertThat(compileActionArgs)
.matches(".*-iquote.*/third_party/cc_lib/_virtual_includes/cc_lib.*");
assertThat(compileActionArgs).matches(".*-iquote.*/_virtual_includes/848637ee.*");
}

@Test
Expand Down