From 2d13614b8901044cb36e98f1f0aae7ff367fb0dd Mon Sep 17 00:00:00 2001 From: Yu Liu Date: Thu, 18 Aug 2022 14:46:13 -0700 Subject: [PATCH] Support proto.include_dirs For each package in the include_dirs property a proto_library target should be added to the BUILD file in that package and a mapping should be added to the bp2build code, by this way a proper dependency relationship can be established and used by bazel. Bug: 239944064 Test: Added unit tests and manually verified include_dirs can be properly converted to bazel and used by bazel to build the targets. Change-Id: I50d8ee21fabcfec0a44487f6e5f3d8a3845e79c3 --- android/proto.go | 18 ++++++++++- bp2build/cc_library_conversion_test.go | 44 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/android/proto.go b/android/proto.go index 8ad16a6a3..25cecf411 100644 --- a/android/proto.go +++ b/android/proto.go @@ -15,9 +15,10 @@ package android import ( - "android/soong/bazel" "strings" + "android/soong/bazel" + "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) @@ -161,6 +162,13 @@ type Bp2buildProtoInfo struct { type protoAttrs struct { Srcs bazel.LabelListAttribute Strip_import_prefix *string + Deps bazel.LabelListAttribute +} + +// For each package in the include_dirs property a proto_library target should +// be added to the BUILD file in that package and a mapping should be added here +var includeDirsToProtoDeps = map[string]string{ + "external/protobuf/src": "//external/protobuf:libprotobuf-proto", } // Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the @@ -191,6 +199,14 @@ func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs baz path := "" attrs.Strip_import_prefix = &path } + + for _, dir := range props.Proto.Include_dirs { + if dep, ok := includeDirsToProtoDeps[dir]; ok { + attrs.Deps.Add(bazel.MakeLabelAttribute(dep)) + } else { + ctx.PropertyErrorf("Could not find the proto_library target for include dir", dir) + } + } } else if props.Proto.Type != info.Type && props.Proto.Type != nil { ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.") } diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index 5117c625e..a21a5665b 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -2217,6 +2217,50 @@ func TestCcLibraryProtoExportHeaders(t *testing.T) { }) } +func TestCcLibraryProtoIncludeDirs(t *testing.T) { + runCcLibraryTestCase(t, Bp2buildTestCase{ + ModuleTypeUnderTest: "cc_library", + ModuleTypeUnderTestFactory: cc.LibraryFactory, + Blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + proto: { + include_dirs: ["external/protobuf/src"], + }, + include_build_directory: false, +}`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ + "srcs": `["foo.proto"]`, + "deps": `["//external/protobuf:libprotobuf-proto"]`, + }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{ + "deps": `[":foo_proto"]`, + }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ + "deps": `[":libprotobuf-cpp-lite"]`, + "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, + }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + }), + }, + }) +} + +func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) { + runCcLibraryTestCase(t, Bp2buildTestCase{ + ModuleTypeUnderTest: "cc_library", + ModuleTypeUnderTestFactory: cc.LibraryFactory, + Blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + proto: { + include_dirs: ["external/protobuf/abc"], + }, + include_build_directory: false, +}`, + ExpectedErr: fmt.Errorf("module \"foo\": Could not find the proto_library target for include dir: external/protobuf/abc"), + }) +} + func TestCcLibraryProtoFilegroups(t *testing.T) { runCcLibraryTestCase(t, Bp2buildTestCase{ ModuleTypeUnderTest: "cc_library",