fix protos in another dir + a module that uses it

Protos in another directory were using import prefix, which was
prepending the repository-relative path with the value, instead, we want
to strip the prefix of the directory of the module the protos were used
in such that they can be referenced at the appropriate relative path.

Reference on import_prefix:
https://bazel.build/reference/be/protocol-buffer#proto_library.import_prefix

Test: b build //packages/modules/adb:libfastdeploy_host
Change-Id: If050b0f5fc5103bd9cc5a99703bd604325aa4204
This commit is contained in:
Liz Kammer
2023-08-10 12:51:59 -04:00
parent b6e0a64bf1
commit 7dc6bcbd58
3 changed files with 87 additions and 12 deletions

View File

@@ -4930,6 +4930,9 @@ cc_library_static {
"bar/bar.proto", // Different package because there is a bar/Android.bp
"baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
],
proto: {
canonical_path_from_root: true,
}
}
` + simpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
Filesystem: map[string]string{
@@ -4963,8 +4966,7 @@ cc_library_static {
tc.Dir = "bar"
tc.ExpectedBazelTargets = []string{
MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
"srcs": `["//bar:bar.proto"]`,
"import_prefix": `"bar"`,
"srcs": `["//bar:bar.proto"]`,
}),
}
runCcLibraryTestCase(t, tc)
@@ -4973,8 +4975,77 @@ cc_library_static {
tc.Dir = "baz/subbaz"
tc.ExpectedBazelTargets = []string{
MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
"srcs": `["//baz/subbaz:baz.proto"]`,
"import_prefix": `"baz/subbaz"`,
"srcs": `["//baz/subbaz:baz.proto"]`,
}),
}
runCcLibraryTestCase(t, tc)
}
// Bazel enforces that proto_library and the .proto file are in the same bazel package
func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) {
tc := Bp2buildTestCase{
Description: "cc_library depends on .proto files from multiple packages",
ModuleTypeUnderTest: "cc_library",
ModuleTypeUnderTestFactory: cc.LibraryFactory,
Blueprint: `
cc_library_static {
name: "foo",
srcs: [
"foo.proto",
"bar/bar.proto", // Different package because there is a bar/Android.bp
"baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp
],
proto: {
canonical_path_from_root: false,
}
}
` + simpleModuleDoNotConvertBp2build("cc_library", "libprotobuf-cpp-lite"),
Filesystem: map[string]string{
"bar/Android.bp": "",
"baz/subbaz/Android.bp": "",
},
}
// We will run the test 3 times and check in the root, bar and baz/subbaz directories
// Root dir
tc.ExpectedBazelTargets = []string{
MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
"local_includes": `["."]`,
"deps": `[":libprotobuf-cpp-lite"]`,
"implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
}),
MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
"srcs": `["foo.proto"]`,
"strip_import_prefix": `""`,
}),
MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
"deps": `[
":foo_proto",
"//bar:foo_proto",
"//baz/subbaz:foo_proto",
]`,
}),
}
runCcLibraryTestCase(t, tc)
// bar dir
tc.Dir = "bar"
tc.ExpectedBazelTargets = []string{
MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
"srcs": `["//bar:bar.proto"]`,
"strip_import_prefix": `""`,
"import_prefix": `"bar"`,
}),
}
runCcLibraryTestCase(t, tc)
// baz/subbaz dir
tc.Dir = "baz/subbaz"
tc.ExpectedBazelTargets = []string{
MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
"srcs": `["//baz/subbaz:baz.proto"]`,
"strip_import_prefix": `""`,
"import_prefix": `"baz/subbaz"`,
}),
}
runCcLibraryTestCase(t, tc)