2021-02-17 16:17:28 +01:00
// 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 android
2021-02-17 19:22:03 +01:00
import (
2021-11-02 17:43:57 +01:00
"android/soong/bazel"
2021-02-17 19:22:03 +01:00
"fmt"
"io/ioutil"
"path/filepath"
"strings"
2021-02-24 22:55:11 +01:00
"github.com/google/blueprint"
2021-02-17 19:22:03 +01:00
"github.com/google/blueprint/proptools"
)
// Properties contains common module properties for Bazel migration purposes.
type properties struct {
// In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
// this Soong module.
2021-11-02 17:43:57 +01:00
Bazel_module bazel . BazelModuleProperties
2021-02-17 19:22:03 +01:00
}
2021-02-17 16:17:28 +01:00
2021-11-15 13:28:43 +01:00
// namespacedVariableProperties is a map from a string representing a Soong
2021-11-17 11:57:35 +01:00
// config variable namespace, like "android" or "vendor_name" to a slice of
// pointer to a struct containing a single field called Soong_config_variables
// whose value mirrors the structure in the Blueprint file.
type namespacedVariableProperties map [ string ] [ ] interface { }
2021-11-02 17:43:57 +01:00
2021-02-17 16:17:28 +01:00
// BazelModuleBase contains the property structs with metadata for modules which can be converted to
// Bazel.
type BazelModuleBase struct {
2021-02-17 19:22:03 +01:00
bazelProperties properties
2021-11-02 17:43:57 +01:00
// namespacedVariableProperties is used for soong_config_module_type support
// in bp2build. Soong config modules allow users to set module properties
// based on custom product variables defined in Android.bp files. These
// variables are namespaced to prevent clobbering, especially when set from
// Makefiles.
namespacedVariableProperties namespacedVariableProperties
// baseModuleType is set when this module was created from a module type
// defined by a soong_config_module_type. Every soong_config_module_type
// "wraps" another module type, e.g. a soong_config_module_type can wrap a
// cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
// This baseModuleType is set to the wrapped module type.
baseModuleType string
2021-02-17 16:17:28 +01:00
}
// Bazelable is specifies the interface for modules that can be converted to Bazel.
type Bazelable interface {
2021-02-17 19:22:03 +01:00
bazelProps ( ) * properties
HasHandcraftedLabel ( ) bool
2021-02-24 22:55:11 +01:00
HandcraftedLabel ( ) string
GetBazelLabel ( ctx BazelConversionPathContext , module blueprint . Module ) string
2021-11-02 07:40:51 +01:00
ConvertWithBp2build ( ctx BazelConversionContext ) bool
convertWithBp2build ( ctx BazelConversionContext , module blueprint . Module ) bool
2021-02-17 19:22:03 +01:00
GetBazelBuildFileContents ( c Config , path , name string ) ( string , error )
2021-11-02 17:43:57 +01:00
2021-11-17 11:57:35 +01:00
// namespacedVariableProps is a map from a soong config variable namespace
// (e.g. acme, android) to a map of interfaces{}, which are really
// reflect.Struct pointers, representing the value of the
// soong_config_variables property of a module. The struct pointer is the
// one with the single member called Soong_config_variables, which itself is
// a struct containing fields for each supported feature in that namespace.
//
// The reason for using an slice of interface{} is to support defaults
// propagation of the struct pointers.
2021-11-02 17:43:57 +01:00
namespacedVariableProps ( ) namespacedVariableProperties
setNamespacedVariableProps ( props namespacedVariableProperties )
BaseModuleType ( ) string
2021-11-17 11:57:35 +01:00
SetBaseModuleType ( baseModuleType string )
2021-02-17 16:17:28 +01:00
}
// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
type BazelModule interface {
Module
Bazelable
}
// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
// properties.
func InitBazelModule ( module BazelModule ) {
module . AddProperties ( module . bazelProps ( ) )
}
// bazelProps returns the Bazel properties for the given BazelModuleBase.
2021-02-17 19:22:03 +01:00
func ( b * BazelModuleBase ) bazelProps ( ) * properties {
2021-02-17 16:17:28 +01:00
return & b . bazelProperties
}
2021-11-02 17:43:57 +01:00
func ( b * BazelModuleBase ) namespacedVariableProps ( ) namespacedVariableProperties {
return b . namespacedVariableProperties
}
func ( b * BazelModuleBase ) setNamespacedVariableProps ( props namespacedVariableProperties ) {
b . namespacedVariableProperties = props
}
func ( b * BazelModuleBase ) BaseModuleType ( ) string {
return b . baseModuleType
}
func ( b * BazelModuleBase ) SetBaseModuleType ( baseModuleType string ) {
b . baseModuleType = baseModuleType
}
2021-02-17 19:22:03 +01:00
// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
func ( b * BazelModuleBase ) HasHandcraftedLabel ( ) bool {
return b . bazelProperties . Bazel_module . Label != nil
}
// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
func ( b * BazelModuleBase ) HandcraftedLabel ( ) string {
return proptools . String ( b . bazelProperties . Bazel_module . Label )
}
2021-02-17 16:17:28 +01:00
// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
2021-02-24 22:55:11 +01:00
func ( b * BazelModuleBase ) GetBazelLabel ( ctx BazelConversionPathContext , module blueprint . Module ) string {
if b . HasHandcraftedLabel ( ) {
return b . HandcraftedLabel ( )
}
2021-03-10 08:05:59 +01:00
if b . ConvertWithBp2build ( ctx ) {
2021-02-24 22:55:11 +01:00
return bp2buildModuleLabel ( ctx , module )
}
return "" // no label for unconverted module
2021-02-17 16:17:28 +01:00
}
2021-03-10 08:05:59 +01:00
// Configuration to decide if modules in a directory should default to true/false for bp2build_available
type Bp2BuildConfig map [ string ] BazelConversionConfigEntry
type BazelConversionConfigEntry int
const (
Add os/target configurable selects for label list attributes.
This CL is pretty large, so I recommend starting with reading the newly
added tests for the expected behavior.
This change works in conjunction with the linked CLs in the Gerrit topic.
Those CLs add support for new platform() definitions for OS targets
specified in Soong's arch.go, which are configurable through
Android.bp's `target {}` property. It works similary to previous CLs
adding support for the `arch {}` property.
These configurable props are keyed by the OS: android, linux_bionic,
windows, and so on. They map to `select` statements in label list
attributes, which this CL enables for cc_library_headers' header_libs
and export_header_lib_headers props.
This enables //bionic/libc:libc_headers to be generated correctly, from:
cc_library_headers {
name: "libc_headers",
target: {
android: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
linux_bionic: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
},
// omitted props
}
to:
cc_library_headers(
name = "libc_headers",
deps = [] + select({
"//build/bazel/platforms/os:android": [
":libc_headers_arch",
],
"//build/bazel/platforms/os:linux_bionic": [
":libc_headers_arch",
],
"//conditions:default": [],
}),
)
Test: TH
Test: Verify generated //bionic/libc:libc_headers
Fixes: 183597786
Change-Id: I01016cc2cc9a71449f02300d747f01decebf3f6e
2021-03-24 07:18:33 +01:00
// A sentinel value to be used as a key in Bp2BuildConfig for modules with
// no package path. This is also the module dir for top level Android.bp
// modules.
BP2BUILD_TOPLEVEL = "."
2021-03-10 08:05:59 +01:00
// iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
// which can also mean that the key doesn't exist in a lookup.
// all modules in this package and subpackages default to bp2build_available: true.
// allows modules to opt-out.
Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
2021-08-31 07:58:01 +02:00
// all modules in this package (not recursively) default to bp2build_available: true.
// allows modules to opt-out.
Bp2BuildDefaultTrue
2021-03-10 08:05:59 +01:00
// all modules in this package (not recursively) default to bp2build_available: false.
// allows modules to opt-in.
Bp2BuildDefaultFalse
)
var (
2021-05-13 03:20:13 +02:00
// Keep any existing BUILD files (and do not generate new BUILD files) for these directories
2021-07-26 06:45:48 +02:00
// in the synthetic Bazel workspace.
2021-05-13 03:20:13 +02:00
bp2buildKeepExistingBuildFile = map [ string ] bool {
// This is actually build/bazel/build.BAZEL symlinked to ./BUILD
2021-07-16 11:28:53 +02:00
"." : /*recursive = */ false ,
2021-05-13 03:20:13 +02:00
2021-07-26 06:45:48 +02:00
// build/bazel/examples/apex/... BUILD files should be generated, so
// build/bazel is not recursive. Instead list each subdirectory under
// build/bazel explicitly.
"build/bazel" : /* recursive = */ false ,
"build/bazel/examples/android_app" : /* recursive = */ true ,
2021-08-26 17:07:48 +02:00
"build/bazel/examples/java" : /* recursive = */ true ,
2021-07-26 06:45:48 +02:00
"build/bazel/bazel_skylib" : /* recursive = */ true ,
"build/bazel/rules" : /* recursive = */ true ,
"build/bazel/rules_cc" : /* recursive = */ true ,
2021-08-31 07:58:01 +02:00
"build/bazel/scripts" : /* recursive = */ true ,
2021-07-26 06:45:48 +02:00
"build/bazel/tests" : /* recursive = */ true ,
"build/bazel/platforms" : /* recursive = */ true ,
"build/bazel/product_variables" : /* recursive = */ true ,
2021-08-24 07:14:22 +02:00
"build/bazel_common_rules" : /* recursive = */ true ,
2021-09-08 20:09:02 +02:00
"build/make/tools" : /* recursive = */ true ,
2021-05-13 03:20:13 +02:00
"build/pesto" : /* recursive = */ true ,
// external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
// e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed
"external/bazelbuild-rules_android" : /* recursive = */ true ,
2021-08-10 15:00:33 +02:00
"external/bazel-skylib" : /* recursive = */ true ,
2021-09-08 20:09:02 +02:00
"external/guava" : /* recursive = */ true ,
"external/error_prone" : /* recursive = */ true ,
"external/jsr305" : /* recursive = */ true ,
"frameworks/ex/common" : /* recursive = */ true ,
2021-05-13 03:20:13 +02:00
2021-09-22 20:43:43 +02:00
"packages/apps/Music" : /* recursive = */ true ,
"packages/apps/QuickSearchBox" : /* recursive = */ true ,
"packages/apps/WallpaperPicker" : /* recursive = */ false ,
2021-11-09 16:29:52 +01:00
"prebuilts/gcc" : /* recursive = */ true ,
2021-05-13 03:20:13 +02:00
"prebuilts/sdk" : /* recursive = */ false ,
2021-09-22 20:43:43 +02:00
"prebuilts/sdk/current/extras/app-toolkit" : /* recursive = */ false ,
"prebuilts/sdk/current/support" : /* recursive = */ false ,
2021-05-13 03:20:13 +02:00
"prebuilts/sdk/tools" : /* recursive = */ false ,
2021-08-02 19:27:06 +02:00
"prebuilts/r8" : /* recursive = */ false ,
2021-04-21 13:10:09 +02:00
}
2021-03-10 08:05:59 +01:00
// Configure modules in these directories to enable bp2build_available: true or false by default.
bp2buildDefaultConfig = Bp2BuildConfig {
2021-09-28 22:47:36 +02:00
"bionic" : Bp2BuildDefaultTrueRecursively ,
"build/bazel/examples/apex/minimal" : Bp2BuildDefaultTrueRecursively ,
2021-11-10 16:37:05 +01:00
"build/soong/cc/libbuildversion" : Bp2BuildDefaultTrue , // Skip tests subdir
2021-09-28 22:47:36 +02:00
"development/sdk" : Bp2BuildDefaultTrueRecursively ,
"external/arm-optimized-routines" : Bp2BuildDefaultTrueRecursively ,
"external/boringssl" : Bp2BuildDefaultTrueRecursively ,
"external/brotli" : Bp2BuildDefaultTrue ,
"external/fmtlib" : Bp2BuildDefaultTrueRecursively ,
2021-10-04 18:48:29 +02:00
"external/google-benchmark" : Bp2BuildDefaultTrueRecursively ,
2021-10-11 18:56:48 +02:00
"external/googletest/googletest" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"external/gwp_asan" : Bp2BuildDefaultTrueRecursively ,
"external/jemalloc_new" : Bp2BuildDefaultTrueRecursively ,
2021-10-04 18:48:29 +02:00
"external/jsoncpp" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"external/libcap" : Bp2BuildDefaultTrueRecursively ,
"external/libcxx" : Bp2BuildDefaultTrueRecursively ,
"external/libcxxabi" : Bp2BuildDefaultTrueRecursively ,
"external/lz4/lib" : Bp2BuildDefaultTrue ,
2021-10-11 18:56:48 +02:00
"external/mdnsresponder" : Bp2BuildDefaultTrueRecursively ,
"external/minijail" : Bp2BuildDefaultTrueRecursively ,
"external/pcre" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"external/protobuf" : Bp2BuildDefaultTrueRecursively ,
"external/python/six" : Bp2BuildDefaultTrueRecursively ,
"external/scudo" : Bp2BuildDefaultTrueRecursively ,
2021-10-11 18:56:48 +02:00
"external/selinux/libselinux" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"external/zlib" : Bp2BuildDefaultTrueRecursively ,
2021-10-11 18:56:48 +02:00
"external/zstd" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"frameworks/native/libs/adbd_auth" : Bp2BuildDefaultTrueRecursively ,
"packages/modules/adb" : Bp2BuildDefaultTrue ,
"packages/modules/adb/crypto" : Bp2BuildDefaultTrueRecursively ,
"packages/modules/adb/libs" : Bp2BuildDefaultTrueRecursively ,
"packages/modules/adb/pairing_auth" : Bp2BuildDefaultTrueRecursively ,
"packages/modules/adb/pairing_connection" : Bp2BuildDefaultTrueRecursively ,
"packages/modules/adb/proto" : Bp2BuildDefaultTrueRecursively ,
"packages/modules/adb/tls" : Bp2BuildDefaultTrueRecursively ,
"prebuilts/clang/host/linux-x86" : Bp2BuildDefaultTrueRecursively ,
2021-11-17 12:05:42 +01:00
"system/apex" : Bp2BuildDefaultTrue ,
2021-09-28 22:47:36 +02:00
"system/core/diagnose_usb" : Bp2BuildDefaultTrueRecursively ,
"system/core/libasyncio" : Bp2BuildDefaultTrue ,
"system/core/libcrypto_utils" : Bp2BuildDefaultTrueRecursively ,
"system/core/libcutils" : Bp2BuildDefaultTrueRecursively ,
2021-10-11 18:56:48 +02:00
"system/core/libpackagelistparser" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"system/core/libprocessgroup" : Bp2BuildDefaultTrue ,
2021-10-04 18:48:29 +02:00
"system/core/libprocessgroup/cgrouprc" : Bp2BuildDefaultTrue ,
"system/core/libprocessgroup/cgrouprc_format" : Bp2BuildDefaultTrue ,
2021-04-26 12:15:57 +02:00
"system/core/property_service/libpropertyinfoparser" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"system/libbase" : Bp2BuildDefaultTrueRecursively ,
2021-10-02 00:00:31 +02:00
"system/libziparchive" : Bp2BuildDefaultTrueRecursively ,
2021-09-28 22:47:36 +02:00
"system/logging/liblog" : Bp2BuildDefaultTrueRecursively ,
"system/sepolicy/apex" : Bp2BuildDefaultTrueRecursively ,
"system/timezone/apex" : Bp2BuildDefaultTrueRecursively ,
"system/timezone/output_data" : Bp2BuildDefaultTrueRecursively ,
2021-03-10 08:05:59 +01:00
}
2021-03-25 10:28:38 +01:00
2021-04-15 23:27:08 +02:00
// Per-module denylist to always opt modules out of both bp2build and mixed builds.
2021-03-30 16:13:16 +02:00
bp2buildModuleDoNotConvertList = [ ] string {
2021-11-09 16:29:52 +01:00
"libprotobuf-cpp-full" , "libprotobuf-cpp-lite" , // Unsupported product&vendor suffix. b/204811222 and b/204810610.
2021-10-02 00:00:31 +02:00
"libc_malloc_debug" , // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
"libbase_ndk" , // http://b/186826477, fails to link libctscamera2_jni for device (required for CtsCameraTestCases)
2021-09-01 23:22:09 +02:00
2021-09-28 19:48:21 +02:00
"libprotobuf-python" , // contains .proto sources
"libprotobuf-internal-protos" , // we don't handle path property for fileegroups
"libprotobuf-internal-python-srcs" , // we don't handle path property for fileegroups
2021-09-28 22:47:36 +02:00
"libseccomp_policy" , // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds.
"libfdtrack" , // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
2021-04-30 10:20:01 +02:00
2021-05-13 03:20:13 +02:00
"gwp_asan_crash_handler" , // cc_library, ld.lld: error: undefined symbol: memset
2021-10-04 18:48:29 +02:00
"brotli-fuzzer-corpus" , // b/202015218: outputs are in location incompatible with bazel genrule handling.
2021-08-31 07:58:01 +02:00
2021-10-18 08:33:16 +02:00
// b/203369847: multiple genrules in the same package creating the same file
// //development/sdk/...
"platform_tools_properties" ,
"build_tools_source_properties" ,
2021-11-11 19:39:20 +01:00
"libminijail" , // b/202491296: Uses unsupported c_std property.
"minijail0" , // depends on unconverted modules: libminijail
2021-10-04 19:55:44 +02:00
"drop_privs" , // depends on unconverted modules: libminijail
2021-10-11 18:56:48 +02:00
2021-04-30 10:20:01 +02:00
// Tests. Handle later.
"libbionic_tests_headers_posix" , // http://b/186024507, cc_library_static, sched.h, time.h not found
"libjemalloc5_integrationtest" ,
"libjemalloc5_stresstestlib" ,
"libjemalloc5_unittest" ,
2021-07-27 07:34:59 +02:00
// APEX support
2021-10-04 18:48:29 +02:00
"com.android.runtime" , // http://b/194746715, apex, depends on 'libc_malloc_debug'
2021-09-28 22:47:36 +02:00
"libadb_crypto" , // Depends on libadb_protos
"libadb_crypto_static" , // Depends on libadb_protos_static
"libadb_pairing_connection" , // Depends on libadb_protos
"libadb_pairing_connection_static" , // Depends on libadb_protos_static
"libadb_pairing_server" , // Depends on libadb_protos
"libadb_pairing_server_static" , // Depends on libadb_protos_static
"libadbd" , // Depends on libadbd_core
"libadbd_core" , // Depends on libadb_protos
"libadbd_services" , // Depends on libadb_protos
"libadb_protos_static" , // b/200601772: Requires cc_library proto support
"libadb_protos" , // b/200601772: Requires cc_library proto support
"libapp_processes_protos_lite" , // b/200601772: Requires cc_library proto support
2021-10-02 00:00:31 +02:00
"libgtest_ndk_c++" , // b/201816222: Requires sdk_version support.
"libgtest_main_ndk_c++" , // b/201816222: Requires sdk_version support.
2021-10-04 19:55:44 +02:00
"abb" , // depends on unconverted modules: libadbd_core, libadbd_services, libcmd, libbinder, libutils, libselinux
"adb" , // depends on unconverted modules: bin2c_fastdeployagent, libadb_crypto, libadb_host, libadb_pairing_connection, libadb_protos, libandroidfw, libapp_processes_protos_full, libfastdeploy_host, libmdnssd, libopenscreen-discovery, libopenscreen-platform-impl, libusb, libutils, libziparchive, libzstd, AdbWinApi
"adbd" , // depends on unconverted modules: libadb_crypto, libadb_pairing_connection, libadb_protos, libadbd, libadbd_core, libapp_processes_protos_lite, libmdnssd, libzstd, libadbd_services, libcap, libminijail, libselinux
"bionic_tests_zipalign" , // depends on unconverted modules: libziparchive, libutils
"linker" , // depends on unconverted modules: liblinker_debuggerd_stub, libdebuggerd_handler_fallback, libziparchive, liblinker_main, liblinker_malloc
"linker_reloc_bench_main" , // depends on unconverted modules: liblinker_reloc_bench_*
"sefcontext_compile" , // depends on unconverted modules: libsepol
"versioner" , // depends on unconverted modules: libclang_cxx_host, libLLVM_host
"linkerconfig" , // http://b/202876379 has arch-variant static_executable
"mdnsd" , // http://b/202876379 has arch-variant static_executable
"acvp_modulewrapper" , // disabled for android x86/x86_64
2021-04-15 23:27:08 +02:00
}
2021-04-13 19:08:04 +02:00
2021-05-03 11:15:48 +02:00
// Per-module denylist of cc_library modules to only generate the static
// variant if their shared variant isn't ready or buildable by Bazel.
bp2buildCcLibraryStaticOnlyList = [ ] string {
2021-05-18 11:01:49 +02:00
"libjemalloc5" , // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets.
2021-05-03 11:15:48 +02:00
}
2021-04-15 23:27:08 +02:00
// Per-module denylist to opt modules out of mixed builds. Such modules will
// still be generated via bp2build.
2021-08-10 17:58:07 +02:00
mixedBuildsDisabledList = [ ] string {
2021-11-04 23:58:12 +01:00
"libbrotli" , // http://b/198585397, ld.lld: error: bionic/libc/arch-arm64/generic/bionic/memmove.S:95:(.text+0x10): relocation R_AARCH64_CONDBR19 out of range: -1404176 is not in [-1048576, 1048575]; references __memcpy
"minijail_constants_json" , // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
2021-11-16 23:01:11 +01:00
"cap_names.h" , // TODO(b/204913827) runfiles need to be handled in mixed builds
"libcap" , // TODO(b/204913827) runfiles need to be handled in mixed builds
2021-08-10 17:58:07 +02:00
}
2021-03-30 16:13:16 +02:00
// Used for quicker lookups
2021-04-21 13:10:09 +02:00
bp2buildModuleDoNotConvert = map [ string ] bool { }
2021-05-03 11:15:48 +02:00
bp2buildCcLibraryStaticOnly = map [ string ] bool { }
2021-04-21 13:10:09 +02:00
mixedBuildsDisabled = map [ string ] bool { }
2021-03-10 08:05:59 +01:00
)
2021-03-30 16:13:16 +02:00
func init ( ) {
for _ , moduleName := range bp2buildModuleDoNotConvertList {
bp2buildModuleDoNotConvert [ moduleName ] = true
}
2021-04-15 23:27:08 +02:00
2021-05-03 11:15:48 +02:00
for _ , moduleName := range bp2buildCcLibraryStaticOnlyList {
bp2buildCcLibraryStaticOnly [ moduleName ] = true
}
2021-04-15 23:27:08 +02:00
for _ , moduleName := range mixedBuildsDisabledList {
mixedBuildsDisabled [ moduleName ] = true
}
}
2021-09-20 21:14:39 +02:00
func GenerateCcLibraryStaticOnly ( moduleName string ) bool {
return bp2buildCcLibraryStaticOnly [ moduleName ]
2021-05-03 11:15:48 +02:00
}
2021-05-13 03:20:13 +02:00
func ShouldKeepExistingBuildFileForDir ( dir string ) bool {
if _ , ok := bp2buildKeepExistingBuildFile [ dir ] ; ok {
// Exact dir match
2021-04-21 13:10:09 +02:00
return true
}
2021-05-13 03:20:13 +02:00
// Check if subtree match
for prefix , recursive := range bp2buildKeepExistingBuildFile {
if recursive {
if strings . HasPrefix ( dir , prefix + "/" ) {
return true
}
}
}
// Default
return false
2021-04-21 13:10:09 +02:00
}
2021-04-15 23:27:08 +02:00
// MixedBuildsEnabled checks that a module is ready to be replaced by a
// converted or handcrafted Bazel target.
2021-11-09 16:29:52 +01:00
func ( b * BazelModuleBase ) MixedBuildsEnabled ( ctx ModuleContext ) bool {
if ctx . Os ( ) == Windows {
// Windows toolchains are not currently supported.
return false
}
2021-04-15 23:27:08 +02:00
if ! ctx . Config ( ) . BazelContext . BazelEnabled ( ) {
return false
}
2021-08-26 14:37:59 +02:00
if ! convertedToBazel ( ctx , ctx . Module ( ) ) {
2021-04-15 23:27:08 +02:00
return false
}
2021-08-26 14:37:59 +02:00
2021-09-20 21:14:39 +02:00
if GenerateCcLibraryStaticOnly ( ctx . Module ( ) . Name ( ) ) {
2021-05-03 11:15:48 +02:00
// Don't use partially-converted cc_library targets in mixed builds,
// since mixed builds would generally rely on both static and shared
// variants of a cc_library.
return false
}
2021-04-15 23:27:08 +02:00
return ! mixedBuildsDisabled [ ctx . Module ( ) . Name ( ) ]
2021-03-30 16:13:16 +02:00
}
2021-08-26 14:37:59 +02:00
// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
2021-11-02 07:40:51 +01:00
func convertedToBazel ( ctx BazelConversionContext , module blueprint . Module ) bool {
2021-08-26 14:37:59 +02:00
b , ok := module . ( Bazelable )
if ! ok {
return false
}
return b . convertWithBp2build ( ctx , module ) || b . HasHandcraftedLabel ( )
}
2021-02-17 16:17:28 +01:00
// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
2021-11-02 07:40:51 +01:00
func ( b * BazelModuleBase ) ConvertWithBp2build ( ctx BazelConversionContext ) bool {
2021-08-26 14:37:59 +02:00
return b . convertWithBp2build ( ctx , ctx . Module ( ) )
}
2021-11-02 07:40:51 +01:00
func ( b * BazelModuleBase ) convertWithBp2build ( ctx BazelConversionContext , module blueprint . Module ) bool {
2021-08-26 14:37:59 +02:00
if bp2buildModuleDoNotConvert [ module . Name ( ) ] {
2021-03-25 10:28:38 +01:00
return false
}
2021-03-10 08:05:59 +01:00
// Ensure that the module type of this module has a bp2build converter. This
// prevents mixed builds from using auto-converted modules just by matching
// the package dir; it also has to have a bp2build mutator as well.
2021-08-26 14:37:59 +02:00
if ctx . Config ( ) . bp2buildModuleTypeConfig [ ctx . OtherModuleType ( module ) ] == false {
2021-11-02 17:43:57 +01:00
if b , ok := module . ( Bazelable ) ; ok && b . BaseModuleType ( ) != "" {
// For modules with custom types from soong_config_module_types,
// check that their _base module type_ has a bp2build mutator.
if ctx . Config ( ) . bp2buildModuleTypeConfig [ b . BaseModuleType ( ) ] == false {
return false
}
} else {
return false
}
2021-03-10 08:05:59 +01:00
}
2021-08-26 14:37:59 +02:00
packagePath := ctx . OtherModuleDir ( module )
2021-03-10 08:05:59 +01:00
config := ctx . Config ( ) . bp2buildPackageConfig
// This is a tristate value: true, false, or unset.
propValue := b . bazelProperties . Bazel_module . Bp2build_available
if bp2buildDefaultTrueRecursively ( packagePath , config ) {
// Allow modules to explicitly opt-out.
return proptools . BoolDefault ( propValue , true )
}
// Allow modules to explicitly opt-in.
return proptools . BoolDefault ( propValue , false )
}
// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
// set of package prefixes where all modules must be converted. That is, if the
// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
// return true.
//
// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
// exactly, this module will return false early.
//
// This function will also return false if the package doesn't match anything in
// the config.
func bp2buildDefaultTrueRecursively ( packagePath string , config Bp2BuildConfig ) bool {
ret := false
2021-08-31 07:58:01 +02:00
// Check if the package path has an exact match in the config.
if config [ packagePath ] == Bp2BuildDefaultTrue || config [ packagePath ] == Bp2BuildDefaultTrueRecursively {
Add os/target configurable selects for label list attributes.
This CL is pretty large, so I recommend starting with reading the newly
added tests for the expected behavior.
This change works in conjunction with the linked CLs in the Gerrit topic.
Those CLs add support for new platform() definitions for OS targets
specified in Soong's arch.go, which are configurable through
Android.bp's `target {}` property. It works similary to previous CLs
adding support for the `arch {}` property.
These configurable props are keyed by the OS: android, linux_bionic,
windows, and so on. They map to `select` statements in label list
attributes, which this CL enables for cc_library_headers' header_libs
and export_header_lib_headers props.
This enables //bionic/libc:libc_headers to be generated correctly, from:
cc_library_headers {
name: "libc_headers",
target: {
android: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
linux_bionic: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
},
// omitted props
}
to:
cc_library_headers(
name = "libc_headers",
deps = [] + select({
"//build/bazel/platforms/os:android": [
":libc_headers_arch",
],
"//build/bazel/platforms/os:linux_bionic": [
":libc_headers_arch",
],
"//conditions:default": [],
}),
)
Test: TH
Test: Verify generated //bionic/libc:libc_headers
Fixes: 183597786
Change-Id: I01016cc2cc9a71449f02300d747f01decebf3f6e
2021-03-24 07:18:33 +01:00
return true
2021-08-31 07:58:01 +02:00
} else if config [ packagePath ] == Bp2BuildDefaultFalse {
2021-03-10 08:05:59 +01:00
return false
}
Add os/target configurable selects for label list attributes.
This CL is pretty large, so I recommend starting with reading the newly
added tests for the expected behavior.
This change works in conjunction with the linked CLs in the Gerrit topic.
Those CLs add support for new platform() definitions for OS targets
specified in Soong's arch.go, which are configurable through
Android.bp's `target {}` property. It works similary to previous CLs
adding support for the `arch {}` property.
These configurable props are keyed by the OS: android, linux_bionic,
windows, and so on. They map to `select` statements in label list
attributes, which this CL enables for cc_library_headers' header_libs
and export_header_lib_headers props.
This enables //bionic/libc:libc_headers to be generated correctly, from:
cc_library_headers {
name: "libc_headers",
target: {
android: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
linux_bionic: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
},
// omitted props
}
to:
cc_library_headers(
name = "libc_headers",
deps = [] + select({
"//build/bazel/platforms/os:android": [
":libc_headers_arch",
],
"//build/bazel/platforms/os:linux_bionic": [
":libc_headers_arch",
],
"//conditions:default": [],
}),
)
Test: TH
Test: Verify generated //bionic/libc:libc_headers
Fixes: 183597786
Change-Id: I01016cc2cc9a71449f02300d747f01decebf3f6e
2021-03-24 07:18:33 +01:00
// If not, check for the config recursively.
2021-03-10 08:05:59 +01:00
packagePrefix := ""
// e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
for _ , part := range strings . Split ( packagePath , "/" ) {
packagePrefix += part
if config [ packagePrefix ] == Bp2BuildDefaultTrueRecursively {
// package contains this prefix and this prefix should convert all modules
return true
}
// Continue to the next part of the package dir.
packagePrefix += "/"
}
return ret
2021-02-17 16:17:28 +01:00
}
2021-02-17 19:22:03 +01:00
// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
// an error if there are errors reading the file.
// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
// something more targeted based on the rule type and target.
func ( b * BazelModuleBase ) GetBazelBuildFileContents ( c Config , path , name string ) ( string , error ) {
2021-02-24 22:55:11 +01:00
if ! strings . Contains ( b . HandcraftedLabel ( ) , path ) {
return "" , fmt . Errorf ( "%q not found in bazel_module.label %q" , path , b . HandcraftedLabel ( ) )
2021-02-17 19:22:03 +01:00
}
name = filepath . Join ( path , name )
f , err := c . fs . Open ( name )
if err != nil {
return "" , err
}
defer f . Close ( )
data , err := ioutil . ReadAll ( f )
if err != nil {
return "" , err
}
return string ( data [ : ] ) , nil
}