Merge "Support empty strings in bp2build" am: a9351ef6e6 am: 7cb87525ef am: 3044f94f32

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1907331

Change-Id: Ibbf71c6e6dd331ba639219dfab935c0422af86a8
This commit is contained in:
Liz Kammer
2021-12-02 17:03:19 +00:00
committed by Automerger Merge Worker
7 changed files with 57 additions and 28 deletions

View File

@@ -3201,7 +3201,7 @@ type bazelApexBundleAttributes struct {
File_contexts bazel.LabelAttribute File_contexts bazel.LabelAttribute
Key bazel.LabelAttribute Key bazel.LabelAttribute
Certificate bazel.LabelAttribute Certificate bazel.LabelAttribute
Min_sdk_version string Min_sdk_version *string
Updatable bazel.BoolAttribute Updatable bazel.BoolAttribute
Installable bazel.BoolAttribute Installable bazel.BoolAttribute
Native_shared_libs bazel.LabelListAttribute Native_shared_libs bazel.LabelListAttribute
@@ -3241,9 +3241,9 @@ func apexBundleBp2BuildInternal(ctx android.TopDownMutatorContext, module *apexB
fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *module.properties.File_contexts)) fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *module.properties.File_contexts))
} }
var minSdkVersion string var minSdkVersion *string
if module.properties.Min_sdk_version != nil { if module.properties.Min_sdk_version != nil {
minSdkVersion = *module.properties.Min_sdk_version minSdkVersion = module.properties.Min_sdk_version
} }
var keyLabelAttribute bazel.LabelAttribute var keyLabelAttribute bazel.LabelAttribute

View File

@@ -698,9 +698,9 @@ func isZero(value reflect.Value) bool {
} else { } else {
return true return true
} }
// Always print bools, if you want a bool attribute to be able to take the default value, use a // Always print bool/strings, if you want a bool/string attribute to be able to take the default value, use a
// bool pointer instead // pointer instead
case reflect.Bool: case reflect.Bool, reflect.String:
return false return false
default: default:
if !value.IsValid() { if !value.IsValid() {

View File

@@ -41,6 +41,7 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
soong_module_deps = [ soong_module_deps = [
], ],
bool_prop = False, bool_prop = False,
string_prop = "",
)`, )`,
}, },
{ {
@@ -58,6 +59,7 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
soong_module_deps = [ soong_module_deps = [
], ],
bool_prop = True, bool_prop = True,
string_prop = "",
)`, )`,
}, },
{ {
@@ -76,6 +78,7 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
], ],
bool_prop = False, bool_prop = False,
owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
string_prop = "",
)`, )`,
}, },
{ {
@@ -94,6 +97,7 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
], ],
bool_prop = False, bool_prop = False,
required = ["bar"], required = ["bar"],
string_prop = "",
)`, )`,
}, },
{ {
@@ -111,6 +115,7 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
soong_module_deps = [ soong_module_deps = [
], ],
bool_prop = False, bool_prop = False,
string_prop = "",
target_required = [ target_required = [
"qux", "qux",
"bazqux", "bazqux",
@@ -147,6 +152,7 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
"tag": ".bar", "tag": ".bar",
"targets": ["goal_bar"], "targets": ["goal_bar"],
}], }],
string_prop = "",
)`, )`,
}, },
{ {
@@ -179,6 +185,7 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
}], }],
owner = "custom_owner", owner = "custom_owner",
required = ["bar"], required = ["bar"],
string_prop = "",
target_required = [ target_required = [
"qux", "qux",
"bazqux", "bazqux",
@@ -222,12 +229,25 @@ func TestGenerateSoongModuleTargets(t *testing.T) {
func TestGenerateBazelTargetModules(t *testing.T) { func TestGenerateBazelTargetModules(t *testing.T) {
testCases := []bp2buildTestCase{ testCases := []bp2buildTestCase{
{
description: "string ptr props",
blueprint: `custom {
name: "foo",
string_ptr_prop: "",
bazel_module: { bp2build_available: true },
}`,
expectedBazelTargets: []string{
makeBazelTarget("custom", "foo", attrNameToString{
"string_ptr_prop": `""`,
}),
},
},
{ {
description: "string props", description: "string props",
blueprint: `custom { blueprint: `custom {
name: "foo", name: "foo",
string_list_prop: ["a", "b"], string_list_prop: ["a", "b"],
string_prop: "a", string_ptr_prop: "a",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
@@ -236,7 +256,7 @@ func TestGenerateBazelTargetModules(t *testing.T) {
"a", "a",
"b", "b",
]`, ]`,
"string_prop": `"a"`, "string_ptr_prop": `"a"`,
}), }),
}, },
}, },
@@ -245,7 +265,7 @@ func TestGenerateBazelTargetModules(t *testing.T) {
blueprint: `custom { blueprint: `custom {
name: "foo", name: "foo",
string_list_prop: ["\t", "\n"], string_list_prop: ["\t", "\n"],
string_prop: "a\t\n\r", string_ptr_prop: "a\t\n\r",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
@@ -254,7 +274,7 @@ func TestGenerateBazelTargetModules(t *testing.T) {
"\t", "\t",
"\n", "\n",
]`, ]`,
"string_prop": `"a\t\n\r"`, "string_ptr_prop": `"a\t\n\r"`,
}), }),
}, },
}, },

