Skip to content

Commit 7841b51

Browse files
committed
Add back some proto library tests which was accidentally deleted in 37e0d23
1 parent 6e10c0e commit 7841b51

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

src/main/starlark/builtins_bzl/common/cc/compile/cc_compilation_helper.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _compute_public_headers(
111111

112112
module_map_headers = []
113113
virtual_to_original_headers_list = []
114-
virtual_include_dir = paths.join(_VIRTUAL_INCLUDES_DIR, "%x" % hash(paths.join(cc_helper.package_source_root(label.workspace_name, label.package, is_sibling_repository_layout), label.name)))
114+
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)))
115115
for original_header in public_headers_artifacts:
116116
repo_relative_path = _repo_relative_path(original_header)
117117
if not repo_relative_path.startswith(strip_prefix):

src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.prettyArtifactNames;
2020

2121
import com.google.common.collect.ImmutableList;
22+
import com.google.common.collect.Iterables;
2223
import com.google.common.eventbus.EventBus;
2324
import com.google.devtools.build.lib.actions.Artifact;
2425
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
2526
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
2627
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
28+
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
2729
import com.google.devtools.build.lib.cmdline.RepositoryName;
2830
import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
2931
import com.google.devtools.build.lib.packages.util.MockProtoSupport;
@@ -32,6 +34,7 @@
3234
import com.google.devtools.build.lib.rules.cpp.CppCompileAction;
3335
import com.google.devtools.build.lib.rules.cpp.CppRuleClasses;
3436
import com.google.devtools.build.lib.testutil.TestConstants;
37+
import com.google.devtools.build.lib.vfs.FileSystemUtils;
3538
import java.util.List;
3639
import org.junit.Before;
3740
import org.junit.Test;
@@ -246,6 +249,43 @@ public void outputDirectoryForProtoCompileAction() throws Exception {
246249
"--cpp_out=%s", getTargetConfiguration().getGenfilesFragment(RepositoryName.MAIN)));
247250
}
248251

