bp2build: arch-configurable selects for label list attrs.
This CL adds the configurable LabelListAttribute support to bp2build.
Test: go test
Change-Id: I2ef9e385d9cf1b1845988128eca1d8cda1ecb5e8
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index 90bbd4d..d48d975 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -415,9 +415,34 @@
case reflect.Struct:
// Special cases where the bp2build sends additional information to the codegenerator
// by wrapping the attributes in a custom struct type.
- if labels, ok := propertyValue.Interface().(bazel.LabelList); ok {
+ if labels, ok := propertyValue.Interface().(bazel.LabelListAttribute); ok {
// TODO(b/165114590): convert glob syntax
- return prettyPrint(reflect.ValueOf(labels.Includes), indent)
+ ret, err := prettyPrint(reflect.ValueOf(labels.Value.Includes), indent)
+ if err != nil {
+ return ret, err
+ }
+
+ if !labels.HasArchSpecificValues() {
+ // Select statement not needed.
+ return ret, nil
+ }
+
+ ret += " + " + "select({\n"
+ for _, arch := range android.ArchTypeList() {
+ value := labels.GetValueForArch(arch.Name)
+ if len(value.Includes) > 0 {
+ ret += makeIndent(indent + 1)
+ list, _ := prettyPrint(reflect.ValueOf(value.Includes), indent+1)
+ ret += fmt.Sprintf("\"%s\": %s,\n", platformArchMap[arch], list)
+ }
+ }
+
+ ret += makeIndent(indent + 1)
+ ret += fmt.Sprintf("\"%s\": [],\n", "//conditions:default")
+
+ ret += makeIndent(indent)
+ ret += "})"
+ return ret, err
} else if label, ok := propertyValue.Interface().(bazel.Label); ok {
return fmt.Sprintf("%q", label.Label), nil
} else if stringList, ok := propertyValue.Interface().(bazel.StringListAttribute); ok {
@@ -443,8 +468,7 @@
}
ret += makeIndent(indent + 1)
- list, _ := prettyPrint(reflect.ValueOf(stringList.GetValueForArch("default")), indent+1)
- ret += fmt.Sprintf("\"%s\": %s,\n", "//conditions:default", list)
+ ret += fmt.Sprintf("\"%s\": [],\n", "//conditions:default")
ret += makeIndent(indent)
ret += "})"
diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go
index b9b250a..ad88e97 100644
--- a/bp2build/build_conversion_test.go
+++ b/bp2build/build_conversion_test.go
@@ -502,8 +502,6 @@
expectedBazelTargets: []string{
`filegroup(
name = "fg_foo",
- srcs = [
- ],
)`,
},
},
@@ -1101,8 +1099,8 @@
"out",
],
srcs = [
- "srcs-from-3",
"in1",
+ "srcs-from-3",
],
)`,
description: "genrule applies properties from genrule_defaults transitively",
diff --git a/bp2build/cc_object_conversion_test.go b/bp2build/cc_object_conversion_test.go
index 1c058ba..b007033 100644
--- a/bp2build/cc_object_conversion_test.go
+++ b/bp2build/cc_object_conversion_test.go
@@ -74,8 +74,8 @@
],
srcs = [
"a/b/bar.h",
- "a/b/foo.h",
"a/b/c.c",
+ "a/b/foo.h",
],
)`,
},
@@ -278,9 +278,13 @@
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
+ srcs: ["a.cpp"],
arch: {
x86: {
- cflags: ["-fPIC"],
+ cflags: ["-fPIC"], // string list
+ },
+ arm: {
+ srcs: ["arch/arm/file.S"], // label list
},
},
bazel_module: { bp2build_available: true },
@@ -295,12 +299,19 @@
"@bazel_tools//platforms:x86_32": [
"-fPIC",
],
- "//conditions:default": [
- ],
+ "//conditions:default": [],
}),
local_include_dirs = [
".",
],
+ srcs = [
+ "a.cpp",
+ ] + select({
+ "@bazel_tools//platforms:arm": [
+ "arch/arm/file.S",
+ ],
+ "//conditions:default": [],
+ }),
)`,
},
},
@@ -311,17 +322,22 @@
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
+ srcs: ["base.cpp"],
arch: {
x86: {
+ srcs: ["x86.cpp"],
cflags: ["-fPIC"],
},
x86_64: {
+ srcs: ["x86_64.cpp"],
cflags: ["-fPIC"],
},
arm: {
+ srcs: ["arm.cpp"],
cflags: ["-Wall"],
},
arm64: {
+ srcs: ["arm64.cpp"],
cflags: ["-Wall"],
},
},
@@ -346,12 +362,28 @@
"@bazel_tools//platforms:x86_64": [
"-fPIC",
],
- "//conditions:default": [
- ],
+ "//conditions:default": [],
}),
local_include_dirs = [
".",
],
+ srcs = [
+ "base.cpp",
+ ] + select({
+ "@bazel_tools//platforms:arm": [
+ "arm.cpp",
+ ],
+ "@bazel_tools//platforms:aarch64": [
+ "arm64.cpp",
+ ],
+ "@bazel_tools//platforms:x86_32": [
+ "x86.cpp",
+ ],
+ "@bazel_tools//platforms:x86_64": [
+ "x86_64.cpp",
+ ],
+ "//conditions:default": [],
+ }),
)`,
},
},