From 8ff105860d20f2ccbb8d8044bf562f94100b7f6a Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 7 Dec 2023 13:10:56 -0800 Subject: [PATCH] Remove ConvertWithBp2build implementations Remove the ConvertWithBp2build implementations from all the module types, along with the related code. Bug: 315353489 Test: m blueprint_tests Change-Id: I212672286686a318893bc7348ddd5a5ec51e77a7 --- aconfig/Android.bp | 1 - aconfig/aconfig_declarations.go | 26 - aconfig/aconfig_value_set.go | 23 - aconfig/aconfig_values.go | 27 - aconfig/codegen/cc_aconfig_library.go | 39 - aconfig/codegen/java_aconfig_library.go | 54 - aidl_library/aidl_library.go | 51 - android/defaults.go | 66 - android/filegroup.go | 293 - android/filegroup_test.go | 9 - android/license.go | 49 +- android/license_kind.go | 31 +- android/package.go | 47 +- android/proto.go | 279 - android/soong_config_modules.go | 57 +- apex/Android.bp | 2 - apex/androidmk.go | 29 +- apex/apex.go | 518 -- apex/bp2build.go | 36 - apex/bp2build_test.go | 545 -- apex/key.go | 42 - bp2build/Android.bp | 56 - bp2build/aar_conversion_test.go | 249 - bp2build/aconfig_conversion_test.go | 254 - bp2build/aidl_library_conversion_test.go | 119 - ...android_app_certificate_conversion_test.go | 49 - bp2build/android_app_conversion_test.go | 518 -- bp2build/android_test_conversion_test.go | 178 - bp2build/apex_conversion_test.go | 1562 ----- bp2build/apex_key_conversion_test.go | 101 - bp2build/bp2build_product_config_test.go | 154 - bp2build/bpf_conversion_test.go | 65 - bp2build/build_conversion_test.go | 2256 ------- bp2build/bzl_conversion_test.go | 241 - bp2build/cc_binary_conversion_test.go | 1309 ---- bp2build/cc_library_conversion_test.go | 5366 ----------------- .../cc_library_headers_conversion_test.go | 486 -- bp2build/cc_library_shared_conversion_test.go | 1656 ----- bp2build/cc_library_static_conversion_test.go | 2295 ------- bp2build/cc_object_conversion_test.go | 478 -- .../cc_prebuilt_binary_conversion_test.go | 125 - .../cc_prebuilt_library_conversion_test.go | 412 -- ...prebuilt_library_shared_conversion_test.go | 165 - bp2build/cc_prebuilt_library_shared_test.go | 85 - ...prebuilt_library_static_conversion_test.go | 199 - bp2build/cc_prebuilt_library_static_test.go | 157 - .../cc_prebuilt_object_conversion_test.go | 101 - bp2build/cc_test_conversion_test.go | 633 -- bp2build/cc_yasm_conversion_test.go | 179 - bp2build/conversion.go | 22 - bp2build/conversion_test.go | 147 - .../droiddoc_exported_dir_conversion_test.go | 60 - bp2build/fdo_profile_conversion_test.go | 85 - bp2build/filegroup_conversion_test.go | 205 - bp2build/genrule_conversion_test.go | 959 --- bp2build/gensrcs_conversion_test.go | 96 - bp2build/go_conversion_test.go | 208 - bp2build/java_binary_host_conversion_test.go | 338 -- .../java_host_for_device_conversion_test.go | 67 - bp2build/java_import_conversion_test.go | 157 - bp2build/java_library_conversion_test.go | 1131 ---- bp2build/java_library_host_conversion_test.go | 84 - bp2build/java_plugin_conversion_test.go | 108 - bp2build/java_proto_conversion_test.go | 238 - bp2build/java_sdk_library_conversion_test.go | 148 - ...java_sdk_library_import_conversion_test.go | 84 - bp2build/java_test_host_conversion_test.go | 156 - bp2build/license_conversion_test.go | 81 - bp2build/license_kind_conversion_test.go | 69 - bp2build/linker_config_conversion_test.go | 59 - bp2build/package_conversion_test.go | 139 - .../platform_compat_config_conversion_test.go | 54 - bp2build/prebuilt_etc_conversion_test.go | 396 -- bp2build/python_binary_conversion_test.go | 320 - bp2build/python_library_conversion_test.go | 392 -- bp2build/python_test_conversion_test.go | 67 - bp2build/rust_binary_conversion_test.go | 88 - bp2build/rust_ffi_conversion_test.go | 78 - bp2build/rust_library_conversion_test.go | 109 - bp2build/rust_proc_macro_conversion_test.go | 76 - bp2build/rust_protobuf_conversion_test.go | 89 - bp2build/sh_conversion_test.go | 92 - bp2build/sh_test_conversion_test.go | 340 -- ...oong_config_module_type_conversion_test.go | 1608 ----- bpf/bpf.go | 66 - bpf/bpf_test.go | 23 - cc/Android.bp | 3 - cc/afdo.go | 10 - cc/binary.go | 163 +- cc/binary_test.go | 80 - cc/bp2build.go | 2127 ------- cc/cc.go | 268 +- cc/cc_test.go | 698 --- cc/config/global.go | 6 - cc/fdo_profile.go | 49 - cc/fuzz.go | 2 +- cc/gen.go | 67 - cc/generated_cc_library.go | 3 - cc/genrule.go | 1 - cc/library.go | 763 +-- cc/library_headers.go | 95 - cc/library_sdk_member.go | 9 +- cc/library_test.go | 233 - cc/linkable.go | 17 - cc/ndk_headers.go | 52 +- cc/ndk_library.go | 47 - cc/ndk_prebuilt.go | 82 - cc/object.go | 129 - cc/object_test.go | 24 - cc/prebuilt.go | 323 +- cc/prebuilt_test.go | 309 - cc/proto.go | 107 - cc/rs.go | 33 - cc/sanitize.go | 4 - cc/stl.go | 4 - cc/strip.go | 1 - cc/test.go | 192 +- cc/tidy.go | 1 - etc/prebuilt_etc.go | 161 - etc/prebuilt_etc_test.go | 39 - genrule/Android.bp | 1 - genrule/genrule.go | 259 - genrule/genrule_test.go | 25 - java/Android.bp | 1 - java/aar.go | 190 - java/app.go | 264 - java/base.go | 53 - java/config/config.go | 4 - java/device_host_converter.go | 34 - java/droiddoc.go | 25 - java/droidstubs.go | 23 - java/droidstubs_test.go | 42 - java/gen.go | 14 +- java/generated_java_library.go | 7 - java/generated_java_library_test.go | 3 - java/genrule.go | 2 - java/java.go | 703 --- java/java_test.go | 118 +- java/platform_compat_config.go | 23 - java/plugin.go | 38 - java/prebuilt_apis.go | 42 +- java/proto.go | 79 - java/sdk_library.go | 72 - linkerconfig/linkerconfig.go | 27 - python/Android.bp | 1 - python/binary.go | 1 - python/bp2build.go | 240 - python/python.go | 10 - python/test.go | 1 - rust/binary.go | 90 - rust/config/global.go | 7 - rust/library.go | 186 - rust/proc_macro.go | 67 - rust/protobuf.go | 75 - rust/rust.go | 47 - sh/sh_binary.go | 126 +- sysprop/Android.bp | 3 - sysprop/bp2build/Android.bp | 16 - sysprop/bp2build/bp2build.go | 106 - sysprop/sysprop_library.go | 56 - sysprop/sysprop_library_conversion_test.go | 195 - testing/code_metadata.go | 1 - testing/test_spec.go | 1 - tradefed/Android.bp | 1 - tradefed/autogen_bazel.go | 111 - xml/Android.bp | 2 - xml/xml.go | 42 - xml/xml_conversion_test.go | 129 - 168 files changed, 64 insertions(+), 39712 deletions(-) delete mode 100644 apex/bp2build.go delete mode 100644 apex/bp2build_test.go delete mode 100644 bp2build/aar_conversion_test.go delete mode 100644 bp2build/aconfig_conversion_test.go delete mode 100644 bp2build/aidl_library_conversion_test.go delete mode 100644 bp2build/android_app_certificate_conversion_test.go delete mode 100644 bp2build/android_app_conversion_test.go delete mode 100644 bp2build/android_test_conversion_test.go delete mode 100644 bp2build/apex_conversion_test.go delete mode 100644 bp2build/apex_key_conversion_test.go delete mode 100644 bp2build/bp2build_product_config_test.go delete mode 100644 bp2build/bpf_conversion_test.go delete mode 100644 bp2build/build_conversion_test.go delete mode 100644 bp2build/bzl_conversion_test.go delete mode 100644 bp2build/cc_binary_conversion_test.go delete mode 100644 bp2build/cc_library_conversion_test.go delete mode 100644 bp2build/cc_library_headers_conversion_test.go delete mode 100644 bp2build/cc_library_shared_conversion_test.go delete mode 100644 bp2build/cc_library_static_conversion_test.go delete mode 100644 bp2build/cc_object_conversion_test.go delete mode 100644 bp2build/cc_prebuilt_binary_conversion_test.go delete mode 100644 bp2build/cc_prebuilt_library_conversion_test.go delete mode 100644 bp2build/cc_prebuilt_library_shared_conversion_test.go delete mode 100644 bp2build/cc_prebuilt_library_shared_test.go delete mode 100644 bp2build/cc_prebuilt_library_static_conversion_test.go delete mode 100644 bp2build/cc_prebuilt_library_static_test.go delete mode 100644 bp2build/cc_prebuilt_object_conversion_test.go delete mode 100644 bp2build/cc_test_conversion_test.go delete mode 100644 bp2build/cc_yasm_conversion_test.go delete mode 100644 bp2build/droiddoc_exported_dir_conversion_test.go delete mode 100644 bp2build/fdo_profile_conversion_test.go delete mode 100644 bp2build/filegroup_conversion_test.go delete mode 100644 bp2build/genrule_conversion_test.go delete mode 100644 bp2build/gensrcs_conversion_test.go delete mode 100644 bp2build/go_conversion_test.go delete mode 100644 bp2build/java_binary_host_conversion_test.go delete mode 100644 bp2build/java_host_for_device_conversion_test.go delete mode 100644 bp2build/java_import_conversion_test.go delete mode 100644 bp2build/java_library_conversion_test.go delete mode 100644 bp2build/java_library_host_conversion_test.go delete mode 100644 bp2build/java_plugin_conversion_test.go delete mode 100644 bp2build/java_proto_conversion_test.go delete mode 100644 bp2build/java_sdk_library_conversion_test.go delete mode 100644 bp2build/java_sdk_library_import_conversion_test.go delete mode 100644 bp2build/java_test_host_conversion_test.go delete mode 100644 bp2build/license_conversion_test.go delete mode 100644 bp2build/license_kind_conversion_test.go delete mode 100644 bp2build/linker_config_conversion_test.go delete mode 100644 bp2build/package_conversion_test.go delete mode 100644 bp2build/platform_compat_config_conversion_test.go delete mode 100644 bp2build/prebuilt_etc_conversion_test.go delete mode 100644 bp2build/python_binary_conversion_test.go delete mode 100644 bp2build/python_library_conversion_test.go delete mode 100644 bp2build/python_test_conversion_test.go delete mode 100644 bp2build/rust_binary_conversion_test.go delete mode 100644 bp2build/rust_ffi_conversion_test.go delete mode 100644 bp2build/rust_library_conversion_test.go delete mode 100644 bp2build/rust_proc_macro_conversion_test.go delete mode 100644 bp2build/rust_protobuf_conversion_test.go delete mode 100644 bp2build/sh_conversion_test.go delete mode 100644 bp2build/sh_test_conversion_test.go delete mode 100644 bp2build/soong_config_module_type_conversion_test.go delete mode 100644 cc/bp2build.go delete mode 100644 python/bp2build.go delete mode 100644 sysprop/bp2build/Android.bp delete mode 100644 sysprop/bp2build/bp2build.go delete mode 100644 sysprop/sysprop_library_conversion_test.go delete mode 100644 tradefed/autogen_bazel.go delete mode 100644 xml/xml_conversion_test.go diff --git a/aconfig/Android.bp b/aconfig/Android.bp index 04de9198a..fed419005 100644 --- a/aconfig/Android.bp +++ b/aconfig/Android.bp @@ -11,7 +11,6 @@ bootstrap_go_package { "sbox_proto", "soong", "soong-android", - "soong-bazel", ], srcs: [ "aconfig_declarations.go", diff --git a/aconfig/aconfig_declarations.go b/aconfig/aconfig_declarations.go index 9bf75649a..593ccc067 100644 --- a/aconfig/aconfig_declarations.go +++ b/aconfig/aconfig_declarations.go @@ -19,7 +19,6 @@ import ( "strings" "android/soong/android" - "android/soong/bazel" "github.com/google/blueprint" ) @@ -27,7 +26,6 @@ import ( type DeclarationsModule struct { android.ModuleBase android.DefaultableModuleBase - android.BazelModuleBase // Properties for "aconfig_declarations" properties struct { @@ -53,7 +51,6 @@ func DeclarationsFactory() android.Module { android.InitAndroidModule(module) android.InitDefaultableModule(module) module.AddProperties(&module.properties) - android.InitBazelModule(module) return module } @@ -232,26 +229,3 @@ func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android. return android.Paths{output} } - -type bazelAconfigDeclarationsAttributes struct { - Srcs bazel.LabelListAttribute - Package string -} - -func (module *DeclarationsModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - if ctx.ModuleType() != "aconfig_declarations" { - return - } - srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs)) - - attrs := bazelAconfigDeclarationsAttributes{ - Srcs: srcs, - Package: module.properties.Package, - } - props := bazel.BazelTargetModuleProperties{ - Rule_class: "aconfig_declarations", - Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl", - } - - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) -} diff --git a/aconfig/aconfig_value_set.go b/aconfig/aconfig_value_set.go index cd178d480..94c322a42 100644 --- a/aconfig/aconfig_value_set.go +++ b/aconfig/aconfig_value_set.go @@ -16,7 +16,6 @@ package aconfig import ( "android/soong/android" - "android/soong/bazel" "github.com/google/blueprint" ) @@ -24,7 +23,6 @@ import ( type ValueSetModule struct { android.ModuleBase android.DefaultableModuleBase - android.BazelModuleBase properties struct { // aconfig_values modules @@ -38,7 +36,6 @@ func ValueSetFactory() android.Module { android.InitAndroidModule(module) android.InitDefaultableModule(module) module.AddProperties(&module.properties) - android.InitBazelModule(module) return module } @@ -91,23 +88,3 @@ func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleCont AvailablePackages: packages, }) } - -type bazelAconfigValueSetAttributes struct { - Values bazel.LabelListAttribute -} - -func (module *ValueSetModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - if ctx.ModuleType() != "aconfig_value_set" { - return - } - - attrs := bazelAconfigValueSetAttributes{ - Values: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, module.properties.Values)), - } - props := bazel.BazelTargetModuleProperties{ - Rule_class: "aconfig_value_set", - Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_value_set.bzl", - } - - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) -} diff --git a/aconfig/aconfig_values.go b/aconfig/aconfig_values.go index 03a930dbd..621aae8cf 100644 --- a/aconfig/aconfig_values.go +++ b/aconfig/aconfig_values.go @@ -16,7 +16,6 @@ package aconfig import ( "android/soong/android" - "android/soong/bazel" "github.com/google/blueprint" ) @@ -24,7 +23,6 @@ import ( type ValuesModule struct { android.ModuleBase android.DefaultableModuleBase - android.BazelModuleBase properties struct { // aconfig files, relative to this Android.bp file @@ -41,7 +39,6 @@ func ValuesFactory() android.Module { android.InitAndroidModule(module) android.InitDefaultableModule(module) module.AddProperties(&module.properties) - android.InitBazelModule(module) return module } @@ -69,27 +66,3 @@ func (module *ValuesModule) GenerateAndroidBuildActions(ctx android.ModuleContex } ctx.SetProvider(valuesProviderKey, providerData) } - -type bazelAconfigValuesAttributes struct { - Srcs bazel.LabelListAttribute - Package string -} - -func (module *ValuesModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - if ctx.ModuleType() != "aconfig_values" { - return - } - - srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs)) - - attrs := bazelAconfigValuesAttributes{ - Srcs: srcs, - Package: module.properties.Package, - } - props := bazel.BazelTargetModuleProperties{ - Rule_class: "aconfig_values", - Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_values.bzl", - } - - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) -} diff --git a/aconfig/codegen/cc_aconfig_library.go b/aconfig/codegen/cc_aconfig_library.go index 68551187b..30f6863a0 100644 --- a/aconfig/codegen/cc_aconfig_library.go +++ b/aconfig/codegen/cc_aconfig_library.go @@ -17,7 +17,6 @@ package codegen import ( "android/soong/aconfig" "android/soong/android" - "android/soong/bazel" "android/soong/cc" "github.com/google/blueprint" @@ -144,41 +143,3 @@ func (this *CcAconfigLibraryCallbacks) GeneratorBuildActions(ctx cc.ModuleContex }, }) } - -type bazelCcAconfigLibraryAttributes struct { - cc.SdkAttributes - Aconfig_declarations bazel.LabelAttribute - Dynamic_deps bazel.LabelListAttribute -} - -// Convert the cc_aconfig_library module to bazel. -// -// This method is called from cc.ConvertWithBp2build to actually convert the -// cc_aconfig_library module. This is necessary since the factory method of this -// module type returns a cc library and the bp2build conversion is called on the -// cc library type. - -func (this *CcAconfigLibraryCallbacks) GeneratorBp2build(ctx android.Bp2buildMutatorContext, module *cc.Module) bool { - if ctx.ModuleType() != "cc_aconfig_library" { - return false - } - - attrs := bazelCcAconfigLibraryAttributes{ - SdkAttributes: cc.Bp2BuildParseSdkAttributes(module), - Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, this.properties.Aconfig_declarations).Label), - Dynamic_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, []string{baseLibDep})), - } - props := bazel.BazelTargetModuleProperties{ - Rule_class: "cc_aconfig_library", - Bzl_load_location: "//build/bazel/rules/cc:cc_aconfig_library.bzl", - } - - ctx.CreateBazelTargetModule( - props, - android.CommonAttributes{ - Name: ctx.ModuleName(), - Tags: android.ApexAvailableTagsWithoutTestApexes(ctx, module), - }, - &attrs) - return true -} diff --git a/aconfig/codegen/java_aconfig_library.go b/aconfig/codegen/java_aconfig_library.go index 211bdce90..202e358ee 100644 --- a/aconfig/codegen/java_aconfig_library.go +++ b/aconfig/codegen/java_aconfig_library.go @@ -19,7 +19,6 @@ import ( "android/soong/aconfig" "android/soong/android" - "android/soong/bazel" "android/soong/java" "github.com/google/blueprint" @@ -101,56 +100,3 @@ func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuild func isModeSupported(mode string) bool { return android.InList(mode, aconfigSupportedModes) } - -type bazelJavaAconfigLibraryAttributes struct { - Aconfig_declarations bazel.LabelAttribute - Sdk_version *string - Libs bazel.LabelListAttribute -} - -func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) Bp2build(ctx android.Bp2buildMutatorContext, module *java.GeneratedJavaLibraryModule) { - if ctx.ModuleType() != "java_aconfig_library" { - return - } - - // By default, soong builds the aconfig java library with private_current, however - // bazel currently doesn't support it so we default it to system_current. One reason - // is that the dependency of all java_aconfig_library aconfig-annotations-lib is - // built with system_current. For the java aconfig library itself it doesn't really - // matter whether it uses private API or system API because the only module it uses - // is DeviceConfig which is in system, and the rdeps of the java aconfig library - // won't change its sdk version either, so this should be fine. - // Ideally we should only use the default value if it is not set by the user, but - // bazel only supports a limited sdk versions, for example, the java_aconfig_library - // modules in framework/base use core_platform which is not supported by bazel yet. - // TODO(b/302148527): change soong to default to system_current as well. - sdkVersion := "system_current" - - var libs bazel.LabelListAttribute - archVariantProps := module.GetArchVariantProperties(ctx, &java.CommonProperties{}) - for axis, configToProps := range archVariantProps { - for config, p := range configToProps { - if archProps, ok := p.(*java.CommonProperties); ok { - var libLabels []bazel.Label - for _, d := range archProps.Libs { - neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d) - neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink" - libLabels = append(libLabels, neverlinkLabel) - } - libs.SetSelectValue(axis, config, (bazel.MakeLabelList(libLabels))) - } - } - } - - attrs := bazelJavaAconfigLibraryAttributes{ - Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, callbacks.properties.Aconfig_declarations).Label), - Sdk_version: &sdkVersion, - Libs: libs, - } - props := bazel.BazelTargetModuleProperties{ - Rule_class: "java_aconfig_library", - Bzl_load_location: "//build/bazel/rules/java:java_aconfig_library.bzl", - } - - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: ctx.ModuleName()}, &attrs) -} diff --git a/aidl_library/aidl_library.go b/aidl_library/aidl_library.go index 2c0aef77f..b49c51619 100644 --- a/aidl_library/aidl_library.go +++ b/aidl_library/aidl_library.go @@ -16,8 +16,6 @@ package aidl_library import ( "android/soong/android" - "android/soong/bazel" - "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) @@ -53,57 +51,9 @@ type aidlLibraryProperties struct { type AidlLibrary struct { android.ModuleBase - android.BazelModuleBase properties aidlLibraryProperties } -type bazelAidlLibraryAttributes struct { - Srcs bazel.LabelListAttribute - Hdrs bazel.LabelListAttribute - Strip_import_prefix *string - Deps bazel.LabelListAttribute -} - -func (lib *AidlLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - srcs := bazel.MakeLabelListAttribute( - android.BazelLabelForModuleSrc( - ctx, - lib.properties.Srcs, - ), - ) - - hdrs := bazel.MakeLabelListAttribute( - android.BazelLabelForModuleSrc( - ctx, - lib.properties.Hdrs, - ), - ) - - tags := []string{"apex_available=//apex_available:anyapex"} - deps := bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, lib.properties.Deps)) - - attrs := &bazelAidlLibraryAttributes{ - Srcs: srcs, - Hdrs: hdrs, - Strip_import_prefix: lib.properties.Strip_import_prefix, - Deps: deps, - } - - props := bazel.BazelTargetModuleProperties{ - Rule_class: "aidl_library", - Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl", - } - - ctx.CreateBazelTargetModule( - props, - android.CommonAttributes{ - Name: lib.Name(), - Tags: bazel.MakeStringListAttribute(tags), - }, - attrs, - ) -} - type AidlLibraryInfo struct { // The direct aidl files of the module Srcs android.Paths @@ -170,7 +120,6 @@ func AidlLibraryFactory() android.Module { module := &AidlLibrary{} module.AddProperties(&module.properties) android.InitAndroidModule(module) - android.InitBazelModule(module) return module } diff --git a/android/defaults.go b/android/defaults.go index cc723f728..ff7900232 100644 --- a/android/defaults.go +++ b/android/defaults.go @@ -17,7 +17,6 @@ package android import ( "reflect" - "android/soong/ui/metrics/bp2build_metrics_proto" "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) @@ -118,11 +117,6 @@ type DefaultsVisibilityProperties struct { type DefaultsModuleBase struct { DefaultableModuleBase - - // Included to support setting bazel_module.label for multiple Soong modules to the same Bazel - // target. This is primarily useful for modules that were architecture specific and instead are - // handled in Bazel as a select(). - BazelModuleBase } // The common pattern for defaults modules is to register separate instances of @@ -165,7 +159,6 @@ func (d *DefaultsModuleBase) isDefaults() bool { type DefaultsModule interface { Module Defaults - Bazelable } func (d *DefaultsModuleBase) properties() []interface{} { @@ -178,13 +171,6 @@ func (d *DefaultsModuleBase) productVariableProperties() interface{} { func (d *DefaultsModuleBase) GenerateAndroidBuildActions(ctx ModuleContext) {} -// ConvertWithBp2build to fulfill Bazelable interface; however, at this time defaults module are -// *NOT* converted with bp2build -func (defaultable *DefaultsModuleBase) ConvertWithBp2build(ctx Bp2buildMutatorContext) { - // Defaults types are never convertible. - ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "") -} - func InitDefaultsModule(module DefaultsModule) { commonProperties := &commonProperties{} @@ -194,8 +180,6 @@ func InitDefaultsModule(module DefaultsModule) { &ApexProperties{}, &distProperties{}) - // Bazel module must be initialized _before_ Defaults to be included in cc_defaults module. - InitBazelModule(module) initAndroidModuleBase(module) initProductVariableModule(module) initArchModule(module) @@ -225,60 +209,10 @@ func InitDefaultsModule(module DefaultsModule) { var _ Defaults = (*DefaultsModuleBase)(nil) -// applyNamespacedVariableDefaults only runs in bp2build mode for -// defaultable/defaults modules. Its purpose is to merge namespaced product -// variable props from defaults deps, even if those defaults are custom module -// types created from soong_config_module_type, e.g. one that's wrapping a -// cc_defaults or java_defaults. -func applyNamespacedVariableDefaults(defaultDep Defaults, ctx TopDownMutatorContext) { - var dep, b Bazelable - - dep, ok := defaultDep.(Bazelable) - if !ok { - if depMod, ok := defaultDep.(Module); ok { - // Track that this dependency hasn't been converted to bp2build yet. - ctx.AddUnconvertedBp2buildDep(depMod.Name()) - return - } else { - panic("Expected default dep to be a Module.") - } - } - - b, ok = ctx.Module().(Bazelable) - if !ok { - return - } - - // namespacedVariableProps is a map from namespaces (e.g. acme, android, - // vendor_foo) to a slice of soong_config_variable struct pointers, - // containing properties for that particular module. - src := dep.namespacedVariableProps() - dst := b.namespacedVariableProps() - if dst == nil { - dst = make(namespacedVariableProperties) - } - - // Propagate all soong_config_variable structs from the dep. We'll merge the - // actual property values later in variable.go. - for namespace := range src { - if dst[namespace] == nil { - dst[namespace] = []interface{}{} - } - for _, i := range src[namespace] { - dst[namespace] = append(dst[namespace], i) - } - } - - b.setNamespacedVariableProps(dst) -} - func (defaultable *DefaultableModuleBase) applyDefaults(ctx TopDownMutatorContext, defaultsList []Defaults) { for _, defaults := range defaultsList { - if ctx.Config().BuildMode == Bp2build { - applyNamespacedVariableDefaults(defaults, ctx) - } for _, prop := range defaultable.defaultableProperties { if prop == defaultable.defaultableVariableProperties { defaultable.applyDefaultVariableProperties(ctx, defaults, prop) diff --git a/android/filegroup.go b/android/filegroup.go index c34a615e5..04bd8a8ad 100644 --- a/android/filegroup.go +++ b/android/filegroup.go @@ -15,15 +15,9 @@ package android import ( - "path/filepath" - "regexp" "strings" - "android/soong/bazel" - "android/soong/bazel/cquery" - "android/soong/ui/metrics/bp2build_metrics_proto" "github.com/google/blueprint" - "github.com/google/blueprint/proptools" ) func init() { @@ -39,181 +33,6 @@ func RegisterFilegroupBuildComponents(ctx RegistrationContext) { ctx.RegisterModuleType("filegroup_defaults", FileGroupDefaultsFactory) } -var convertedProtoLibrarySuffix = "_bp2build_converted" - -// IsFilegroup checks that a module is a filegroup type -func IsFilegroup(ctx bazel.OtherModuleContext, m blueprint.Module) bool { - return ctx.OtherModuleType(m) == "filegroup" -} - -var ( - // ignoring case, checks for proto or protos as an independent word in the name, whether at the - // beginning, end, or middle. e.g. "proto.foo", "bar-protos", "baz_proto_srcs" would all match - filegroupLikelyProtoPattern = regexp.MustCompile("(?i)(^|[^a-z])proto(s)?([^a-z]|$)") - filegroupLikelyAidlPattern = regexp.MustCompile("(?i)(^|[^a-z])aidl(s)?([^a-z]|$)") - - ProtoSrcLabelPartition = bazel.LabelPartition{ - Extensions: []string{".proto"}, - LabelMapper: isFilegroupWithPattern(filegroupLikelyProtoPattern), - } - AidlSrcLabelPartition = bazel.LabelPartition{ - Extensions: []string{".aidl"}, - LabelMapper: isFilegroupWithPattern(filegroupLikelyAidlPattern), - } -) - -func isFilegroupWithPattern(pattern *regexp.Regexp) bazel.LabelMapper { - return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { - m, exists := ctx.ModuleFromName(label.OriginalModuleName) - labelStr := label.Label - if !exists || !IsFilegroup(ctx, m) { - return labelStr, false - } - likelyMatched := pattern.MatchString(label.OriginalModuleName) - return labelStr, likelyMatched - } -} - -// https://docs.bazel.build/versions/master/be/general.html#filegroup -type bazelFilegroupAttributes struct { - Srcs bazel.LabelListAttribute - Applicable_licenses bazel.LabelListAttribute -} - -type bazelAidlLibraryAttributes struct { - Srcs bazel.LabelListAttribute - Strip_import_prefix *string -} - -// ConvertWithBp2build performs bp2build conversion of filegroup -func (fg *fileGroup) ConvertWithBp2build(ctx Bp2buildMutatorContext) { - srcs := bazel.MakeLabelListAttribute( - BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs)) - - // For Bazel compatibility, don't generate the filegroup if there is only 1 - // source file, and that the source file is named the same as the module - // itself. In Bazel, eponymous filegroups like this would be an error. - // - // Instead, dependents on this single-file filegroup can just depend - // on the file target, instead of rule target, directly. - // - // You may ask: what if a filegroup has multiple files, and one of them - // shares the name? The answer: we haven't seen that in the wild, and - // should lock Soong itself down to prevent the behavior. For now, - // we raise an error if bp2build sees this problem. - for _, f := range srcs.Value.Includes { - if f.Label == fg.Name() { - if len(srcs.Value.Includes) > 1 { - ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name()) - ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_SRC_NAME_COLLISION, "") - } else { - panic("This situation should have been handled by FileGroupFactory's call to InitBazelModuleAsHandcrafted") - } - return - } - } - - // Convert module that has only AIDL files to aidl_library - // If the module has a mixed bag of AIDL and non-AIDL files, split the filegroup manually - // and then convert - if fg.ShouldConvertToAidlLibrary(ctx) { - tags := []string{"apex_available=//apex_available:anyapex"} - attrs := &bazelAidlLibraryAttributes{ - Srcs: srcs, - Strip_import_prefix: fg.properties.Path, - } - - props := bazel.BazelTargetModuleProperties{ - Rule_class: "aidl_library", - Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl", - } - - ctx.CreateBazelTargetModule( - props, - CommonAttributes{ - Name: fg.Name(), - Tags: bazel.MakeStringListAttribute(tags), - }, - attrs) - } else { - if fg.ShouldConvertToProtoLibrary(ctx) { - pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), bazel.MakeLabelList(srcs.Value.Includes)) - if len(pkgToSrcs) > 1 { - ctx.ModuleErrorf("TODO: Add bp2build support for multiple package .protosrcs in filegroup") - return - } - pkg := SortedKeys(pkgToSrcs)[0] - attrs := &ProtoAttrs{ - Srcs: bazel.MakeLabelListAttribute(pkgToSrcs[pkg]), - Strip_import_prefix: fg.properties.Path, - } - - tags := []string{ - "apex_available=//apex_available:anyapex", - // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug. - "manual", - } - if pkg != ctx.ModuleDir() { - // Since we are creating the proto_library in a subpackage, create an import_prefix relative to the current package - if rel, err := filepath.Rel(ctx.ModuleDir(), pkg); err != nil { - ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err) - } else if rel != "." { - attrs.Import_prefix = &rel - // Strip the package prefix - attrs.Strip_import_prefix = proptools.StringPtr("") - } - } - - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, - CommonAttributes{ - Name: fg.Name() + "_proto", - Dir: proptools.StringPtr(pkg), - Tags: bazel.MakeStringListAttribute(tags), - }, - attrs) - - // Create an alias in the current dir. The actual target might exist in a different package, but rdeps - // can reliabily use this alias - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{Rule_class: "alias"}, - CommonAttributes{ - Name: fg.Name() + convertedProtoLibrarySuffix, - // TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug. - Tags: bazel.MakeStringListAttribute(tags), - }, - &bazelAliasAttributes{ - Actual: bazel.MakeLabelAttribute("//" + pkg + ":" + fg.Name() + "_proto"), - }, - ) - } - - // TODO(b/242847534): Still convert to a filegroup because other unconverted - // modules may depend on the filegroup - attrs := &bazelFilegroupAttributes{ - Srcs: srcs, - } - - props := bazel.BazelTargetModuleProperties{ - Rule_class: "filegroup", - Bzl_load_location: "//build/bazel/rules:filegroup.bzl", - } - - ctx.CreateBazelTargetModule(props, CommonAttributes{Name: fg.Name()}, attrs) - } -} - -type FileGroupPath interface { - GetPath(ctx Bp2buildMutatorContext) string -} - -func (fg *fileGroup) GetPath(ctx Bp2buildMutatorContext) string { - if fg.properties.Path != nil { - return *fg.properties.Path - } - return "" -} - type fileGroupProperties struct { // srcs lists files that will be included in this filegroup Srcs []string `android:"path"` @@ -233,18 +52,12 @@ type fileGroupProperties struct { type fileGroup struct { ModuleBase - BazelModuleBase DefaultableModuleBase - FileGroupAsLibrary - FileGroupPath properties fileGroupProperties srcs Paths } -var _ MixedBuildBuildable = (*fileGroup)(nil) var _ SourceFileProducer = (*fileGroup)(nil) -var _ FileGroupAsLibrary = (*fileGroup)(nil) -var _ FileGroupPath = (*fileGroup)(nil) // filegroup contains a list of files that are referenced by other modules // properties (such as "srcs") using the syntax ":". filegroup are @@ -253,17 +66,6 @@ func FileGroupFactory() Module { module := &fileGroup{} module.AddProperties(&module.properties) InitAndroidModule(module) - InitBazelModule(module) - AddBazelHandcraftedHook(module, func(ctx LoadHookContext) string { - // If there is a single src with the same name as the filegroup module name, - // then don't generate this filegroup. It will be OK for other targets - // to depend on this source file by name directly. - fg := ctx.Module().(*fileGroup) - if len(fg.properties.Srcs) == 1 && fg.Name() == fg.properties.Srcs[0] { - return fg.Name() - } - return "" - }) InitDefaultableModule(module) return module } @@ -303,101 +105,6 @@ func (fg *fileGroup) MakeVars(ctx MakeVarsModuleContext) { } } -func (fg *fileGroup) QueueBazelCall(ctx BaseModuleContext) { - bazelCtx := ctx.Config().BazelContext - - bazelCtx.QueueBazelRequest( - fg.GetBazelLabel(ctx, fg), - cquery.GetOutputFiles, - configKey{arch: Common.String(), osType: CommonOS}) -} - -func (fg *fileGroup) IsMixedBuildSupported(ctx BaseModuleContext) bool { - // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups - return false -} - -func (fg *fileGroup) ProcessBazelQueryResponse(ctx ModuleContext) { - bazelCtx := ctx.Config().BazelContext - // This is a short-term solution because we rely on info from Android.bp to handle - // a converted module. This will block when we want to remove Android.bp for all - // converted modules at some point. - // TODO(b/242847534): Implement a long-term solution in which we don't need to rely - // on info form Android.bp for modules that are already converted to Bazel - relativeRoot := ctx.ModuleDir() - if fg.properties.Path != nil { - relativeRoot = filepath.Join(relativeRoot, *fg.properties.Path) - } - - filePaths, err := bazelCtx.GetOutputFiles(fg.GetBazelLabel(ctx, fg), configKey{arch: Common.String(), osType: CommonOS}) - if err != nil { - ctx.ModuleErrorf(err.Error()) - return - } - - bazelOuts := make(Paths, 0, len(filePaths)) - for _, p := range filePaths { - bazelOuts = append(bazelOuts, PathForBazelOutRelative(ctx, relativeRoot, p)) - } - fg.srcs = bazelOuts -} - -func (fg *fileGroup) ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool { - return fg.shouldConvertToLibrary(ctx, ".aidl") -} - -func (fg *fileGroup) ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool { - return fg.shouldConvertToLibrary(ctx, ".proto") -} - -func (fg *fileGroup) shouldConvertToLibrary(ctx BazelConversionPathContext, suffix string) bool { - if len(fg.properties.Srcs) == 0 || !fg.ShouldConvertWithBp2build(ctx) { - return false - } - for _, src := range fg.properties.Srcs { - if !strings.HasSuffix(src, suffix) { - return false - } - } - return true -} - -func (fg *fileGroup) GetAidlLibraryLabel(ctx BazelConversionPathContext) string { - return fg.getFileGroupAsLibraryLabel(ctx) -} - -func (fg *fileGroup) GetProtoLibraryLabel(ctx BazelConversionPathContext) string { - return fg.getFileGroupAsLibraryLabel(ctx) + convertedProtoLibrarySuffix -} - -func (fg *fileGroup) getFileGroupAsLibraryLabel(ctx BazelConversionPathContext) string { - if ctx.OtherModuleDir(fg.module) == ctx.ModuleDir() { - return ":" + fg.Name() - } else { - return fg.GetBazelLabel(ctx, fg) - } -} - -// Given a name in srcs prop, check to see if the name references a filegroup -// and the filegroup is converted to aidl_library -func IsConvertedToAidlLibrary(ctx BazelConversionPathContext, name string) bool { - if fg, ok := ToFileGroupAsLibrary(ctx, name); ok { - return fg.ShouldConvertToAidlLibrary(ctx) - } - return false -} - -func ToFileGroupAsLibrary(ctx BazelConversionPathContext, name string) (FileGroupAsLibrary, bool) { - if module, ok := ctx.ModuleFromName(name); ok { - if IsFilegroup(ctx, module) { - if fg, ok := module.(FileGroupAsLibrary); ok { - return fg, true - } - } - } - return nil, false -} - // Defaults type FileGroupDefaults struct { ModuleBase diff --git a/android/filegroup_test.go b/android/filegroup_test.go index 893da5774..14e9368ca 100644 --- a/android/filegroup_test.go +++ b/android/filegroup_test.go @@ -40,17 +40,8 @@ func TestFileGroupWithPathProp(t *testing.T) { } for _, testCase := range testCases { - outBaseDir := "outputbase" result := GroupFixturePreparers( PrepareForTestWithFilegroup, - FixtureModifyConfig(func(config Config) { - config.BazelContext = MockBazelContext{ - OutputBaseDir: outBaseDir, - LabelToOutputFiles: map[string][]string{ - "//:baz": []string{"a/b/c/d/test.aidl"}, - }, - } - }), ).RunTestWithBp(t, testCase.bp) fg := result.Module("baz", "").(*fileGroup) diff --git a/android/license.go b/android/license.go index 76f5115f5..5bffc2519 100644 --- a/android/license.go +++ b/android/license.go @@ -15,12 +15,7 @@ package android import ( - "fmt" - "os" - "github.com/google/blueprint" - - "android/soong/bazel" ) type licenseKindDependencyTag struct { @@ -53,54 +48,13 @@ type licenseProperties struct { Visibility []string } -var _ Bazelable = &licenseModule{} - type licenseModule struct { ModuleBase DefaultableModuleBase - BazelModuleBase properties licenseProperties } -type bazelLicenseAttributes struct { - License_kinds []string - Copyright_notice *string - License_text bazel.LabelAttribute - Package_name *string - Visibility []string -} - -func (m *licenseModule) ConvertWithBp2build(ctx Bp2buildMutatorContext) { - attrs := &bazelLicenseAttributes{ - License_kinds: m.properties.License_kinds, - Copyright_notice: m.properties.Copyright_notice, - Package_name: m.properties.Package_name, - Visibility: m.properties.Visibility, - } - - // TODO(asmundak): Soong supports multiple license texts while Bazel's license - // rule does not. Have android_license create a genrule to concatenate multiple - // license texts. - if len(m.properties.License_text) > 1 && ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") { - fmt.Fprintf(os.Stderr, "warning: using only the first license_text item from //%s:%s\n", - ctx.ModuleDir(), m.Name()) - } - if len(m.properties.License_text) >= 1 { - attrs.License_text.SetValue(BazelLabelForModuleSrcSingle(ctx, m.properties.License_text[0])) - } - - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{ - Rule_class: "android_license", - Bzl_load_location: "//build/bazel/rules/license:license.bzl", - }, - CommonAttributes{ - Name: m.Name(), - }, - attrs) -} - func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) { for i, license := range m.properties.License_kinds { for j := i + 1; j < len(m.properties.License_kinds); j++ { @@ -131,14 +85,13 @@ func LicenseFactory() Module { module := &licenseModule{} base := module.base() - module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus) + module.AddProperties(&base.nameProperties, &module.properties) // The visibility property needs to be checked and parsed by the visibility module. setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility) initAndroidModuleBase(module) InitDefaultableModule(module) - InitBazelModule(module) return module } diff --git a/android/license_kind.go b/android/license_kind.go index 78df93835..838deddd2 100644 --- a/android/license_kind.go +++ b/android/license_kind.go @@ -14,8 +14,6 @@ package android -import "android/soong/bazel" - func init() { RegisterLicenseKindBuildComponents(InitRegistrationContext) } @@ -34,39 +32,13 @@ type licenseKindProperties struct { Visibility []string } -var _ Bazelable = &licenseKindModule{} - type licenseKindModule struct { ModuleBase DefaultableModuleBase - BazelModuleBase properties licenseKindProperties } -type bazelLicenseKindAttributes struct { - Conditions []string - Url string - Visibility []string -} - -func (m *licenseKindModule) ConvertWithBp2build(ctx Bp2buildMutatorContext) { - attrs := &bazelLicenseKindAttributes{ - Conditions: m.properties.Conditions, - Url: m.properties.Url, - Visibility: m.properties.Visibility, - } - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{ - Rule_class: "license_kind", - Bzl_load_location: "@rules_license//rules:license_kind.bzl", - }, - CommonAttributes{ - Name: m.Name(), - }, - attrs) -} - func (m *licenseKindModule) DepsMutator(ctx BottomUpMutatorContext) { // Nothing to do. } @@ -79,14 +51,13 @@ func LicenseKindFactory() Module { module := &licenseKindModule{} base := module.base() - module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus) + module.AddProperties(&base.nameProperties, &module.properties) // The visibility property needs to be checked and parsed by the visibility module. setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility) initAndroidModuleBase(module) InitDefaultableModule(module) - InitBazelModule(module) return module } diff --git a/android/package.go b/android/package.go index ce0b15013..878e4c4ed 100644 --- a/android/package.go +++ b/android/package.go @@ -15,9 +15,6 @@ package android import ( - "path/filepath" - - "android/soong/bazel" "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) @@ -40,52 +37,12 @@ type packageProperties struct { Default_applicable_licenses []string } -type bazelPackageAttributes struct { - Default_visibility []string - Default_package_metadata bazel.LabelListAttribute -} - type packageModule struct { ModuleBase - BazelModuleBase properties packageProperties } -var _ Bazelable = &packageModule{} - -func (p *packageModule) ConvertWithBp2build(ctx Bp2buildMutatorContext) { - defaultPackageMetadata := bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, p.properties.Default_applicable_licenses)) - // If METADATA file exists in the package, add it to package(default_package_metadata=) using a - // filegroup(name="default_metadata_file") which can be accessed later on each module in Bazel - // using attribute "applicable_licenses". - // Attribute applicable_licenses of filegroup "default_metadata_file" has to be set to [], - // otherwise Bazel reports cyclic reference error. - if existed, _, _ := ctx.Config().fs.Exists(filepath.Join(ctx.ModuleDir(), "METADATA")); existed { - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{ - Rule_class: "filegroup", - }, - CommonAttributes{Name: "default_metadata_file"}, - &bazelFilegroupAttributes{ - Srcs: bazel.MakeLabelListAttribute(BazelLabelForModuleSrc(ctx, []string{"METADATA"})), - Applicable_licenses: bazel.LabelListAttribute{Value: bazel.LabelList{Includes: []bazel.Label{}}, EmitEmptyList: true}, - }) - defaultPackageMetadata.Value.Add(&bazel.Label{Label: ":default_metadata_file"}) - } - - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{ - Rule_class: "package", - }, - CommonAttributes{}, - &bazelPackageAttributes{ - Default_package_metadata: defaultPackageMetadata, - // FIXME(asmundak): once b/221436821 is resolved - Default_visibility: []string{"//visibility:public"}, - }) -} - func (p *packageModule) GenerateAndroidBuildActions(ModuleContext) { // Nothing to do. } @@ -102,7 +59,7 @@ func (p *packageModule) qualifiedModuleId(ctx BaseModuleContext) qualifiedModule func PackageFactory() Module { module := &packageModule{} - module.AddProperties(&module.properties, &module.commonProperties.BazelConversionStatus) + module.AddProperties(&module.properties) // The name is the relative path from build root to the directory containing this // module. Set that name at the earliest possible moment that information is available @@ -119,7 +76,5 @@ func PackageFactory() Module { // its checking and parsing phases so make it the primary licenses property. setPrimaryLicensesProperty(module, "default_applicable_licenses", &module.properties.Default_applicable_licenses) - InitBazelModule(module) - return module } diff --git a/android/proto.go b/android/proto.go index c449a87ea..0d8e0972a 100644 --- a/android/proto.go +++ b/android/proto.go @@ -15,11 +15,7 @@ package android import ( - "path/filepath" "strings" - "sync" - - "android/soong/bazel" "github.com/google/blueprint" "github.com/google/blueprint/proptools" @@ -154,278 +150,3 @@ 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 - Proto_libs bazel.LabelList - Transitive_proto_libs bazel.LabelList -} - -type ProtoAttrs struct { - Srcs bazel.LabelListAttribute - Import_prefix *string - Strip_import_prefix *string - Deps bazel.LabelListAttribute -} - -// For each package in the include_dirs property a proto_library target should -// be added to the BUILD file in that package and a mapping should be added here -var includeDirsToProtoDeps = map[string]string{ - "external/protobuf/src": "//external/protobuf:libprotobuf-proto", -} - -// Partitions srcs by the pkg it is in -// srcs has been created using `TransformSubpackagePaths` -// This function uses existence of Android.bp/BUILD files to create a label that is compatible with the package structure of bp2build workspace -func partitionSrcsByPackage(currentDir string, srcs bazel.LabelList) map[string]bazel.LabelList { - getPackageFromLabel := func(label string) string { - // Remove any preceding // - label = strings.TrimPrefix(label, "//") - split := strings.Split(label, ":") - if len(split) == 1 { - // e.g. foo.proto - return currentDir - } else if split[0] == "" { - // e.g. :foo.proto - return currentDir - } else { - return split[0] - } - } - - pkgToSrcs := map[string]bazel.LabelList{} - for _, src := range srcs.Includes { - pkg := getPackageFromLabel(src.Label) - list := pkgToSrcs[pkg] - list.Add(&src) - pkgToSrcs[pkg] = list - } - return pkgToSrcs -} - -// Bp2buildProtoProperties converts proto properties, creating a proto_library and returning the -// information necessary for language-specific handling. -func Bp2buildProtoProperties(ctx Bp2buildMutatorContext, m *ModuleBase, srcs bazel.LabelListAttribute) (Bp2buildProtoInfo, bool) { - var info Bp2buildProtoInfo - if srcs.IsEmpty() { - return info, false - } - - var protoLibraries bazel.LabelList - var transitiveProtoLibraries bazel.LabelList - var directProtoSrcs bazel.LabelList - - // For filegroups that should be converted to proto_library just collect the - // labels of converted proto_library targets. - for _, protoSrc := range srcs.Value.Includes { - src := protoSrc.OriginalModuleName - if fg, ok := ToFileGroupAsLibrary(ctx, src); ok && - fg.ShouldConvertToProtoLibrary(ctx) { - protoLibraries.Add(&bazel.Label{ - Label: fg.GetProtoLibraryLabel(ctx), - }) - } else { - directProtoSrcs.Add(&protoSrc) - } - } - - name := m.Name() + "_proto" - - depsFromFilegroup := protoLibraries - var canonicalPathFromRoot bool - - if len(directProtoSrcs.Includes) > 0 { - pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), directProtoSrcs) - protoIncludeDirs := []string{} - for _, pkg := range SortedStringKeys(pkgToSrcs) { - srcs := pkgToSrcs[pkg] - attrs := ProtoAttrs{ - Srcs: bazel.MakeLabelListAttribute(srcs), - } - attrs.Deps.Append(bazel.MakeLabelListAttribute(depsFromFilegroup)) - - 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 - - canonicalPathFromRoot = proptools.BoolDefault(props.Proto.Canonical_path_from_root, canonicalPathFromRootDefault) - if !canonicalPathFromRoot { - // an empty string indicates to strips the package path - path := "" - attrs.Strip_import_prefix = &path - } - - for _, dir := range props.Proto.Include_dirs { - if dep, ok := includeDirsToProtoDeps[dir]; ok { - attrs.Deps.Add(bazel.MakeLabelAttribute(dep)) - } else { - protoIncludeDirs = append(protoIncludeDirs, dir) - } - } - - // proto.local_include_dirs are similar to proto.include_dirs, except that it is relative to the module directory - for _, dir := range props.Proto.Local_include_dirs { - relativeToTop := pathForModuleSrc(ctx, dir).String() - protoIncludeDirs = append(protoIncludeDirs, relativeToTop) - } - - } else if props.Proto.Type != info.Type && props.Proto.Type != nil { - ctx.ModuleErrorf("Cannot handle arch-variant types for protos at this time.") - } - } - } - - if p, ok := m.module.(PkgPathInterface); ok && p.PkgPath(ctx) != nil { - // python_library with pkg_path - // proto_library for this module should have the pkg_path as the import_prefix - attrs.Import_prefix = p.PkgPath(ctx) - attrs.Strip_import_prefix = proptools.StringPtr("") - } - - tags := ApexAvailableTagsWithoutTestApexes(ctx, ctx.Module()) - - moduleDir := ctx.ModuleDir() - if !canonicalPathFromRoot { - // Since we are creating the proto_library in a subpackage, set the import_prefix relative to the current package - if rel, err := filepath.Rel(moduleDir, pkg); err != nil { - ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err) - } else if rel != "." { - attrs.Import_prefix = &rel - } - } - - // TODO - b/246997908: Handle potential orphaned proto_library targets - // To create proto_library targets in the same package, we split the .proto files - // This means that if a proto_library in a subpackage imports another proto_library from the parent package - // (or a different subpackage), it will not find it. - // The CcProtoGen action itself runs fine because we construct the correct ProtoInfo, - // but the FileDescriptorSet of each proto_library might not be compile-able - // - // Add manual tag if either - // 1. .proto files are in more than one package - // 2. proto.include_dirs is not empty - if len(SortedStringKeys(pkgToSrcs)) > 1 || len(protoIncludeDirs) > 0 { - tags.Append(bazel.MakeStringListAttribute([]string{"manual"})) - } - - ctx.CreateBazelTargetModule( - bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, - CommonAttributes{Name: name, Dir: proptools.StringPtr(pkg), Tags: tags}, - &attrs, - ) - - l := "" - if pkg == moduleDir { // same package that the original module lives in - l = ":" + name - } else { - l = "//" + pkg + ":" + name - } - protoLibraries.Add(&bazel.Label{ - Label: l, - }) - } - // Partitioning by packages can create dupes of protoIncludeDirs, so dedupe it first. - protoLibrariesInIncludeDir := createProtoLibraryTargetsForIncludeDirs(ctx, SortedUniqueStrings(protoIncludeDirs)) - transitiveProtoLibraries.Append(protoLibrariesInIncludeDir) - } - - info.Proto_libs = protoLibraries - info.Transitive_proto_libs = transitiveProtoLibraries - - return info, true -} - -// PkgPathInterface is used as a type assertion in bp2build to get pkg_path property of python_library_host -type PkgPathInterface interface { - PkgPath(ctx BazelConversionContext) *string -} - -var ( - protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto" - protoIncludeDirsBp2buildKey = NewOnceKey("protoIncludeDirsBp2build") -) - -func getProtoIncludeDirsBp2build(config Config) *sync.Map { - return config.Once(protoIncludeDirsBp2buildKey, func() interface{} { - return &sync.Map{} - }).(*sync.Map) -} - -// key for dynamically creating proto_library per proto.include_dirs -type protoIncludeDirKey struct { - dir string - subpackgeInDir string -} - -// createProtoLibraryTargetsForIncludeDirs creates additional proto_library targets for .proto files in includeDirs -// Since Bazel imposes a constratint that the proto_library must be in the same package as the .proto file, this function -// might create the targets in a subdirectory of `includeDir` -// Returns the labels of the proto_library targets -func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, includeDirs []string) bazel.LabelList { - var ret bazel.LabelList - for _, dir := range includeDirs { - if exists, _, _ := ctx.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists { - ctx.ModuleErrorf("TODO: Add support for proto.include_dir: %v. This directory does not contain an Android.bp file", dir) - } - dirMap := getProtoIncludeDirsBp2build(ctx.Config()) - // Find all proto file targets in this dir - protoLabelsInDir := BazelLabelForSrcPatternExcludes(ctx, dir, "**/*.proto", []string{}) - // Partition the labels by package and subpackage(s) - protoLabelelsPartitionedByPkg := partitionSrcsByPackage(dir, protoLabelsInDir) - for _, pkg := range SortedStringKeys(protoLabelelsPartitionedByPkg) { - label := strings.ReplaceAll(dir, "/", ".") + protoIncludeDirGeneratedSuffix - ret.Add(&bazel.Label{ - Label: "//" + pkg + ":" + label, - }) - key := protoIncludeDirKey{dir: dir, subpackgeInDir: pkg} - if _, exists := dirMap.LoadOrStore(key, true); exists { - // A proto_library has already been created for this package relative to this include dir - continue - } - srcs := protoLabelelsPartitionedByPkg[pkg] - rel, err := filepath.Rel(dir, pkg) - if err != nil { - ctx.ModuleErrorf("Could not create a proto_library in pkg %v due to %v\n", pkg, err) - } - // Create proto_library - attrs := ProtoAttrs{ - Srcs: bazel.MakeLabelListAttribute(srcs), - Strip_import_prefix: proptools.StringPtr(""), - } - if rel != "." { - attrs.Import_prefix = proptools.StringPtr(rel) - } - - // If a specific directory is listed in proto.include_dirs of two separate modules (one host-specific and another device-specific), - // we do not want to create the proto_library with target_compatible_with of the first visited of these two modules - // As a workarounds, delete `target_compatible_with` - alwaysEnabled := bazel.BoolAttribute{} - alwaysEnabled.Value = proptools.BoolPtr(true) - // Add android and linux explicitly so that fillcommonbp2buildmoduleattrs can override these configs - // When we extend b support for other os'es (darwin/windows), we should add those configs here as well - alwaysEnabled.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid, proptools.BoolPtr(true)) - alwaysEnabled.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsLinux, proptools.BoolPtr(true)) - - ctx.CreateBazelTargetModuleWithRestrictions( - bazel.BazelTargetModuleProperties{Rule_class: "proto_library"}, - CommonAttributes{ - Name: label, - Dir: proptools.StringPtr(pkg), - // This proto_library is used to construct a ProtoInfo - // But it might not be buildable on its own - Tags: bazel.MakeStringListAttribute([]string{"manual"}), - }, - &attrs, - alwaysEnabled, - ) - } - } - return ret -} diff --git a/android/soong_config_modules.go b/android/soong_config_modules.go index 0246a08d8..ec0edfd73 100644 --- a/android/soong_config_modules.go +++ b/android/soong_config_modules.go @@ -187,7 +187,6 @@ func (*soongConfigModuleTypeImport) GenerateAndroidBuildActions(ModuleContext) { type soongConfigModuleTypeModule struct { ModuleBase - BazelModuleBase properties soongconfig.ModuleTypeProperties } @@ -395,10 +394,6 @@ func loadSoongConfigModuleTypeDefinition(ctx LoadHookContext, from string) map[s return (map[string]blueprint.ModuleFactory)(nil) } - if ctx.Config().BuildMode == Bp2build { - ctx.Config().Bp2buildSoongConfigDefinitions.AddVars(mtDef) - } - globalModuleTypes := ctx.moduleFactories() factories := make(map[string]blueprint.ModuleFactory) @@ -406,7 +401,7 @@ func loadSoongConfigModuleTypeDefinition(ctx LoadHookContext, from string) map[s for name, moduleType := range mtDef.ModuleTypes { factory := globalModuleTypes[moduleType.BaseModuleType] if factory != nil { - factories[name] = configModuleFactory(factory, moduleType, ctx.Config().BuildMode == Bp2build) + factories[name] = configModuleFactory(factory, moduleType) } else { reportErrors(ctx, from, fmt.Errorf("missing global module type factory for %q", moduleType.BaseModuleType)) @@ -474,7 +469,7 @@ var _ soongconfig.SoongConfig = (*tracingConfig)(nil) // configModuleFactory takes an existing soongConfigModuleFactory and a // ModuleType to create a new ModuleFactory that uses a custom loadhook. -func configModuleFactory(factory blueprint.ModuleFactory, moduleType *soongconfig.ModuleType, bp2build bool) blueprint.ModuleFactory { +func configModuleFactory(factory blueprint.ModuleFactory, moduleType *soongconfig.ModuleType) blueprint.ModuleFactory { // Defer creation of conditional properties struct until the first call from the factory // method. That avoids having to make a special call to the factory to create the properties // structs from which the conditional properties struct is created. This is needed in order to @@ -515,40 +510,22 @@ func configModuleFactory(factory blueprint.ModuleFactory, moduleType *soongconfi conditionalProps := proptools.CloneEmptyProperties(conditionalFactoryProps) props = append(props, conditionalProps.Interface()) - if bp2build { - // The loadhook is different for bp2build, since we don't want to set a specific - // set of property values based on a vendor var -- we want __all of them__ to - // generate select statements, so we put the entire soong_config_variables - // struct, together with the namespace representing those variables, while - // creating the custom module with the factory. - AddLoadHook(module, func(ctx LoadHookContext) { - if m, ok := module.(Bazelable); ok { - m.SetBaseModuleType(moduleType.BaseModuleType) - // Instead of applying all properties, keep the entire conditionalProps struct as - // part of the custom module so dependent modules can create the selects accordingly - m.setNamespacedVariableProps(namespacedVariableProperties{ - moduleType.ConfigNamespace: []interface{}{conditionalProps.Interface()}, - }) - } - }) - } else { - // Regular Soong operation wraps the existing module factory with a - // conditional on Soong config variables by reading the product - // config variables from Make. - AddLoadHook(module, func(ctx LoadHookContext) { - tracingConfig := newTracingConfig(ctx.Config().VendorConfig(moduleType.ConfigNamespace)) - newProps, err := soongconfig.PropertiesToApply(moduleType, conditionalProps, tracingConfig) - if err != nil { - ctx.ModuleErrorf("%s", err) - return - } - for _, ps := range newProps { - ctx.AppendProperties(ps) - } + // Regular Soong operation wraps the existing module factory with a + // conditional on Soong config variables by reading the product + // config variables from Make. + AddLoadHook(module, func(ctx LoadHookContext) { + tracingConfig := newTracingConfig(ctx.Config().VendorConfig(moduleType.ConfigNamespace)) + newProps, err := soongconfig.PropertiesToApply(moduleType, conditionalProps, tracingConfig) + if err != nil { + ctx.ModuleErrorf("%s", err) + return + } + for _, ps := range newProps { + ctx.AppendProperties(ps) + } - module.(Module).base().commonProperties.SoongConfigTrace = tracingConfig.getTrace() - }) - } + module.(Module).base().commonProperties.SoongConfigTrace = tracingConfig.getTrace() + }) return module, props } } diff --git a/apex/Android.bp b/apex/Android.bp index 0a5063ff7..27017ae02 100644 --- a/apex/Android.bp +++ b/apex/Android.bp @@ -11,7 +11,6 @@ bootstrap_go_package { "soong-aconfig", "soong-aconfig-codegen", "soong-android", - "soong-bazel", "soong-bpf", "soong-cc", "soong-filesystem", @@ -28,7 +27,6 @@ bootstrap_go_package { "apex_sdk_member.go", "apex_singleton.go", "builder.go", - "bp2build.go", "deapexer.go", "key.go", "prebuilt.go", diff --git a/apex/androidmk.go b/apex/androidmk.go index 4e968eaad..bc68ad36b 100644 --- a/apex/androidmk.go +++ b/apex/androidmk.go @@ -139,9 +139,6 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, moduleDir st archStr := fi.module.Target().Arch.ArchType.String() fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr) } - } else if fi.isBazelPrebuilt && fi.arch != "" { - // This apexFile comes from Bazel - fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", fi.arch) } if fi.jacocoReportClassesFile != nil { fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String()) @@ -185,21 +182,17 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, moduleDir st fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_android_app_set.mk") case nativeSharedLib, nativeExecutable, nativeTest: fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.stem()) - if fi.isBazelPrebuilt { - fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", fi.unstrippedBuiltFile) - } else { - if ccMod, ok := fi.module.(*cc.Module); ok { - if ccMod.UnstrippedOutputFile() != nil { - fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", ccMod.UnstrippedOutputFile().String()) - } - ccMod.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w) - if ccMod.CoverageOutputFile().Valid() { - fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", ccMod.CoverageOutputFile().String()) - } - } else if rustMod, ok := fi.module.(*rust.Module); ok { - if rustMod.UnstrippedOutputFile() != nil { - fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", rustMod.UnstrippedOutputFile().String()) - } + if ccMod, ok := fi.module.(*cc.Module); ok { + if ccMod.UnstrippedOutputFile() != nil { + fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", ccMod.UnstrippedOutputFile().String()) + } + ccMod.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w) + if ccMod.CoverageOutputFile().Valid() { + fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", ccMod.CoverageOutputFile().String()) + } + } else if rustMod, ok := fi.module.(*rust.Module); ok { + if rustMod.UnstrippedOutputFile() != nil { + fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", rustMod.UnstrippedOutputFile().String()) } } fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk") diff --git a/apex/apex.go b/apex/apex.go index 7ddb3e6c5..a4dc7f147 100644 --- a/apex/apex.go +++ b/apex/apex.go @@ -25,13 +25,10 @@ import ( "strings" "android/soong/aconfig" - "android/soong/bazel/cquery" - "github.com/google/blueprint" "github.com/google/blueprint/proptools" "android/soong/android" - "android/soong/bazel" "android/soong/bpf" "android/soong/cc" prebuilt_etc "android/soong/etc" @@ -389,7 +386,6 @@ type apexBundle struct { android.ModuleBase android.DefaultableModuleBase android.OverridableModuleBase - android.BazelModuleBase multitree.ExportableModuleBase // Properties @@ -547,10 +543,6 @@ type apexFile struct { multilib string - isBazelPrebuilt bool - unstrippedBuiltFile android.Path - arch string - // TODO(jiyong): remove this module android.Module } @@ -1821,119 +1813,6 @@ func (f fsType) string() string { } } -var _ android.MixedBuildBuildable = (*apexBundle)(nil) - -func (a *apexBundle) IsMixedBuildSupported(ctx android.BaseModuleContext) bool { - return true -} - -func (a *apexBundle) QueueBazelCall(ctx android.BaseModuleContext) { - bazelCtx := ctx.Config().BazelContext - bazelCtx.QueueBazelRequest(a.GetBazelLabel(ctx, a), cquery.GetApexInfo, android.GetConfigKey(ctx)) -} - -// GetBazelLabel returns the bazel label of this apexBundle, or the label of the -// override_apex module overriding this apexBundle. An apexBundle can be -// overridden by different override_apex modules (e.g. Google or Go variants), -// which is handled by the overrides mutators. -func (a *apexBundle) GetBazelLabel(ctx android.BazelConversionPathContext, module blueprint.Module) string { - return a.BazelModuleBase.GetBazelLabel(ctx, a) -} - -func (a *apexBundle) ProcessBazelQueryResponse(ctx android.ModuleContext) { - if !a.commonBuildActions(ctx) { - return - } - - a.setPayloadFsType(ctx) - a.setSystemLibLink(ctx) - a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx) - - bazelCtx := ctx.Config().BazelContext - outputs, err := bazelCtx.GetApexInfo(a.GetBazelLabel(ctx, a), android.GetConfigKey(ctx)) - if err != nil { - ctx.ModuleErrorf(err.Error()) - return - } - a.installDir = android.PathForModuleInstall(ctx, "apex") - - // Set the output file to .apex or .capex depending on the compression configuration. - a.setCompression(ctx) - if a.isCompressed { - a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedCompressedOutput) - } else { - a.outputApexFile = android.PathForBazelOutRelative(ctx, ctx.ModuleDir(), outputs.SignedOutput) - } - a.outputFile = a.outputApexFile - - if len(outputs.TidyFiles) > 0 { - tidyFiles := android.PathsForBazelOut(ctx, outputs.TidyFiles) - a.outputFile = android.AttachValidationActions(ctx, a.outputFile, tidyFiles) - } - - // TODO(b/257829940): These are used by the apex_keys_text singleton; would probably be a clearer - // interface if these were set in a provider rather than the module itself - a.publicKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[0]) - a.privateKeyFile = android.PathForBazelOut(ctx, outputs.BundleKeyInfo[1]) - a.containerCertificateFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[0]) - a.containerPrivateKeyFile = android.PathForBazelOut(ctx, outputs.ContainerKeyInfo[1]) - - // Ensure ApexMkInfo.install_to_system make module names are installed as - // part of a bundled build. - a.makeModulesToInstall = append(a.makeModulesToInstall, outputs.MakeModulesToInstall...) - - a.bundleModuleFile = android.PathForBazelOut(ctx, outputs.BundleFile) - a.nativeApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.SymbolsUsedByApex)) - a.nativeApisBackedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.BackingLibs)) - // TODO(b/239084755): Generate the java api using.xml file from Bazel. - a.javaApisUsedByModuleFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.JavaSymbolsUsedByApex)) - a.installedFilesFile = android.ModuleOutPath(android.PathForBazelOut(ctx, outputs.InstalledFiles)) - installSuffix := imageApexSuffix - if a.isCompressed { - installSuffix = imageCapexSuffix - } - a.installedFile = ctx.InstallFile(a.installDir, a.Name()+installSuffix, a.outputFile, - a.compatSymlinks...) - - // filesInfo in mixed mode must retrieve all information about the apex's - // contents completely from the Starlark providers. It should never rely on - // Android.bp information, as they might not exist for fully migrated - // dependencies. - // - // Prevent accidental writes to filesInfo in the earlier parts Soong by - // asserting it to be nil. - if a.filesInfo != nil { - panic( - fmt.Errorf("internal error: filesInfo must be nil for an apex handled by Bazel. " + - "Did something else set filesInfo before this line of code?")) - } - for _, f := range outputs.PayloadFilesInfo { - fileInfo := apexFile{ - isBazelPrebuilt: true, - - builtFile: android.PathForBazelOut(ctx, f["built_file"]), - unstrippedBuiltFile: android.PathForBazelOut(ctx, f["unstripped_built_file"]), - androidMkModuleName: f["make_module_name"], - installDir: f["install_dir"], - class: classes[f["class"]], - customStem: f["basename"], - moduleDir: f["package"], - } - - arch := f["arch"] - fileInfo.arch = arch - if len(arch) > 0 { - fileInfo.multilib = "lib32" - if strings.HasSuffix(arch, "64") { - fileInfo.multilib = "lib64" - } - } - - a.filesInfo = append(a.filesInfo, fileInfo) - } - a.apexKeysPath = writeApexKeys(ctx, a) -} - func (a *apexBundle) setCompression(ctx android.ModuleContext) { if a.testOnlyShouldForceCompression() { a.isCompressed = true @@ -2587,7 +2466,6 @@ func newApexBundle() *apexBundle { android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) android.InitDefaultableModule(module) android.InitOverridableModule(module, &module.overridableProperties.Overrides) - android.InitBazelModule(module) multitree.InitExportableModule(module) return module } @@ -2635,7 +2513,6 @@ func DefaultsFactory() android.Module { type OverrideApex struct { android.ModuleBase android.OverrideModuleBase - android.BazelModuleBase } func (o *OverrideApex) GenerateAndroidBuildActions(_ android.ModuleContext) { @@ -2651,104 +2528,9 @@ func OverrideApexFactory() android.Module { android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon) android.InitOverrideModule(m) - android.InitBazelModule(m) return m } -func (o *OverrideApex) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - if ctx.ModuleType() != "override_apex" { - return - } - - baseApexModuleName := o.OverrideModuleBase.GetOverriddenModuleName() - baseModule, baseApexExists := ctx.ModuleFromName(baseApexModuleName) - if !baseApexExists { - panic(fmt.Errorf("Base apex module doesn't exist: %s", baseApexModuleName)) - } - - a, baseModuleIsApex := baseModule.(*apexBundle) - if !baseModuleIsApex { - panic(fmt.Errorf("Base module is not apex module: %s", baseApexModuleName)) - } - attrs, props, commonAttrs := convertWithBp2build(a, ctx) - - // We just want the name, not module reference. - baseApexName := strings.TrimPrefix(baseApexModuleName, ":") - attrs.Base_apex_name = &baseApexName - - for _, p := range o.GetProperties() { - overridableProperties, ok := p.(*overridableProperties) - if !ok { - continue - } - - // Manifest is either empty or a file in the directory of base APEX and is not overridable. - // After it is converted in convertWithBp2build(baseApex, ctx), - // the attrs.Manifest.Value.Label is the file path relative to the directory - // of base apex. So the following code converts it to a label that looks like - // : if base apex and override - // apex are not in the same package. - baseApexPackage := ctx.OtherModuleDir(a) - overrideApexPackage := ctx.ModuleDir() - if baseApexPackage != overrideApexPackage { - attrs.Manifest.Value.Label = "//" + baseApexPackage + ":" + attrs.Manifest.Value.Label - } - - // Key - if overridableProperties.Key != nil { - attrs.Key = bazel.LabelAttribute{} - attrs.Key.SetValue(android.BazelLabelForModuleDepSingle(ctx, *overridableProperties.Key)) - } - - // Certificate - if overridableProperties.Certificate == nil { - // If overridableProperties.Certificate is nil, clear this out as - // well with zeroed structs, so the override_apex does not use the - // base apex's certificate. - attrs.Certificate = bazel.LabelAttribute{} - attrs.Certificate_name = bazel.StringAttribute{} - } else { - attrs.Certificate, attrs.Certificate_name = android.BazelStringOrLabelFromProp(ctx, overridableProperties.Certificate) - } - - // Prebuilts - if overridableProperties.Prebuilts != nil { - prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, overridableProperties.Prebuilts) - attrs.Prebuilts = bazel.MakeLabelListAttribute(prebuiltsLabelList) - } - - // Compressible - if overridableProperties.Compressible != nil { - attrs.Compressible = bazel.BoolAttribute{Value: overridableProperties.Compressible} - } - - // Package name - // - // e.g. com.android.adbd's package name is com.android.adbd, but - // com.google.android.adbd overrides the package name to com.google.android.adbd - // - // TODO: this can be overridden from the product configuration, see - // getOverrideManifestPackageName and - // PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES. - // - // Instead of generating the BUILD files differently based on the product config - // at the point of conversion, this should be handled by the BUILD file loading - // from the soong_injection's product_vars, so product config is decoupled from bp2build. - if overridableProperties.Package_name != "" { - attrs.Package_name = &overridableProperties.Package_name - } - - // Logging parent - if overridableProperties.Logging_parent != "" { - attrs.Logging_parent = &overridableProperties.Logging_parent - } - } - - commonAttrs.Name = o.Name() - - ctx.CreateBazelTargetModule(props, commonAttrs, &attrs) -} - /////////////////////////////////////////////////////////////////////////////////////////////////// // Vality check routines // @@ -3203,306 +2985,6 @@ func rBcpPackages() map[string][]string { } } -// For Bazel / bp2build - -type bazelApexBundleAttributes struct { - Manifest bazel.LabelAttribute - Android_manifest bazel.LabelAttribute - File_contexts bazel.LabelAttribute - Canned_fs_config bazel.LabelAttribute - Key bazel.LabelAttribute - Certificate bazel.LabelAttribute // used when the certificate prop is a module - Certificate_name bazel.StringAttribute // used when the certificate prop is a string - Min_sdk_version bazel.StringAttribute - Updatable bazel.BoolAttribute - Installable bazel.BoolAttribute - Binaries bazel.LabelListAttribute - Prebuilts bazel.LabelListAttribute - Native_shared_libs_32 bazel.LabelListAttribute - Native_shared_libs_64 bazel.LabelListAttribute - Compressible bazel.BoolAttribute - Package_name *string - Logging_parent *string - Tests bazel.LabelListAttribute - Base_apex_name *string - Apex_available_name *string - Variant_version *string -} - -type convertedNativeSharedLibs struct { - Native_shared_libs_32 bazel.LabelListAttribute - Native_shared_libs_64 bazel.LabelListAttribute -} - -const ( - minSdkVersionPropName = "Min_sdk_version" -) - -// ConvertWithBp2build performs bp2build conversion of an apex -func (a *apexBundle) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - // We only convert apex and apex_test modules at this time - if ctx.ModuleType() != "apex" && ctx.ModuleType() != "apex_test" { - return - } - - attrs, props, commonAttrs := convertWithBp2build(a, ctx) - commonAttrs.Name = a.Name() - ctx.CreateBazelTargetModule(props, commonAttrs, &attrs) -} - -func convertWithBp2build(a *apexBundle, ctx android.Bp2buildMutatorContext) (bazelApexBundleAttributes, bazel.BazelTargetModuleProperties, android.CommonAttributes) { - var manifestLabelAttribute bazel.LabelAttribute - manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))) - - var androidManifestLabelAttribute bazel.LabelAttribute - if a.properties.AndroidManifest != nil { - androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.AndroidManifest)) - } - - var fileContextsLabelAttribute bazel.LabelAttribute - if a.properties.File_contexts == nil { - // See buildFileContexts(), if file_contexts is not specified the default one is used, which is //system/sepolicy/apex:-file_contexts - fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, a.Name()+"-file_contexts")) - } else if strings.HasPrefix(*a.properties.File_contexts, ":") { - // File_contexts is a module - fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.properties.File_contexts)) - } else { - // File_contexts is a file - fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.File_contexts)) - } - - var cannedFsConfigAttribute bazel.LabelAttribute - if a.properties.Canned_fs_config != nil { - cannedFsConfigAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *a.properties.Canned_fs_config)) - } - - productVariableProps, errs := android.ProductVariableProperties(ctx, a) - for _, err := range errs { - ctx.ModuleErrorf("ProductVariableProperties error: %s", err) - } - // TODO(b/219503907) this would need to be set to a.MinSdkVersionValue(ctx) but - // given it's coming via config, we probably don't want to put it in here. - var minSdkVersion bazel.StringAttribute - if a.properties.Min_sdk_version != nil { - minSdkVersion.SetValue(*a.properties.Min_sdk_version) - } - if props, ok := productVariableProps[minSdkVersionPropName]; ok { - for c, p := range props { - if val, ok := p.(*string); ok { - minSdkVersion.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val) - } - } - } - - var keyLabelAttribute bazel.LabelAttribute - if a.overridableProperties.Key != nil { - keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *a.overridableProperties.Key)) - } - - // Certificate - certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableProperties.Certificate) - - nativeSharedLibs := &convertedNativeSharedLibs{ - Native_shared_libs_32: bazel.LabelListAttribute{}, - Native_shared_libs_64: bazel.LabelListAttribute{}, - } - - // https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=698;drc=f05b0d35d2fbe51be9961ce8ce8031f840295c68 - // https://cs.android.com/android/platform/superproject/+/master:build/soong/apex/apex.go;l=2549;drc=ec731a83e3e2d80a1254e32fd4ad7ef85e262669 - // In Soong, decodeMultilib, used to get multilib, return "first" if defaultMultilib is set to "common". - // Since apex sets defaultMultilib to be "common", equivalent compileMultilib in bp2build for apex should be "first" - compileMultilib := "first" - if a.CompileMultilib() != nil { - compileMultilib = *a.CompileMultilib() - } - - // properties.Native_shared_libs is treated as "both" - convertBothLibs(ctx, compileMultilib, a.properties.Native_shared_libs, nativeSharedLibs) - convertBothLibs(ctx, compileMultilib, a.properties.Multilib.Both.Native_shared_libs, nativeSharedLibs) - convert32Libs(ctx, compileMultilib, a.properties.Multilib.Lib32.Native_shared_libs, nativeSharedLibs) - convert64Libs(ctx, compileMultilib, a.properties.Multilib.Lib64.Native_shared_libs, nativeSharedLibs) - convertFirstLibs(ctx, compileMultilib, a.properties.Multilib.First.Native_shared_libs, nativeSharedLibs) - - prebuilts := a.overridableProperties.Prebuilts - prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts) - prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList) - - binaries := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Binaries) - binariesLabelListAttribute := bazel.MakeLabelListAttribute(binaries) - - var testsAttrs bazel.LabelListAttribute - if a.testApex && len(a.properties.ApexNativeDependencies.Tests) > 0 { - tests := android.BazelLabelForModuleDeps(ctx, a.properties.ApexNativeDependencies.Tests) - testsAttrs = bazel.MakeLabelListAttribute(tests) - } - - var updatableAttribute bazel.BoolAttribute - if a.properties.Updatable != nil { - updatableAttribute.Value = a.properties.Updatable - } - - var installableAttribute bazel.BoolAttribute - if a.properties.Installable != nil { - installableAttribute.Value = a.properties.Installable - } - - var compressibleAttribute bazel.BoolAttribute - if a.overridableProperties.Compressible != nil { - compressibleAttribute.Value = a.overridableProperties.Compressible - } - - var packageName *string - if a.overridableProperties.Package_name != "" { - packageName = &a.overridableProperties.Package_name - } - - var loggingParent *string - if a.overridableProperties.Logging_parent != "" { - loggingParent = &a.overridableProperties.Logging_parent - } - - attrs := bazelApexBundleAttributes{ - Manifest: manifestLabelAttribute, - Android_manifest: androidManifestLabelAttribute, - File_contexts: fileContextsLabelAttribute, - Canned_fs_config: cannedFsConfigAttribute, - Min_sdk_version: minSdkVersion, - Key: keyLabelAttribute, - Certificate: certificate, - Certificate_name: certificateName, - Updatable: updatableAttribute, - Installable: installableAttribute, - Native_shared_libs_32: nativeSharedLibs.Native_shared_libs_32, - Native_shared_libs_64: nativeSharedLibs.Native_shared_libs_64, - Binaries: binariesLabelListAttribute, - Prebuilts: prebuiltsLabelListAttribute, - Compressible: compressibleAttribute, - Package_name: packageName, - Logging_parent: loggingParent, - Tests: testsAttrs, - Apex_available_name: a.properties.Apex_available_name, - Variant_version: a.properties.Variant_version, - } - - props := bazel.BazelTargetModuleProperties{ - Rule_class: "apex", - Bzl_load_location: "//build/bazel/rules/apex:apex.bzl", - } - - commonAttrs := android.CommonAttributes{} - if a.testApex { - commonAttrs.Testonly = proptools.BoolPtr(true) - // Set the api_domain of the test apex - attrs.Base_apex_name = proptools.StringPtr(cc.GetApiDomain(a.Name())) - } - - return attrs, props, commonAttrs -} - -// The following conversions are based on this table where the rows are the compile_multilib -// values and the columns are the properties.Multilib.*.Native_shared_libs. Each cell -// represents how the libs should be compiled for a 64-bit/32-bit device: 32 means it -// should be compiled as 32-bit, 64 means it should be compiled as 64-bit, none means it -// should not be compiled. -// multib/compile_multilib, 32, 64, both, first -// 32, 32/32, none/none, 32/32, none/32 -// 64, none/none, 64/none, 64/none, 64/none -// both, 32/32, 64/none, 32&64/32, 64/32 -// first, 32/32, 64/none, 64/32, 64/32 - -func convert32Libs(ctx android.Bp2buildMutatorContext, compileMultilb string, - libs []string, nativeSharedLibs *convertedNativeSharedLibs) { - libsLabelList := android.BazelLabelForModuleDeps(ctx, libs) - switch compileMultilb { - case "both", "32": - makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "first": - make32SharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "64": - // Incompatible, ignore - default: - invalidCompileMultilib(ctx, compileMultilb) - } -} - -func convert64Libs(ctx android.Bp2buildMutatorContext, compileMultilb string, - libs []string, nativeSharedLibs *convertedNativeSharedLibs) { - libsLabelList := android.BazelLabelForModuleDeps(ctx, libs) - switch compileMultilb { - case "both", "64", "first": - make64SharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "32": - // Incompatible, ignore - default: - invalidCompileMultilib(ctx, compileMultilb) - } -} - -func convertBothLibs(ctx android.Bp2buildMutatorContext, compileMultilb string, - libs []string, nativeSharedLibs *convertedNativeSharedLibs) { - libsLabelList := android.BazelLabelForModuleDeps(ctx, libs) - switch compileMultilb { - case "both": - makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs) - make64SharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "first": - makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "32": - makeNoConfig32SharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "64": - make64SharedLibsAttributes(libsLabelList, nativeSharedLibs) - default: - invalidCompileMultilib(ctx, compileMultilb) - } -} - -func convertFirstLibs(ctx android.Bp2buildMutatorContext, compileMultilb string, - libs []string, nativeSharedLibs *convertedNativeSharedLibs) { - libsLabelList := android.BazelLabelForModuleDeps(ctx, libs) - switch compileMultilb { - case "both", "first": - makeFirstSharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "32": - make32SharedLibsAttributes(libsLabelList, nativeSharedLibs) - case "64": - make64SharedLibsAttributes(libsLabelList, nativeSharedLibs) - default: - invalidCompileMultilib(ctx, compileMultilb) - } -} - -func makeFirstSharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) { - make32SharedLibsAttributes(libsLabelList, nativeSharedLibs) - make64SharedLibsAttributes(libsLabelList, nativeSharedLibs) -} - -func makeNoConfig32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) { - list := bazel.LabelListAttribute{} - list.SetSelectValue(bazel.NoConfigAxis, "", libsLabelList) - nativeSharedLibs.Native_shared_libs_32.Append(list) -} - -func make32SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) { - makeSharedLibsAttributes("x86", libsLabelList, &nativeSharedLibs.Native_shared_libs_32) - makeSharedLibsAttributes("arm", libsLabelList, &nativeSharedLibs.Native_shared_libs_32) -} - -func make64SharedLibsAttributes(libsLabelList bazel.LabelList, nativeSharedLibs *convertedNativeSharedLibs) { - makeSharedLibsAttributes("x86_64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64) - makeSharedLibsAttributes("arm64", libsLabelList, &nativeSharedLibs.Native_shared_libs_64) -} - -func makeSharedLibsAttributes(config string, libsLabelList bazel.LabelList, - labelListAttr *bazel.LabelListAttribute) { - list := bazel.LabelListAttribute{} - list.SetSelectValue(bazel.ArchConfigurationAxis, config, libsLabelList) - labelListAttr.Append(list) -} - -func invalidCompileMultilib(ctx android.Bp2buildMutatorContext, value string) { - ctx.PropertyErrorf("compile_multilib", "Invalid value: %s", value) -} - func (a *apexBundle) IsTestApex() bool { return a.testApex } diff --git a/apex/bp2build.go b/apex/bp2build.go deleted file mode 100644 index a3dda83b1..000000000 --- a/apex/bp2build.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (C) 2022 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package apex - -import ( - "android/soong/android" - "encoding/json" - "strings" -) - -// This file contains the bp2build integration for the apex package. - -// Export constants as Starlark using bp2build to Bazel. -func BazelApexToolchainVars() (string, error) { - marshalled, err := json.Marshal(apexAvailBaseline) - if err != nil { - return "", err - } - content := []string{ - "# GENERATED BY SOONG. DO NOT EDIT.", - "default_manifest_version = " + android.DefaultUpdatableModuleVersion, // constants.go is different in every branch. - "apex_available_baseline = json.decode('''" + string(marshalled) + "''')", - } - return strings.Join(content, "\n"), nil -} diff --git a/apex/bp2build_test.go b/apex/bp2build_test.go deleted file mode 100644 index 6bab67dbe..000000000 --- a/apex/bp2build_test.go +++ /dev/null @@ -1,545 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package apex - -import ( - "android/soong/android" - "android/soong/android/allowlists" - "android/soong/bazel/cquery" - "fmt" - "path/filepath" - "strings" - "testing" -) - -func TestApexImageInMixedBuilds(t *testing.T) { - bp := ` -apex_key{ - name: "foo_key", -} - -apex { - name: "foo", - key: "foo_key", - updatable: true, - min_sdk_version: "31", - file_contexts: ":myapex-file_contexts", - bazel_module: { label: "//:foo" }, -}` - - outputBaseDir := "out/bazel" - result := android.GroupFixturePreparers( - prepareForApexTest, - android.FixtureModifyConfig(func(config android.Config) { - config.BazelContext = android.MockBazelContext{ - OutputBaseDir: outputBaseDir, - LabelToApexInfo: map[string]cquery.ApexInfo{ - "//:foo": cquery.ApexInfo{ - // ApexInfo Starlark provider. - SignedOutput: "signed_out.apex", - SignedCompressedOutput: "signed_out.capex", - UnsignedOutput: "unsigned_out.apex", - BundleKeyInfo: []string{"public_key", "private_key"}, - ContainerKeyInfo: []string{"container_cert", "container_private"}, - SymbolsUsedByApex: "foo_using.txt", - JavaSymbolsUsedByApex: "foo_using.xml", - BundleFile: "apex_bundle.zip", - InstalledFiles: "installed-files.txt", - RequiresLibs: []string{"//path/c:c", "//path/d:d"}, - - // unused - PackageName: "pkg_name", - ProvidesLibs: []string{"a", "b"}, - - // ApexMkInfo Starlark provider - PayloadFilesInfo: []map[string]string{ - { - "built_file": "bazel-out/adbd", - "install_dir": "bin", - "class": "nativeExecutable", - "make_module_name": "adbd", - "basename": "adbd", - "package": "foo", - }, - }, - MakeModulesToInstall: []string{"c"}, // d deliberately omitted - }, - }, - } - }), - ).RunTestWithBp(t, bp) - - m := result.ModuleForTests("foo", "android_common_foo").Module() - ab, ok := m.(*apexBundle) - - if !ok { - t.Fatalf("Expected module to be an apexBundle, was not") - } - - // TODO: refactor to android.AssertStringEquals - if w, g := "out/bazel/execroot/__main__/public_key", ab.publicKeyFile.String(); w != g { - t.Errorf("Expected public key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/private_key", ab.privateKeyFile.String(); w != g { - t.Errorf("Expected private key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/container_cert", ab.containerCertificateFile.String(); w != g { - t.Errorf("Expected public container key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/container_private", ab.containerPrivateKeyFile.String(); w != g { - t.Errorf("Expected private container key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/signed_out.apex", ab.outputFile.String(); w != g { - t.Errorf("Expected output file %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { - t.Errorf("Expected output file %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { - t.Errorf("Expected output file %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/installed-files.txt", ab.installedFilesFile.String(); w != g { - t.Errorf("Expected installed-files.txt %q, got %q", w, g) - } - - mkData := android.AndroidMkDataForTest(t, result.TestContext, m) - var builder strings.Builder - mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData) - - data := builder.String() - if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/apex_bundle.zip"; !strings.Contains(data, w) { - t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) - } - if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/installed-files.txt:foo-installed-files.txt)"; !strings.Contains(data, w) { - t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) - } - - // make modules to be installed to system - if len(ab.makeModulesToInstall) != 1 && ab.makeModulesToInstall[0] != "c" { - t.Errorf("Expected makeModulesToInstall slice to only contain 'c', got %q", ab.makeModulesToInstall) - } - if w := "LOCAL_REQUIRED_MODULES := adbd.foo c"; !strings.Contains(data, w) { - t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) - } -} - -func TestApexImageCreatesFilesInfoForMake(t *testing.T) { - bp := ` -apex_key{ - name: "foo_key", -} - -apex { - name: "foo", - key: "foo_key", - updatable: true, - min_sdk_version: "31", - file_contexts: ":myapex-file_contexts", - bazel_module: { label: "//:foo" }, -}` - - outputBaseDir := "out/bazel" - result := android.GroupFixturePreparers( - prepareForApexTest, - android.FixtureModifyConfig(func(config android.Config) { - config.BazelContext = android.MockBazelContext{ - OutputBaseDir: outputBaseDir, - LabelToApexInfo: map[string]cquery.ApexInfo{ - "//:foo": { - // ApexInfo Starlark provider. Necessary for the test. - SignedOutput: "signed_out.apex", - BundleKeyInfo: []string{"public_key", "private_key"}, - ContainerKeyInfo: []string{"container_cert", "container_private"}, - - // ApexMkInfo Starlark provider - PayloadFilesInfo: []map[string]string{ - { - "arch": "arm64", - "basename": "libcrypto.so", - "built_file": "bazel-out/64/libcrypto.so", - "class": "nativeSharedLib", - "install_dir": "lib64", - "make_module_name": "libcrypto", - "package": "foo/bar", - "unstripped_built_file": "bazel-out/64/unstripped_libcrypto.so", - }, - { - "arch": "arm", - "basename": "libcrypto.so", - "built_file": "bazel-out/32/libcrypto.so", - "class": "nativeSharedLib", - "install_dir": "lib", - "make_module_name": "libcrypto", - "package": "foo/bar", - }, - { - "arch": "arm64", - "basename": "adbd", - "built_file": "bazel-out/adbd", - "class": "nativeExecutable", - "install_dir": "bin", - "make_module_name": "adbd", - "package": "foo", - }, - }, - }, - }, - } - }), - ).RunTestWithBp(t, bp) - - m := result.ModuleForTests("foo", "android_common_foo").Module() - ab, ok := m.(*apexBundle) - - if !ok { - t.Fatalf("Expected module to be an apexBundle, was not") - } - - expectedFilesInfo := []apexFile{ - { - androidMkModuleName: "libcrypto", - builtFile: android.PathForTesting("out/bazel/execroot/__main__/bazel-out/64/libcrypto.so"), - class: nativeSharedLib, - customStem: "libcrypto.so", - installDir: "lib64", - moduleDir: "foo/bar", - arch: "arm64", - unstrippedBuiltFile: android.PathForTesting("out/bazel/execroot/__main__/bazel-out/64/unstripped_libcrypto.so"), - }, - { - androidMkModuleName: "libcrypto", - builtFile: android.PathForTesting("out/bazel/execroot/__main__/bazel-out/32/libcrypto.so"), - class: nativeSharedLib, - customStem: "libcrypto.so", - installDir: "lib", - moduleDir: "foo/bar", - arch: "arm", - }, - { - androidMkModuleName: "adbd", - builtFile: android.PathForTesting("out/bazel/execroot/__main__/bazel-out/adbd"), - class: nativeExecutable, - customStem: "adbd", - installDir: "bin", - moduleDir: "foo", - arch: "arm64", - }, - } - - if len(ab.filesInfo) != len(expectedFilesInfo) { - t.Errorf("Expected %d entries in ab.filesInfo, but got %d", len(ab.filesInfo), len(expectedFilesInfo)) - } - - for idx, f := range ab.filesInfo { - expected := expectedFilesInfo[idx] - android.AssertSame(t, "different class", expected.class, f.class) - android.AssertStringEquals(t, "different built file", expected.builtFile.String(), f.builtFile.String()) - android.AssertStringEquals(t, "different custom stem", expected.customStem, f.customStem) - android.AssertStringEquals(t, "different install dir", expected.installDir, f.installDir) - android.AssertStringEquals(t, "different make module name", expected.androidMkModuleName, f.androidMkModuleName) - android.AssertStringEquals(t, "different moduleDir", expected.moduleDir, f.moduleDir) - android.AssertStringEquals(t, "different arch", expected.arch, f.arch) - if expected.unstrippedBuiltFile != nil { - if f.unstrippedBuiltFile == nil { - t.Errorf("expected an unstripped built file path.") - } - android.AssertStringEquals(t, "different unstripped built file", expected.unstrippedBuiltFile.String(), f.unstrippedBuiltFile.String()) - } - } -} - -func TestCompressedApexImageInMixedBuilds(t *testing.T) { - bp := ` -apex_key{ - name: "foo_key", -} -apex { - name: "foo", - key: "foo_key", - updatable: true, - min_sdk_version: "31", - file_contexts: ":myapex-file_contexts", - bazel_module: { label: "//:foo" }, - test_only_force_compression: true, // force compression -}` - - outputBaseDir := "out/bazel" - result := android.GroupFixturePreparers( - prepareForApexTest, - android.FixtureModifyConfig(func(config android.Config) { - config.BazelContext = android.MockBazelContext{ - OutputBaseDir: outputBaseDir, - LabelToApexInfo: map[string]cquery.ApexInfo{ - "//:foo": cquery.ApexInfo{ - SignedOutput: "signed_out.apex", - SignedCompressedOutput: "signed_out.capex", - BundleKeyInfo: []string{"public_key", "private_key"}, - ContainerKeyInfo: []string{"container_cert", "container_private"}, - }, - }, - } - }), - ).RunTestWithBp(t, bp) - - m := result.ModuleForTests("foo", "android_common_foo").Module() - ab, ok := m.(*apexBundle) - if !ok { - t.Fatalf("Expected module to be an apexBundle, was not") - } - - if w, g := "out/bazel/execroot/__main__/signed_out.capex", ab.outputFile.String(); w != g { - t.Errorf("Expected output file to be compressed apex %q, got %q", w, g) - } - - mkData := android.AndroidMkDataForTest(t, result.TestContext, m) - var builder strings.Builder - mkData.Custom(&builder, "foo", "BAZEL_TARGET_", "", mkData) - - data := builder.String() - - expectedAndroidMk := []string{ - "LOCAL_PREBUILT_MODULE_FILE := out/bazel/execroot/__main__/signed_out.capex", - - // Check that the source install file is the capex. The dest is not important. - "LOCAL_SOONG_INSTALL_PAIRS := out/bazel/execroot/__main__/signed_out.capex:", - } - for _, androidMk := range expectedAndroidMk { - if !strings.Contains(data, androidMk) { - t.Errorf("Expected %q in androidmk data, but did not find %q", androidMk, data) - } - } -} - -func TestOverrideApexImageInMixedBuilds(t *testing.T) { - originalBp := ` -apex_key{ - name: "foo_key", -} -apex_key{ - name: "override_foo_key", -} -apex { - name: "foo", - key: "foo_key", - updatable: true, - min_sdk_version: "31", - package_name: "pkg_name", - file_contexts: ":myapex-file_contexts", - %s -}` - overrideBp := ` -override_apex { - name: "override_foo", - key: "override_foo_key", - package_name: "override_pkg_name", - base: "foo", - %s -} -` - - originalApexBpDir := "original" - originalApexName := "foo" - overrideApexBpDir := "override" - overrideApexName := "override_foo" - - defaultApexLabel := fmt.Sprintf("//%s:%s", originalApexBpDir, originalApexName) - defaultOverrideApexLabel := fmt.Sprintf("//%s:%s", overrideApexBpDir, overrideApexName) - - testCases := []struct { - desc string - bazelModuleProp string - apexLabel string - overrideBazelModuleProp string - overrideApexLabel string - bp2buildConfiguration android.Bp2BuildConversionAllowlist - }{ - { - desc: "both explicit labels", - bazelModuleProp: `bazel_module: { label: "//:foo" },`, - apexLabel: "//:foo", - overrideBazelModuleProp: `bazel_module: { label: "//:override_foo" },`, - overrideApexLabel: "//:override_foo", - bp2buildConfiguration: android.NewBp2BuildAllowlist(), - }, - { - desc: "both explicitly allowed", - bazelModuleProp: `bazel_module: { bp2build_available: true },`, - apexLabel: defaultApexLabel, - overrideBazelModuleProp: `bazel_module: { bp2build_available: true },`, - overrideApexLabel: defaultOverrideApexLabel, - bp2buildConfiguration: android.NewBp2BuildAllowlist(), - }, - { - desc: "original allowed by dir, override allowed by name", - apexLabel: defaultApexLabel, - overrideApexLabel: defaultOverrideApexLabel, - bp2buildConfiguration: android.NewBp2BuildAllowlist().SetDefaultConfig( - map[string]allowlists.BazelConversionConfigEntry{ - originalApexBpDir: allowlists.Bp2BuildDefaultTrue, - }).SetModuleAlwaysConvertList([]string{ - overrideApexName, - }), - }, - { - desc: "both allowed by name", - apexLabel: defaultApexLabel, - overrideApexLabel: defaultOverrideApexLabel, - bp2buildConfiguration: android.NewBp2BuildAllowlist().SetModuleAlwaysConvertList([]string{ - originalApexName, - overrideApexName, - }), - }, - { - desc: "override allowed by name", - apexLabel: defaultApexLabel, - overrideApexLabel: defaultOverrideApexLabel, - bp2buildConfiguration: android.NewBp2BuildAllowlist().SetModuleAlwaysConvertList([]string{ - overrideApexName, - }), - }, - { - desc: "override allowed by dir", - apexLabel: defaultApexLabel, - overrideApexLabel: defaultOverrideApexLabel, - bp2buildConfiguration: android.NewBp2BuildAllowlist().SetDefaultConfig( - map[string]allowlists.BazelConversionConfigEntry{ - overrideApexBpDir: allowlists.Bp2BuildDefaultTrue, - }).SetModuleAlwaysConvertList([]string{}), - }, - } - - for _, tc := range testCases { - t.Run(tc.desc, func(t *testing.T) { - outputBaseDir := "out/bazel" - result := android.GroupFixturePreparers( - prepareForApexTest, - android.FixtureAddTextFile(filepath.Join(originalApexBpDir, "Android.bp"), fmt.Sprintf(originalBp, tc.bazelModuleProp)), - android.FixtureAddTextFile(filepath.Join(overrideApexBpDir, "Android.bp"), fmt.Sprintf(overrideBp, tc.overrideBazelModuleProp)), - android.FixtureModifyContext(func(ctx *android.TestContext) { - ctx.RegisterBp2BuildConfig(tc.bp2buildConfiguration) - }), - android.FixtureModifyConfig(func(config android.Config) { - config.BazelContext = android.MockBazelContext{ - OutputBaseDir: outputBaseDir, - LabelToApexInfo: map[string]cquery.ApexInfo{ - tc.apexLabel: cquery.ApexInfo{ - // ApexInfo Starlark provider - SignedOutput: "signed_out.apex", - UnsignedOutput: "unsigned_out.apex", - BundleKeyInfo: []string{"public_key", "private_key"}, - ContainerKeyInfo: []string{"container_cert", "container_private"}, - SymbolsUsedByApex: "foo_using.txt", - JavaSymbolsUsedByApex: "foo_using.xml", - BundleFile: "apex_bundle.zip", - InstalledFiles: "installed-files.txt", - RequiresLibs: []string{"//path/c:c", "//path/d:d"}, - - // unused - PackageName: "pkg_name", - ProvidesLibs: []string{"a", "b"}, - - // ApexMkInfo Starlark provider - MakeModulesToInstall: []string{"c"}, // d deliberately omitted - }, - tc.overrideApexLabel: cquery.ApexInfo{ - // ApexInfo Starlark provider - SignedOutput: "override_signed_out.apex", - UnsignedOutput: "override_unsigned_out.apex", - BundleKeyInfo: []string{"override_public_key", "override_private_key"}, - ContainerKeyInfo: []string{"override_container_cert", "override_container_private"}, - SymbolsUsedByApex: "override_foo_using.txt", - JavaSymbolsUsedByApex: "override_foo_using.xml", - BundleFile: "override_apex_bundle.zip", - InstalledFiles: "override_installed-files.txt", - RequiresLibs: []string{"//path/c:c", "//path/d:d"}, - - // unused - PackageName: "override_pkg_name", - ProvidesLibs: []string{"a", "b"}, - - // ApexMkInfo Starlark provider - MakeModulesToInstall: []string{"c"}, // d deliberately omitted - }, - }, - } - }), - ).RunTest(t) - - m := result.ModuleForTests("foo", "android_common_override_foo_foo").Module() - ab, ok := m.(*apexBundle) - if !ok { - t.Fatalf("Expected module to be an apexBundle, was not") - } - - if w, g := "out/bazel/execroot/__main__/override_public_key", ab.publicKeyFile.String(); w != g { - t.Errorf("Expected public key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/override_private_key", ab.privateKeyFile.String(); w != g { - t.Errorf("Expected private key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/override_container_cert", ab.containerCertificateFile; g != nil && w != g.String() { - t.Errorf("Expected public container key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/override_container_private", ab.containerPrivateKeyFile; g != nil && w != g.String() { - t.Errorf("Expected private container key %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/override_signed_out.apex", ab.outputFile.String(); w != g { - t.Errorf("Expected output file %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/override_foo_using.txt", ab.nativeApisUsedByModuleFile.String(); w != g { - t.Errorf("Expected output file %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/override_foo_using.xml", ab.javaApisUsedByModuleFile.String(); w != g { - t.Errorf("Expected output file %q, got %q", w, g) - } - - if w, g := "out/bazel/execroot/__main__/override_installed-files.txt", ab.installedFilesFile.String(); w != g { - t.Errorf("Expected installed-files.txt %q, got %q", w, g) - } - - mkData := android.AndroidMkDataForTest(t, result.TestContext, m) - var builder strings.Builder - mkData.Custom(&builder, "override_foo", "BAZEL_TARGET_", "", mkData) - - data := builder.String() - if w := "ALL_MODULES.$(my_register_name).BUNDLE := out/bazel/execroot/__main__/override_apex_bundle.zip"; !strings.Contains(data, w) { - t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) - } - if w := "$(call dist-for-goals,checkbuild,out/bazel/execroot/__main__/override_installed-files.txt:override_foo-installed-files.txt)"; !strings.Contains(data, w) { - t.Errorf("Expected %q in androidmk data, but did not find %q", w, data) - } - - // make modules to be installed to system - if len(ab.makeModulesToInstall) != 1 || ab.makeModulesToInstall[0] != "c" { - t.Errorf("Expected makeModulestoInstall slice to only contain 'c', got %q", ab.makeModulesToInstall) - } - if w := "LOCAL_REQUIRED_MODULES := c"; !strings.Contains(data, w) { - t.Errorf("Expected %q in androidmk data, but did not find it in %q", w, data) - } - }) - } -} diff --git a/apex/key.go b/apex/key.go index 2405e9842..e4214f0e0 100644 --- a/apex/key.go +++ b/apex/key.go @@ -18,8 +18,6 @@ import ( "fmt" "android/soong/android" - "android/soong/bazel" - "github.com/google/blueprint/proptools" ) @@ -35,7 +33,6 @@ func registerApexKeyBuildComponents(ctx android.RegistrationContext) { type apexKey struct { android.ModuleBase - android.BazelModuleBase properties apexKeyProperties @@ -58,7 +55,6 @@ func ApexKeyFactory() android.Module { module := &apexKey{} module.AddProperties(&module.properties) android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) - android.InitBazelModule(module) return module } @@ -159,41 +155,3 @@ func writeApexKeys(ctx android.ModuleContext, module android.Module) android.Wri android.WriteFileRuleVerbatim(ctx, path, entry.String()) return path } - -// For Bazel / bp2build - -type bazelApexKeyAttributes struct { - Public_key bazel.LabelAttribute - Public_key_name bazel.StringAttribute - - Private_key bazel.LabelAttribute - Private_key_name bazel.StringAttribute -} - -// ConvertWithBp2build performs conversion apexKey for bp2build -func (m *apexKey) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { - apexKeyBp2BuildInternal(ctx, m) -} - -func apexKeyBp2BuildInternal(ctx android.Bp2buildMutatorContext, module *apexKey) { - privateKeyLabelAttribute, privateKeyNameAttribute := - android.BazelStringOrLabelFromProp(ctx, module.properties.Private_key) - - publicKeyLabelAttribute, publicKeyNameAttribute := - android.BazelStringOrLabelFromProp(ctx, module.properties.Public_key) - - attrs := &bazelApexKeyAttributes{ - Private_key: privateKeyLabelAttribute, - Private_key_name: privateKeyNameAttribute, - - Public_key: publicKeyLabelAttribute, - Public_key_name: publicKeyNameAttribute, - } - - props := bazel.BazelTargetModuleProperties{ - Rule_class: "apex_key", - Bzl_load_location: "//build/bazel/rules/apex:apex_key.bzl", - } - - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs) -} diff --git a/bp2build/Android.bp b/bp2build/Android.bp index c998074fe..aff703df0 100644 --- a/bp2build/Android.bp +++ b/bp2build/Android.bp @@ -40,64 +40,8 @@ bootstrap_go_package { "soong-ui-metrics", ], testSrcs: [ - "go_conversion_test.go", - "aar_conversion_test.go", - "aconfig_conversion_test.go", - "aidl_library_conversion_test.go", - "android_app_certificate_conversion_test.go", - "android_app_conversion_test.go", - "android_test_conversion_test.go", - "apex_conversion_test.go", - "apex_key_conversion_test.go", - "build_conversion_test.go", - "bp2build_product_config_test.go", - "bzl_conversion_test.go", - "cc_binary_conversion_test.go", - "cc_library_conversion_test.go", - "cc_library_headers_conversion_test.go", - "cc_library_shared_conversion_test.go", - "cc_library_static_conversion_test.go", - "cc_object_conversion_test.go", - "cc_prebuilt_library_conversion_test.go", - "cc_prebuilt_library_shared_test.go", - "cc_prebuilt_library_static_test.go", - "cc_prebuilt_object_conversion_test.go", - "cc_test_conversion_test.go", - "cc_yasm_conversion_test.go", "conversion_test.go", - "droiddoc_exported_dir_conversion_test.go", - "fdo_profile_conversion_test.go", - "filegroup_conversion_test.go", - "genrule_conversion_test.go", - "gensrcs_conversion_test.go", - "java_binary_host_conversion_test.go", - "java_host_for_device_conversion_test.go", - "java_import_conversion_test.go", - "java_library_conversion_test.go", - "java_library_host_conversion_test.go", - "java_plugin_conversion_test.go", - "java_proto_conversion_test.go", - "java_sdk_library_conversion_test.go", - "java_sdk_library_import_conversion_test.go", - "java_test_host_conversion_test.go", - "license_conversion_test.go", - "license_kind_conversion_test.go", - "linker_config_conversion_test.go", - "package_conversion_test.go", "performance_test.go", - "platform_compat_config_conversion_test.go", - "prebuilt_etc_conversion_test.go", - "python_binary_conversion_test.go", - "python_library_conversion_test.go", - "python_test_conversion_test.go", - "rust_binary_conversion_test.go", - "rust_ffi_conversion_test.go", - "rust_library_conversion_test.go", - "rust_proc_macro_conversion_test.go", - "rust_protobuf_conversion_test.go", - "sh_conversion_test.go", - "sh_test_conversion_test.go", - "soong_config_module_type_conversion_test.go", ], pluginFor: [ "soong_build", diff --git a/bp2build/aar_conversion_test.go b/bp2build/aar_conversion_test.go deleted file mode 100644 index bdad1f4cd..000000000 --- a/bp2build/aar_conversion_test.go +++ /dev/null @@ -1,249 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "testing" - - "android/soong/android" - "android/soong/java" -) - -func runAndroidLibraryImportTestWithRegistrationCtxFunc(t *testing.T, registrationCtxFunc func(ctx android.RegistrationContext), tc Bp2buildTestCase) { - t.Helper() - (&tc).ModuleTypeUnderTest = "android_library_import" - (&tc).ModuleTypeUnderTestFactory = java.AARImportFactory - RunBp2BuildTestCase(t, registrationCtxFunc, tc) -} - -func runAndroidLibraryImportTest(t *testing.T, tc Bp2buildTestCase) { - runAndroidLibraryImportTestWithRegistrationCtxFunc(t, func(ctx android.RegistrationContext) {}, tc) -} - -func TestConvertAndroidLibrary(t *testing.T) { - t.Helper() - RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{ - Description: "Android Library - simple example", - ModuleTypeUnderTest: "android_library", - ModuleTypeUnderTestFactory: java.AndroidLibraryFactory, - Filesystem: map[string]string{ - "lib.java": "", - "arm.java": "", - "x86.java": "", - "res/res.png": "", - "manifest/AndroidManifest.xml": "", - }, - StubbedBuildDefinitions: []string{"static_lib_dep"}, - Blueprint: simpleModule("android_library", "static_lib_dep") + ` -android_library { - name: "TestLib", - srcs: ["lib.java"], - arch: { - arm: { - srcs: ["arm.java"], - }, - x86: { - srcs: ["x86.java"], - } - }, - manifest: "manifest/AndroidManifest.xml", - static_libs: ["static_lib_dep"], - sdk_version: "current", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget( - "android_library", - "TestLib", - AttrNameToString{ - "srcs": `["lib.java"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arm.java"], - "//build/bazel_common_rules/platforms/arch:x86": ["x86.java"], - "//conditions:default": [], - })`, - "manifest": `"manifest/AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "deps": `[":static_lib_dep"]`, - "exports": `[":static_lib_dep"]`, - "sdk_version": `"current"`, // use as default - }), - MakeNeverlinkDuplicateTarget("android_library", "TestLib"), - }}) -} - -func TestConvertAndroidLibraryWithNoSources(t *testing.T) { - t.Helper() - RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{ - Description: "Android Library - modules will deps when there are no sources", - ModuleTypeUnderTest: "android_library", - ModuleTypeUnderTestFactory: java.AndroidLibraryFactory, - Filesystem: map[string]string{ - "res/res.png": "", - "AndroidManifest.xml": "", - }, - Blueprint: simpleModule("android_library", "lib_dep") + ` -android_library { - name: "TestLib", - srcs: [], - manifest: "AndroidManifest.xml", - libs: ["lib_dep"], - sdk_version: "current", -} -`, - StubbedBuildDefinitions: []string{"lib_dep"}, - ExpectedBazelTargets: []string{ - MakeBazelTarget( - "android_library", - "TestLib", - AttrNameToString{ - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "sdk_version": `"current"`, // use as default - }, - ), - MakeNeverlinkDuplicateTarget("android_library", "TestLib"), - }, - }) -} - -func TestConvertAndroidLibraryImport(t *testing.T) { - runAndroidLibraryImportTestWithRegistrationCtxFunc(t, - func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("android_library", java.AndroidLibraryFactory) - }, - Bp2buildTestCase{ - Description: "Android Library Import", - StubbedBuildDefinitions: []string{"static_lib_dep", "static_import_dep", "static_import_dep-neverlink"}, - // Bazel's aar_import can only export *_import targets, so we expect - // only "static_import_dep" in exports, but both "static_lib_dep" and - // "static_import_dep" in deps - Blueprint: simpleModule("android_library", "static_lib_dep") + ` -android_library_import { - name: "TestImport", - aars: ["import.aar"], - static_libs: ["static_lib_dep", "static_import_dep"], - sdk_version: "current", -} - -android_library_import { - name: "static_import_dep", - aars: ["import.aar"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget( - "aar_import", - "TestImport", - AttrNameToString{ - "aar": `"import.aar"`, - "deps": `[ - ":static_lib_dep", - ":static_import_dep", - ]`, - "exports": `[":static_import_dep"]`, - "sdk_version": `"current"`, // use as default - }, - ), - MakeNeverlinkDuplicateTarget("android_library", "TestImport"), - }, - }, - ) -} - -func TestConvertAndroidLibraryKotlin(t *testing.T) { - t.Helper() - RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{ - Description: "Android Library with .kt srcs and common_srcs attribute", - ModuleTypeUnderTest: "android_library", - ModuleTypeUnderTestFactory: java.AndroidLibraryFactory, - Filesystem: map[string]string{ - "AndroidManifest.xml": "", - }, - Blueprint: ` -android_library { - name: "TestLib", - srcs: ["a.java", "b.kt"], - common_srcs: ["c.kt"], - sdk_version: "current", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget( - "android_library", - "TestLib", - AttrNameToString{ - "srcs": `[ - "a.java", - "b.kt", - ]`, - "common_srcs": `["c.kt"]`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "sdk_version": `"current"`, // use as default - }), - MakeNeverlinkDuplicateTarget("android_library", "TestLib"), - }}) -} - -func TestConvertAndroidLibraryKotlinCflags(t *testing.T) { - t.Helper() - RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{ - Description: "Android Library with .kt srcs and kotlincflags ", - ModuleTypeUnderTest: "android_library", - ModuleTypeUnderTestFactory: java.AndroidLibraryFactory, - Filesystem: map[string]string{ - "AndroidManifest.xml": "", - }, - Blueprint: ` -android_library { - name: "TestLib", - srcs: ["a.java", "b.kt"], - kotlincflags: ["-flag1", "-flag2"], - sdk_version: "current", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget( - "android_library", - "TestLib", - AttrNameToString{ - "srcs": `[ - "a.java", - "b.kt", - ]`, - "kotlincflags": `[ - "-flag1", - "-flag2", - ]`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "sdk_version": `"current"`, // use as default - }), - MakeNeverlinkDuplicateTarget("android_library", "TestLib"), - }}) -} - -func TestAarImportFailsToConvertNoAars(t *testing.T) { - runAndroidLibraryImportTest(t, - Bp2buildTestCase{ - Description: "Android Library Import with no aars does not convert.", - Blueprint: ` -android_library_import { - name: "no_aar_import", -} -`, - ExpectedBazelTargets: []string{}, - }) -} diff --git a/bp2build/aconfig_conversion_test.go b/bp2build/aconfig_conversion_test.go deleted file mode 100644 index ac830c69f..000000000 --- a/bp2build/aconfig_conversion_test.go +++ /dev/null @@ -1,254 +0,0 @@ -// Copyright 2023 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "android/soong/aconfig/codegen" - "testing" - - "android/soong/aconfig" - "android/soong/android" - "android/soong/cc" - "android/soong/java" -) - -func registerAconfigModuleTypes(ctx android.RegistrationContext) { - aconfig.RegisterBuildComponents(ctx) - codegen.RegisterBuildComponents(ctx) - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - ctx.RegisterModuleType("java_library", java.LibraryFactory) -} - -func TestAconfigDeclarations(t *testing.T) { - bp := ` - aconfig_declarations { - name: "foo", - srcs: [ - "foo1.aconfig", - "test/foo2.aconfig", - ], - package: "com.android.foo", - } - ` - expectedBazelTarget := MakeBazelTargetNoRestrictions( - "aconfig_declarations", - "foo", - AttrNameToString{ - "srcs": `[ - "foo1.aconfig", - "test/foo2.aconfig", - ]`, - "package": `"com.android.foo"`, - }, - ) - RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: []string{expectedBazelTarget}, - }) -} - -func TestAconfigValues(t *testing.T) { - bp := ` - aconfig_values { - name: "foo", - srcs: [ - "foo1.textproto", - ], - package: "com.android.foo", - } - aconfig_value_set { - name: "bar", - values: [ - "foo" - ] - } - ` - expectedBazelTargets := []string{ - MakeBazelTargetNoRestrictions( - "aconfig_values", - "foo", - AttrNameToString{ - "srcs": `["foo1.textproto"]`, - "package": `"com.android.foo"`, - }, - ), - MakeBazelTargetNoRestrictions( - "aconfig_value_set", - "bar", - AttrNameToString{ - "values": `[":foo"]`, - }, - )} - RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - }) -} - -func TestCcAconfigLibrary(t *testing.T) { - bp := ` - aconfig_declarations { - name: "foo_aconfig_declarations", - srcs: [ - "foo1.aconfig", - ], - package: "com.android.foo", - } - cc_library { - name: "server_configurable_flags", - srcs: ["bar.cc"], - } - cc_aconfig_library { - name: "foo", - aconfig_declarations: "foo_aconfig_declarations", - } - ` - expectedBazelTargets := []string{ - MakeBazelTargetNoRestrictions( - "aconfig_declarations", - "foo_aconfig_declarations", - AttrNameToString{ - "srcs": `["foo1.aconfig"]`, - "package": `"com.android.foo"`, - }, - ), - MakeBazelTargetNoRestrictions( - "cc_aconfig_library", - "foo", - AttrNameToString{ - "aconfig_declarations": `":foo_aconfig_declarations"`, - "dynamic_deps": `[":server_configurable_flags"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - )} - RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - StubbedBuildDefinitions: []string{"server_configurable_flags"}, - }) -} - -func TestJavaAconfigLibrary(t *testing.T) { - bp := ` - aconfig_declarations { - name: "foo_aconfig_declarations", - srcs: [ - "foo1.aconfig", - ], - package: "com.android.foo", - } - java_library { - name: "foo_java_library", - srcs: ["foo.java"], - sdk_version: "current", - } - java_aconfig_library { - name: "foo", - aconfig_declarations: "foo_aconfig_declarations", - libs: ["foo_java_library"], - mode: "test", - } - ` - expectedBazelTargets := []string{ - MakeBazelTargetNoRestrictions( - "aconfig_declarations", - "foo_aconfig_declarations", - AttrNameToString{ - "srcs": `["foo1.aconfig"]`, - "package": `"com.android.foo"`, - }, - ), - MakeBazelTargetNoRestrictions( - "java_library", - "foo_java_library", - AttrNameToString{ - "srcs": `["foo.java"]`, - "sdk_version": `"current"`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - ), - MakeNeverlinkDuplicateTarget("java_library", "foo_java_library"), - MakeBazelTargetNoRestrictions( - "java_aconfig_library", - "foo", - AttrNameToString{ - "aconfig_declarations": `":foo_aconfig_declarations"`, - "libs": `[":foo_java_library-neverlink"]`, - "sdk_version": `"system_current"`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - )} - RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - }) -} - -func TestJavaAconfigLibraryAsTaggedOutput(t *testing.T) { - bp := ` - aconfig_declarations { - name: "foo_aconfig_declarations", - srcs: [ - "foo.aconfig", - ], - package: "com.android.foo", - } - java_library { - name: "foo_library", - srcs: [":foo_aconfig_library{.generated_srcjars}"], - sdk_version: "current", - bazel_module: { bp2build_available: true }, - } - java_aconfig_library { - name: "foo_aconfig_library", - aconfig_declarations: "foo_aconfig_declarations", - mode: "test", - } - ` - expectedBazelTargets := []string{ - MakeBazelTargetNoRestrictions( - "aconfig_declarations", - "foo_aconfig_declarations", - AttrNameToString{ - "srcs": `["foo.aconfig"]`, - "package": `"com.android.foo"`, - }, - ), - MakeBazelTargetNoRestrictions( - "java_aconfig_library", - "foo_aconfig_library", - AttrNameToString{ - "aconfig_declarations": `":foo_aconfig_declarations"`, - "sdk_version": `"system_current"`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - ), - MakeBazelTargetNoRestrictions( - "java_library", - "foo_library", - AttrNameToString{ - "srcs": `[":foo_aconfig_library.generated_srcjars"]`, - "sdk_version": `"current"`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - ), - MakeNeverlinkDuplicateTarget("java_library", "foo_library"), - } - - RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - }) -} diff --git a/bp2build/aidl_library_conversion_test.go b/bp2build/aidl_library_conversion_test.go deleted file mode 100644 index 0522da45e..000000000 --- a/bp2build/aidl_library_conversion_test.go +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2023 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "testing" - - "android/soong/aidl_library" - "android/soong/android" -) - -func runAidlLibraryTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - (&tc).ModuleTypeUnderTest = "aidl_library" - (&tc).ModuleTypeUnderTestFactory = aidl_library.AidlLibraryFactory - RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) -} - -func TestAidlLibrary(t *testing.T) { - testcases := []struct { - name string - bp string - expectedBazelAttrs AttrNameToString - }{ - { - name: "aidl_library with strip_import_prefix", - bp: ` - aidl_library { - name: "foo", - srcs: ["aidl/foo.aidl"], - hdrs: ["aidl/header.aidl"], - strip_import_prefix: "aidl", - }`, - expectedBazelAttrs: AttrNameToString{ - "srcs": `["aidl/foo.aidl"]`, - "hdrs": `["aidl/header.aidl"]`, - "strip_import_prefix": `"aidl"`, - "tags": `["apex_available=//apex_available:anyapex"]`, - }, - }, - { - name: "aidl_library without strip_import_prefix", - bp: ` - aidl_library { - name: "foo", - srcs: ["aidl/foo.aidl"], - hdrs: ["aidl/header.aidl"], - }`, - expectedBazelAttrs: AttrNameToString{ - "srcs": `["aidl/foo.aidl"]`, - "hdrs": `["aidl/header.aidl"]`, - "tags": `["apex_available=//apex_available:anyapex"]`, - }, - }, - } - - for _, test := range testcases { - t.Run(test.name, func(t *testing.T) { - expectedBazelTargets := []string{ - MakeBazelTargetNoRestrictions("aidl_library", "foo", test.expectedBazelAttrs), - } - runAidlLibraryTestCase(t, Bp2buildTestCase{ - Description: test.name, - Blueprint: test.bp, - ExpectedBazelTargets: expectedBazelTargets, - }) - }) - } -} - -func TestAidlLibraryWithDeps(t *testing.T) { - bp := ` - aidl_library { - name: "bar", - srcs: ["Bar.aidl"], - hdrs: ["aidl/BarHeader.aidl"], - } - aidl_library { - name: "foo", - srcs: ["aidl/Foo.aidl"], - hdrs: ["aidl/FooHeader.aidl"], - strip_import_prefix: "aidl", - deps: ["bar"], - }` - - t.Run("aidl_library with deps", func(t *testing.T) { - expectedBazelTargets := []string{ - MakeBazelTargetNoRestrictions("aidl_library", "bar", AttrNameToString{ - "srcs": `["Bar.aidl"]`, - "hdrs": `["aidl/BarHeader.aidl"]`, - "tags": `["apex_available=//apex_available:anyapex"]`, - }), - MakeBazelTargetNoRestrictions("aidl_library", "foo", AttrNameToString{ - "srcs": `["aidl/Foo.aidl"]`, - "hdrs": `["aidl/FooHeader.aidl"]`, - "strip_import_prefix": `"aidl"`, - "deps": `[":bar"]`, - "tags": `["apex_available=//apex_available:anyapex"]`, - }), - } - runAidlLibraryTestCase(t, Bp2buildTestCase{ - Description: "aidl_library with deps", - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - }) - }) -} diff --git a/bp2build/android_app_certificate_conversion_test.go b/bp2build/android_app_certificate_conversion_test.go deleted file mode 100644 index 01045134c..000000000 --- a/bp2build/android_app_certificate_conversion_test.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "android/soong/android" - "android/soong/java" - - "testing" -) - -func runAndroidAppCertificateTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - RunBp2BuildTestCase(t, registerAndroidAppCertificateModuleTypes, tc) -} - -func registerAndroidAppCertificateModuleTypes(ctx android.RegistrationContext) { -} - -func TestAndroidAppCertificateSimple(t *testing.T) { - runAndroidAppCertificateTestCase(t, Bp2buildTestCase{ - Description: "Android app certificate - simple example", - ModuleTypeUnderTest: "android_app_certificate", - ModuleTypeUnderTestFactory: java.AndroidAppCertificateFactory, - Filesystem: map[string]string{}, - Blueprint: ` -android_app_certificate { - name: "com.android.apogee.cert", - certificate: "chamber_of_secrets_dir", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("android_app_certificate", "com.android.apogee.cert", AttrNameToString{ - "certificate": `"chamber_of_secrets_dir"`, - }), - }}) -} diff --git a/bp2build/android_app_conversion_test.go b/bp2build/android_app_conversion_test.go deleted file mode 100644 index 33d1a040d..000000000 --- a/bp2build/android_app_conversion_test.go +++ /dev/null @@ -1,518 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "android/soong/android" - "android/soong/cc" - "android/soong/java" - - "testing" -) - -func runAndroidAppTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - RunBp2BuildTestCase(t, registerAndroidAppModuleTypes, tc) -} - -func registerAndroidAppModuleTypes(ctx android.RegistrationContext) { - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.RegisterModuleType("java_library", java.LibraryFactory) - ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory) -} - -func TestMinimalAndroidApp(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app - simple example", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "app.java": "", - "res/res.png": "", - "AndroidManifest.xml": "", - "assets/asset.png": "", - }, - Blueprint: ` -android_app { - name: "TestApp", - srcs: ["app.java"], - sdk_version: "current", - optimize: { - shrink: true, - optimize: true, - obfuscate: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "srcs": `["app.java"]`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "sdk_version": `"current"`, - "assets": `["assets/asset.png"]`, - "assets_dir": `"assets"`, - }), - }}) -} - -func TestAndroidAppAllSupportedFields(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app - all supported fields", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "app.java": "", - "resa/res.png": "", - "resb/res.png": "", - "manifest/AndroidManifest.xml": "", - "assets_/asset.png": "", - }, - StubbedBuildDefinitions: []string{"static_lib_dep", "jni_lib"}, - Blueprint: simpleModule("android_app", "static_lib_dep") + - simpleModule("cc_library_shared", "jni_lib") + ` -android_app { - name: "TestApp", - srcs: ["app.java"], - sdk_version: "current", - package_name: "com.google", - resource_dirs: ["resa", "resb"], - manifest: "manifest/AndroidManifest.xml", - static_libs: ["static_lib_dep"], - java_version: "7", - certificate: "foocert", - required: ["static_lib_dep"], - asset_dirs: ["assets_"], - optimize: { - enabled: true, - optimize: false, - proguard_flags_files: ["proguard.flags"], - shrink: false, - obfuscate: false, - ignore_warnings: true, - }, - jni_libs: ["jni_lib"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "srcs": `["app.java"]`, - "manifest": `"manifest/AndroidManifest.xml"`, - "resource_files": `[ - "resa/res.png", - "resb/res.png", - ]`, - "assets": `["assets_/asset.png"]`, - "assets_dir": `"assets_"`, - "custom_package": `"com.google"`, - "deps": `[ - ":static_lib_dep", - ":jni_lib", - ]`, - "java_version": `"7"`, - "sdk_version": `"current"`, - "certificate_name": `"foocert"`, - "proguard_specs": `[ - "proguard.flags", - ":TestApp_proguard_flags", - ]`, - }), - MakeBazelTarget("genrule", "TestApp_proguard_flags", AttrNameToString{ - "outs": `["TestApp_proguard.flags"]`, - "cmd": `"echo -ignorewarning -dontshrink -dontoptimize -dontobfuscate > $(OUTS)"`, - }), - }}) -} - -func TestAndroidAppArchVariantSrcs(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app - arch variant srcs", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "arm.java": "", - "x86.java": "", - "res/res.png": "", - "AndroidManifest.xml": "", - }, - Blueprint: ` -android_app { - name: "TestApp", - sdk_version: "current", - arch: { - arm: { - srcs: ["arm.java"], - }, - x86: { - srcs: ["x86.java"], - } - }, - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "srcs": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arm.java"], - "//build/bazel_common_rules/platforms/arch:x86": ["x86.java"], - "//conditions:default": [], - })`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "sdk_version": `"current"`, - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppCertIsModule(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app - cert is module", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"foocert"}, - Blueprint: simpleModule("filegroup", "foocert") + ` -android_app { - name: "TestApp", - certificate: ":foocert", - sdk_version: "current", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "certificate": `":foocert"`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "sdk_version": `"current"`, // use as default - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppCertIsSrcFile(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app - cert is src file", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "foocert": "", - }, - Blueprint: ` -android_app { - name: "TestApp", - certificate: "foocert", - sdk_version: "current", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "certificate": `"foocert"`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "sdk_version": `"current"`, // use as default - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppCertIsNotSrcOrModule(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app - cert is not src or module", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - // deliberate empty - }, - Blueprint: ` -android_app { - name: "TestApp", - certificate: "foocert", - sdk_version: "current", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "certificate_name": `"foocert"`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "sdk_version": `"current"`, // use as default - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppLibs(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app with libs", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"barLib"}, - Blueprint: simpleModule("java_library", "barLib") + ` -android_app { - name: "foo", - libs: ["barLib"], - sdk_version: "current", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "foo", AttrNameToString{ - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "deps": `[":barLib-neverlink"]`, - "sdk_version": `"current"`, // use as default - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppKotlinSrcs(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app with kotlin sources and common_srcs", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "res/res.png": "", - }, - StubbedBuildDefinitions: []string{"foocert", "barLib"}, - Blueprint: simpleModule("filegroup", "foocert") + - simpleModule("java_library", "barLib") + ` -android_app { - name: "foo", - srcs: ["a.java", "b.kt"], - certificate: ":foocert", - manifest: "fooManifest.xml", - libs: ["barLib"], - sdk_version: "current", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_library", "foo_kt", AttrNameToString{ - "srcs": `[ - "a.java", - "b.kt", - ]`, - "manifest": `"fooManifest.xml"`, - "resource_files": `["res/res.png"]`, - "deps": `[":barLib-neverlink"]`, - "sdk_version": `"current"`, // use as default - }), - MakeBazelTarget("android_binary", "foo", AttrNameToString{ - "deps": `[":foo_kt"]`, - "certificate": `":foocert"`, - "manifest": `"fooManifest.xml"`, - "sdk_version": `"current"`, // use as default - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppCommonSrcs(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app with common_srcs", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "res/res.png": "", - }, - StubbedBuildDefinitions: []string{"barLib"}, - Blueprint: ` -android_app { - name: "foo", - srcs: ["a.java"], - common_srcs: ["b.kt"], - manifest: "fooManifest.xml", - libs: ["barLib"], - sdk_version: "current", - optimize: { - enabled: false, - }, -} -java_library{ - name: "barLib", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_library", "foo_kt", AttrNameToString{ - "srcs": `["a.java"]`, - "common_srcs": `["b.kt"]`, - "manifest": `"fooManifest.xml"`, - "resource_files": `["res/res.png"]`, - "deps": `[":barLib-neverlink"]`, - "sdk_version": `"current"`, // use as default - }), - MakeBazelTarget("android_binary", "foo", AttrNameToString{ - "deps": `[":foo_kt"]`, - "manifest": `"fooManifest.xml"`, - "sdk_version": `"current"`, // use as default - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppKotlinCflags(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app with kotlincflags", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "res/res.png": "", - }, - Blueprint: ` -android_app { - name: "foo", - srcs: ["a.java", "b.kt"], - manifest: "fooManifest.xml", - kotlincflags: ["-flag1", "-flag2"], - sdk_version: "current", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_library", "foo_kt", AttrNameToString{ - "srcs": `[ - "a.java", - "b.kt", - ]`, - "manifest": `"fooManifest.xml"`, - "resource_files": `["res/res.png"]`, - "kotlincflags": `[ - "-flag1", - "-flag2", - ]`, - "sdk_version": `"current"`, // use as default - }), - MakeBazelTarget("android_binary", "foo", AttrNameToString{ - "deps": `[":foo_kt"]`, - "manifest": `"fooManifest.xml"`, - "sdk_version": `"current"`, - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppManifestSdkVersionsProvided(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app with value for min_sdk_version", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{}, - Blueprint: ` -android_app { - name: "foo", - sdk_version: "current", - min_sdk_version: "24", - target_sdk_version: "29", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "foo", AttrNameToString{ - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "manifest_values": `{ - "minSdkVersion": "24", - "targetSdkVersion": "29", - }`, - "sdk_version": `"current"`, - "optimize": `False`, - }), - }}) -} - -func TestAndroidAppMinAndTargetSdkDefaultToSdkVersion(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android app with value for sdk_version", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{}, - Blueprint: ` -android_app { - name: "foo", - sdk_version: "30", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "foo", AttrNameToString{ - "manifest": `"AndroidManifest.xml"`, - "resource_files": `[]`, - "sdk_version": `"30"`, - "optimize": `False`, - }), - }}) -} - -func TestFrameworkResConversion(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Framework Res custom conversion", - ModuleTypeUnderTest: "android_app", - ModuleTypeUnderTestFactory: java.AndroidAppFactory, - Filesystem: map[string]string{ - "res/values/attrs.xml": "", - "resource_zip.zip": "", - }, - Blueprint: ` -android_app { - name: "framework-res", - resource_zips: [ - "resource_zip.zip", - ], - certificate: "platform", -} - -filegroup { - name: "framework-res-package-jar", - srcs: [":framework-res{.export-package.apk}"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("framework_resources", "framework-res", AttrNameToString{ - "certificate_name": `"platform"`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/values/attrs.xml"]`, - "resource_zips": `["resource_zip.zip"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }), - MakeBazelTargetNoRestrictions("filegroup", "framework-res-package-jar", AttrNameToString{ - "srcs": `[":framework-res.export-package.apk"]`, - }), - }}) - -} diff --git a/bp2build/android_test_conversion_test.go b/bp2build/android_test_conversion_test.go deleted file mode 100644 index 486f154ca..000000000 --- a/bp2build/android_test_conversion_test.go +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright 2023 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "android/soong/android" - "android/soong/java" - - "testing" -) - -func runAndroidTestTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - RunBp2BuildTestCase(t, registerAndroidTestModuleTypes, tc) -} - -func registerAndroidTestModuleTypes(ctx android.RegistrationContext) { - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.RegisterModuleType("java_library", java.LibraryFactory) -} - -func TestMinimalAndroidTest(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android test - simple example", - ModuleTypeUnderTest: "android_test", - ModuleTypeUnderTestFactory: java.AndroidTestFactory, - Filesystem: map[string]string{ - "app.java": "", - "res/res.png": "", - "AndroidManifest.xml": "", - "assets/asset.png": "", - }, - Blueprint: ` -android_test { - name: "TestApp", - srcs: ["app.java"], - sdk_version: "current", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_test", "TestApp", AttrNameToString{ - "srcs": `["app.java"]`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "sdk_version": `"current"`, - "assets": `["assets/asset.png"]`, - "assets_dir": `"assets"`, - // no need for optimize = False because it's false for - // android_test by default - }), - }}) -} - -func TestAndroidTest_OptimizationEnabled(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android test - simple example", - ModuleTypeUnderTest: "android_test", - ModuleTypeUnderTestFactory: java.AndroidTestFactory, - Filesystem: map[string]string{ - "app.java": "", - "res/res.png": "", - "AndroidManifest.xml": "", - "assets/asset.png": "", - }, - Blueprint: ` -android_test { - name: "TestApp", - srcs: ["app.java"], - sdk_version: "current", - optimize: { - enabled: true, - shrink: true, - optimize: true, - obfuscate: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_test", "TestApp", AttrNameToString{ - "srcs": `["app.java"]`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "sdk_version": `"current"`, - "assets": `["assets/asset.png"]`, - "assets_dir": `"assets"`, - // optimize = True because it's false for android_test by - // default - "optimize": `True`, - }), - }}) -} - -func TestMinimalAndroidTestHelperApp(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android test helper app - simple example", - ModuleTypeUnderTest: "android_test_helper_app", - ModuleTypeUnderTestFactory: java.AndroidTestHelperAppFactory, - Filesystem: map[string]string{ - "app.java": "", - "res/res.png": "", - "AndroidManifest.xml": "", - "assets/asset.png": "", - }, - Blueprint: ` -android_test_helper_app { - name: "TestApp", - srcs: ["app.java"], - sdk_version: "current", - optimize: { - shrink: true, - optimize: true, - obfuscate: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "srcs": `["app.java"]`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "sdk_version": `"current"`, - "assets": `["assets/asset.png"]`, - "assets_dir": `"assets"`, - "testonly": `True`, - // no need for optimize = True because it's true for - // android_test_helper_app by default - }), - }}) -} - -func TestAndroidTestHelperApp_OptimizationDisabled(t *testing.T) { - runAndroidAppTestCase(t, Bp2buildTestCase{ - Description: "Android test helper app - simple example", - ModuleTypeUnderTest: "android_test_helper_app", - ModuleTypeUnderTestFactory: java.AndroidTestHelperAppFactory, - Filesystem: map[string]string{ - "app.java": "", - "res/res.png": "", - "AndroidManifest.xml": "", - "assets/asset.png": "", - }, - Blueprint: ` -android_test_helper_app { - name: "TestApp", - srcs: ["app.java"], - sdk_version: "current", - optimize: { - enabled: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("android_binary", "TestApp", AttrNameToString{ - "srcs": `["app.java"]`, - "manifest": `"AndroidManifest.xml"`, - "resource_files": `["res/res.png"]`, - "sdk_version": `"current"`, - "assets": `["assets/asset.png"]`, - "assets_dir": `"assets"`, - "testonly": `True`, - // optimize = False because it's true for - // android_test_helper_app by default - "optimize": `False`, - }), - }}) -} diff --git a/bp2build/apex_conversion_test.go b/bp2build/apex_conversion_test.go deleted file mode 100644 index bdb655b78..000000000 --- a/bp2build/apex_conversion_test.go +++ /dev/null @@ -1,1562 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "android/soong/android" - "android/soong/apex" - "android/soong/cc" - "android/soong/etc" - "android/soong/java" - "android/soong/sh" - - "fmt" - "testing" -) - -func runApexTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - RunBp2BuildTestCase(t, registerApexModuleTypes, tc) -} - -func registerApexModuleTypes(ctx android.RegistrationContext) { - // CC module types needed as they can be APEX dependencies - cc.RegisterCCBuildComponents(ctx) - - ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory) - ctx.RegisterModuleType("cc_binary", cc.BinaryFactory) - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) - ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory) - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.RegisterModuleType("prebuilt_etc", etc.PrebuiltEtcFactory) - ctx.RegisterModuleType("cc_test", cc.TestFactory) -} - -func runOverrideApexTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - RunBp2BuildTestCase(t, registerOverrideApexModuleTypes, tc) -} - -func registerOverrideApexModuleTypes(ctx android.RegistrationContext) { - // CC module types needed as they can be APEX dependencies - cc.RegisterCCBuildComponents(ctx) - - ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory) - ctx.RegisterModuleType("cc_binary", cc.BinaryFactory) - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) - ctx.RegisterModuleType("apex_test", apex.TestApexBundleFactory) - ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory) - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.RegisterModuleType("apex", apex.BundleFactory) - ctx.RegisterModuleType("apex_defaults", apex.DefaultsFactory) - ctx.RegisterModuleType("prebuilt_etc", etc.PrebuiltEtcFactory) - ctx.RegisterModuleType("soong_config_module_type", android.SoongConfigModuleTypeFactory) - ctx.RegisterModuleType("soong_config_string_variable", android.SoongConfigStringVariableDummyFactory) -} - -func TestApexBundleSimple(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - example with all props, file_context is a module in same Android.bp", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee.key", "com.android.apogee.certificate", "native_shared_lib_1", "native_shared_lib_2", - "prebuilt_1", "prebuilt_2", "com.android.apogee-file_contexts", "cc_binary_1", "sh_binary_2"}, - Blueprint: ` -apex_key { - name: "com.android.apogee.key", - public_key: "com.android.apogee.avbpubkey", - private_key: "com.android.apogee.pem", -} - -android_app_certificate { - name: "com.android.apogee.certificate", - certificate: "com.android.apogee", -} - -cc_library { - name: "native_shared_lib_1", -} - -cc_library { - name: "native_shared_lib_2", -} - -prebuilt_etc { - name: "prebuilt_1", -} - -prebuilt_etc { - name: "prebuilt_2", -} - -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} - -cc_binary { name: "cc_binary_1"} -sh_binary { name: "sh_binary_2", src: "foo.sh"} - -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - androidManifest: "ApogeeAndroidManifest.xml", - apex_available_name: "apogee_apex_name", - file_contexts: ":com.android.apogee-file_contexts", - min_sdk_version: "29", - key: "com.android.apogee.key", - certificate: ":com.android.apogee.certificate", - updatable: false, - installable: false, - compressible: false, - native_shared_libs: [ - "native_shared_lib_1", - "native_shared_lib_2", - ], - binaries: [ - "cc_binary_1", - "sh_binary_2", - ], - prebuilts: [ - "prebuilt_1", - "prebuilt_2", - ], - package_name: "com.android.apogee.test.package", - logging_parent: "logging.parent", - variant_version: "3", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "android_manifest": `"ApogeeAndroidManifest.xml"`, - "apex_available_name": `"apogee_apex_name"`, - "binaries": `[ - ":cc_binary_1", - ":sh_binary_2", - ]`, - "certificate": `":com.android.apogee.certificate"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "installable": "False", - "key": `":com.android.apogee.key"`, - "manifest": `"apogee_manifest.json"`, - "min_sdk_version": `"29"`, - "native_shared_libs_32": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//conditions:default": [], - })`, - "native_shared_libs_64": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//conditions:default": [], - })`, - "prebuilts": `[ - ":prebuilt_1", - ":prebuilt_2", - ]`, - "updatable": "False", - "compressible": "False", - "package_name": `"com.android.apogee.test.package"`, - "logging_parent": `"logging.parent"`, - "variant_version": `"3"`, - }), - }}) -} - -func TestApexBundleSimple_fileContextsInAnotherAndroidBp(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - file contexts is a module in another Android.bp", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: []string{"//a/b:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "a/b/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} -`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", - file_contexts: ":com.android.apogee-file_contexts", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "file_contexts": `"//a/b:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} - -func TestApexBundleSimple_fileContextsIsFile(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - file contexts is a file", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - Filesystem: map[string]string{}, - Blueprint: ` -apex { - name: "com.android.apogee", - file_contexts: "file_contexts_file", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "file_contexts": `"file_contexts_file"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} - -func TestApexBundleSimple_fileContextsIsNotSpecified(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - file contexts is not specified", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: []string{"//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} -`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} - -func TestApexBundleCompileMultilibBoth(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - example with compile_multilib=both", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: append(multilibStubNames(), "//system/sepolicy/apex:com.android.apogee-file_contexts"), - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -} -`, - }, - Blueprint: createMultilibBlueprint(`compile_multilib: "both",`), - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "native_shared_libs_32": `[ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib32", - ] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [":native_shared_lib_for_first"], - "//build/bazel_common_rules/platforms/arch:x86": [":native_shared_lib_for_first"], - "//conditions:default": [], - })`, - "native_shared_libs_64": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib64", - ":native_shared_lib_for_first", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib64", - ":native_shared_lib_for_first", - ], - "//conditions:default": [], - })`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} - -func TestApexBundleCompileMultilibFirstAndDefaultValue(t *testing.T) { - expectedBazelTargets := []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "native_shared_libs_32": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib32", - ":native_shared_lib_for_first", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib32", - ":native_shared_lib_for_first", - ], - "//conditions:default": [], - })`, - "native_shared_libs_64": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib64", - ":native_shared_lib_for_first", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib64", - ":native_shared_lib_for_first", - ], - "//conditions:default": [], - })`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - }), - } - - // "first" is the default value of compile_multilib prop so `compile_multilib_: "first"` and unset compile_multilib - // should result to the same bp2build output - compileMultiLibPropValues := []string{`compile_multilib: "first",`, ""} - for _, compileMultiLibProp := range compileMultiLibPropValues { - descriptionSuffix := compileMultiLibProp - if descriptionSuffix == "" { - descriptionSuffix = "compile_multilib unset" - } - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - example with " + compileMultiLibProp, - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: append(multilibStubNames(), "//system/sepolicy/apex:com.android.apogee-file_contexts"), - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` - filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], - } - `, - }, - Blueprint: createMultilibBlueprint(compileMultiLibProp), - ExpectedBazelTargets: expectedBazelTargets, - }) - } -} - -func TestApexBundleCompileMultilib32(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - example with compile_multilib=32", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: append(multilibStubNames(), "//system/sepolicy/apex:com.android.apogee-file_contexts"), - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -} -`, - }, - Blueprint: createMultilibBlueprint(`compile_multilib: "32",`), - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "native_shared_libs_32": `[ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib32", - ] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [":native_shared_lib_for_first"], - "//build/bazel_common_rules/platforms/arch:x86": [":native_shared_lib_for_first"], - "//conditions:default": [], - })`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} - -func TestApexBundleCompileMultilib64(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - example with compile_multilib=64", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: append(multilibStubNames(), "//system/sepolicy/apex:com.android.apogee-file_contexts"), - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -} -`, - }, - Blueprint: createMultilibBlueprint(`compile_multilib: "64",`), - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "native_shared_libs_64": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib64", - ":native_shared_lib_for_first", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - ":unnested_native_shared_lib", - ":native_shared_lib_for_both", - ":native_shared_lib_for_lib64", - ":native_shared_lib_for_first", - ], - "//conditions:default": [], - })`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} - -func multilibStubNames() []string { - return []string{"native_shared_lib_for_both", "native_shared_lib_for_first", "native_shared_lib_for_lib32", "native_shared_lib_for_lib64", - "native_shared_lib_for_lib64", "unnested_native_shared_lib"} -} - -func createMultilibBlueprint(compile_multilib string) string { - return fmt.Sprintf(` -cc_library { - name: "native_shared_lib_for_both", -} - -cc_library { - name: "native_shared_lib_for_first", -} - -cc_library { - name: "native_shared_lib_for_lib32", -} - -cc_library { - name: "native_shared_lib_for_lib64", -} - -cc_library { - name: "unnested_native_shared_lib", -} - -apex { - name: "com.android.apogee", - %s - native_shared_libs: ["unnested_native_shared_lib"], - multilib: { - both: { - native_shared_libs: [ - "native_shared_lib_for_both", - ], - }, - first: { - native_shared_libs: [ - "native_shared_lib_for_first", - ], - }, - lib32: { - native_shared_libs: [ - "native_shared_lib_for_lib32", - ], - }, - lib64: { - native_shared_libs: [ - "native_shared_lib_for_lib64", - ], - }, - }, -}`, compile_multilib) -} - -func TestApexBundleDefaultPropertyValues(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - default property values", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: []string{"//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -} -`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", -} -`, - ExpectedBazelTargets: []string{MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "manifest": `"apogee_manifest.json"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - }), - }}) -} - -func TestApexBundleHasBazelModuleProps(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - has bazel module props", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - StubbedBuildDefinitions: []string{"//system/sepolicy/apex:apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -} -`, - }, - Blueprint: ` -apex { - name: "apogee", - manifest: "manifest.json", - bazel_module: { bp2build_available: true }, -} -`, - ExpectedBazelTargets: []string{MakeBazelTarget("apex", "apogee", AttrNameToString{ - "manifest": `"manifest.json"`, - "file_contexts": `"//system/sepolicy/apex:apogee-file_contexts"`, - }), - }}) -} - -func TestBp2BuildOverrideApex(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee.key", "com.android.apogee.certificate", "native_shared_lib_1", - "native_shared_lib_2", "prebuilt_1", "prebuilt_2", "com.android.apogee-file_contexts", "cc_binary_1", - "sh_binary_2", "com.android.apogee", "com.google.android.apogee.key", "com.google.android.apogee.certificate"}, - Blueprint: ` -apex_key { - name: "com.android.apogee.key", - public_key: "com.android.apogee.avbpubkey", - private_key: "com.android.apogee.pem", -} - -android_app_certificate { - name: "com.android.apogee.certificate", - certificate: "com.android.apogee", -} - -cc_library { - name: "native_shared_lib_1", -} - -cc_library { - name: "native_shared_lib_2", -} - -prebuilt_etc { - name: "prebuilt_1", -} - -prebuilt_etc { - name: "prebuilt_2", -} - -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} - -cc_binary { name: "cc_binary_1" } -sh_binary { name: "sh_binary_2", src: "foo.sh"} - -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - androidManifest: "ApogeeAndroidManifest.xml", - file_contexts: ":com.android.apogee-file_contexts", - min_sdk_version: "29", - key: "com.android.apogee.key", - certificate: ":com.android.apogee.certificate", - updatable: false, - installable: false, - compressible: false, - native_shared_libs: [ - "native_shared_lib_1", - "native_shared_lib_2", - ], - binaries: [ - "cc_binary_1", - "sh_binary_2", - ], - prebuilts: [ - "prebuilt_1", - "prebuilt_2", - ], -} - -apex_key { - name: "com.google.android.apogee.key", - public_key: "com.google.android.apogee.avbpubkey", - private_key: "com.google.android.apogee.pem", -} - -android_app_certificate { - name: "com.google.android.apogee.certificate", - certificate: "com.google.android.apogee", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - key: "com.google.android.apogee.key", - certificate: ":com.google.android.apogee.certificate", - prebuilts: [], - compressible: true, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "android_manifest": `"ApogeeAndroidManifest.xml"`, - "base_apex_name": `"com.android.apogee"`, - "binaries": `[ - ":cc_binary_1", - ":sh_binary_2", - ]`, - "certificate": `":com.google.android.apogee.certificate"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "installable": "False", - "key": `":com.google.android.apogee.key"`, - "manifest": `"apogee_manifest.json"`, - "min_sdk_version": `"29"`, - "native_shared_libs_32": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//conditions:default": [], - })`, - "native_shared_libs_64": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - ":native_shared_lib_1", - ":native_shared_lib_2", - ], - "//conditions:default": [], - })`, - "prebuilts": `[]`, - "updatable": "False", - "compressible": "True", - }), - }}) -} - -func TestOverrideApexTest(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee.certificate", "native_shared_lib_1", - "prebuilt_1", "com.android.apogee-file_contexts", "cc_binary_1", "sh_binary_2", - "com.android.apogee", "com.google.android.apogee.key", "com.google.android.apogee.certificate", "com.android.apogee.key"}, - Blueprint: ` -apex_key { - name: "com.android.apogee.key", - public_key: "com.android.apogee.avbpubkey", - private_key: "com.android.apogee.pem", -} - -android_app_certificate { - name: "com.android.apogee.certificate", - certificate: "com.android.apogee", -} - -cc_library { - name: "native_shared_lib_1", -} - -prebuilt_etc { - name: "prebuilt_1", -} - -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} - -cc_binary { name: "cc_binary_1"} -sh_binary { name: "sh_binary_2", src: "foo.sh"} - -apex_test { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - androidManifest: "ApogeeAndroidManifest.xml", - file_contexts: ":com.android.apogee-file_contexts", - min_sdk_version: "29", - key: "com.android.apogee.key", - certificate: ":com.android.apogee.certificate", - updatable: false, - installable: false, - compressible: false, - native_shared_libs: [ - "native_shared_lib_1", - ], - binaries: [ - "cc_binary_1", - "sh_binary_2", - ], - prebuilts: [ - "prebuilt_1", - ], -} - -apex_key { - name: "com.google.android.apogee.key", - public_key: "com.google.android.apogee.avbpubkey", - private_key: "com.google.android.apogee.pem", -} - -android_app_certificate { - name: "com.google.android.apogee.certificate", - certificate: "com.google.android.apogee", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - key: "com.google.android.apogee.key", - certificate: ":com.google.android.apogee.certificate", - prebuilts: [], - compressible: true, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "android_manifest": `"ApogeeAndroidManifest.xml"`, - "base_apex_name": `"com.android.apogee"`, - "binaries": `[ - ":cc_binary_1", - ":sh_binary_2", - ]`, - "certificate": `":com.google.android.apogee.certificate"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "installable": "False", - "key": `":com.google.android.apogee.key"`, - "manifest": `"apogee_manifest.json"`, - "min_sdk_version": `"29"`, - "native_shared_libs_32": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [":native_shared_lib_1"], - "//build/bazel_common_rules/platforms/arch:x86": [":native_shared_lib_1"], - "//conditions:default": [], - })`, - "native_shared_libs_64": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":native_shared_lib_1"], - "//build/bazel_common_rules/platforms/arch:x86_64": [":native_shared_lib_1"], - "//conditions:default": [], - })`, - "testonly": "True", - "prebuilts": `[]`, - "updatable": "False", - "compressible": "True", - }), - }}) -} - -func TestApexBundleSimple_manifestIsEmpty_baseApexOverrideApexInDifferentAndroidBp(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - manifest of base apex is empty, base apex and override_apex is in different Android.bp", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"//a/b:com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - "a/b/Android.bp": ` -apex { - name: "com.android.apogee", -} -`, - }, - Blueprint: ` -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"//a/b:apex_manifest.json"`, - }), - }}) -} - -func TestApexBundleSimple_manifestIsSet_baseApexOverrideApexInDifferentAndroidBp(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - manifest of base apex is set, base apex and override_apex is in different Android.bp", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"//a/b:com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - "a/b/Android.bp": ` -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", -} -`, - }, - Blueprint: ` -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"//a/b:apogee_manifest.json"`, - }), - }}) -} - -func TestApexBundleSimple_manifestIsEmpty_baseApexOverrideApexInSameAndroidBp(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - manifest of base apex is empty, base apex and override_apex is in same Android.bp", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} - -func TestApexBundleSimple_manifestIsSet_baseApexOverrideApexInSameAndroidBp(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - manifest of base apex is set, base apex and override_apex is in same Android.bp", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apogee_manifest.json"`, - }), - }}) -} - -func TestApexBundleSimple_packageNameOverride(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - override package name", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - package_name: "com.google.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "package_name": `"com.google.android.apogee"`, - }), - }}) -} - -func TestApexBundleSimple_NoPrebuiltsOverride(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - no override", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"prebuilt_file", "com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -prebuilt_etc { - name: "prebuilt_file", -} - -apex { - name: "com.android.apogee", - prebuilts: ["prebuilt_file"] -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "prebuilts": `[":prebuilt_file"]`, - }), - }}) -} - -func TestApexBundleSimple_PrebuiltsOverride(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - ooverride", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"prebuilt_file", "prebuilt_file2", "com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -prebuilt_etc { - name: "prebuilt_file", -} - -prebuilt_etc { - name: "prebuilt_file2", -} - -apex { - name: "com.android.apogee", - prebuilts: ["prebuilt_file"] -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - prebuilts: ["prebuilt_file2"] -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "prebuilts": `[":prebuilt_file2"]`, - }), - }}) -} - -func TestApexBundleSimple_PrebuiltsOverrideEmptyList(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - override with empty list", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"prebuilt_file", "com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -prebuilt_etc { - name: "prebuilt_file", -} - -apex { - name: "com.android.apogee", - prebuilts: ["prebuilt_file"] -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - prebuilts: [], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "prebuilts": `[]`, - }), - }}) -} - -func TestApexBundleSimple_NoLoggingParentOverride(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - logging_parent - no override", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", - logging_parent: "foo.bar.baz", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "logging_parent": `"foo.bar.baz"`, - }), - }}) -} - -func TestApexBundleSimple_LoggingParentOverride(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - logging_parent - override", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"com.android.apogee", "//system/sepolicy/apex:com.android.apogee-file_contexts"}, - Filesystem: map[string]string{ - "system/sepolicy/apex/Android.bp": ` -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ "apogee-file_contexts", ], -}`, - }, - Blueprint: ` -apex { - name: "com.android.apogee", - logging_parent: "foo.bar.baz", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - logging_parent: "foo.bar.baz.override", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `"//system/sepolicy/apex:com.android.apogee-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "logging_parent": `"foo.bar.baz.override"`, - }), - }}) -} - -func TestBp2BuildOverrideApex_CertificateNil(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - don't set default certificate", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee.certificate", "com.android.apogee-file_contexts", "com.android.apogee"}, - Blueprint: ` -android_app_certificate { - name: "com.android.apogee.certificate", - certificate: "com.android.apogee", -} - -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} - -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - file_contexts: ":com.android.apogee-file_contexts", - certificate: ":com.android.apogee.certificate", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - // certificate is deliberately omitted, and not converted to bazel, - // because the overridden apex shouldn't be using the base apex's cert. -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "manifest": `"apogee_manifest.json"`, - }), - }}) -} - -func TestApexCertificateIsModule(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - certificate is module", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee-file_contexts", "com.android.apogee.certificate"}, - Blueprint: ` -android_app_certificate { - name: "com.android.apogee.certificate", - certificate: "com.android.apogee", -} - -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - file_contexts: ":com.android.apogee-file_contexts", - certificate: ":com.android.apogee.certificate", -} -` + simpleModule("filegroup", "com.android.apogee-file_contexts"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "certificate": `":com.android.apogee.certificate"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "manifest": `"apogee_manifest.json"`, - }), - }}) -} - -func TestApexWithStubLib(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - static variant of stub lib should not have apex_available tag", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"myapex-file_contexts"}, - Blueprint: ` -cc_library{ - name: "foo", - stubs: { symbol_file: "foo.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["myapex"], -} - -cc_binary{ - name: "bar", - static_libs: ["foo"], - apex_available: ["myapex"], -} - -apex { - name: "myapex", - manifest: "myapex_manifest.json", - file_contexts: ":myapex-file_contexts", - binaries: ["bar"], - native_shared_libs: ["foo"], -} -` + simpleModule("filegroup", "myapex-file_contexts"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_binary", "bar", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":foo_bp2build_cc_library_static"]`, - "tags": `["apex_available=myapex"]`, - }), - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "local_includes": `["."]`, - "stubs_symbol_file": `"foo.map.txt"`, - "tags": `["apex_available=myapex"]`, - }), - MakeBazelTarget("cc_stub_suite", "foo_stub_libs", AttrNameToString{ - "api_surface": `"module-libapi"`, - "soname": `"foo.so"`, - "source_library_label": `"//:foo"`, - "symbol_file": `"foo.map.txt"`, - "versions": `[ - "28", - "29", - "current", - ]`, - }), - MakeBazelTarget("apex", "myapex", AttrNameToString{ - "file_contexts": `":myapex-file_contexts"`, - "manifest": `"myapex_manifest.json"`, - "binaries": `[":bar"]`, - "native_shared_libs_32": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [":foo"], - "//build/bazel_common_rules/platforms/arch:x86": [":foo"], - "//conditions:default": [], - })`, - "native_shared_libs_64": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":foo"], - "//build/bazel_common_rules/platforms/arch:x86_64": [":foo"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestApexCertificateIsSrc(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - certificate is src", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee-file_contexts"}, - Blueprint: ` -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - file_contexts: ":com.android.apogee-file_contexts", - certificate: "com.android.apogee.certificate", -} -` + simpleModule("filegroup", "com.android.apogee-file_contexts"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "certificate_name": `"com.android.apogee.certificate"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "manifest": `"apogee_manifest.json"`, - }), - }}) -} - -func TestBp2BuildOverrideApex_CertificateIsModule(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - certificate is module", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee.certificate", "com.android.apogee-file_contexts", - "com.android.apogee", "com.google.android.apogee.certificate"}, - Blueprint: ` -android_app_certificate { - name: "com.android.apogee.certificate", - certificate: "com.android.apogee", -} - -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} - -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - file_contexts: ":com.android.apogee-file_contexts", - certificate: ":com.android.apogee.certificate", -} - -android_app_certificate { - name: "com.google.android.apogee.certificate", - certificate: "com.google.android.apogee", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - certificate: ":com.google.android.apogee.certificate", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "certificate": `":com.google.android.apogee.certificate"`, - "manifest": `"apogee_manifest.json"`, - }), - }}) -} - -func TestBp2BuildOverrideApex_CertificateIsSrc(t *testing.T) { - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "override_apex - certificate is src", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee", "com.android.apogee.certificate", "com.android.apogee", "com.android.apogee-file_contexts"}, - Blueprint: ` -android_app_certificate { - name: "com.android.apogee.certificate", - certificate: "com.android.apogee", -} - -filegroup { - name: "com.android.apogee-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} - -apex { - name: "com.android.apogee", - manifest: "apogee_manifest.json", - file_contexts: ":com.android.apogee-file_contexts", - certificate: ":com.android.apogee.certificate", -} - -override_apex { - name: "com.google.android.apogee", - base: ":com.android.apogee", - certificate: "com.google.android.apogee.certificate", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.google.android.apogee", AttrNameToString{ - "base_apex_name": `"com.android.apogee"`, - "file_contexts": `":com.android.apogee-file_contexts"`, - "certificate_name": `"com.google.android.apogee.certificate"`, - "manifest": `"apogee_manifest.json"`, - }), - }}) -} - -func TestApexTestBundleSimple(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex_test - simple", - ModuleTypeUnderTest: "apex_test", - ModuleTypeUnderTestFactory: apex.TestApexBundleFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"cc_test_1"}, - Blueprint: ` -cc_test { name: "cc_test_1"} - -apex_test { - name: "test_com.android.apogee", - file_contexts: "file_contexts_file", - tests: ["cc_test_1"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "test_com.android.apogee", AttrNameToString{ - "file_contexts": `"file_contexts_file"`, - "base_apex_name": `"com.android.apogee"`, - "manifest": `"apex_manifest.json"`, - "testonly": `True`, - "tests": `[":cc_test_1"]`, - }), - }}) -} - -func TestApexBundle_overridePlusProductVars(t *testing.T) { - // Reproduction of b/271424349 - // Tests that overriding an apex that uses product variables correctly copies the product var - // selects over to the override. - runOverrideApexTestCase(t, Bp2buildTestCase{ - Description: "apex - overriding a module that uses product vars", - ModuleTypeUnderTest: "override_apex", - ModuleTypeUnderTestFactory: apex.OverrideApexFactory, - StubbedBuildDefinitions: []string{"foo-file_contexts"}, - Blueprint: ` -soong_config_string_variable { - name: "library_linking_strategy", - values: [ - "prefer_static", - ], -} - -soong_config_module_type { - name: "library_linking_strategy_apex_defaults", - module_type: "apex_defaults", - config_namespace: "ANDROID", - variables: ["library_linking_strategy"], - properties: [ - "manifest", - "min_sdk_version", - ], -} - -library_linking_strategy_apex_defaults { - name: "higher_min_sdk_when_prefer_static", - soong_config_variables: { - library_linking_strategy: { - // Use the R min_sdk_version - prefer_static: {}, - // Override the R min_sdk_version to min_sdk_version that supports dcla - conditions_default: { - min_sdk_version: "31", - }, - }, - }, -} - -filegroup { - name: "foo-file_contexts", - srcs: [ - "com.android.apogee-file_contexts", - ], -} - -apex { - name: "foo", - defaults: ["higher_min_sdk_when_prefer_static"], - min_sdk_version: "30", - package_name: "pkg_name", - file_contexts: ":foo-file_contexts", -} -override_apex { - name: "override_foo", - base: ":foo", - package_name: "override_pkg_name", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "foo", AttrNameToString{ - "file_contexts": `":foo-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "min_sdk_version": `select({ - "//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": "30", - "//conditions:default": "31", - })`, - "package_name": `"pkg_name"`, - }), MakeBazelTarget("apex", "override_foo", AttrNameToString{ - "base_apex_name": `"foo"`, - "file_contexts": `":foo-file_contexts"`, - "manifest": `"apex_manifest.json"`, - "min_sdk_version": `select({ - "//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": "30", - "//conditions:default": "31", - })`, - "package_name": `"override_pkg_name"`, - }), - }}) -} - -func TestApexBundleSimple_customCannedFsConfig(t *testing.T) { - runApexTestCase(t, Bp2buildTestCase{ - Description: "apex - custom canned_fs_config", - ModuleTypeUnderTest: "apex", - ModuleTypeUnderTestFactory: apex.BundleFactory, - Filesystem: map[string]string{}, - Blueprint: ` -apex { - name: "com.android.apogee", - canned_fs_config: "custom.canned_fs_config", - file_contexts: "file_contexts_file", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("apex", "com.android.apogee", AttrNameToString{ - "canned_fs_config": `"custom.canned_fs_config"`, - "file_contexts": `"file_contexts_file"`, - "manifest": `"apex_manifest.json"`, - }), - }}) -} diff --git a/bp2build/apex_key_conversion_test.go b/bp2build/apex_key_conversion_test.go deleted file mode 100644 index 05e2f25bd..000000000 --- a/bp2build/apex_key_conversion_test.go +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "android/soong/android" - "android/soong/apex" - - "testing" -) - -func runApexKeyTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - RunBp2BuildTestCase(t, registerApexKeyModuleTypes, tc) -} - -func registerApexKeyModuleTypes(ctx android.RegistrationContext) { - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) -} - -func TestApexKeySimple_KeysAreSrcFilesInSameDir(t *testing.T) { - runApexKeyTestCase(t, Bp2buildTestCase{ - Description: "apex key - keys are src files, use key attributes", - ModuleTypeUnderTest: "apex_key", - ModuleTypeUnderTestFactory: apex.ApexKeyFactory, - Filesystem: map[string]string{ - "com.android.apogee.avbpubkey": "", - "com.android.apogee.pem": "", - }, - Blueprint: ` -apex_key { - name: "com.android.apogee.key", - public_key: "com.android.apogee.avbpubkey", - private_key: "com.android.apogee.pem", -} -`, - ExpectedBazelTargets: []string{MakeBazelTargetNoRestrictions("apex_key", "com.android.apogee.key", AttrNameToString{ - "private_key": `"com.android.apogee.pem"`, - "public_key": `"com.android.apogee.avbpubkey"`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }), - }}) -} - -func TestApexKeySimple_KeysAreSrcFilesNotInDir(t *testing.T) { - runApexKeyTestCase(t, Bp2buildTestCase{ - Description: "apex key - keys are not src or module, use key_name attributes", - ModuleTypeUnderTest: "apex_key", - ModuleTypeUnderTestFactory: apex.ApexKeyFactory, - Filesystem: map[string]string{ - // deliberately left empty - }, - Blueprint: ` -apex_key { - name: "com.android.apogee.key", - public_key: "com.android.apogee.avbpubkey", - private_key: "com.android.apogee.pem", -} -`, - ExpectedBazelTargets: []string{MakeBazelTargetNoRestrictions("apex_key", "com.android.apogee.key", AttrNameToString{ - "private_key_name": `"com.android.apogee.pem"`, - "public_key_name": `"com.android.apogee.avbpubkey"`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }), - }}) -} - -func TestApexKey_KeysAreModules(t *testing.T) { - runApexKeyTestCase(t, Bp2buildTestCase{ - Description: "apex key - keys are modules, use key attributes", - ModuleTypeUnderTest: "apex_key", - ModuleTypeUnderTestFactory: apex.ApexKeyFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"com.android.apogee.avbpubkey", "com.android.apogee.pem"}, - Blueprint: ` -apex_key { - name: "com.android.apogee.key", - public_key: ":com.android.apogee.avbpubkey", - private_key: ":com.android.apogee.pem", -} -` + simpleModule("filegroup", "com.android.apogee.avbpubkey") + - simpleModule("filegroup", "com.android.apogee.pem"), - ExpectedBazelTargets: []string{MakeBazelTargetNoRestrictions("apex_key", "com.android.apogee.key", AttrNameToString{ - "private_key": `":com.android.apogee.pem"`, - "public_key": `":com.android.apogee.avbpubkey"`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }), - }}) -} diff --git a/bp2build/bp2build_product_config_test.go b/bp2build/bp2build_product_config_test.go deleted file mode 100644 index 02a2b510e..000000000 --- a/bp2build/bp2build_product_config_test.go +++ /dev/null @@ -1,154 +0,0 @@ -package bp2build - -import ( - "android/soong/android" - "android/soong/cc" - "android/soong/starlark_import" - "encoding/json" - "reflect" - "testing" - - "github.com/google/blueprint/proptools" - "go.starlark.net/starlark" -) - -func createStarlarkValue(t *testing.T, code string) starlark.Value { - t.Helper() - result, err := starlark.ExecFile(&starlark.Thread{}, "main.bzl", "x = "+code, nil) - if err != nil { - t.Error(err) - } - return result["x"] -} - -func createStarlarkProductVariablesMap(t *testing.T, code string) map[string]starlark.Value { - t.Helper() - rawValue := createStarlarkValue(t, code) - value, err := starlark_import.Unmarshal[map[string]starlark.Value](rawValue) - if err != nil { - t.Error(err) - } - return value -} - -func TestStarlarkMapToProductVariables(t *testing.T) { - thirty := 30 - cases := []struct { - starlark string - result android.ProductVariables - }{ - { - starlark: `{"CompressedApex": True}`, - result: android.ProductVariables{CompressedApex: proptools.BoolPtr(true)}, - }, - { - starlark: `{"ApexGlobalMinSdkVersionOverride": "Tiramisu"}`, - result: android.ProductVariables{ApexGlobalMinSdkVersionOverride: proptools.StringPtr("Tiramisu")}, - }, - { - starlark: `{"ProductManufacturer": "Google"}`, - result: android.ProductVariables{ProductManufacturer: "Google"}, - }, - { - starlark: `{"Unbundled_build_apps": ["app1", "app2"]}`, - result: android.ProductVariables{Unbundled_build_apps: []string{"app1", "app2"}}, - }, - { - starlark: `{"Platform_sdk_version": 30}`, - result: android.ProductVariables{Platform_sdk_version: &thirty}, - }, - { - starlark: `{"HostFakeSnapshotEnabled": True}`, - result: android.ProductVariables{HostFakeSnapshotEnabled: true}, - }, - } - - for _, testCase := range cases { - productVariables, err := starlarkMapToProductVariables(createStarlarkProductVariablesMap(t, - testCase.starlark)) - if err != nil { - t.Error(err) - continue - } - testCase.result.Native_coverage = proptools.BoolPtr(false) - if !reflect.DeepEqual(testCase.result, productVariables) { - expected, err := json.Marshal(testCase.result) - if err != nil { - t.Error(err) - continue - } - actual, err := json.Marshal(productVariables) - if err != nil { - t.Error(err) - continue - } - expectedStr := string(expected) - actualStr := string(actual) - t.Errorf("expected %q, but got %q", expectedStr, actualStr) - } - } -} - -func TestSystemPartitionDeps(t *testing.T) { - RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - }, Bp2buildTestCase{ - ExtraFixturePreparer: android.GroupFixturePreparers( - android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { - deviceProduct := "aosp_arm64" - variables.DeviceProduct = &deviceProduct - partitionVars := &variables.PartitionVarsForBazelMigrationOnlyDoNotUse - partitionVars.ProductDirectory = "build/make/target/product/" - partitionVars.ProductPackages = []string{"foo"} - var systemVars android.PartitionQualifiedVariablesType - systemVars.BuildingImage = true - partitionVars.PartitionQualifiedVariables = map[string]android.PartitionQualifiedVariablesType{ - "system": systemVars, - } - }), - android.FixtureModifyConfig(func(config android.Config) { - // MockBazelContext will pretend everything is mixed-builds allowlisted. - // The default is noopBazelContext, which does the opposite. - config.BazelContext = android.MockBazelContext{} - }), - ), - Blueprint: ` -cc_library { - name: "foo", -}`, - ExpectedBazelTargets: []string{`android_product( - name = "aosp_arm64", - soong_variables = _soong_variables, -)`, `partition( - name = "system_image", - base_staging_dir = "//build/bazel/bazel_sandwich:system_staging_dir", - base_staging_dir_file_list = "//build/bazel/bazel_sandwich:system_staging_dir_file_list", - root_dir = "//build/bazel/bazel_sandwich:root_staging_dir", - selinux_file_contexts = "//build/bazel/bazel_sandwich:selinux_file_contexts", - image_properties = """ -building_system_image=true -erofs_sparse_flag=-s -extfs_sparse_flag=-s -f2fs_sparse_flag=-S -skip_fsck=true -squashfs_sparse_flag=-s -system_disable_sparse=true - -""", - deps = [ - "//:foo", - ], - - type = "system", -)`, `partition_diff_test( - name = "system_image_test", - partition1 = "//build/bazel/bazel_sandwich:make_system_image", - partition2 = ":system_image", -)`, `run_test_in_build( - name = "run_system_image_test", - test = ":system_image_test", -)`}, - Dir: "build/make/target/product/aosp_arm64", - RunBp2buildProductConfig: true, - }) -} diff --git a/bp2build/bpf_conversion_test.go b/bp2build/bpf_conversion_test.go deleted file mode 100644 index 26b0bf434..000000000 --- a/bp2build/bpf_conversion_test.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "android/soong/android" - "android/soong/bpf" - - "testing" -) - -func runBpfTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - (&tc).ModuleTypeUnderTest = "bpf" - (&tc).ModuleTypeUnderTestFactory = bpf.BpfFactory - RunBp2BuildTestCase(t, registerBpfModuleTypes, tc) -} - -func registerBpfModuleTypes(ctx android.RegistrationContext) {} - -func TestBpfSupportedAttrs(t *testing.T) { - runBpfTestCase(t, Bp2buildTestCase{ - Description: "Bpf module only converts supported attributes", - Filesystem: map[string]string{}, - Blueprint: ` -bpf { - name: "bpfTestOut.o", - srcs: ["bpfTestSrcOne.c", - "bpfTestSrcTwo.c"], - btf: true, - cflags: ["-bpfCflagOne", - "-bpfCflagTwo"], - include_dirs: ["ia/ib/ic"], - sub_dir: "sa/ab", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("bpf", "bpfTestOut.o", AttrNameToString{ - "absolute_includes": `["ia/ib/ic"]`, - "btf": `True`, - "copts": `[ - "-bpfCflagOne", - "-bpfCflagTwo", - ]`, - "srcs": `[ - "bpfTestSrcOne.c", - "bpfTestSrcTwo.c", - ]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }), - }, - }) -} diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go deleted file mode 100644 index f809bcc07..000000000 --- a/bp2build/build_conversion_test.go +++ /dev/null @@ -1,2256 +0,0 @@ -// Copyright 2020 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "fmt" - "strings" - "testing" - - "android/soong/android" - "android/soong/android/allowlists" - "android/soong/bazel" - "android/soong/python" -) - -func TestGenerateSoongModuleTargets(t *testing.T) { - testCases := []struct { - description string - bp string - expectedBazelTarget string - }{ - { - description: "only name", - bp: `custom { name: "foo" } - `, - expectedBazelTarget: `soong_module( - name = "foo", - soong_module_name = "foo", - soong_module_type = "custom", - soong_module_variant = "", - soong_module_deps = [ - ], - bool_prop = False, - string_prop = "", -)`, - }, - { - description: "handles bool", - bp: `custom { - name: "foo", - bool_prop: true, -} - `, - expectedBazelTarget: `soong_module( - name = "foo", - soong_module_name = "foo", - soong_module_type = "custom", - soong_module_variant = "", - soong_module_deps = [ - ], - bool_prop = True, - string_prop = "", -)`, - }, - { - description: "string escaping", - bp: `custom { - name: "foo", - owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", -} - `, - expectedBazelTarget: `soong_module( - name = "foo", - soong_module_name = "foo", - soong_module_type = "custom", - soong_module_variant = "", - soong_module_deps = [ - ], - bool_prop = False, - owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", - string_prop = "", -)`, - }, - { - description: "single item string list", - bp: `custom { - name: "foo", - required: ["bar"], -} - `, - expectedBazelTarget: `soong_module( - name = "foo", - soong_module_name = "foo", - soong_module_type = "custom", - soong_module_variant = "", - soong_module_deps = [ - ], - bool_prop = False, - required = ["bar"], - string_prop = "", -)`, - }, - { - description: "list of strings", - bp: `custom { - name: "foo", - target_required: ["qux", "bazqux"], -} - `, - expectedBazelTarget: `soong_module( - name = "foo", - soong_module_name = "foo", - soong_module_type = "custom", - soong_module_variant = "", - soong_module_deps = [ - ], - bool_prop = False, - string_prop = "", - target_required = [ - "qux", - "bazqux", - ], -)`, - }, - { - description: "dist/dists", - bp: `custom { - name: "foo", - dist: { - targets: ["goal_foo"], - tag: ".foo", - }, - dists: [{ - targets: ["goal_bar"], - tag: ".bar", - }], -} - `, - expectedBazelTarget: `soong_module( - name = "foo", - soong_module_name = "foo", - soong_module_type = "custom", - soong_module_variant = "", - soong_module_deps = [ - ], - bool_prop = False, - dist = { - "tag": ".foo", - "targets": ["goal_foo"], - }, - dists = [{ - "tag": ".bar", - "targets": ["goal_bar"], - }], - string_prop = "", -)`, - }, - { - description: "put it together", - bp: `custom { - name: "foo", - required: ["bar"], - target_required: ["qux", "bazqux"], - bool_prop: true, - owner: "custom_owner", - dists: [ - { - tag: ".tag", - targets: ["my_goal"], - }, - ], -} - `, - expectedBazelTarget: `soong_module( - name = "foo", - soong_module_name = "foo", - soong_module_type = "custom", - soong_module_variant = "", - soong_module_deps = [ - ], - bool_prop = True, - dists = [{ - "tag": ".tag", - "targets": ["my_goal"], - }], - owner = "custom_owner", - required = ["bar"], - string_prop = "", - target_required = [ - "qux", - "bazqux", - ], -)`, - }, - } - - dir := "." - for _, testCase := range testCases { - t.Run(testCase.description, func(t *testing.T) { - config := android.TestConfig(buildDir, nil, testCase.bp, nil) - ctx := android.NewTestContext(config) - - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - ctx.Register() - - _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) - - codegenCtx := NewCodegenContext(config, ctx.Context, QueryView, "") - bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) - android.FailIfErrored(t, err) - if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { - t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) - } - - actualBazelTarget := bazelTargets[0] - if actualBazelTarget.content != testCase.expectedBazelTarget { - t.Errorf( - "Expected generated Bazel target to be '%s', got '%s'", - testCase.expectedBazelTarget, - actualBazelTarget.content, - ) - } - }) - } -} - -func TestGenerateBazelTargetModules(t *testing.T) { - testCases := []Bp2buildTestCase{ - { - Description: "string prop empty", - Blueprint: `custom { - name: "foo", - string_literal_prop: "", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "foo", AttrNameToString{ - "string_literal_prop": `""`, - }), - }, - }, - { - Description: `string prop "PROP"`, - Blueprint: `custom { - name: "foo", - string_literal_prop: "PROP", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "foo", AttrNameToString{ - "string_literal_prop": `"PROP"`, - }), - }, - }, - { - Description: `string prop arch variant`, - Blueprint: `custom { - name: "foo", - arch: { - arm: { string_literal_prop: "ARM" }, - arm64: { string_literal_prop: "ARM64" }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "foo", AttrNameToString{ - "string_literal_prop": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "ARM", - "//build/bazel_common_rules/platforms/arch:arm64": "ARM64", - "//conditions:default": None, - })`, - }), - }, - }, - { - Description: "string ptr props", - Blueprint: `custom { - name: "foo", - string_ptr_prop: "", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "foo", AttrNameToString{ - "string_ptr_prop": `""`, - }), - }, - }, - { - Description: "string list props", - Blueprint: `custom { - name: "foo", - string_list_prop: ["a", "b"], - string_ptr_prop: "a", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "foo", AttrNameToString{ - "string_list_prop": `[ - "a", - "b", - ]`, - "string_ptr_prop": `"a"`, - }), - }, - }, - { - Description: "control characters", - Blueprint: `custom { - name: "foo", - string_list_prop: ["\t", "\n"], - string_ptr_prop: "a\t\n\r", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "foo", AttrNameToString{ - "string_list_prop": `[ - "\t", - "\n", - ]`, - "string_ptr_prop": `"a\t\n\r"`, - }), - }, - }, - { - Description: "handles dep", - Blueprint: `custom { - name: "has_dep", - arch_paths: [":dep"], - bazel_module: { bp2build_available: true }, -} - -custom { - name: "dep", - arch_paths: ["abc"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "dep", AttrNameToString{ - "arch_paths": `["abc"]`, - }), - MakeBazelTarget("custom", "has_dep", AttrNameToString{ - "arch_paths": `[":dep"]`, - }), - }, - }, - { - Description: "arch-variant srcs", - Blueprint: `custom { - name: "arch_paths", - arch: { - x86: { arch_paths: ["x86.txt"] }, - x86_64: { arch_paths: ["x86_64.txt"] }, - arm: { arch_paths: ["arm.txt"] }, - arm64: { arch_paths: ["arm64.txt"] }, - riscv64: { arch_paths: ["riscv64.txt"] }, - }, - target: { - linux: { arch_paths: ["linux.txt"] }, - bionic: { arch_paths: ["bionic.txt"] }, - host: { arch_paths: ["host.txt"] }, - not_windows: { arch_paths: ["not_windows.txt"] }, - android: { arch_paths: ["android.txt"] }, - linux_musl: { arch_paths: ["linux_musl.txt"] }, - musl: { arch_paths: ["musl.txt"] }, - linux_glibc: { arch_paths: ["linux_glibc.txt"] }, - glibc: { arch_paths: ["glibc.txt"] }, - linux_bionic: { arch_paths: ["linux_bionic.txt"] }, - darwin: { arch_paths: ["darwin.txt"] }, - windows: { arch_paths: ["windows.txt"] }, - }, - multilib: { - lib32: { arch_paths: ["lib32.txt"] }, - lib64: { arch_paths: ["lib64.txt"] }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "arch_paths", AttrNameToString{ - "arch_paths": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "arm.txt", - "lib32.txt", - ], - "//build/bazel_common_rules/platforms/arch:arm64": [ - "arm64.txt", - "lib64.txt", - ], - "//build/bazel_common_rules/platforms/arch:riscv64": [ - "riscv64.txt", - "lib64.txt", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - "x86.txt", - "lib32.txt", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - "x86_64.txt", - "lib64.txt", - ], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [ - "linux.txt", - "bionic.txt", - "android.txt", - ], - "//build/bazel_common_rules/platforms/os:darwin": [ - "host.txt", - "darwin.txt", - "not_windows.txt", - ], - "//build/bazel_common_rules/platforms/os:linux_bionic": [ - "host.txt", - "linux.txt", - "bionic.txt", - "linux_bionic.txt", - "not_windows.txt", - ], - "//build/bazel_common_rules/platforms/os:linux_glibc": [ - "host.txt", - "linux.txt", - "glibc.txt", - "linux_glibc.txt", - "not_windows.txt", - ], - "//build/bazel_common_rules/platforms/os:linux_musl": [ - "host.txt", - "linux.txt", - "musl.txt", - "linux_musl.txt", - "not_windows.txt", - ], - "//build/bazel_common_rules/platforms/os:windows": [ - "host.txt", - "windows.txt", - ], - "//conditions:default": [], - })`, - }), - }, - }, - { - Description: "arch-variant deps", - Blueprint: `custom { - name: "has_dep", - arch: { - x86: { - arch_paths: [":dep"], - }, - }, - bazel_module: { bp2build_available: true }, -} - -custom { - name: "dep", - arch_paths: ["abc"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "dep", AttrNameToString{ - "arch_paths": `["abc"]`, - }), - MakeBazelTarget("custom", "has_dep", AttrNameToString{ - "arch_paths": `select({ - "//build/bazel_common_rules/platforms/arch:x86": [":dep"], - "//conditions:default": [], - })`, - }), - }, - }, - { - Description: "embedded props", - Blueprint: `custom { - name: "embedded_props", - embedded_prop: "abc", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "embedded_props", AttrNameToString{ - "embedded_attr": `"abc"`, - }), - }, - }, - { - Description: "ptr to embedded props", - Blueprint: `custom { - name: "ptr_to_embedded_props", - other_embedded_prop: "abc", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{ - "other_embedded_attr": `"abc"`, - }), - }, - }, - } - - dir := "." - for _, testCase := range testCases { - t.Run(testCase.Description, func(t *testing.T) { - config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil) - ctx := android.NewTestContext(config) - - registerCustomModuleForBp2buildConversion(ctx) - - _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) - if errored(t, testCase, errs) { - return - } - _, errs = ctx.ResolveDependencies(config) - if errored(t, testCase, errs) { - return - } - - codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") - bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) - android.FailIfErrored(t, err) - - if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount { - t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets) - } else { - for i, expectedBazelTarget := range testCase.ExpectedBazelTargets { - actualBazelTarget := bazelTargets[i] - if actualBazelTarget.content != expectedBazelTarget { - t.Errorf( - "Expected generated Bazel target to be '%s', got '%s'", - expectedBazelTarget, - actualBazelTarget.content, - ) - } - } - } - }) - } -} - -func TestBp2buildHostAndDevice(t *testing.T) { - testCases := []Bp2buildTestCase{ - { - Description: "host and device, device only", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), - }, - }, - { - Description: "host and device, both", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - host_supported: true, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), - }, - }, - { - Description: "host and device, host explicitly disabled", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - host_supported: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), - }, - }, - { - Description: "host and device, neither", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - host_supported: false, - device_supported: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ - "target_compatible_with": `["@platforms//:incompatible"]`, - }), - }, - }, - { - Description: "host and device, neither, cannot override with product_var", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - host_supported: false, - device_supported: false, - product_variables: { unbundled_build: { enabled: true } }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ - "target_compatible_with": `["@platforms//:incompatible"]`, - }), - }, - }, - { - Description: "host and device, both, disabled overrided with product_var", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - host_supported: true, - device_supported: true, - enabled: false, - product_variables: { unbundled_build: { enabled: true } }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ - "target_compatible_with": `select({ - "//build/bazel/product_config/config_settings:unbundled_build": [], - "//conditions:default": ["@platforms//:incompatible"], - })`, - }), - }, - }, - { - Description: "host and device, neither, cannot override with arch enabled", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - host_supported: false, - device_supported: false, - arch: { x86: { enabled: true } }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ - "target_compatible_with": `["@platforms//:incompatible"]`, - }), - }, - }, - { - Description: "host and device, host only", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - Blueprint: `custom { - name: "foo", - host_supported: true, - device_supported: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), - }, - }, - { - Description: "host only", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostSupported, - Blueprint: `custom { - name: "foo", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), - }, - }, - { - Description: "device only", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported, - Blueprint: `custom { - name: "foo", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), - }, - }, - { - Description: "host and device default, default", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, - Blueprint: `custom { - name: "foo", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), - }, - }, - { - Description: "host and device default, device only", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, - Blueprint: `custom { - name: "foo", - host_supported: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), - }, - }, - { - Description: "host and device default, host only", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, - Blueprint: `custom { - name: "foo", - device_supported: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), - }, - }, - { - Description: "host and device default, neither", - ModuleTypeUnderTest: "custom", - ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, - Blueprint: `custom { - name: "foo", - host_supported: false, - device_supported: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ - "target_compatible_with": `["@platforms//:incompatible"]`, - }), - }, - }, - } - - for _, tc := range testCases { - t.Run(tc.Description, func(t *testing.T) { - RunBp2BuildTestCaseSimple(t, tc) - }) - } -} - -func TestLoadStatements(t *testing.T) { - testCases := []struct { - bazelTargets BazelTargets - expectedLoadStatements string - }{ - { - bazelTargets: BazelTargets{ - BazelTarget{ - name: "foo", - ruleClass: "cc_library", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, - }}, - }, - }, - expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, - }, - { - bazelTargets: BazelTargets{ - BazelTarget{ - name: "foo", - ruleClass: "cc_library", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, - }}, - }, - BazelTarget{ - name: "bar", - ruleClass: "cc_library", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, - }}, - }, - }, - expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, - }, - { - bazelTargets: BazelTargets{ - BazelTarget{ - name: "foo", - ruleClass: "cc_library", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, - }}, - }, - BazelTarget{ - name: "bar", - ruleClass: "cc_binary", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, - }}, - }, - }, - expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, - }, - { - bazelTargets: BazelTargets{ - BazelTarget{ - name: "foo", - ruleClass: "cc_library", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, - }}, - }, - BazelTarget{ - name: "bar", - ruleClass: "cc_binary", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, - }}, - }, - BazelTarget{ - name: "baz", - ruleClass: "java_binary", - loads: []BazelLoad{{ - file: "//build/bazel/rules:java.bzl", - symbols: []BazelLoadSymbol{{symbol: "java_binary"}}, - }}, - }, - }, - expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") -load("//build/bazel/rules:java.bzl", "java_binary")`, - }, - { - bazelTargets: BazelTargets{ - BazelTarget{ - name: "foo", - ruleClass: "cc_binary", - loads: []BazelLoad{{ - file: "//build/bazel/rules:cc.bzl", - symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, - }}, - }, - BazelTarget{ - name: "bar", - ruleClass: "java_binary", - loads: []BazelLoad{{ - file: "//build/bazel/rules:java.bzl", - symbols: []BazelLoadSymbol{{symbol: "java_binary"}}, - }}, - }, - BazelTarget{ - name: "baz", - ruleClass: "genrule", - // Note: no loads for native rules - }, - }, - expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") -load("//build/bazel/rules:java.bzl", "java_binary")`, - }, - } - - for _, testCase := range testCases { - actual := testCase.bazelTargets.LoadStatements() - expected := testCase.expectedLoadStatements - if actual != expected { - t.Fatalf("Expected load statements to be %s, got %s", expected, actual) - } - } - -} - -func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { - testCases := []struct { - bp string - expectedBazelTarget string - expectedBazelTargetCount int - expectedLoadStatements string - }{ - { - bp: `custom { - name: "bar", - host_supported: true, - one_to_many_prop: true, - bazel_module: { bp2build_available: true }, -}`, - expectedBazelTarget: `my_library( - name = "bar", -) - -proto_library( - name = "bar_proto_library_deps", -) - -my_proto_library( - name = "bar_my_proto_library_deps", -)`, - expectedBazelTargetCount: 3, - expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") -load("//build/bazel/rules:rules.bzl", "my_library")`, - }, - } - - dir := "." - for _, testCase := range testCases { - config := android.TestConfig(buildDir, nil, testCase.bp, nil) - ctx := android.NewTestContext(config) - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - ctx.RegisterForBazelConversion() - - _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.ResolveDependencies(config) - android.FailIfErrored(t, errs) - - codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") - bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) - android.FailIfErrored(t, err) - if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { - t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) - } - - actualBazelTargets := bazelTargets.String() - if actualBazelTargets != testCase.expectedBazelTarget { - t.Errorf( - "Expected generated Bazel target to be '%s', got '%s'", - testCase.expectedBazelTarget, - actualBazelTargets, - ) - } - - actualLoadStatements := bazelTargets.LoadStatements() - if actualLoadStatements != testCase.expectedLoadStatements { - t.Errorf( - "Expected generated load statements to be '%s', got '%s'", - testCase.expectedLoadStatements, - actualLoadStatements, - ) - } - } -} - -func TestModuleTypeBp2Build(t *testing.T) { - testCases := []Bp2buildTestCase{ - { - Description: "filegroup with does not specify srcs", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), - }, - }, - { - Description: "filegroup with no srcs", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: [], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), - }, - }, - { - Description: "filegroup with srcs", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["a", "b"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a", - "b", - ]`, - }), - }, - }, - { - Description: "filegroup with dot-slash-prefixed srcs", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["./a", "./b"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a", - "b", - ]`, - }), - }, - }, - { - Description: "filegroup with excludes srcs", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["a", "b"], - exclude_srcs: ["a"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `["b"]`, - }), - }, - }, - { - Description: "depends_on_other_dir_module", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: [ - ":foo", - "c", - ], - bazel_module: { bp2build_available: true }, -}`, - Filesystem: map[string]string{ - "other/Android.bp": `filegroup { - name: "foo", - srcs: ["a", "b"], - bazel_module: { bp2build_available: true }, -}`, - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "//other:foo", - "c", - ]`, - }), - }, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.Description, func(t *testing.T) { - RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase) - }) - } -} - -func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { - testCases := []struct { - moduleTypeUnderTest string - moduleTypeUnderTestFactory android.ModuleFactory - bp string - expectedCount int - description string - }{ - { - description: "explicitly unavailable", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - bp: `filegroup { - name: "foo", - srcs: ["a", "b"], - bazel_module: { bp2build_available: false }, -}`, - expectedCount: 0, - }, - { - description: "implicitly unavailable", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - bp: `filegroup { - name: "foo", - srcs: ["a", "b"], -}`, - expectedCount: 0, - }, - { - description: "explicitly available", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - bp: `filegroup { - name: "foo", - srcs: ["a", "b"], - bazel_module: { bp2build_available: true }, -}`, - expectedCount: 1, - }, - { - description: "generates more than 1 target if needed", - moduleTypeUnderTest: "custom", - moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice, - bp: `custom { - name: "foo", - one_to_many_prop: true, - bazel_module: { bp2build_available: true }, -}`, - expectedCount: 3, - }, - } - - dir := "." - for _, testCase := range testCases { - t.Run(testCase.description, func(t *testing.T) { - config := android.TestConfig(buildDir, nil, testCase.bp, nil) - ctx := android.NewTestContext(config) - ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) - ctx.RegisterForBazelConversion() - - _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.ResolveDependencies(config) - android.FailIfErrored(t, errs) - - codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") - bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) - android.FailIfErrored(t, err) - if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { - t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) - } - }) - } -} - -func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { - testCases := []struct { - moduleTypeUnderTest string - moduleTypeUnderTestFactory android.ModuleFactory - expectedCount map[string]int - description string - bp2buildConfig allowlists.Bp2BuildConfig - checkDir string - fs map[string]string - forceEnabledModules []string - expectedErrorMessages []string - }{ - { - description: "test bp2build config package and subpackages config", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - expectedCount: map[string]int{ - "migrated": 1, - "migrated/but_not_really": 0, - "migrated/but_not_really/but_really": 1, - "not_migrated": 0, - "also_not_migrated": 0, - }, - bp2buildConfig: allowlists.Bp2BuildConfig{ - "migrated": allowlists.Bp2BuildDefaultTrueRecursively, - "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, - "not_migrated": allowlists.Bp2BuildDefaultFalse, - }, - fs: map[string]string{ - "migrated/Android.bp": `filegroup { name: "a" }`, - "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, - "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, - "not_migrated/Android.bp": `filegroup { name: "d" }`, - "also_not_migrated/Android.bp": `filegroup { name: "e" }`, - }, - }, - { - description: "test bp2build config opt-in and opt-out", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - expectedCount: map[string]int{ - "package-opt-in": 2, - "package-opt-in/subpackage": 0, - "package-opt-out": 1, - "package-opt-out/subpackage": 0, - }, - bp2buildConfig: allowlists.Bp2BuildConfig{ - "package-opt-in": allowlists.Bp2BuildDefaultFalse, - "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively, - }, - fs: map[string]string{ - "package-opt-in/Android.bp": ` -filegroup { name: "opt-in-a" } -filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } -filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } -`, - - "package-opt-in/subpackage/Android.bp": ` -filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively -`, - - "package-opt-out/Android.bp": ` -filegroup { name: "opt-out-a" } -filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } -filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } -`, - - "package-opt-out/subpackage/Android.bp": ` -filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } -filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } -`, - }, - }, - { - description: "test force-enabled errors out", - moduleTypeUnderTest: "filegroup", - moduleTypeUnderTestFactory: android.FileGroupFactory, - expectedCount: map[string]int{ - "migrated": 0, - "not_migrated": 0, - }, - bp2buildConfig: allowlists.Bp2BuildConfig{ - "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, - "not_migrated": allowlists.Bp2BuildDefaultFalse, - }, - fs: map[string]string{ - "migrated/Android.bp": `filegroup { name: "a" }`, - }, - forceEnabledModules: []string{"a"}, - expectedErrorMessages: []string{"Force Enabled Module a not converted"}, - }, - } - - dir := "." - for _, testCase := range testCases { - fs := make(map[string][]byte) - toParse := []string{ - "Android.bp", - } - for f, content := range testCase.fs { - if strings.HasSuffix(f, "Android.bp") { - toParse = append(toParse, f) - } - fs[f] = []byte(content) - } - config := android.TestConfig(buildDir, nil, "", fs) - config.AddForceEnabledModules(testCase.forceEnabledModules) - ctx := android.NewTestContext(config) - ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) - allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig) - ctx.RegisterBp2BuildConfig(allowlist) - ctx.RegisterForBazelConversion() - - _, errs := ctx.ParseFileList(dir, toParse) - android.FailIfErrored(t, errs) - _, errs = ctx.ResolveDependencies(config) - android.FailIfErrored(t, errs) - - codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") - - // For each directory, test that the expected number of generated targets is correct. - for dir, expectedCount := range testCase.expectedCount { - bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) - android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages) - if actualCount := len(bazelTargets); actualCount != expectedCount { - t.Fatalf( - "%s: Expected %d bazel target for %s package, got %d", - testCase.description, - expectedCount, - dir, - actualCount) - } - - } - } -} - -func TestCombineBuildFilesBp2buildTargets(t *testing.T) { - testCases := []Bp2buildTestCase{ - { - Description: "filegroup bazel_module.label", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - bazel_module: { label: "//other:fg_foo" }, -}`, - ExpectedBazelTargets: []string{}, - Filesystem: map[string]string{ - "other/BUILD.bazel": `// BUILD file`, - }, - }, - { - Description: "multiple bazel_module.label same BUILD", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - bazel_module: { label: "//other:fg_foo" }, - } - - filegroup { - name: "foo", - bazel_module: { label: "//other:foo" }, - }`, - ExpectedBazelTargets: []string{}, - Filesystem: map[string]string{ - "other/BUILD.bazel": `// BUILD file`, - }, - }, - { - Description: "filegroup bazel_module.label and bp2build in subdir", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Dir: "other", - Blueprint: ``, - Filesystem: map[string]string{ - "other/Android.bp": `filegroup { - name: "fg_foo", - bazel_module: { - bp2build_available: true, - }, - } - filegroup { - name: "fg_bar", - bazel_module: { - label: "//other:fg_bar" - }, - }`, - "other/BUILD.bazel": `// definition for fg_bar`, - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), - }, - }, - { - Description: "filegroup bazel_module.label and filegroup bp2build", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - - Filesystem: map[string]string{ - "other/BUILD.bazel": `// BUILD file`, - }, - Blueprint: `filegroup { - name: "fg_foo", - bazel_module: { - label: "//other:fg_foo", - }, - } - - filegroup { - name: "fg_bar", - bazel_module: { - bp2build_available: true, - }, - }`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}), - }, - }, - } - - dir := "." - for _, testCase := range testCases { - t.Run(testCase.Description, func(t *testing.T) { - fs := make(map[string][]byte) - toParse := []string{ - "Android.bp", - } - for f, content := range testCase.Filesystem { - if strings.HasSuffix(f, "Android.bp") { - toParse = append(toParse, f) - } - fs[f] = []byte(content) - } - config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs) - ctx := android.NewTestContext(config) - ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory) - ctx.RegisterForBazelConversion() - - _, errs := ctx.ParseFileList(dir, toParse) - if errored(t, testCase, errs) { - return - } - _, errs = ctx.ResolveDependencies(config) - if errored(t, testCase, errs) { - return - } - - checkDir := dir - if testCase.Dir != "" { - checkDir = testCase.Dir - } - codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") - bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir) - android.FailIfErrored(t, err) - bazelTargets.sort() - actualCount := len(bazelTargets) - expectedCount := len(testCase.ExpectedBazelTargets) - if actualCount != expectedCount { - t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) - } - for i, target := range bazelTargets { - actualContent := target.content - expectedContent := testCase.ExpectedBazelTargets[i] - if expectedContent != actualContent { - t.Errorf( - "Expected generated Bazel target to be '%s', got '%s'", - expectedContent, - actualContent, - ) - } - } - }) - } -} - -func TestGlob(t *testing.T) { - testCases := []Bp2buildTestCase{ - { - Description: "filegroup with glob", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "other/a.txt", - "other/b.txt", - "other/subdir/a.txt", - ]`, - }), - }, - Filesystem: map[string]string{ - "other/a.txt": "", - "other/b.txt": "", - "other/subdir/a.txt": "", - "other/file": "", - }, - }, - { - Description: "filegroup with glob in subdir", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Dir: "other", - Filesystem: map[string]string{ - "other/Android.bp": `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - bazel_module: { bp2build_available: true }, -}`, - "other/a.txt": "", - "other/b.txt": "", - "other/subdir/a.txt": "", - "other/file": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "b.txt", - "subdir/a.txt", - ]`, - }), - }, - }, - { - Description: "filegroup with glob with no kept BUILD files", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - KeepBuildFileForDirs: []string{ - // empty - }, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - bazel_module: { bp2build_available: true }, -}`, - Filesystem: map[string]string{ - "a.txt": "", - "b.txt": "", - "foo/BUILD": "", - "foo/a.txt": "", - "foo/bar/BUILD": "", - "foo/bar/b.txt": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "b.txt", - "foo/a.txt", - "foo/bar/b.txt", - ]`, - }), - }, - }, - { - Description: "filegroup with glob with kept BUILD file", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - KeepBuildFileForDirs: []string{ - "foo", - }, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - bazel_module: { bp2build_available: true }, -}`, - Filesystem: map[string]string{ - "a.txt": "", - "b.txt": "", - "foo/BUILD": "", - "foo/a.txt": "", - "foo/bar/BUILD": "", - "foo/bar/b.txt": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "b.txt", - "//foo:a.txt", - "//foo:bar/b.txt", - ]`, - }), - }, - }, - { - Description: "filegroup with glob with kept BUILD.bazel file", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - KeepBuildFileForDirs: []string{ - "foo", - }, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - bazel_module: { bp2build_available: true }, -}`, - Filesystem: map[string]string{ - "a.txt": "", - "b.txt": "", - "foo/BUILD.bazel": "", - "foo/a.txt": "", - "foo/bar/BUILD.bazel": "", - "foo/bar/b.txt": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "b.txt", - "//foo:a.txt", - "//foo:bar/b.txt", - ]`, - }), - }, - }, - { - Description: "filegroup with glob with Android.bp file as boundary", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - bazel_module: { bp2build_available: true }, -}`, - Filesystem: map[string]string{ - "a.txt": "", - "b.txt": "", - "foo/Android.bp": "", - "foo/a.txt": "", - "foo/bar/Android.bp": "", - "foo/bar/b.txt": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "b.txt", - "//foo:a.txt", - "//foo/bar:b.txt", - ]`, - }), - }, - }, - { - Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Dir: "other", - KeepBuildFileForDirs: []string{ - "other/foo", - "other/foo/bar", - // deliberately not other/foo/baz/BUILD. - }, - Filesystem: map[string]string{ - "other/Android.bp": `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - bazel_module: { bp2build_available: true }, -}`, - "other/a.txt": "", - "other/b.txt": "", - "other/foo/BUILD": "", - "other/foo/a.txt": "", - "other/foo/bar/BUILD.bazel": "", - "other/foo/bar/b.txt": "", - "other/foo/baz/BUILD": "", - "other/foo/baz/c.txt": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "b.txt", - "//other/foo:a.txt", - "//other/foo/bar:b.txt", - "//other/foo:baz/c.txt", - ]`, - }), - }, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.Description, func(t *testing.T) { - RunBp2BuildTestCaseSimple(t, testCase) - }) - } -} - -func TestGlobExcludeSrcs(t *testing.T) { - testCases := []Bp2buildTestCase{ - { - Description: "filegroup top level exclude_srcs", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - exclude_srcs: ["c.txt"], - bazel_module: { bp2build_available: true }, -}`, - Filesystem: map[string]string{ - "a.txt": "", - "b.txt": "", - "c.txt": "", - "dir/Android.bp": "", - "dir/e.txt": "", - "dir/f.txt": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "b.txt", - "//dir:e.txt", - "//dir:f.txt", - ]`, - }), - }, - }, - { - Description: "filegroup in subdir exclude_srcs", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: "", - Dir: "dir", - Filesystem: map[string]string{ - "dir/Android.bp": `filegroup { - name: "fg_foo", - srcs: ["**/*.txt"], - exclude_srcs: ["b.txt"], - bazel_module: { bp2build_available: true }, -} -`, - "dir/a.txt": "", - "dir/b.txt": "", - "dir/subdir/Android.bp": "", - "dir/subdir/e.txt": "", - "dir/subdir/f.txt": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "srcs": `[ - "a.txt", - "//dir/subdir:e.txt", - "//dir/subdir:f.txt", - ]`, - }), - }, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.Description, func(t *testing.T) { - RunBp2BuildTestCaseSimple(t, testCase) - }) - } -} - -func TestCommonBp2BuildModuleAttrs(t *testing.T) { - testCases := []Bp2buildTestCase{ - { - Description: "Required into data test", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - StubbedBuildDefinitions: []string{"reqd"}, - Blueprint: simpleModule("filegroup", "reqd") + ` -filegroup { - name: "fg_foo", - required: ["reqd"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "data": `[":reqd"]`, - }), - }, - }, - { - Description: "Required into data test, cyclic self reference is filtered out", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - StubbedBuildDefinitions: []string{"reqd"}, - Blueprint: simpleModule("filegroup", "reqd") + ` -filegroup { - name: "fg_foo", - required: ["reqd", "fg_foo"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "data": `[":reqd"]`, - }), - }, - }, - { - Description: "Required via arch into data test", - ModuleTypeUnderTest: "python_library", - ModuleTypeUnderTestFactory: python.PythonLibraryFactory, - StubbedBuildDefinitions: []string{"reqdx86", "reqdarm"}, - Blueprint: simpleModule("python_library", "reqdx86") + - simpleModule("python_library", "reqdarm") + ` -python_library { - name: "fg_foo", - arch: { - arm: { - required: ["reqdarm"], - }, - x86: { - required: ["reqdx86"], - }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("py_library", "fg_foo", map[string]string{ - "data": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [":reqdarm"], - "//build/bazel_common_rules/platforms/arch:x86": [":reqdx86"], - "//conditions:default": [], - })`, - "srcs_version": `"PY3"`, - "imports": `["."]`, - }), - }, - }, - { - Description: "Required appended to data test", - ModuleTypeUnderTest: "python_library", - ModuleTypeUnderTestFactory: python.PythonLibraryFactory, - Filesystem: map[string]string{ - "data.bin": "", - "src.py": "", - }, - StubbedBuildDefinitions: []string{"reqd"}, - Blueprint: simpleModule("python_library", "reqd") + ` -python_library { - name: "fg_foo", - data: ["data.bin"], - required: ["reqd"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("py_library", "fg_foo", map[string]string{ - "data": `[ - "data.bin", - ":reqd", - ]`, - "srcs_version": `"PY3"`, - "imports": `["."]`, - }), - }, - }, - { - Description: "All props-to-attrs at once together test", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - StubbedBuildDefinitions: []string{"reqd"}, - Blueprint: simpleModule("filegroup", "reqd") + ` -filegroup { - name: "fg_foo", - required: ["reqd"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ - "data": `[":reqd"]`, - }), - }, - }, - } - - for _, tc := range testCases { - t.Run(tc.Description, func(t *testing.T) { - RunBp2BuildTestCaseSimple(t, tc) - }) - } -} - -func TestLicensesAttrConversion(t *testing.T) { - RunBp2BuildTestCase(t, - func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("license", android.LicenseFactory) - }, - Bp2buildTestCase{ - Description: "Test that licenses: attribute is converted", - ModuleTypeUnderTest: "filegroup", - ModuleTypeUnderTestFactory: android.FileGroupFactory, - Blueprint: ` -license { - name: "my_license", -} -filegroup { - name: "my_filegroup", - licenses: ["my_license"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{ - "applicable_licenses": `[":my_license"]`, - }), - MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}), - }, - }) -} - -func TestGenerateConfigSetting(t *testing.T) { - bp := ` - custom { - name: "foo", - test_config_setting: true, - } - ` - expectedBazelTargets := []string{ - MakeBazelTargetNoRestrictions( - "config_setting", - "foo_config_setting", - AttrNameToString{ - "flag_values": `{ - "//build/bazel/rules/my_string_setting": "foo", - }`, - }, - ), - MakeBazelTarget( - "custom", - "foo", - AttrNameToString{}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Generating API contribution Bazel targets for custom module", - }) -} - -// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value -func TestPrettyPrintSelectMapEqualValues(t *testing.T) { - lla := bazel.LabelListAttribute{ - Value: bazel.LabelList{}, - } - libFooImplLabel := bazel.Label{ - Label: ":libfoo.impl", - } - lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) - lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) - actual, _ := prettyPrintAttribute(lla, 0) - android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual) -} - -func TestAlreadyPresentBuildTarget(t *testing.T) { - bp := ` - custom { - name: "foo", - } - custom { - name: "bar", - } - ` - alreadyPresentBuildFile := - MakeBazelTarget( - "custom", - "foo", - AttrNameToString{}, - ) - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "bar", - AttrNameToString{}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - AlreadyExistingBuildContents: alreadyPresentBuildFile, - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Not duplicating work for an already-present BUILD target.", - }) -} - -func TestAlreadyPresentOneToManyBuildTarget(t *testing.T) { - bp := ` - custom { - name: "foo", - one_to_many_prop: true, - } - custom { - name: "bar", - } - ` - alreadyPresentBuildFile := - MakeBazelTarget( - "custom", - // one_to_many_prop ensures that foo generates "foo_proto_library_deps". - "foo_proto_library_deps", - AttrNameToString{}, - ) - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "bar", - AttrNameToString{}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - AlreadyExistingBuildContents: alreadyPresentBuildFile, - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Not duplicating work for an already-present BUILD target (different generated name)", - }) -} - -// Verifies that if a module is defined in pkg1/Android.bp, that a target present -// in pkg2/BUILD.bazel does not result in the module being labeled "already defined -// in a BUILD file". -func TestBuildTargetPresentOtherDirectory(t *testing.T) { - bp := ` - custom { - name: "foo", - } - ` - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "foo", - AttrNameToString{}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - KeepBuildFileForDirs: []string{"other_pkg"}, - Filesystem: map[string]string{ - "other_pkg/BUILD.bazel": MakeBazelTarget("custom", "foo", AttrNameToString{}), - }, - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Not treating a BUILD target as the bazel definition for a module in another package", - }) -} - -// If CommonAttributes.Dir is set, the bazel target should be created in that dir -func TestCreateBazelTargetInDifferentDir(t *testing.T) { - t.Parallel() - bp := ` - custom { - name: "foo", - dir: "subdir", - } - ` - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - // Check that foo is not created in root dir - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Description: "foo is not created in root dir because it sets dir explicitly", - Blueprint: bp, - Filesystem: map[string]string{ - "subdir/Android.bp": "", - }, - ExpectedBazelTargets: []string{}, - }) - // Check that foo is created in `subdir` - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Description: "foo is created in `subdir` because it sets dir explicitly", - Blueprint: bp, - Filesystem: map[string]string{ - "subdir/Android.bp": "", - }, - Dir: "subdir", - ExpectedBazelTargets: []string{ - MakeBazelTarget("custom", "foo", AttrNameToString{}), - }, - }) - // Check that we cannot create target in different dir if it is does not an Android.bp - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file", - Blueprint: bp, - Dir: "subdir", - ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"), - }) - -} - -func TestBp2buildDepsMutator_missingTransitiveDep(t *testing.T) { - bp := ` - custom { - name: "foo", - } - - custom { - name: "has_deps", - arch_paths: [":has_missing_dep", ":foo"], - } - - custom { - name: "has_missing_dep", - arch_paths: [":missing"], - } - ` - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "foo", - AttrNameToString{}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Skipping conversion of a target with missing transitive dep", - }) -} - -func TestBp2buildDepsMutator_missingDirectDep(t *testing.T) { - bp := ` - custom { - name: "foo", - arch_paths: [":exists"], - } - custom { - name: "exists", - } - - custom { - name: "has_missing_dep", - arch_paths: [":missing"], - } - ` - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "foo", - AttrNameToString{"arch_paths": `[":exists"]`}, - ), - MakeBazelTarget( - "custom", - "exists", - AttrNameToString{}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Skipping conversion of a target with missing direct dep", - }) -} - -func TestBp2buildDepsMutator_unconvertedDirectDep(t *testing.T) { - bp := ` - custom { - name: "has_unconverted_dep", - arch_paths: [":unconvertible"], - } - - custom { - name: "unconvertible", - does_not_convert_to_bazel: true - } - ` - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: []string{}, - Description: "Skipping conversion of a target with unconverted direct dep", - }) -} - -func TestBp2buildDepsMutator_unconvertedTransitiveDep(t *testing.T) { - bp := ` - custom { - name: "foo", - arch_paths: [":has_unconverted_dep", ":bar"], - } - - custom { - name: "bar", - } - - custom { - name: "has_unconverted_dep", - arch_paths: [":unconvertible"], - } - - custom { - name: "unconvertible", - does_not_convert_to_bazel: true - } - ` - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "bar", - AttrNameToString{}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Skipping conversion of a target with unconverted transitive dep", - }) -} - -func TestBp2buildDepsMutator_alreadyExistsBuildDeps(t *testing.T) { - bp := ` - custom { - name: "foo", - arch_paths: [":bar"], - } - custom { - name: "bar", - arch_paths: [":checked_in"], - } - custom { - name: "checked_in", - arch_paths: [":checked_in"], - does_not_convert_to_bazel: true - } - ` - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "foo", - AttrNameToString{"arch_paths": `[":bar"]`}, - ), - MakeBazelTarget( - "custom", - "bar", - AttrNameToString{"arch_paths": `[":checked_in"]`}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"checked_in"}, - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Convert target with already-existing build dep", - }) -} - -// Tests that deps of libc are always considered valid for libc. This circumvents -// an issue that, in a variantless graph (such as bp2build's), libc has the -// unique predicament that it depends on itself. -func TestBp2buildDepsMutator_depOnLibc(t *testing.T) { - bp := ` - custom { - name: "foo", - arch_paths: [":libc"], - } - custom { - name: "libc", - arch_paths: [":libc_dep"], - } - custom { - name: "libc_dep", - does_not_convert_to_bazel: true - } - ` - expectedBazelTargets := []string{ - MakeBazelTarget( - "custom", - "foo", - AttrNameToString{"arch_paths": `[":libc"]`}, - ), - MakeBazelTarget( - "custom", - "libc", - AttrNameToString{"arch_paths": `[":libc_dep"]`}, - ), - } - registerCustomModule := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) - } - RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"checked_in"}, - Blueprint: bp, - ExpectedBazelTargets: expectedBazelTargets, - Description: "Convert target with dep on libc", - }) -} diff --git a/bp2build/bzl_conversion_test.go b/bp2build/bzl_conversion_test.go deleted file mode 100644 index 645298f15..000000000 --- a/bp2build/bzl_conversion_test.go +++ /dev/null @@ -1,241 +0,0 @@ -// Copyright 2020 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "io/ioutil" - "os" - "strings" - "testing" - - "android/soong/android" -) - -func setUp() { - var err error - buildDir, err = ioutil.TempDir("", "bazel_queryview_test") - if err != nil { - panic(err) - } -} - -func tearDown() { - os.RemoveAll(buildDir) -} - -func TestMain(m *testing.M) { - run := func() int { - setUp() - defer tearDown() - - return m.Run() - } - - os.Exit(run()) -} - -func TestGenerateModuleRuleShims(t *testing.T) { - moduleTypeFactories := map[string]android.ModuleFactory{ - "custom": customModuleFactoryBase, - "custom_test": customTestModuleFactoryBase, - "custom_defaults": customDefaultsModuleFactoryBasic, - } - ruleShims := CreateRuleShims(moduleTypeFactories) - - if len(ruleShims) != 1 { - t.Errorf("Expected to generate 1 rule shim, but got %d", len(ruleShims)) - } - - ruleShim := ruleShims["bp2build"] - expectedRules := []string{ - "custom", - "custom_defaults", - "custom_test_", - } - - if len(ruleShim.rules) != len(expectedRules) { - t.Errorf("Expected %d rules, but got %d", len(expectedRules), len(ruleShim.rules)) - } - - for i, rule := range ruleShim.rules { - if rule != expectedRules[i] { - t.Errorf("Expected rule shim to contain %s, but got %s", expectedRules[i], rule) - } - } - expectedBzl := `load("//build/bazel/queryview_rules:providers.bzl", "SoongModuleInfo") - -def _custom_impl(ctx): - return [SoongModuleInfo()] - -custom = rule( - implementation = _custom_impl, - attrs = { - "soong_module_name": attr.string(mandatory = True), - "soong_module_variant": attr.string(), - "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), - "api": attr.string(), - "arch_paths": attr.string_list(), - "arch_paths_exclude": attr.string_list(), - # bazel_module start -# "label": attr.string(), -# "bp2build_available": attr.bool(), - # bazel_module end - "bool_prop": attr.bool(), - "bool_ptr_prop": attr.bool(), - "dir": attr.string(), - "does_not_convert_to_bazel": attr.bool(), - "embedded_prop": attr.string(), - "int64_ptr_prop": attr.int(), - # nested_props start -# "nested_prop": attr.string(), - # nested_props end - # nested_props_ptr start -# "nested_prop": attr.string(), - # nested_props_ptr end - "one_to_many_prop": attr.bool(), - "other_embedded_prop": attr.string(), - "string_list_prop": attr.string_list(), - "string_literal_prop": attr.string(), - "string_prop": attr.string(), - "string_ptr_prop": attr.string(), - "test_config_setting": attr.bool(), - }, -) - -def _custom_defaults_impl(ctx): - return [SoongModuleInfo()] - -custom_defaults = rule( - implementation = _custom_defaults_impl, - attrs = { - "soong_module_name": attr.string(mandatory = True), - "soong_module_variant": attr.string(), - "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), - "api": attr.string(), - "arch_paths": attr.string_list(), - "arch_paths_exclude": attr.string_list(), - "bool_prop": attr.bool(), - "bool_ptr_prop": attr.bool(), - "dir": attr.string(), - "does_not_convert_to_bazel": attr.bool(), - "embedded_prop": attr.string(), - "int64_ptr_prop": attr.int(), - # nested_props start -# "nested_prop": attr.string(), - # nested_props end - # nested_props_ptr start -# "nested_prop": attr.string(), - # nested_props_ptr end - "one_to_many_prop": attr.bool(), - "other_embedded_prop": attr.string(), - "string_list_prop": attr.string_list(), - "string_literal_prop": attr.string(), - "string_prop": attr.string(), - "string_ptr_prop": attr.string(), - "test_config_setting": attr.bool(), - }, -) - -def _custom_test__impl(ctx): - return [SoongModuleInfo()] - -custom_test_ = rule( - implementation = _custom_test__impl, - attrs = { - "soong_module_name": attr.string(mandatory = True), - "soong_module_variant": attr.string(), - "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), - "api": attr.string(), - "arch_paths": attr.string_list(), - "arch_paths_exclude": attr.string_list(), - "bool_prop": attr.bool(), - "bool_ptr_prop": attr.bool(), - "dir": attr.string(), - "does_not_convert_to_bazel": attr.bool(), - "embedded_prop": attr.string(), - "int64_ptr_prop": attr.int(), - # nested_props start -# "nested_prop": attr.string(), - # nested_props end - # nested_props_ptr start -# "nested_prop": attr.string(), - # nested_props_ptr end - "one_to_many_prop": attr.bool(), - "other_embedded_prop": attr.string(), - "string_list_prop": attr.string_list(), - "string_literal_prop": attr.string(), - "string_prop": attr.string(), - "string_ptr_prop": attr.string(), - "test_config_setting": attr.bool(), - # test_prop start -# "test_string_prop": attr.string(), - # test_prop end - }, -) -` - - if ruleShim.content != expectedBzl { - t.Errorf( - "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s", - expectedBzl, - ruleShim.content) - } -} - -func TestGenerateSoongModuleBzl(t *testing.T) { - ruleShims := map[string]RuleShim{ - "file1": RuleShim{ - rules: []string{"a", "b"}, - content: "irrelevant", - }, - "file2": RuleShim{ - rules: []string{"c", "d"}, - content: "irrelevant", - }, - } - files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView) - - var actualSoongModuleBzl BazelFile - for _, f := range files { - if f.Basename == "soong_module.bzl" { - actualSoongModuleBzl = f - } - } - - expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b") -load("//build/bazel/queryview_rules:file2.bzl", "c", "d") -` - expectedRuleMap := `soong_module_rule_map = { - "a": a, - "b": b, - "c": c, - "d": d, -}` - if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) { - t.Errorf( - "Generated soong_module.bzl:\n\n%s\n\n"+ - "Could not find the load statement in the generated soong_module.bzl:\n%s", - actualSoongModuleBzl.Contents, - expectedLoad) - } - - if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) { - t.Errorf( - "Generated soong_module.bzl:\n\n%s\n\n"+ - "Could not find the module -> rule map in the generated soong_module.bzl:\n%s", - actualSoongModuleBzl.Contents, - expectedRuleMap) - } -} diff --git a/bp2build/cc_binary_conversion_test.go b/bp2build/cc_binary_conversion_test.go deleted file mode 100644 index 2857a91df..000000000 --- a/bp2build/cc_binary_conversion_test.go +++ /dev/null @@ -1,1309 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "fmt" - "strings" - "testing" - - "android/soong/android" - "android/soong/cc" - "android/soong/genrule" -) - -const ( - ccBinaryTypePlaceHolder = "{rule_name}" -) - -type testBazelTarget struct { - typ string - name string - attrs AttrNameToString -} - -func generateBazelTargetsForTest(targets []testBazelTarget, hod android.HostOrDeviceSupported) []string { - ret := make([]string, 0, len(targets)) - for _, t := range targets { - attrs := t.attrs.clone() - ret = append(ret, makeBazelTargetHostOrDevice(t.typ, t.name, attrs, hod)) - } - return ret -} - -type ccBinaryBp2buildTestCase struct { - description string - filesystem map[string]string - blueprint string - targets []testBazelTarget - stubbedBuildDefinitions []string -} - -func registerCcBinaryModuleTypes(ctx android.RegistrationContext) { - cc.RegisterCCBuildComponents(ctx) - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) -} - -var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary") -var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host") - -func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) { - t.Helper() - runCcBinaryTestCase(t, tc) - runCcHostBinaryTestCase(t, tc) -} - -func runCcBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) { - t.Helper() - moduleTypeUnderTest := "cc_binary" - - description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description) - t.Run(description, func(t *testing.T) { - t.Helper() - RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{ - ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.DeviceSupported), - ModuleTypeUnderTest: moduleTypeUnderTest, - ModuleTypeUnderTestFactory: cc.BinaryFactory, - Description: description, - Blueprint: binaryReplacer.Replace(testCase.blueprint), - Filesystem: testCase.filesystem, - StubbedBuildDefinitions: testCase.stubbedBuildDefinitions, - }) - }) -} - -func runCcHostBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) { - t.Helper() - moduleTypeUnderTest := "cc_binary_host" - description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description) - t.Run(description, func(t *testing.T) { - RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{ - ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostSupported), - ModuleTypeUnderTest: moduleTypeUnderTest, - ModuleTypeUnderTestFactory: cc.BinaryHostFactory, - Description: description, - Blueprint: hostBinaryReplacer.Replace(testCase.blueprint), - Filesystem: testCase.filesystem, - StubbedBuildDefinitions: testCase.stubbedBuildDefinitions, - }) - }) -} - -func TestBasicCcBinary(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "basic -- properties -> attrs with little/no transformation", - filesystem: map[string]string{ - soongCcVersionLibBpPath: soongCcVersionLibBp, - }, - stubbedBuildDefinitions: []string{"//build/soong/cc/libbuildversion:libbuildversion"}, - blueprint: ` -{rule_name} { - name: "foo", - srcs: ["a.cc"], - local_include_dirs: ["dir"], - include_dirs: ["absolute_dir"], - cflags: ["-Dcopt"], - cppflags: ["-Dcppflag"], - conlyflags: ["-Dconlyflag"], - asflags: ["-Dasflag"], - ldflags: ["ld-flag"], - rtti: true, - strip: { - all: true, - keep_symbols: true, - keep_symbols_and_debug_frame: true, - keep_symbols_list: ["symbol"], - none: true, - }, - sdk_version: "current", - min_sdk_version: "29", - use_version_lib: true, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "absolute_includes": `["absolute_dir"]`, - "asflags": `["-Dasflag"]`, - "conlyflags": `["-Dconlyflag"]`, - "copts": `["-Dcopt"]`, - "cppflags": `["-Dcppflag"]`, - "linkopts": `["ld-flag"]`, - "local_includes": `[ - "dir", - ".", - ]`, - "rtti": `True`, - "srcs": `["a.cc"]`, - "strip": `{ - "all": True, - "keep_symbols": True, - "keep_symbols_and_debug_frame": True, - "keep_symbols_list": ["symbol"], - "none": True, - }`, - "sdk_version": `"current"`, - "min_sdk_version": `"29"`, - "use_version_lib": `True`, - "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`, - }, - }, - }, - }) -} - -func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: `ldflag "-shared" disables static_flag feature`, - blueprint: ` -{rule_name} { - name: "foo", - ldflags: ["-shared"], - include_build_directory: false, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "features": `["-static_flag"]`, - "linkopts": `["-shared"]`, - }, - }, - }, - }) -} - -func TestCcBinaryWithLinkStatic(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "link static", - blueprint: ` -{rule_name} { - name: "foo", - static_executable: true, - include_build_directory: false, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "linkshared": `False`, - }, - }, - }, - }) -} - -func TestCcBinaryVersionScriptAndDynamicList(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: `version script and dynamic list`, - blueprint: ` -{rule_name} { - name: "foo", - include_build_directory: false, - version_script: "vs", - dynamic_list: "dynamic.list", -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "additional_linker_inputs": `[ - "vs", - "dynamic.list", - ]`, - "linkopts": `[ - "-Wl,--version-script,$(location vs)", - "-Wl,--dynamic-list,$(location dynamic.list)", - ]`, - "features": `["android_cfi_exports_map"]`, - }, - }, - }, - }) -} - -func TestCcBinaryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)", - blueprint: ` -{rule_name} { - name: "foo", - ldflags: [ - "--nospace_flag", - "-z spaceflag", - ], - version_script: "version_script", - dynamic_list: "dynamic.list", - include_build_directory: false, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "additional_linker_inputs": `[ - "version_script", - "dynamic.list", - ]`, - "features": `["android_cfi_exports_map"]`, - "linkopts": `[ - "--nospace_flag", - "-z", - "spaceflag", - "-Wl,--version-script,$(location version_script)", - "-Wl,--dynamic-list,$(location dynamic.list)", - ]`, - }}}, - }) -} - -func TestCcBinarySplitSrcsByLang(t *testing.T) { - runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "split srcs by lang", - stubbedBuildDefinitions: []string{"fg_foo"}, - blueprint: ` -{rule_name} { - name: "foo", - srcs: [ - "asonly.S", - "conly.c", - "cpponly.cpp", - ":fg_foo", - ], - include_build_directory: false, -} -` + simpleModule("filegroup", "fg_foo"), - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "srcs": `[ - "cpponly.cpp", - ":fg_foo_cpp_srcs", - ]`, - "srcs_as": `[ - "asonly.S", - ":fg_foo_as_srcs", - ]`, - "srcs_c": `[ - "conly.c", - ":fg_foo_c_srcs", - ]`, - }, - }, - }, - }) -} - -func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "no implementation deps", - stubbedBuildDefinitions: []string{"generated_hdr", "export_generated_hdr", "static_dep", "implementation_static_dep", - "whole_static_dep", "not_explicitly_exported_whole_static_dep", "shared_dep", "implementation_shared_dep"}, - blueprint: ` -genrule { - name: "generated_hdr", - cmd: "nothing to see here", -} - -genrule { - name: "export_generated_hdr", - cmd: "nothing to see here", -} - -{rule_name} { - name: "foo", - srcs: ["foo.cpp"], - shared_libs: ["implementation_shared_dep", "shared_dep"], - export_shared_lib_headers: ["shared_dep"], - static_libs: ["implementation_static_dep", "static_dep"], - export_static_lib_headers: ["static_dep", "whole_static_dep"], - whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"], - include_build_directory: false, - generated_headers: ["generated_hdr", "export_generated_hdr"], - export_generated_headers: ["export_generated_hdr"], -} -` + - simpleModule("cc_library_static", "static_dep") + - simpleModule("cc_library_static", "implementation_static_dep") + - simpleModule("cc_library_static", "whole_static_dep") + - simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep") + - simpleModule("cc_library", "shared_dep") + - simpleModule("cc_library", "implementation_shared_dep"), - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "deps": `[ - ":implementation_static_dep", - ":static_dep", - ]`, - "dynamic_deps": `[ - ":implementation_shared_dep", - ":shared_dep", - ]`, - "srcs": `[ - "foo.cpp", - ":generated_hdr", - ":export_generated_hdr", - ]`, - "whole_archive_deps": `[ - ":not_explicitly_exported_whole_static_dep", - ":whole_static_dep", - ]`, - "local_includes": `["."]`, - }, - }, - }, - }) -} - -func TestCcBinaryNocrtTests(t *testing.T) { - baseTestCases := []struct { - description string - soongProperty string - bazelAttr AttrNameToString - }{ - { - description: "nocrt: true", - soongProperty: `nocrt: true,`, - bazelAttr: AttrNameToString{"features": `["-link_crt"]`}, - }, - { - description: "nocrt: false", - soongProperty: `nocrt: false,`, - bazelAttr: AttrNameToString{}, - }, - { - description: "nocrt: not set", - bazelAttr: AttrNameToString{}, - }, - } - - baseBlueprint := `{rule_name} { - name: "foo",%s - include_build_directory: false, -} -` - - for _, btc := range baseTestCases { - prop := btc.soongProperty - if len(prop) > 0 { - prop = "\n" + prop - } - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: btc.description, - blueprint: fmt.Sprintf(baseBlueprint, prop), - targets: []testBazelTarget{ - {"cc_binary", "foo", btc.bazelAttr}, - }, - }) - } -} - -func TestCcBinaryNo_libcrtTests(t *testing.T) { - baseTestCases := []struct { - description string - soongProperty string - bazelAttr AttrNameToString - }{ - { - description: "no_libcrt: true", - soongProperty: `no_libcrt: true,`, - bazelAttr: AttrNameToString{"features": `["-use_libcrt"]`}, - }, - { - description: "no_libcrt: false", - soongProperty: `no_libcrt: false,`, - bazelAttr: AttrNameToString{}, - }, - { - description: "no_libcrt: not set", - bazelAttr: AttrNameToString{}, - }, - } - - baseBlueprint := `{rule_name} { - name: "foo",%s - include_build_directory: false, -} -` - - for _, btc := range baseTestCases { - prop := btc.soongProperty - if len(prop) > 0 { - prop = "\n" + prop - } - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: btc.description, - blueprint: fmt.Sprintf(baseBlueprint, prop), - targets: []testBazelTarget{ - {"cc_binary", "foo", btc.bazelAttr}, - }, - }) - } -} - -func TestCcBinaryPropertiesToFeatures(t *testing.T) { - baseTestCases := []struct { - description string - soongProperty string - bazelAttr AttrNameToString - }{ - { - description: "pack_relocation: true", - soongProperty: `pack_relocations: true,`, - bazelAttr: AttrNameToString{}, - }, - { - description: "pack_relocations: false", - soongProperty: `pack_relocations: false,`, - bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`}, - }, - { - description: "pack_relocations: not set", - bazelAttr: AttrNameToString{}, - }, - { - description: "pack_relocation: true", - soongProperty: `allow_undefined_symbols: true,`, - bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`}, - }, - { - description: "allow_undefined_symbols: false", - soongProperty: `allow_undefined_symbols: false,`, - bazelAttr: AttrNameToString{}, - }, - { - description: "allow_undefined_symbols: not set", - bazelAttr: AttrNameToString{}, - }, - } - - baseBlueprint := `{rule_name} { - name: "foo",%s - include_build_directory: false, -} -` - for _, btc := range baseTestCases { - prop := btc.soongProperty - if len(prop) > 0 { - prop = "\n" + prop - } - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: btc.description, - blueprint: fmt.Sprintf(baseBlueprint, prop), - targets: []testBazelTarget{ - {"cc_binary", "foo", btc.bazelAttr}, - }, - }) - } -} - -func TestCcBinarySharedProto(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - stubbedBuildDefinitions: []string{"libprotobuf-cpp-full", "libprotobuf-cpp-lite"}, - blueprint: soongCcProtoLibraries + `{rule_name} { - name: "foo", - srcs: ["foo.proto"], - proto: { - }, - 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{ - stubbedBuildDefinitions: []string{"libprotobuf-cpp-full", "libprotobuf-cpp-lite"}, - blueprint: soongCcProtoLibraries + `{rule_name} { - name: "foo", - srcs: ["foo.proto"], - static_executable: true, - proto: { - }, - 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`, - }}, - }, - }) -} - -func TestCcBinaryConvertLex(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: `.l and .ll sources converted to .c and .cc`, - blueprint: ` -{rule_name} { - name: "foo", - srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"], - lex: { flags: ["--foo_opt", "--bar_opt"] }, - include_build_directory: false, -} -`, - targets: []testBazelTarget{ - {"genlex", "foo_genlex_l", AttrNameToString{ - "srcs": `[ - "foo1.l", - "foo2.l", - ]`, - "lexopts": `[ - "--foo_opt", - "--bar_opt", - ]`, - }}, - {"genlex", "foo_genlex_ll", AttrNameToString{ - "srcs": `[ - "bar1.ll", - "bar2.ll", - ]`, - "lexopts": `[ - "--foo_opt", - "--bar_opt", - ]`, - }}, - {"cc_binary", "foo", AttrNameToString{ - "srcs": `[ - "bar.cc", - ":foo_genlex_ll", - ]`, - "srcs_c": `[ - "foo.c", - ":foo_genlex_l", - ]`, - }}, - }, - }) -} - -func TestCcBinaryRuntimeLibs(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "cc_binary with runtime libs", - blueprint: ` -cc_library { - name: "bar", - srcs: ["b.cc"], -} - -{rule_name} { - name: "foo", - srcs: ["a.cc"], - runtime_libs: ["bar"], -} -`, - targets: []testBazelTarget{ - {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["b.cc"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - }, - {"cc_library_shared", "bar", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["b.cc"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - }, - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["a.cc"]`, - "runtime_deps": `[":bar"]`, - }, - }, - }, - }) -} - -func TestCcBinaryWithInstructionSet(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "instruction set", - blueprint: ` -{rule_name} { - name: "foo", - arch: { - arm: { - instruction_set: "arm", - } - } -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arm_isa_arm"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }}, - }, - }) -} - -func TestCcBinaryEmptySuffix(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "binary with empty suffix", - blueprint: ` -{rule_name} { - name: "foo", - suffix: "", -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "suffix": `""`, - }}, - }, - }) -} - -func TestCcBinarySuffix(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "binary with suffix", - blueprint: ` -{rule_name} { - name: "foo", - suffix: "-suf", -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "suffix": `"-suf"`, - }}, - }, - }) -} - -func TestCcArchVariantBinarySuffix(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: "binary with suffix", - blueprint: ` -{rule_name} { - name: "foo", - arch: { - arm64: { suffix: "-64" }, - arm: { suffix: "-32" }, - }, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "suffix": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "-32", - "//build/bazel_common_rules/platforms/arch:arm64": "-64", - "//conditions:default": None, - })`, - }}, - }, - }) -} - -func TestCcBinaryWithSyspropSrcs(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary with sysprop sources", - blueprint: ` -{rule_name} { - name: "foo", - srcs: [ - "bar.sysprop", - "baz.sysprop", - "blah.cpp", - ], - min_sdk_version: "5", -}`, - targets: []testBazelTarget{ - {"sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `[ - "bar.sysprop", - "baz.sysprop", - ]`, - }}, - {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }}, - {"cc_binary", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`, - }}, - }, - }) -} - -func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary with sysprop sources in some configs but not others", - blueprint: ` -{rule_name} { - name: "foo", - srcs: [ - "blah.cpp", - ], - target: { - android: { - srcs: ["bar.sysprop"], - }, - }, - min_sdk_version: "5", -}`, - targets: []testBazelTarget{ - {"sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `select({ - "//build/bazel_common_rules/platforms/os:android": ["bar.sysprop"], - "//conditions:default": [], - })`, - }}, - {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }}, - {"cc_binary", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":foo_cc_sysprop_library_static"], - "//conditions:default": [], - })`, - }}, - }, - }) -} - -func TestCcBinaryWithIntegerOverflowProperty(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary with integer overflow property specified", - blueprint: ` -{rule_name} { - name: "foo", - sanitize: { - integer_overflow: true, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `["ubsan_integer_overflow"]`, - }}, - }, - }) -} - -func TestCcBinaryWithMiscUndefinedProperty(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary with miscellaneous properties specified", - blueprint: ` -{rule_name} { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ]`, - }}, - }, - }) -} - -func TestCcBinaryWithUBSanPropertiesArchSpecific(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct feature select when UBSan props are specified in arch specific blocks", - blueprint: ` -{rule_name} { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, - target: { - android: { - sanitize: { - misc_undefined: ["alignment"], - }, - }, - linux_glibc: { - sanitize: { - integer_overflow: true, - }, - }, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ] + select({ - "//build/bazel_common_rules/platforms/os:android": ["ubsan_alignment"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["ubsan_integer_overflow"], - "//conditions:default": [], - })`, - }}, - }, - }) -} - -func TestCcBinaryWithSanitizerBlocklist(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has the correct feature when sanitize.blocklist is provided", - blueprint: ` -{rule_name} { - name: "foo", - sanitize: { - blocklist: "foo_blocklist.txt", - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "copts": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"], - "//conditions:default": [], - })`, - "additional_compiler_inputs": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }}, - }, - }) -} - -func TestCcBinaryWithThinLto(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when thin LTO is enabled", - blueprint: ` -{rule_name} { - name: "foo", - lto: { - thin: true, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `["android_thin_lto"]`, - }}, - }, - }) -} - -func TestCcBinaryWithLtoNever(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when LTO is explicitly disabled", - blueprint: ` -{rule_name} { - name: "foo", - lto: { - never: true, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `["-android_thin_lto"]`, - }}, - }, - }) -} - -func TestCcBinaryWithThinLtoArchSpecific(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when LTO differs across arch and os variants", - blueprint: ` -{rule_name} { - name: "foo", - target: { - android: { - lto: { - thin: true, - }, - }, - }, - arch: { - riscv64: { - lto: { - thin: false, - }, - }, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_riscv64": ["-android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_thin_lto"], - "//conditions:default": [], - })`, - }}, - }, - }) -} - -func TestCcBinaryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when LTO disabled by default but enabled on a particular variant", - blueprint: ` -{rule_name} { - name: "foo", - lto: { - never: true, - }, - target: { - android: { - lto: { - thin: true, - never: false, - }, - }, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_thin_lto"], - "//conditions:default": ["-android_thin_lto"], - })`, - }}, - }, - }) -} - -func TestCcBinaryWithThinLtoAndWholeProgramVtables(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when thin LTO is enabled with whole_program_vtables", - blueprint: ` -{rule_name} { - name: "foo", - lto: { - thin: true, - }, - whole_program_vtables: true, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `[ - "android_thin_lto", - "android_thin_lto_whole_program_vtables", - ]`, - }}, - }, - }) -} - -func TestCcBinaryHiddenVisibilityConvertedToFeature(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary changes hidden visibility to feature", - blueprint: ` -{rule_name} { - name: "foo", - cflags: ["-fvisibility=hidden"], -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `["visibility_hidden"]`, - }}, - }, - }) -} - -func TestCcBinaryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary changes hidden visibility to feature for specific os", - blueprint: ` -{rule_name} { - name: "foo", - target: { - android: { - cflags: ["-fvisibility=hidden"], - }, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["visibility_hidden"], - "//conditions:default": [], - })`, - }}, - }, - }) -} - -func TestCcBinaryWithCfi(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when cfi is enabled", - blueprint: ` -{rule_name} { - name: "foo", - sanitize: { - cfi: true, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "features": `["android_cfi"]`, - "local_includes": `["."]`, - }}, - }, - }) -} - -func TestCcBinaryWithCfiOsSpecific(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when cfi is enabled for specific variants", - blueprint: ` -{rule_name} { - name: "foo", - target: { - android: { - sanitize: { - cfi: true, - }, - }, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_cfi"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }}, - }, - }) -} - -func TestCcBinaryWithCfiAndCfiAssemblySupport(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary has correct features when cfi is enabled with cfi assembly support", - blueprint: ` -{rule_name} { - name: "foo", - sanitize: { - cfi: true, - config: { - cfi_assembly_support: true, - }, - }, -}`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "features": `[ - "android_cfi", - "android_cfi_assembly_support", - ]`, - "local_includes": `["."]`, - }}, - }, - }) -} - -func TestCcBinaryExplicitlyDisablesCfiWhenFalse(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary disables cfi when explciitly set to false in the bp", - blueprint: ` -{rule_name} { - name: "foo", - sanitize: { - cfi: false, - }, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo", AttrNameToString{ - "features": `["-android_cfi"]`, - "local_includes": `["."]`, - }}, - }, - }) -} - -func TestCcBinaryStem(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - description: "cc_binary with stem property", - blueprint: ` -cc_binary { - name: "foo_with_stem_simple", - stem: "foo", -} -cc_binary { - name: "foo_with_arch_variant_stem", - arch: { - arm: { - stem: "foo-arm", - }, - arm64: { - stem: "foo-arm64", - }, - }, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "foo_with_stem_simple", AttrNameToString{ - "stem": `"foo"`, - "local_includes": `["."]`, - }}, - {"cc_binary", "foo_with_arch_variant_stem", AttrNameToString{ - "stem": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "foo-arm", - "//build/bazel_common_rules/platforms/arch:arm64": "foo-arm64", - "//conditions:default": None, - })`, - "local_includes": `["."]`, - }}, - }, - }) -} - -func TestCCBinaryRscriptSrc(t *testing.T) { - runCcBinaryTests(t, ccBinaryBp2buildTestCase{ - description: `cc_binary with rscript files in sources`, - blueprint: ` -{rule_name} { - name : "foo", - srcs : [ - "ccSrc.cc", - "rsSrc.rscript", - ], - include_build_directory: false, -} -`, - targets: []testBazelTarget{ - {"rscript_to_cpp", "foo_renderscript", AttrNameToString{ - "srcs": `["rsSrc.rscript"]`, - }}, - {"cc_binary", "foo", AttrNameToString{ - "absolute_includes": `[ - "frameworks/rs", - "frameworks/rs/cpp", - ]`, - "local_includes": `["."]`, - "srcs": `[ - "ccSrc.cc", - "foo_renderscript", - ]`, - }}, - }, - }) -} - -func TestCcBinaryStatic_SystemSharedLibUsedAsDep(t *testing.T) { - runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{ - stubbedBuildDefinitions: []string{"libm", "libc"}, - description: "cc_library_static system_shared_lib empty for linux_bionic variant", - blueprint: soongCcLibraryStaticPreamble + - simpleModule("cc_library", "libc") + ` - -cc_library { - name: "libm", -} - -cc_binary { - name: "used_in_bionic_oses", - target: { - android: { - static_libs: ["libc"], - }, - linux_bionic: { - static_libs: ["libc"], - }, - }, - include_build_directory: false, - static_executable: true, -} - -cc_binary { - name: "all", - static_libs: ["libc"], - include_build_directory: false, - static_executable: true, -} - -cc_binary { - name: "keep_for_empty_system_shared_libs", - static_libs: ["libc"], - system_shared_libs: [], - include_build_directory: false, - static_executable: true, -} - -cc_binary { - name: "used_with_stubs", - static_libs: ["libm"], - include_build_directory: false, - static_executable: true, -} - -cc_binary { - name: "keep_with_stubs", - static_libs: ["libm"], - system_shared_libs: [], - include_build_directory: false, - static_executable: true, -} -`, - targets: []testBazelTarget{ - {"cc_binary", "all", AttrNameToString{ - "linkshared": "False", - }}, - {"cc_binary", "keep_for_empty_system_shared_libs", AttrNameToString{ - "deps": `[":libc_bp2build_cc_library_static"]`, - "system_deps": `[]`, - "linkshared": "False", - }}, - {"cc_binary", "keep_with_stubs", AttrNameToString{ - "linkshared": "False", - "deps": `[":libm_bp2build_cc_library_static"]`, - "system_deps": `[]`, - }}, - {"cc_binary", "used_in_bionic_oses", AttrNameToString{ - "linkshared": "False", - }}, - {"cc_binary", "used_with_stubs", AttrNameToString{ - "linkshared": "False", - }}, - }, - }) -} diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go deleted file mode 100644 index 188c81b2a..000000000 --- a/bp2build/cc_library_conversion_test.go +++ /dev/null @@ -1,5366 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/aidl_library" - "android/soong/android" - "android/soong/cc" -) - -const ( - // See cc/testing.go for more context - soongCcLibraryPreamble = ` -cc_defaults { - name: "linux_bionic_supported", -} -` - - soongCcVersionLibBpPath = "build/soong/cc/libbuildversion/Android.bp" - soongCcVersionLibBp = ` -cc_library_static { - name: "libbuildversion", -} -` - - soongCcProtoLibraries = ` -cc_library { - name: "libprotobuf-cpp-lite", -} - -cc_library { - name: "libprotobuf-cpp-full", -}` - - soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries -) - -func runCcLibraryTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - tc.StubbedBuildDefinitions = append(tc.StubbedBuildDefinitions, "libprotobuf-cpp-lite", "libprotobuf-cpp-full") - RunBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc) -} - -func registerCcLibraryModuleTypes(ctx android.RegistrationContext) { - cc.RegisterCCBuildComponents(ctx) - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) - ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory) - ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) - ctx.RegisterModuleType("aidl_library", aidl_library.AidlLibraryFactory) - ctx.RegisterModuleType("ndk_library", cc.NdkLibraryFactory) -} - -func TestCcLibrarySimple(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library - simple example", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"//build/soong/cc/libbuildversion:libbuildversion", "some-headers"}, - Filesystem: map[string]string{ - soongCcVersionLibBpPath: soongCcVersionLibBp, - "android.cpp": "", - "bionic.cpp": "", - "darwin.cpp": "", - // Refer to cc.headerExts for the supported header extensions in Soong. - "header.h": "", - "header.hh": "", - "header.hpp": "", - "header.hxx": "", - "header.h++": "", - "header.inl": "", - "header.inc": "", - "header.ipp": "", - "header.h.generic": "", - "impl.cpp": "", - "linux.cpp": "", - "x86.cpp": "", - "x86_64.cpp": "", - "foo-dir/a.h": "", - }, - Blueprint: soongCcLibraryPreamble + - simpleModule("cc_library_headers", "some-headers") + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - cflags: ["-Wall"], - header_libs: ["some-headers"], - export_include_dirs: ["foo-dir"], - ldflags: ["-Wl,--exclude-libs=bar.a"], - arch: { - x86: { - ldflags: ["-Wl,--exclude-libs=baz.a"], - srcs: ["x86.cpp"], - }, - x86_64: { - ldflags: ["-Wl,--exclude-libs=qux.a"], - srcs: ["x86_64.cpp"], - }, - }, - target: { - android: { - srcs: ["android.cpp"], - }, - linux_glibc: { - srcs: ["linux.cpp"], - }, - darwin: { - srcs: ["darwin.cpp"], - }, - bionic: { - srcs: ["bionic.cpp"] - }, - }, - include_build_directory: false, - sdk_version: "current", - min_sdk_version: "29", - use_version_lib: true, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "copts": `["-Wall"]`, - "export_includes": `["foo-dir"]`, - "implementation_deps": `[":some-headers"]`, - "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"], - "//conditions:default": [], - })`, - "srcs": `["impl.cpp"] + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["x86.cpp"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["x86_64.cpp"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [ - "bionic.cpp", - "android.cpp", - ], - "//build/bazel_common_rules/platforms/os:darwin": ["darwin.cpp"], - "//build/bazel_common_rules/platforms/os:linux_bionic": ["bionic.cpp"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.cpp"], - "//conditions:default": [], - })`, - "sdk_version": `"current"`, - "min_sdk_version": `"29"`, - "use_version_lib": `True`, - "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`, - "deps": `select({ - "//build/bazel/rules/apex:unbundled_app": ["//build/bazel/rules/cc:ndk_sysroot"], - "//conditions:default": [], - })`, - }), - }) -} - -func TestCcLibraryTrimmedLdAndroid(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library - trimmed example of //bionic/linker:ld-android", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"libc_headers"}, - Filesystem: map[string]string{ - "ld-android.cpp": "", - "linked_list.h": "", - "linker.h": "", - "linker_block_allocator.h": "", - "linker_cfi.h": "", - }, - Blueprint: soongCcLibraryPreamble + - simpleModule("cc_library_headers", "libc_headers") + ` -cc_library { - name: "fake-ld-android", - srcs: ["ld_android.cpp"], - cflags: [ - "-Wall", - "-Wextra", - "-Wunused", - "-Werror", - ], - header_libs: ["libc_headers"], - ldflags: [ - "-Wl,--exclude-libs=libgcc.a", - "-Wl,--exclude-libs=libgcc_stripped.a", - "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a", - "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a", - "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a", - "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a", - ], - arch: { - x86: { - ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"], - }, - x86_64: { - ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{ - "srcs": `["ld_android.cpp"]`, - "copts": `[ - "-Wall", - "-Wextra", - "-Wunused", - "-Werror", - ]`, - "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", - "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a", - "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a", - "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a", - ] + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"], - "//conditions:default": [], - })`, - }), - }) -} - -func TestCcLibraryExcludeSrcs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Dir: "external", - Filesystem: map[string]string{ - "external/math/cosf.c": "", - "external/math/erf.c": "", - "external/math/erf_data.c": "", - "external/math/erff.c": "", - "external/math/erff_data.c": "", - "external/Android.bp": ` -cc_library { - name: "fake-libarm-optimized-routines-math", - exclude_srcs: [ - // Provided by: - // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c - // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c - "math/erf.c", - "math/erf_data.c", - "math/erff.c", - "math/erff_data.c", - ], - srcs: [ - "math/*.c", - ], - // arch-specific settings - arch: { - arm64: { - cflags: [ - "-DHAVE_FAST_FMA=1", - ], - }, - }, - bazel_module: { bp2build_available: true }, -} -`, - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{ - "copts": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - "srcs_c": `["math/cosf.c"]`, - }), - }) -} - -func TestCcLibrarySharedStaticProps(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library shared/static props", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "both.cpp": "", - "sharedonly.cpp": "", - "staticonly.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "a", - srcs: ["both.cpp"], - cflags: ["bothflag"], - shared_libs: ["shared_dep_for_both"], - static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"], - whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"], - static: { - srcs: ["staticonly.cpp"], - cflags: ["staticflag"], - shared_libs: ["shared_dep_for_static"], - static_libs: ["static_dep_for_static"], - whole_static_libs: ["whole_static_lib_for_static"], - }, - shared: { - srcs: ["sharedonly.cpp"], - cflags: ["sharedflag"], - shared_libs: ["shared_dep_for_shared"], - static_libs: ["static_dep_for_shared"], - whole_static_libs: ["whole_static_lib_for_shared"], - }, - include_build_directory: false, -} - -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: "whole_static_lib_for_shared", -} - -cc_library_static { - name: "whole_static_lib_for_static", -} - -cc_library_static { - name: "whole_static_lib_for_both", -} - -cc_library_static { - name: "whole_and_static_lib_for_both", -} - -cc_library { - name: "shared_dep_for_shared", -} - -cc_library { - name: "shared_dep_for_static", -} - -cc_library { - name: "shared_dep_for_both", -} -`, - StubbedBuildDefinitions: []string{"static_dep_for_shared", "static_dep_for_static", - "static_dep_for_both", "whole_static_lib_for_shared", "whole_static_lib_for_static", - "whole_static_lib_for_both", "whole_and_static_lib_for_both", "shared_dep_for_shared", - "shared_dep_for_static", "shared_dep_for_both", - }, - ExpectedBazelTargets: []string{ - 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_and_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_and_static_lib_for_both", - ":whole_static_lib_for_shared", - ]`, - }), - }, - }) -} - -func TestCcLibraryDeps(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library shared/static props", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "both.cpp": "", - "sharedonly.cpp": "", - "staticonly.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "a", - srcs: ["both.cpp"], - cflags: ["bothflag"], - shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"], - export_shared_lib_headers: ["shared_dep_for_both"], - static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"], - export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"], - whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"], - static: { - srcs: ["staticonly.cpp"], - cflags: ["staticflag"], - shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"], - export_shared_lib_headers: ["shared_dep_for_static"], - static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"], - export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"], - whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"], - }, - shared: { - srcs: ["sharedonly.cpp"], - cflags: ["sharedflag"], - shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"], - export_shared_lib_headers: ["shared_dep_for_shared"], - static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"], - export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"], - whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"], - }, - include_build_directory: false, -} -` + simpleModule("cc_library_static", "static_dep_for_shared") + - simpleModule("cc_library_static", "implementation_static_dep_for_shared") + - simpleModule("cc_library_static", "static_dep_for_static") + - simpleModule("cc_library_static", "implementation_static_dep_for_static") + - simpleModule("cc_library_static", "static_dep_for_both") + - simpleModule("cc_library_static", "implementation_static_dep_for_both") + - simpleModule("cc_library_static", "whole_static_dep_for_shared") + - simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") + - simpleModule("cc_library_static", "whole_static_dep_for_static") + - simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") + - simpleModule("cc_library_static", "whole_static_dep_for_both") + - simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") + - simpleModule("cc_library", "shared_dep_for_shared") + - simpleModule("cc_library", "implementation_shared_dep_for_shared") + - simpleModule("cc_library", "shared_dep_for_static") + - simpleModule("cc_library", "implementation_shared_dep_for_static") + - simpleModule("cc_library", "shared_dep_for_both") + - simpleModule("cc_library", "implementation_shared_dep_for_both"), - StubbedBuildDefinitions: []string{"static_dep_for_shared", "implementation_static_dep_for_shared", - "static_dep_for_static", "implementation_static_dep_for_static", "static_dep_for_both", - "implementation_static_dep_for_both", "whole_static_dep_for_shared", - "not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_static", - "not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_both", - "not_explicitly_exported_whole_static_dep_for_both", "shared_dep_for_shared", - "implementation_shared_dep_for_shared", "shared_dep_for_static", - "implementation_shared_dep_for_static", "shared_dep_for_both", - "implementation_shared_dep_for_both", - }, - ExpectedBazelTargets: []string{ - 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) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Dir: "foo/bar", - StubbedBuildDefinitions: []string{"//foo/bar:whole_static_lib_for_shared", "//foo/bar:whole_static_lib_for_static", - "//foo/bar:whole_static_lib_for_both"}, - Filesystem: map[string]string{ - "foo/bar/Android.bp": ` -cc_library { - name: "a", - whole_static_libs: ["whole_static_lib_for_both"], - static: { - whole_static_libs: ["whole_static_lib_for_static"], - }, - shared: { - whole_static_libs: ["whole_static_lib_for_shared"], - }, - bazel_module: { bp2build_available: true }, - include_build_directory: false, -} - -cc_prebuilt_library_static { name: "whole_static_lib_for_shared" } - -cc_prebuilt_library_static { name: "whole_static_lib_for_static" } - -cc_prebuilt_library_static { name: "whole_static_lib_for_both" } -`, - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: []string{ - 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) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library shared/static props in arch", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - 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" } -`, - }, - StubbedBuildDefinitions: []string{"//foo/bar:static_dep_for_shared", "//foo/bar:static_dep_for_static", - "//foo/bar:static_dep_for_both", "//foo/bar:arm_static_dep_for_shared", "//foo/bar:arm_whole_static_dep_for_shared", - "//foo/bar:arm_shared_dep_for_shared", "//foo/bar:x86_dep_for_static", "//foo/bar:android_dep_for_shared", - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{ - "copts": `[ - "bothflag", - "staticflag", - ] + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["-DX86_STATIC"], - "//conditions:default": [], - })`, - "implementation_deps": `[ - ":static_dep_for_both", - ":static_dep_for_static", - ] + select({ - "//build/bazel_common_rules/platforms/arch:x86": [":x86_dep_for_static"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - "srcs": `[ - "both.cpp", - "staticonly.cpp", - ] + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["x86_static.cpp"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "copts": `[ - "bothflag", - "sharedflag", - ] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["-DARM_SHARED"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": ["-DANDROID_SHARED"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"], - "//conditions:default": [], - })`, - "implementation_deps": `[ - ":static_dep_for_both", - ":static_dep_for_shared", - ] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [":arm_static_dep_for_shared"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [":android_dep_for_shared"], - "//conditions:default": [], - })`, - "implementation_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [":arm_shared_dep_for_shared"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - "srcs": `[ - "both.cpp", - "sharedonly.cpp", - ] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arm_shared.cpp"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": ["android_shared.cpp"], - "//conditions:default": [], - })`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [":arm_whole_static_dep_for_shared"], - "//conditions:default": [], - })`, - }), - }, - }, - ) -} - -func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library shared/static props with c/cpp/s mixed sources", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Dir: "foo/bar", - StubbedBuildDefinitions: []string{"//foo/bar:shared_filegroup", "//foo/bar:static_filegroup", "//foo/bar:both_filegroup"}, - Filesystem: map[string]string{ - "foo/bar/both_source.cpp": "", - "foo/bar/both_source.cc": "", - "foo/bar/both_source.c": "", - "foo/bar/both_source.s": "", - "foo/bar/both_source.S": "", - "foo/bar/shared_source.cpp": "", - "foo/bar/shared_source.cc": "", - "foo/bar/shared_source.c": "", - "foo/bar/shared_source.s": "", - "foo/bar/shared_source.S": "", - "foo/bar/static_source.cpp": "", - "foo/bar/static_source.cc": "", - "foo/bar/static_source.c": "", - "foo/bar/static_source.s": "", - "foo/bar/static_source.S": "", - "foo/bar/Android.bp": ` -cc_library { - name: "a", - srcs: [ - "both_source.cpp", - "both_source.cc", - "both_source.c", - "both_source.s", - "both_source.S", - ":both_filegroup", - ], - static: { - srcs: [ - "static_source.cpp", - "static_source.cc", - "static_source.c", - "static_source.s", - "static_source.S", - ":static_filegroup", - ], - }, - shared: { - srcs: [ - "shared_source.cpp", - "shared_source.cc", - "shared_source.c", - "shared_source.s", - "shared_source.S", - ":shared_filegroup", - ], - }, - bazel_module: { bp2build_available: true }, -} - -filegroup { - name: "both_filegroup", - srcs: [ - // Not relevant, handled by filegroup macro - ], -} - -filegroup { - name: "shared_filegroup", - srcs: [ - // Not relevant, handled by filegroup macro - ], -} - -filegroup { - name: "static_filegroup", - srcs: [ - // Not relevant, handled by filegroup macro - ], -} -`, - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - "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", - ]`, - }), - 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 TestCcLibraryNonConfiguredVersionScriptAndDynamicList(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library non-configured version script and dynamic list", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Dir: "foo/bar", - Filesystem: map[string]string{ - "foo/bar/Android.bp": ` -cc_library { - name: "a", - srcs: ["a.cpp"], - version_script: "v.map", - dynamic_list: "dynamic.list", - bazel_module: { bp2build_available: true }, - include_build_directory: false, -} -`, - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "additional_linker_inputs": `[ - "v.map", - "dynamic.list", - ]`, - "linkopts": `[ - "-Wl,--version-script,$(location v.map)", - "-Wl,--dynamic-list,$(location dynamic.list)", - ]`, - "srcs": `["a.cpp"]`, - "features": `["android_cfi_exports_map"]`, - }), - }, - ) -} - -func TestCcLibraryConfiguredVersionScriptAndDynamicList(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library configured version script and dynamic list", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Dir: "foo/bar", - Filesystem: map[string]string{ - "foo/bar/Android.bp": ` -cc_library { - name: "a", - srcs: ["a.cpp"], - arch: { - arm: { - version_script: "arm.map", - dynamic_list: "dynamic_arm.list", - }, - arm64: { - version_script: "arm64.map", - dynamic_list: "dynamic_arm64.list", - }, - }, - - bazel_module: { bp2build_available: true }, - include_build_directory: false, -} - `, - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "additional_linker_inputs": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "arm.map", - "dynamic_arm.list", - ], - "//build/bazel_common_rules/platforms/arch:arm64": [ - "arm64.map", - "dynamic_arm64.list", - ], - "//conditions:default": [], - })`, - "linkopts": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "-Wl,--version-script,$(location arm.map)", - "-Wl,--dynamic-list,$(location dynamic_arm.list)", - ], - "//build/bazel_common_rules/platforms/arch:arm64": [ - "-Wl,--version-script,$(location arm64.map)", - "-Wl,--dynamic-list,$(location dynamic_arm64.list)", - ], - "//conditions:default": [], - })`, - "srcs": `["a.cpp"]`, - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["android_cfi_exports_map"], - "//build/bazel_common_rules/platforms/arch:arm64": ["android_cfi_exports_map"], - "//conditions:default": [], - })`, - }), - }, - ) -} - -func TestCcLibraryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "version_script": "", - "dynamic.list": "", - }, - Blueprint: ` -cc_library { - name: "foo", - ldflags: [ - "--nospace_flag", - "-z spaceflag", - ], - version_script: "version_script", - dynamic_list: "dynamic.list", - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["android_cfi_exports_map"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "additional_linker_inputs": `[ - "version_script", - "dynamic.list", - ]`, - "features": `["android_cfi_exports_map"]`, - "linkopts": `[ - "--nospace_flag", - "-z", - "spaceflag", - "-Wl,--version-script,$(location version_script)", - "-Wl,--dynamic-list,$(location dynamic.list)", - ]`, - }), - }, - }) -} - -func TestCcLibrarySharedLibs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library shared_libs", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"mylib"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "mylib", -} - -cc_library { - name: "a", - shared_libs: ["mylib",], - include_build_directory: false, -} -`, - 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", - ]`, - "native_coverage": `False`, - "srcs": `["a.cpp"]`, - })...) - expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/arch:x86_64": [ - "disable_pack_relocations", - "-no_undefined_symbols", - ], - "//conditions:default": [], - })`, - "native_coverage": `False`, - "srcs": `["b.cpp"]`, - })...) - expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/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", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "a", - srcs: ["a.cpp"], - pack_relocations: false, - allow_undefined_symbols: true, - include_build_directory: false, - native_coverage: false, -} - -cc_library { - name: "b", - srcs: ["b.cpp"], - arch: { - x86_64: { - pack_relocations: false, - allow_undefined_symbols: true, - }, - }, - include_build_directory: false, - native_coverage: false, -} - -cc_library { - name: "c", - srcs: ["c.cpp"], - target: { - darwin: { - pack_relocations: false, - allow_undefined_symbols: true, - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: expected_targets, - }) -} - -func TestCcLibrarySpacesInCopts(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library spaces in copts", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "a", - cflags: ["-include header.h",], - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "copts": `[ - "-include", - "header.h", - ]`, - }), - }, - ) -} - -func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library cppflags usage", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + `cc_library { - name: "a", - srcs: ["a.cpp"], - cflags: ["-Wall"], - cppflags: [ - "-fsigned-char", - "-pedantic", - ], - arch: { - arm64: { - cppflags: ["-DARM64=1"], - }, - }, - target: { - android: { - cppflags: ["-DANDROID=1"], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "copts": `["-Wall"]`, - "cppflags": `[ - "-fsigned-char", - "-pedantic", - ] + select({ - "//build/bazel_common_rules/platforms/arch:arm64": ["-DARM64=1"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": ["-DANDROID=1"], - "//conditions:default": [], - })`, - "srcs": `["a.cpp"]`, - }), - }, - ) -} - -func TestCcLibraryExcludeLibs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"arm_whole_static_lib_excludes", "malloc_not_svelte_whole_static_lib", - "arm_static_lib_excludes", "malloc_not_svelte_whole_static_lib_excludes", "arm_shared_lib_excludes", - "malloc_not_svelte_static_lib_excludes", "arm_shared_lib_excludes", "malloc_not_svelte_shared_lib", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library { - name: "foo_static", - srcs: ["common.c"], - whole_static_libs: [ - "arm_whole_static_lib_excludes", - "malloc_not_svelte_whole_static_lib_excludes" - ], - static_libs: [ - "arm_static_lib_excludes", - "malloc_not_svelte_static_lib_excludes" - ], - shared_libs: [ - "arm_shared_lib_excludes", - ], - arch: { - arm: { - exclude_shared_libs: [ - "arm_shared_lib_excludes", - ], - exclude_static_libs: [ - "arm_static_lib_excludes", - "arm_whole_static_lib_excludes", - ], - }, - }, - product_variables: { - malloc_not_svelte: { - shared_libs: ["malloc_not_svelte_shared_lib"], - whole_static_libs: ["malloc_not_svelte_whole_static_lib"], - exclude_static_libs: [ - "malloc_not_svelte_static_lib_excludes", - "malloc_not_svelte_whole_static_lib_excludes", - ], - }, - }, - include_build_directory: false, -} - -cc_library { - name: "arm_whole_static_lib_excludes", -} - -cc_library { - name: "malloc_not_svelte_whole_static_lib", -} - -cc_library { - name: "malloc_not_svelte_whole_static_lib_excludes", -} - -cc_library { - name: "arm_static_lib_excludes", -} - -cc_library { - name: "malloc_not_svelte_static_lib_excludes", -} - -cc_library { - name: "arm_shared_lib_excludes", -} - -cc_library { - name: "malloc_not_svelte_shared_lib", -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{ - "implementation_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [], - "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte": [], - "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"], - })`, - "implementation_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [], - "//conditions:default": [":arm_shared_lib_excludes"], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_shared_lib"], - "//conditions:default": [], - })`, - "srcs_c": `["common.c"]`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [], - "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"], - }) + select({ - "//build/bazel/product_config/config_settings: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 TestCcLibraryProductVariablesHeaderLibs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"malloc_not_svelte_header_lib"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library { - name: "foo_static", - srcs: ["common.c"], - product_variables: { - malloc_not_svelte: { - header_libs: ["malloc_not_svelte_header_lib"], - }, - }, - include_build_directory: false, -} - -cc_library { - name: "malloc_not_svelte_header_lib", -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{ - "implementation_deps": `select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_header_lib"], - "//conditions:default": [], - })`, - "srcs_c": `["common.c"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }), - }, - ) -} - -func TestCCLibraryNoCrtTrue(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library - nocrt: true disables feature", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - nocrt: true, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "features": `["-link_crt"]`, - "srcs": `["impl.cpp"]`, - }), - }, - ) -} - -func TestCCLibraryNoCrtFalse(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library - nocrt: false - does not emit attribute", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - nocrt: false, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "srcs": `["impl.cpp"]`, - }), - }) -} - -func TestCCLibraryNoCrtArchVariant(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library - nocrt in select", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - arch: { - arm: { - nocrt: true, - }, - x86: { - nocrt: false, - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["-link_crt"], - "//conditions:default": [], - })`, - "srcs": `["impl.cpp"]`, - }), - }) -} - -func TestCCLibraryNoLibCrtTrue(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - no_libcrt: true, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "features": `["-use_libcrt"]`, - "srcs": `["impl.cpp"]`, - }), - }) -} - -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, - "inject_bssl_hash": true, - "stubs_symbol_file": true, - "use_version_lib": 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) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - no_libcrt: false, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "srcs": `["impl.cpp"]`, - }), - }) -} - -func TestCCLibraryNoLibCrtArchVariant(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - arch: { - arm: { - no_libcrt: true, - }, - x86: { - no_libcrt: true, - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "srcs": `["impl.cpp"]`, - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["-use_libcrt"], - "//build/bazel_common_rules/platforms/arch:x86": ["-use_libcrt"], - "//conditions:default": [], - })`, - }), - }) -} - -func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - arch: { - arm: { - no_libcrt: true, - }, - x86: { - no_libcrt: true, - }, - }, - target: { - darwin: { - no_libcrt: true, - } - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["-use_libcrt"], - "//build/bazel_common_rules/platforms/arch:x86": ["-use_libcrt"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:darwin": ["-use_libcrt"], - "//conditions:default": [], - })`, - "srcs": `["impl.cpp"]`, - }), - }) -} - -func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["impl.cpp"], - arch: { - arm: { - no_libcrt: true, - }, - // This is expected to override the value for darwin_x86_64. - x86_64: { - no_libcrt: true, - }, - }, - target: { - darwin: { - no_libcrt: false, - } - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "srcs": `["impl.cpp"]`, - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["-use_libcrt"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["-use_libcrt"], - "//conditions:default": [], - })`, - }), - }) -} - -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", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "nothing", - include_build_directory: false, -} -cc_library { - name: "keep_symbols", - strip: { - keep_symbols: true, - }, - include_build_directory: false, -} -cc_library { - name: "keep_symbols_and_debug_frame", - strip: { - keep_symbols_and_debug_frame: true, - }, - include_build_directory: false, -} -cc_library { - name: "none", - strip: { - none: true, - }, - include_build_directory: false, -} -cc_library { - name: "keep_symbols_list", - strip: { - keep_symbols_list: ["symbol"], - }, - include_build_directory: false, -} -cc_library { - name: "all", - strip: { - all: true, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: expectedTargets, - }) -} - -func TestCcLibraryStripWithArch(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library strip args", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "multi-arch", - target: { - darwin: { - strip: { - keep_symbols_list: ["foo", "bar"] - } - }, - }, - arch: { - arm: { - strip: { - keep_symbols_and_debug_frame: true, - }, - }, - arm64: { - strip: { - keep_symbols: true, - }, - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{ - "strip": `{ - "keep_symbols": select({ - "//build/bazel_common_rules/platforms/arch:arm64": True, - "//conditions:default": None, - }), - "keep_symbols_and_debug_frame": select({ - "//build/bazel_common_rules/platforms/arch:arm": True, - "//conditions:default": None, - }), - "keep_symbols_list": select({ - "//build/bazel_common_rules/platforms/os:darwin": [ - "foo", - "bar", - ], - "//conditions:default": [], - }), - }`, - }), - }, - ) -} - -func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_libs empty at root", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "root_empty", - system_shared_libs: [], - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - ) -} - -func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_libs empty for static variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "static_empty", - static: { - system_shared_libs: [], - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{ - "system_dynamic_deps": "[]", - }), - MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}), - }, - }) -} - -func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_libs empty for shared variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "shared_empty", - shared: { - system_shared_libs: [], - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{ - "system_dynamic_deps": "[]", - }), - }, - }) -} - -func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_libs empty for shared, bionic variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "shared_empty", - target: { - bionic: { - shared: { - system_shared_libs: [], - } - } - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{ - "system_dynamic_deps": "[]", - }), - }, - }) -} - -func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) { - // Note that this behavior is technically incorrect (it's a simplification). - // The correct behavior would be if bp2build wrote `system_dynamic_deps = []` - // only for linux_bionic, but `android` had `["libc", "libdl", "libm"]. - // b/195791252 tracks the fix. - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_libs empty for linux_bionic variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library { - name: "target_linux_bionic_empty", - target: { - linux_bionic: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{ - "system_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"], - "//conditions:default": [], - })`, - }), - }, - ) -} - -func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_libs empty for bionic variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library { - name: "target_bionic_empty", - target: { - bionic: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{ - "system_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"], - "//conditions:default": [], - })`, - }), - }, - ) -} - -func TestCcLibrary_SystemSharedLibsMuslEmpty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_lib empty for musl variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library { - name: "target_musl_empty", - target: { - musl: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("target_musl_empty", AttrNameToString{ - "system_dynamic_deps": `[]`, - }), - }) -} - -func TestCcLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_lib empty for linux_musl variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library { - name: "target_linux_musl_empty", - target: { - linux_musl: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("target_linux_musl_empty", AttrNameToString{ - "system_dynamic_deps": `[]`, - }), - }) -} -func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library system_shared_libs set for shared and root", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"libc", "libm"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "libc", -} -cc_library { - name: "libm", -} - -cc_library { - name: "foo", - system_shared_libs: ["libc"], - shared: { - system_shared_libs: ["libm"], - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - 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", - ]`, - }), - }, - }) -} - -func TestCcLibraryOsSelects(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library - selects for all os targets", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo-lib", - srcs: ["base.cpp"], - target: { - android: { - srcs: ["android.cpp"], - }, - linux: { - srcs: ["linux.cpp"], - }, - linux_glibc: { - srcs: ["linux_glibc.cpp"], - }, - darwin: { - srcs: ["darwin.cpp"], - }, - bionic: { - srcs: ["bionic.cpp"], - }, - linux_musl: { - srcs: ["linux_musl.cpp"], - }, - windows: { - srcs: ["windows.cpp"], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{ - "srcs": `["base.cpp"] + select({ - "//build/bazel_common_rules/platforms/os:android": [ - "linux.cpp", - "bionic.cpp", - "android.cpp", - ], - "//build/bazel_common_rules/platforms/os:darwin": ["darwin.cpp"], - "//build/bazel_common_rules/platforms/os:linux_bionic": [ - "linux.cpp", - "bionic.cpp", - ], - "//build/bazel_common_rules/platforms/os:linux_glibc": [ - "linux.cpp", - "linux_glibc.cpp", - ], - "//build/bazel_common_rules/platforms/os:linux_musl": [ - "linux.cpp", - "linux_musl.cpp", - ], - "//build/bazel_common_rules/platforms/os:windows": ["windows.cpp"], - "//conditions:default": [], - })`, - }), - }, - ) -} - -func TestLibcryptoHashInjection(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library - libcrypto hash injection", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{}, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "libcrypto", - target: { - android: { - inject_bssl_hash: true, - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{ - "inject_bssl_hash": `select({ - "//build/bazel_common_rules/platforms/os:android": True, - "//conditions:default": None, - })`, - }), - }, - ) -} - -func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) { - type testCase struct { - cpp_std string - c_std string - gnu_extensions string - bazel_cpp_std string - bazel_c_std string - } - - testCases := []testCase{ - // Existing usages of cpp_std in AOSP are: - // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17 - // - // not set, only emit if gnu_extensions is disabled. the default (gnu+17 - // is set in the toolchain.) - {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""}, - {cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""}, - // experimental defaults to gnu++2a - {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"}, - {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"}, - // Explicitly setting a c++ std does not use replace gnu++ std even if - // gnu_extensions is true. - // "c++11", - {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"}, - {cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"}, - // "c++17", - {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"}, - {cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"}, - // "c++2a", - {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"}, - {cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"}, - // "c++98", - {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"}, - {cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"}, - // gnu++ is replaced with c++ if gnu_extensions is explicitly false. - // "gnu++11", - {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"}, - {cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"}, - // "gnu++17", - {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"}, - {cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"}, - {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"}, - - // some c_std test cases - {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"}, - {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"}, - {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"}, - {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"}, - {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"}, - {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"}, - } - for i, tc := range testCases { - name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions) - t.Run(name, func(t *testing.T) { - name_prefix := fmt.Sprintf("a_%v", i) - cppStdProp := "" - if tc.cpp_std != "" { - cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std) - } - cStdProp := "" - if tc.c_std != "" { - cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std) - } - gnuExtensionsProp := "" - if tc.gnu_extensions != "" { - gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions) - } - attrs := AttrNameToString{} - if tc.bazel_cpp_std != "" { - attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std) - } - if tc.bazel_c_std != "" { - attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std) - } - - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: fmt.Sprintf( - "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions), - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + fmt.Sprintf(` -cc_library { - name: "%s_full", -%s // cpp_std: *string -%s // c_std: *string -%s // gnu_extensions: *bool - include_build_directory: false, -} -`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp), - ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs), - }) - - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: fmt.Sprintf( - "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions), - ModuleTypeUnderTest: "cc_library_static", - ModuleTypeUnderTestFactory: cc.LibraryStaticFactory, - Blueprint: soongCcLibraryPreamble + fmt.Sprintf(` -cc_library_static { - name: "%s_static", -%s // cpp_std: *string -%s // c_std: *string -%s // gnu_extensions: *bool - include_build_directory: false, -} -`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs), - }, - }) - - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: fmt.Sprintf( - "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions), - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - Blueprint: soongCcLibraryPreamble + fmt.Sprintf(` -cc_library_shared { - name: "%s_shared", -%s // cpp_std: *string -%s // c_std: *string -%s // gnu_extensions: *bool - include_build_directory: false, -} -`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs), - }, - }) - }) - } -} - -func TestCcLibraryProtoSimple(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.proto"], - 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"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - }, - }) -} - -func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - 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"]`, - "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"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - }, - }) -} - -func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - 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"]`, - }), 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"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - }, - }) -} - -func TestCcLibraryProtoFull(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.proto"], - proto: { - 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"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto"]`, - }), - }, - }) -} - -func TestCcLibraryProtoLite(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.proto"], - proto: { - 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"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - }, - }) -} - -func TestCcLibraryProtoExportHeaders(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.proto"], - proto: { - 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"]`, - }), - }, - }) -} - -func TestCcLibraryProtoIncludeDirs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.proto"], - proto: { - include_dirs: ["external/protobuf/src"], - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["foo.proto"]`, - "deps": `["//external/protobuf:libprotobuf-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"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - }, - }) -} - -func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.proto"], - proto: { - include_dirs: ["external/protobuf/abc"], - }, - include_build_directory: false, -}`, - ExpectedErr: fmt.Errorf("module \"foo\": TODO: Add support for proto.include_dir: external/protobuf/abc. This directory does not contain an Android.bp file"), - }) -} - -func TestCcLibraryConvertedProtoFilegroups(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + ` -filegroup { - name: "a_fg_proto", - srcs: ["a_fg.proto"], -} - -cc_library { - name: "a", - srcs: [ - ":a_fg_proto", - "a.proto", - ], - proto: { - export_proto_headers: true, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("proto_library", "a_proto", AttrNameToString{ - "deps": `[":a_fg_proto_bp2build_converted"]`, - "srcs": `["a.proto"]`, - }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{ - "deps": `[ - ":a_fg_proto_bp2build_converted", - ":a_proto", - ]`, - }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{ - "deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{ - "srcs": `["a_fg.proto"]`, - "tags": `[ - "apex_available=//apex_available:anyapex", - "manual", - ]`, - }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{ - "actual": `"//.:a_fg_proto_proto"`, - "tags": `[ - "apex_available=//apex_available:anyapex", - "manual", - ]`, - }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{ - "srcs": `["a_fg.proto"]`, - }), - }, - }) -} - -func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + ` -filegroup { - name: "a_fg_proto", - srcs: ["a_fg.proto"], -} - -cc_library { - name: "a", - srcs: [ - ":a_fg_proto", - ], - proto: { - export_proto_headers: true, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{ - "deps": `[":a_fg_proto_bp2build_converted"]`, - }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{ - "deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{ - "srcs": `["a_fg.proto"]`, - "tags": `[ - "apex_available=//apex_available:anyapex", - "manual", - ]`, - }), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{ - "actual": `"//.:a_fg_proto_proto"`, - "tags": `[ - "apex_available=//apex_available:anyapex", - "manual", - ]`, - }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{ - "srcs": `["a_fg.proto"]`, - }), - }, - }) -} - -func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"//path/to/A:a_fg_proto"}, - Filesystem: map[string]string{ - "path/to/A/Android.bp": ` -filegroup { - name: "a_fg_proto", - srcs: ["a_fg.proto"], -}`, - }, - Blueprint: soongCcProtoPreamble + ` -cc_library { - name: "a", - srcs: [ - ":a_fg_proto", - "a.proto", - ], - proto: { - export_proto_headers: true, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("proto_library", "a_proto", AttrNameToString{ - "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`, - "srcs": `["a.proto"]`, - }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{ - "deps": `[ - "//path/to/A:a_fg_proto_bp2build_converted", - ":a_proto", - ]`, - }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{ - "deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - }), - }, - }) -} - -func TestCcLibraryProtoFilegroups(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"a_fg_proto", "b_protos", "c-proto-srcs", "proto-srcs-d"}, - Blueprint: soongCcProtoPreamble + - simpleModule("filegroup", "a_fg_proto") + - simpleModule("filegroup", "b_protos") + - simpleModule("filegroup", "c-proto-srcs") + - simpleModule("filegroup", "proto-srcs-d") + ` -cc_library { - name: "a", - srcs: [":a_fg_proto"], - proto: { - export_proto_headers: true, - }, - include_build_directory: false, -} - -cc_library { - name: "b", - srcs: [":b_protos"], - proto: { - export_proto_headers: true, - }, - include_build_directory: false, -} - -cc_library { - name: "c", - srcs: [":c-proto-srcs"], - proto: { - export_proto_headers: true, - }, - include_build_directory: false, -} - -cc_library { - name: "d", - srcs: [":proto-srcs-d"], - proto: { - export_proto_headers: true, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("proto_library", "a_proto", AttrNameToString{ - "srcs": `[":a_fg_proto"]`, - }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{ - "deps": `[":a_proto"]`, - }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{ - "deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - "srcs": `[":a_fg_proto_cpp_srcs"]`, - "srcs_as": `[":a_fg_proto_as_srcs"]`, - "srcs_c": `[":a_fg_proto_c_srcs"]`, - }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":a_cc_proto_lite"]`, - "srcs": `[":a_fg_proto_cpp_srcs"]`, - "srcs_as": `[":a_fg_proto_as_srcs"]`, - "srcs_c": `[":a_fg_proto_c_srcs"]`, - }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{ - "srcs": `[":b_protos"]`, - }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{ - "deps": `[":b_proto"]`, - }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{ - "deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":b_cc_proto_lite"]`, - "srcs": `[":b_protos_cpp_srcs"]`, - "srcs_as": `[":b_protos_as_srcs"]`, - "srcs_c": `[":b_protos_c_srcs"]`, - }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":b_cc_proto_lite"]`, - "srcs": `[":b_protos_cpp_srcs"]`, - "srcs_as": `[":b_protos_as_srcs"]`, - "srcs_c": `[":b_protos_c_srcs"]`, - }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{ - "srcs": `[":c-proto-srcs"]`, - }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{ - "deps": `[":c_proto"]`, - }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{ - "deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":c_cc_proto_lite"]`, - "srcs": `[":c-proto-srcs_cpp_srcs"]`, - "srcs_as": `[":c-proto-srcs_as_srcs"]`, - "srcs_c": `[":c-proto-srcs_c_srcs"]`, - }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":c_cc_proto_lite"]`, - "srcs": `[":c-proto-srcs_cpp_srcs"]`, - "srcs_as": `[":c-proto-srcs_as_srcs"]`, - "srcs_c": `[":c-proto-srcs_c_srcs"]`, - }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{ - "srcs": `[":proto-srcs-d"]`, - }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{ - "deps": `[":d_proto"]`, - }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{ - "deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":d_cc_proto_lite"]`, - "srcs": `[":proto-srcs-d_cpp_srcs"]`, - "srcs_as": `[":proto-srcs-d_as_srcs"]`, - "srcs_c": `[":proto-srcs-d_c_srcs"]`, - }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{ - "dynamic_deps": `[":libprotobuf-cpp-lite"]`, - "whole_archive_deps": `[":d_cc_proto_lite"]`, - "srcs": `[":proto-srcs-d_cpp_srcs"]`, - "srcs_as": `[":proto-srcs-d_as_srcs"]`, - "srcs_c": `[":proto-srcs-d_c_srcs"]`, - }), - }, - }) -} - -func TestCcLibraryDisabledArchAndTarget(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.cpp"], - host_supported: true, - target: { - darwin: { - enabled: false, - }, - windows: { - enabled: false, - }, - linux_glibc_x86: { - enabled: false, - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{ - "srcs": `["foo.cpp"]`, - "target_compatible_with": `select({ - "//build/bazel_common_rules/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/os_arch:windows_x86": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"], - "//conditions:default": [], - })`, - }), - }) -} - -func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.cpp"], - enabled: false, - host_supported: true, - target: { - darwin: { - enabled: true, - }, - windows: { - enabled: false, - }, - linux_glibc_x86: { - enabled: false, - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{ - "srcs": `["foo.cpp"]`, - "target_compatible_with": `select({ - "//build/bazel_common_rules/platforms/os_arch:darwin_arm64": [], - "//build/bazel_common_rules/platforms/os_arch:darwin_x86_64": [], - "//conditions:default": ["@platforms//:incompatible"], - })`, - }), - }) -} - -func TestCcLibrarySharedDisabled(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - srcs: ["foo.cpp"], - enabled: false, - shared: { - enabled: true, - }, - target: { - android: { - shared: { - enabled: false, - }, - } - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "srcs": `["foo.cpp"]`, - "target_compatible_with": `["@platforms//:incompatible"]`, - }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs": `["foo.cpp"]`, - "target_compatible_with": `select({ - "//build/bazel_common_rules/platforms/os:android": ["@platforms//:incompatible"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - host_supported: true, - srcs: ["foo.cpp"], - shared: { - enabled: false - }, - target: { - darwin: { - enabled: true, - }, - windows: { - enabled: false, - }, - linux_glibc_x86: { - shared: { - enabled: true, - }, - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "srcs": `["foo.cpp"]`, - "target_compatible_with": `select({ - "//build/bazel_common_rules/platforms/os:windows": ["@platforms//:incompatible"], - "//conditions:default": [], - })`, - }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs": `["foo.cpp"]`, - "target_compatible_with": `select({ - "//build/bazel_common_rules/platforms/os_arch:darwin_arm64": [], - "//build/bazel_common_rules/platforms/os_arch:darwin_x86_64": [], - "//build/bazel_common_rules/platforms/os_arch:linux_glibc_x86": [], - "//conditions:default": ["@platforms//:incompatible"], - })`, - }), - }}) -} - -func TestCcLibraryStubs(t *testing.T) { - expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{ - "stubs_symbol_file": `"a.map.txt"`, - }) - expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{ - "api_surface": `"module-libapi"`, - "soname": `"a.so"`, - "source_library_label": `"//foo/bar:a"`, - "stubs_symbol_file": `"a.map.txt"`, - "stubs_versions": `[ - "28", - "29", - "current", - ]`, - })) - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library stubs", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Dir: "foo/bar", - Filesystem: map[string]string{ - "foo/bar/Android.bp": ` -cc_library { - name: "a", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - bazel_module: { bp2build_available: true }, - include_build_directory: false, -} -`, - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: expectedBazelTargets, - }, - ) -} - -func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "stub target generation of the same lib across configs should not result in duplicates", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "bar.map.txt": "", - }, - StubbedBuildDefinitions: []string{"barlib"}, - Blueprint: ` -cc_library { - name: "barlib", - stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] }, -} -cc_library { - name: "foolib", - shared_libs: ["barlib"], - target: { - android: { - shared_libs: ["barlib"], - }, - }, - bazel_module: { bp2build_available: true }, - apex_available: ["foo"], -}`, - ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:barlib"], - "//conditions:default": [":barlib"], - })`, - "local_includes": `["."]`, - "tags": `["apex_available=foo"]`, - }), - }) -} - -func TestCcLibraryExcludesLibsHost(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "bar.map.txt": "", - }, - StubbedBuildDefinitions: []string{"bazlib", "quxlib", "barlib"}, - Blueprint: simpleModule("cc_library", "bazlib") + ` -cc_library { - name: "quxlib", - stubs: { symbol_file: "bar.map.txt", versions: ["current"] }, -} -cc_library { - name: "barlib", - stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] }, -} -cc_library { - name: "foolib", - shared_libs: ["barlib", "quxlib"], - target: { - host: { - shared_libs: ["bazlib"], - exclude_shared_libs: ["barlib"], - }, - }, - include_build_directory: false, - bazel_module: { bp2build_available: true }, - apex_available: ["foo"], -}`, - ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/os:darwin": [":bazlib"], - "//build/bazel_common_rules/platforms/os:linux_bionic": [":bazlib"], - "//build/bazel_common_rules/platforms/os:linux_glibc": [":bazlib"], - "//build/bazel_common_rules/platforms/os:linux_musl": [":bazlib"], - "//build/bazel_common_rules/platforms/os:windows": [":bazlib"], - "//conditions:default": [], - }) + select({ - "//build/bazel/rules/apex:foo": [ - "@api_surfaces//module-libapi/current:barlib", - "@api_surfaces//module-libapi/current:quxlib", - ], - "//build/bazel_common_rules/platforms/os:darwin": [":quxlib"], - "//build/bazel_common_rules/platforms/os:linux_bionic": [":quxlib"], - "//build/bazel_common_rules/platforms/os:linux_glibc": [":quxlib"], - "//build/bazel_common_rules/platforms/os:linux_musl": [":quxlib"], - "//build/bazel_common_rules/platforms/os:windows": [":quxlib"], - "//conditions:default": [ - ":barlib", - ":quxlib", - ], - })`, - "tags": `["apex_available=foo"]`, - }), - }) -} - -func TestCcLibraryEscapeLdflags(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcProtoPreamble + `cc_library { - name: "foo", - ldflags: ["-Wl,--rpath,${ORIGIN}"], - include_build_directory: false, -}`, - ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{ - "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`, - }), - }) -} - -func TestCcLibraryConvertLex(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "foo.c": "", - "bar.cc": "", - "foo1.l": "", - "bar1.ll": "", - "foo2.l": "", - "bar2.ll": "", - }, - Blueprint: `cc_library { - name: "foo_lib", - srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"], - lex: { flags: ["--foo_flags"] }, - include_build_directory: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: append([]string{ - MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{ - "srcs": `[ - "foo1.l", - "foo2.l", - ]`, - "lexopts": `["--foo_flags"]`, - }), - MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{ - "srcs": `[ - "bar1.ll", - "bar2.ll", - ]`, - "lexopts": `["--foo_flags"]`, - }), - }, - makeCcLibraryTargets("foo_lib", AttrNameToString{ - "srcs": `[ - "bar.cc", - ":foo_lib_genlex_ll", - ]`, - "srcs_c": `[ - "foo.c", - ":foo_lib_genlex_l", - ]`, - })...), - }) -} - -func TestCCLibraryRuntimeDeps(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Blueprint: `cc_library_shared { - name: "bar", -} - -cc_library { - name: "foo", - runtime_libs: ["bar"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{ - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "runtime_deps": `[":bar"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "runtime_deps": `[":bar"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithInstructionSet(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: `cc_library { - name: "foo", - arch: { - arm: { - instruction_set: "arm", - } - } -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arm_isa_arm"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }) -} - -func TestCcLibraryEmptySuffix(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with empty suffix", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "foo.c": "", - }, - Blueprint: `cc_library { - name: "foo", - suffix: "", - srcs: ["foo.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "srcs_c": `["foo.c"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs_c": `["foo.c"]`, - "suffix": `""`, - }), - }, - }) -} - -func TestCcLibrarySuffix(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with suffix", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "foo.c": "", - }, - Blueprint: `cc_library { - name: "foo", - suffix: "-suf", - srcs: ["foo.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "srcs_c": `["foo.c"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs_c": `["foo.c"]`, - "suffix": `"-suf"`, - }), - }, - }) -} - -func TestCcLibraryArchVariantSuffix(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with arch-variant suffix", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Filesystem: map[string]string{ - "foo.c": "", - }, - Blueprint: `cc_library { - name: "foo", - arch: { - arm64: { suffix: "-64" }, - arm: { suffix: "-32" }, - }, - srcs: ["foo.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "srcs_c": `["foo.c"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs_c": `["foo.c"]`, - "suffix": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "-32", - "//build/bazel_common_rules/platforms/arch:arm64": "-64", - "//conditions:default": None, - })`, - }), - }, - }) -} - -func TestCcLibraryWithAidlLibrary(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with aidl_library", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -aidl_library { - name: "A_aidl", - srcs: ["aidl/A.aidl"], - hdrs: ["aidl/Header.aidl"], - strip_import_prefix: "aidl", -} -cc_library { - name: "foo", - aidl: { - libs: ["A_aidl"], - }, - export_include_dirs: ["include"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{ - "srcs": `["aidl/A.aidl"]`, - "hdrs": `["aidl/Header.aidl"]`, - "strip_import_prefix": `"aidl"`, - "tags": `["apex_available=//apex_available:anyapex"]`, - }), - MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{ - "deps": `[":A_aidl"]`, - "local_includes": `["."]`, - "export_includes": `["include"]`, - }), - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`, - "local_includes": `["."]`, - "export_includes": `["include"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`, - "local_includes": `["."]`, - "export_includes": `["include"]`, - }), - }, - }) -} - -func TestCcLibraryWithAidlSrcs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with aidl srcs", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -filegroup { - name: "A_aidl", - srcs: ["aidl/A.aidl"], - path: "aidl", -} -cc_library { - name: "foo", - srcs: [ - ":A_aidl", - "B.aidl", - ], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{ - "srcs": `["aidl/A.aidl"]`, - "strip_import_prefix": `"aidl"`, - "tags": `["apex_available=//apex_available:anyapex"]`, - }), - MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{ - "srcs": `["B.aidl"]`, - }), - MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[ - ":A_aidl", - ":foo_aidl_library", - ]`, - }), - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with non aidl filegroup", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"//path/to/A:A_aidl"}, - Filesystem: map[string]string{ - "path/to/A/Android.bp": ` -filegroup { - name: "A_aidl", - srcs: ["aidl/A.aidl"], - path: "aidl", -}`, - }, - Blueprint: ` -cc_library { - name: "foo", - srcs: [ - ":A_aidl", - ], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{ - "local_includes": `["."]`, - "deps": `["//path/to/A:A_aidl"]`, - }), - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "local_includes": `["."]`, - "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`, - }), - }, - }) -} - -func TestCcLibraryWithExportAidlHeaders(t *testing.T) { - t.Parallel() - - expectedBazelTargets := []string{ - MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":foo_aidl_library"]`, - }), - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "whole_archive_deps": `[":foo_cc_aidl_library"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "whole_archive_deps": `[":foo_cc_aidl_library"]`, - "local_includes": `["."]`, - }), - } - testCases := []struct { - description string - bp string - expectedBazelTargets []string - }{ - { - description: "cc_library with aidl srcs and aidl.export_aidl_headers set", - bp: ` - cc_library { - name: "foo", - srcs: [ - "Foo.aidl", - ], - aidl: { - export_aidl_headers: true, - } - }`, - expectedBazelTargets: append( - expectedBazelTargets, - MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{ - "srcs": `["Foo.aidl"]`, - })), - }, - { - description: "cc_library with aidl.libs and aidl.export_aidl_headers set", - bp: ` - aidl_library { - name: "foo_aidl_library", - srcs: ["Foo.aidl"], - } - cc_library { - name: "foo", - aidl: { - libs: ["foo_aidl_library"], - export_aidl_headers: true, - } - }`, - expectedBazelTargets: append( - expectedBazelTargets, - MakeBazelTargetNoRestrictions("aidl_library", "foo_aidl_library", AttrNameToString{ - "srcs": `["Foo.aidl"]`, - "tags": `["apex_available=//apex_available:anyapex"]`, - }), - ), - }, - } - - for _, testCase := range testCases { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with export aidl headers", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: testCase.bp, - ExpectedBazelTargets: testCase.expectedBazelTargets, - }) - } -} - -func TestCcLibraryWithTargetApex(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with target.apex", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"bar", "baz", "buh"}, - Blueprint: ` -cc_library { - name: "foo", - shared_libs: ["bar", "baz"], - static_libs: ["baz", "buh"], - target: { - apex: { - exclude_shared_libs: ["bar"], - exclude_static_libs: ["buh"], - } - } -}` + simpleModule("cc_library_static", "baz") + - simpleModule("cc_library_static", "buh") + - simpleModule("cc_library_static", "bar"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "implementation_deps": `[":baz"] + select({ - "//build/bazel/rules/apex:in_apex": [], - "//conditions:default": [":buh"], - })`, - "implementation_dynamic_deps": `[":baz"] + select({ - "//build/bazel/rules/apex:in_apex": [], - "//conditions:default": [":bar"], - })`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "implementation_deps": `[":baz"] + select({ - "//build/bazel/rules/apex:in_apex": [], - "//conditions:default": [":buh"], - })`, - "implementation_dynamic_deps": `[":baz"] + select({ - "//build/bazel/rules/apex:in_apex": [], - "//conditions:default": [":bar"], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with target.apex and export_shared|static_lib_headers", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library_static { - name: "foo", - shared_libs: ["bar", "baz"], - static_libs: ["abc"], - export_shared_lib_headers: ["baz"], - export_static_lib_headers: ["abc"], - target: { - apex: { - exclude_shared_libs: ["baz", "bar"], - exclude_static_libs: ["abc"], - } - } -}` + simpleModule("cc_library_static", "bar") + - simpleModule("cc_library_static", "baz") + - simpleModule("cc_library_static", "abc"), - StubbedBuildDefinitions: []string{"bar", "baz", "abc"}, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel/rules/apex:in_apex": [], - "//conditions:default": [":bar"], - })`, - "dynamic_deps": `select({ - "//build/bazel/rules/apex:in_apex": [], - "//conditions:default": [":baz"], - })`, - "deps": `select({ - "//build/bazel/rules/apex:in_apex": [], - "//conditions:default": [":abc"], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithSyspropSrcs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with sysprop sources", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - srcs: [ - "bar.sysprop", - "baz.sysprop", - "blah.cpp", - ], - min_sdk_version: "5", -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `[ - "bar.sysprop", - "baz.sysprop", - ]`, - }), - MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }), - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`, - }), - }, - }) -} - -func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with sysprop sources in some configs but not others", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - host_supported: true, - srcs: [ - "blah.cpp", - ], - target: { - android: { - srcs: ["bar.sysprop"], - }, - }, - min_sdk_version: "5", -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `select({ - "//build/bazel_common_rules/platforms/os:android": ["bar.sysprop"], - "//conditions:default": [], - })`, - }), - MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }), - MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":foo_cc_sysprop_library_static"], - "//conditions:default": [], - })`, - }), - MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":foo_cc_sysprop_library_static"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryWithAidlAndLibs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_aidl_library depends on libs from parent cc_library_static", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"bar-static", "baz-static", "bar-shared", "baz-shared"}, - Blueprint: ` -cc_library_static { - name: "foo", - srcs: [ - "Foo.aidl", - ], - static_libs: [ - "bar-static", - "baz-static", - ], - shared_libs: [ - "bar-shared", - "baz-shared", - ], - export_static_lib_headers: [ - "baz-static", - ], - export_shared_lib_headers: [ - "baz-shared", - ], -}` + - simpleModule("cc_library_static", "bar-static") + - simpleModule("cc_library_static", "baz-static") + - simpleModule("cc_library", "bar-shared") + - simpleModule("cc_library", "baz-shared"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{ - "srcs": `["Foo.aidl"]`, - }), - MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":foo_aidl_library"]`, - "implementation_deps": `[ - ":baz-static", - ":bar-static", - ]`, - "implementation_dynamic_deps": `[ - ":baz-shared", - ":bar-shared", - ]`, - }), - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`, - "deps": `[":baz-static"]`, - "implementation_deps": `[":bar-static"]`, - "dynamic_deps": `[":baz-shared"]`, - "implementation_dynamic_deps": `[":bar-shared"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithTidy(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library uses tidy properties", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library_static { - name: "foo", - srcs: ["foo.cpp"], -} -cc_library_static { - name: "foo-no-tidy", - srcs: ["foo.cpp"], - tidy: false, -} -cc_library_static { - name: "foo-tidy", - srcs: ["foo.cpp"], - tidy: true, - tidy_checks: ["check1", "check2"], - tidy_checks_as_errors: ["check1error", "check2error"], - tidy_disabled_srcs: ["bar.cpp"], - tidy_timeout_srcs: ["baz.cpp"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["foo.cpp"]`, - }), - MakeBazelTarget("cc_library_static", "foo-no-tidy", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["foo.cpp"]`, - "tidy": `"never"`, - }), - MakeBazelTarget("cc_library_static", "foo-tidy", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["foo.cpp"]`, - "tidy": `"local"`, - "tidy_checks": `[ - "check1", - "check2", - ]`, - "tidy_checks_as_errors": `[ - "check1error", - "check2error", - ]`, - "tidy_disabled_srcs": `["bar.cpp"]`, - "tidy_timeout_srcs": `["baz.cpp"]`, - }), - }, - }) -} - -func TestCcLibraryWithAfdoEnabled(t *testing.T) { - bp := ` -cc_library { - name: "foo", - afdo: true, - include_build_directory: false, -}` - - // TODO(b/260714900): Add test case for arch-specific afdo profile - testCases := []struct { - description string - filesystem map[string]string - expectedBazelTargets []string - }{ - { - description: "cc_library with afdo enabled and existing profile", - filesystem: map[string]string{ - "vendor/google_data/pgo_profile/sampling/Android.bp": "", - "vendor/google_data/pgo_profile/sampling/foo.afdo": "", - }, - expectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`, - }), - }, - }, - { - description: "cc_library with afdo enabled and existing profile in AOSP", - filesystem: map[string]string{ - "toolchain/pgo-profiles/sampling/Android.bp": "", - "toolchain/pgo-profiles/sampling/foo.afdo": "", - }, - expectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`, - }), - }, - }, - { - description: "cc_library with afdo enabled but profile filename doesn't match with module name", - filesystem: map[string]string{ - "toolchain/pgo-profiles/sampling/Android.bp": "", - "toolchain/pgo-profiles/sampling/bar.afdo": "", - }, - expectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}), - }, - }, - { - description: "cc_library with afdo enabled but profile doesn't exist", - expectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}), - }, - }, - { - description: "cc_library with afdo enabled and existing profile but BUILD file doesn't exist", - filesystem: map[string]string{ - "vendor/google_data/pgo_profile/sampling/foo.afdo": "", - }, - expectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}), - }, - }, - } - for _, testCase := range testCases { - t.Run(testCase.description, func(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - ExpectedBazelTargets: testCase.expectedBazelTargets, - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Description: testCase.description, - Blueprint: binaryReplacer.Replace(bp), - Filesystem: testCase.filesystem, - }) - }) - } -} - -func TestCcLibraryHeaderAbiChecker(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with header abi checker", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: `cc_library { - name: "foo", - header_abi_checker: { - enabled: true, - symbol_file: "a.map.txt", - exclude_symbol_versions: [ - "29", - "30", - ], - exclude_symbol_tags: [ - "tag1", - "tag2", - ], - check_all_apis: true, - diff_flags: ["-allow-adding-removing-weak-symbols"], - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "abi_checker_enabled": `True`, - "abi_checker_symbol_file": `"a.map.txt"`, - "abi_checker_exclude_symbol_versions": `[ - "29", - "30", - ]`, - "abi_checker_exclude_symbol_tags": `[ - "tag1", - "tag2", - ]`, - "abi_checker_check_all_apis": `True`, - "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`, - }), - }, - }) -} - -func TestCcLibraryApexAvailable(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library apex_available converted to tags", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "a", - srcs: ["a.cpp"], - apex_available: ["com.android.foo"], -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "tags": `["apex_available=com.android.foo"]`, - "srcs": `["a.cpp"]`, - "local_includes": `["."]`, - }), - }, - ) -} - -func TestCcLibraryApexAvailableMultiple(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library apex_available converted to multiple tags", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "a", - srcs: ["a.cpp"], - apex_available: ["com.android.foo", "//apex_available:platform", "com.android.bar"], -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "tags": `[ - "apex_available=com.android.foo", - "apex_available=//apex_available:platform", - "apex_available=com.android.bar", - ]`, - "srcs": `["a.cpp"]`, - "local_includes": `["."]`, - }), - }, - ) -} - -// Export_include_dirs and Export_system_include_dirs have "variant_prepend" tag. -// In bp2build output, variant info(select) should go before general info. -// Internal order of the property should be unchanged. (e.g. ["eid1", "eid2"]) -func TestCcLibraryVariantPrependPropOrder(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library variant prepend properties order", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "a", - srcs: ["a.cpp"], - export_include_dirs: ["eid1", "eid2"], - export_system_include_dirs: ["esid1", "esid2"], - target: { - android: { - export_include_dirs: ["android_eid1", "android_eid2"], - export_system_include_dirs: ["android_esid1", "android_esid2"], - }, - android_arm: { - export_include_dirs: ["android_arm_eid1", "android_arm_eid2"], - export_system_include_dirs: ["android_arm_esid1", "android_arm_esid2"], - }, - linux: { - export_include_dirs: ["linux_eid1", "linux_eid2"], - export_system_include_dirs: ["linux_esid1", "linux_esid2"], - }, - }, - multilib: { - lib32: { - export_include_dirs: ["lib32_eid1", "lib32_eid2"], - export_system_include_dirs: ["lib32_esid1", "lib32_esid2"], - }, - }, - arch: { - arm: { - export_include_dirs: ["arm_eid1", "arm_eid2"], - export_system_include_dirs: ["arm_esid1", "arm_esid2"], - }, - } -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "export_includes": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": [ - "android_arm_eid1", - "android_arm_eid2", - ], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [ - "android_eid1", - "android_eid2", - "linux_eid1", - "linux_eid2", - ], - "//build/bazel_common_rules/platforms/os:linux_bionic": [ - "linux_eid1", - "linux_eid2", - ], - "//build/bazel_common_rules/platforms/os:linux_glibc": [ - "linux_eid1", - "linux_eid2", - ], - "//build/bazel_common_rules/platforms/os:linux_musl": [ - "linux_eid1", - "linux_eid2", - ], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "lib32_eid1", - "lib32_eid2", - "arm_eid1", - "arm_eid2", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - "lib32_eid1", - "lib32_eid2", - ], - "//conditions:default": [], - }) + [ - "eid1", - "eid2", - ]`, - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": [ - "android_arm_esid1", - "android_arm_esid2", - ], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [ - "android_esid1", - "android_esid2", - "linux_esid1", - "linux_esid2", - ], - "//build/bazel_common_rules/platforms/os:linux_bionic": [ - "linux_esid1", - "linux_esid2", - ], - "//build/bazel_common_rules/platforms/os:linux_glibc": [ - "linux_esid1", - "linux_esid2", - ], - "//build/bazel_common_rules/platforms/os:linux_musl": [ - "linux_esid1", - "linux_esid2", - ], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "lib32_esid1", - "lib32_esid2", - "arm_esid1", - "arm_esid2", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - "lib32_esid1", - "lib32_esid2", - ], - "//conditions:default": [], - }) + [ - "esid1", - "esid2", - ]`, - "srcs": `["a.cpp"]`, - "local_includes": `["."]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - }), - }, - ) -} - -func TestCcLibraryWithIntegerOverflowProperty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when integer_overflow property is provided", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - sanitize: { - integer_overflow: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["ubsan_integer_overflow"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["ubsan_integer_overflow"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithMiscUndefinedProperty(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when misc_undefined property is provided", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithSanitizerBlocklist(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct feature when sanitize.blocklist is provided", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - sanitize: { - blocklist: "foo_blocklist.txt", - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "copts": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"], - "//conditions:default": [], - })`, - "additional_compiler_inputs": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "copts": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"], - "//conditions:default": [], - })`, - "additional_compiler_inputs": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithUBSanPropertiesArchSpecific(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct feature select when UBSan props are specified in arch specific blocks", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, - target: { - android: { - sanitize: { - misc_undefined: ["alignment"], - }, - }, - linux_glibc: { - sanitize: { - integer_overflow: true, - }, - }, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ] + select({ - "//build/bazel_common_rules/platforms/os:android": ["ubsan_alignment"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["ubsan_integer_overflow"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ] + select({ - "//build/bazel_common_rules/platforms/os:android": ["ubsan_alignment"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["ubsan_integer_overflow"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"barlib", "bazlib"}, - Blueprint: ` -cc_library { - name: "barlib", - stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["//apex_available:platform",], -} -cc_library { - name: "bazlib", - stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["//apex_available:platform",], -} -cc_library { - name: "foo", - shared_libs: ["barlib", "bazlib"], - export_shared_lib_headers: ["bazlib"], - apex_available: [ - "//apex_available:platform", - ], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "implementation_dynamic_deps": `[":barlib"]`, - "dynamic_deps": `[":bazlib"]`, - "local_includes": `["."]`, - "tags": `["apex_available=//apex_available:platform"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "implementation_dynamic_deps": `[":barlib"]`, - "dynamic_deps": `[":bazlib"]`, - "local_includes": `["."]`, - "tags": `["apex_available=//apex_available:platform"]`, - }), - }, - }) -} - -func TestCcLibraryWithThinLto(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when thin LTO is enabled", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - lto: { - thin: true, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["android_thin_lto"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["android_thin_lto"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithLtoNever(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when LTO is explicitly disabled", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - lto: { - never: true, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["-android_thin_lto"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["-android_thin_lto"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithThinLtoArchSpecific(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when LTO differs across arch and os variants", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - target: { - android: { - lto: { - thin: true, - }, - }, - }, - arch: { - riscv64: { - lto: { - thin: false, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_riscv64": ["-android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_thin_lto"], - "//conditions:default": [], - })`}), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_riscv64": ["-android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_thin_lto"], - "//conditions:default": [], - })`}), - }, - }) -} - -func TestCcLibraryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when LTO disabled by default but enabled on a particular variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - lto: { - never: true, - }, - target: { - android: { - lto: { - thin: true, - never: false, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_thin_lto"], - "//conditions:default": ["-android_thin_lto"], - })`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_thin_lto"], - "//conditions:default": ["-android_thin_lto"], - })`, - }), - }, - }) -} - -func TestCcLibraryWithThinLtoWholeProgramVtables(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when thin LTO is enabled with whole_program_vtables", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - lto: { - thin: true, - }, - whole_program_vtables: true, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `[ - "android_thin_lto", - "android_thin_lto_whole_program_vtables", - ]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `[ - "android_thin_lto", - "android_thin_lto_whole_program_vtables", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryHiddenVisibilityConvertedToFeature(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library changes hidden visibility flag to feature", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - cflags: ["-fvisibility=hidden"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["visibility_hidden"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["visibility_hidden"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryHiddenVisibilityConvertedToFeatureSharedSpecific(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library changes hidden visibility flag to feature when specific to shared variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - shared: { - cflags: ["-fvisibility=hidden"], - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["visibility_hidden"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryHiddenVisibilityConvertedToFeatureStaticSpecific(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library changes hidden visibility flag to feature when specific to static variant", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - static: { - cflags: ["-fvisibility=hidden"], - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["visibility_hidden"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library changes hidden visibility flag to feature when specific to an os", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - target: { - android: { - cflags: ["-fvisibility=hidden"], - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["visibility_hidden"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["visibility_hidden"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -// Test that a config_setting specific to an apex is created by cc_library. -func TestCcLibraryCreatesInApexConfigSetting(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library creates a config_setting for each apex in apex_available", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Dir: "build/bazel/rules/apex", - Blueprint: ` -cc_library { - name: "foo", - apex_available: [ - "//apex_available:platform", // This will be skipped, since it is equivalent to //build/bazel/rules/apex:android-non_apex - "myapex" - ], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions( - "config_setting", - "myapex", - AttrNameToString{ - "flag_values": `{ - "//build/bazel/rules/apex:api_domain": "myapex", - }`, - "constraint_values": `["//build/bazel_common_rules/platforms/os:android"]`, - }, - ), - }, - }) -} - -func TestCcLibraryCppFlagsInProductVariables(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library cppflags in product variables", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: soongCcLibraryPreamble + `cc_library { - name: "a", - srcs: ["a.cpp"], - cppflags: [ - "-Wextra", - "-DDEBUG_ONLY_CODE=0", - ], - product_variables: { - eng: { - cppflags: [ - "-UDEBUG_ONLY_CODE", - "-DDEBUG_ONLY_CODE=1", - ], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{ - "cppflags": `[ - "-Wextra", - "-DDEBUG_ONLY_CODE=0", - ] + select({ - "//build/bazel/product_config/config_settings:eng": [ - "-UDEBUG_ONLY_CODE", - "-DDEBUG_ONLY_CODE=1", - ], - "//conditions:default": [], - })`, - "srcs": `["a.cpp"]`, - }), - }, - ) -} - -func TestCcLibraryYaccConversion(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library is built from .y/.yy files", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - StubbedBuildDefinitions: []string{"staticlib", "sharedlib"}, - Blueprint: soongCcLibraryPreamble + `cc_library { - name: "a", - srcs: [ - "a.cpp", - "a.yy", - ], - shared_libs: ["sharedlib"], - static_libs: ["staticlib"], - yacc: { - flags: ["someYaccFlag"], - gen_location_hh: true, - gen_position_hh: true, - }, -} -cc_library_static { - name: "staticlib", -} -cc_library { - name: "sharedlib", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{ - "src": `"a.yy"`, - "implementation_deps": `[":staticlib"]`, - "implementation_dynamic_deps": `[":sharedlib"]`, - "flags": `["someYaccFlag"]`, - "gen_location_hh": "True", - "gen_position_hh": "True", - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "srcs": `["a.cpp"]`, - "implementation_deps": `[":staticlib"]`, - "implementation_dynamic_deps": `[":sharedlib"]`, - "implementation_whole_archive_deps": `[":a_yacc"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{ - "srcs": `["a.cpp"]`, - "implementation_deps": `[":staticlib"]`, - "implementation_dynamic_deps": `[":sharedlib"]`, - "implementation_whole_archive_deps": `[":a_yacc"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryHostLdLibs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_binary linker flags for host_ldlibs", - ModuleTypeUnderTest: "cc_binary", - ModuleTypeUnderTestFactory: cc.BinaryFactory, - Blueprint: soongCcLibraryPreamble + `cc_binary { - name: "a", - host_supported: true, - ldflags: ["-lcommon"], - target: { - linux: { - host_ldlibs: [ - "-llinux", - ], - }, - darwin: { - ldflags: ["-ldarwinadditional"], - host_ldlibs: [ - "-ldarwin", - ], - }, - windows: { - host_ldlibs: [ - "-lwindows", - ], - }, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("cc_binary", "a", AttrNameToString{ - "linkopts": `["-lcommon"] + select({ - "//build/bazel_common_rules/platforms/os:darwin": [ - "-ldarwinadditional", - "-ldarwin", - ], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["-llinux"], - "//build/bazel_common_rules/platforms/os:windows": ["-lwindows"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithCfi(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when cfi is enabled", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - sanitize: { - cfi: true, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["android_cfi"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["android_cfi"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithCfiOsSpecific(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when cfi is enabled for specific variants", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - target: { - android: { - sanitize: { - cfi: true, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_cfi"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_cfi"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithCfiAndCfiAssemblySupport(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library has correct features when cfi is enabled with cfi_assembly_support", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - sanitize: { - cfi: true, - config: { - cfi_assembly_support: true, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `[ - "android_cfi", - "android_cfi_assembly_support", - ]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `[ - "android_cfi", - "android_cfi_assembly_support", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryExplicitlyDisablesCfiWhenFalse(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library disables cfi when explciitly set to false in the bp", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "foo", - sanitize: { - cfi: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{ - "features": `["-android_cfi"]`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["-android_cfi"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryWithStem(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library with stem property", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - Blueprint: soongCcLibraryPreamble + ` -cc_library_shared { - name: "foo_with_stem_simple", - stem: "foo", -} -cc_library_shared { - name: "foo_with_arch_variant_stem", - arch: { - arm: { - stem: "foo-arm", - }, - arm64: { - stem: "foo-arm64", - }, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_with_stem_simple", AttrNameToString{ - "stem": `"foo"`, - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo_with_arch_variant_stem", AttrNameToString{ - "stem": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "foo-arm", - "//build/bazel_common_rules/platforms/arch:arm64": "foo-arm64", - "//conditions:default": None, - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -// Bazel enforces that proto_library and the .proto file are in the same bazel package -func TestGenerateProtoLibraryInSamePackage(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "cc_library depends on .proto files from multiple packages", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library_static { - name: "foo", - srcs: [ - "foo.proto", - "bar/bar.proto", // Different package because there is a bar/Android.bp - "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp - ], - proto: { - canonical_path_from_root: true, - } -} -` + simpleModule("cc_library", "libprotobuf-cpp-lite"), - Filesystem: map[string]string{ - "bar/Android.bp": "", - "baz/subbaz/Android.bp": "", - }, - } - - // We will run the test 3 times and check in the root, bar and baz/subbaz directories - // Root dir - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":libprotobuf-cpp-lite"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["foo.proto"]`, - "tags": `["manual"]`, - }), - MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{ - "deps": `[ - ":foo_proto", - "//bar:foo_proto", - "//baz/subbaz:foo_proto", - ]`, - }), - } - runCcLibraryTestCase(t, tc) - - // bar dir - tc.Dir = "bar" - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["//bar:bar.proto"]`, - "tags": `["manual"]`, - }), - } - runCcLibraryTestCase(t, tc) - - // baz/subbaz dir - tc.Dir = "baz/subbaz" - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["//baz/subbaz:baz.proto"]`, - "tags": `["manual"]`, - }), - } - runCcLibraryTestCase(t, tc) -} - -// Bazel enforces that proto_library and the .proto file are in the same bazel package -func TestGenerateProtoLibraryInSamePackageNotCanonicalFromRoot(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "cc_library depends on .proto files from multiple packages", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library_static { - name: "foo", - srcs: [ - "foo.proto", - "bar/bar.proto", // Different package because there is a bar/Android.bp - "baz/subbaz/baz.proto", // Different package because there is baz/subbaz/Android.bp - ], - proto: { - canonical_path_from_root: false, - } -} -` + simpleModule("cc_library", "libprotobuf-cpp-lite"), - Filesystem: map[string]string{ - "bar/Android.bp": "", - "baz/subbaz/Android.bp": "", - }, - } - - // We will run the test 3 times and check in the root, bar and baz/subbaz directories - // Root dir - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":libprotobuf-cpp-lite"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["foo.proto"]`, - "strip_import_prefix": `""`, - "tags": `["manual"]`, - }), - MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{ - "deps": `[ - ":foo_proto", - "//bar:foo_proto", - "//baz/subbaz:foo_proto", - ]`, - }), - } - runCcLibraryTestCase(t, tc) - - // bar dir - tc.Dir = "bar" - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["//bar:bar.proto"]`, - "strip_import_prefix": `""`, - "import_prefix": `"bar"`, - "tags": `["manual"]`, - }), - } - runCcLibraryTestCase(t, tc) - - // baz/subbaz dir - tc.Dir = "baz/subbaz" - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["//baz/subbaz:baz.proto"]`, - "strip_import_prefix": `""`, - "import_prefix": `"baz/subbaz"`, - "tags": `["manual"]`, - }), - } - runCcLibraryTestCase(t, tc) -} - -func TestProtoIncludeDirs(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "cc_library depends on .proto files using proto.include_dirs", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library_static { - name: "foo", - srcs: [ - "foo.proto", - ], - proto: { - include_dirs: ["bar"], - } -} -` + simpleModule("cc_library", "libprotobuf-cpp-lite"), - Filesystem: map[string]string{ - "bar/Android.bp": "", - "bar/bar.proto": "", - "bar/baz/Android.bp": "", - "bar/baz/baz.proto": "", - }, - } - - // We will run the test 3 times and check in the root, bar and bar/baz directories - // Root dir - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":libprotobuf-cpp-lite"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["foo.proto"]`, - "tags": `["manual"]`, - }), - MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{ - "deps": `[":foo_proto"]`, - "transitive_deps": `[ - "//bar:bar.include_dir_bp2build_generated_proto", - "//bar/baz:bar.include_dir_bp2build_generated_proto", - ]`, - }), - } - runCcLibraryTestCase(t, tc) - - // bar dir - tc.Dir = "bar" - tc.ExpectedBazelTargets = []string{ - MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{ - "srcs": `["bar.proto"]`, - "strip_import_prefix": `""`, - "tags": `["manual"]`, - }), - } - runCcLibraryTestCase(t, tc) - - // bar/baz dir - tc.Dir = "bar/baz" - tc.ExpectedBazelTargets = []string{ - MakeBazelTargetNoRestrictions("proto_library", "bar.include_dir_bp2build_generated_proto", AttrNameToString{ - "srcs": `["//bar/baz:baz.proto"]`, - "strip_import_prefix": `""`, - "import_prefix": `"baz"`, - "tags": `["manual"]`, - }), - } - runCcLibraryTestCase(t, tc) -} - -func TestProtoIncludeDirsWithSrcsInMultiplePackages(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "cc_library has srcs in multiple bazel packages and uses proto.include_dirs", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library_static { - name: "foo", - srcs: [ - "foo.proto", - "bar/bar.proto", - ], - proto: { - include_dirs: ["baz"], - } -} -` + simpleModule("cc_library", "libprotobuf-cpp-lite"), - Filesystem: map[string]string{ - "bar/Android.bp": "", // package boundary - "baz/Android.bp": "", - "baz/baz.proto": "", - }, - } - - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":libprotobuf-cpp-lite"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["foo.proto"]`, - "tags": `["manual"]`, - }), - MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{ - "deps": `[ - ":foo_proto", - "//bar:foo_proto", - ]`, - "transitive_deps": `["//baz:baz.include_dir_bp2build_generated_proto"]`, - }), - } - runCcLibraryTestCase(t, tc) - -} - -func TestProtoLocalIncludeDirs(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "cc_library depends on .proto files using proto.local_include_dirs", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "libprotobuf-cpp-lite", - // TODO: b/285631638 - A stubbed proto library dependency does not work as a protolib - // dependency of cc_library_static. - bazel_module: { bp2build_available: false }, -} -`, - Filesystem: map[string]string{ - "foo/Android.bp": `cc_library_static { - name: "foo", - srcs: [ - "foo.proto", - ], - proto: { - local_include_dirs: ["foo_subdir"], - }, - bazel_module: { bp2build_available: true }, -}`, - "foo/foo.proto": "", - "foo/foo_subdir/Android.bp": "", - "foo/foo_subdir/foo_subdir.proto": "", - }, - } - - // We will run the test 2 times and check in foo and foo/foo_subdir directories - // foo dir - tc.Dir = "foo" - tc.ExpectedBazelTargets = []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "deps": `["//:libprotobuf-cpp-lite"]`, - "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`, - }), - MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ - "srcs": `["foo.proto"]`, - "tags": `["manual"]`, - }), - MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{ - "deps": `[":foo_proto"]`, - "transitive_deps": `["//foo/foo_subdir:foo.foo_subdir.include_dir_bp2build_generated_proto"]`, - }), - } - runCcLibraryTestCase(t, tc) - - // foo/foo_subdir - tc.Dir = "foo/foo_subdir" - tc.ExpectedBazelTargets = []string{ - MakeBazelTargetNoRestrictions("proto_library", "foo.foo_subdir.include_dir_bp2build_generated_proto", AttrNameToString{ - "srcs": `["foo_subdir.proto"]`, - "strip_import_prefix": `""`, - "tags": `["manual"]`, - }), - } - runCcLibraryTestCase(t, tc) -} - -// `foo_device` and `bar_host` can depend on .proto files of a specific dir, -// the dynamically generated proto_library should not have any target_compatible_with -func TestProtoLibraryForIncludeDirsIsOsAgnostic(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "proto_library generated for proto.include_dirs is compatible for all axes", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: simpleModule("cc_library", "libprotobuf-cpp-lite") + ` -cc_library { - name: "foo_device", - device_supported: true, // this is the default behavior, but added explicitly here for illustration - host_supported: false, - proto: {include_dirs: ["dir"]}, -} -cc_library { - name: "bar_host", - device_supported: false, - host_supported: true, - srcs: ["bar.proto"], - proto: {include_dirs: ["dir"]}, -} -`, - Filesystem: map[string]string{ - "dir/Android.bp": "", - "dir/dir.proto": "", - }, - Dir: "dir", // check for the generated proto_library - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("proto_library", "dir.include_dir_bp2build_generated_proto", AttrNameToString{ - "srcs": `["dir.proto"]`, - "strip_import_prefix": `""`, - "tags": `["manual"]`, - }), - }, - } - runCcLibraryTestCase(t, tc) -} - -func TestCcCompileMultilibConversion(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "cc_library with compile_multilib", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "lib32", - compile_multilib: "32", -} -cc_library { - name: "lib64", - compile_multilib: "64", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("cc_library_shared", "lib32", AttrNameToString{ - "local_includes": `["."]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({ - "//build/bazel_common_rules/platforms/arch:arm64": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/arch:riscv64": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["@platforms//:incompatible"], - "//conditions:default": [], - })`, - }), - MakeBazelTargetNoRestrictions("cc_library_static", "lib32_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({ - "//build/bazel_common_rules/platforms/arch:arm64": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/arch:riscv64": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["@platforms//:incompatible"], - "//conditions:default": [], - })`, - }), - MakeBazelTargetNoRestrictions("cc_library_shared", "lib64", AttrNameToString{ - "local_includes": `["."]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/arch:x86": ["@platforms//:incompatible"], - "//conditions:default": [], - })`, - }), - MakeBazelTargetNoRestrictions("cc_library_static", "lib64_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["@platforms//:incompatible"], - "//build/bazel_common_rules/platforms/arch:x86": ["@platforms//:incompatible"], - "//conditions:default": [], - })`, - }), - }, - } - runCcLibraryTestCase(t, tc) -} - -func TestNdkLibraryConversion(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "ndk_library conversion", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -cc_library { - name: "libfoo", -} -ndk_library { - name: "libfoo", - first_version: "29", - symbol_file: "libfoo.map.txt", -} -`, - StubbedBuildDefinitions: []string{"libfoo"}, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_stub_suite", "libfoo.ndk_stub_libs", AttrNameToString{ - "api_surface": `"publicapi"`, - "included_in_ndk": `True`, - "soname": `"libfoo.so"`, - "source_library_label": `"//:libfoo"`, - "symbol_file": `"libfoo.map.txt"`, - "versions": `[ - "29", - "30", - "S", - "Tiramisu", - "current", - ]`, - }), - }, - } - runCcLibraryTestCase(t, tc) -} - -func TestNdkHeadersConversion(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "ndk_headers conversion", - ModuleTypeUnderTest: "ndk_headers", - ModuleTypeUnderTestFactory: cc.NdkHeadersFactory, - Blueprint: ` -ndk_headers { - name: "libfoo_headers", - from: "from", - to: "to", - srcs: ["foo.h", "foo_other.h"] -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{ - "strip_import_prefix": `"from"`, - "import_prefix": `"to"`, - "hdrs": `[ - "foo.h", - "foo_other.h", - ]`, - }), - }, - } - runCcLibraryTestCase(t, tc) -} - -func TestVersionedNdkHeadersConversion(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "versioned_ndk_headers conversion", - ModuleTypeUnderTest: "versioned_ndk_headers", - ModuleTypeUnderTestFactory: cc.VersionedNdkHeadersFactory, - Blueprint: ` -versioned_ndk_headers { - name: "libfoo_headers", - from: "from", - to: "to", -} -`, - Filesystem: map[string]string{ - "from/foo.h": "", - "from/foo_other.h": "", - }, - ExpectedBazelTargets: []string{ - MakeBazelTargetNoRestrictions("ndk_headers", "libfoo_headers", AttrNameToString{ - "strip_import_prefix": `"from"`, - "import_prefix": `"to"`, - "hdrs": `[ - "from/foo.h", - "from/foo_other.h", - ]`, - "run_versioner": "True", - }), - }, - } - runCcLibraryTestCase(t, tc) -} - -// Regression test for b/303307456. -// TODO: b/202299295 - Remove this test when cc rules have proper support -// for the `required` property -func TestCcModules_requiredProperty(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc modules do not use the required property", - Filesystem: map[string]string{ - "foo.c": "", - "bar.c": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library { - name: "foo_both", - srcs: ["foo.c"], - include_build_directory: false, - required: ["bar"], -} -cc_library_shared { - name: "foo_shared", - srcs: ["foo.c"], - include_build_directory: false, - required: ["bar"], -} -cc_library_static { - name: "foo_static", - srcs: ["foo.c"], - include_build_directory: false, - required: ["bar"], -} -cc_library_static { - name: "bar", - srcs: ["bar.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_both_bp2build_cc_library_static", AttrNameToString{ - "srcs_c": `["foo.c"]`, - }), - MakeBazelTarget("cc_library_shared", "foo_both", AttrNameToString{ - "srcs_c": `["foo.c"]`, - }), - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "srcs_c": `["foo.c"]`, - }), - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["foo.c"]`, - }), - MakeBazelTarget("cc_library_static", "bar", AttrNameToString{ - "srcs_c": `["bar.c"]`, - }), - }, - }) -} - -func TestPropertiesIfStubLibraryIsInNdk(t *testing.T) { - tc := Bp2buildTestCase{ - Description: "If an equivalent ndk_library exists, set included_in_ndk=true for module-libapi stubs", - ModuleTypeUnderTest: "cc_library", - ModuleTypeUnderTestFactory: cc.LibraryFactory, - Blueprint: ` -// libfoo is an ndk library and contributes to module-libapi -cc_library { - name: "libfoo", - stubs: {symbol_file: "libfoo.map.txt"}, -} -ndk_library { - name: "libfoo", - first_version: "29", - symbol_file: "libfoo.map.txt", -} -// libbar is not an ndk library, but contributes to module-libapi -cc_library { - name: "libbar", - stubs: {symbol_file: "libbar.map.txt"}, -} -`, - StubbedBuildDefinitions: []string{"libfoo.ndk"}, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "libfoo_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "libfoo", AttrNameToString{ - "local_includes": `["."]`, - "stubs_symbol_file": `"libfoo.map.txt"`, - }), - MakeBazelTarget("cc_stub_suite", "libfoo_stub_libs", AttrNameToString{ - "api_surface": `"module-libapi"`, - "soname": `"libfoo.so"`, - "source_library_label": `"//:libfoo"`, - "symbol_file": `"libfoo.map.txt"`, - "versions": `["current"]`, - "included_in_ndk": `True`, - }), - MakeBazelTarget("cc_library_static", "libbar_bp2build_cc_library_static", AttrNameToString{ - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "libbar", AttrNameToString{ - "local_includes": `["."]`, - "stubs_symbol_file": `"libbar.map.txt"`, - }), - MakeBazelTarget("cc_stub_suite", "libbar_stub_libs", AttrNameToString{ - "api_surface": `"module-libapi"`, - "soname": `"libbar.so"`, - "source_library_label": `"//:libbar"`, - "symbol_file": `"libbar.map.txt"`, - "versions": `["current"]`, - }), - }, - } - runCcLibraryTestCase(t, tc) -} diff --git a/bp2build/cc_library_headers_conversion_test.go b/bp2build/cc_library_headers_conversion_test.go deleted file mode 100644 index 765598666..000000000 --- a/bp2build/cc_library_headers_conversion_test.go +++ /dev/null @@ -1,486 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "testing" - - "android/soong/android" - "android/soong/cc" -) - -const ( - // See cc/testing.go for more context - soongCcLibraryHeadersPreamble = ` -cc_defaults { - name: "linux_bionic_supported", -}` -) - -func TestCcLibraryHeadersLoadStatement(t *testing.T) { - testCases := []struct { - bazelTargets BazelTargets - expectedLoadStatements string - }{ - { - bazelTargets: BazelTargets{ - BazelTarget{ - name: "cc_library_headers_target", - ruleClass: "cc_library_headers", - // Note: no bzlLoadLocation for native rules - }, - }, - expectedLoadStatements: ``, - }, - } - - for _, testCase := range testCases { - actual := testCase.bazelTargets.LoadStatements() - expected := testCase.expectedLoadStatements - if actual != expected { - t.Fatalf("Expected load statements to be %s, got %s", expected, actual) - } - } -} - -func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) { - cc.RegisterCCBuildComponents(ctx) - cc.RegisterLibraryHeadersBuildComponents(ctx) - ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory) -} - -func runCcLibraryHeadersTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - RunBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc) -} - -func TestCcLibraryHeadersSimple(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers test", - Filesystem: map[string]string{ - "dir-1/dir1a.h": "", - "dir-1/dir1b.h": "", - "dir-2/dir2a.h": "", - "dir-2/dir2b.h": "", - "arch_arm64_exported_include_dir/a.h": "", - "arch_x86_exported_include_dir/b.h": "", - "arch_x86_64_exported_include_dir/c.h": "", - }, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_library_headers { - name: "foo_headers", - export_include_dirs: ["dir-1", "dir-2"], - - arch: { - arm64: { - // We expect dir-1 headers to be dropped, because dir-1 is already in export_include_dirs - export_include_dirs: ["arch_arm64_exported_include_dir", "dir-1"], - }, - x86: { - export_include_dirs: ["arch_x86_exported_include_dir"], - }, - x86_64: { - export_include_dirs: ["arch_x86_64_exported_include_dir"], - }, - }, - sdk_version: "current", - min_sdk_version: "29", - - // TODO: Also support export_header_lib_headers -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "export_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": ["arch_arm64_exported_include_dir"], - "//build/bazel_common_rules/platforms/arch:x86": ["arch_x86_exported_include_dir"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["arch_x86_64_exported_include_dir"], - "//conditions:default": [], - }) + [ - "dir-1", - "dir-2", - ]`, - "sdk_version": `"current"`, - "min_sdk_version": `"29"`, - "deps": `select({ - "//build/bazel/rules/apex:unbundled_app": ["//build/bazel/rules/cc:ndk_sysroot"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -// header_libs has "variant_prepend" tag. In bp2build output, -// variant info(select) should go before general info. -func TestCcLibraryHeadersOsSpecificHeader(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers test with os-specific header_libs props", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"android-lib", "base-lib", "darwin-lib", - "linux-lib", "linux_bionic-lib", "windows-lib"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library_headers { - name: "android-lib", -} -cc_library_headers { - name: "base-lib", -} -cc_library_headers { - name: "darwin-lib", -} -cc_library_headers { - name: "linux-lib", -} -cc_library_headers { - name: "linux_bionic-lib", -} -cc_library_headers { - name: "windows-lib", -} -cc_library_headers { - name: "foo_headers", - header_libs: ["base-lib"], - export_header_lib_headers: ["base-lib"], - target: { - android: { - header_libs: ["android-lib"], - export_header_lib_headers: ["android-lib"], - }, - darwin: { - header_libs: ["darwin-lib"], - export_header_lib_headers: ["darwin-lib"], - }, - linux_bionic: { - header_libs: ["linux_bionic-lib"], - export_header_lib_headers: ["linux_bionic-lib"], - }, - linux_glibc: { - header_libs: ["linux-lib"], - export_header_lib_headers: ["linux-lib"], - }, - windows: { - header_libs: ["windows-lib"], - export_header_lib_headers: ["windows-lib"], - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":android-lib"], - "//build/bazel_common_rules/platforms/os:darwin": [":darwin-lib"], - "//build/bazel_common_rules/platforms/os:linux_bionic": [":linux_bionic-lib"], - "//build/bazel_common_rules/platforms/os:linux_glibc": [":linux-lib"], - "//build/bazel_common_rules/platforms/os:windows": [":windows-lib"], - "//conditions:default": [], - }) + [":base-lib"]`, - }), - }, - }) -} - -func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"android-lib", "exported-lib"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library_headers { - name: "android-lib", - } -cc_library_headers { - name: "exported-lib", -} -cc_library_headers { - name: "foo_headers", - target: { - android: { - header_libs: ["android-lib", "exported-lib"], - export_header_lib_headers: ["exported-lib"] - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":exported-lib"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props", - Filesystem: map[string]string{}, - Blueprint: soongCcLibraryPreamble + `cc_library_headers { - name: "foo_headers", - export_system_include_dirs: [ - "shared_include_dir", - ], - target: { - android: { - export_system_include_dirs: [ - "android_include_dir", - ], - }, - linux_glibc: { - export_system_include_dirs: [ - "linux_include_dir", - ], - }, - darwin: { - export_system_include_dirs: [ - "darwin_include_dir", - ], - }, - }, - arch: { - arm: { - export_system_include_dirs: [ - "arm_include_dir", - ], - }, - x86_64: { - export_system_include_dirs: [ - "x86_64_include_dir", - ], - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_include_dir"], - "//build/bazel_common_rules/platforms/os:darwin": ["darwin_include_dir"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux_include_dir"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arm_include_dir"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["x86_64_include_dir"], - "//conditions:default": [], - }) + ["shared_include_dir"]`, - }), - }, - }) -} - -func TestCcLibraryHeadersNoCrtIgnored(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers test", - Filesystem: map[string]string{ - "lib-1/lib1a.h": "", - "lib-1/lib1b.h": "", - "lib-2/lib2a.h": "", - "lib-2/lib2b.h": "", - "dir-1/dir1a.h": "", - "dir-1/dir1b.h": "", - "dir-2/dir2a.h": "", - "dir-2/dir2b.h": "", - "arch_arm64_exported_include_dir/a.h": "", - "arch_x86_exported_include_dir/b.h": "", - "arch_x86_64_exported_include_dir/c.h": "", - }, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_library_headers { - name: "lib-1", - export_include_dirs: ["lib-1"], - no_libcrt: true, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "lib-1", AttrNameToString{ - "export_includes": `["lib-1"]`, - }), - }, - }) -} - -func TestCcLibraryHeadersExportedStaticLibHeadersReexported(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers exported_static_lib_headers is reexported", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"foo_export", "foo_no_reexport"}, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_library_headers { - name: "foo_headers", - export_static_lib_headers: ["foo_export"], - static_libs: ["foo_export", "foo_no_reexport"], - bazel_module: { bp2build_available: true }, -} -` + simpleModule("cc_library_headers", "foo_export") + - simpleModule("cc_library_headers", "foo_no_reexport"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `[":foo_export"]`, - }), - }, - }) -} - -func TestCcLibraryHeadersExportedSharedLibHeadersReexported(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers exported_shared_lib_headers is reexported", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"foo_export", "foo_no_reexport"}, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_library_headers { - name: "foo_headers", - export_shared_lib_headers: ["foo_export"], - shared_libs: ["foo_export", "foo_no_reexport"], - bazel_module: { bp2build_available: true }, -} -` + simpleModule("cc_library_headers", "foo_export") + - simpleModule("cc_library_headers", "foo_no_reexport"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `[":foo_export"]`, - }), - }, - }) -} - -func TestCcLibraryHeadersExportedHeaderLibHeadersReexported(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers exported_header_lib_headers is reexported", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"foo_export", "foo_no_reexport"}, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_library_headers { - name: "foo_headers", - export_header_lib_headers: ["foo_export"], - header_libs: ["foo_export", "foo_no_reexport"], - bazel_module: { bp2build_available: true }, -} -` + simpleModule("cc_library_headers", "foo_export") + - simpleModule("cc_library_headers", "foo_no_reexport"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `[":foo_export"]`, - }), - }, - }) -} - -func TestCcLibraryHeadersWholeStaticLibsReexported(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers whole_static_libs is reexported", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"foo_export"}, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_library_headers { - name: "foo_headers", - whole_static_libs: ["foo_export"], - bazel_module: { bp2build_available: true }, -} -` + simpleModule("cc_library_headers", "foo_export"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `[":foo_export"]`, - }), - }, - }) -} - -func TestPrebuiltCcLibraryHeadersWholeStaticLibsReexported(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers whole_static_libs is reexported", - Filesystem: map[string]string{ - "foo/bar/Android.bp": simpleModule("cc_library_headers", "foo_headers"), - }, - StubbedBuildDefinitions: []string{"foo_export"}, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_prebuilt_library_headers { - name: "foo_headers", - whole_static_libs: ["foo_export"], - bazel_module: { bp2build_available: true }, -} -` + simpleModule("cc_library_headers", "foo_export"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `[":foo_export"]`, - }), - }, - }) -} - -func TestPrebuiltCcLibraryHeadersPreferredRdepUpdated(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers prebuilt preferred is used as rdep", - StubbedBuildDefinitions: []string{"foo_export", "//foo/bar:foo_headers"}, - Filesystem: map[string]string{ - "foo/bar/Android.bp": simpleModule("cc_library_headers", "foo_headers"), - }, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_prebuilt_library_headers { - name: "foo_headers", - whole_static_libs: ["foo_export"], - bazel_module: { bp2build_available: true }, - prefer: true, -} - -cc_library_shared { - name: "foo", - header_libs: ["foo_headers"], - include_build_directory: false, -} -` + simpleModule("cc_library_headers", "foo_export"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `[":foo_export"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "implementation_deps": `[":foo_headers"]`, - }), - }, - }) -} - -func TestPrebuiltCcLibraryHeadersRdepUpdated(t *testing.T) { - runCcLibraryHeadersTestCase(t, Bp2buildTestCase{ - Description: "cc_library_headers not preferred is not used for rdep", - StubbedBuildDefinitions: []string{"foo_export", "//foo/bar:foo_headers"}, - Filesystem: map[string]string{ - "foo/bar/Android.bp": simpleModule("cc_library_headers", "foo_headers"), - }, - Blueprint: soongCcLibraryHeadersPreamble + ` -cc_prebuilt_library_headers { - name: "foo_headers", - whole_static_libs: ["foo_export"], - bazel_module: { bp2build_available: true }, - prefer: false, -} - -cc_library_shared { - name: "foo", - header_libs: ["foo_headers"], - include_build_directory: false, -} -` + simpleModule("cc_library_headers", "foo_export"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_headers", "foo_headers", AttrNameToString{ - "deps": `[":foo_export"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "implementation_deps": `["//foo/bar:foo_headers"]`, - }), - }, - }) -} diff --git a/bp2build/cc_library_shared_conversion_test.go b/bp2build/cc_library_shared_conversion_test.go deleted file mode 100644 index 9f9fcf9c1..000000000 --- a/bp2build/cc_library_shared_conversion_test.go +++ /dev/null @@ -1,1656 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "testing" - - "android/soong/android" - "android/soong/cc" -) - -const ( - // See cc/testing.go for more context - // TODO(alexmarquez): Split out the preamble into common code? - soongCcLibrarySharedPreamble = soongCcLibraryStaticPreamble -) - -func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) { - cc.RegisterCCBuildComponents(ctx) - ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) - ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - ctx.RegisterModuleType("ndk_library", cc.NdkLibraryFactory) -} - -func runCcLibrarySharedTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - t.Parallel() - tc.StubbedBuildDefinitions = append(tc.StubbedBuildDefinitions, "libbuildversion", "libprotobuf-cpp-lite", "libprotobuf-cpp-full") - (&tc).ModuleTypeUnderTest = "cc_library_shared" - (&tc).ModuleTypeUnderTestFactory = cc.LibrarySharedFactory - RunBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc) -} - -func TestCcLibrarySharedSimple(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared simple overall test", - StubbedBuildDefinitions: []string{"header_lib_1", "header_lib_2", "whole_static_lib_1", "whole_static_lib_2", "shared_lib_1", "shared_lib_2"}, - Filesystem: map[string]string{ - // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?) - "include_dir_1/include_dir_1_a.h": "", - "include_dir_1/include_dir_1_b.h": "", - "include_dir_2/include_dir_2_a.h": "", - "include_dir_2/include_dir_2_b.h": "", - // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?) - "local_include_dir_1/local_include_dir_1_a.h": "", - "local_include_dir_1/local_include_dir_1_b.h": "", - "local_include_dir_2/local_include_dir_2_a.h": "", - "local_include_dir_2/local_include_dir_2_b.h": "", - // NOTE: export_include_dir headers *should* appear in Bazel hdrs later - "export_include_dir_1/export_include_dir_1_a.h": "", - "export_include_dir_1/export_include_dir_1_b.h": "", - "export_include_dir_2/export_include_dir_2_a.h": "", - "export_include_dir_2/export_include_dir_2_b.h": "", - // NOTE: Soong implicitly includes headers in the current directory - "implicit_include_1.h": "", - "implicit_include_2.h": "", - }, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_headers { - name: "header_lib_1", - export_include_dirs: ["header_lib_1"], -} - -cc_library_headers { - name: "header_lib_2", - export_include_dirs: ["header_lib_2"], -} - -cc_library_shared { - name: "shared_lib_1", - srcs: ["shared_lib_1.cc"], -} - -cc_library_shared { - name: "shared_lib_2", - srcs: ["shared_lib_2.cc"], -} - -cc_library_static { - name: "whole_static_lib_1", - srcs: ["whole_static_lib_1.cc"], -} - -cc_library_static { - name: "whole_static_lib_2", - srcs: ["whole_static_lib_2.cc"], -} - -cc_library_shared { - name: "foo_shared", - srcs: [ - "foo_shared1.cc", - "foo_shared2.cc", - ], - cflags: [ - "-Dflag1", - "-Dflag2" - ], - shared_libs: [ - "shared_lib_1", - "shared_lib_2" - ], - whole_static_libs: [ - "whole_static_lib_1", - "whole_static_lib_2" - ], - include_dirs: [ - "include_dir_1", - "include_dir_2", - ], - local_include_dirs: [ - "local_include_dir_1", - "local_include_dir_2", - ], - export_include_dirs: [ - "export_include_dir_1", - "export_include_dir_2" - ], - header_libs: [ - "header_lib_1", - "header_lib_2" - ], - sdk_version: "current", - min_sdk_version: "29", - - // TODO: Also support export_header_lib_headers -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "absolute_includes": `[ - "include_dir_1", - "include_dir_2", - ]`, - "copts": `[ - "-Dflag1", - "-Dflag2", - ]`, - "export_includes": `[ - "export_include_dir_1", - "export_include_dir_2", - ]`, - "implementation_deps": `[ - ":header_lib_1", - ":header_lib_2", - ]`, - "implementation_dynamic_deps": `[ - ":shared_lib_1", - ":shared_lib_2", - ]`, - "local_includes": `[ - "local_include_dir_1", - "local_include_dir_2", - ".", - ]`, - "srcs": `[ - "foo_shared1.cc", - "foo_shared2.cc", - ]`, - "whole_archive_deps": `[ - ":whole_static_lib_1", - ":whole_static_lib_2", - ]`, - "sdk_version": `"current"`, - "min_sdk_version": `"29"`, - "deps": `select({ - "//build/bazel/rules/apex:unbundled_app": ["//build/bazel/rules/cc:ndk_sysroot"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared arch-specific shared_libs with whole_static_libs", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"static_dep", "shared_dep"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_static { - name: "static_dep", -} -cc_library_shared { - name: "shared_dep", -} -cc_library_shared { - name: "foo_shared", - arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":shared_dep"], - "//conditions:default": [], - })`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":static_dep"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"shared_dep"}, - Description: "cc_library_shared os-specific shared_libs", - Filesystem: map[string]string{}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "shared_dep", -} -cc_library_shared { - name: "foo_shared", - target: { android: { shared_libs: ["shared_dep"], } }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":shared_dep"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"shared_dep", "shared_dep2", "shared_dep3"}, - Description: "cc_library_shared base, arch, and os-specific shared_libs", - Filesystem: map[string]string{}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "shared_dep", -} -cc_library_shared { - name: "shared_dep2", -} -cc_library_shared { - name: "shared_dep3", -} -cc_library_shared { - name: "foo_shared", - shared_libs: ["shared_dep"], - target: { android: { shared_libs: ["shared_dep2"] } }, - arch: { arm64: { shared_libs: ["shared_dep3"] } }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "implementation_dynamic_deps": `[":shared_dep"] + select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":shared_dep3"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [":shared_dep2"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared simple exclude_srcs", - Filesystem: map[string]string{ - "common.c": "", - "foo-a.c": "", - "foo-excluded.c": "", - }, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "foo_shared", - srcs: ["common.c", "foo-*.c"], - exclude_srcs: ["foo-excluded.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "srcs_c": `[ - "common.c", - "foo-a.c", - ]`, - }), - }, - }) -} - -func TestCcLibrarySharedStrip(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared stripping", - Filesystem: map[string]string{}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "foo_shared", - strip: { - keep_symbols: false, - keep_symbols_and_debug_frame: true, - keep_symbols_list: ["sym", "sym2"], - all: true, - none: false, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "strip": `{ - "all": True, - "keep_symbols": False, - "keep_symbols_and_debug_frame": True, - "keep_symbols_list": [ - "sym", - "sym2", - ], - "none": False, - }`, - }), - }, - }) -} - -func TestCcLibrarySharedVersionScriptAndDynamicList(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared version script and dynamic list", - Filesystem: map[string]string{ - "version_script": "", - "dynamic.list": "", - }, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "foo_shared", - version_script: "version_script", - dynamic_list: "dynamic.list", - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "additional_linker_inputs": `[ - "version_script", - "dynamic.list", - ]`, - "linkopts": `[ - "-Wl,--version-script,$(location version_script)", - "-Wl,--dynamic-list,$(location dynamic.list)", - ]`, - "features": `["android_cfi_exports_map"]`, - }), - }, - }) -} - -func TestCcLibraryLdflagsSplitBySpaceSoongAdded(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)", - Filesystem: map[string]string{ - "version_script": "", - "dynamic.list": "", - }, - Blueprint: ` -cc_library_shared { - name: "foo", - ldflags: [ - "--nospace_flag", - "-z spaceflag", - ], - version_script: "version_script", - dynamic_list: "dynamic.list", - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "additional_linker_inputs": `[ - "version_script", - "dynamic.list", - ]`, - "linkopts": `[ - "--nospace_flag", - "-z", - "spaceflag", - "-Wl,--version-script,$(location version_script)", - "-Wl,--dynamic-list,$(location dynamic.list)", - ]`, - "features": `["android_cfi_exports_map"]`, - }), - }, - }) -} - -func TestCcLibrarySharedNoCrtTrue(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared - nocrt: true disables feature", - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library_shared { - name: "foo_shared", - srcs: ["impl.cpp"], - nocrt: true, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "features": `["-link_crt"]`, - "srcs": `["impl.cpp"]`, - }), - }, - }) -} - -func TestCcLibrarySharedNoCrtFalse(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared - nocrt: false doesn't disable feature", - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library_shared { - name: "foo_shared", - srcs: ["impl.cpp"], - nocrt: false, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "srcs": `["impl.cpp"]`, - }), - }, - }) -} - -func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared - nocrt in select", - Filesystem: map[string]string{ - "impl.cpp": "", - }, - Blueprint: soongCcLibraryPreamble + ` -cc_library_shared { - name: "foo_shared", - srcs: ["impl.cpp"], - arch: { - arm: { - nocrt: true, - }, - x86: { - nocrt: false, - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["-link_crt"], - "//conditions:default": [], - })`, - "srcs": `["impl.cpp"]`, - }), - }, - }) -} - -func TestCcLibrarySharedProto(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Blueprint: soongCcProtoPreamble + `cc_library_shared { - name: "foo", - srcs: ["foo.proto"], - proto: { - 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"]`, - }), - }, - }) -} - -func TestCcLibrarySharedUseVersionLib(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Filesystem: map[string]string{ - soongCcVersionLibBpPath: soongCcVersionLibBp, - }, - StubbedBuildDefinitions: []string{"//build/soong/cc/libbuildversion:libbuildversion"}, - Blueprint: soongCcProtoPreamble + `cc_library_shared { - name: "foo", - use_version_lib: true, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "use_version_lib": "True", - "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`, - }), - }, - }) -} - -func TestCcLibrarySharedStubs(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared stubs", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - Dir: "foo/bar", - Filesystem: map[string]string{ - "foo/bar/Android.bp": ` -cc_library_shared { - name: "a", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - bazel_module: { bp2build_available: true }, - include_build_directory: false, -} -`, - }, - Blueprint: soongCcLibraryPreamble, - ExpectedBazelTargets: []string{makeCcStubSuiteTargets("a", AttrNameToString{ - "api_surface": `"module-libapi"`, - "soname": `"a.so"`, - "source_library_label": `"//foo/bar:a"`, - "stubs_symbol_file": `"a.map.txt"`, - "stubs_versions": `[ - "28", - "29", - "current", - ]`, - }), - MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "stubs_symbol_file": `"a.map.txt"`, - }), - }, - }) -} - -func TestCcLibrarySharedStubs_UseImplementationInSameApex(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared stubs", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - StubbedBuildDefinitions: []string{"a"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "a", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - include_build_directory: false, - apex_available: ["made_up_apex"], -} -cc_library_shared { - name: "b", - shared_libs: [":a"], - include_build_directory: false, - apex_available: ["made_up_apex"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "b", AttrNameToString{ - "implementation_dynamic_deps": `[":a"]`, - "tags": `["apex_available=made_up_apex"]`, - }), - }, - }) -} - -func TestCcLibrarySharedStubs_UseStubsInDifferentApex(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared stubs", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - StubbedBuildDefinitions: []string{"a"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "a", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - include_build_directory: false, - apex_available: ["apex_a"], -} -cc_library_shared { - name: "b", - shared_libs: [":a"], - include_build_directory: false, - apex_available: ["apex_b"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "b", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel/rules/apex:apex_b": ["@api_surfaces//module-libapi/current:a"], - "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:a"], - "//conditions:default": [":a"], - })`, - "tags": `["apex_available=apex_b"]`, - }), - }, - }) -} - -// Tests that library in apexfoo links against stubs of platform_lib and otherapex_lib -func TestCcLibrarySharedStubs_UseStubsFromMultipleApiDomains(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared stubs", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - StubbedBuildDefinitions: []string{"libplatform_stable", "libapexfoo_stable"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "libplatform_stable", - stubs: { symbol_file: "libplatform_stable.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["//apex_available:platform"], - include_build_directory: false, -} -cc_library_shared { - name: "libapexfoo_stable", - stubs: { symbol_file: "libapexfoo_stable.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["apexfoo"], - include_build_directory: false, -} -cc_library_shared { - name: "libutils", - shared_libs: ["libplatform_stable", "libapexfoo_stable",], - apex_available: ["//apex_available:platform", "apexfoo", "apexbar"], - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "libutils", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel/rules/apex:apexbar": [ - "@api_surfaces//module-libapi/current:libplatform_stable", - "@api_surfaces//module-libapi/current:libapexfoo_stable", - ], - "//build/bazel/rules/apex:apexfoo": [ - "@api_surfaces//module-libapi/current:libplatform_stable", - ":libapexfoo_stable", - ], - "//build/bazel/rules/apex:system": [ - ":libplatform_stable", - "@api_surfaces//module-libapi/current:libapexfoo_stable", - ], - "//conditions:default": [ - ":libplatform_stable", - ":libapexfoo_stable", - ], - })`, - "tags": `[ - "apex_available=//apex_available:platform", - "apex_available=apexfoo", - "apex_available=apexbar", - ]`, - }), - }, - }) -} - -func TestCcLibrarySharedStubs_IgnorePlatformAvailable(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared stubs", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - StubbedBuildDefinitions: []string{"a"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "a", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - include_build_directory: false, - apex_available: ["//apex_available:platform", "apex_a"], -} -cc_library_shared { - name: "b", - shared_libs: [":a"], - include_build_directory: false, - apex_available: ["//apex_available:platform", "apex_b"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "b", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel/rules/apex:apex_b": ["@api_surfaces//module-libapi/current:a"], - "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:a"], - "//conditions:default": [":a"], - })`, - "tags": `[ - "apex_available=//apex_available:platform", - "apex_available=apex_b", - ]`, - }), - }, - }) -} - -func TestCcLibraryDoesNotDropStubDepIfNoVariationAcrossAxis(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library depeends on impl for all configurations", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - StubbedBuildDefinitions: []string{"a"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "a", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["//apex_available:platform"], -} -cc_library_shared { - name: "b", - shared_libs: [":a"], - include_build_directory: false, - apex_available: ["//apex_available:platform"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "b", AttrNameToString{ - "implementation_dynamic_deps": `[":a"]`, - "tags": `["apex_available=//apex_available:platform"]`, - }), - }, - }) -} - -func TestCcLibrarySharedStubs_MultipleApexAvailable(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - StubbedBuildDefinitions: []string{"a"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "a", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - include_build_directory: false, - apex_available: ["//apex_available:platform", "apex_a", "apex_b"], -} -cc_library_shared { - name: "b", - shared_libs: [":a"], - include_build_directory: false, - apex_available: ["//apex_available:platform", "apex_b"], -} - -cc_library_shared { - name: "c", - shared_libs: [":a"], - include_build_directory: false, - apex_available: ["//apex_available:platform", "apex_a", "apex_b"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "b", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:a"], - "//conditions:default": [":a"], - })`, - "tags": `[ - "apex_available=//apex_available:platform", - "apex_available=apex_b", - ]`, - }), - MakeBazelTarget("cc_library_shared", "c", AttrNameToString{ - "implementation_dynamic_deps": `[":a"]`, - "tags": `[ - "apex_available=//apex_available:platform", - "apex_available=apex_a", - "apex_available=apex_b", - ]`, - }), - }, - }) -} - -func TestCcLibrarySharedSystemSharedLibsSharedEmpty(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared system_shared_libs empty shared default", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_defaults { - name: "empty_defaults", - shared: { - system_shared_libs: [], - }, - include_build_directory: false, -} -cc_library_shared { - name: "empty", - defaults: ["empty_defaults"], -} -`, - ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_shared", "empty", AttrNameToString{ - "system_dynamic_deps": "[]", - })}, - }) -} - -func TestCcLibrarySharedConvertLex(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with lex files", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - Filesystem: map[string]string{ - "foo.c": "", - "bar.cc": "", - "foo1.l": "", - "bar1.ll": "", - "foo2.l": "", - "bar2.ll": "", - }, - Blueprint: `cc_library_shared { - name: "foo_lib", - srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"], - lex: { flags: ["--foo_flags"] }, - include_build_directory: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{ - "srcs": `[ - "foo1.l", - "foo2.l", - ]`, - "lexopts": `["--foo_flags"]`, - }), - MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{ - "srcs": `[ - "bar1.ll", - "bar2.ll", - ]`, - "lexopts": `["--foo_flags"]`, - }), - MakeBazelTarget("cc_library_shared", "foo_lib", AttrNameToString{ - "srcs": `[ - "bar.cc", - ":foo_lib_genlex_ll", - ]`, - "srcs_c": `[ - "foo.c", - ":foo_lib_genlex_l", - ]`, - }), - }, - }) -} - -func TestCcLibrarySharedClangUnknownFlags(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Blueprint: soongCcProtoPreamble + `cc_library_shared { - name: "foo", - conlyflags: ["-a", "-finline-functions"], - cflags: ["-b","-finline-functions"], - cppflags: ["-c", "-finline-functions"], - ldflags: ["-d","-finline-functions", "-e"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "conlyflags": `["-a"]`, - "copts": `["-b"]`, - "cppflags": `["-c"]`, - "linkopts": `[ - "-d", - "-e", - ]`, - }), - }, - }) -} - -func TestCCLibraryFlagSpaceSplitting(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Blueprint: soongCcProtoPreamble + `cc_library_shared { - name: "foo", - conlyflags: [ "-include header.h"], - cflags: ["-include header.h"], - cppflags: ["-include header.h"], - version_script: "version_script", - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "additional_linker_inputs": `["version_script"]`, - "conlyflags": `[ - "-include", - "header.h", - ]`, - "copts": `[ - "-include", - "header.h", - ]`, - "cppflags": `[ - "-include", - "header.h", - ]`, - "linkopts": `["-Wl,--version-script,$(location version_script)"]`, - "features": `["android_cfi_exports_map"]`, - }), - }, - }) -} - -func TestCCLibrarySharedRuntimeDeps(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Blueprint: `cc_library_shared { - name: "bar", -} - -cc_library_shared { - name: "foo", - runtime_libs: ["bar"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{ - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "runtime_deps": `[":bar"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedEmptySuffix(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with empty suffix", - Filesystem: map[string]string{ - "foo.c": "", - }, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "foo_shared", - suffix: "", - srcs: ["foo.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "srcs_c": `["foo.c"]`, - "suffix": `""`, - }), - }, - }) -} - -func TestCcLibrarySharedSuffix(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with suffix", - Filesystem: map[string]string{ - "foo.c": "", - }, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "foo_shared", - suffix: "-suf", - srcs: ["foo.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "srcs_c": `["foo.c"]`, - "suffix": `"-suf"`, - }), - }, - }) -} - -func TestCcLibrarySharedArchVariantSuffix(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with arch-variant suffix", - Filesystem: map[string]string{ - "foo.c": "", - }, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "foo_shared", - arch: { - arm64: { suffix: "-64" }, - arm: { suffix: "-32" }, - }, - srcs: ["foo.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{ - "srcs_c": `["foo.c"]`, - "suffix": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "-32", - "//build/bazel_common_rules/platforms/arch:arm64": "-64", - "//conditions:default": None, - })`, - }), - }, - }) -} - -func TestCcLibrarySharedWithSyspropSrcs(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with sysprop sources", - Blueprint: ` -cc_library_shared { - name: "foo", - srcs: [ - "bar.sysprop", - "baz.sysprop", - "blah.cpp", - ], - min_sdk_version: "5", -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `[ - "bar.sysprop", - "baz.sysprop", - ]`, - }), - MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithSyspropSrcsSomeConfigs(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with sysprop sources in some configs but not others", - Blueprint: ` -cc_library_shared { - name: "foo", - srcs: [ - "blah.cpp", - ], - target: { - android: { - srcs: ["bar.sysprop"], - }, - }, - min_sdk_version: "5", -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `select({ - "//build/bazel_common_rules/platforms/os:android": ["bar.sysprop"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":foo_cc_sysprop_library_static"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibrarySharedHeaderAbiChecker(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with header abi checker", - Blueprint: `cc_library_shared { - name: "foo", - header_abi_checker: { - enabled: true, - symbol_file: "a.map.txt", - exclude_symbol_versions: [ - "29", - "30", - ], - exclude_symbol_tags: [ - "tag1", - "tag2", - ], - check_all_apis: true, - diff_flags: ["-allow-adding-removing-weak-symbols"], - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "abi_checker_enabled": `True`, - "abi_checker_symbol_file": `"a.map.txt"`, - "abi_checker_exclude_symbol_versions": `[ - "29", - "30", - ]`, - "abi_checker_exclude_symbol_tags": `[ - "tag1", - "tag2", - ]`, - "abi_checker_check_all_apis": `True`, - "abi_checker_diff_flags": `["-allow-adding-removing-weak-symbols"]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithIntegerOverflowProperty(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when integer_overflow property is provided", - Blueprint: ` -cc_library_shared { - name: "foo", - sanitize: { - integer_overflow: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["ubsan_integer_overflow"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithMiscUndefinedProperty(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when misc_undefined property is provided", - Blueprint: ` -cc_library_shared { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithUBSanPropertiesArchSpecific(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct feature select when UBSan props are specified in arch specific blocks", - Blueprint: ` -cc_library_shared { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, - target: { - android: { - sanitize: { - misc_undefined: ["alignment"], - }, - }, - linux_glibc: { - sanitize: { - integer_overflow: true, - }, - }, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ] + select({ - "//build/bazel_common_rules/platforms/os:android": ["ubsan_alignment"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["ubsan_integer_overflow"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithSanitizerBlocklist(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when sanitize.blocklist is provided", - Blueprint: ` -cc_library_shared { - name: "foo", - sanitize: { - blocklist: "foo_blocklist.txt", - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "copts": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"], - "//conditions:default": [], - })`, - "additional_compiler_inputs": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithThinLto(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when thin lto is enabled", - Blueprint: ` -cc_library_shared { - name: "foo", - lto: { - thin: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["android_thin_lto"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithLtoNever(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when thin lto is enabled", - Blueprint: ` -cc_library_shared { - name: "foo", - lto: { - never: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["-android_thin_lto"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithThinLtoArchSpecific(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when LTO differs across arch and os variants", - Blueprint: ` -cc_library_shared { - name: "foo", - target: { - android: { - lto: { - thin: true, - }, - }, - }, - arch: { - riscv64: { - lto: { - thin: false, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_riscv64": ["-android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_thin_lto"], - "//conditions:default": [], - })`}), - }, - }) -} - -func TestCcLibrarySharedWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared with thin lto disabled by default but enabled on a particular variant", - Blueprint: ` -cc_library_shared { - name: "foo", - lto: { - never: true, - }, - target: { - android: { - lto: { - thin: true, - never: false, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_thin_lto"], - "//conditions:default": ["-android_thin_lto"], - })`, - }), - }, - }) -} - -func TestCcLibrarySharedWithThinLtoAndWholeProgramVtables(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when thin LTO is enabled with whole_program_vtables", - Blueprint: ` -cc_library_shared { - name: "foo", - lto: { - thin: true, - }, - whole_program_vtables: true, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `[ - "android_thin_lto", - "android_thin_lto_whole_program_vtables", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedHiddenVisibilityConvertedToFeature(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared changes hidden visibility flag to feature", - Blueprint: ` -cc_library_shared{ - name: "foo", - cflags: ["-fvisibility=hidden"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["visibility_hidden"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared changes hidden visibility flag to feature for specific os", - Blueprint: ` -cc_library_shared{ - name: "foo", - target: { - android: { - cflags: ["-fvisibility=hidden"], - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["visibility_hidden"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedStubsDessertVersionConversion(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared converts dessert codename versions to numerical versions", - Blueprint: ` -cc_library_shared { - name: "a", - include_build_directory: false, - stubs: { - symbol_file: "a.map.txt", - versions: [ - "Q", - "R", - "31", - ], - }, -} -cc_library_shared { - name: "b", - include_build_directory: false, - stubs: { - symbol_file: "b.map.txt", - versions: [ - "Q", - "R", - "31", - "current", - ], - }, -} -`, - ExpectedBazelTargets: []string{ - makeCcStubSuiteTargets("a", AttrNameToString{ - "api_surface": `"module-libapi"`, - "soname": `"a.so"`, - "source_library_label": `"//:a"`, - "stubs_symbol_file": `"a.map.txt"`, - "stubs_versions": `[ - "29", - "30", - "31", - "current", - ]`, - }), - MakeBazelTarget("cc_library_shared", "a", AttrNameToString{ - "stubs_symbol_file": `"a.map.txt"`, - }), - makeCcStubSuiteTargets("b", AttrNameToString{ - "api_surface": `"module-libapi"`, - "soname": `"b.so"`, - "source_library_label": `"//:b"`, - "stubs_symbol_file": `"b.map.txt"`, - "stubs_versions": `[ - "29", - "30", - "31", - "current", - ]`, - }), - MakeBazelTarget("cc_library_shared", "b", AttrNameToString{ - "stubs_symbol_file": `"b.map.txt"`, - }), - }, - }) -} - -func TestCcLibrarySharedWithCfi(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when cfi is enabled for specific variants", - Blueprint: ` -cc_library_shared { - name: "foo", - sanitize: { - cfi: true, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["android_cfi"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithCfiOsSpecific(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when cfi is enabled", - Blueprint: ` -cc_library_shared { - name: "foo", - target: { - android: { - sanitize: { - cfi: true, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_cfi"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedWithCfiAndCfiAssemblySupport(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared has correct features when cfi is enabled with cfi assembly support", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - cfi: true, - config: { - cfi_assembly_support: true, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `[ - "android_cfi", - "android_cfi_assembly_support", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibrarySharedExplicitlyDisablesCfiWhenFalse(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared disables cfi when explciitly set to false in the bp", - Blueprint: ` -cc_library_shared { - name: "foo", - sanitize: { - cfi: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "features": `["-android_cfi"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCCLibrarySharedRscriptSrc(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: ``, - Blueprint: ` -cc_library_shared{ - name : "foo", - srcs : [ - "ccSrc.cc", - "rsSrc.rscript", - ], - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("rscript_to_cpp", "foo_renderscript", AttrNameToString{ - "srcs": `["rsSrc.rscript"]`, - }), - MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{ - "absolute_includes": `[ - "frameworks/rs", - "frameworks/rs/cpp", - ]`, - "local_includes": `["."]`, - "srcs": `[ - "ccSrc.cc", - "foo_renderscript", - ]`, - })}}) -} - -func TestCcLibrarySdkVariantUsesStubs(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_shared stubs", - ModuleTypeUnderTest: "cc_library_shared", - ModuleTypeUnderTestFactory: cc.LibrarySharedFactory, - StubbedBuildDefinitions: []string{"libNoStubs", "libHasApexStubs", "libHasApexAndNdkStubs", "libHasApexAndNdkStubs.ndk_stub_libs"}, - Blueprint: soongCcLibrarySharedPreamble + ` -cc_library_shared { - name: "libUsesSdk", - sdk_version: "current", - shared_libs: [ - "libNoStubs", - "libHasApexStubs", - "libHasApexAndNdkStubs", - ] -} -cc_library_shared { - name: "libNoStubs", -} -cc_library_shared { - name: "libHasApexStubs", - stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["apex_a"], -} -cc_library_shared { - name: "libHasApexAndNdkStubs", - stubs: { symbol_file: "b.map.txt", versions: ["28", "29", "current"] }, - apex_available: ["apex_b"], -} -ndk_library { - name: "libHasApexAndNdkStubs", - first_version: "28", -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "libUsesSdk", AttrNameToString{ - "implementation_dynamic_deps": `[":libNoStubs"] + select({ - "//build/bazel/rules/apex:system": [ - "@api_surfaces//module-libapi/current:libHasApexStubs", - "@api_surfaces//module-libapi/current:libHasApexAndNdkStubs", - ], - "//build/bazel/rules/apex:unbundled_app": [ - ":libHasApexStubs", - "//.:libHasApexAndNdkStubs.ndk_stub_libs-current", - ], - "//conditions:default": [ - ":libHasApexStubs", - ":libHasApexAndNdkStubs", - ], - })`, - "local_includes": `["."]`, - "sdk_version": `"current"`, - "deps": `select({ - "//build/bazel/rules/apex:unbundled_app": ["//build/bazel/rules/cc:ndk_sysroot"], - "//conditions:default": [], - })`, - }), - }, - }) -} diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go deleted file mode 100644 index d7bbd6810..000000000 --- a/bp2build/cc_library_static_conversion_test.go +++ /dev/null @@ -1,2295 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/android" - "android/soong/cc" - "android/soong/genrule" -) - -const ( - // See cc/testing.go for more context - soongCcLibraryStaticPreamble = ` -cc_defaults { - name: "linux_bionic_supported", -}` -) - -func TestCcLibraryStaticLoadStatement(t *testing.T) { - testCases := []struct { - bazelTargets BazelTargets - expectedLoadStatements string - }{ - { - bazelTargets: BazelTargets{ - BazelTarget{ - name: "cc_library_static_target", - ruleClass: "cc_library_static", - // NOTE: No bzlLoadLocation for native rules - }, - }, - expectedLoadStatements: ``, - }, - } - - for _, testCase := range testCases { - actual := testCase.bazelTargets.LoadStatements() - expected := testCase.expectedLoadStatements - if actual != expected { - t.Fatalf("Expected load statements to be %s, got %s", expected, actual) - } - } -} - -func registerCcLibraryStaticModuleTypes(ctx android.RegistrationContext) { - cc.RegisterCCBuildComponents(ctx) - ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) - ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) - // Required for system_shared_libs dependencies. - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) -} - -func runCcLibraryStaticTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - - (&tc).ModuleTypeUnderTest = "cc_library_static" - (&tc).ModuleTypeUnderTestFactory = cc.LibraryStaticFactory - RunBp2BuildTestCase(t, registerCcLibraryStaticModuleTypes, tc) -} - -func TestCcLibraryStaticSimple(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static test", - Filesystem: map[string]string{ - // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?) - "include_dir_1/include_dir_1_a.h": "", - "include_dir_1/include_dir_1_b.h": "", - "include_dir_2/include_dir_2_a.h": "", - "include_dir_2/include_dir_2_b.h": "", - // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?) - "local_include_dir_1/local_include_dir_1_a.h": "", - "local_include_dir_1/local_include_dir_1_b.h": "", - "local_include_dir_2/local_include_dir_2_a.h": "", - "local_include_dir_2/local_include_dir_2_b.h": "", - // NOTE: export_include_dir headers *should* appear in Bazel hdrs later - "export_include_dir_1/export_include_dir_1_a.h": "", - "export_include_dir_1/export_include_dir_1_b.h": "", - "export_include_dir_2/export_include_dir_2_a.h": "", - "export_include_dir_2/export_include_dir_2_b.h": "", - // NOTE: Soong implicitly includes headers in the current directory - "implicit_include_1.h": "", - "implicit_include_2.h": "", - }, - StubbedBuildDefinitions: []string{"header_lib_1", "header_lib_2", - "static_lib_1", "static_lib_2", "whole_static_lib_1", "whole_static_lib_2"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_headers { - name: "header_lib_1", - export_include_dirs: ["header_lib_1"], -} - -cc_library_headers { - name: "header_lib_2", - export_include_dirs: ["header_lib_2"], -} - -cc_library_static { - name: "static_lib_1", - srcs: ["static_lib_1.cc"], -} - -cc_library_static { - name: "static_lib_2", - srcs: ["static_lib_2.cc"], -} - -cc_library_static { - name: "whole_static_lib_1", - srcs: ["whole_static_lib_1.cc"], -} - -cc_library_static { - name: "whole_static_lib_2", - srcs: ["whole_static_lib_2.cc"], -} - -cc_library_static { - name: "foo_static", - srcs: [ - "foo_static1.cc", - "foo_static2.cc", - ], - cflags: [ - "-Dflag1", - "-Dflag2" - ], - static_libs: [ - "static_lib_1", - "static_lib_2" - ], - whole_static_libs: [ - "whole_static_lib_1", - "whole_static_lib_2" - ], - include_dirs: [ - "include_dir_1", - "include_dir_2", - ], - local_include_dirs: [ - "local_include_dir_1", - "local_include_dir_2", - ], - export_include_dirs: [ - "export_include_dir_1", - "export_include_dir_2" - ], - header_libs: [ - "header_lib_1", - "header_lib_2" - ], - sdk_version: "current", - min_sdk_version: "29", - - // TODO: Also support export_header_lib_headers -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "absolute_includes": `[ - "include_dir_1", - "include_dir_2", - ]`, - "copts": `[ - "-Dflag1", - "-Dflag2", - ]`, - "export_includes": `[ - "export_include_dir_1", - "export_include_dir_2", - ]`, - "implementation_deps": `[ - ":header_lib_1", - ":header_lib_2", - ":static_lib_1", - ":static_lib_2", - ]`, - "local_includes": `[ - "local_include_dir_1", - "local_include_dir_2", - ".", - ]`, - "srcs": `[ - "foo_static1.cc", - "foo_static2.cc", - ]`, - "whole_archive_deps": `[ - ":whole_static_lib_1", - ":whole_static_lib_2", - ]`, - "sdk_version": `"current"`, - "min_sdk_version": `"29"`, - "deps": `select({ - "//build/bazel/rules/apex:unbundled_app": ["//build/bazel/rules/cc:ndk_sysroot"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticSubpackage(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static subpackage test", - Filesystem: map[string]string{ - // subpackage with subdirectory - "subpackage/Android.bp": "", - "subpackage/subpackage_header.h": "", - "subpackage/subdirectory/subdirectory_header.h": "", - // subsubpackage with subdirectory - "subpackage/subsubpackage/Android.bp": "", - "subpackage/subsubpackage/subsubpackage_header.h": "", - "subpackage/subsubpackage/subdirectory/subdirectory_header.h": "", - // subsubsubpackage with subdirectory - "subpackage/subsubpackage/subsubsubpackage/Android.bp": "", - "subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "", - "subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: [], - include_dirs: [ - "subpackage", - ], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "absolute_includes": `["subpackage"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticExportIncludeDir(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static export include dir", - Filesystem: map[string]string{ - // subpackage with subdirectory - "subpackage/Android.bp": "", - "subpackage/subpackage_header.h": "", - "subpackage/subdirectory/subdirectory_header.h": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - export_include_dirs: ["subpackage"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "export_includes": `["subpackage"]`, - }), - }, - }) -} - -func TestCcLibraryStaticExportSystemIncludeDir(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static export system include dir", - Filesystem: map[string]string{ - // subpackage with subdirectory - "subpackage/Android.bp": "", - "subpackage/subpackage_header.h": "", - "subpackage/subdirectory/subdirectory_header.h": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - export_system_include_dirs: ["subpackage"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "export_system_includes": `["subpackage"]`, - }), - }, - }) -} - -func TestCcLibraryStaticManyIncludeDirs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)", - Dir: "subpackage", - Filesystem: map[string]string{ - // subpackage with subdirectory - "subpackage/Android.bp": ` -cc_library_static { - name: "foo_static", - // include_dirs are workspace/root relative - include_dirs: [ - "subpackage/subsubpackage", - "subpackage2", - "subpackage3/subsubpackage" - ], - local_include_dirs: ["subsubpackage2"], // module dir relative - export_include_dirs: ["./exported_subsubpackage"], // module dir relative - include_build_directory: true, - bazel_module: { bp2build_available: true }, -}`, - "subpackage/subsubpackage/header.h": "", - "subpackage/subsubpackage2/header.h": "", - "subpackage/exported_subsubpackage/header.h": "", - "subpackage2/header.h": "", - "subpackage3/subsubpackage/header.h": "", - }, - Blueprint: soongCcLibraryStaticPreamble, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "absolute_includes": `[ - "subpackage/subsubpackage", - "subpackage2", - "subpackage3/subsubpackage", - ]`, - "export_includes": `["./exported_subsubpackage"]`, - "local_includes": `[ - "subsubpackage2", - ".", - ]`, - })}, - }) -} - -func TestCcLibraryStaticIncludeBuildDirectoryDisabled(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static include_build_directory disabled", - Filesystem: map[string]string{ - // subpackage with subdirectory - "subpackage/Android.bp": "", - "subpackage/subpackage_header.h": "", - "subpackage/subdirectory/subdirectory_header.h": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended - local_include_dirs: ["subpackage2"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "absolute_includes": `["subpackage"]`, - "local_includes": `["subpackage2"]`, - }), - }, - }) -} - -func TestCcLibraryStaticIncludeBuildDirectoryEnabled(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static include_build_directory enabled", - Filesystem: map[string]string{ - // subpackage with subdirectory - "subpackage/Android.bp": "", - "subpackage/subpackage_header.h": "", - "subpackage2/Android.bp": "", - "subpackage2/subpackage2_header.h": "", - "subpackage/subdirectory/subdirectory_header.h": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended - local_include_dirs: ["subpackage2"], - include_build_directory: true, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "absolute_includes": `["subpackage"]`, - "local_includes": `[ - "subpackage2", - ".", - ]`, - }), - }, - }) -} - -func TestCcLibraryStaticArchSpecificStaticLib(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static arch-specific static_libs", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"static_dep", "static_dep2"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "static_dep", -} -cc_library_static { - name: "static_dep2", -} -cc_library_static { - name: "foo_static", - arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "implementation_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":static_dep"], - "//conditions:default": [], - })`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":static_dep2"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticOsSpecificStaticLib(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static os-specific static_libs", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"static_dep", "static_dep2"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "static_dep", -} -cc_library_static { - name: "static_dep2", -} -cc_library_static { - name: "foo_static", - target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "implementation_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":static_dep"], - "//conditions:default": [], - })`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":static_dep2"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticBaseArchOsSpecificStaticLib(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static base, arch and os-specific static_libs", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"static_dep", "static_dep2", "static_dep3", - "static_dep4"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "static_dep", -} -cc_library_static { - name: "static_dep2", -} -cc_library_static { - name: "static_dep3", -} -cc_library_static { - name: "static_dep4", -} -cc_library_static { - name: "foo_static", - static_libs: ["static_dep"], - whole_static_libs: ["static_dep2"], - target: { android: { static_libs: ["static_dep3"] } }, - arch: { arm64: { static_libs: ["static_dep4"] } }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "implementation_deps": `[":static_dep"] + select({ - "//build/bazel_common_rules/platforms/arch:arm64": [":static_dep4"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [":static_dep3"], - "//conditions:default": [], - })`, - "whole_archive_deps": `[":static_dep2"]`, - }), - }, - }) -} - -func TestCcLibraryStaticSimpleExcludeSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static simple exclude_srcs", - Filesystem: map[string]string{ - "common.c": "", - "foo-a.c": "", - "foo-excluded.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c", "foo-*.c"], - exclude_srcs: ["foo-excluded.c"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `[ - "common.c", - "foo-a.c", - ]`, - }), - }, - }) -} - -func TestCcLibraryStaticOneArchSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static one arch specific srcs", - Filesystem: map[string]string{ - "common.c": "", - "foo-arm.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c"], - arch: { arm: { srcs: ["foo-arm.c"] } }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["common.c"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["foo-arm.c"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticOneArchSrcsExcludeSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static one arch specific srcs and exclude_srcs", - Filesystem: map[string]string{ - "common.c": "", - "for-arm.c": "", - "not-for-arm.c": "", - "not-for-anything.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c", "not-for-*.c"], - exclude_srcs: ["not-for-anything.c"], - arch: { - arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["common.c"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["for-arm.c"], - "//conditions:default": ["not-for-arm.c"], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticTwoArchExcludeSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static arch specific exclude_srcs for 2 architectures", - Filesystem: map[string]string{ - "common.c": "", - "for-arm.c": "", - "for-x86.c": "", - "not-for-arm.c": "", - "not-for-x86.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c", "not-for-*.c"], - exclude_srcs: ["not-for-everything.c"], - arch: { - arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] }, - x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] }, - }, - include_build_directory: false, -} `, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["common.c"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "not-for-x86.c", - "for-arm.c", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - "not-for-arm.c", - "for-x86.c", - ], - "//conditions:default": [ - "not-for-arm.c", - "not-for-x86.c", - ], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticFourArchExcludeSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static arch specific exclude_srcs for 4 architectures", - Filesystem: map[string]string{ - "common.c": "", - "for-arm.c": "", - "for-arm64.c": "", - "for-x86.c": "", - "for-x86_64.c": "", - "not-for-arm.c": "", - "not-for-arm64.c": "", - "not-for-x86.c": "", - "not-for-x86_64.c": "", - "not-for-everything.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c", "not-for-*.c"], - exclude_srcs: ["not-for-everything.c"], - arch: { - arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] }, - arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] }, - x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] }, - x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] }, - }, - include_build_directory: false, -} `, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["common.c"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "not-for-arm64.c", - "not-for-x86.c", - "not-for-x86_64.c", - "for-arm.c", - ], - "//build/bazel_common_rules/platforms/arch:arm64": [ - "not-for-arm.c", - "not-for-x86.c", - "not-for-x86_64.c", - "for-arm64.c", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - "not-for-arm.c", - "not-for-arm64.c", - "not-for-x86_64.c", - "for-x86.c", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - "not-for-arm.c", - "not-for-arm64.c", - "not-for-x86.c", - "for-x86_64.c", - ], - "//conditions:default": [ - "not-for-arm.c", - "not-for-arm64.c", - "not-for-x86.c", - "not-for-x86_64.c", - ], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticOneArchEmpty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static one arch empty", - Filesystem: map[string]string{ - "common.cc": "", - "foo-no-arm.cc": "", - "foo-excluded.cc": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.cc", "foo-*.cc"], - exclude_srcs: ["foo-excluded.cc"], - arch: { - arm: { exclude_srcs: ["foo-no-arm.cc"] }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs": `["common.cc"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [], - "//conditions:default": ["foo-no-arm.cc"], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticOneArchEmptyOtherSet(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static one arch empty other set", - Filesystem: map[string]string{ - "common.cc": "", - "foo-no-arm.cc": "", - "x86-only.cc": "", - "foo-excluded.cc": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.cc", "foo-*.cc"], - exclude_srcs: ["foo-excluded.cc"], - arch: { - arm: { exclude_srcs: ["foo-no-arm.cc"] }, - x86: { srcs: ["x86-only.cc"] }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs": `["common.cc"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [], - "//build/bazel_common_rules/platforms/arch:x86": [ - "foo-no-arm.cc", - "x86-only.cc", - ], - "//conditions:default": ["foo-no-arm.cc"], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticMultipleDepSameName(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static multiple dep same name panic", - Filesystem: map[string]string{}, - StubbedBuildDefinitions: []string{"static_dep"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "static_dep", -} -cc_library_static { - name: "foo_static", - static_libs: ["static_dep", "static_dep"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "implementation_deps": `[":static_dep"]`, - }), - }, - }) -} - -func TestCcLibraryStaticOneMultilibSrcsExcludeSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static 1 multilib srcs and exclude_srcs", - Filesystem: map[string]string{ - "common.c": "", - "for-lib32.c": "", - "not-for-lib32.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c", "not-for-*.c"], - multilib: { - lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] }, - }, - include_build_directory: false, -} `, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["common.c"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["for-lib32.c"], - "//build/bazel_common_rules/platforms/arch:x86": ["for-lib32.c"], - "//conditions:default": ["not-for-lib32.c"], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticTwoMultilibSrcsExcludeSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static 2 multilib srcs and exclude_srcs", - Filesystem: map[string]string{ - "common.c": "", - "for-lib32.c": "", - "for-lib64.c": "", - "not-for-lib32.c": "", - "not-for-lib64.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c", "not-for-*.c"], - multilib: { - lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] }, - lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] }, - }, - include_build_directory: false, -} `, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["common.c"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "not-for-lib64.c", - "for-lib32.c", - ], - "//build/bazel_common_rules/platforms/arch:arm64": [ - "not-for-lib32.c", - "for-lib64.c", - ], - "//build/bazel_common_rules/platforms/arch:riscv64": [ - "not-for-lib32.c", - "for-lib64.c", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - "not-for-lib64.c", - "for-lib32.c", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - "not-for-lib32.c", - "for-lib64.c", - ], - "//conditions:default": [ - "not-for-lib32.c", - "not-for-lib64.c", - ], - })`, - }), - }, - }) -} - -func TestCcLibrarySTaticArchMultilibSrcsExcludeSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static arch and multilib srcs and exclude_srcs", - Filesystem: map[string]string{ - "common.c": "", - "for-arm.c": "", - "for-arm64.c": "", - "for-x86.c": "", - "for-x86_64.c": "", - "for-lib32.c": "", - "for-lib64.c": "", - "not-for-arm.c": "", - "not-for-arm64.c": "", - "not-for-riscv64.c": "", - "not-for-x86.c": "", - "not-for-x86_64.c": "", - "not-for-lib32.c": "", - "not-for-lib64.c": "", - "not-for-everything.c": "", - }, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c", "not-for-*.c"], - exclude_srcs: ["not-for-everything.c"], - arch: { - arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] }, - arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] }, - riscv64: { srcs: ["for-riscv64.c"], exclude_srcs: ["not-for-riscv64.c"] }, - x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] }, - x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] }, - }, - multilib: { - lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] }, - lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `["common.c"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": [ - "not-for-arm64.c", - "not-for-lib64.c", - "not-for-riscv64.c", - "not-for-x86.c", - "not-for-x86_64.c", - "for-arm.c", - "for-lib32.c", - ], - "//build/bazel_common_rules/platforms/arch:arm64": [ - "not-for-arm.c", - "not-for-lib32.c", - "not-for-riscv64.c", - "not-for-x86.c", - "not-for-x86_64.c", - "for-arm64.c", - "for-lib64.c", - ], - "//build/bazel_common_rules/platforms/arch:riscv64": [ - "not-for-arm.c", - "not-for-arm64.c", - "not-for-lib32.c", - "not-for-x86.c", - "not-for-x86_64.c", - "for-riscv64.c", - "for-lib64.c", - ], - "//build/bazel_common_rules/platforms/arch:x86": [ - "not-for-arm.c", - "not-for-arm64.c", - "not-for-lib64.c", - "not-for-riscv64.c", - "not-for-x86_64.c", - "for-x86.c", - "for-lib32.c", - ], - "//build/bazel_common_rules/platforms/arch:x86_64": [ - "not-for-arm.c", - "not-for-arm64.c", - "not-for-lib32.c", - "not-for-riscv64.c", - "not-for-x86.c", - "for-x86_64.c", - "for-lib64.c", - ], - "//conditions:default": [ - "not-for-arm.c", - "not-for-arm64.c", - "not-for-lib32.c", - "not-for-lib64.c", - "not-for-riscv64.c", - "not-for-x86.c", - "not-for-x86_64.c", - ], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticGeneratedHeadersAllPartitions(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"generated_hdr", "export_generated_hdr"}, - Blueprint: soongCcLibraryStaticPreamble + ` -genrule { - name: "generated_hdr", - cmd: "nothing to see here", -} - -genrule { - name: "export_generated_hdr", - cmd: "nothing to see here", -} - -cc_library_static { - name: "foo_static", - srcs: ["cpp_src.cpp", "as_src.S", "c_src.c"], - generated_headers: ["generated_hdr", "export_generated_hdr"], - export_generated_headers: ["export_generated_hdr"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "export_includes": `["."]`, - "local_includes": `["."]`, - "hdrs": `[":export_generated_hdr"]`, - "srcs": `[ - "cpp_src.cpp", - ":generated_hdr", - ]`, - "srcs_as": `[ - "as_src.S", - ":generated_hdr", - ]`, - "srcs_c": `[ - "c_src.c", - ":generated_hdr", - ]`, - }), - }, - }) -} - -func TestCcLibraryStaticGeneratedHeadersMultipleExports(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"generated_hdr", "export_generated_hdr"}, - Blueprint: soongCcLibraryStaticPreamble + ` -genrule { - name: "generated_hdr", - cmd: "nothing to see here", - export_include_dirs: ["foo", "bar"], -} - -genrule { - name: "export_generated_hdr", - cmd: "nothing to see here", - export_include_dirs: ["a", "b"], -} - -cc_library_static { - name: "foo_static", - generated_headers: ["generated_hdr", "export_generated_hdr"], - export_generated_headers: ["export_generated_hdr"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "deps": `[":export_generated_hdr__header_library"]`, - "implementation_deps": `[":generated_hdr__header_library"]`, - }), - }, - }) -} - -// generated_headers has "variant_prepend" tag. In bp2build output, -// variant info(select) should go before general info. -func TestCcLibraryStaticArchSrcsExcludeSrcsGeneratedFiles(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static arch srcs/exclude_srcs with generated files", - StubbedBuildDefinitions: []string{"//dep:generated_src_other_pkg", "//dep:generated_hdr_other_pkg", - "//dep:generated_src_other_pkg_x86", "//dep:generated_hdr_other_pkg_x86", "//dep:generated_hdr_other_pkg_android", - "generated_src", "generated_src_not_x86", "generated_src_android", "generated_hdr", - }, - Filesystem: map[string]string{ - "common.cpp": "", - "for-x86.cpp": "", - "not-for-x86.cpp": "", - "not-for-everything.cpp": "", - "dep/Android.bp": simpleModule("genrule", "generated_src_other_pkg") + - simpleModule("genrule", "generated_hdr_other_pkg") + - simpleModule("genrule", "generated_src_other_pkg_x86") + - simpleModule("genrule", "generated_hdr_other_pkg_x86") + - simpleModule("genrule", "generated_hdr_other_pkg_android"), - }, - Blueprint: soongCcLibraryStaticPreamble + - simpleModule("genrule", "generated_src") + - simpleModule("genrule", "generated_src_not_x86") + - simpleModule("genrule", "generated_src_android") + - simpleModule("genrule", "generated_hdr") + ` -cc_library_static { - name: "foo_static", - srcs: ["common.cpp", "not-for-*.cpp"], - exclude_srcs: ["not-for-everything.cpp"], - generated_sources: ["generated_src", "generated_src_other_pkg", "generated_src_not_x86"], - generated_headers: ["generated_hdr", "generated_hdr_other_pkg"], - export_generated_headers: ["generated_hdr_other_pkg"], - arch: { - x86: { - srcs: ["for-x86.cpp"], - exclude_srcs: ["not-for-x86.cpp"], - generated_headers: ["generated_hdr_other_pkg_x86"], - exclude_generated_sources: ["generated_src_not_x86"], - export_generated_headers: ["generated_hdr_other_pkg_x86"], - }, - }, - target: { - android: { - generated_sources: ["generated_src_android"], - generated_headers: ["generated_hdr_other_pkg_android"], - export_generated_headers: ["generated_hdr_other_pkg_android"], - }, - }, - - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs": `[ - "common.cpp", - ":generated_src", - "//dep:generated_src_other_pkg", - ":generated_hdr", - ] + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["for-x86.cpp"], - "//conditions:default": [ - "not-for-x86.cpp", - ":generated_src_not_x86", - ], - }) + select({ - "//build/bazel_common_rules/platforms/os:android": [":generated_src_android"], - "//conditions:default": [], - })`, - "hdrs": `select({ - "//build/bazel_common_rules/platforms/os:android": ["//dep:generated_hdr_other_pkg_android"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["//dep:generated_hdr_other_pkg_x86"], - "//conditions:default": [], - }) + ["//dep:generated_hdr_other_pkg"]`, - "local_includes": `["."]`, - "export_absolute_includes": `["dep"]`, - }), - }, - }) -} - -func TestCcLibraryStaticGetTargetProperties(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - - Description: "cc_library_static complex GetTargetProperties", - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - target: { - android: { - srcs: ["android_src.c"], - }, - android_arm: { - srcs: ["android_arm_src.c"], - }, - android_arm64: { - srcs: ["android_arm64_src.c"], - }, - android_x86: { - srcs: ["android_x86_src.c"], - }, - android_x86_64: { - srcs: ["android_x86_64_src.c"], - }, - linux_bionic_arm64: { - srcs: ["linux_bionic_arm64_src.c"], - }, - linux_bionic_x86_64: { - srcs: ["linux_bionic_x86_64_src.c"], - }, - }, - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "srcs_c": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_src.c"], - "//conditions:default": [], - }) + select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_arm_src.c"], - "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_arm64_src.c"], - "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_x86_src.c"], - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_x86_64_src.c"], - "//build/bazel_common_rules/platforms/os_arch:linux_bionic_arm64": ["linux_bionic_arm64_src.c"], - "//build/bazel_common_rules/platforms/os_arch:linux_bionic_x86_64": ["linux_bionic_x86_64_src.c"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticProductVariableSelects(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static product variable selects", - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c"], - product_variables: { - malloc_not_svelte: { - cflags: ["-Wmalloc_not_svelte"], - }, - malloc_zero_contents: { - cflags: ["-Wmalloc_zero_contents"], - }, - binder32bit: { - cflags: ["-Wbinder32bit"], - }, - }, - include_build_directory: false, -} `, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "copts": `select({ - "//build/bazel/product_config/config_settings:binder32bit": ["-Wbinder32bit"], - "//conditions:default": [], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte": ["-Wmalloc_not_svelte"], - "//conditions:default": [], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_zero_contents": ["-Wmalloc_zero_contents"], - "//conditions:default": [], - })`, - "srcs_c": `["common.c"]`, - }), - }, - }) -} - -func TestCcLibraryStaticProductVariableArchSpecificSelects(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static arch-specific product variable selects", - Filesystem: map[string]string{}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.c"], - product_variables: { - malloc_not_svelte: { - cflags: ["-Wmalloc_not_svelte"], - }, - }, - arch: { - arm64: { - product_variables: { - malloc_not_svelte: { - cflags: ["-Warm64_malloc_not_svelte"], - }, - }, - }, - }, - multilib: { - lib32: { - product_variables: { - malloc_not_svelte: { - cflags: ["-Wlib32_malloc_not_svelte"], - }, - }, - }, - }, - target: { - android: { - product_variables: { - malloc_not_svelte: { - cflags: ["-Wandroid_malloc_not_svelte"], - }, - }, - } - }, - include_build_directory: false, -} `, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "copts": `select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte": ["-Wmalloc_not_svelte"], - "//conditions:default": [], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte-android": ["-Wandroid_malloc_not_svelte"], - "//conditions:default": [], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte-arm": ["-Wlib32_malloc_not_svelte"], - "//conditions:default": [], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte-arm64": ["-Warm64_malloc_not_svelte"], - "//conditions:default": [], - }) + select({ - "//build/bazel/product_config/config_settings:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"], - "//conditions:default": [], - })`, - "srcs_c": `["common.c"]`, - }), - }, - }) -} - -func TestCcLibraryStaticProductVariableStringReplacement(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static product variable string replacement", - Filesystem: map[string]string{}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "foo_static", - srcs: ["common.S"], - product_variables: { - platform_sdk_version: { - asflags: ["-DPLATFORM_SDK_VERSION=%d"], - }, - }, - include_build_directory: false, -} `, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{ - "asflags": `select({ - "//build/bazel/product_config/config_settings:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"], - "//conditions:default": [], - })`, - "srcs_as": `["common.S"]`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsRootEmpty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_lib empty root", - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library_static { - name: "root_empty", - system_shared_libs: [], - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "root_empty", AttrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsStaticEmpty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_lib empty static default", - Blueprint: soongCcLibraryStaticPreamble + ` -cc_defaults { - name: "static_empty_defaults", - static: { - system_shared_libs: [], - }, - include_build_directory: false, -} -cc_library_static { - name: "static_empty", - defaults: ["static_empty_defaults"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "static_empty", AttrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsBionicEmpty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_lib empty for bionic variant", - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library_static { - name: "target_bionic_empty", - target: { - bionic: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "target_bionic_empty", AttrNameToString{ - "system_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) { - // Note that this behavior is technically incorrect (it's a simplification). - // The correct behavior would be if bp2build wrote `system_dynamic_deps = []` - // only for linux_bionic, but `android` had `["libc", "libdl", "libm"]. - // b/195791252 tracks the fix. - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_lib empty for linux_bionic variant", - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library_static { - name: "target_linux_bionic_empty", - target: { - linux_bionic: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "target_linux_bionic_empty", AttrNameToString{ - "system_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsMuslEmpty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_lib empty for musl variant", - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library_static { - name: "target_musl_empty", - target: { - musl: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "target_musl_empty", AttrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_lib empty for linux_musl variant", - StubbedBuildDefinitions: []string{"libc_musl"}, - Blueprint: soongCcLibraryStaticPreamble + ` -cc_library { - name: "libc_musl", -} - -cc_library_static { - name: "target_linux_musl_empty", - target: { - linux_musl: { - system_shared_libs: [], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "target_linux_musl_empty", AttrNameToString{ - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsBionic(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_libs set for bionic variant", - StubbedBuildDefinitions: []string{"libc", "libc_musl"}, - Blueprint: soongCcLibraryStaticPreamble + - simpleModule("cc_library", "libc") + ` -cc_library { - name: "libc_musl", -} - -cc_library_static { - name: "target_bionic", - target: { - bionic: { - system_shared_libs: ["libc"], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "target_bionic", AttrNameToString{ - "system_dynamic_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":libc"], - "//build/bazel_common_rules/platforms/os:linux_bionic": [":libc"], - "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestStaticLibrary_SystemSharedLibsLinuxRootAndLinuxBionic(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_libs set for root and linux_bionic variant", - StubbedBuildDefinitions: []string{"libc", "libm", "libc_musl"}, - Blueprint: soongCcLibraryStaticPreamble + - simpleModule("cc_library", "libc") + - simpleModule("cc_library", "libm") + ` -cc_library { - name: "libc_musl", -} - -cc_library_static { - name: "target_linux_bionic", - system_shared_libs: ["libc"], - target: { - linux_bionic: { - system_shared_libs: ["libm"], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "target_linux_bionic", AttrNameToString{ - "system_dynamic_deps": `[":libc"] + select({ - "//build/bazel_common_rules/platforms/os:linux_bionic": [":libm"], - "//build/bazel_common_rules/platforms/os:linux_musl": [":libc_musl"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibrarystatic_SystemSharedLibUsedAsDep(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static system_shared_lib empty for linux_bionic variant", - StubbedBuildDefinitions: []string{"libc", "libm"}, - Blueprint: soongCcLibraryStaticPreamble + - simpleModule("cc_library", "libc") + ` - -cc_library { - name: "libm", - stubs: { - symbol_file: "libm.map.txt", - versions: ["current"], - }, - apex_available: ["com.android.runtime"], -} - -cc_library_static { - name: "used_in_bionic_oses", - target: { - android: { - shared_libs: ["libc"], - }, - linux_bionic: { - shared_libs: ["libc"], - }, - }, - include_build_directory: false, - apex_available: ["foo"], -} - -cc_library_static { - name: "all", - shared_libs: ["libc"], - include_build_directory: false, - apex_available: ["foo"], -} - -cc_library_static { - name: "keep_for_empty_system_shared_libs", - shared_libs: ["libc"], - system_shared_libs: [], - include_build_directory: false, - apex_available: ["foo"], -} - -cc_library_static { - name: "used_with_stubs", - shared_libs: ["libm"], - include_build_directory: false, - apex_available: ["foo"], -} - -cc_library_static { - name: "keep_with_stubs", - shared_libs: ["libm"], - system_shared_libs: [], - include_build_directory: false, - apex_available: ["foo"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "all", AttrNameToString{ - "tags": `["apex_available=foo"]`, - }), - MakeBazelTarget("cc_library_static", "keep_for_empty_system_shared_libs", AttrNameToString{ - "implementation_dynamic_deps": `[":libc"]`, - "system_dynamic_deps": `[]`, - "tags": `["apex_available=foo"]`, - }), - MakeBazelTarget("cc_library_static", "keep_with_stubs", AttrNameToString{ - "implementation_dynamic_deps": `select({ - "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:libm"], - "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:libm"], - "//conditions:default": [":libm"], - })`, - "system_dynamic_deps": `[]`, - "tags": `["apex_available=foo"]`, - }), - MakeBazelTarget("cc_library_static", "used_in_bionic_oses", AttrNameToString{ - "tags": `["apex_available=foo"]`, - }), - MakeBazelTarget("cc_library_static", "used_with_stubs", AttrNameToString{ - "tags": `["apex_available=foo"]`, - }), - }, - }) -} - -func TestCcLibraryStaticProto(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"libprotobuf-cpp-full", "libprotobuf-cpp-lite"}, - Blueprint: soongCcProtoPreamble + `cc_library_static { - name: "foo", - srcs: ["foo.proto"], - proto: { - 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"]`, - }), - }, - }) -} - -func TestCcLibraryStaticUseVersionLib(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Filesystem: map[string]string{ - soongCcVersionLibBpPath: soongCcVersionLibBp, - }, - StubbedBuildDefinitions: []string{"//build/soong/cc/libbuildversion:libbuildversion", "libprotobuf-cpp-full", "libprotobuf-cpp-lite"}, - Blueprint: soongCcProtoPreamble + `cc_library_static { - name: "foo", - use_version_lib: true, - static_libs: ["libbuildversion"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`, - }), - }, - }) -} - -func TestCcLibraryStaticUseVersionLibHasDep(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Filesystem: map[string]string{ - soongCcVersionLibBpPath: soongCcVersionLibBp, - }, - StubbedBuildDefinitions: []string{"//build/soong/cc/libbuildversion:libbuildversion", "libprotobuf-cpp-full", "libprotobuf-cpp-lite"}, - - Blueprint: soongCcProtoPreamble + `cc_library_static { - name: "foo", - use_version_lib: true, - whole_static_libs: ["libbuildversion"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`, - }), - }, - }) -} - -func TestCcLibraryStaticStdInFlags(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - StubbedBuildDefinitions: []string{"libprotobuf-cpp-full", "libprotobuf-cpp-lite"}, - Blueprint: soongCcProtoPreamble + `cc_library_static { - name: "foo", - cflags: ["-std=candcpp"], - conlyflags: ["-std=conly"], - cppflags: ["-std=cpp"], - include_build_directory: false, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "conlyflags": `["-std=conly"]`, - "cppflags": `["-std=cpp"]`, - }), - }, - }) -} - -func TestCcLibraryStaticStl(t *testing.T) { - testCases := []struct { - desc string - prop string - attr AttrNameToString - }{ - { - desc: "c++_shared deduped to libc++", - prop: `stl: "c++_shared",`, - attr: AttrNameToString{ - "stl": `"libc++"`, - }, - }, - { - desc: "libc++ to libc++", - prop: `stl: "libc++",`, - attr: AttrNameToString{ - "stl": `"libc++"`, - }, - }, - { - desc: "c++_static to libc++_static", - prop: `stl: "c++_static",`, - attr: AttrNameToString{ - "stl": `"libc++_static"`, - }, - }, - { - desc: "libc++_static to libc++_static", - prop: `stl: "libc++_static",`, - attr: AttrNameToString{ - "stl": `"libc++_static"`, - }, - }, - { - desc: "system to system", - prop: `stl: "system",`, - attr: AttrNameToString{ - "stl": `"system"`, - }, - }, - { - desc: "none to none", - prop: `stl: "none",`, - attr: AttrNameToString{ - "stl": `"none"`, - }, - }, - { - desc: "empty to empty", - attr: AttrNameToString{}, - }, - } - for _, tc := range testCases { - t.Run(tc.desc, func(*testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Blueprint: fmt.Sprintf(`cc_library_static { - name: "foo", - include_build_directory: false, - %s -}`, tc.prop), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", tc.attr), - }, - }) - }) - } -} - -func TestCCLibraryStaticRuntimeDeps(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Blueprint: `cc_library_shared { - name: "bar", -} - -cc_library_static { - name: "foo", - runtime_libs: ["bar"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{ - "local_includes": `["."]`, - }), - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "runtime_deps": `[":bar"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithSyspropSrcs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static with sysprop sources", - Blueprint: ` -cc_library_static { - name: "foo", - srcs: [ - "bar.sysprop", - "baz.sysprop", - "blah.cpp", - ], - min_sdk_version: "5", -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `[ - "bar.sysprop", - "baz.sysprop", - ]`, - }), - MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }), - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithSyspropSrcsSomeConfigs(t *testing.T) { - runCcLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static with sysprop sources in some configs but not others", - Blueprint: ` -cc_library_static { - name: "foo", - srcs: [ - "blah.cpp", - ], - target: { - android: { - srcs: ["bar.sysprop"], - }, - }, - min_sdk_version: "5", -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{ - "srcs": `select({ - "//build/bazel_common_rules/platforms/os:android": ["bar.sysprop"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{ - "dep": `":foo_sysprop_library"`, - "min_sdk_version": `"5"`, - }), - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "srcs": `["blah.cpp"]`, - "local_includes": `["."]`, - "min_sdk_version": `"5"`, - "whole_archive_deps": `select({ - "//build/bazel_common_rules/platforms/os:android": [":foo_cc_sysprop_library_static"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticWithIntegerOverflowProperty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when integer_overflow property is provided", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - integer_overflow: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `["ubsan_integer_overflow"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithMiscUndefinedProperty(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when misc_undefined property is provided", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithUBSanPropertiesArchSpecific(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct feature select when UBSan props are specified in arch specific blocks", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - misc_undefined: ["undefined", "nullability"], - }, - target: { - android: { - sanitize: { - misc_undefined: ["alignment"], - }, - }, - linux_glibc: { - sanitize: { - integer_overflow: true, - }, - }, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `[ - "ubsan_undefined", - "ubsan_nullability", - ] + select({ - "//build/bazel_common_rules/platforms/os:android": ["ubsan_alignment"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["ubsan_integer_overflow"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithSanitizerBlocklist(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when sanitize.blocklist is provided", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - blocklist: "foo_blocklist.txt", - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "copts": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"], - "//conditions:default": [], - })`, - "additional_compiler_inputs": `select({ - "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithThinLto(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when thin lto is enabled", - Blueprint: ` -cc_library_static { - name: "foo", - lto: { - thin: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `["android_thin_lto"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithLtoNever(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when thin lto is enabled", - Blueprint: ` -cc_library_static { - name: "foo", - lto: { - never: true, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `["-android_thin_lto"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithThinLtoArchSpecific(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when LTO differs across arch and os variants", - Blueprint: ` -cc_library_static { - name: "foo", - target: { - android: { - lto: { - thin: true, - }, - }, - }, - arch: { - riscv64: { - lto: { - thin: false, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_arm64": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_riscv64": ["-android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86": ["android_thin_lto"], - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": ["android_thin_lto"], - "//conditions:default": [], - })`}), - }, - }) -} - -func TestCcLibraryStaticWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when LTO disabled by default but enabled on a particular variant", - Blueprint: ` -cc_library_static { - name: "foo", - lto: { - never: true, - }, - target: { - android: { - lto: { - thin: true, - never: false, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "local_includes": `["."]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_thin_lto"], - "//conditions:default": ["-android_thin_lto"], - })`, - }), - }, - }) -} - -func TestCcLibraryStaticWithThinLtoAndWholeProgramVtables(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when thin lto is enabled with whole_program_vtables", - Blueprint: ` -cc_library_static { - name: "foo", - lto: { - thin: true, - }, - whole_program_vtables: true, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `[ - "android_thin_lto", - "android_thin_lto_whole_program_vtables", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticHiddenVisibilityConvertedToFeature(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static changes hidden visibility flag to feature", - Blueprint: ` -cc_library_static { - name: "foo", - cflags: ["-fvisibility=hidden"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `["visibility_hidden"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static changes hidden visibility flag to feature for specific os", - Blueprint: ` -cc_library_static { - name: "foo", - target: { - android: { - cflags: ["-fvisibility=hidden"], - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["visibility_hidden"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithCfi(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when cfi is enabled", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - cfi: true, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `["android_cfi"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithCfiOsSpecific(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when cfi is enabled for specific variants", - Blueprint: ` -cc_library_static { - name: "foo", - target: { - android: { - sanitize: { - cfi: true, - }, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `select({ - "//build/bazel_common_rules/platforms/os:android": ["android_cfi"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticWithCfiAndCfiAssemblySupport(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static has correct features when cfi is enabled with cfi_assembly_support", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - cfi: true, - config: { - cfi_assembly_support: true, - }, - }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `[ - "android_cfi", - "android_cfi_assembly_support", - ]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCcLibraryStaticExplicitlyDisablesCfiWhenFalse(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static disables cfi when explciitly set to false in the bp", - Blueprint: ` -cc_library_static { - name: "foo", - sanitize: { - cfi: false, - }, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "features": `["-android_cfi"]`, - "local_includes": `["."]`, - }), - }, - }) -} - -func TestCCLibraryStaticRscriptSrc(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: `cc_library_static with rscript files in sources`, - Blueprint: ` -cc_library_static{ - name : "foo", - srcs : [ - "ccSrc.cc", - "rsSrc.rscript", - ], - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("rscript_to_cpp", "foo_renderscript", AttrNameToString{ - "srcs": `["rsSrc.rscript"]`, - }), - MakeBazelTarget("cc_library_static", "foo", AttrNameToString{ - "absolute_includes": `[ - "frameworks/rs", - "frameworks/rs/cpp", - ]`, - "local_includes": `["."]`, - "srcs": `[ - "ccSrc.cc", - "foo_renderscript", - ]`, - })}}) -} - -func TestCcLibraryWithProtoInGeneratedSrcs(t *testing.T) { - runCcLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_library with a .proto file generated from a genrule", - ModuleTypeUnderTest: "cc_library_static", - ModuleTypeUnderTestFactory: cc.LibraryStaticFactory, - StubbedBuildDefinitions: []string{"libprotobuf-cpp-lite"}, - Blueprint: soongCcLibraryPreamble + ` -cc_library_static { - name: "mylib", - generated_sources: ["myprotogen"], -} -genrule { - name: "myprotogen", - out: ["myproto.proto"], -} -` + simpleModule("cc_library", "libprotobuf-cpp-lite"), - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_library_static", "mylib", AttrNameToString{ - "local_includes": `["."]`, - "deps": `[":libprotobuf-cpp-lite"]`, - "implementation_whole_archive_deps": `[":mylib_cc_proto_lite"]`, - }), - MakeBazelTarget("cc_lite_proto_library", "mylib_cc_proto_lite", AttrNameToString{ - "deps": `[":mylib_proto"]`, - }), - MakeBazelTarget("proto_library", "mylib_proto", AttrNameToString{ - "srcs": `[":myprotogen"]`, - }), - MakeBazelTargetNoRestrictions("genrule", "myprotogen", AttrNameToString{ - "cmd": `""`, - "outs": `["myproto.proto"]`, - }), - }, - }) -} diff --git a/bp2build/cc_object_conversion_test.go b/bp2build/cc_object_conversion_test.go deleted file mode 100644 index 4d44db76a..000000000 --- a/bp2build/cc_object_conversion_test.go +++ /dev/null @@ -1,478 +0,0 @@ -// Copyright 2021 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "testing" - - "android/soong/android" - "android/soong/cc" -) - -func registerCcObjectModuleTypes(ctx android.RegistrationContext) { - // Always register cc_defaults module factory - ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() }) - ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) -} - -func runCcObjectTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - (&tc).ModuleTypeUnderTest = "cc_object" - (&tc).ModuleTypeUnderTestFactory = cc.ObjectFactory - RunBp2BuildTestCase(t, registerCcObjectModuleTypes, tc) -} - -func TestCcObjectSimple(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "simple cc_object generates cc_object with include header dep", - Filesystem: map[string]string{ - "a/b/foo.h": "", - "a/b/bar.h": "", - "a/b/exclude.c": "", - "a/b/c.c": "", - }, - Blueprint: `cc_object { - name: "foo", - local_include_dirs: ["include"], - system_shared_libs: [], - cflags: [ - "-Wno-gcc-compat", - "-Wall", - "-Werror", - ], - srcs: [ - "a/b/*.c" - ], - exclude_srcs: ["a/b/exclude.c"], - sdk_version: "current", - min_sdk_version: "29", - crt: true, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `[ - "-fno-addrsig", - "-Wno-gcc-compat", - "-Wall", - "-Werror", - ]`, - "local_includes": `[ - "include", - ".", - ]`, - "srcs": `["a/b/c.c"]`, - "system_dynamic_deps": `[]`, - "sdk_version": `"current"`, - "min_sdk_version": `"29"`, - "crt": "True", - }), - }, - }) -} - -func TestCcObjectDefaults(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Blueprint: `cc_object { - name: "foo", - system_shared_libs: [], - srcs: [ - "a/b/*.h", - "a/b/c.c" - ], - - defaults: ["foo_defaults"], -} - -cc_defaults { - name: "foo_defaults", - defaults: ["foo_bar_defaults"], -} - -cc_defaults { - name: "foo_bar_defaults", - cflags: [ - "-Werror", - ], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `[ - "-Werror", - "-fno-addrsig", - ]`, - "local_includes": `["."]`, - "srcs": `["a/b/c.c"]`, - "system_dynamic_deps": `[]`, - }), - }}) -} - -func TestCcObjectCcObjetDepsInObjs(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "cc_object with cc_object deps in objs props", - Filesystem: map[string]string{ - "a/b/c.c": "", - "x/y/z.c": "", - }, - Blueprint: `cc_object { - name: "foo", - system_shared_libs: [], - srcs: ["a/b/c.c"], - objs: ["bar"], - include_build_directory: false, -} - -cc_object { - name: "bar", - system_shared_libs: [], - srcs: ["x/y/z.c"], - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "bar", AttrNameToString{ - "copts": `["-fno-addrsig"]`, - "srcs": `["x/y/z.c"]`, - "system_dynamic_deps": `[]`, - }), MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `["-fno-addrsig"]`, - "objs": `[":bar"]`, - "srcs": `["a/b/c.c"]`, - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestCcObjectIncludeBuildDirFalse(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "cc_object with include_build_dir: false", - Filesystem: map[string]string{ - "a/b/c.c": "", - "x/y/z.c": "", - }, - Blueprint: `cc_object { - name: "foo", - system_shared_libs: [], - srcs: ["a/b/c.c"], - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `["-fno-addrsig"]`, - "srcs": `["a/b/c.c"]`, - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestCcObjectProductVariable(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "cc_object with product variable", - Blueprint: `cc_object { - name: "foo", - system_shared_libs: [], - include_build_directory: false, - product_variables: { - platform_sdk_version: { - asflags: ["-DPLATFORM_SDK_VERSION=%d"], - }, - }, - srcs: ["src.S"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "asflags": `select({ - "//build/bazel/product_config/config_settings:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"], - "//conditions:default": [], - })`, - "copts": `["-fno-addrsig"]`, - "srcs_as": `["src.S"]`, - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestCcObjectCflagsOneArch(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "cc_object setting cflags for one arch", - Blueprint: `cc_object { - name: "foo", - system_shared_libs: [], - srcs: ["a.cpp"], - arch: { - x86: { - cflags: ["-fPIC"], // string list - }, - arm: { - srcs: ["arch/arm/file.cpp"], // label list - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `["-fno-addrsig"] + select({ - "//build/bazel_common_rules/platforms/arch:x86": ["-fPIC"], - "//conditions:default": [], - })`, - "srcs": `["a.cpp"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arch/arm/file.cpp"], - "//conditions:default": [], - })`, - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestCcObjectCflagsFourArch(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "cc_object setting cflags for 4 architectures", - Blueprint: `cc_object { - name: "foo", - system_shared_libs: [], - srcs: ["base.cpp"], - arch: { - x86: { - srcs: ["x86.cpp"], - cflags: ["-fPIC"], - }, - x86_64: { - srcs: ["x86_64.cpp"], - cflags: ["-fPIC"], - }, - arm: { - srcs: ["arm.cpp"], - cflags: ["-Wall"], - }, - arm64: { - srcs: ["arm64.cpp"], - cflags: ["-Wall"], - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `["-fno-addrsig"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["-Wall"], - "//build/bazel_common_rules/platforms/arch:arm64": ["-Wall"], - "//build/bazel_common_rules/platforms/arch:x86": ["-fPIC"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["-fPIC"], - "//conditions:default": [], - })`, - "srcs": `["base.cpp"] + select({ - "//build/bazel_common_rules/platforms/arch:arm": ["arm.cpp"], - "//build/bazel_common_rules/platforms/arch:arm64": ["arm64.cpp"], - "//build/bazel_common_rules/platforms/arch:x86": ["x86.cpp"], - "//build/bazel_common_rules/platforms/arch:x86_64": ["x86_64.cpp"], - "//conditions:default": [], - })`, - "system_dynamic_deps": `[]`, - }), - }, - }) -} - -func TestCcObjectLinkerScript(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "cc_object setting linker_script", - Blueprint: `cc_object { - name: "foo", - srcs: ["base.cpp"], - linker_script: "bunny.lds", - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "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", - StubbedBuildDefinitions: []string{"x86_obj", "x86_64_obj", "arm_obj"}, - 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, -} - -cc_object { - name: "x86_64_obj", - system_shared_libs: [], - srcs: ["x86_64.cpp"], - include_build_directory: false, -} - -cc_object { - name: "arm_obj", - system_shared_libs: [], - srcs: ["arm.cpp"], - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `["-fno-addrsig"]`, - "objs": `select({ - "//build/bazel_common_rules/platforms/arch:arm": [":arm_obj"], - "//build/bazel_common_rules/platforms/arch:x86": [":x86_obj"], - "//build/bazel_common_rules/platforms/arch:x86_64": [":x86_64_obj"], - "//conditions:default": [], - })`, - "linker_script": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "arm.lds", - "//build/bazel_common_rules/platforms/arch:x86": "x86.lds", - "//build/bazel_common_rules/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", - Blueprint: `cc_object { - name: "foo", - srcs: ["base.cpp"], - target: { - linux_arm64: { - srcs: ["linux_arm64.cpp",] - }, - linux_x86: { - srcs: ["linux_x86.cpp",] - }, - bionic_arm64: { - srcs: ["bionic_arm64.cpp",] - }, - }, - include_build_directory: false, -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `["-fno-addrsig"]`, - "srcs": `["base.cpp"] + select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm64": [ - "linux_arm64.cpp", - "bionic_arm64.cpp", - ], - "//build/bazel_common_rules/platforms/os_arch:android_x86": ["linux_x86.cpp"], - "//build/bazel_common_rules/platforms/os_arch:linux_bionic_arm64": [ - "linux_arm64.cpp", - "bionic_arm64.cpp", - ], - "//build/bazel_common_rules/platforms/os_arch:linux_glibc_x86": ["linux_x86.cpp"], - "//build/bazel_common_rules/platforms/os_arch:linux_musl_arm64": ["linux_arm64.cpp"], - "//build/bazel_common_rules/platforms/os_arch:linux_musl_x86": ["linux_x86.cpp"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestCcObjectHeaderLib(t *testing.T) { - runCcObjectTestCase(t, Bp2buildTestCase{ - Description: "simple cc_object generates cc_object with include header dep", - Filesystem: map[string]string{ - "a/b/foo.h": "", - "a/b/bar.h": "", - "a/b/exclude.c": "", - "a/b/c.c": "", - }, - Blueprint: `cc_object { - name: "foo", - header_libs: ["libheaders"], - system_shared_libs: [], - cflags: [ - "-Wno-gcc-compat", - "-Wall", - "-Werror", - ], - srcs: [ - "a/b/*.c" - ], - exclude_srcs: ["a/b/exclude.c"], - sdk_version: "current", - min_sdk_version: "29", -} - -cc_library_headers { - name: "libheaders", - export_include_dirs: ["include"], -} -`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_object", "foo", AttrNameToString{ - "copts": `[ - "-fno-addrsig", - "-Wno-gcc-compat", - "-Wall", - "-Werror", - ]`, - "deps": `[":libheaders"]`, - "local_includes": `["."]`, - "srcs": `["a/b/c.c"]`, - "system_dynamic_deps": `[]`, - "sdk_version": `"current"`, - "min_sdk_version": `"29"`, - }), - MakeBazelTarget("cc_library_headers", "libheaders", AttrNameToString{ - "export_includes": `["include"]`, - }), - }, - }) -} diff --git a/bp2build/cc_prebuilt_binary_conversion_test.go b/bp2build/cc_prebuilt_binary_conversion_test.go deleted file mode 100644 index 9adaf32d1..000000000 --- a/bp2build/cc_prebuilt_binary_conversion_test.go +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/cc" -) - -func runCcPrebuiltBinaryTestCase(t *testing.T, testCase Bp2buildTestCase) { - t.Helper() - description := fmt.Sprintf("cc_prebuilt_binary: %s", testCase.Description) - testCase.ModuleTypeUnderTest = "cc_prebuilt_binary" - testCase.ModuleTypeUnderTestFactory = cc.PrebuiltBinaryFactory - testCase.Description = description - t.Run(description, func(t *testing.T) { - t.Helper() - RunBp2BuildTestCaseSimple(t, testCase) - }) -} - -func TestPrebuiltBinary(t *testing.T) { - runCcPrebuiltBinaryTestCase(t, - Bp2buildTestCase{ - Description: "simple", - Filesystem: map[string]string{ - "bin": "", - }, - Blueprint: ` -cc_prebuilt_binary { - name: "bintest", - srcs: ["bin"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_binary", "bintest", AttrNameToString{ - "src": `"bin"`, - })}, - }) -} - -func TestPrebuiltBinaryWithStrip(t *testing.T) { - runCcPrebuiltBinaryTestCase(t, - Bp2buildTestCase{ - Description: "with strip", - Filesystem: map[string]string{ - "bin": "", - }, - Blueprint: ` -cc_prebuilt_binary { - name: "bintest", - srcs: ["bin"], - strip: { all: true }, - bazel_module: { bp2build_available: true }, -}`, ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_binary", "bintest", AttrNameToString{ - "src": `"bin"`, - "strip": `{ - "all": True, - }`, - }), - }, - }) -} - -func TestPrebuiltBinaryWithArchVariance(t *testing.T) { - runCcPrebuiltBinaryTestCase(t, - Bp2buildTestCase{ - Description: "with arch variance", - Filesystem: map[string]string{ - "bina": "", - "binb": "", - }, - Blueprint: ` -cc_prebuilt_binary { - name: "bintest", - arch: { - arm64: { srcs: ["bina"], }, - arm: { srcs: ["binb"], }, - }, - bazel_module: { bp2build_available: true }, -}`, ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_binary", "bintest", AttrNameToString{ - "src": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "binb", - "//build/bazel_common_rules/platforms/arch:arm64": "bina", - "//conditions:default": None, - })`, - }), - }, - }) -} - -func TestPrebuiltBinaryMultipleSrcsFails(t *testing.T) { - runCcPrebuiltBinaryTestCase(t, - Bp2buildTestCase{ - Description: "fails because multiple sources", - Filesystem: map[string]string{ - "bina": "", - "binb": "", - }, - Blueprint: ` -cc_prebuilt_binary { - name: "bintest", - srcs: ["bina", "binb"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedErr: fmt.Errorf("Expected at most one source file"), - }) -} - -// TODO: nosrcs test diff --git a/bp2build/cc_prebuilt_library_conversion_test.go b/bp2build/cc_prebuilt_library_conversion_test.go deleted file mode 100644 index a7f0c7b27..000000000 --- a/bp2build/cc_prebuilt_library_conversion_test.go +++ /dev/null @@ -1,412 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/android" - "android/soong/cc" -) - -func runCcPrebuiltLibraryTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Helper() - (&tc).ModuleTypeUnderTest = "cc_prebuilt_library" - (&tc).ModuleTypeUnderTestFactory = cc.PrebuiltLibraryFactory - RunBp2BuildTestCaseSimple(t, tc) -} - -func TestPrebuiltLibraryStaticAndSharedSimple(t *testing.T) { - runCcPrebuiltLibraryTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library static and shared simple", - Filesystem: map[string]string{ - "libf.so": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - srcs: ["libf.so"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ - "static_library": `"libf.so"`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ - "static_library": `"libf.so"`, - "alwayslink": "True", - }), - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - }), - }, - }) -} - -func TestPrebuiltLibraryWithArchVariance(t *testing.T) { - runCcPrebuiltLibraryTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - arch: { - arm64: { srcs: ["libf.so"], }, - arm: { srcs: ["libg.so"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ - "static_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`}), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ - "alwayslink": "True", - "static_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`}), - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`, - }), - }, - }) -} - -func TestPrebuiltLibraryAdditionalAttrs(t *testing.T) { - runCcPrebuiltLibraryTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library additional attributes", - Filesystem: map[string]string{ - "libf.so": "", - "testdir/1/include.h": "", - "testdir/2/other.h": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - srcs: ["libf.so"], - export_include_dirs: ["testdir/1/"], - export_system_include_dirs: ["testdir/2/"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ - "static_library": `"libf.so"`, - "export_includes": `["testdir/1/"]`, - "export_system_includes": `["testdir/2/"]`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ - "static_library": `"libf.so"`, - "export_includes": `["testdir/1/"]`, - "export_system_includes": `["testdir/2/"]`, - "alwayslink": "True", - }), - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - "export_includes": `["testdir/1/"]`, - "export_system_includes": `["testdir/2/"]`, - }), - }, - }) -} - -func TestPrebuiltLibrarySharedStanzaFails(t *testing.T) { - runCcPrebuiltLibraryTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library with shared stanza fails because multiple sources", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - srcs: ["libf.so"], - shared: { - srcs: ["libg.so"], - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedErr: fmt.Errorf("Expected at most one source file"), - }) -} - -func TestPrebuiltLibraryStaticStanzaFails(t *testing.T) { - RunBp2BuildTestCaseSimple(t, - Bp2buildTestCase{ - Description: "prebuilt library with static stanza fails because multiple sources", - ModuleTypeUnderTest: "cc_prebuilt_library", - ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory, - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - srcs: ["libf.so"], - static: { - srcs: ["libg.so"], - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedErr: fmt.Errorf("Expected at most one source file"), - }) -} - -func TestPrebuiltLibrarySharedAndStaticStanzas(t *testing.T) { - runCcPrebuiltLibraryTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library with both shared and static stanzas", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - static: { - srcs: ["libf.so"], - }, - shared: { - srcs: ["libg.so"], - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ - "static_library": `"libf.so"`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ - "static_library": `"libf.so"`, - "alwayslink": "True", - }), - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libg.so"`, - }), - }, - }) -} - -// TODO(b/228623543): When this bug is fixed, enable this test -//func TestPrebuiltLibraryOnlyShared(t *testing.T) { -// runCcPrebuiltLibraryTestCase(t, -// bp2buildTestCase{ -// description: "prebuilt library shared only", -// filesystem: map[string]string{ -// "libf.so": "", -// }, -// blueprint: ` -//cc_prebuilt_library { -// name: "libtest", -// srcs: ["libf.so"], -// static: { -// enabled: false, -// }, -// bazel_module: { bp2build_available: true }, -//}`, -// expectedBazelTargets: []string{ -// makeBazelTarget("cc_prebuilt_library_shared", "libtest", attrNameToString{ -// "shared_library": `"libf.so"`, -// }), -// }, -// }) -//} - -// TODO(b/228623543): When this bug is fixed, enable this test -//func TestPrebuiltLibraryOnlyStatic(t *testing.T) { -// runCcPrebuiltLibraryTestCase(t, -// bp2buildTestCase{ -// description: "prebuilt library static only", -// filesystem: map[string]string{ -// "libf.so": "", -// }, -// blueprint: ` -//cc_prebuilt_library { -// name: "libtest", -// srcs: ["libf.so"], -// shared: { -// enabled: false, -// }, -// bazel_module: { bp2build_available: true }, -//}`, -// expectedBazelTargets: []string{ -// makeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{ -// "static_library": `"libf.so"`, -// }), -// makeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_always", attrNameToString{ -// "static_library": `"libf.so"`, -// "alwayslink": "True", -// }), -// }, -// }) -//} - -func TestPrebuiltLibraryWithExportIncludesArchVariant(t *testing.T) { - runCcPrebuiltLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_prebuilt_library correctly translates export_includes with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - srcs: ["libf.so"], - arch: { - arm: { export_include_dirs: ["testdir/1/"], }, - arm64: { export_include_dirs: ["testdir/2/"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - "export_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ - "static_library": `"libf.so"`, - "export_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ - "alwayslink": "True", - "static_library": `"libf.so"`, - "export_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestPrebuiltLibraryWithExportSystemIncludesArchVariant(t *testing.T) { - runCcPrebuiltLibraryTestCase(t, Bp2buildTestCase{ - Description: "cc_prebuilt_ibrary correctly translates export_system_includes with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library { - name: "libtest", - srcs: ["libf.so"], - arch: { - arm: { export_system_include_dirs: ["testdir/1/"], }, - arm64: { export_system_include_dirs: ["testdir/2/"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{ - "static_library": `"libf.so"`, - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_bp2build_cc_library_static_alwayslink", AttrNameToString{ - "alwayslink": "True", - "static_library": `"libf.so"`, - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestPrebuiltNdkStlConversion(t *testing.T) { - registerNdkStlModuleTypes := func(ctx android.RegistrationContext) { - ctx.RegisterModuleType("ndk_prebuilt_static_stl", cc.NdkPrebuiltStaticStlFactory) - ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory) - } - RunBp2BuildTestCase(t, registerNdkStlModuleTypes, Bp2buildTestCase{ - Description: "TODO", - Blueprint: ` -ndk_prebuilt_static_stl { - name: "ndk_libfoo_static", - export_include_dirs: ["dir1", "dir2"], -} -ndk_prebuilt_shared_stl { - name: "ndk_libfoo_shared", - export_include_dirs: ["dir1", "dir2"], -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "ndk_libfoo_static", AttrNameToString{ - "static_library": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": "current/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libfoo_static.a", - "//build/bazel_common_rules/platforms/os_arch:android_arm64": "current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libfoo_static.a", - "//build/bazel_common_rules/platforms/os_arch:android_riscv64": "current/sources/cxx-stl/llvm-libc++/libs/riscv64/libfoo_static.a", - "//build/bazel_common_rules/platforms/os_arch:android_x86": "current/sources/cxx-stl/llvm-libc++/libs/x86/libfoo_static.a", - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": "current/sources/cxx-stl/llvm-libc++/libs/x86_64/libfoo_static.a", - "//conditions:default": None, - })`, - "export_system_includes": `[ - "dir1", - "dir2", - ]`, - }), - MakeBazelTarget("cc_prebuilt_library_shared", "ndk_libfoo_shared", AttrNameToString{ - "shared_library": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm": "current/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libfoo_shared.so", - "//build/bazel_common_rules/platforms/os_arch:android_arm64": "current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libfoo_shared.so", - "//build/bazel_common_rules/platforms/os_arch:android_riscv64": "current/sources/cxx-stl/llvm-libc++/libs/riscv64/libfoo_shared.so", - "//build/bazel_common_rules/platforms/os_arch:android_x86": "current/sources/cxx-stl/llvm-libc++/libs/x86/libfoo_shared.so", - "//build/bazel_common_rules/platforms/os_arch:android_x86_64": "current/sources/cxx-stl/llvm-libc++/libs/x86_64/libfoo_shared.so", - "//conditions:default": None, - })`, - "export_system_includes": `[ - "dir1", - "dir2", - ]`, - }), - }, - }) -} diff --git a/bp2build/cc_prebuilt_library_shared_conversion_test.go b/bp2build/cc_prebuilt_library_shared_conversion_test.go deleted file mode 100644 index 2242758c6..000000000 --- a/bp2build/cc_prebuilt_library_shared_conversion_test.go +++ /dev/null @@ -1,165 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package bp2build - -import ( - "testing" - - "android/soong/cc" -) - -func runCcPrebuiltLibrarySharedTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Parallel() - t.Helper() - (&tc).ModuleTypeUnderTest = "cc_prebuilt_library_shared" - (&tc).ModuleTypeUnderTestFactory = cc.PrebuiltSharedLibraryFactory - RunBp2BuildTestCaseSimple(t, tc) -} - -func TestPrebuiltLibrarySharedSimple(t *testing.T) { - runCcPrebuiltLibrarySharedTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library shared simple", - Filesystem: map[string]string{ - "libf.so": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - srcs: ["libf.so"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - }), - }, - }) -} - -func TestPrebuiltLibrarySharedWithArchVariance(t *testing.T) { - runCcPrebuiltLibrarySharedTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library shared with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - arch: { - arm64: { srcs: ["libf.so"], }, - arm: { srcs: ["libg.so"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`, - }), - }, - }) -} - -func TestPrebuiltLibrarySharedAdditionalAttrs(t *testing.T) { - runCcPrebuiltLibrarySharedTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library shared additional attributes", - Filesystem: map[string]string{ - "libf.so": "", - "testdir/1/include.h": "", - "testdir/2/other.h": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - srcs: ["libf.so"], - export_include_dirs: ["testdir/1/"], - export_system_include_dirs: ["testdir/2/"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - "export_includes": `["testdir/1/"]`, - "export_system_includes": `["testdir/2/"]`, - }), - }, - }) -} - -func TestPrebuiltLibrarySharedWithExportIncludesArchVariant(t *testing.T) { - runCcPrebuiltLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_prebuilt_library_shared correctly translates export_includes with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - srcs: ["libf.so"], - arch: { - arm: { export_include_dirs: ["testdir/1/"], }, - arm64: { export_include_dirs: ["testdir/2/"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - "export_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestPrebuiltLibrarySharedWithExportSystemIncludesArchVariant(t *testing.T) { - runCcPrebuiltLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_prebuilt_library_shared correctly translates export_system_includes with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - srcs: ["libf.so"], - arch: { - arm: { export_system_include_dirs: ["testdir/1/"], }, - arm64: { export_system_include_dirs: ["testdir/2/"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - }, - }) -} diff --git a/bp2build/cc_prebuilt_library_shared_test.go b/bp2build/cc_prebuilt_library_shared_test.go deleted file mode 100644 index 1a9579ace..000000000 --- a/bp2build/cc_prebuilt_library_shared_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/cc" -) - -func TestSharedPrebuiltLibrary(t *testing.T) { - RunBp2BuildTestCaseSimple(t, - Bp2buildTestCase{ - Description: "prebuilt library shared simple", - ModuleTypeUnderTest: "cc_prebuilt_library_shared", - ModuleTypeUnderTestFactory: cc.PrebuiltSharedLibraryFactory, - Filesystem: map[string]string{ - "libf.so": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - srcs: ["libf.so"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `"libf.so"`, - }), - }, - }) -} - -func TestSharedPrebuiltLibraryWithArchVariance(t *testing.T) { - RunBp2BuildTestCaseSimple(t, - Bp2buildTestCase{ - Description: "prebuilt library shared with arch variance", - ModuleTypeUnderTest: "cc_prebuilt_library_shared", - ModuleTypeUnderTestFactory: cc.PrebuiltSharedLibraryFactory, - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - arch: { - arm64: { srcs: ["libf.so"], }, - arm: { srcs: ["libg.so"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_shared", "libtest", AttrNameToString{ - "shared_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`, - }), - }, - }) -} - -func TestSharedPrebuiltLibrarySharedStanzaFails(t *testing.T) { - RunBp2BuildTestCaseSimple(t, - Bp2buildTestCase{ - Description: "prebuilt library shared with shared stanza fails because multiple sources", - ModuleTypeUnderTest: "cc_prebuilt_library_shared", - ModuleTypeUnderTestFactory: cc.PrebuiltSharedLibraryFactory, - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_shared { - name: "libtest", - srcs: ["libf.so"], - shared: { - srcs: ["libg.so"], - }, - bazel_module: { bp2build_available: true}, -}`, - ExpectedErr: fmt.Errorf("Expected at most one source file"), - }) -} diff --git a/bp2build/cc_prebuilt_library_static_conversion_test.go b/bp2build/cc_prebuilt_library_static_conversion_test.go deleted file mode 100644 index fb408b5f0..000000000 --- a/bp2build/cc_prebuilt_library_static_conversion_test.go +++ /dev/null @@ -1,199 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package bp2build - -import ( - "testing" - - "android/soong/cc" -) - -func runCcPrebuiltLibraryStaticTestCase(t *testing.T, tc Bp2buildTestCase) { - t.Parallel() - t.Helper() - (&tc).ModuleTypeUnderTest = "cc_prebuilt_library_static" - (&tc).ModuleTypeUnderTestFactory = cc.PrebuiltStaticLibraryFactory - RunBp2BuildTestCaseSimple(t, tc) -} - -func TestPrebuiltLibraryStaticSimple(t *testing.T) { - runCcPrebuiltLibraryStaticTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library static simple", - Filesystem: map[string]string{ - "libf.so": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - srcs: ["libf.so"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ - "static_library": `"libf.so"`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ - "static_library": `"libf.so"`, - "alwayslink": "True", - }), - }, - }) -} - -func TestPrebuiltLibraryStaticWithArchVariance(t *testing.T) { - runCcPrebuiltLibraryStaticTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - arch: { - arm64: { srcs: ["libf.so"], }, - arm: { srcs: ["libg.so"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ - "static_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`}), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ - "alwayslink": "True", - "static_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`}), - }, - }) -} - -func TestPrebuiltLibraryStaticAdditionalAttrs(t *testing.T) { - runCcPrebuiltLibraryStaticTestCase(t, - Bp2buildTestCase{ - Description: "prebuilt library additional attributes", - Filesystem: map[string]string{ - "libf.so": "", - "testdir/1/include.h": "", - "testdir/2/other.h": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - srcs: ["libf.so"], - export_include_dirs: ["testdir/1/"], - export_system_include_dirs: ["testdir/2/"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ - "static_library": `"libf.so"`, - "export_includes": `["testdir/1/"]`, - "export_system_includes": `["testdir/2/"]`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ - "static_library": `"libf.so"`, - "export_includes": `["testdir/1/"]`, - "export_system_includes": `["testdir/2/"]`, - "alwayslink": "True", - }), - }, - }) -} - -func TestPrebuiltLibraryStaticWithExportIncludesArchVariant(t *testing.T) { - runCcPrebuiltLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_prebuilt_library_static correctly translates export_includes with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - srcs: ["libf.so"], - arch: { - arm: { export_include_dirs: ["testdir/1/"], }, - arm64: { export_include_dirs: ["testdir/2/"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ - "static_library": `"libf.so"`, - "export_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ - "alwayslink": "True", - "static_library": `"libf.so"`, - "export_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - }, - }) -} - -func TestPrebuiltLibraryStaticWithExportSystemIncludesArchVariant(t *testing.T) { - runCcPrebuiltLibraryStaticTestCase(t, Bp2buildTestCase{ - Description: "cc_prebuilt_library_static correctly translates export_system_includes with arch variance", - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - srcs: ["libf.so"], - arch: { - arm: { export_system_include_dirs: ["testdir/1/"], }, - arm64: { export_system_include_dirs: ["testdir/2/"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ - "static_library": `"libf.so"`, - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ - "alwayslink": "True", - "static_library": `"libf.so"`, - "export_system_includes": `select({ - "//build/bazel_common_rules/platforms/arch:arm": ["testdir/1/"], - "//build/bazel_common_rules/platforms/arch:arm64": ["testdir/2/"], - "//conditions:default": [], - })`, - }), - }, - }) -} diff --git a/bp2build/cc_prebuilt_library_static_test.go b/bp2build/cc_prebuilt_library_static_test.go deleted file mode 100644 index 7d0ab2874..000000000 --- a/bp2build/cc_prebuilt_library_static_test.go +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/cc" -) - -func TestStaticPrebuiltLibrary(t *testing.T) { - RunBp2BuildTestCaseSimple(t, - Bp2buildTestCase{ - Description: "prebuilt library static simple", - ModuleTypeUnderTest: "cc_prebuilt_library_static", - ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, - Filesystem: map[string]string{ - "libf.so": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - srcs: ["libf.so"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ - "static_library": `"libf.so"`, - }), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ - "static_library": `"libf.so"`, - "alwayslink": "True", - }), - }, - }) -} - -func TestStaticPrebuiltLibraryWithArchVariance(t *testing.T) { - RunBp2BuildTestCaseSimple(t, - Bp2buildTestCase{ - Description: "prebuilt library static with arch variance", - ModuleTypeUnderTest: "cc_prebuilt_library_static", - ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - arch: { - arm64: { srcs: ["libf.so"], }, - arm: { srcs: ["libg.so"], }, - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{ - "static_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`}), - MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{ - "alwayslink": "True", - "static_library": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "libg.so", - "//build/bazel_common_rules/platforms/arch:arm64": "libf.so", - "//conditions:default": None, - })`}), - }, - }) -} - -func TestStaticPrebuiltLibraryStaticStanzaFails(t *testing.T) { - RunBp2BuildTestCaseSimple(t, - Bp2buildTestCase{ - Description: "prebuilt library with static stanza fails because multiple sources", - ModuleTypeUnderTest: "cc_prebuilt_library_static", - ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory, - Filesystem: map[string]string{ - "libf.so": "", - "libg.so": "", - }, - Blueprint: ` -cc_prebuilt_library_static { - name: "libtest", - srcs: ["libf.so"], - static: { - srcs: ["libg.so"], - }, - bazel_module: { bp2build_available: true }, -}`, - ExpectedErr: fmt.Errorf("Expected at most one source file"), - }) -} - -func TestCcLibraryStaticConvertLex(t *testing.T) { - runCcLibrarySharedTestCase(t, Bp2buildTestCase{ - Description: "cc_library_static with lex files", - ModuleTypeUnderTest: "cc_library_static", - ModuleTypeUnderTestFactory: cc.LibraryStaticFactory, - Filesystem: map[string]string{ - "foo.c": "", - "bar.cc": "", - "foo1.l": "", - "bar1.ll": "", - "foo2.l": "", - "bar2.ll": "", - }, - Blueprint: `cc_library_static { - name: "foo_lib", - srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"], - lex: { flags: ["--foo_flags"] }, - include_build_directory: false, - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{ - "srcs": `[ - "foo1.l", - "foo2.l", - ]`, - "lexopts": `["--foo_flags"]`, - }), - MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{ - "srcs": `[ - "bar1.ll", - "bar2.ll", - ]`, - "lexopts": `["--foo_flags"]`, - }), - MakeBazelTarget("cc_library_static", "foo_lib", AttrNameToString{ - "srcs": `[ - "bar.cc", - ":foo_lib_genlex_ll", - ]`, - "srcs_c": `[ - "foo.c", - ":foo_lib_genlex_l", - ]`, - }), - }, - }) -} diff --git a/bp2build/cc_prebuilt_object_conversion_test.go b/bp2build/cc_prebuilt_object_conversion_test.go deleted file mode 100644 index 068e4e28b..000000000 --- a/bp2build/cc_prebuilt_object_conversion_test.go +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/cc" -) - -func runCcPrebuiltObjectTestCase(t *testing.T, testCase Bp2buildTestCase) { - t.Helper() - description := fmt.Sprintf("cc_prebuilt_object: %s", testCase.Description) - testCase.ModuleTypeUnderTest = "cc_prebuilt_object" - testCase.ModuleTypeUnderTestFactory = cc.PrebuiltObjectFactory - testCase.Description = description - t.Run(description, func(t *testing.T) { - t.Helper() - RunBp2BuildTestCaseSimple(t, testCase) - }) -} - -func TestPrebuiltObject(t *testing.T) { - runCcPrebuiltObjectTestCase(t, - Bp2buildTestCase{ - Description: "simple", - Filesystem: map[string]string{ - "obj.o": "", - }, - Blueprint: ` -cc_prebuilt_object { - name: "objtest", - srcs: ["obj.o"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_object", "objtest", AttrNameToString{ - "src": `"obj.o"`, - })}, - }) -} - -func TestPrebuiltObjectWithArchVariance(t *testing.T) { - runCcPrebuiltObjectTestCase(t, - Bp2buildTestCase{ - Description: "with arch variance", - Filesystem: map[string]string{ - "obja.o": "", - "objb.o": "", - }, - Blueprint: ` -cc_prebuilt_object { - name: "objtest", - arch: { - arm64: { srcs: ["obja.o"], }, - arm: { srcs: ["objb.o"], }, - }, - bazel_module: { bp2build_available: true }, -}`, ExpectedBazelTargets: []string{ - MakeBazelTarget("cc_prebuilt_object", "objtest", AttrNameToString{ - "src": `select({ - "//build/bazel_common_rules/platforms/arch:arm": "objb.o", - "//build/bazel_common_rules/platforms/arch:arm64": "obja.o", - "//conditions:default": None, - })`, - }), - }, - }) -} - -func TestPrebuiltObjectMultipleSrcsFails(t *testing.T) { - runCcPrebuiltObjectTestCase(t, - Bp2buildTestCase{ - Description: "fails because multiple sources", - Filesystem: map[string]string{ - "obja": "", - "objb": "", - }, - Blueprint: ` -cc_prebuilt_object { - name: "objtest", - srcs: ["obja.o", "objb.o"], - bazel_module: { bp2build_available: true }, -}`, - ExpectedErr: fmt.Errorf("Expected at most one source file"), - }) -} - -// TODO: nosrcs test diff --git a/bp2build/cc_test_conversion_test.go b/bp2build/cc_test_conversion_test.go deleted file mode 100644 index c5f5f795c..000000000 --- a/bp2build/cc_test_conversion_test.go +++ /dev/null @@ -1,633 +0,0 @@ -// Copyright 2022 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package bp2build - -import ( - "fmt" - "testing" - - "android/soong/android" - "android/soong/cc" - "android/soong/genrule" -) - -type ccTestBp2buildTestCase struct { - description string - blueprint string - filesystem map[string]string - targets []testBazelTarget - stubbedBuildDefinitions []string -} - -func registerCcTestModuleTypes(ctx android.RegistrationContext) { - cc.RegisterCCBuildComponents(ctx) - ctx.RegisterModuleType("cc_binary", cc.BinaryFactory) - ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) - ctx.RegisterModuleType("cc_library", cc.LibraryFactory) - ctx.RegisterModuleType("cc_test_library", cc.TestLibraryFactory) - ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) -} - -func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) { - t.Helper() - moduleTypeUnderTest := "cc_test" - description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description) - t.Run(description, func(t *testing.T) { - t.Helper() - RunBp2BuildTestCase(t, registerCcTestModuleTypes, Bp2buildTestCase{ - ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostAndDeviceSupported), - Filesystem: testCase.filesystem, - ModuleTypeUnderTest: moduleTypeUnderTest, - ModuleTypeUnderTestFactory: cc.TestFactory, - Description: description, - Blueprint: testCase.blueprint, - StubbedBuildDefinitions: testCase.stubbedBuildDefinitions, - }) - }) -} - -func TestBasicCcTest(t *testing.T) { - runCcTestTestCase(t, ccTestBp2buildTestCase{ - description: "basic cc_test with commonly used attributes", - stubbedBuildDefinitions: []string{"libbuildversion", "libprotobuf-cpp-lite", "libprotobuf-cpp-full", - "foolib", "hostlib", "data_mod", "cc_bin", "cc_lib", "cc_test_lib2", "libgtest_main", "libgtest"}, - blueprint: ` -cc_test { - name: "mytest", - host_supported: true, - srcs: ["test.cpp"], - target: { - android: { - srcs: ["android.cpp"], - shared_libs: ["foolib"], - }, - linux: { - srcs: ["linux.cpp"], - }, - host: { - static_libs: ["hostlib"], - }, - }, - static_libs: ["cc_test_lib1"], - shared_libs: ["cc_test_lib2"], - data: [":data_mod", "file.txt"], - data_bins: [":cc_bin"], - data_libs: [":cc_lib"], - cflags: ["-Wall"], -} - -cc_test_library { - name: "cc_test_lib1", - host_supported: true, - include_build_directory: false, -} -` + simpleModule("cc_library", "foolib") + - simpleModule("cc_library_static", "hostlib") + - simpleModule("genrule", "data_mod") + - simpleModule("cc_binary", "cc_bin") + - simpleModule("cc_library", "cc_lib") + - simpleModule("cc_test_library", "cc_test_lib2") + - simpleModule("cc_library_static", "libgtest_main") + - simpleModule("cc_library_static", "libgtest"), - targets: []testBazelTarget{ - {"cc_library_shared", "cc_test_lib1", AttrNameToString{}}, - {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}}, - {"cc_test", "mytest", AttrNameToString{ - "copts": `["-Wall"]`, - "data": `[ - ":data_mod", - "file.txt", - ":cc_bin", - ":cc_lib", - ]`, - "deps": `[ - ":cc_test_lib1_bp2build_cc_library_static", - ":libgtest_main", - ":libgtest", - ] + select({ - "//build/bazel_common_rules/platforms/os:darwin": [":hostlib"], - "//build/bazel_common_rules/platforms/os:linux_bionic": [":hostlib"], - "//build/bazel_common_rules/platforms/os:linux_glibc": [":hostlib"], - "//build/bazel_common_rules/platforms/os:linux_musl": [":hostlib"], - "//build/bazel_common_rules/platforms/os:windows": [":hostlib"], - "//conditions:default": [], - })`, - "local_includes": `["."]`, - "dynamic_deps": `[":cc_test_lib2"] + select({ - "//build/bazel_common_rules/platforms/os:android": [":foolib"], - "//conditions:default": [], - })`, - "srcs": `["test.cpp"] + select({ - "//build/bazel_common_rules/platforms/os:android": [ - "linux.cpp", - "android.cpp", - ], - "//build/bazel_common_rules/platforms/os:linux_bionic": ["linux.cpp"], - "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.cpp"], - "//build/bazel_common_rules/platforms/os:linux_musl": ["linux.cpp"], - "//conditions:default": [], - })`, - "runs_on": `[ - "host_without_device", - "device", - ]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm64": [ - "memtag_heap", - "diag_memtag_heap", - ], - "//conditions:default": [], - })`, - }, - }, - }, - }) -} - -func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) { - runCcTestTestCase(t, ccTestBp2buildTestCase{ - description: "cc test with disabled gtest and isolated props", - blueprint: ` -cc_test { - name: "mytest", - host_supported: true, - srcs: ["test.cpp"], - gtest: false, - isolated: false, -} -`, - targets: []testBazelTarget{ - {"cc_test", "mytest", AttrNameToString{ - "gtest": "False", - "local_includes": `["."]`, - "srcs": `["test.cpp"]`, - "runs_on": `[ - "host_without_device", - "device", - ]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm64": [ - "memtag_heap", - "diag_memtag_heap", - ], - "//conditions:default": [], - })`, - }, - }, - }, - }) -} - -func TestCcTest_TestOptions_Tags(t *testing.T) { - runCcTestTestCase(t, ccTestBp2buildTestCase{ - description: "cc test with test_options.tags converted to tags", - stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"}, - blueprint: ` -cc_test { - name: "mytest", - host_supported: true, - srcs: ["test.cpp"], - test_options: { tags: ["no-remote"] }, -} -` + simpleModule("cc_library_static", "libgtest_main") + - simpleModule("cc_library_static", "libgtest"), - targets: []testBazelTarget{ - {"cc_test", "mytest", AttrNameToString{ - "tags": `["no-remote"]`, - "local_includes": `["."]`, - "srcs": `["test.cpp"]`, - "deps": `[ - ":libgtest_main", - ":libgtest", - ]`, - "runs_on": `[ - "host_without_device", - "device", - ]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm64": [ - "memtag_heap", - "diag_memtag_heap", - ], - "//conditions:default": [], - })`, - }, - }, - }, - }) -} - -func TestCcTest_TestConfig(t *testing.T) { - runCcTestTestCase(t, ccTestBp2buildTestCase{ - description: "cc test that sets a test_config", - filesystem: map[string]string{ - "test_config.xml": "", - }, - stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"}, - blueprint: ` -cc_test { - name: "mytest", - srcs: ["test.cpp"], - test_config: "test_config.xml", -} -` + simpleModule("cc_library_static", "libgtest_main") + - simpleModule("cc_library_static", "libgtest"), - targets: []testBazelTarget{ - {"cc_test", "mytest", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["test.cpp"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - "test_config": `"test_config.xml"`, - "deps": `[ - ":libgtest_main", - ":libgtest", - ]`, - "runs_on": `["device"]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm64": [ - "memtag_heap", - "diag_memtag_heap", - ], - "//conditions:default": [], - })`, - }, - }, - }, - }) -} - -func TestCcTest_TestConfigAndroidTestXML(t *testing.T) { - runCcTestTestCase(t, ccTestBp2buildTestCase{ - description: "cc test that defaults to test config AndroidTest.xml", - filesystem: map[string]string{ - "AndroidTest.xml": "", - "DynamicConfig.xml": "", - }, - stubbedBuildDefinitions: []string{"libgtest_main", "libgtest"}, - blueprint: ` -cc_test { - name: "mytest", - srcs: ["test.cpp"], -} -` + simpleModule("cc_library_static", "libgtest_main") + - simpleModule("cc_library_static", "libgtest"), - targets: []testBazelTarget{ - {"cc_test", "mytest", AttrNameToString{ - "local_includes": `["."]`, - "srcs": `["test.cpp"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - "test_config": `"AndroidTest.xml"`, - "dynamic_config": `"DynamicConfig.xml"`, - "deps": `[ - ":libgtest_main", - ":libgtest", - ]`, - "runs_on": `["device"]`, - "features": `select({ - "//build/bazel_common_rules/platforms/os_arch:android_arm64": [ - "memtag_heap", - "diag_memtag_heap", - ], - "//conditions:default": [], - })`, - }, - }, - }, - }) -} - -func TestCcTest_TestConfigTemplateOptions(t *testing.T) { - runCcTestTestCase(t, ccTestBp2buildTestCase{ - description: "cc test that sets test config template attributes", - filesystem: map[string]string{ - "test_config_template.xml": "", - }, - stubbedBuildDefinitions: []string{"libgtest_isolated_main", "liblog"}, - blueprint: ` -cc_test { - name: "mytest", - srcs: ["test.cpp"], - test_config_template: "test_config_template.xml", - auto_gen_config: true, - isolated: true, -} -` + simpleModule("cc_library_static", "libgtest_isolated_main") + - simpleModule("cc_library", "liblog"), - targets: []testBazelTarget{ - {"cc_test", "mytest", AttrNameToString{ - "auto_generate_test_config": "True", - "local_includes": `["."]`, - "srcs": `["test.cpp"]`, - "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, - "template_configs": `[ - "'\\n '", - "'