blob: bf214a5417aeb79b4aea01fc58731cb33c2d79be [file] [log] [blame]
Liz Kammerea6666f2021-02-17 10:17:28 -05001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
Liz Kammerba3ea162021-02-17 13:22:03 -050017import (
Jingwen Chena47f28d2021-11-02 16:43:57 +000018 "android/soong/bazel"
Liz Kammerba3ea162021-02-17 13:22:03 -050019 "fmt"
20 "io/ioutil"
21 "path/filepath"
22 "strings"
23
Liz Kammerbdc60992021-02-24 16:55:11 -050024 "github.com/google/blueprint"
Liz Kammerba3ea162021-02-17 13:22:03 -050025 "github.com/google/blueprint/proptools"
26)
27
Liz Kammerba3ea162021-02-17 13:22:03 -050028// Properties contains common module properties for Bazel migration purposes.
29type properties struct {
30 // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
31 // this Soong module.
Jingwen Chena47f28d2021-11-02 16:43:57 +000032 Bazel_module bazel.BazelModuleProperties
Liz Kammerba3ea162021-02-17 13:22:03 -050033}
Liz Kammerea6666f2021-02-17 10:17:28 -050034
Jingwen Chen25825ca2021-11-15 12:28:43 +000035// namespacedVariableProperties is a map from a string representing a Soong
36// config variable namespace, like "android" or "vendor_name" to a struct
37// pointer representing the soong_config_variables property of a module created
38// by a soong_config_module_type or soong_config_module_type_import.
Jingwen Chena47f28d2021-11-02 16:43:57 +000039type namespacedVariableProperties map[string]interface{}
40
Liz Kammerea6666f2021-02-17 10:17:28 -050041// BazelModuleBase contains the property structs with metadata for modules which can be converted to
42// Bazel.
43type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050044 bazelProperties properties
Jingwen Chena47f28d2021-11-02 16:43:57 +000045
46 // namespacedVariableProperties is used for soong_config_module_type support
47 // in bp2build. Soong config modules allow users to set module properties
48 // based on custom product variables defined in Android.bp files. These
49 // variables are namespaced to prevent clobbering, especially when set from
50 // Makefiles.
51 namespacedVariableProperties namespacedVariableProperties
52
53 // baseModuleType is set when this module was created from a module type
54 // defined by a soong_config_module_type. Every soong_config_module_type
55 // "wraps" another module type, e.g. a soong_config_module_type can wrap a
56 // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
57 // This baseModuleType is set to the wrapped module type.
58 baseModuleType string
Liz Kammerea6666f2021-02-17 10:17:28 -050059}
60
61// Bazelable is specifies the interface for modules that can be converted to Bazel.
62type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -050063 bazelProps() *properties
64 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -050065 HandcraftedLabel() string
66 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Jingwen Chen55bc8202021-11-02 06:40:51 +000067 ConvertWithBp2build(ctx BazelConversionContext) bool
68 convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool
Liz Kammerba3ea162021-02-17 13:22:03 -050069 GetBazelBuildFileContents(c Config, path, name string) (string, error)
Jingwen Chena47f28d2021-11-02 16:43:57 +000070
71 // For namespaced config variable support
72 namespacedVariableProps() namespacedVariableProperties
73 setNamespacedVariableProps(props namespacedVariableProperties)
74 BaseModuleType() string
75 SetBaseModuleType(string)
Liz Kammerea6666f2021-02-17 10:17:28 -050076}
77
78// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
79type BazelModule interface {
80 Module
81 Bazelable
82}
83
84// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
85// properties.
86func InitBazelModule(module BazelModule) {
87 module.AddProperties(module.bazelProps())
88}
89
90// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -050091func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -050092 return &b.bazelProperties
93}
94
Jingwen Chena47f28d2021-11-02 16:43:57 +000095func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
96 return b.namespacedVariableProperties
97}
98
99func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
100 b.namespacedVariableProperties = props
101}
102
103func (b *BazelModuleBase) BaseModuleType() string {
104 return b.baseModuleType
105}
106
107func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
108 b.baseModuleType = baseModuleType
109}
110
Liz Kammerba3ea162021-02-17 13:22:03 -0500111// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
112func (b *BazelModuleBase) HasHandcraftedLabel() bool {
113 return b.bazelProperties.Bazel_module.Label != nil
114}
115
116// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
117func (b *BazelModuleBase) HandcraftedLabel() string {
118 return proptools.String(b.bazelProperties.Bazel_module.Label)
119}
120
Liz Kammerea6666f2021-02-17 10:17:28 -0500121// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -0500122func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
123 if b.HasHandcraftedLabel() {
124 return b.HandcraftedLabel()
125 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500126 if b.ConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500127 return bp2buildModuleLabel(ctx, module)
128 }
129 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -0500130}
131
Jingwen Chen12b4c272021-03-10 02:05:59 -0500132// Configuration to decide if modules in a directory should default to true/false for bp2build_available
133type Bp2BuildConfig map[string]BazelConversionConfigEntry
134type BazelConversionConfigEntry int
135
136const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400137 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
138 // no package path. This is also the module dir for top level Android.bp
139 // modules.
140 BP2BUILD_TOPLEVEL = "."
141
Jingwen Chen12b4c272021-03-10 02:05:59 -0500142 // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
143 // which can also mean that the key doesn't exist in a lookup.
144
145 // all modules in this package and subpackages default to bp2build_available: true.
146 // allows modules to opt-out.
147 Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
148
Jingwen Chen294e7742021-08-31 05:58:01 +0000149 // all modules in this package (not recursively) default to bp2build_available: true.
150 // allows modules to opt-out.
151 Bp2BuildDefaultTrue
152
Jingwen Chen12b4c272021-03-10 02:05:59 -0500153 // all modules in this package (not recursively) default to bp2build_available: false.
154 // allows modules to opt-in.
155 Bp2BuildDefaultFalse
156)
157
158var (
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400159 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000160 // in the synthetic Bazel workspace.
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400161 bp2buildKeepExistingBuildFile = map[string]bool{
162 // This is actually build/bazel/build.BAZEL symlinked to ./BUILD
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000163 ".":/*recursive = */ false,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400164
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000165 // build/bazel/examples/apex/... BUILD files should be generated, so
166 // build/bazel is not recursive. Instead list each subdirectory under
167 // build/bazel explicitly.
168 "build/bazel":/* recursive = */ false,
169 "build/bazel/examples/android_app":/* recursive = */ true,
Romain Jobredeaux5cc9c9d2021-08-26 15:07:48 +0000170 "build/bazel/examples/java":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000171 "build/bazel/bazel_skylib":/* recursive = */ true,
172 "build/bazel/rules":/* recursive = */ true,
173 "build/bazel/rules_cc":/* recursive = */ true,
Jingwen Chen294e7742021-08-31 05:58:01 +0000174 "build/bazel/scripts":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000175 "build/bazel/tests":/* recursive = */ true,
176 "build/bazel/platforms":/* recursive = */ true,
177 "build/bazel/product_variables":/* recursive = */ true,
Jingwen Chen5e49b822021-08-24 05:14:22 +0000178 "build/bazel_common_rules":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000179 "build/make/tools":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400180 "build/pesto":/* recursive = */ true,
181
182 // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
183 // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed
184 "external/bazelbuild-rules_android":/* recursive = */ true,
Jingwen Chen91632252021-08-10 13:00:33 +0000185 "external/bazel-skylib":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000186 "external/guava":/* recursive = */ true,
187 "external/error_prone":/* recursive = */ true,
188 "external/jsr305":/* recursive = */ true,
189 "frameworks/ex/common":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400190
Romain Jobredeauxf1b0ac82021-08-12 14:39:00 +0000191 "packages/apps/Music":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000192 "packages/apps/QuickSearchBox":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400193 "packages/apps/WallpaperPicker":/* recursive = */ false,
194
Chris Parsons494eef32021-11-09 10:29:52 -0500195 "prebuilts/gcc":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400196 "prebuilts/sdk":/* recursive = */ false,
197 "prebuilts/sdk/current/extras/app-toolkit":/* recursive = */ false,
198 "prebuilts/sdk/current/support":/* recursive = */ false,
199 "prebuilts/sdk/tools":/* recursive = */ false,
200 "prebuilts/r8":/* recursive = */ false,
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400201 }
202
Jingwen Chen12b4c272021-03-10 02:05:59 -0500203 // Configure modules in these directories to enable bp2build_available: true or false by default.
204 bp2buildDefaultConfig = Bp2BuildConfig{
Chris Parsonsa37e1952021-09-28 16:47:36 -0400205 "bionic": Bp2BuildDefaultTrueRecursively,
206 "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
Rupert Shuttleworth2ac9c482021-11-10 10:37:05 -0500207 "build/soong/cc/libbuildversion": Bp2BuildDefaultTrue, // Skip tests subdir
Chris Parsonsa37e1952021-09-28 16:47:36 -0400208 "development/sdk": Bp2BuildDefaultTrueRecursively,
209 "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
210 "external/boringssl": Bp2BuildDefaultTrueRecursively,
211 "external/brotli": Bp2BuildDefaultTrue,
212 "external/fmtlib": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400213 "external/google-benchmark": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400214 "external/googletest/googletest": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400215 "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
216 "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400217 "external/jsoncpp": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400218 "external/libcap": Bp2BuildDefaultTrueRecursively,
219 "external/libcxx": Bp2BuildDefaultTrueRecursively,
220 "external/libcxxabi": Bp2BuildDefaultTrueRecursively,
221 "external/lz4/lib": Bp2BuildDefaultTrue,
Liz Kammer2fc34892021-10-11 12:56:48 -0400222 "external/mdnsresponder": Bp2BuildDefaultTrueRecursively,
223 "external/minijail": Bp2BuildDefaultTrueRecursively,
224 "external/pcre": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400225 "external/protobuf": Bp2BuildDefaultTrueRecursively,
226 "external/python/six": Bp2BuildDefaultTrueRecursively,
227 "external/scudo": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400228 "external/selinux/libselinux": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400229 "external/zlib": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400230 "external/zstd": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400231 "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively,
232 "packages/modules/adb": Bp2BuildDefaultTrue,
233 "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively,
234 "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively,
235 "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively,
236 "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively,
237 "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively,
238 "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively,
239 "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively,
240 "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively,
241 "system/core/libasyncio": Bp2BuildDefaultTrue,
242 "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively,
243 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400244 "system/core/libpackagelistparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400245 "system/core/libprocessgroup": Bp2BuildDefaultTrue,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400246 "system/core/libprocessgroup/cgrouprc": Bp2BuildDefaultTrue,
247 "system/core/libprocessgroup/cgrouprc_format": Bp2BuildDefaultTrue,
Lukacs T. Berki497f17d2021-04-26 12:15:57 +0200248 "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400249 "system/libbase": Bp2BuildDefaultTrueRecursively,
Chris Parsonsc39f6332021-10-01 18:00:31 -0400250 "system/libziparchive": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400251 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
252 "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively,
253 "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
254 "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500255 }
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000256
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400257 // Per-module denylist to always opt modules out of both bp2build and mixed builds.
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000258 bp2buildModuleDoNotConvertList = []string{
Chris Parsons494eef32021-11-09 10:29:52 -0500259 "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610.
260
Chris Parsonsc39f6332021-10-01 18:00:31 -0400261 "libc_malloc_debug", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Rupert Shuttleworth47aa5842021-04-30 04:04:15 -0400262
Chris Parsonsc39f6332021-10-01 18:00:31 -0400263 "libbase_ndk", // http://b/186826477, fails to link libctscamera2_jni for device (required for CtsCameraTestCases)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000264
Liz Kammer0f3b7d22021-09-28 13:48:21 -0400265 "libprotobuf-python", // contains .proto sources
266 "libprotobuf-internal-protos", // we don't handle path property for fileegroups
267 "libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups
268
Chris Parsonsa37e1952021-09-28 16:47:36 -0400269 "libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds.
270 "libfdtrack", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200271
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400272 "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset
273
Chris Parsonsb97a8172021-10-04 12:48:29 -0400274 "brotli-fuzzer-corpus", // b/202015218: outputs are in location incompatible with bazel genrule handling.
Jingwen Chen294e7742021-08-31 05:58:01 +0000275
Jingwen Chendf27b7a2021-10-18 06:33:16 +0000276 // b/203369847: multiple genrules in the same package creating the same file
277 // //development/sdk/...
278 "platform_tools_properties",
279 "build_tools_source_properties",
280
Chris Parsons393002a2021-11-11 13:39:20 -0500281 "libminijail", // b/202491296: Uses unsupported c_std property.
282 "minijail0", // depends on unconverted modules: libminijail
Liz Kammer2b8004b2021-10-04 13:55:44 -0400283 "drop_privs", // depends on unconverted modules: libminijail
Liz Kammer2fc34892021-10-11 12:56:48 -0400284
Jingwen Chen790324e2021-04-30 08:20:01 +0000285 // Tests. Handle later.
286 "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
287 "libjemalloc5_integrationtest",
288 "libjemalloc5_stresstestlib",
289 "libjemalloc5_unittest",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400290
291 // APEX support
Chris Parsonsb97a8172021-10-04 12:48:29 -0400292 "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug'
Chris Parsonsa37e1952021-09-28 16:47:36 -0400293
294 "libadb_crypto", // Depends on libadb_protos
295 "libadb_crypto_static", // Depends on libadb_protos_static
296 "libadb_pairing_connection", // Depends on libadb_protos
297 "libadb_pairing_connection_static", // Depends on libadb_protos_static
298 "libadb_pairing_server", // Depends on libadb_protos
299 "libadb_pairing_server_static", // Depends on libadb_protos_static
300 "libadbd", // Depends on libadbd_core
301 "libadbd_core", // Depends on libadb_protos
302 "libadbd_services", // Depends on libadb_protos
303
304 "libadb_protos_static", // b/200601772: Requires cc_library proto support
305 "libadb_protos", // b/200601772: Requires cc_library proto support
306 "libapp_processes_protos_lite", // b/200601772: Requires cc_library proto support
Chris Parsonsc39f6332021-10-01 18:00:31 -0400307
308 "libgtest_ndk_c++", // b/201816222: Requires sdk_version support.
309 "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support.
Liz Kammer2b8004b2021-10-04 13:55:44 -0400310
311 "abb", // depends on unconverted modules: libadbd_core, libadbd_services, libcmd, libbinder, libutils, libselinux
312 "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
313 "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
314 "bionic_tests_zipalign", // depends on unconverted modules: libziparchive, libutils
315 "linker", // depends on unconverted modules: liblinker_debuggerd_stub, libdebuggerd_handler_fallback, libziparchive, liblinker_main, liblinker_malloc
316 "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_*
317 "sefcontext_compile", // depends on unconverted modules: libsepol
318 "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host
319
320 "linkerconfig", // http://b/202876379 has arch-variant static_executable
321 "mdnsd", // http://b/202876379 has arch-variant static_executable
322
323 "acvp_modulewrapper", // disabled for android x86/x86_64
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400324 }
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400325
Jingwen Chen179856a2021-05-03 09:15:48 +0000326 // Per-module denylist of cc_library modules to only generate the static
327 // variant if their shared variant isn't ready or buildable by Bazel.
328 bp2buildCcLibraryStaticOnlyList = []string{
Jingwen Chen53542922021-05-18 09:01:49 +0000329 "libjemalloc5", // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets.
Jingwen Chen179856a2021-05-03 09:15:48 +0000330 }
331
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400332 // Per-module denylist to opt modules out of mixed builds. Such modules will
333 // still be generated via bp2build.
Chris Parsons2c788392021-08-10 11:58:07 -0400334 mixedBuildsDisabledList = []string{
Wei Li455ba832021-11-04 22:58:12 +0000335 "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
336 "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.
Liz Kammerc3192992021-11-16 17:01:11 -0500337
338 "cap_names.h", // TODO(b/204913827) runfiles need to be handled in mixed builds
339 "libcap", // TODO(b/204913827) runfiles need to be handled in mixed builds
Chris Parsons2c788392021-08-10 11:58:07 -0400340 }
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000341
342 // Used for quicker lookups
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400343 bp2buildModuleDoNotConvert = map[string]bool{}
Jingwen Chen179856a2021-05-03 09:15:48 +0000344 bp2buildCcLibraryStaticOnly = map[string]bool{}
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400345 mixedBuildsDisabled = map[string]bool{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500346)
347
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000348func init() {
349 for _, moduleName := range bp2buildModuleDoNotConvertList {
350 bp2buildModuleDoNotConvert[moduleName] = true
351 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400352
Jingwen Chen179856a2021-05-03 09:15:48 +0000353 for _, moduleName := range bp2buildCcLibraryStaticOnlyList {
354 bp2buildCcLibraryStaticOnly[moduleName] = true
355 }
356
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400357 for _, moduleName := range mixedBuildsDisabledList {
358 mixedBuildsDisabled[moduleName] = true
359 }
360}
361
Chris Parsons953b3562021-09-20 15:14:39 -0400362func GenerateCcLibraryStaticOnly(moduleName string) bool {
363 return bp2buildCcLibraryStaticOnly[moduleName]
Jingwen Chen179856a2021-05-03 09:15:48 +0000364}
365
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400366func ShouldKeepExistingBuildFileForDir(dir string) bool {
367 if _, ok := bp2buildKeepExistingBuildFile[dir]; ok {
368 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400369 return true
370 }
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400371 // Check if subtree match
372 for prefix, recursive := range bp2buildKeepExistingBuildFile {
373 if recursive {
374 if strings.HasPrefix(dir, prefix+"/") {
375 return true
376 }
377 }
378 }
379 // Default
380 return false
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400381}
382
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400383// MixedBuildsEnabled checks that a module is ready to be replaced by a
384// converted or handcrafted Bazel target.
Chris Parsons494eef32021-11-09 10:29:52 -0500385func (b *BazelModuleBase) MixedBuildsEnabled(ctx ModuleContext) bool {
386 if ctx.Os() == Windows {
387 // Windows toolchains are not currently supported.
388 return false
389 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400390 if !ctx.Config().BazelContext.BazelEnabled() {
391 return false
392 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400393 if !convertedToBazel(ctx, ctx.Module()) {
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400394 return false
395 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400396
Chris Parsons953b3562021-09-20 15:14:39 -0400397 if GenerateCcLibraryStaticOnly(ctx.Module().Name()) {
Jingwen Chen179856a2021-05-03 09:15:48 +0000398 // Don't use partially-converted cc_library targets in mixed builds,
399 // since mixed builds would generally rely on both static and shared
400 // variants of a cc_library.
401 return false
402 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400403 return !mixedBuildsDisabled[ctx.Module().Name()]
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000404}
405
Liz Kammer6eff3232021-08-26 08:37:59 -0400406// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000407func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400408 b, ok := module.(Bazelable)
409 if !ok {
410 return false
411 }
412 return b.convertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
413}
414
Liz Kammerea6666f2021-02-17 10:17:28 -0500415// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000416func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionContext) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400417 return b.convertWithBp2build(ctx, ctx.Module())
418}
419
Jingwen Chen55bc8202021-11-02 06:40:51 +0000420func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400421 if bp2buildModuleDoNotConvert[module.Name()] {
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000422 return false
423 }
424
Jingwen Chen12b4c272021-03-10 02:05:59 -0500425 // Ensure that the module type of this module has a bp2build converter. This
426 // prevents mixed builds from using auto-converted modules just by matching
427 // the package dir; it also has to have a bp2build mutator as well.
Liz Kammer6eff3232021-08-26 08:37:59 -0400428 if ctx.Config().bp2buildModuleTypeConfig[ctx.OtherModuleType(module)] == false {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000429 if b, ok := module.(Bazelable); ok && b.BaseModuleType() != "" {
430 // For modules with custom types from soong_config_module_types,
431 // check that their _base module type_ has a bp2build mutator.
432 if ctx.Config().bp2buildModuleTypeConfig[b.BaseModuleType()] == false {
433 return false
434 }
435 } else {
436 return false
437 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500438 }
439
Liz Kammer6eff3232021-08-26 08:37:59 -0400440 packagePath := ctx.OtherModuleDir(module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500441 config := ctx.Config().bp2buildPackageConfig
442
443 // This is a tristate value: true, false, or unset.
444 propValue := b.bazelProperties.Bazel_module.Bp2build_available
445 if bp2buildDefaultTrueRecursively(packagePath, config) {
446 // Allow modules to explicitly opt-out.
447 return proptools.BoolDefault(propValue, true)
448 }
449
450 // Allow modules to explicitly opt-in.
451 return proptools.BoolDefault(propValue, false)
452}
453
454// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
455// set of package prefixes where all modules must be converted. That is, if the
456// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
457// return true.
458//
459// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
460// exactly, this module will return false early.
461//
462// This function will also return false if the package doesn't match anything in
463// the config.
464func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
465 ret := false
466
Jingwen Chen294e7742021-08-31 05:58:01 +0000467 // Check if the package path has an exact match in the config.
468 if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively {
Jingwen Chen91220d72021-03-24 02:18:33 -0400469 return true
Jingwen Chen294e7742021-08-31 05:58:01 +0000470 } else if config[packagePath] == Bp2BuildDefaultFalse {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500471 return false
472 }
473
Jingwen Chen91220d72021-03-24 02:18:33 -0400474 // If not, check for the config recursively.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500475 packagePrefix := ""
476 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
477 for _, part := range strings.Split(packagePath, "/") {
478 packagePrefix += part
479 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
480 // package contains this prefix and this prefix should convert all modules
481 return true
482 }
483 // Continue to the next part of the package dir.
484 packagePrefix += "/"
485 }
486
487 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500488}
Liz Kammerba3ea162021-02-17 13:22:03 -0500489
490// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
491// an error if there are errors reading the file.
492// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
493// something more targeted based on the rule type and target.
494func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500495 if !strings.Contains(b.HandcraftedLabel(), path) {
496 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500497 }
498 name = filepath.Join(path, name)
499 f, err := c.fs.Open(name)
500 if err != nil {
501 return "", err
502 }
503 defer f.Close()
504
505 data, err := ioutil.ReadAll(f)
506 if err != nil {
507 return "", err
508 }
509 return string(data[:]), nil
510}