Emit gtest in BUILD files only if Android.bp sets it
cc_test bazel macro will default `gtest` to True (similar to Soong). So
we can skip explicitly setting this in the generated BUILD files. The
benefit will be that it will make the generated files less verbose,
without loss of information.
This will be implemented by changing its datatype to *bool from bool.
Test: go test ./bp2build
Test: TH
Change-Id: I284e10f1d58c8e7893b170209827f7d5084ca95e
diff --git a/cc/test.go b/cc/test.go
index 53a097a..0be2301 100644
--- a/cc/test.go
+++ b/cc/test.go
@@ -682,7 +682,7 @@
type testBinaryAttributes struct {
binaryAttributes
- Gtest bool
+ Gtest *bool
tidyAttributes
tradefed.TestConfigAttributes
@@ -720,15 +720,15 @@
m.convertTidyAttributes(ctx, &testBinaryAttrs.tidyAttributes)
- gtestIsolated := m.linker.(*testBinary).isolated(ctx)
- for _, propIntf := range m.GetProperties() {
- if testLinkerProps, ok := propIntf.(*TestLinkerProperties); ok {
- testBinaryAttrs.Gtest = proptools.BoolDefault(testLinkerProps.Gtest, true)
- break
- }
- }
+ testBinary := m.linker.(*testBinary)
+ gtest := testBinary.gtest()
+ gtestIsolated := testBinary.isolated(ctx)
+ // Use the underling bool pointer for Gtest in attrs
+ // This ensures that if this property is not set in Android.bp file, it will not be set in BUILD file either
+ // cc_test macro will default gtest to True
+ testBinaryAttrs.Gtest = testBinary.LinkerProperties.Gtest
- addImplicitGtestDeps(ctx, &testBinaryAttrs, gtestIsolated)
+ addImplicitGtestDeps(ctx, &testBinaryAttrs, gtest, gtestIsolated)
for _, testProps := range m.GetProperties() {
if p, ok := testProps.(*TestBinaryProperties); ok {
@@ -764,7 +764,7 @@
// cc_test that builds using gtest needs some additional deps
// addImplicitGtestDeps makes these deps explicit in the generated BUILD files
-func addImplicitGtestDeps(ctx android.BazelConversionPathContext, attrs *testBinaryAttributes, gtestIsolated bool) {
+func addImplicitGtestDeps(ctx android.BazelConversionPathContext, attrs *testBinaryAttributes, gtest, gtestIsolated bool) {
addDepsAndDedupe := func(lla *bazel.LabelListAttribute, modules []string) {
moduleLabels := android.BazelLabelForModuleDeps(ctx, modules)
lla.Value.Append(moduleLabels)
@@ -773,7 +773,7 @@
}
// this must be kept in sync with Soong's implementation in:
// https://cs.android.com/android/_/android/platform/build/soong/+/460fb2d6d546b5ab493a7e5479998c4933a80f73:cc/test.go;l=300-313;drc=ec7314336a2b35ea30ce5438b83949c28e3ac429;bpv=1;bpt=0
- if attrs.Gtest {
+ if gtest {
// TODO - b/244433197: Handle canUseSdk
if gtestIsolated {
addDepsAndDedupe(&attrs.Deps, []string{"libgtest_isolated_main"})