blob: 26d70e48febc4ea3e4c7d105d6e76afc9c851744 [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 Chena47f28d2021-11-02 16:43:57 +000035type namespacedVariableProperties map[string]interface{}
36
Liz Kammerea6666f2021-02-17 10:17:28 -050037// BazelModuleBase contains the property structs with metadata for modules which can be converted to
38// Bazel.
39type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050040 bazelProperties properties
Jingwen Chena47f28d2021-11-02 16:43:57 +000041
42 // namespacedVariableProperties is used for soong_config_module_type support
43 // in bp2build. Soong config modules allow users to set module properties
44 // based on custom product variables defined in Android.bp files. These
45 // variables are namespaced to prevent clobbering, especially when set from
46 // Makefiles.
47 namespacedVariableProperties namespacedVariableProperties
48
49 // baseModuleType is set when this module was created from a module type
50 // defined by a soong_config_module_type. Every soong_config_module_type
51 // "wraps" another module type, e.g. a soong_config_module_type can wrap a
52 // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
53 // This baseModuleType is set to the wrapped module type.
54 baseModuleType string
Liz Kammerea6666f2021-02-17 10:17:28 -050055}
56
57// Bazelable is specifies the interface for modules that can be converted to Bazel.
58type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -050059 bazelProps() *properties
60 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -050061 HandcraftedLabel() string
62 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Jingwen Chen55bc8202021-11-02 06:40:51 +000063 ConvertWithBp2build(ctx BazelConversionContext) bool
64 convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool
Liz Kammerba3ea162021-02-17 13:22:03 -050065 GetBazelBuildFileContents(c Config, path, name string) (string, error)
Jingwen Chena47f28d2021-11-02 16:43:57 +000066
67 // For namespaced config variable support
68 namespacedVariableProps() namespacedVariableProperties
69 setNamespacedVariableProps(props namespacedVariableProperties)
70 BaseModuleType() string
71 SetBaseModuleType(string)
Liz Kammerea6666f2021-02-17 10:17:28 -050072}
73
74// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
75type BazelModule interface {
76 Module
77 Bazelable
78}
79
80// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
81// properties.
82func InitBazelModule(module BazelModule) {
83 module.AddProperties(module.bazelProps())
84}
85
86// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -050087func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -050088 return &b.bazelProperties
89}
90
Jingwen Chena47f28d2021-11-02 16:43:57 +000091func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
92 return b.namespacedVariableProperties
93}
94
95func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
96 b.namespacedVariableProperties = props
97}
98
99func (b *BazelModuleBase) BaseModuleType() string {
100 return b.baseModuleType
101}
102
103func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
104 b.baseModuleType = baseModuleType
105}
106
Liz Kammerba3ea162021-02-17 13:22:03 -0500107// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
108func (b *BazelModuleBase) HasHandcraftedLabel() bool {
109 return b.bazelProperties.Bazel_module.Label != nil
110}
111
112// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
113func (b *BazelModuleBase) HandcraftedLabel() string {
114 return proptools.String(b.bazelProperties.Bazel_module.Label)
115}
116
Liz Kammerea6666f2021-02-17 10:17:28 -0500117// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -0500118func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
119 if b.HasHandcraftedLabel() {
120 return b.HandcraftedLabel()
121 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500122 if b.ConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500123 return bp2buildModuleLabel(ctx, module)
124 }
125 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -0500126}
127
Jingwen Chen12b4c272021-03-10 02:05:59 -0500128// Configuration to decide if modules in a directory should default to true/false for bp2build_available
129type Bp2BuildConfig map[string]BazelConversionConfigEntry
130type BazelConversionConfigEntry int
131
132const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400133 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
134 // no package path. This is also the module dir for top level Android.bp
135 // modules.
136 BP2BUILD_TOPLEVEL = "."
137
Jingwen Chen12b4c272021-03-10 02:05:59 -0500138 // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
139 // which can also mean that the key doesn't exist in a lookup.
140
141 // all modules in this package and subpackages default to bp2build_available: true.
142 // allows modules to opt-out.
143 Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
144
Jingwen Chen294e7742021-08-31 05:58:01 +0000145 // all modules in this package (not recursively) default to bp2build_available: true.
146 // allows modules to opt-out.
147 Bp2BuildDefaultTrue
148
Jingwen Chen12b4c272021-03-10 02:05:59 -0500149 // all modules in this package (not recursively) default to bp2build_available: false.
150 // allows modules to opt-in.
151 Bp2BuildDefaultFalse
152)
153
154var (
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400155 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000156 // in the synthetic Bazel workspace.
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400157 bp2buildKeepExistingBuildFile = map[string]bool{
158 // This is actually build/bazel/build.BAZEL symlinked to ./BUILD
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000159 ".":/*recursive = */ false,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400160
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000161 // build/bazel/examples/apex/... BUILD files should be generated, so
162 // build/bazel is not recursive. Instead list each subdirectory under
163 // build/bazel explicitly.
164 "build/bazel":/* recursive = */ false,
165 "build/bazel/examples/android_app":/* recursive = */ true,
Romain Jobredeaux5cc9c9d2021-08-26 15:07:48 +0000166 "build/bazel/examples/java":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000167 "build/bazel/bazel_skylib":/* recursive = */ true,
168 "build/bazel/rules":/* recursive = */ true,
169 "build/bazel/rules_cc":/* recursive = */ true,
Jingwen Chen294e7742021-08-31 05:58:01 +0000170 "build/bazel/scripts":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000171 "build/bazel/tests":/* recursive = */ true,
172 "build/bazel/platforms":/* recursive = */ true,
173 "build/bazel/product_variables":/* recursive = */ true,
Jingwen Chen5e49b822021-08-24 05:14:22 +0000174 "build/bazel_common_rules":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000175 "build/make/tools":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400176 "build/pesto":/* recursive = */ true,
177
178 // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
179 // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed
180 "external/bazelbuild-rules_android":/* recursive = */ true,
Jingwen Chen91632252021-08-10 13:00:33 +0000181 "external/bazel-skylib":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000182 "external/guava":/* recursive = */ true,
183 "external/error_prone":/* recursive = */ true,
184 "external/jsr305":/* recursive = */ true,
185 "frameworks/ex/common":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400186
Romain Jobredeauxf1b0ac82021-08-12 14:39:00 +0000187 "packages/apps/Music":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000188 "packages/apps/QuickSearchBox":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400189 "packages/apps/WallpaperPicker":/* recursive = */ false,
190
Chris Parsons494eef32021-11-09 10:29:52 -0500191 "prebuilts/gcc":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400192 "prebuilts/sdk":/* recursive = */ false,
193 "prebuilts/sdk/current/extras/app-toolkit":/* recursive = */ false,
194 "prebuilts/sdk/current/support":/* recursive = */ false,
195 "prebuilts/sdk/tools":/* recursive = */ false,
196 "prebuilts/r8":/* recursive = */ false,
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400197 }
198
Jingwen Chen12b4c272021-03-10 02:05:59 -0500199 // Configure modules in these directories to enable bp2build_available: true or false by default.
200 bp2buildDefaultConfig = Bp2BuildConfig{
Chris Parsonsa37e1952021-09-28 16:47:36 -0400201 "bionic": Bp2BuildDefaultTrueRecursively,
202 "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
203 "development/sdk": Bp2BuildDefaultTrueRecursively,
204 "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
205 "external/boringssl": Bp2BuildDefaultTrueRecursively,
206 "external/brotli": Bp2BuildDefaultTrue,
207 "external/fmtlib": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400208 "external/google-benchmark": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400209 "external/googletest/googletest": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400210 "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
211 "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400212 "external/jsoncpp": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400213 "external/libcap": Bp2BuildDefaultTrueRecursively,
214 "external/libcxx": Bp2BuildDefaultTrueRecursively,
215 "external/libcxxabi": Bp2BuildDefaultTrueRecursively,
216 "external/lz4/lib": Bp2BuildDefaultTrue,
Liz Kammer2fc34892021-10-11 12:56:48 -0400217 "external/mdnsresponder": Bp2BuildDefaultTrueRecursively,
218 "external/minijail": Bp2BuildDefaultTrueRecursively,
219 "external/pcre": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400220 "external/protobuf": Bp2BuildDefaultTrueRecursively,
221 "external/python/six": Bp2BuildDefaultTrueRecursively,
222 "external/scudo": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400223 "external/selinux/libselinux": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400224 "external/zlib": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400225 "external/zstd": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400226 "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively,
227 "packages/modules/adb": Bp2BuildDefaultTrue,
228 "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively,
229 "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively,
230 "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively,
231 "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively,
232 "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively,
233 "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively,
234 "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively,
235 "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively,
236 "system/core/libasyncio": Bp2BuildDefaultTrue,
237 "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively,
238 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400239 "system/core/libpackagelistparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400240 "system/core/libprocessgroup": Bp2BuildDefaultTrue,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400241 "system/core/libprocessgroup/cgrouprc": Bp2BuildDefaultTrue,
242 "system/core/libprocessgroup/cgrouprc_format": Bp2BuildDefaultTrue,
Lukacs T. Berki497f17d2021-04-26 12:15:57 +0200243 "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400244 "system/libbase": Bp2BuildDefaultTrueRecursively,
Chris Parsonsc39f6332021-10-01 18:00:31 -0400245 "system/libziparchive": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400246 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
247 "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively,
248 "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
249 "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500250 }
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000251
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400252 // Per-module denylist to always opt modules out of both bp2build and mixed builds.
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000253 bp2buildModuleDoNotConvertList = []string{
Chris Parsons494eef32021-11-09 10:29:52 -0500254 "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610.
255
Chris Parsonsc39f6332021-10-01 18:00:31 -0400256 "libc_malloc_debug", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Rupert Shuttleworth47aa5842021-04-30 04:04:15 -0400257
Chris Parsonsc39f6332021-10-01 18:00:31 -0400258 "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 +0000259
Liz Kammer0f3b7d22021-09-28 13:48:21 -0400260 "libprotobuf-python", // contains .proto sources
261 "libprotobuf-internal-protos", // we don't handle path property for fileegroups
262 "libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups
263
Chris Parsonsa37e1952021-09-28 16:47:36 -0400264 "libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds.
265 "libfdtrack", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200266
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400267 "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset
268
Chris Parsonsb97a8172021-10-04 12:48:29 -0400269 "brotli-fuzzer-corpus", // b/202015218: outputs are in location incompatible with bazel genrule handling.
Jingwen Chen294e7742021-08-31 05:58:01 +0000270
Jingwen Chendf27b7a2021-10-18 06:33:16 +0000271 // b/203369847: multiple genrules in the same package creating the same file
272 // //development/sdk/...
273 "platform_tools_properties",
274 "build_tools_source_properties",
275
Chris Parsons393002a2021-11-11 13:39:20 -0500276 "libminijail", // b/202491296: Uses unsupported c_std property.
277 "minijail0", // depends on unconverted modules: libminijail
Liz Kammer2b8004b2021-10-04 13:55:44 -0400278 "drop_privs", // depends on unconverted modules: libminijail
Liz Kammer2fc34892021-10-11 12:56:48 -0400279
Jingwen Chen790324e2021-04-30 08:20:01 +0000280 // Tests. Handle later.
281 "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
282 "libjemalloc5_integrationtest",
283 "libjemalloc5_stresstestlib",
284 "libjemalloc5_unittest",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400285
286 // APEX support
Chris Parsonsb97a8172021-10-04 12:48:29 -0400287 "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug'
Chris Parsonsa37e1952021-09-28 16:47:36 -0400288
289 "libadb_crypto", // Depends on libadb_protos
290 "libadb_crypto_static", // Depends on libadb_protos_static
291 "libadb_pairing_connection", // Depends on libadb_protos
292 "libadb_pairing_connection_static", // Depends on libadb_protos_static
293 "libadb_pairing_server", // Depends on libadb_protos
294 "libadb_pairing_server_static", // Depends on libadb_protos_static
295 "libadbd", // Depends on libadbd_core
296 "libadbd_core", // Depends on libadb_protos
297 "libadbd_services", // Depends on libadb_protos
298
299 "libadb_protos_static", // b/200601772: Requires cc_library proto support
300 "libadb_protos", // b/200601772: Requires cc_library proto support
301 "libapp_processes_protos_lite", // b/200601772: Requires cc_library proto support
Chris Parsonsc39f6332021-10-01 18:00:31 -0400302
303 "libgtest_ndk_c++", // b/201816222: Requires sdk_version support.
304 "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support.
Liz Kammer2b8004b2021-10-04 13:55:44 -0400305
306 "abb", // depends on unconverted modules: libadbd_core, libadbd_services, libcmd, libbinder, libutils, libselinux
307 "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
308 "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
309 "bionic_tests_zipalign", // depends on unconverted modules: libziparchive, libutils
310 "linker", // depends on unconverted modules: liblinker_debuggerd_stub, libdebuggerd_handler_fallback, libziparchive, liblinker_main, liblinker_malloc
311 "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_*
312 "sefcontext_compile", // depends on unconverted modules: libsepol
313 "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host
314
315 "linkerconfig", // http://b/202876379 has arch-variant static_executable
316 "mdnsd", // http://b/202876379 has arch-variant static_executable
317
318 "acvp_modulewrapper", // disabled for android x86/x86_64
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400319 }
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400320
Jingwen Chen179856a2021-05-03 09:15:48 +0000321 // Per-module denylist of cc_library modules to only generate the static
322 // variant if their shared variant isn't ready or buildable by Bazel.
323 bp2buildCcLibraryStaticOnlyList = []string{
Jingwen Chen53542922021-05-18 09:01:49 +0000324 "libjemalloc5", // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets.
Jingwen Chen179856a2021-05-03 09:15:48 +0000325 }
326
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400327 // Per-module denylist to opt modules out of mixed builds. Such modules will
328 // still be generated via bp2build.
Chris Parsons2c788392021-08-10 11:58:07 -0400329 mixedBuildsDisabledList = []string{
Wei Lic989eaf2021-11-03 01:48:03 +0000330 "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
331 "func_to_syscall_nrs", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
332 "libseccomp_policy_app_zygote_sources", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
333 "libseccomp_policy_app_sources", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
334 "libseccomp_policy_system_sources", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
335 "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.
Chris Parsons2c788392021-08-10 11:58:07 -0400336 }
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000337
338 // Used for quicker lookups
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400339 bp2buildModuleDoNotConvert = map[string]bool{}
Jingwen Chen179856a2021-05-03 09:15:48 +0000340 bp2buildCcLibraryStaticOnly = map[string]bool{}
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400341 mixedBuildsDisabled = map[string]bool{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500342)
343
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000344func init() {
345 for _, moduleName := range bp2buildModuleDoNotConvertList {
346 bp2buildModuleDoNotConvert[moduleName] = true
347 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400348
Jingwen Chen179856a2021-05-03 09:15:48 +0000349 for _, moduleName := range bp2buildCcLibraryStaticOnlyList {
350 bp2buildCcLibraryStaticOnly[moduleName] = true
351 }
352
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400353 for _, moduleName := range mixedBuildsDisabledList {
354 mixedBuildsDisabled[moduleName] = true
355 }
356}
357
Chris Parsons953b3562021-09-20 15:14:39 -0400358func GenerateCcLibraryStaticOnly(moduleName string) bool {
359 return bp2buildCcLibraryStaticOnly[moduleName]
Jingwen Chen179856a2021-05-03 09:15:48 +0000360}
361
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400362func ShouldKeepExistingBuildFileForDir(dir string) bool {
363 if _, ok := bp2buildKeepExistingBuildFile[dir]; ok {
364 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400365 return true
366 }
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400367 // Check if subtree match
368 for prefix, recursive := range bp2buildKeepExistingBuildFile {
369 if recursive {
370 if strings.HasPrefix(dir, prefix+"/") {
371 return true
372 }
373 }
374 }
375 // Default
376 return false
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400377}
378
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400379// MixedBuildsEnabled checks that a module is ready to be replaced by a
380// converted or handcrafted Bazel target.
Chris Parsons494eef32021-11-09 10:29:52 -0500381func (b *BazelModuleBase) MixedBuildsEnabled(ctx ModuleContext) bool {
382 if ctx.Os() == Windows {
383 // Windows toolchains are not currently supported.
384 return false
385 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400386 if !ctx.Config().BazelContext.BazelEnabled() {
387 return false
388 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400389 if !convertedToBazel(ctx, ctx.Module()) {
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400390 return false
391 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400392
Chris Parsons953b3562021-09-20 15:14:39 -0400393 if GenerateCcLibraryStaticOnly(ctx.Module().Name()) {
Jingwen Chen179856a2021-05-03 09:15:48 +0000394 // Don't use partially-converted cc_library targets in mixed builds,
395 // since mixed builds would generally rely on both static and shared
396 // variants of a cc_library.
397 return false
398 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400399 return !mixedBuildsDisabled[ctx.Module().Name()]
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000400}
401
Liz Kammer6eff3232021-08-26 08:37:59 -0400402// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000403func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400404 b, ok := module.(Bazelable)
405 if !ok {
406 return false
407 }
408 return b.convertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
409}
410
Liz Kammerea6666f2021-02-17 10:17:28 -0500411// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000412func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionContext) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400413 return b.convertWithBp2build(ctx, ctx.Module())
414}
415
Jingwen Chen55bc8202021-11-02 06:40:51 +0000416func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400417 if bp2buildModuleDoNotConvert[module.Name()] {
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000418 return false
419 }
420
Jingwen Chen12b4c272021-03-10 02:05:59 -0500421 // Ensure that the module type of this module has a bp2build converter. This
422 // prevents mixed builds from using auto-converted modules just by matching
423 // the package dir; it also has to have a bp2build mutator as well.
Liz Kammer6eff3232021-08-26 08:37:59 -0400424 if ctx.Config().bp2buildModuleTypeConfig[ctx.OtherModuleType(module)] == false {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000425 if b, ok := module.(Bazelable); ok && b.BaseModuleType() != "" {
426 // For modules with custom types from soong_config_module_types,
427 // check that their _base module type_ has a bp2build mutator.
428 if ctx.Config().bp2buildModuleTypeConfig[b.BaseModuleType()] == false {
429 return false
430 }
431 } else {
432 return false
433 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500434 }
435
Liz Kammer6eff3232021-08-26 08:37:59 -0400436 packagePath := ctx.OtherModuleDir(module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500437 config := ctx.Config().bp2buildPackageConfig
438
439 // This is a tristate value: true, false, or unset.
440 propValue := b.bazelProperties.Bazel_module.Bp2build_available
441 if bp2buildDefaultTrueRecursively(packagePath, config) {
442 // Allow modules to explicitly opt-out.
443 return proptools.BoolDefault(propValue, true)
444 }
445
446 // Allow modules to explicitly opt-in.
447 return proptools.BoolDefault(propValue, false)
448}
449
450// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
451// set of package prefixes where all modules must be converted. That is, if the
452// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
453// return true.
454//
455// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
456// exactly, this module will return false early.
457//
458// This function will also return false if the package doesn't match anything in
459// the config.
460func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
461 ret := false
462
Jingwen Chen294e7742021-08-31 05:58:01 +0000463 // Check if the package path has an exact match in the config.
464 if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively {
Jingwen Chen91220d72021-03-24 02:18:33 -0400465 return true
Jingwen Chen294e7742021-08-31 05:58:01 +0000466 } else if config[packagePath] == Bp2BuildDefaultFalse {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500467 return false
468 }
469
Jingwen Chen91220d72021-03-24 02:18:33 -0400470 // If not, check for the config recursively.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500471 packagePrefix := ""
472 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
473 for _, part := range strings.Split(packagePath, "/") {
474 packagePrefix += part
475 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
476 // package contains this prefix and this prefix should convert all modules
477 return true
478 }
479 // Continue to the next part of the package dir.
480 packagePrefix += "/"
481 }
482
483 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500484}
Liz Kammerba3ea162021-02-17 13:22:03 -0500485
486// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
487// an error if there are errors reading the file.
488// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
489// something more targeted based on the rule type and target.
490func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500491 if !strings.Contains(b.HandcraftedLabel(), path) {
492 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500493 }
494 name = filepath.Join(path, name)
495 f, err := c.fs.Open(name)
496 if err != nil {
497 return "", err
498 }
499 defer f.Close()
500
501 data, err := ioutil.ReadAll(f)
502 if err != nil {
503 return "", err
504 }
505 return string(data[:]), nil
506}