platform_build_soong/bp2build/configurability.go
Jingwen Chen 91220d7334 Add os/target configurable selects for label list attributes.
This CL is pretty large, so I recommend starting with reading the newly
added tests for the expected behavior.

This change works in conjunction with the linked CLs in the Gerrit topic.
Those CLs add support for new platform() definitions for OS targets
specified in Soong's arch.go, which are configurable through
Android.bp's `target {}` property. It works similary to previous CLs
adding support for the `arch {}` property.

These configurable props are keyed by the OS: android, linux_bionic,
windows, and so on. They map to `select` statements in label list
attributes, which this CL enables for cc_library_headers' header_libs
and export_header_lib_headers props.

This enables //bionic/libc:libc_headers to be generated correctly, from:

    cc_library_headers {
        name: "libc_headers",
        target: {
            android: {
                header_libs: ["libc_headers_arch"],
                export_header_lib_headers: ["libc_headers_arch"],
            },
            linux_bionic: {
                header_libs: ["libc_headers_arch"],
                export_header_lib_headers: ["libc_headers_arch"],
            },
        },
        // omitted props
    }

to:

    cc_library_headers(
        name = "libc_headers",
        deps = [] + select({
            "//build/bazel/platforms/os:android": [
                ":libc_headers_arch",
            ],
            "//build/bazel/platforms/os:linux_bionic": [
                ":libc_headers_arch",
            ],
            "//conditions:default": [],
        }),
    )

Test: TH
Test: Verify generated //bionic/libc:libc_headers
Fixes: 183597786

Change-Id: I01016cc2cc9a71449f02300d747f01decebf3f6e
2021-04-02 08:17:34 +00:00

112 lines
3.3 KiB
Go

package bp2build
import (
"android/soong/android"
"android/soong/bazel"
"fmt"
"reflect"
)
// Configurability support for bp2build.
// prettyPrintStringListAttribute converts a StringListAttribute to its Bazel
// syntax. May contain a select statement.
func prettyPrintStringListAttribute(stringList bazel.StringListAttribute, indent int) (string, error) {
ret, err := prettyPrint(reflect.ValueOf(stringList.Value), indent)
if err != nil {
return ret, err
}
if !stringList.HasConfigurableValues() {
// Select statement not needed.
return ret, nil
}
// Create the selects for arch specific values.
selects := map[string]reflect.Value{}
for arch, selectKey := range bazel.PlatformArchMap {
selects[selectKey] = reflect.ValueOf(stringList.GetValueForArch(arch))
}
selectMap, err := prettyPrintSelectMap(selects, "[]", indent)
return ret + selectMap, err
}
// prettyPrintLabelListAttribute converts a LabelListAttribute to its Bazel
// syntax. May contain select statements.
func prettyPrintLabelListAttribute(labels bazel.LabelListAttribute, indent int) (string, error) {
// TODO(b/165114590): convert glob syntax
ret, err := prettyPrint(reflect.ValueOf(labels.Value.Includes), indent)
if err != nil {
return ret, err
}
if !labels.HasConfigurableValues() {
// Select statements not needed.
return ret, nil
}
// Create the selects for arch specific values.
archSelects := map[string]reflect.Value{}
for arch, selectKey := range bazel.PlatformArchMap {
archSelects[selectKey] = reflect.ValueOf(labels.GetValueForArch(arch).Includes)
}
selectMap, err := prettyPrintSelectMap(archSelects, "[]", indent)
if err != nil {
return "", err
}
ret += selectMap
// Create the selects for target os specific values.
osSelects := map[string]reflect.Value{}
for os, selectKey := range bazel.PlatformOsMap {
osSelects[selectKey] = reflect.ValueOf(labels.GetValueForOS(os).Includes)
}
selectMap, err = prettyPrintSelectMap(osSelects, "[]", indent)
return ret + selectMap, err
}
// prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way
// to construct a select map for any kind of attribute type.
func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue string, indent int) (string, error) {
var selects string
for _, selectKey := range android.SortedStringKeys(selectMap) {
value := selectMap[selectKey]
if isZero(value) {
// Ignore zero values to not generate empty lists.
continue
}
s, err := prettyPrintSelectEntry(value, selectKey, indent)
if err != nil {
return "", err
}
selects += s + ",\n"
}
if len(selects) == 0 {
// No conditions (or all values are empty lists), so no need for a map.
return "", nil
}
// Create the map.
ret := " + select({\n"
ret += selects
// default condition comes last.
ret += fmt.Sprintf("%s\"%s\": %s,\n", makeIndent(indent+1), "//conditions:default", defaultValue)
ret += makeIndent(indent)
ret += "})"
return ret, nil
}
// prettyPrintSelectEntry converts a reflect.Value into an entry in a select map
// with a provided key.
func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) {
s := makeIndent(indent + 1)
v, err := prettyPrint(value, indent+1)
if err != nil {
return "", err
}
s += fmt.Sprintf("\"%s\": %s", key, v)
return s, nil
}