From a37e1951822ad4e393a0f30aef241b340f8c0cf8 Mon Sep 17 00:00:00 2001 From: Chris Parsons Date: Tue, 28 Sep 2021 16:47:36 -0400 Subject: [PATCH] Support building libcrypto via mixed builds This required the following fixes to bp2build: - Correctly handle Bionic_* and Linux_* targets - Correctly handle cc_object's selects - Generate linker_script, stl, and system_dynamic_deps for cc_object in bp2build Test: USE_BAZEL_ANALYSIS=1 m adbd Change-Id: I753fd18df8ae551fb69df07e4174527c5388f289 --- android/arch.go | 54 +++++++--- android/bazel.go | 85 ++++++++++------ bp2build/cc_object_conversion_test.go | 137 +++++++++++++++++++++++--- cc/object.go | 58 +++++++---- 4 files changed, 260 insertions(+), 74 deletions(-) diff --git a/android/arch.go b/android/arch.go index 6add1b132..273c02b66 100644 --- a/android/arch.go +++ b/android/arch.go @@ -1999,12 +1999,7 @@ func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySe // Create a new instance of the requested property set value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface() - // Merge all the structs together - for _, propertyStruct := range propertyStructs { - mergePropertyStruct(ctx, value, propertyStruct) - } - - archToProp[arch.Name] = value + archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, value) } axisToProps[bazel.ArchConfigurationAxis] = archToProp @@ -2016,19 +2011,50 @@ func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySe // It looks like this OS value is not used in Blueprint files continue } - osToProp[os.Name] = getTargetStruct(ctx, propertySet, archProperties, os.Field) + osStructs, ok := getTargetStructs(ctx, archProperties, os.Field) + if ok { + osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet) + } + // For arm, x86, ... for _, arch := range osArchTypeMap[os] { + osArchStructs := make([]reflect.Value, 0) + targetField := GetCompoundTargetField(os, arch) targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name) - archOsToProp[targetName] = getTargetStruct(ctx, propertySet, archProperties, targetField) + targetStructs, ok := getTargetStructs(ctx, archProperties, targetField) + if ok { + osArchStructs = append(osArchStructs, targetStructs...) + } + + // Auto-combine with Linux_ and Bionic_ targets. This potentially results in + // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare. + // TODO(b/201423152): Look into cleanup. + if os.Linux() { + targetField := "Linux_" + arch.Name + targetStructs, ok := getTargetStructs(ctx, archProperties, targetField) + if ok { + osArchStructs = append(osArchStructs, targetStructs...) + } + } + if os.Bionic() { + targetField := "Bionic_" + arch.Name + targetStructs, ok := getTargetStructs(ctx, archProperties, targetField) + if ok { + osArchStructs = append(osArchStructs, targetStructs...) + } + } + + archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet) } } axisToProps[bazel.OsConfigurationAxis] = osToProp axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp - axisToProps[bazel.BionicConfigurationAxis] = map[string]interface{}{ - "bionic": getTargetStruct(ctx, propertySet, archProperties, "Bionic"), + bionicStructs, ok := getTargetStructs(ctx, archProperties, "Bionic") + if ok { + axisToProps[bazel.BionicConfigurationAxis] = map[string]interface{}{ + "bionic": mergeStructs(ctx, bionicStructs, propertySet)} } return axisToProps @@ -2048,7 +2074,7 @@ func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySe // } // } // This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"] -func getTargetStruct(ctx ArchVariantContext, propertySet interface{}, archProperties []interface{}, targetName string) interface{} { +func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) ([]reflect.Value, bool) { propertyStructs := make([]reflect.Value, 0) for _, archProperty := range archProperties { archPropValues := reflect.ValueOf(archProperty).Elem() @@ -2056,9 +2082,15 @@ func getTargetStruct(ctx ArchVariantContext, propertySet interface{}, archProper targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName) if ok { propertyStructs = append(propertyStructs, targetStruct) + } else { + return propertyStructs, false } } + return propertyStructs, true +} + +func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} { // Create a new instance of the requested property set value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface() diff --git a/android/bazel.go b/android/bazel.go index 737533af0..f5ae453c2 100644 --- a/android/bazel.go +++ b/android/bazel.go @@ -175,32 +175,43 @@ var ( // Configure modules in these directories to enable bp2build_available: true or false by default. bp2buildDefaultConfig = Bp2BuildConfig{ - "bionic": Bp2BuildDefaultTrueRecursively, - "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively, - "development/sdk": Bp2BuildDefaultTrueRecursively, - "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively, - "external/brotli": Bp2BuildDefaultTrue, - "external/fmtlib": Bp2BuildDefaultTrueRecursively, - "external/gwp_asan": Bp2BuildDefaultTrueRecursively, - "external/jemalloc_new": Bp2BuildDefaultTrueRecursively, - "external/libcap": Bp2BuildDefaultTrueRecursively, - "external/libcxx": Bp2BuildDefaultTrueRecursively, - "external/libcxxabi": Bp2BuildDefaultTrueRecursively, - "external/lz4/lib": Bp2BuildDefaultTrue, - "external/protobuf": Bp2BuildDefaultTrueRecursively, - "external/python/six": Bp2BuildDefaultTrueRecursively, - "external/scudo": Bp2BuildDefaultTrueRecursively, - "external/zlib": Bp2BuildDefaultTrueRecursively, - "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively, - "system/core/libasyncio": Bp2BuildDefaultTrue, - "system/core/libcutils": Bp2BuildDefaultTrueRecursively, - "system/core/libprocessgroup": Bp2BuildDefaultTrue, + "bionic": Bp2BuildDefaultTrueRecursively, + "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively, + "development/sdk": Bp2BuildDefaultTrueRecursively, + "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively, + "external/boringssl": Bp2BuildDefaultTrueRecursively, + "external/brotli": Bp2BuildDefaultTrue, + "external/fmtlib": Bp2BuildDefaultTrueRecursively, + "external/gwp_asan": Bp2BuildDefaultTrueRecursively, + "external/jemalloc_new": Bp2BuildDefaultTrueRecursively, + "external/libcap": Bp2BuildDefaultTrueRecursively, + "external/libcxx": Bp2BuildDefaultTrueRecursively, + "external/libcxxabi": Bp2BuildDefaultTrueRecursively, + "external/lz4/lib": Bp2BuildDefaultTrue, + "external/protobuf": Bp2BuildDefaultTrueRecursively, + "external/python/six": Bp2BuildDefaultTrueRecursively, + "external/scudo": Bp2BuildDefaultTrueRecursively, + "external/zlib": Bp2BuildDefaultTrueRecursively, + "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively, + "packages/modules/adb": Bp2BuildDefaultTrue, + "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively, + "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively, + "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively, + "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively, + "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively, + "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively, + "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively, + "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively, + "system/core/libasyncio": Bp2BuildDefaultTrue, + "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively, + "system/core/libcutils": Bp2BuildDefaultTrueRecursively, + "system/core/libprocessgroup": Bp2BuildDefaultTrue, "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively, - "system/libbase": Bp2BuildDefaultTrueRecursively, - "system/logging/liblog": Bp2BuildDefaultTrueRecursively, - "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively, - "system/timezone/apex": Bp2BuildDefaultTrueRecursively, - "system/timezone/output_data": Bp2BuildDefaultTrueRecursively, + "system/libbase": Bp2BuildDefaultTrueRecursively, + "system/logging/liblog": Bp2BuildDefaultTrueRecursively, + "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively, + "system/timezone/apex": Bp2BuildDefaultTrueRecursively, + "system/timezone/output_data": Bp2BuildDefaultTrueRecursively, } // Per-module denylist to always opt modules out of both bp2build and mixed builds. @@ -212,8 +223,8 @@ var ( "libc_malloc_debug", // http://b/186824339, cc_library_static, depends on //system/libbase:libbase (http://b/186823646) "libc_malloc_debug_backtrace", // http://b/186824112, cc_library_static, depends on //external/libcxxabi:libc++demangle (http://b/186823773) - "libcutils", // http://b/186827426, cc_library, depends on //system/core/libprocessgroup:libprocessgroup_headers (http://b/186826841) - "libcutils_sockets", // http://b/186826853, cc_library, depends on //system/libbase:libbase (http://b/186826479) + //"libcutils", // http://b/186827426, cc_library, depends on //system/core/libprocessgroup:libprocessgroup_headers (http://b/186826841) + //"libcutils_sockets", // http://b/186826853, cc_library, depends on //system/libbase:libbase (http://b/186826479) "liblinker_debuggerd_stub", // http://b/186824327, cc_library_static, depends on //external/zlib:libz (http://b/186823782) // also depends on //system/libziparchive:libziparchive (http://b/186823656) @@ -232,15 +243,13 @@ var ( "libbase_ndk", // http://b/186826477, cc_library, no such target '//build/bazel/platforms/os:darwin' when --platforms //build/bazel/platforms:android_x86 is added // libcxx "libBionicBenchmarksUtils", // cc_library_static, fatal error: 'map' file not found, from libcxx - "libbase", // Depends on fmtlib via static_libs and also whole_static_libs, which results in bazel errors. - - "libfdtrack", // depends on liblzma and libbase "libprotobuf-python", // contains .proto sources "libprotobuf-internal-protos", // we don't handle path property for fileegroups "libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups - "libseccomp_policy", // depends on libbase + "libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds. + "libfdtrack", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset @@ -262,6 +271,20 @@ var ( // APEX support "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug' and 'libc_malloc_hooks' + + "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 + + "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 } // Per-module denylist of cc_library modules to only generate the static diff --git a/bp2build/cc_object_conversion_test.go b/bp2build/cc_object_conversion_test.go index b0a88ae0d..63a0b8a21 100644 --- a/bp2build/cc_object_conversion_test.go +++ b/bp2build/cc_object_conversion_test.go @@ -71,6 +71,7 @@ func TestCcObjectSimple(t *testing.T) { ".", ], srcs = ["a/b/c.c"], + system_dynamic_deps = [], )`, }, }) @@ -116,6 +117,7 @@ cc_defaults { ], local_includes = ["."], srcs = ["a/b/c.c"], + system_dynamic_deps = [], )`, }}) } @@ -149,11 +151,13 @@ cc_object { name = "bar", copts = ["-fno-addrsig"], srcs = ["x/y/z.c"], + system_dynamic_deps = [], )`, `cc_object( name = "foo", copts = ["-fno-addrsig"], deps = [":bar"], srcs = ["a/b/c.c"], + system_dynamic_deps = [], )`, }, }) @@ -180,6 +184,7 @@ func TestCcObjectIncludeBuildDirFalse(t *testing.T) { name = "foo", copts = ["-fno-addrsig"], srcs = ["a/b/c.c"], + system_dynamic_deps = [], )`, }, }) @@ -211,6 +216,7 @@ func TestCcObjectProductVariable(t *testing.T) { }), copts = ["-fno-addrsig"], srcs_as = ["src.S"], + system_dynamic_deps = [], )`, }, }) @@ -248,6 +254,7 @@ func TestCcObjectCflagsOneArch(t *testing.T) { "//build/bazel/platforms/arch:arm": ["arch/arm/file.cpp"], "//conditions:default": [], }), + system_dynamic_deps = [], )`, }, }) @@ -301,30 +308,126 @@ func TestCcObjectCflagsFourArch(t *testing.T) { "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], "//conditions:default": [], }), + system_dynamic_deps = [], )`, }, }) } -func TestCcObjectCflagsMultiOs(t *testing.T) { +func TestCcObjectLinkerScript(t *testing.T) { runCcObjectTestCase(t, bp2buildTestCase{ - description: "cc_object setting cflags for multiple OSes", + description: "cc_object setting linker_script", moduleTypeUnderTest: "cc_object", moduleTypeUnderTestFactory: cc.ObjectFactory, moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, blueprint: `cc_object { name: "foo", + srcs: ["base.cpp"], + linker_script: "bunny.lds", + include_build_directory: false, +} +`, + expectedBazelTargets: []string{ + `cc_object( + name = "foo", + copts = ["-fno-addrsig"], + linker_script = "bunny.lds", + srcs = ["base.cpp"], +)`, + }, + }) +} + +func TestCcObjectDepsAndLinkerScriptSelects(t *testing.T) { + runCcObjectTestCase(t, bp2buildTestCase{ + description: "cc_object setting deps and linker_script across archs", + moduleTypeUnderTest: "cc_object", + moduleTypeUnderTestFactory: cc.ObjectFactory, + moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + blueprint: `cc_object { + name: "foo", + srcs: ["base.cpp"], + arch: { + x86: { + objs: ["x86_obj"], + linker_script: "x86.lds", + }, + x86_64: { + objs: ["x86_64_obj"], + linker_script: "x86_64.lds", + }, + arm: { + objs: ["arm_obj"], + linker_script: "arm.lds", + }, + }, + include_build_directory: false, +} + +cc_object { + name: "x86_obj", system_shared_libs: [], + srcs: ["x86.cpp"], + include_build_directory: false, + bazel_module: { bp2build_available: false }, +} + +cc_object { + name: "x86_64_obj", + system_shared_libs: [], + srcs: ["x86_64.cpp"], + include_build_directory: false, + bazel_module: { bp2build_available: false }, +} + +cc_object { + name: "arm_obj", + system_shared_libs: [], + srcs: ["arm.cpp"], + include_build_directory: false, + bazel_module: { bp2build_available: false }, +} +`, + expectedBazelTargets: []string{ + `cc_object( + name = "foo", + copts = ["-fno-addrsig"], + deps = select({ + "//build/bazel/platforms/arch:arm": [":arm_obj"], + "//build/bazel/platforms/arch:x86": [":x86_obj"], + "//build/bazel/platforms/arch:x86_64": [":x86_64_obj"], + "//conditions:default": [], + }), + linker_script = select({ + "//build/bazel/platforms/arch:arm": "arm.lds", + "//build/bazel/platforms/arch:x86": "x86.lds", + "//build/bazel/platforms/arch:x86_64": "x86_64.lds", + "//conditions:default": None, + }), + srcs = ["base.cpp"], +)`, + }, + }) +} + +func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) { + runCcObjectTestCase(t, bp2buildTestCase{ + description: "cc_object setting srcs based on linux and bionic archs", + moduleTypeUnderTest: "cc_object", + moduleTypeUnderTestFactory: cc.ObjectFactory, + moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, + blueprint: `cc_object { + name: "foo", srcs: ["base.cpp"], target: { - android: { - cflags: ["-fPIC"], + linux_arm64: { + srcs: ["linux_arm64.cpp",] }, - windows: { - cflags: ["-fPIC"], + linux_x86: { + srcs: ["linux_x86.cpp",] }, - darwin: { - cflags: ["-Wall"], + bionic_arm64: { + srcs: ["bionic_arm64.cpp",] }, }, include_build_directory: false, @@ -333,13 +436,21 @@ func TestCcObjectCflagsMultiOs(t *testing.T) { expectedBazelTargets: []string{ `cc_object( name = "foo", - copts = ["-fno-addrsig"] + select({ - "//build/bazel/platforms/os:android": ["-fPIC"], - "//build/bazel/platforms/os:darwin": ["-Wall"], - "//build/bazel/platforms/os:windows": ["-fPIC"], + copts = ["-fno-addrsig"], + srcs = ["base.cpp"] + select({ + "//build/bazel/platforms/os_arch:android_arm64": [ + "bionic_arm64.cpp", + "linux_arm64.cpp", + ], + "//build/bazel/platforms/os_arch:android_x86": ["linux_x86.cpp"], + "//build/bazel/platforms/os_arch:linux_bionic_arm64": [ + "bionic_arm64.cpp", + "linux_arm64.cpp", + ], + "//build/bazel/platforms/os_arch:linux_glibc_x86": ["linux_x86.cpp"], + "//build/bazel/platforms/os_arch:linux_musl_x86": ["linux_x86.cpp"], "//conditions:default": [], }), - srcs = ["base.cpp"], )`, }, }) diff --git a/cc/object.go b/cc/object.go index 99b257aaa..4ec2b19a2 100644 --- a/cc/object.go +++ b/cc/object.go @@ -122,14 +122,17 @@ func ObjectFactory() android.Module { // For bp2build conversion. type bazelObjectAttributes struct { - Srcs bazel.LabelListAttribute - Srcs_as bazel.LabelListAttribute - Hdrs bazel.LabelListAttribute - Deps bazel.LabelListAttribute - Copts bazel.StringListAttribute - Asflags bazel.StringListAttribute - Local_includes bazel.StringListAttribute - Absolute_includes bazel.StringListAttribute + Srcs bazel.LabelListAttribute + Srcs_as bazel.LabelListAttribute + Hdrs bazel.LabelListAttribute + Deps bazel.LabelListAttribute + System_dynamic_deps bazel.LabelListAttribute + Copts bazel.StringListAttribute + Asflags bazel.StringListAttribute + Local_includes bazel.StringListAttribute + Absolute_includes bazel.StringListAttribute + Stl *string + Linker_script bazel.LabelAttribute } // ObjectBp2Build is the bp2build converter from cc_object modules to the @@ -153,12 +156,26 @@ func ObjectBp2Build(ctx android.TopDownMutatorContext) { // Set arch-specific configurable attributes compilerAttrs := bp2BuildParseCompilerProps(ctx, m) var deps bazel.LabelListAttribute - for _, props := range m.linker.linkerProps() { - if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok { - deps = bazel.MakeLabelListAttribute( - android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs)) + systemDynamicDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true} + + var linkerScript bazel.LabelAttribute + + for axis, configToProps := range m.GetArchVariantProperties(ctx, &ObjectLinkerProperties{}) { + for config, props := range configToProps { + if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok { + if objectLinkerProps.Linker_script != nil { + linkerScript.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *objectLinkerProps.Linker_script)) + } + deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs)) + systemSharedLibs := objectLinkerProps.System_shared_libs + if len(systemSharedLibs) > 0 { + systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) + } + systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) + } } } + deps.ResolveExcludes() // Don't split cc_object srcs across languages. Doing so would add complexity, // and this isn't typically done for cc_object. @@ -172,13 +189,16 @@ func ObjectBp2Build(ctx android.TopDownMutatorContext) { } attrs := &bazelObjectAttributes{ - Srcs: srcs, - Srcs_as: compilerAttrs.asSrcs, - Deps: deps, - Copts: compilerAttrs.copts, - Asflags: asFlags, - Local_includes: compilerAttrs.localIncludes, - Absolute_includes: compilerAttrs.absoluteIncludes, + Srcs: srcs, + Srcs_as: compilerAttrs.asSrcs, + Deps: deps, + System_dynamic_deps: systemDynamicDeps, + Copts: compilerAttrs.copts, + Asflags: asFlags, + Local_includes: compilerAttrs.localIncludes, + Absolute_includes: compilerAttrs.absoluteIncludes, + Stl: compilerAttrs.stl, + Linker_script: linkerScript, } props := bazel.BazelTargetModuleProperties{