Handle proto.include_dirs in bp2build for CC
Soong's proto.include_dirs becomes the -I path for aprotoc cpp code
generation. This does not translate well to Bazel because it runs this
action in a sandbox. Even if we construct an -I path, the .proto files
will not be there. This CL attempts to handle this kind of dependency
automatically via bp2build.
For this hypothetical example
```
foo.proto # contains `import bar.proto"
Android.bp # cc_library {srcs:"foo.proto", p.include_dirs:["subdir"]},
subdir/bar.proto
```
Implementation details for CcProtoGen of foo
- Glob the labels of .proto files for each includeDir, and create a
proto_library that encapsulates them.
- Since Bazel poses a contraint that proto_library target needs to be
in the same pacakge as the .proto file, the proto_library might be created
in a subdirectory with an import_prefix
- Add bar's proto_library as transitive deps of foo's cc_proto_library.
This will be added to -I during aprotoc. We cannot add them to `deps`,
otherwise bar's symbols will be statically linked into foo's .a
file.
Implementation details for clang compile of foo
At the end of CcProtoGen, we have converted foo.proto
to .cpp/.h files. To compile them to .a files, we need the .h files
generated from bar.proto. Soong specifies this at the
top-level cc_library soong module, so add those deps as the
implementation deps for clang compile.
(Will add support for java in a follow-up CL)
Test: go test ./bp2build
Test: built some internal modules that were previously blocked by this
Bug: 285140726
Change-Id: I7e1f9f0d1b1ba916a7ba8278f6cfb342b381d695
diff --git a/cc/proto.go b/cc/proto.go
index 5d9aef6..0ed4381 100644
--- a/cc/proto.go
+++ b/cc/proto.go
@@ -165,7 +165,17 @@
}
type protoAttributes struct {
- Deps bazel.LabelListAttribute
+ Deps bazel.LabelListAttribute
+
+ // A list of proto_library targets that that the proto_library in `deps` depends on
+ // This list is overestimation.
+ // Overestimation is necessary since Soong includes other protos via proto.include_dirs and not
+ // a specific .proto file module explicitly.
+ Transitive_deps bazel.LabelListAttribute
+
+ // A list of cc_library_* targets that the generated cpp code depends on
+ Cc_deps bazel.LabelListAttribute
+
Min_sdk_version *string
}
@@ -175,7 +185,7 @@
protoDep *bazel.LabelAttribute
}
-func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs bazel.LabelListAttribute) bp2buildProtoDeps {
+func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs bazel.LabelListAttribute, la linkerAttributes) bp2buildProtoDeps {
var ret bp2buildProtoDeps
protoInfo, ok := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, protoSrcs)
@@ -204,6 +214,35 @@
var protoAttrs protoAttributes
protoAttrs.Deps.SetValue(protoInfo.Proto_libs)
+ protoAttrs.Transitive_deps.SetValue(protoInfo.Transitive_proto_libs)
+
+ // Add the implementation deps of the top-level cc_library_static
+ // This is necessary to compile the internal root of cc_proto_library.
+ // Without this, clang might not be able to find .h files that the generated cpp files depends on
+ protoAttrs.Cc_deps = *la.implementationDeps.Clone()
+ protoAttrs.Cc_deps.Append(la.implementationDynamicDeps)
+ protoAttrs.Cc_deps.Append(la.implementationWholeArchiveDeps)
+ protoAttrs.Cc_deps.Append(la.wholeArchiveDeps)
+ // Subtract myself to prevent possible circular dep
+ protoAttrs.Cc_deps = bazel.SubtractBazelLabelListAttribute(
+ protoAttrs.Cc_deps,
+ bazel.MakeLabelListAttribute(
+ bazel.MakeLabelList([]bazel.Label{
+ bazel.Label{Label: ":" + m.Name() + suffix},
+ }),
+ ),
+ )
+ // Subtract the protobuf libraries since cc_proto_library implicitly adds them
+ protoAttrs.Cc_deps = bazel.SubtractBazelLabelListAttribute(
+ protoAttrs.Cc_deps,
+ bazel.MakeLabelListAttribute(
+ bazel.MakeLabelList([]bazel.Label{
+ bazel.Label{Label: "//external/protobuf:libprotobuf-cpp-full", OriginalModuleName: "libprotobuf-cpp-full"},
+ bazel.Label{Label: "//external/protobuf:libprotobuf-cpp-lite", OriginalModuleName: "libprotobuf-cpp-lite"},
+ }),
+ ),
+ )
+
protoAttrs.Min_sdk_version = m.Properties.Min_sdk_version
name := m.Name() + suffix