diff options
| -rw-r--r-- | android/apex.go | 11 | ||||
| -rw-r--r-- | android/bazel.go | 27 | ||||
| -rw-r--r-- | android/mutator.go | 7 | ||||
| -rw-r--r-- | android/proto.go | 61 | ||||
| -rw-r--r-- | apex/apex.go | 6 | ||||
| -rw-r--r-- | bazel/properties.go | 74 | ||||
| -rw-r--r-- | bazel/properties_test.go | 10 | ||||
| -rw-r--r-- | bp2build/cc_binary_conversion_test.go | 60 | ||||
| -rw-r--r-- | bp2build/cc_library_conversion_test.go | 954 | ||||
| -rw-r--r-- | bp2build/cc_library_shared_conversion_test.go | 25 | ||||
| -rw-r--r-- | bp2build/cc_library_static_conversion_test.go | 24 | ||||
| -rw-r--r-- | cc/binary.go | 10 | ||||
| -rw-r--r-- | cc/bp2build.go | 95 | ||||
| -rw-r--r-- | cc/cc.go | 10 | ||||
| -rw-r--r-- | cc/library.go | 136 | ||||
| -rw-r--r-- | cc/library_test.go | 7 | ||||
| -rw-r--r-- | cc/proto.go | 76 | ||||
| -rw-r--r-- | cc/sanitize.go | 4 | ||||
| -rwxr-xr-x | java/app.go | 2 | ||||
| -rw-r--r-- | rust/config/global.go | 2 |
20 files changed, 1118 insertions, 483 deletions
diff --git a/android/apex.go b/android/apex.go index 461faf760..d5fd92257 100644 --- a/android/apex.go +++ b/android/apex.go @@ -908,16 +908,17 @@ var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel { // // Return true if the `to` module should be visited, false otherwise. type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool +type WalkPayloadDepsFunc func(ctx ModuleContext, do PayloadDepsCallback) -// UpdatableModule represents updatable APEX/APK -type UpdatableModule interface { +// ModuleWithMinSdkVersionCheck represents a module that implements min_sdk_version checks +type ModuleWithMinSdkVersionCheck interface { Module - WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback) + CheckMinSdkVersion(ctx ModuleContext) } // CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version // accordingly -func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiLevel) { +func CheckMinSdkVersion(ctx ModuleContext, minSdkVersion ApiLevel, walk WalkPayloadDepsFunc) { // do not enforce min_sdk_version for host if ctx.Host() { return @@ -933,7 +934,7 @@ func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiL return } - m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool { + walk(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool { if externalDep { // external deps are outside the payload boundary, which is "stable" // interface. We don't have to check min_sdk_version for external diff --git a/android/bazel.go b/android/bazel.go index 61469dc4a..c3efb0aa0 100644 --- a/android/bazel.go +++ b/android/bazel.go @@ -338,9 +338,8 @@ var ( "host_bionic_linker_asm", // depends on extract_linker, a go binary. "host_bionic_linker_script", // depends on extract_linker, a go binary. - "pbtombstone", // depends on libprotobuf-cpp-lite, libtombstone_proto - "crash_dump", // depends on unconverted module libprotobuf-cpp-lite - "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610. + "pbtombstone", // depends on libprotobuf-cpp-lite, libtombstone_proto + "crash_dump", // depends on unconverted module libprotobuf-cpp-lite "libunwindstack_local", "libunwindstack_utils", // depends on unconverted module libunwindstack "libunwindstack", // depends on libdexfile_support, of unsupported module type art_cc_library_static @@ -375,19 +374,10 @@ var ( // APEX support "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug' - "libadb_crypto", // Depends on libadb_protos - "libadb_crypto_static", // Depends on libadb_protos_static - "libadb_pairing_connection", // Depends on libadb_protos - "libadb_pairing_connection_static", // Depends on libadb_protos_static - "libadb_pairing_server", // Depends on libadb_protos - "libadb_pairing_server_static", // Depends on libadb_protos_static - "libadbd", // Depends on libadbd_core - "libadbd_core", // Depends on libadb_protos - "libadbd_services", // Depends on libadb_protos + "libadbd_core", // http://b/208481704: requijres use_version_lib + "libadbd_services", // http://b/208481704: requires use_version_lib - "libadb_protos_static", // b/200601772: Requires cc_library proto support - "libadb_protos", // b/200601772: Requires cc_library proto support - "libapp_processes_protos_lite", // b/200601772: Requires cc_library proto support + "libadbd", // depends on unconverted modules: libadbd_core, libadbd_services "libgtest_ndk_c++", // b/201816222: Requires sdk_version support. "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support. @@ -420,6 +410,13 @@ var ( "cap_names.h", // TODO(b/204913827) runfiles need to be handled in mixed builds "libcap", // TODO(b/204913827) runfiles need to be handled in mixed builds "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610. + + // Depends on libprotobuf-cpp-* + "libadb_crypto", "libadb_crypto_static", "libadb_pairing_connection", + "libadb_pairing_connection_static", + "libadb_pairing_server", "libadb_pairing_server_static", + "libadb_protos_static", "libadb_protos", + "libapp_processes_protos_lite", } // Used for quicker lookups diff --git a/android/mutator.go b/android/mutator.go index 0a9351745..bf1cf806e 100644 --- a/android/mutator.go +++ b/android/mutator.go @@ -221,6 +221,13 @@ var bp2buildMutators = map[string]RegisterMutatorFunc{} // See http://b/192523357 var bp2buildLock sync.Mutex +// A minimal context for Bp2build conversion +type Bp2buildMutatorContext interface { + BazelConversionPathContext + + CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}) +} + // RegisterBp2BuildMutator registers specially crafted mutators for // converting Blueprint/Android modules into special modules that can // be code-generated into Bazel BUILD targets. diff --git a/android/proto.go b/android/proto.go index 0be7893c8..64d4d057c 100644 --- a/android/proto.go +++ b/android/proto.go @@ -15,12 +15,17 @@ package android import ( + "android/soong/bazel" "strings" "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) +const ( + canonicalPathFromRootDefault = true +) + // TODO(ccross): protos are often used to communicate between multiple modules. If the only // way to convert a proto to source is to reference it as a source file, and external modules cannot // reference source files in other modules, then every module that owns a proto file will need to @@ -90,7 +95,7 @@ func GetProtoFlags(ctx ModuleContext, p *ProtoProperties) ProtoFlags { Flags: flags, Deps: deps, OutTypeFlag: protoOutFlag, - CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, true), + CanonicalPathFromRoot: proptools.BoolDefault(p.Proto.Canonical_path_from_root, canonicalPathFromRootDefault), Dir: PathForModuleGen(ctx, "proto"), SubDir: PathForModuleGen(ctx, "proto", ctx.ModuleDir()), } @@ -146,3 +151,57 @@ func ProtoRule(rule *RuleBuilder, protoFile Path, flags ProtoFlags, deps Paths, rule.Command(). BuiltTool("dep_fixer").Flag(depFile.String()) } + +// Bp2buildProtoInfo contains information necessary to pass on to language specific conversion. +type Bp2buildProtoInfo struct { + Type *string + Name string +} + +type protoAttrs struct { + Srcs bazel.LabelListAttribute + Strip_import_prefix *string +} + +// Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the +// information necessary for language-specific handling. +func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, module Module, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) { + var info Bp2buildProtoInfo + if srcs.IsEmpty() { + return info, false + } + m := module.base() + + info.Name = m.Name() + "_proto" + attrs := protoAttrs{ + Srcs: srcs, + } + + for axis, configToProps := range m.GetArchVariantProperties(ctx, &ProtoProperties{}) { + for _, rawProps := range configToProps { + var props *ProtoProperties + var ok bool + if props, ok = rawProps.(*ProtoProperties); !ok { + ctx.ModuleErrorf("Could not cast ProtoProperties to expected type") + } + if axis == bazel.NoConfigAxis { + info.Type = props.Proto.Type + + if proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) { + // an empty string indicates to strips the package path + path := "" + attrs.Strip_import_prefix = &path + } + } else if props.Proto.Type != info.Type && props.Proto.Type != nil { + ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.") + } + } + } + + ctx.CreateBazelTargetModule( + bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, + CommonAttributes{Name: info.Name}, + &attrs) + + return info, true +} diff --git a/apex/apex.go b/apex/apex.go index 25b1568c2..2ca26a284 100644 --- a/apex/apex.go +++ b/apex/apex.go @@ -1681,7 +1681,7 @@ func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) { // 1) do some validity checks such as apex_available, min_sdk_version, etc. a.checkApexAvailability(ctx) a.checkUpdatable(ctx) - a.checkMinSdkVersion(ctx) + a.CheckMinSdkVersion(ctx) a.checkStaticLinkingToStubLibraries(ctx) a.checkStaticExecutables(ctx) if len(a.properties.Tests) > 0 && !a.testApex { @@ -2314,13 +2314,13 @@ func overrideApexFactory() android.Module { // Entures that min_sdk_version of the included modules are equal or less than the min_sdk_version // of this apexBundle. -func (a *apexBundle) checkMinSdkVersion(ctx android.ModuleContext) { +func (a *apexBundle) CheckMinSdkVersion(ctx android.ModuleContext) { if a.testApex || a.vndkApex { return } // apexBundle::minSdkVersion reports its own errors. minSdkVersion := a.minSdkVersion(ctx) - android.CheckMinSdkVersion(a, ctx, minSdkVersion) + android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps) } func (a *apexBundle) minSdkVersion(ctx android.BaseModuleContext) android.ApiLevel { diff --git a/bazel/properties.go b/bazel/properties.go index b370bbfb2..76be0581b 100644 --- a/bazel/properties.go +++ b/bazel/properties.go @@ -107,6 +107,14 @@ func (ll *LabelList) uniqueParentDirectories() []string { return dirs } +// Add inserts the label Label at the end of the LabelList. +func (ll *LabelList) Add(label *Label) { + if label == nil { + return + } + ll.Includes = append(ll.Includes, *label) +} + // Append appends the fields of other labelList to the corresponding fields of ll. func (ll *LabelList) Append(other LabelList) { if len(ll.Includes) > 0 || len(other.Includes) > 0 { @@ -366,9 +374,23 @@ func (ba *BoolAttribute) SortedConfigurationAxes() []ConfigurationAxis { // labelListSelectValues supports config-specific label_list typed Bazel attribute values. type labelListSelectValues map[string]LabelList -func (ll labelListSelectValues) appendSelects(other labelListSelectValues) { +func (ll labelListSelectValues) addSelects(label labelSelectValues) { + for k, v := range label { + if label == nil { + continue + } + l := ll[k] + (&l).Add(v) + ll[k] = l + } +} + +func (ll labelListSelectValues) appendSelects(other labelListSelectValues, forceSpecifyEmptyList bool) { for k, v := range other { l := ll[k] + if forceSpecifyEmptyList && l.IsNil() && !v.IsNil() { + l.Includes = []Label{} + } (&l).Append(v) ll[k] = l } @@ -424,17 +446,22 @@ func (cll configurableLabelLists) setValueForAxis(axis ConfigurationAxis, config cll[axis][config] = list } -func (cll configurableLabelLists) Append(other configurableLabelLists) { +func (cll configurableLabelLists) Append(other configurableLabelLists, forceSpecifyEmptyList bool) { for axis, otherSelects := range other { selects := cll[axis] if selects == nil { selects = make(labelListSelectValues, len(otherSelects)) } - selects.appendSelects(otherSelects) + selects.appendSelects(otherSelects, forceSpecifyEmptyList) cll[axis] = selects } } +func (lla *LabelListAttribute) Clone() *LabelListAttribute { + result := &LabelListAttribute{ForceSpecifyEmptyList: lla.ForceSpecifyEmptyList} + return result.Append(*lla) +} + // MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value. func MakeLabelListAttribute(value LabelList) LabelListAttribute { return LabelListAttribute{ @@ -488,16 +515,37 @@ func (lla *LabelListAttribute) SortedConfigurationAxes() []ConfigurationAxis { } // Append all values, including os and arch specific ones, from another -// LabelListAttribute to this LabelListAttribute. -func (lla *LabelListAttribute) Append(other LabelListAttribute) { - if lla.ForceSpecifyEmptyList && !other.Value.IsNil() { +// LabelListAttribute to this LabelListAttribute. Returns this LabelListAttribute. +func (lla *LabelListAttribute) Append(other LabelListAttribute) *LabelListAttribute { + forceSpecifyEmptyList := lla.ForceSpecifyEmptyList || other.ForceSpecifyEmptyList + if forceSpecifyEmptyList && lla.Value.IsNil() && !other.Value.IsNil() { lla.Value.Includes = []Label{} } lla.Value.Append(other.Value) if lla.ConfigurableValues == nil { lla.ConfigurableValues = make(configurableLabelLists) } - lla.ConfigurableValues.Append(other.ConfigurableValues) + lla.ConfigurableValues.Append(other.ConfigurableValues, forceSpecifyEmptyList) + return lla +} + +// Add inserts the labels for each axis of LabelAttribute at the end of corresponding axis's +// LabelList within the LabelListAttribute +func (lla *LabelListAttribute) Add(label *LabelAttribute) { + if label == nil { + return + } + + lla.Value.Add(label.Value) + if lla.ConfigurableValues == nil && label.ConfigurableValues != nil { + lla.ConfigurableValues = make(configurableLabelLists) + } + for axis, _ := range label.ConfigurableValues { + if _, exists := lla.ConfigurableValues[axis]; !exists { + lla.ConfigurableValues[axis] = make(labelListSelectValues) + } + lla.ConfigurableValues[axis].addSelects(label.ConfigurableValues[axis]) + } } // HasConfigurableValues returns true if the attribute contains axis-specific label list values. @@ -566,7 +614,7 @@ type OtherModuleContext interface { // LabelMapper is a function that takes a OtherModuleContext and returns a (potentially changed) // label and whether it was changed. -type LabelMapper func(OtherModuleContext, string) (string, bool) +type LabelMapper func(OtherModuleContext, Label) (string, bool) // LabelPartition contains descriptions of a partition for labels type LabelPartition struct { @@ -588,7 +636,7 @@ type LabelPartitions map[string]LabelPartition // not. func (lf LabelPartition) filter(ctx OtherModuleContext, label Label) *Label { if lf.LabelMapper != nil { - if newLabel, changed := lf.LabelMapper(ctx, label.Label); changed { + if newLabel, changed := lf.LabelMapper(ctx, label); changed { return &Label{newLabel, label.OriginalModuleName} } } @@ -757,12 +805,18 @@ func (sla StringListAttribute) HasConfigurableValues() bool { // Append appends all values, including os and arch specific ones, from another // StringListAttribute to this StringListAttribute -func (sla *StringListAttribute) Append(other StringListAttribute) { +func (sla *StringListAttribute) Append(other StringListAttribute) *StringListAttribute { sla.Value = append(sla.Value, other.Value...) if sla.ConfigurableValues == nil { sla.ConfigurableValues = make(configurableStringLists) } sla.ConfigurableValues.Append(other.ConfigurableValues) + return sla +} + +func (sla *StringListAttribute) Clone() *StringListAttribute { + result := &StringListAttribute{} + return result.Append(*sla) } // SetSelectValue set a value for a bazel select for the given axis, config and value. diff --git a/bazel/properties_test.go b/bazel/properties_test.go index 7a7d6f3b8..c7f977678 100644 --- a/bazel/properties_test.go +++ b/bazel/properties_test.go @@ -313,16 +313,16 @@ func TestResolveExcludes(t *testing.T) { // labelAddSuffixForTypeMapper returns a LabelMapper that adds suffix to label name for modules of // typ func labelAddSuffixForTypeMapper(suffix, typ string) LabelMapper { - return func(omc OtherModuleContext, label string) (string, bool) { - m, ok := omc.ModuleFromName(label) + return func(omc OtherModuleContext, label Label) (string, bool) { + m, ok := omc.ModuleFromName(label.Label) if !ok { - return label, false + return label.Label, false } mTyp := omc.OtherModuleType(m) if typ == mTyp { - return label + suffix, true + return label.Label + suffix, true } - return label, false + return label.Label, false } } diff --git a/bp2build/cc_binary_conversion_test.go b/bp2build/cc_binary_conversion_test.go index f9abcba6f..24468506f 100644 --- a/bp2build/cc_binary_conversion_test.go +++ b/bp2build/cc_binary_conversion_test.go @@ -24,8 +24,7 @@ import ( ) const ( - ccBinaryTypePlaceHolder = "{rule_name}" - compatibleWithPlaceHolder = "{target_compatible_with}" + ccBinaryTypePlaceHolder = "{rule_name}" ) type testBazelTarget struct { @@ -84,12 +83,15 @@ func runCcBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) { func runCcHostBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) { t.Helper() testCase := tc - for i, t := range testCase.targets { - t.attrs["target_compatible_with"] = `select({ + for i, tar := range testCase.targets { + if tar.typ != "cc_binary" { + continue + } + tar.attrs["target_compatible_with"] = `select({ "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], "//conditions:default": [], })` - testCase.targets[i] = t + testCase.targets[i] = tar } moduleTypeUnderTest := "cc_binary_host" t.Run(testCase.description, func(t *testing.T) { @@ -448,3 +450,51 @@ func TestCcBinaryPropertiesToFeatures(t *testing.T) { }) } } + +func TestCcBinarySharedProto(t *testing.T) { + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ + blueprint: soongCcProtoLibraries + `{rule_name} { + name: "foo", + srcs: ["foo.proto"], + proto: { + canonical_path_from_root: false, + }, + include_build_directory: false, +}`, + targets: []testBazelTarget{ + {"proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }}, {"cc_binary", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + "whole_archive_deps": `[":foo_cc_proto_lite"]`, + }}, + }, + }) +} + +func TestCcBinaryStaticProto(t *testing.T) { + runCcBinaryTests(t, ccBinaryBp2buildTestCase{ + blueprint: soongCcProtoLibraries + `{rule_name} { + name: "foo", + srcs: ["foo.proto"], + static_executable: true, + proto: { + canonical_path_from_root: false, + }, + include_build_directory: false, +}`, + targets: []testBazelTarget{ + {"proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }}, {"cc_binary", "foo", attrNameToString{ + "deps": `[":libprotobuf-cpp-lite"]`, + "whole_archive_deps": `[":foo_cc_proto_lite"]`, + "linkshared": `False`, + }}, + }, + }) +} diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index d23ea01ed..dcbe326e9 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -39,6 +39,19 @@ toolchain_library { native_bridge_supported: true, src: "", }` + + soongCcProtoLibraries = ` +cc_library { + name: "libprotobuf-cpp-lite", + bazel_module: { bp2build_available: false }, +} + +cc_library { + name: "libprotobuf-cpp-full", + bazel_module: { bp2build_available: false }, +}` + + soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries ) func runCcLibraryTestCase(t *testing.T, tc bp2buildTestCase) { @@ -117,17 +130,16 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo-lib", attrNameToString{ - "copts": `["-Wall"]`, - "export_includes": `["foo-dir"]`, - "implementation_deps": `[":some-headers"]`, - "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({ + expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ + "copts": `["-Wall"]`, + "export_includes": `["foo-dir"]`, + "implementation_deps": `[":some-headers"]`, + "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({ "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"], "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"], "//conditions:default": [], })`, - "srcs": `["impl.cpp"] + select({ + "srcs": `["impl.cpp"] + select({ "//build/bazel/platforms/arch:x86": ["x86.cpp"], "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], "//conditions:default": [], @@ -141,8 +153,7 @@ cc_library { "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"], "//conditions:default": [], })`, - }), - }, + }), }) } @@ -190,17 +201,16 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "fake-ld-android", attrNameToString{ - "srcs": `["ld_android.cpp"]`, - "copts": `[ + expectedBazelTargets: makeCcLibraryTargets("fake-ld-android", attrNameToString{ + "srcs": `["ld_android.cpp"]`, + "copts": `[ "-Wall", "-Wextra", "-Wunused", "-Werror", ]`, - "implementation_deps": `[":libc_headers"]`, - "linkopts": `[ + "implementation_deps": `[":libc_headers"]`, + "linkopts": `[ "-Wl,--exclude-libs=libgcc.a", "-Wl,--exclude-libs=libgcc_stripped.a", "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a", @@ -212,8 +222,7 @@ cc_library { "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"], "//conditions:default": [], })`, - }), - }, + }), }) } @@ -258,16 +267,14 @@ cc_library { `, }, blueprint: soongCcLibraryPreamble, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "fake-libarm-optimized-routines-math", attrNameToString{ - "copts": `select({ + expectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", attrNameToString{ + "copts": `select({ "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"], "//conditions:default": [], })`, - "local_includes": `["."]`, - "srcs_c": `["math/cosf.c"]`, - }), - }, + "local_includes": `["."]`, + "srcs_c": `["math/cosf.c"]`, + }), }) } @@ -353,26 +360,48 @@ cc_library { } `, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "copts": `["bothflag"]`, - "implementation_deps": `[":static_dep_for_both"]`, - "implementation_dynamic_deps": `[":shared_dep_for_both"]`, - "shared": `{ - "copts": ["sharedflag"], - "implementation_deps": [":static_dep_for_shared"], - "implementation_dynamic_deps": [":shared_dep_for_shared"], - "srcs": ["sharedonly.cpp"], - "whole_archive_deps": [":whole_static_lib_for_shared"], - }`, - "srcs": `["both.cpp"]`, - "static": `{ - "copts": ["staticflag"], - "implementation_deps": [":static_dep_for_static"], - "implementation_dynamic_deps": [":shared_dep_for_static"], - "srcs": ["staticonly.cpp"], - "whole_archive_deps": [":whole_static_lib_for_static"], - }`, - "whole_archive_deps": `[":whole_static_lib_for_both"]`, + makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{ + "copts": `[ + "bothflag", + "staticflag", + ]`, + "implementation_deps": `[ + ":static_dep_for_both", + ":static_dep_for_static", + ]`, + "implementation_dynamic_deps": `[ + ":shared_dep_for_both", + ":shared_dep_for_static", + ]`, + "srcs": `[ + "both.cpp", + "staticonly.cpp", + ]`, + "whole_archive_deps": `[ + ":whole_static_lib_for_both", + ":whole_static_lib_for_static", + ]`}), + makeBazelTarget("cc_library_shared", "a", attrNameToString{ + "copts": `[ + "bothflag", + "sharedflag", + ]`, + "implementation_deps": `[ + ":static_dep_for_both", + ":static_dep_for_shared", + ]`, + "implementation_dynamic_deps": `[ + ":shared_dep_for_both", + ":shared_dep_for_shared", + ]`, + "srcs": `[ + "both.cpp", + "sharedonly.cpp", + ]`, + "whole_archive_deps": `[ + ":whole_static_lib_for_both", + ":whole_static_lib_for_shared", + ]`, }), }, }) @@ -438,44 +467,72 @@ cc_library { simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") + simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"), expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "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", - ], - }`, + makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{ + "copts": `[ + "bothflag", + "staticflag", + ]`, + "deps": `[ + ":static_dep_for_both", + ":static_dep_for_static", + ]`, + "dynamic_deps": `[ + ":shared_dep_for_both", + ":shared_dep_for_static", + ]`, + "implementation_deps": `[ + ":implementation_static_dep_for_both", + ":implementation_static_dep_for_static", + ]`, + "implementation_dynamic_deps": `[ + ":implementation_shared_dep_for_both", + ":implementation_shared_dep_for_static", + ]`, + "srcs": `[ + "both.cpp", + "staticonly.cpp", + ]`, "whole_archive_deps": `[ ":not_explicitly_exported_whole_static_dep_for_both", ":whole_static_dep_for_both", + ":not_explicitly_exported_whole_static_dep_for_static", + ":whole_static_dep_for_static", ]`, }), - }, - }) + makeBazelTarget("cc_library_shared", "a", attrNameToString{ + "copts": `[ + "bothflag", + "sharedflag", + ]`, + "deps": `[ + ":static_dep_for_both", + ":static_dep_for_shared", + ]`, + "dynamic_deps": `[ + ":shared_dep_for_both", + ":shared_dep_for_shared", + ]`, + "implementation_deps": `[ + ":implementation_static_dep_for_both", + ":implementation_static_dep_for_shared", + ]`, + "implementation_dynamic_deps": `[ + ":implementation_shared_dep_for_both", + ":implementation_shared_dep_for_shared", + ]`, + "srcs": `[ + "both.cpp", + "sharedonly.cpp", + ]`, + "whole_archive_deps": `[ + ":not_explicitly_exported_whole_static_dep_for_both", + ":whole_static_dep_for_both", + ":not_explicitly_exported_whole_static_dep_for_shared", + ":whole_static_dep_for_shared", + ]`, + })}, + }, + ) } func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) { @@ -508,17 +565,21 @@ cc_prebuilt_library_static { name: "whole_static_lib_for_both" } }, blueprint: soongCcLibraryPreamble, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "shared": `{ - "whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"], - }`, - "static": `{ - "whole_archive_deps": [":whole_static_lib_for_static_alwayslink"], - }`, - "whole_archive_deps": `[":whole_static_lib_for_both_alwayslink"]`, + makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{ + "whole_archive_deps": `[ + ":whole_static_lib_for_both_alwayslink", + ":whole_static_lib_for_static_alwayslink", + ]`, + }), + makeBazelTarget("cc_library_shared", "a", attrNameToString{ + "whole_archive_deps": `[ + ":whole_static_lib_for_both_alwayslink", + ":whole_static_lib_for_shared_alwayslink", + ]`, }), }, - }) + }, + ) } func TestCcLibrarySharedStaticPropsInArch(t *testing.T) { @@ -599,62 +660,77 @@ cc_library_static { name: "android_dep_for_shared" } }, blueprint: soongCcLibraryPreamble, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "copts": `["bothflag"]`, - "implementation_deps": `[":static_dep_for_both"]`, - "local_includes": `["."]`, - "shared": `{ - "copts": ["sharedflag"] + select({ - "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"], - "//conditions:default": [], - }) + select({ - "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"], - "//conditions:default": [], - }) + select({ - "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"], - "//conditions:default": [], - }), - "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": [], - }), - "srcs": ["sharedonly.cpp"] + select({ - "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"], - "//conditions:default": [], - }) + select({ - "//build/bazel/platforms/os:android": ["android_shared.cpp"], - "//conditions:default": [], - }), - "whole_archive_deps": select({ - "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"], - "//conditions:default": [], - }), - }`, - "srcs": `["both.cpp"]`, - "static": `{ - "copts": ["staticflag"] + select({ - "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"], - "//conditions:default": [], - }), - "implementation_deps": [":static_dep_for_static"] + select({ - "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"], - "//conditions:default": [], - }), - "srcs": ["staticonly.cpp"] + select({ - "//build/bazel/platforms/arch:x86": ["x86_static.cpp"], - "//conditions:default": [], - }), - }`, + makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{ + "copts": `[ + "bothflag", + "staticflag", + ] + select({ + "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"], + "//conditions:default": [], + })`, + "implementation_deps": `[ + ":static_dep_for_both", + ":static_dep_for_static", + ] + select({ + "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"], + "//conditions:default": [], + })`, + "local_includes": `["."]`, + "srcs": `[ + "both.cpp", + "staticonly.cpp", + ] + select({ + "//build/bazel/platforms/arch:x86": ["x86_static.cpp"], + "//conditions:default": [], + })`, + }), + makeBazelTarget("cc_library_shared", "a", attrNameToString{ + "copts": `[ + "bothflag", + "sharedflag", + ] + select({ + "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"], + "//conditions:default": [], + }) + select({ + "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"], + "//conditions:default": [], + }) + select({ + "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"], + "//conditions:default": [], + })`, + "implementation_deps": `[ + ":static_dep_for_both", + ":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": [], + })`, + "local_includes": `["."]`, + "srcs": `[ + "both.cpp", + "sharedonly.cpp", + ] + select({ + "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"], + "//conditions:default": [], + }) + select({ + "//build/bazel/platforms/os:android": ["android_shared.cpp"], + "//conditions:default": [], + })`, + "whole_archive_deps": `select({ + "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"], + "//conditions:default": [], + })`, }), }, - }) + }, + ) } func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) { @@ -738,57 +814,56 @@ filegroup { }, blueprint: soongCcLibraryPreamble, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ + makeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", attrNameToString{ "local_includes": `["."]`, - "shared": `{ - "srcs": [ - "shared_source.cpp", - "shared_source.cc", - ":shared_filegroup_cpp_srcs", - ], - "srcs_as": [ - "shared_source.s", - "shared_source.S", - ":shared_filegroup_as_srcs", - ], - "srcs_c": [ - "shared_source.c", - ":shared_filegroup_c_srcs", - ], - }`, "srcs": `[ "both_source.cpp", "both_source.cc", ":both_filegroup_cpp_srcs", + "static_source.cpp", + "static_source.cc", + ":static_filegroup_cpp_srcs", ]`, "srcs_as": `[ "both_source.s", "both_source.S", ":both_filegroup_as_srcs", + "static_source.s", + "static_source.S", + ":static_filegroup_as_srcs", ]`, "srcs_c": `[ "both_source.c", ":both_filegroup_c_srcs", + "static_source.c", + ":static_filegroup_c_srcs", ]`, - "static": `{ - "srcs": [ - "static_source.cpp", - "static_source.cc", - ":static_filegroup_cpp_srcs", - ], - "srcs_as": [ - "static_source.s", - "static_source.S", - ":static_filegroup_as_srcs", - ], - "srcs_c": [ - "static_source.c", - ":static_filegroup_c_srcs", - ], - }`, }), - }, - }) + makeBazelTarget("cc_library_shared", "a", attrNameToString{ + "local_includes": `["."]`, + "srcs": `[ + "both_source.cpp", + "both_source.cc", + ":both_filegroup_cpp_srcs", + "shared_source.cpp", + "shared_source.cc", + ":shared_filegroup_cpp_srcs", + ]`, + "srcs_as": `[ + "both_source.s", + "both_source.S", + ":both_filegroup_as_srcs", + "shared_source.s", + "shared_source.S", + ":shared_filegroup_as_srcs", + ]`, + "srcs_c": `[ + "both_source.c", + ":both_filegroup_c_srcs", + "shared_source.c", + ":shared_filegroup_c_srcs", + ]`, + })}}) } func TestCcLibraryNonConfiguredVersionScript(t *testing.T) { @@ -810,14 +885,13 @@ cc_library { `, }, blueprint: soongCcLibraryPreamble, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "additional_linker_inputs": `["v.map"]`, - "linkopts": `["-Wl,--version-script,$(location v.map)"]`, - "srcs": `["a.cpp"]`, - }), - }, - }) + expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{ + "additional_linker_inputs": `["v.map"]`, + "linkopts": `["-Wl,--version-script,$(location v.map)"]`, + "srcs": `["a.cpp"]`, + }), + }, + ) } func TestCcLibraryConfiguredVersionScript(t *testing.T) { @@ -847,22 +921,21 @@ cc_library { `, }, blueprint: soongCcLibraryPreamble, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "additional_linker_inputs": `select({ + expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{ + "additional_linker_inputs": `select({ "//build/bazel/platforms/arch:arm": ["arm.map"], "//build/bazel/platforms/arch:arm64": ["arm64.map"], "//conditions:default": [], })`, - "linkopts": `select({ + "linkopts": `select({ "//build/bazel/platforms/arch:arm": ["-Wl,--version-script,$(location arm.map)"], "//build/bazel/platforms/arch:arm64": ["-Wl,--version-script,$(location arm64.map)"], "//conditions:default": [], })`, - "srcs": `["a.cpp"]`, - }), - }, - }) + "srcs": `["a.cpp"]`, + }), + }, + ) } func TestCcLibrarySharedLibs(t *testing.T) { @@ -883,15 +956,43 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "implementation_dynamic_deps": `[":mylib"]`, - }), - }, - }) + expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{ + "implementation_dynamic_deps": `[":mylib"]`, + }), + }, + ) } func TestCcLibraryFeatures(t *testing.T) { + expected_targets := []string{} + expected_targets = append(expected_targets, makeCcLibraryTargets("a", attrNameToString{ + "features": `[ + "disable_pack_relocations", + "-no_undefined_symbols", + ]`, + "srcs": `["a.cpp"]`, + })...) + expected_targets = append(expected_targets, makeCcLibraryTargets("b", attrNameToString{ + "features": `select({ + "//build/bazel/platforms/arch:x86_64": [ + "disable_pack_relocations", + "-no_undefined_symbols", + ], + "//conditions:default": [], + })`, + "srcs": `["b.cpp"]`, + })...) + expected_targets = append(expected_targets, makeCcLibraryTargets("c", attrNameToString{ + "features": `select({ + "//build/bazel/platforms/os:darwin": [ + "disable_pack_relocations", + "-no_undefined_symbols", + ], + "//conditions:default": [], + })`, + "srcs": `["c.cpp"]`, + })...) + runCcLibraryTestCase(t, bp2buildTestCase{ description: "cc_library pack_relocations test", moduleTypeUnderTest: "cc_library", @@ -929,33 +1030,7 @@ cc_library { }, include_build_directory: false, }`, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "features": `[ - "disable_pack_relocations", - "-no_undefined_symbols", - ]`, - "srcs": `["a.cpp"]`, - }), makeBazelTarget("cc_library", "b", attrNameToString{ - "features": `select({ - "//build/bazel/platforms/arch:x86_64": [ - "disable_pack_relocations", - "-no_undefined_symbols", - ], - "//conditions:default": [], - })`, - "srcs": `["b.cpp"]`, - }), makeBazelTarget("cc_library", "c", attrNameToString{ - "features": `select({ - "//build/bazel/platforms/os:darwin": [ - "disable_pack_relocations", - "-no_undefined_symbols", - ], - "//conditions:default": [], - })`, - "srcs": `["c.cpp"]`, - }), - }, + expectedBazelTargets: expected_targets, }) } @@ -972,15 +1047,14 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "copts": `[ + expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{ + "copts": `[ "-include", "header.h", ]`, - }), - }, - }) + }), + }, + ) } func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) { @@ -1010,10 +1084,9 @@ func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "a", attrNameToString{ - "copts": `["-Wall"]`, - "cppflags": `[ + expectedBazelTargets: makeCcLibraryTargets("a", attrNameToString{ + "copts": `["-Wall"]`, + "cppflags": `[ "-fsigned-char", "-pedantic", ] + select({ @@ -1023,10 +1096,10 @@ func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) { "//build/bazel/platforms/os:android": ["-DANDROID=1"], "//conditions:default": [], })`, - "srcs": `["a.cpp"]`, - }), - }, - }) + "srcs": `["a.cpp"]`, + }), + }, + ) } func TestCcLibraryExcludeLibs(t *testing.T) { @@ -1109,33 +1182,32 @@ cc_library { bazel_module: { bp2build_available: false }, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo_static", attrNameToString{ - "implementation_deps": `select({ + expectedBazelTargets: makeCcLibraryTargets("foo_static", attrNameToString{ + "implementation_deps": `select({ "//build/bazel/platforms/arch:arm": [], "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"], }) + select({ "//build/bazel/product_variables:malloc_not_svelte": [], "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"], })`, - "implementation_dynamic_deps": `select({ + "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({ + "srcs_c": `["common.c"]`, + "whole_archive_deps": `select({ "//build/bazel/platforms/arch:arm": [], "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"], }) + select({ "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"], "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"], })`, - }), - }, - }) + }), + }, + ) } func TestCCLibraryNoCrtTrue(t *testing.T) { @@ -1155,13 +1227,12 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo-lib", attrNameToString{ - "link_crt": `False`, - "srcs": `["impl.cpp"]`, - }), - }, - }) + expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ + "link_crt": `False`, + "srcs": `["impl.cpp"]`, + }), + }, + ) } func TestCCLibraryNoCrtFalse(t *testing.T) { @@ -1181,11 +1252,9 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo-lib", attrNameToString{ - "srcs": `["impl.cpp"]`, - }), - }, + expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + }), }) } @@ -1235,12 +1304,35 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo-lib", attrNameToString{ - "srcs": `["impl.cpp"]`, - "use_libcrt": `False`, - }), - }}) + expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + "use_libcrt": `False`, + }), + }) +} + +func makeCcLibraryTargets(name string, attrs attrNameToString) []string { + STATIC_ONLY_ATTRS := map[string]bool{} + SHARED_ONLY_ATTRS := map[string]bool{ + "link_crt": true, + "additional_linker_inputs": true, + "linkopts": true, + "strip": true, + } + sharedAttrs := attrNameToString{} + staticAttrs := attrNameToString{} + for key, val := range attrs { + if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly { + sharedAttrs[key] = val + } + if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly { + staticAttrs[key] = val + } + } + sharedTarget := makeBazelTarget("cc_library_shared", name, sharedAttrs) + staticTarget := makeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs) + + return []string{staticTarget, sharedTarget} } func TestCCLibraryNoLibCrtFalse(t *testing.T) { @@ -1260,12 +1352,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo-lib", attrNameToString{ - "srcs": `["impl.cpp"]`, - "use_libcrt": `True`, - }), - }}) + expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + "use_libcrt": `True`, + }), + }) } func TestCCLibraryNoLibCrtArchVariant(t *testing.T) { @@ -1291,19 +1382,46 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo-lib", attrNameToString{ - "srcs": `["impl.cpp"]`, - "use_libcrt": `select({ + expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ + "srcs": `["impl.cpp"]`, + "use_libcrt": `select({ "//build/bazel/platforms/arch:arm": False, "//build/bazel/platforms/arch:x86": False, "//conditions:default": None, })`, - }), - }}) + }), + }) } func TestCcLibraryStrip(t *testing.T) { + expectedTargets := []string{} + expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", attrNameToString{ + "strip": `{ + "all": True, + }`, + })...) + expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", attrNameToString{ + "strip": `{ + "keep_symbols": True, + }`, + })...) + expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", attrNameToString{ + "strip": `{ + "keep_symbols_and_debug_frame": True, + }`, + })...) + expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", attrNameToString{ + "strip": `{ + "keep_symbols_list": ["symbol"], + }`, + })...) + expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", attrNameToString{ + "strip": `{ + "none": True, + }`, + })...) + expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", attrNameToString{})...) + runCcLibraryTestCase(t, bp2buildTestCase{ description: "cc_library strip args", moduleTypeUnderTest: "cc_library", @@ -1350,29 +1468,7 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "all", attrNameToString{ - "strip": `{ - "all": True, - }`, - }), makeBazelTarget("cc_library", "keep_symbols", attrNameToString{ - "strip": `{ - "keep_symbols": True, - }`, - }), makeBazelTarget("cc_library", "keep_symbols_and_debug_frame", attrNameToString{ - "strip": `{ - "keep_symbols_and_debug_frame": True, - }`, - }), makeBazelTarget("cc_library", "keep_symbols_list", attrNameToString{ - "strip": `{ - "keep_symbols_list": ["symbol"], - }`, - }), makeBazelTarget("cc_library", "none", attrNameToString{ - "strip": `{ - "none": True, - }`, - }), makeBazelTarget("cc_library", "nothing", attrNameToString{}), - }, + expectedBazelTargets: expectedTargets, }) } @@ -1407,9 +1503,8 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "multi-arch", attrNameToString{ - "strip": `{ + expectedBazelTargets: makeCcLibraryTargets("multi-arch", attrNameToString{ + "strip": `{ "keep_symbols": select({ "//build/bazel/platforms/arch:arm64": True, "//conditions:default": None, @@ -1426,9 +1521,9 @@ cc_library { "//conditions:default": [], }), }`, - }), - }, - }) + }), + }, + ) } func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) { @@ -1444,12 +1539,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "root_empty", attrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - }) + expectedBazelTargets: makeCcLibraryTargets("root_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, + ) } func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) { @@ -1468,11 +1562,10 @@ cc_library { } `, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "static_empty", attrNameToString{ - "static": `{ - "system_dynamic_deps": [], - }`, + makeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", attrNameToString{ + "system_dynamic_deps": "[]", }), + makeBazelTarget("cc_library_shared", "static_empty", attrNameToString{}), }, }) } @@ -1493,10 +1586,9 @@ cc_library { } `, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "shared_empty", attrNameToString{ - "shared": `{ - "system_dynamic_deps": [], - }`, + makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", attrNameToString{}), + makeBazelTarget("cc_library_shared", "shared_empty", attrNameToString{ + "system_dynamic_deps": "[]", }), }, }) @@ -1522,10 +1614,9 @@ cc_library { } `, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "shared_empty", attrNameToString{ - "shared": `{ - "system_dynamic_deps": [], - }`, + makeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", attrNameToString{}), + makeBazelTarget("cc_library_shared", "shared_empty", attrNameToString{ + "system_dynamic_deps": "[]", }), }, }) @@ -1552,12 +1643,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "target_linux_bionic_empty", attrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - }) + expectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, + ) } func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) { @@ -1577,12 +1667,11 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "target_bionic_empty", attrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - }) + expectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", attrNameToString{ + "system_dynamic_deps": `[]`, + }), + }, + ) } func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) { @@ -1611,12 +1700,15 @@ cc_library { } `, expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo", attrNameToString{ - "shared": `{ - "system_dynamic_deps": [":libm"], - }`, + makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{ "system_dynamic_deps": `[":libc"]`, }), + makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "system_dynamic_deps": `[ + ":libc", + ":libm", + ]`, + }), }, }) } @@ -1659,9 +1751,8 @@ cc_library { include_build_directory: false, } `, - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", "foo-lib", attrNameToString{ - "srcs": `["base.cpp"] + select({ + expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ + "srcs": `["base.cpp"] + select({ "//build/bazel/platforms/os:android": [ "linux.cpp", "bionic.cpp", @@ -1683,9 +1774,9 @@ cc_library { "//build/bazel/platforms/os:windows": ["windows.cpp"], "//conditions:default": [], })`, - }), - }, - }) + }), + }, + ) } func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) { @@ -1783,9 +1874,7 @@ cc_library { include_build_directory: false, } `, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp), - expectedBazelTargets: []string{ - makeBazelTarget("cc_library", name_prefix+"_full", attrs), - }, + expectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs), }) runCcLibraryStaticTestCase(t, bp2buildTestCase{ @@ -1829,3 +1918,170 @@ cc_library_shared { }) } } + +func TestCcLibraryProtoSimple(t *testing.T) { + runCcLibraryTestCase(t, bp2buildTestCase{ + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + "strip_import_prefix": `""`, + }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{ + "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, + "deps": `[":libprotobuf-cpp-lite"]`, + }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + }), + }, + }) +} + +func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) { + runCcLibraryTestCase(t, bp2buildTestCase{ + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + proto: { canonical_path_from_root: false}, + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{ + "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, + "deps": `[":libprotobuf-cpp-lite"]`, + }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + }), + }, + }) +} + +func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) { + runCcLibraryTestCase(t, bp2buildTestCase{ + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + proto: { canonical_path_from_root: true}, + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + "strip_import_prefix": `""`, + }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{ + "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, + "deps": `[":libprotobuf-cpp-lite"]`, + }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + }), + }, + }) +} + +func TestCcLibraryProtoFull(t *testing.T) { + runCcLibraryTestCase(t, bp2buildTestCase{ + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + proto: { + canonical_path_from_root: false, + type: "full", + }, + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }), makeBazelTarget("cc_proto_library", "foo_cc_proto", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{ + "implementation_whole_archive_deps": `[":foo_cc_proto"]`, + "deps": `[":libprotobuf-cpp-full"]`, + }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-full"]`, + }), + }, + }) +} + +func TestCcLibraryProtoLite(t *testing.T) { + runCcLibraryTestCase(t, bp2buildTestCase{ + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + proto: { + canonical_path_from_root: false, + type: "lite", + }, + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{ + "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, + "deps": `[":libprotobuf-cpp-lite"]`, + }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + }), + }, + }) +} + +func TestCcLibraryProtoExportHeaders(t *testing.T) { + runCcLibraryTestCase(t, bp2buildTestCase{ + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + blueprint: soongCcProtoPreamble + `cc_library { + name: "foo", + srcs: ["foo.proto"], + proto: { + canonical_path_from_root: false, + export_proto_headers: true, + }, + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", attrNameToString{ + "deps": `[":libprotobuf-cpp-lite"]`, + "whole_archive_deps": `[":foo_cc_proto_lite"]`, + }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + "whole_archive_deps": `[":foo_cc_proto_lite"]`, + }), + }, + }) +} diff --git a/bp2build/cc_library_shared_conversion_test.go b/bp2build/cc_library_shared_conversion_test.go index 4ec95c3a3..e0331bee5 100644 --- a/bp2build/cc_library_shared_conversion_test.go +++ b/bp2build/cc_library_shared_conversion_test.go @@ -33,6 +33,7 @@ func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) { ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) + ctx.RegisterModuleType("cc_library", cc.LibraryFactory) } func runCcLibrarySharedTestCase(t *testing.T, tc bp2buildTestCase) { @@ -425,3 +426,27 @@ cc_library_shared { expectedErr: fmt.Errorf("Android.bp:16:1: module \"foo_shared\": nocrt is not supported for arch variants"), }) } + +func TestCcLibrarySharedProto(t *testing.T) { + runCcLibrarySharedTestCase(t, bp2buildTestCase{ + blueprint: soongCcProtoPreamble + `cc_library_shared { + name: "foo", + srcs: ["foo.proto"], + proto: { + canonical_path_from_root: false, + export_proto_headers: true, + }, + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_shared", "foo", attrNameToString{ + "dynamic_deps": `[":libprotobuf-cpp-lite"]`, + "whole_archive_deps": `[":foo_cc_proto_lite"]`, + }), + }, + }) +} diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go index 2f760d236..02229e51e 100644 --- a/bp2build/cc_library_static_conversion_test.go +++ b/bp2build/cc_library_static_conversion_test.go @@ -1419,3 +1419,27 @@ cc_library_static { }, }) } + +func TestCcLibraryStaticProto(t *testing.T) { + runCcLibraryStaticTestCase(t, bp2buildTestCase{ + blueprint: soongCcProtoPreamble + `cc_library_static { + name: "foo", + srcs: ["foo.proto"], + proto: { + canonical_path_from_root: false, + export_proto_headers: true, + }, + include_build_directory: false, +}`, + expectedBazelTargets: []string{ + makeBazelTarget("proto_library", "foo_proto", attrNameToString{ + "srcs": `["foo.proto"]`, + }), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{ + "deps": `[":foo_proto"]`, + }), makeBazelTarget("cc_library_static", "foo", attrNameToString{ + "deps": `[":libprotobuf-cpp-lite"]`, + "whole_archive_deps": `[":foo_cc_proto_lite"]`, + }), + }, + }) +} diff --git a/cc/binary.go b/cc/binary.go index a5afb0725..63657e4dd 100644 --- a/cc/binary.go +++ b/cc/binary.go @@ -18,6 +18,7 @@ import ( "path/filepath" "github.com/google/blueprint" + "github.com/google/blueprint/proptools" "android/soong/android" "android/soong/bazel" @@ -578,9 +579,16 @@ func binaryBp2build(ctx android.TopDownMutatorContext, typ string) { } baseAttrs := bp2BuildParseBaseProps(ctx, m) + binaryLinkerAttrs := bp2buildBinaryLinkerProps(ctx, m) + + if proptools.BoolDefault(binaryLinkerAttrs.Linkshared, true) { + baseAttrs.implementationDynamicDeps.Add(baseAttrs.protoDependency) + } else { + baseAttrs.implementationDeps.Add(baseAttrs.protoDependency) + } attrs := &binaryAttributes{ - binaryLinkerAttrs: bp2buildBinaryLinkerProps(ctx, m), + binaryLinkerAttrs: binaryLinkerAttrs, Srcs: baseAttrs.srcs, Srcs_c: baseAttrs.cSrcs, diff --git a/cc/bp2build.go b/cc/bp2build.go index 888c3bacb..f9bbe8752 100644 --- a/cc/bp2build.go +++ b/cc/bp2build.go @@ -27,9 +27,10 @@ import ( ) const ( - cSrcPartition = "c" - asSrcPartition = "as" - cppSrcPartition = "cpp" + cSrcPartition = "c" + asSrcPartition = "as" + cppSrcPartition = "cpp" + protoSrcPartition = "proto" ) // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- @@ -41,52 +42,53 @@ type staticOrSharedAttributes struct { Hdrs bazel.LabelListAttribute Copts bazel.StringListAttribute - Deps bazel.LabelListAttribute - Implementation_deps bazel.LabelListAttribute - Dynamic_deps bazel.LabelListAttribute - Implementation_dynamic_deps bazel.LabelListAttribute - Whole_archive_deps bazel.LabelListAttribute + Deps bazel.LabelListAttribute + Implementation_deps bazel.LabelListAttribute + Dynamic_deps bazel.LabelListAttribute + Implementation_dynamic_deps bazel.LabelListAttribute + Whole_archive_deps bazel.LabelListAttribute + Implementation_whole_archive_deps bazel.LabelListAttribute System_dynamic_deps bazel.LabelListAttribute } func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { - // Check that a module is a filegroup type named <label>. - isFilegroupNamed := func(m android.Module, fullLabel string) bool { - if ctx.OtherModuleType(m) != "filegroup" { - return false - } - labelParts := strings.Split(fullLabel, ":") - if len(labelParts) > 2 { - // There should not be more than one colon in a label. - ctx.ModuleErrorf("%s is not a valid Bazel label for a filegroup", fullLabel) - } - return m.Name() == labelParts[len(labelParts)-1] + // Check that a module is a filegroup type + isFilegroup := func(m blueprint.Module) bool { + return ctx.OtherModuleType(m) == "filegroup" } // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl // macro. addSuffixForFilegroup := func(suffix string) bazel.LabelMapper { - return func(ctx bazel.OtherModuleContext, label string) (string, bool) { - m, exists := ctx.ModuleFromName(label) - if !exists { - return label, false + return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { + m, exists := ctx.ModuleFromName(label.OriginalModuleName) + labelStr := label.Label + if !exists || !isFilegroup(m) { + return labelStr, false } - aModule, _ := m.(android.Module) - if !isFilegroupNamed(aModule, label) { - return label, false - } - return label + suffix, true + return labelStr + suffix, true } } + isProtoFilegroup := func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { + m, exists := ctx.ModuleFromName(label.OriginalModuleName) + labelStr := label.Label + if !exists || !isFilegroup(m) { + return labelStr, false + } + likelyProtos := strings.HasSuffix(labelStr, "proto") || strings.HasSuffix(labelStr, "protos") + return labelStr, likelyProtos + } + // TODO(b/190006308): Handle language detection of sources in a Bazel rule. partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{ cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")}, asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")}, // C++ is the "catch-all" group, and comprises generated sources because we don't // know the language of these sources until the genrule is executed. - cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true}, + cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true}, + protoSrcPartition: bazel.LabelPartition{Extensions: []string{".proto"}, LabelMapper: isProtoFilegroup}, }) return partitioned @@ -195,6 +197,11 @@ func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, mo attrs.Srcs_c = partitionedSrcs[cSrcPartition] attrs.Srcs_as = partitionedSrcs[asSrcPartition] + if !partitionedSrcs[protoSrcPartition].IsEmpty() { + // TODO(b/208815215): determine whether this is used and add support if necessary + ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported") + } + return attrs } @@ -230,6 +237,8 @@ func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, m type baseAttributes struct { compilerAttributes linkerAttributes + + protoDependency *bazel.LabelAttribute } // Convenience struct to hold all attributes parsed from compiler properties. @@ -257,6 +266,8 @@ type compilerAttributes struct { localIncludes bazel.StringListAttribute absoluteIncludes bazel.StringListAttribute + + protoSrcs bazel.LabelListAttribute } func parseCommandLineFlags(soongFlags []string) []string { @@ -337,6 +348,8 @@ func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, i ca.srcs.ResolveExcludes() partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs) + ca.protoSrcs = partitionedSrcs[protoSrcPartition] + for p, lla := range partitionedSrcs { // if there are no sources, there is no need for headers if lla.IsEmpty() { @@ -400,7 +413,7 @@ func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions * } // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. -func bp2BuildParseBaseProps(ctx android.BazelConversionPathContext, module *Module) baseAttributes { +func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes { archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) @@ -456,20 +469,30 @@ func bp2BuildParseBaseProps(ctx android.BazelConversionPathContext, module *Modu (&compilerAttrs).finalize(ctx, implementationHdrs) (&linkerAttrs).finalize() + protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs) + + // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know + // which. This will add the newly generated proto library to the appropriate attribute and nothing + // to the other + (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib) + (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib) + return baseAttributes{ compilerAttrs, linkerAttrs, + protoDep.protoDep, } } // Convenience struct to hold all attributes parsed from linker properties. type linkerAttributes struct { - deps bazel.LabelListAttribute - implementationDeps bazel.LabelListAttribute - dynamicDeps bazel.LabelListAttribute - implementationDynamicDeps bazel.LabelListAttribute - wholeArchiveDeps bazel.LabelListAttribute - systemDynamicDeps bazel.LabelListAttribute + deps bazel.LabelListAttribute + implementationDeps bazel.LabelListAttribute + dynamicDeps bazel.LabelListAttribute + implementationDynamicDeps bazel.LabelListAttribute + wholeArchiveDeps bazel.LabelListAttribute + implementationWholeArchiveDeps bazel.LabelListAttribute + systemDynamicDeps bazel.LabelListAttribute linkCrt bazel.BoolAttribute useLibcrt bazel.BoolAttribute @@ -1713,7 +1713,15 @@ func (c *Module) setSubnameProperty(actx android.ModuleContext) { // Returns true if Bazel was successfully used for the analysis of this module. func (c *Module) maybeGenerateBazelActions(actx android.ModuleContext) bool { - bazelModuleLabel := c.GetBazelLabel(actx, c) + var bazelModuleLabel string + if actx.ModuleType() == "cc_library" && c.static() { + // cc_library is a special case in bp2build; two targets are generated -- one for each + // of the shared and static variants. The shared variant keeps the module name, but the + // static variant uses a different suffixed name. + bazelModuleLabel = bazelLabelForStaticModule(actx, c) + } else { + bazelModuleLabel = c.GetBazelLabel(actx, c) + } bazelActionsUsed := false // Mixed builds mode is disabled for modules outside of device OS. // TODO(b/200841190): Support non-device OS in mixed builds. diff --git a/cc/library.go b/cc/library.go index 3dceda0a3..d07142917 100644 --- a/cc/library.go +++ b/cc/library.go @@ -236,12 +236,13 @@ type bazelCcLibraryAttributes struct { Hdrs bazel.LabelListAttribute - Deps bazel.LabelListAttribute - Implementation_deps bazel.LabelListAttribute - Dynamic_deps bazel.LabelListAttribute - Implementation_dynamic_deps bazel.LabelListAttribute - Whole_archive_deps bazel.LabelListAttribute - System_dynamic_deps bazel.LabelListAttribute + Deps bazel.LabelListAttribute + Implementation_deps bazel.LabelListAttribute + Dynamic_deps bazel.LabelListAttribute + Implementation_dynamic_deps bazel.LabelListAttribute + Whole_archive_deps bazel.LabelListAttribute + Implementation_whole_archive_deps bazel.LabelListAttribute + System_dynamic_deps bazel.LabelListAttribute Export_includes bazel.StringListAttribute Export_system_includes bazel.StringListAttribute @@ -303,40 +304,83 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) { srcs := compilerAttrs.srcs + sharedAttrs.Dynamic_deps.Add(baseAttributes.protoDependency) + staticAttrs.Deps.Add(baseAttributes.protoDependency) + asFlags := compilerAttrs.asFlags if compilerAttrs.asSrcs.IsEmpty() && sharedAttrs.Srcs_as.IsEmpty() && staticAttrs.Srcs_as.IsEmpty() { // Skip asflags for BUILD file simplicity if there are no assembly sources. asFlags = bazel.MakeStringListAttribute(nil) } - attrs := &bazelCcLibraryAttributes{ - Srcs: srcs, - Srcs_c: compilerAttrs.cSrcs, - Srcs_as: compilerAttrs.asSrcs, - Hdrs: compilerAttrs.hdrs, + staticCommonAttrs := staticOrSharedAttributes{ + Srcs: *srcs.Clone().Append(staticAttrs.Srcs), + Srcs_c: *compilerAttrs.cSrcs.Clone().Append(staticAttrs.Srcs_c), + Srcs_as: *compilerAttrs.asSrcs.Clone().Append(staticAttrs.Srcs_as), + Copts: *compilerAttrs.copts.Clone().Append(staticAttrs.Copts), + Hdrs: *compilerAttrs.hdrs.Clone().Append(staticAttrs.Hdrs), + + Deps: *linkerAttrs.deps.Clone().Append(staticAttrs.Deps), + Implementation_deps: *linkerAttrs.implementationDeps.Clone().Append(staticAttrs.Implementation_deps), + Dynamic_deps: *linkerAttrs.dynamicDeps.Clone().Append(staticAttrs.Dynamic_deps), + Implementation_dynamic_deps: *linkerAttrs.implementationDynamicDeps.Clone().Append(staticAttrs.Implementation_dynamic_deps), + Implementation_whole_archive_deps: linkerAttrs.implementationWholeArchiveDeps, + Whole_archive_deps: *linkerAttrs.wholeArchiveDeps.Clone().Append(staticAttrs.Whole_archive_deps), + System_dynamic_deps: *linkerAttrs.systemDynamicDeps.Clone().Append(staticAttrs.System_dynamic_deps), + } + + sharedCommonAttrs := staticOrSharedAttributes{ + Srcs: *srcs.Clone().Append(sharedAttrs.Srcs), + Srcs_c: *compilerAttrs.cSrcs.Clone().Append(sharedAttrs.Srcs_c), + Srcs_as: *compilerAttrs.asSrcs.Clone().Append(sharedAttrs.Srcs_as), + Copts: *compilerAttrs.copts.Clone().Append(sharedAttrs.Copts), + Hdrs: *compilerAttrs.hdrs.Clone().Append(sharedAttrs.Hdrs), + + Deps: *linkerAttrs.deps.Clone().Append(sharedAttrs.Deps), + Implementation_deps: *linkerAttrs.implementationDeps.Clone().Append(sharedAttrs.Implementation_deps), + Dynamic_deps: *linkerAttrs.dynamicDeps.Clone().Append(sharedAttrs.Dynamic_deps), + Implementation_dynamic_deps: *linkerAttrs.implementationDynamicDeps.Clone().Append(sharedAttrs.Implementation_dynamic_deps), + Whole_archive_deps: *linkerAttrs.wholeArchiveDeps.Clone().Append(sharedAttrs.Whole_archive_deps), + System_dynamic_deps: *linkerAttrs.systemDynamicDeps.Clone().Append(sharedAttrs.System_dynamic_deps), + } + + staticTargetAttrs := &bazelCcLibraryStaticAttributes{ + staticOrSharedAttributes: staticCommonAttrs, - Copts: compilerAttrs.copts, Cppflags: compilerAttrs.cppFlags, Conlyflags: compilerAttrs.conlyFlags, Asflags: asFlags, - Implementation_deps: linkerAttrs.implementationDeps, - Deps: linkerAttrs.deps, - Implementation_dynamic_deps: linkerAttrs.implementationDynamicDeps, - Dynamic_deps: linkerAttrs.dynamicDeps, - Whole_archive_deps: linkerAttrs.wholeArchiveDeps, - System_dynamic_deps: linkerAttrs.systemDynamicDeps, - Export_includes: exportedIncludes.Includes, - Export_system_includes: exportedIncludes.SystemIncludes, - Local_includes: compilerAttrs.localIncludes, - Absolute_includes: compilerAttrs.absoluteIncludes, - Linkopts: linkerAttrs.linkopts, - Link_crt: linkerAttrs.linkCrt, - Use_libcrt: linkerAttrs.useLibcrt, - Rtti: compilerAttrs.rtti, - Stl: compilerAttrs.stl, - Cpp_std: compilerAttrs.cppStd, - C_std: compilerAttrs.cStd, + Export_includes: exportedIncludes.Includes, + Export_system_includes: exportedIncludes.SystemIncludes, + Local_includes: compilerAttrs.localIncludes, + Absolute_includes: compilerAttrs.absoluteIncludes, + Use_libcrt: linkerAttrs.useLibcrt, + Rtti: compilerAttrs.rtti, + Stl: compilerAttrs.stl, + Cpp_std: compilerAttrs.cppStd, + C_std: compilerAttrs.cStd, + + Features: linkerAttrs.features, + } + + sharedTargetAttrs := &bazelCcLibrarySharedAttributes{ + staticOrSharedAttributes: sharedCommonAttrs, + Cppflags: compilerAttrs.cppFlags, + Conlyflags: compilerAttrs.conlyFlags, + Asflags: asFlags, + + Export_includes: exportedIncludes.Includes, + Export_system_includes: exportedIncludes.SystemIncludes, + Local_includes: compilerAttrs.localIncludes, + Absolute_includes: compilerAttrs.absoluteIncludes, + Linkopts: linkerAttrs.linkopts, + Link_crt: linkerAttrs.linkCrt, + Use_libcrt: linkerAttrs.useLibcrt, + Rtti: compilerAttrs.rtti, + Stl: compilerAttrs.stl, + Cpp_std: compilerAttrs.cppStd, + C_std: compilerAttrs.cStd, Additional_linker_inputs: linkerAttrs.additionalLinkerInputs, @@ -347,20 +391,20 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) { All: linkerAttrs.stripAll, None: linkerAttrs.stripNone, }, - - Shared: sharedAttrs, - - Static: staticAttrs, - Features: linkerAttrs.features, } - props := bazel.BazelTargetModuleProperties{ - Rule_class: "cc_library", - Bzl_load_location: "//build/bazel/rules:full_cc_library.bzl", + staticProps := bazel.BazelTargetModuleProperties{ + Rule_class: "cc_library_static", + Bzl_load_location: "//build/bazel/rules:cc_library_static.bzl", + } + sharedProps := bazel.BazelTargetModuleProperties{ + Rule_class: "cc_library_shared", + Bzl_load_location: "//build/bazel/rules:cc_library_shared.bzl", } - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) + ctx.CreateBazelTargetModule(staticProps, android.CommonAttributes{Name: m.Name() + "_bp2build_cc_library_static"}, staticTargetAttrs) + ctx.CreateBazelTargetModule(sharedProps, android.CommonAttributes{Name: m.Name()}, sharedTargetAttrs) } // cc_library creates both static and/or shared libraries for a device and/or @@ -2405,16 +2449,18 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext, Copts: compilerAttrs.copts, Hdrs: compilerAttrs.hdrs, - Deps: linkerAttrs.deps, - Implementation_deps: linkerAttrs.implementationDeps, - Dynamic_deps: linkerAttrs.dynamicDeps, - Implementation_dynamic_deps: linkerAttrs.implementationDynamicDeps, - Whole_archive_deps: linkerAttrs.wholeArchiveDeps, - System_dynamic_deps: linkerAttrs.systemDynamicDeps, + Deps: linkerAttrs.deps, + Implementation_deps: linkerAttrs.implementationDeps, + Dynamic_deps: linkerAttrs.dynamicDeps, + Implementation_dynamic_deps: linkerAttrs.implementationDynamicDeps, + Whole_archive_deps: linkerAttrs.wholeArchiveDeps, + Implementation_whole_archive_deps: linkerAttrs.implementationWholeArchiveDeps, + System_dynamic_deps: linkerAttrs.systemDynamicDeps, } var attrs interface{} if isStatic { + commonAttrs.Deps.Add(baseAttributes.protoDependency) attrs = &bazelCcLibraryStaticAttributes{ staticOrSharedAttributes: commonAttrs, @@ -2435,6 +2481,8 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext, Features: linkerAttrs.features, } } else { + commonAttrs.Dynamic_deps.Add(baseAttributes.protoDependency) + attrs = &bazelCcLibrarySharedAttributes{ staticOrSharedAttributes: commonAttrs, diff --git a/cc/library_test.go b/cc/library_test.go index 7ddfaa7fd..7427b5974 100644 --- a/cc/library_test.go +++ b/cc/library_test.go @@ -257,9 +257,14 @@ cc_library { CcObjectFiles: []string{"foo.o"}, Includes: []string{"include"}, SystemIncludes: []string{"system_include"}, - RootStaticArchives: []string{"foo.a"}, RootDynamicLibraries: []string{"foo.so"}, }, + "//foo/bar:bar_bp2build_cc_library_static": cquery.CcInfo{ + CcObjectFiles: []string{"foo.o"}, + Includes: []string{"include"}, + SystemIncludes: []string{"system_include"}, + RootStaticArchives: []string{"foo.a"}, + }, }, } ctx := testCcWithConfig(t, config) diff --git a/cc/proto.go b/cc/proto.go index 44661447c..f3410bc2b 100644 --- a/cc/proto.go +++ b/cc/proto.go @@ -16,8 +16,14 @@ package cc import ( "github.com/google/blueprint/pathtools" + "github.com/google/blueprint/proptools" "android/soong/android" + "android/soong/bazel" +) + +const ( + protoTypeDefault = "lite" ) // genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns @@ -63,7 +69,7 @@ func protoDeps(ctx DepsContext, deps Deps, p *android.ProtoProperties, static bo var lib string if String(p.Proto.Plugin) == "" { - switch String(p.Proto.Type) { + switch proptools.StringDefault(p.Proto.Type, protoTypeDefault) { case "full": if ctx.useSdk() { lib = "libprotobuf-cpp-full-ndk" @@ -71,7 +77,7 @@ func protoDeps(ctx DepsContext, deps Deps, p *android.ProtoProperties, static bo } else { lib = "libprotobuf-cpp-full" } - case "lite", "": + case "lite": if ctx.useSdk() { lib = "libprotobuf-cpp-lite-ndk" static = true @@ -157,3 +163,69 @@ func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flag return flags } + +type protoAttributes struct { + Deps bazel.LabelListAttribute +} + +type bp2buildProtoDeps struct { + wholeStaticLib *bazel.LabelAttribute + implementationWholeStaticLib *bazel.LabelAttribute + protoDep *bazel.LabelAttribute +} + +func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs bazel.LabelListAttribute) bp2buildProtoDeps { + var ret bp2buildProtoDeps + + protoInfo, ok := android.Bp2buildProtoProperties(ctx, m, protoSrcs) + if !ok { + return ret + } + + var depName string + typ := proptools.StringDefault(protoInfo.Type, protoTypeDefault) + var rule_class string + suffix := "_cc_proto" + switch typ { + case "lite": + suffix += "_lite" + rule_class = "cc_lite_proto_library" + depName = "libprotobuf-cpp-lite" + case "full": + rule_class = "cc_proto_library" + depName = "libprotobuf-cpp-full" + default: + ctx.PropertyErrorf("proto.type", "cannot handle conversion at this time: %q", typ) + } + + dep := android.BazelLabelForModuleDepSingle(ctx, depName) + ret.protoDep = &bazel.LabelAttribute{Value: &dep} + + protoLabel := bazel.Label{Label: ":" + protoInfo.Name} + var protoAttrs protoAttributes + protoAttrs.Deps.SetValue(bazel.LabelList{Includes: []bazel.Label{protoLabel}}) + + name := m.Name() + suffix + + ctx.CreateBazelTargetModule( + bazel.BazelTargetModuleProperties{ + Rule_class: rule_class, + Bzl_load_location: "//build/bazel/rules:cc_proto.bzl", + }, + android.CommonAttributes{Name: name}, + &protoAttrs) + + var privateHdrs bool + if lib, ok := m.linker.(*libraryDecorator); ok { + privateHdrs = !proptools.Bool(lib.Properties.Proto.Export_proto_headers) + } + + labelAttr := &bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + name}} + if privateHdrs { + ret.implementationWholeStaticLib = labelAttr + } else { + ret.wholeStaticLib = labelAttr + } + + return ret +} diff --git a/cc/sanitize.go b/cc/sanitize.go index 93d4b4c54..d7b1adee5 100644 --- a/cc/sanitize.go +++ b/cc/sanitize.go @@ -35,7 +35,6 @@ var ( asanCflags = []string{ "-fno-omit-frame-pointer", - "-fno-experimental-new-pass-manager", } asanLdflags = []string{"-Wl,-u,__asan_preinit"} @@ -666,9 +665,6 @@ func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth") flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth") - // TODO(b/133876586): Experimental PM breaks sanitizer coverage. - flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager") - // Disable fortify for fuzzing builds. Generally, we'll be building with // UBSan or ASan here and the fortify checks pollute the stack traces. flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE") diff --git a/java/app.go b/java/app.go index c08ec0697..bf76a5026 100755 --- a/java/app.go +++ b/java/app.go @@ -291,7 +291,7 @@ func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) { if minSdkVersion, err := a.MinSdkVersion(ctx).EffectiveVersion(ctx); err == nil { a.checkJniLibsSdkVersion(ctx, minSdkVersion) - android.CheckMinSdkVersion(a, ctx, minSdkVersion) + android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps) } else { ctx.PropertyErrorf("min_sdk_version", "%s", err.Error()) } diff --git a/rust/config/global.go b/rust/config/global.go index 78c8dae11..23384e551 100644 --- a/rust/config/global.go +++ b/rust/config/global.go @@ -55,6 +55,8 @@ var ( deviceGlobalRustFlags = []string{ "-C panic=abort", "-Z link-native-libraries=no", + // Generate additional debug info for AutoFDO + "-Z debug-info-for-profiling", } deviceGlobalLinkFlags = []string{ |