252+
@Test
253+
public void outputDirectoryForProtoCompileAction_externalRepos() throws Exception {
254+
setBuildLanguageOptions("--experimental_builtins_injection_override=+cc_proto_library");
255+
scratch.file(
256+
"x/BUILD",
257+
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
258+
"cc_proto_library(name = 'foo_cc_proto', deps = ['@bla//foo:bar_proto'])");
259+
260+
scratch.file("/bla/REPO.bazel");
261+
// Create the rule '@bla//foo:bar_proto'.
262+
scratch.file(
263+
"/bla/foo/BUILD",
264+
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
265+
"package(default_visibility=['//visibility:public'])",
266+
"proto_library(name = 'bar_proto', srcs = ['bar.proto'])");
267+
scratch.appendFile(
268+
"MODULE.bazel",
269+
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\", \"local_repository\")",
270+
"local_repository(name = 'bla', path = '/bla/')");
271+
invalidatePackages(); // A dash of magic to re-evaluate the MODULE.bazel file.
272+
273+
ConfiguredTarget target = getConfiguredTarget("//x:foo_cc_proto");
274+
Artifact hFile = getFirstArtifactEndingWith(getFilesToBuild(target), "bar.pb.h");
275+
SpawnAction protoCompileAction = getGeneratingSpawnAction(hFile);
276+
277+
assertThat(protoCompileAction.getArguments())
278+
.contains(
279+
String.format(
280+
"--cpp_out=%s/external/+local_repository+bla",
281+
getTargetConfiguration().getGenfilesFragment(RepositoryName.MAIN)));
282+
283+
CcCompilationContext ccCompilationContext =
284+
target.get(CcInfo.PROVIDER).getCcCompilationContext();
285+
assertThat(prettyArtifactNames(ccCompilationContext.getDeclaredIncludeSrcs()))
286+
.containsExactly("external/+local_repository+bla/foo/bar.pb.h");
287+
}
288+
249289
@Test
250290
public void commandLineControlsOutputFileSuffixes() throws Exception {
251291
getAnalysisMock()
@@ -292,6 +332,102 @@ public void generatedSourcesNotCoverageInstrumented() throws Exception {
292332
assertThat(options).doesNotContain("-ftest-coverage");
293333
}
294334

335+
@Test
336+
public void importPrefixWorksWithRepositories() throws Exception {
337+
scratch.appendFile(
338+
"MODULE.bazel",
339+
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\", \"local_repository\")",
340+
"local_repository(name = 'yolo_repo', path = '/yolo_repo/')");
341+
invalidatePackages();
342+
343+
scratch.file("/yolo_repo/REPO.bazel");
344+
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
345+
scratch.file(
346+
"/yolo_repo/yolo_pkg/BUILD",
347+
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
348+
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
349+
"proto_library(",
350+
" name = 'yolo_proto',",
351+
" srcs = ['yolo.proto'],",
352+
" import_prefix = 'bazel.build/yolo',",
353+
")",
354+
"cc_proto_library(",
355+
" name = 'yolo_cc_proto',",
356+
" deps = [':yolo_proto'],",
357+
")");
358+
assertThat(getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto")).isNotNull();
359+
assertThat(getProtoHeaderExecPath())
360+
.endsWith("_virtual_includes/f25ac60e/bazel.build/yolo/yolo_pkg/yolo.pb.h");
361+
}
362+
363+
@Test
364+
public void stripImportPrefixWorksWithRepositories() throws Exception {
365+
scratch.appendFile(
366+
"MODULE.bazel",
367+
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\", \"local_repository\")",
368+
"local_repository(name = 'yolo_repo', path = '/yolo_repo/')");
369+
invalidatePackages();
370+
371+
scratch.file("/yolo_repo/REPO.bazel");
372+
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
373+
scratch.file(
374+
"/yolo_repo/yolo_pkg/BUILD",
375+
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
376+
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
377+
"proto_library(",
378+
" name = 'yolo_proto',",
379+
" srcs = ['yolo.proto'],",
380+
" strip_import_prefix = '/yolo_pkg',",
381+
")",
382+
"cc_proto_library(",
383+
" name = 'yolo_cc_proto',",
384+
" deps = [':yolo_proto'],",
385+
")");
386+
assertThat(getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto")).isNotNull();
387+
assertThat(getProtoHeaderExecPath()).endsWith("_virtual_includes/f25ac60e/yolo.pb.h");
388+
}
389+
390+
@Test
391+
public void importPrefixAndStripImportPrefixWorksWithRepositories() throws Exception {
392+
scratch.appendFile(
393+
"MODULE.bazel",
394+
"local_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:local.bzl\", \"local_repository\")",
395+
"local_repository(name = 'yolo_repo', path = '/yolo_repo/')");
396+
invalidatePackages();
397+
398+
scratch.file("/yolo_repo/REPO.bazel");
399+
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
400+
scratch.file(
401+
"/yolo_repo/yolo_pkg/BUILD",
402+
"load('@com_google_protobuf//bazel:proto_library.bzl', 'proto_library')",
403+
"load('@com_google_protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
404+
"proto_library(",
405+
" name = 'yolo_proto',",
406+
" srcs = ['yolo.proto'],",
407+
" import_prefix = 'bazel.build/yolo',",
408+
" strip_import_prefix = '/yolo_pkg'",
409+
")",
410+
"cc_proto_library(",
411+
" name = 'yolo_cc_proto',",
412+
" deps = [':yolo_proto'],",
413+
")");
414+
getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto");
415+
416+
assertThat(getTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto")).isNotNull();
417+
assertThat(getProtoHeaderExecPath())
418+
.endsWith("_virtual_includes/f25ac60e/bazel.build/yolo/yolo.pb.h");
419+
}
420+
421+
private String getProtoHeaderExecPath() throws LabelSyntaxException {
422+
ConfiguredTarget configuredTarget = getConfiguredTarget("@@+local_repository+yolo_repo//yolo_pkg:yolo_cc_proto");
423+
CcInfo ccInfo = configuredTarget.get(CcInfo.PROVIDER);
424+
ImmutableList<Artifact> headers =
425+
ccInfo.getCcCompilationContext().getDeclaredIncludeSrcs().toList();
426+
// TODO(b/364873432): cc_proto_library returns headers in both _virtual_includes and
427+
// _virtual_imports
428+
return Iterables.getFirst(headers, null).getExecPathString();
429+
}
430+
295431
@Test
296432
public void testCcProtoLibraryLoadedThroughMacro() throws Exception {
297433
if (!analysisMock.isThisBazel()) {
@@ -317,4 +453,4 @@ private void setupTestCcProtoLibraryLoadedThroughMacro(boolean loadMacro) throws
317453
" srcs = ['a.proto'],",
318454
")");
319455
}
320-
}
456+
}

0 commit comments

Comments
 (0)