View File

@@ -149,15 +149,15 @@ func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.Regi
} }
type nestedProps struct { type nestedProps struct {
Nested_prop string Nested_prop *string
} }
type EmbeddedProps struct { type EmbeddedProps struct {
Embedded_prop string Embedded_prop *string
} }
type OtherEmbeddedProps struct { type OtherEmbeddedProps struct {
Other_embedded_prop string Other_embedded_prop *string
} }
type customProps struct { type customProps struct {
@@ -262,17 +262,17 @@ func customDefaultsModuleFactory() android.Module {
} }
type EmbeddedAttr struct { type EmbeddedAttr struct {
Embedded_attr string Embedded_attr *string
} }
type OtherEmbeddedAttr struct { type OtherEmbeddedAttr struct {
Other_embedded_attr string Other_embedded_attr *string
} }
type customBazelModuleAttributes struct { type customBazelModuleAttributes struct {
EmbeddedAttr EmbeddedAttr
*OtherEmbeddedAttr *OtherEmbeddedAttr
String_prop string String_ptr_prop *string
String_list_prop []string String_list_prop []string
Arch_paths bazel.LabelListAttribute Arch_paths bazel.LabelListAttribute
} }
@@ -296,7 +296,7 @@ func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
paths.ResolveExcludes() paths.ResolveExcludes()
attrs := &customBazelModuleAttributes{ attrs := &customBazelModuleAttributes{
String_prop: m.props.String_prop, String_ptr_prop: m.props.String_ptr_prop,
String_list_prop: m.props.String_list_prop, String_list_prop: m.props.String_list_prop,
Arch_paths: paths, Arch_paths: paths,
} }

View File

@@ -388,7 +388,15 @@ func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *
} }
cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal) cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal)
return &cStdVal, &cppStdVal var c_std_prop, cpp_std_prop *string
if cStdVal != "" {
c_std_prop = &cStdVal
}
if cppStdVal != "" {
cpp_std_prop = &cppStdVal
}
return c_std_prop, cpp_std_prop
} }
// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.

View File

@@ -35,10 +35,10 @@ func registerPythonBinaryComponents(ctx android.RegistrationContext) {
} }
type bazelPythonBinaryAttributes struct { type bazelPythonBinaryAttributes struct {
Main string Main *string
Srcs bazel.LabelListAttribute Srcs bazel.LabelListAttribute
Deps bazel.LabelListAttribute Deps bazel.LabelListAttribute
Python_version string Python_version *string
} }
func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) { func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) {
@@ -52,12 +52,12 @@ func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) {
return return
} }
var main string var main *string
for _, propIntf := range m.GetProperties() { for _, propIntf := range m.GetProperties() {
if props, ok := propIntf.(*BinaryProperties); ok { if props, ok := propIntf.(*BinaryProperties); ok {
// main is optional. // main is optional.
if props.Main != nil { if props.Main != nil {
main = *props.Main main = props.Main
break break
} }
} }
@@ -69,13 +69,13 @@ func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) {
// under Bionic. // under Bionic.
py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false) py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false)
py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false) py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
var python_version string var python_version *string
if py3Enabled && py2Enabled { if py3Enabled && py2Enabled {
panic(fmt.Errorf( panic(fmt.Errorf(
"error for '%s' module: bp2build's python_binary_host converter does not support "+ "error for '%s' module: bp2build's python_binary_host converter does not support "+
"converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name())) "converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name()))
} else if py2Enabled { } else if py2Enabled {
python_version = "PY2" python_version = &pyVersion2
} else { } else {
// do nothing, since python_version defaults to PY3. // do nothing, since python_version defaults to PY3.
} }

View File

@@ -21,6 +21,7 @@ import (
"android/soong/android" "android/soong/android"
"android/soong/bazel" "android/soong/bazel"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
) )
@@ -46,7 +47,7 @@ func PythonLibraryHostFactory() android.Module {
type bazelPythonLibraryAttributes struct { type bazelPythonLibraryAttributes struct {
Srcs bazel.LabelListAttribute Srcs bazel.LabelListAttribute
Deps bazel.LabelListAttribute Deps bazel.LabelListAttribute
Srcs_version string Srcs_version *string
} }
func PythonLibraryHostBp2Build(ctx android.TopDownMutatorContext) { func PythonLibraryHostBp2Build(ctx android.TopDownMutatorContext) {
@@ -74,11 +75,11 @@ func pythonLibBp2Build(ctx android.TopDownMutatorContext, modType string) {
// Bionic. // Bionic.
py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true) py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true)
py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false) py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
var python_version string var python_version *string
if py2Enabled && !py3Enabled { if py2Enabled && !py3Enabled {
python_version = "PY2" python_version = &pyVersion2
} else if !py2Enabled && py3Enabled { } else if !py2Enabled && py3Enabled {
python_version = "PY3" python_version = &pyVersion3
} else if !py2Enabled && !py3Enabled { } else if !py2Enabled && !py3Enabled {
panic(fmt.Errorf( panic(fmt.Errorf(
"error for '%s' module: bp2build's %s converter doesn't understand having "+ "error for '%s' module: bp2build's %s converter doesn't understand having "+