diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index 350bac1f0..6aa148ec2 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -400,6 +400,141 @@ cc_library { name: "shared_dep_for_both" } }) } +func TestCcLibrarySharedStaticPropsInArch(t *testing.T) { + runCcLibraryTestCase(t, bp2buildTestCase{ + description: "cc_library shared/static props in arch", + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, + dir: "foo/bar", + filesystem: map[string]string{ + "foo/bar/arm.cpp": "", + "foo/bar/x86.cpp": "", + "foo/bar/sharedonly.cpp": "", + "foo/bar/staticonly.cpp": "", + "foo/bar/Android.bp": ` +cc_library { + name: "a", + arch: { + arm: { + shared: { + srcs: ["arm_shared.cpp"], + cflags: ["-DARM_SHARED"], + static_libs: ["arm_static_dep_for_shared"], + whole_static_libs: ["arm_whole_static_dep_for_shared"], + shared_libs: ["arm_shared_dep_for_shared"], + }, + }, + x86: { + static: { + srcs: ["x86_static.cpp"], + cflags: ["-DX86_STATIC"], + static_libs: ["x86_dep_for_static"], + }, + }, + }, + target: { + android: { + shared: { + srcs: ["android_shared.cpp"], + cflags: ["-DANDROID_SHARED"], + static_libs: ["android_dep_for_shared"], + }, + }, + android_arm: { + shared: { + cflags: ["-DANDROID_ARM_SHARED"], + }, + }, + }, + srcs: ["both.cpp"], + cflags: ["bothflag"], + static_libs: ["static_dep_for_both"], + static: { + srcs: ["staticonly.cpp"], + cflags: ["staticflag"], + static_libs: ["static_dep_for_static"], + }, + shared: { + srcs: ["sharedonly.cpp"], + cflags: ["sharedflag"], + static_libs: ["static_dep_for_shared"], + }, + bazel_module: { bp2build_available: true }, +} + +cc_library_static { name: "static_dep_for_shared" } +cc_library_static { name: "static_dep_for_static" } +cc_library_static { name: "static_dep_for_both" } + +cc_library_static { name: "arm_static_dep_for_shared" } +cc_library_static { name: "arm_whole_static_dep_for_shared" } +cc_library_static { name: "arm_shared_dep_for_shared" } + +cc_library_static { name: "x86_dep_for_static" } + +cc_library_static { name: "android_dep_for_shared" } +`, + }, + blueprint: soongCcLibraryPreamble, + expectedBazelTargets: []string{`cc_library( + name = "a", + copts = [ + "bothflag", + "-Ifoo/bar", + "-I$(BINDIR)/foo/bar", + ], + dynamic_deps_for_shared = select({ + "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"], + "//conditions:default": [], + }), + implementation_deps = [":static_dep_for_both"], + 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:android_arm": ["-DANDROID_ARM_SHARED"], + "//conditions:default": [], + }), + shared_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": [], + }), + srcs = ["both.cpp"], + static_copts = ["staticflag"] + select({ + "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"], + "//conditions:default": [], + }), + static_deps_for_shared = [":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": [], + }), + static_deps_for_static = [":static_dep_for_static"] + select({ + "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"], + "//conditions:default": [], + }), + static_srcs = ["staticonly.cpp"] + select({ + "//build/bazel/platforms/arch:x86": ["x86_static.cpp"], + "//conditions:default": [], + }), + whole_archive_deps_for_shared = select({ + "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"], + "//conditions:default": [], + }), +)`}, + }) +} + func TestCcLibraryNonConfiguredVersionScript(t *testing.T) { runCcLibraryTestCase(t, bp2buildTestCase{ description: "cc_library non-configured version script", diff --git a/cc/bp2build.go b/cc/bp2build.go index e64573c3f..339a489d0 100644 --- a/cc/bp2build.go +++ b/cc/bp2build.go @@ -104,25 +104,63 @@ func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) { // Deps in the static: { .. } and shared: { .. } props of a cc_library. if lib, ok := module.compiler.(*libraryDecorator); ok { - allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...) - allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...) - allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...) + appendDeps := func(deps []string, p StaticOrSharedProperties) []string { + deps = append(deps, p.Static_libs...) + deps = append(deps, p.Whole_static_libs...) + deps = append(deps, p.Shared_libs...) + return deps + } - allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...) - allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...) - allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...) + allDeps = appendDeps(allDeps, lib.SharedProperties.Shared) + allDeps = appendDeps(allDeps, lib.StaticProperties.Static) // TODO(b/186024507, b/186489250): Temporarily exclude adding // system_shared_libs deps until libc and libm builds. // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) + + // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library. + // target: { : shared: { ... } } + for _, targetProps := range module.GetTargetProperties(ctx, &SharedProperties{}) { + if p, ok := targetProps.Properties.(*SharedProperties); ok { + allDeps = appendDeps(allDeps, p.Shared) + } + for _, archProperties := range targetProps.ArchProperties { + if p, ok := archProperties.(*SharedProperties); ok { + allDeps = appendDeps(allDeps, p.Shared) + } + } + } + // target: { : static: { ... } } + for _, targetProps := range module.GetTargetProperties(ctx, &StaticProperties{}) { + if p, ok := targetProps.Properties.(*StaticProperties); ok { + allDeps = appendDeps(allDeps, p.Static) + } + for _, archProperties := range targetProps.ArchProperties { + if p, ok := archProperties.(*StaticProperties); ok { + allDeps = appendDeps(allDeps, p.Static) + } + } + } + // arch: { : shared: { ... } } + for _, properties := range module.GetArchProperties(ctx, &SharedProperties{}) { + if p, ok := properties.(*SharedProperties); ok { + allDeps = appendDeps(allDeps, p.Shared) + } + } + // arch: { : static: { ... } } + for _, properties := range module.GetArchProperties(ctx, &StaticProperties{}) { + if p, ok := properties.(*StaticProperties); ok { + allDeps = appendDeps(allDeps, p.Static) + } + } } ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) } // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- -// properities which apply to either the shared or static version of a cc_library module. +// properties which apply to either the shared or static version of a cc_library module. type staticOrSharedAttributes struct { copts bazel.StringListAttribute srcs bazel.LabelListAttribute @@ -138,7 +176,7 @@ func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) return staticOrSharedAttributes{} } - return bp2buildParseStaticOrSharedProps(ctx, lib.SharedProperties.Shared) + return bp2buildParseStaticOrSharedProps(ctx, module, lib, false) } // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. @@ -148,31 +186,84 @@ func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) return staticOrSharedAttributes{} } - return bp2buildParseStaticOrSharedProps(ctx, lib.StaticProperties.Static) + return bp2buildParseStaticOrSharedProps(ctx, module, lib, true) } -func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, props StaticOrSharedProperties) staticOrSharedAttributes { - copts := bazel.StringListAttribute{Value: props.Cflags} - - srcs := bazel.LabelListAttribute{ - Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)} - - staticDeps := bazel.LabelListAttribute{ - Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)} - - dynamicDeps := bazel.LabelListAttribute{ - Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)} - - wholeArchiveDeps := bazel.LabelListAttribute{ - Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)} - - return staticOrSharedAttributes{ - copts: copts, - srcs: srcs, - staticDeps: staticDeps, - dynamicDeps: dynamicDeps, - wholeArchiveDeps: wholeArchiveDeps, +func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { + var props StaticOrSharedProperties + if isStatic { + props = lib.StaticProperties.Static + } else { + props = lib.SharedProperties.Shared } + + attrs := staticOrSharedAttributes{ + copts: bazel.StringListAttribute{Value: props.Cflags}, + srcs: bazel.LabelListAttribute{Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)}, + staticDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)}, + dynamicDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)}, + wholeArchiveDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)}, + } + + setArchAttrs := func(arch string, props StaticOrSharedProperties) { + attrs.copts.SetValueForArch(arch, props.Cflags) + attrs.srcs.SetValueForArch(arch, android.BazelLabelForModuleSrc(ctx, props.Srcs)) + attrs.staticDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) + attrs.dynamicDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) + attrs.wholeArchiveDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) + } + + setTargetAttrs := func(target string, props StaticOrSharedProperties) { + attrs.copts.SetOsValueForTarget(target, props.Cflags) + attrs.srcs.SetOsValueForTarget(target, android.BazelLabelForModuleSrc(ctx, props.Srcs)) + attrs.staticDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) + attrs.dynamicDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) + attrs.wholeArchiveDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) + } + + setTargetArchAttrs := func(target, arch string, props StaticOrSharedProperties) { + attrs.copts.SetOsArchValueForTarget(target, arch, props.Cflags) + attrs.srcs.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleSrc(ctx, props.Srcs)) + attrs.staticDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) + attrs.dynamicDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) + attrs.wholeArchiveDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) + } + + if isStatic { + for arch, properties := range module.GetArchProperties(ctx, &StaticProperties{}) { + if staticOrSharedProps, ok := properties.(*StaticProperties); ok { + setArchAttrs(arch.Name, staticOrSharedProps.Static) + } + } + for target, p := range module.GetTargetProperties(ctx, &StaticProperties{}) { + if staticOrSharedProps, ok := p.Properties.(*StaticProperties); ok { + setTargetAttrs(target.Name, staticOrSharedProps.Static) + } + for arch, archProperties := range p.ArchProperties { + if staticOrSharedProps, ok := archProperties.(*StaticProperties); ok { + setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Static) + } + } + } + } else { + for arch, p := range module.GetArchProperties(ctx, &SharedProperties{}) { + if staticOrSharedProps, ok := p.(*SharedProperties); ok { + setArchAttrs(arch.Name, staticOrSharedProps.Shared) + } + } + for target, p := range module.GetTargetProperties(ctx, &SharedProperties{}) { + if staticOrSharedProps, ok := p.Properties.(*SharedProperties); ok { + setTargetAttrs(target.Name, staticOrSharedProps.Shared) + } + for arch, archProperties := range p.ArchProperties { + if staticOrSharedProps, ok := archProperties.(*SharedProperties); ok { + setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Shared) + } + } + } + } + + return attrs } // Convenience struct to hold all attributes parsed from compiler properties.