bp2build: Add support for export_.*headers props
Soong supports export_.*_headers properties, the libraries contained in
this list must also be within a shared/static/whole_static/header libs
property. For bp2build, we eliminate this duplication. The libraries
not listed in an export_.*_headers property will migrate to an attribute
prepended with implementation_, those in export_.*_headers will not have
a prefix.
Test: build/bazel/ci/bp2build.sh
Test: build/bazel/ci/mixed_libc.sh
Bug: 198241472
Change-Id: I3eb84c983ec5d241c8a568e411dfd5619d3184a7
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index b1a6e2c..4a0eeea 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -635,6 +635,7 @@
// Ignore zero-valued fields
continue
}
+
// if the struct is embedded (anonymous), flatten the properties into the containing struct
if field.Anonymous {
if field.Type.Kind() == reflect.Ptr {
diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go
index b3a1053..ec8882b 100644
--- a/bp2build/cc_library_conversion_test.go
+++ b/bp2build/cc_library_conversion_test.go
@@ -349,21 +349,21 @@
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = ["bothflag"],
- dynamic_deps = [":shared_dep_for_both"],
implementation_deps = [":static_dep_for_both"],
+ implementation_dynamic_deps = [":shared_dep_for_both"],
shared = {
"copts": ["sharedflag"],
- "dynamic_deps": [":shared_dep_for_shared"],
+ "implementation_deps": [":static_dep_for_shared"],
+ "implementation_dynamic_deps": [":shared_dep_for_shared"],
"srcs": ["sharedonly.cpp"],
- "static_deps": [":static_dep_for_shared"],
"whole_archive_deps": [":whole_static_lib_for_shared"],
},
srcs = ["both.cpp"],
static = {
"copts": ["staticflag"],
- "dynamic_deps": [":shared_dep_for_static"],
+ "implementation_deps": [":static_dep_for_static"],
+ "implementation_dynamic_deps": [":shared_dep_for_static"],
"srcs": ["staticonly.cpp"],
- "static_deps": [":static_dep_for_static"],
"whole_archive_deps": [":whole_static_lib_for_static"],
},
whole_archive_deps = [":whole_static_lib_for_both"],
@@ -371,6 +371,105 @@
})
}
+func TestCcLibraryDeps(t *testing.T) {
+ runCcLibraryTestCase(t, bp2buildTestCase{
+ description: "cc_library shared/static props",
+ moduleTypeUnderTest: "cc_library",
+ moduleTypeUnderTestFactory: cc.LibraryFactory,
+ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
+ filesystem: map[string]string{
+ "both.cpp": "",
+ "sharedonly.cpp": "",
+ "staticonly.cpp": "",
+ },
+ blueprint: soongCcLibraryPreamble + `
+cc_library {
+ name: "a",
+ srcs: ["both.cpp"],
+ cflags: ["bothflag"],
+ shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
+ export_shared_lib_headers: ["shared_dep_for_both"],
+ static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
+ export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
+ whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
+ static: {
+ srcs: ["staticonly.cpp"],
+ cflags: ["staticflag"],
+ shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
+ export_shared_lib_headers: ["shared_dep_for_static"],
+ static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
+ export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
+ whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
+ },
+ shared: {
+ srcs: ["sharedonly.cpp"],
+ cflags: ["sharedflag"],
+ shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
+ export_shared_lib_headers: ["shared_dep_for_shared"],
+ static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
+ export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
+ whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
+ },
+ include_build_directory: false,
+}
+` + simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_shared") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_shared") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_static") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_static") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_both") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_both") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_shared") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_static") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_both") +
+ simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
+ simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_shared") +
+ simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_shared") +
+ simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_static") +
+ simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
+ simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
+ simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
+ expectedBazelTargets: []string{`cc_library(
+ name = "a",
+ copts = ["bothflag"],
+ deps = [":static_dep_for_both"],
+ dynamic_deps = [":shared_dep_for_both"],
+ implementation_deps = [":implementation_static_dep_for_both"],
+ implementation_dynamic_deps = [":implementation_shared_dep_for_both"],
+ shared = {
+ "copts": ["sharedflag"],
+ "deps": [":static_dep_for_shared"],
+ "dynamic_deps": [":shared_dep_for_shared"],
+ "implementation_deps": [":implementation_static_dep_for_shared"],
+ "implementation_dynamic_deps": [":implementation_shared_dep_for_shared"],
+ "srcs": ["sharedonly.cpp"],
+ "whole_archive_deps": [
+ ":not_explicitly_exported_whole_static_dep_for_shared",
+ ":whole_static_dep_for_shared",
+ ],
+ },
+ srcs = ["both.cpp"],
+ static = {
+ "copts": ["staticflag"],
+ "deps": [":static_dep_for_static"],
+ "dynamic_deps": [":shared_dep_for_static"],
+ "implementation_deps": [":implementation_static_dep_for_static"],
+ "implementation_dynamic_deps": [":implementation_shared_dep_for_static"],
+ "srcs": ["staticonly.cpp"],
+ "whole_archive_deps": [
+ ":not_explicitly_exported_whole_static_dep_for_static",
+ ":whole_static_dep_for_static",
+ ],
+ },
+ whole_archive_deps = [
+ ":not_explicitly_exported_whole_static_dep_for_both",
+ ":whole_static_dep_for_both",
+ ],
+)`},
+ })
+}
+
func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTest: "cc_library",
@@ -506,7 +605,14 @@
"//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
"//conditions:default": [],
}),
- "dynamic_deps": select({
+ "implementation_deps": [":static_dep_for_shared"] + select({
+ "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
+ "//conditions:default": [],
+ }) + select({
+ "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
+ "//conditions:default": [],
+ }),
+ "implementation_dynamic_deps": select({
"//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
"//conditions:default": [],
}),
@@ -517,13 +623,6 @@
"//build/bazel/platforms/os:android": ["android_shared.cpp"],
"//conditions:default": [],
}),
- "static_deps": [":static_dep_for_shared"] + select({
- "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
- "//conditions:default": [],
- }) + select({
- "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
- "//conditions:default": [],
- }),
"whole_archive_deps": select({
"//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
"//conditions:default": [],
@@ -535,12 +634,12 @@
"//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
"//conditions:default": [],
}),
- "srcs": ["staticonly.cpp"] + select({
- "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
+ "implementation_deps": [":static_dep_for_static"] + select({
+ "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
"//conditions:default": [],
}),
- "static_deps": [":static_dep_for_static"] + select({
- "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
+ "srcs": ["staticonly.cpp"] + select({
+ "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
"//conditions:default": [],
}),
},
@@ -767,7 +866,7 @@
`,
expectedBazelTargets: []string{`cc_library(
name = "a",
- dynamic_deps = [":mylib"],
+ implementation_dynamic_deps = [":mylib"],
)`},
})
}
@@ -1013,13 +1112,6 @@
expectedBazelTargets: []string{
`cc_library(
name = "foo_static",
- dynamic_deps = select({
- "//build/bazel/platforms/arch:arm": [],
- "//conditions:default": [":arm_shared_lib_excludes"],
- }) + select({
- "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
- "//conditions:default": [],
- }),
implementation_deps = select({
"//build/bazel/platforms/arch:arm": [],
"//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
@@ -1027,6 +1119,13 @@
"//build/bazel/product_variables:malloc_not_svelte": [],
"//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
}),
+ implementation_dynamic_deps = select({
+ "//build/bazel/platforms/arch:arm": [],
+ "//conditions:default": [":arm_shared_lib_excludes"],
+ }) + select({
+ "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
+ "//conditions:default": [],
+ }),
srcs_c = ["common.c"],
whole_archive_deps = select({
"//build/bazel/platforms/arch:arm": [],
diff --git a/bp2build/cc_library_headers_conversion_test.go b/bp2build/cc_library_headers_conversion_test.go
index 37d806c..e43672b 100644
--- a/bp2build/cc_library_headers_conversion_test.go
+++ b/bp2build/cc_library_headers_conversion_test.go
@@ -224,7 +224,10 @@
cc_library_headers {
name: "foo_headers",
target: {
- android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
+ android: {
+ header_libs: ["android-lib", "exported-lib"],
+ export_header_lib_headers: ["exported-lib"]
+ },
},
include_build_directory: false,
}`,
diff --git a/bp2build/cc_library_shared_conversion_test.go b/bp2build/cc_library_shared_conversion_test.go
index 52a07cc..3dcfbd7 100644
--- a/bp2build/cc_library_shared_conversion_test.go
+++ b/bp2build/cc_library_shared_conversion_test.go
@@ -149,10 +149,6 @@
"-Dflag1",
"-Dflag2",
],
- dynamic_deps = [
- ":shared_lib_1",
- ":shared_lib_2",
- ],
export_includes = [
"export_include_dir_1",
"export_include_dir_2",
@@ -161,6 +157,10 @@
":header_lib_1",
":header_lib_2",
],
+ implementation_dynamic_deps = [
+ ":shared_lib_1",
+ ":shared_lib_2",
+ ],
local_includes = [
"local_include_dir_1",
"local_include_dir_2",
@@ -201,7 +201,7 @@
}`,
expectedBazelTargets: []string{`cc_library_shared(
name = "foo_shared",
- dynamic_deps = select({
+ implementation_dynamic_deps = select({
"//build/bazel/platforms/arch:arm64": [":shared_dep"],
"//conditions:default": [],
}),
@@ -232,7 +232,7 @@
}`,
expectedBazelTargets: []string{`cc_library_shared(
name = "foo_shared",
- dynamic_deps = select({
+ implementation_dynamic_deps = select({
"//build/bazel/platforms/os:android": [":shared_dep"],
"//conditions:default": [],
}),
@@ -269,7 +269,7 @@
}`,
expectedBazelTargets: []string{`cc_library_shared(
name = "foo_shared",
- dynamic_deps = [":shared_dep"] + select({
+ implementation_dynamic_deps = [":shared_dep"] + select({
"//build/bazel/platforms/arch:arm64": [":shared_dep3"],
"//conditions:default": [],
}) + select({
diff --git a/bp2build/conversion.go b/bp2build/conversion.go
index 354abf6..0a86a79 100644
--- a/bp2build/conversion.go
+++ b/bp2build/conversion.go
@@ -142,7 +142,7 @@
}
func shouldSkipStructField(field reflect.StructField) bool {
- if field.PkgPath != "" {
+ if field.PkgPath != "" && !field.Anonymous {
// Skip unexported fields. Some properties are
// internal to Soong only, and these fields do not have PkgPath.
return true
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 74084b1..1e7e53c 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -20,6 +20,7 @@
*/
import (
+ "fmt"
"strings"
"testing"
@@ -358,3 +359,11 @@
ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
ctx.RegisterForBazelConversion()
}
+
+func simpleModuleDoNotConvertBp2build(typ, name string) string {
+ return fmt.Sprintf(`
+%s {
+ name: "%s",
+ bazel_module: { bp2build_available: false },
+}`, typ, name)
+}