19
19
import static com .google .devtools .build .lib .actions .util .ActionsTestUtil .prettyArtifactNames ;
20
20
21
21
import com .google .common .collect .ImmutableList ;
22
+ import com .google .common .collect .Iterables ;
22
23
import com .google .common .eventbus .EventBus ;
23
24
import com .google .devtools .build .lib .actions .Artifact ;
24
25
import com .google .devtools .build .lib .analysis .ConfiguredTarget ;
25
26
import com .google .devtools .build .lib .analysis .actions .SpawnAction ;
26
27
import com .google .devtools .build .lib .analysis .util .BuildViewTestCase ;
28
+ import com .google .devtools .build .lib .cmdline .LabelSyntaxException ;
27
29
import com .google .devtools .build .lib .cmdline .RepositoryName ;
28
30
import com .google .devtools .build .lib .packages .util .Crosstool .CcToolchainConfig ;
29
31
import com .google .devtools .build .lib .packages .util .MockProtoSupport ;
32
34
import com .google .devtools .build .lib .rules .cpp .CppCompileAction ;
33
35
import com .google .devtools .build .lib .rules .cpp .CppRuleClasses ;
34
36
import com .google .devtools .build .lib .testutil .TestConstants ;
37
+ import com .google .devtools .build .lib .vfs .FileSystemUtils ;
35
38
import java .util .List ;
36
39
import org .junit .Before ;
37
40
import org .junit .Test ;
@@ -246,6 +249,43 @@ public void outputDirectoryForProtoCompileAction() throws Exception {
246
249
"--cpp_out=%s" , getTargetConfiguration ().getGenfilesFragment (RepositoryName .MAIN )));
247
250
}
248
251
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
+
249
289
@ Test
250
290
public void commandLineControlsOutputFileSuffixes () throws Exception {
251
291
getAnalysisMock ()
@@ -292,6 +332,102 @@ public void generatedSourcesNotCoverageInstrumented() throws Exception {
292
332
assertThat (options ).doesNotContain ("-ftest-coverage" );
293
333
}
294
334
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
+
295
431
@ Test
296
432
public void testCcProtoLibraryLoadedThroughMacro () throws Exception {
297
433
if (!analysisMock .isThisBazel ()) {
@@ -317,4 +453,4 @@ private void setupTestCcProtoLibraryLoadedThroughMacro(boolean loadMacro) throws
317
453
" srcs = ['a.proto']," ,
318
454
")" );
319
455
}
320
- }
456
+ }
0 commit comments