blob: e97acffafbb4fe9b53f366c5785526356ffed66e [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 (
18 "fmt"
19 "io/ioutil"
20 "path/filepath"
21 "strings"
22
Liz Kammerbdc60992021-02-24 16:55:11 -050023 "github.com/google/blueprint"
Liz Kammerba3ea162021-02-17 13:22:03 -050024 "github.com/google/blueprint/proptools"
25)
26
Jingwen Chen01812022021-11-19 14:29:43 +000027type bazelModuleProperties struct {
28 // The label of the Bazel target replacing this Soong module. When run in conversion mode, this
29 // will import the handcrafted build target into the autogenerated file. Note: this may result in
30 // a conflict due to duplicate targets if bp2build_available is also set.
31 Label *string
32
33 // If true, bp2build will generate the converted Bazel target for this module. Note: this may
34 // cause a conflict due to the duplicate targets if label is also set.
35 //
36 // This is a bool pointer to support tristates: true, false, not set.
37 //
38 // To opt-in a module, set bazel_module: { bp2build_available: true }
39 // To opt-out a module, set bazel_module: { bp2build_available: false }
40 // To defer the default setting for the directory, do not set the value.
41 Bp2build_available *bool
42}
43
Liz Kammerba3ea162021-02-17 13:22:03 -050044// Properties contains common module properties for Bazel migration purposes.
45type properties struct {
46 // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
47 // this Soong module.
Jingwen Chen01812022021-11-19 14:29:43 +000048 Bazel_module bazelModuleProperties
Liz Kammerba3ea162021-02-17 13:22:03 -050049}
Liz Kammerea6666f2021-02-17 10:17:28 -050050
Jingwen Chen25825ca2021-11-15 12:28:43 +000051// namespacedVariableProperties is a map from a string representing a Soong
Jingwen Chen84817de2021-11-17 10:57:35 +000052// config variable namespace, like "android" or "vendor_name" to a slice of
53// pointer to a struct containing a single field called Soong_config_variables
54// whose value mirrors the structure in the Blueprint file.
55type namespacedVariableProperties map[string][]interface{}
Jingwen Chena47f28d2021-11-02 16:43:57 +000056
Liz Kammerea6666f2021-02-17 10:17:28 -050057// BazelModuleBase contains the property structs with metadata for modules which can be converted to
58// Bazel.
59type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050060 bazelProperties properties
Jingwen Chena47f28d2021-11-02 16:43:57 +000061
62 // namespacedVariableProperties is used for soong_config_module_type support
63 // in bp2build. Soong config modules allow users to set module properties
64 // based on custom product variables defined in Android.bp files. These
65 // variables are namespaced to prevent clobbering, especially when set from
66 // Makefiles.
67 namespacedVariableProperties namespacedVariableProperties
68
69 // baseModuleType is set when this module was created from a module type
70 // defined by a soong_config_module_type. Every soong_config_module_type
71 // "wraps" another module type, e.g. a soong_config_module_type can wrap a
72 // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
73 // This baseModuleType is set to the wrapped module type.
74 baseModuleType string
Liz Kammerea6666f2021-02-17 10:17:28 -050075}
76
77// Bazelable is specifies the interface for modules that can be converted to Bazel.
78type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -050079 bazelProps() *properties
80 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -050081 HandcraftedLabel() string
82 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Jingwen Chen55bc8202021-11-02 06:40:51 +000083 ConvertWithBp2build(ctx BazelConversionContext) bool
84 convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool
Liz Kammerba3ea162021-02-17 13:22:03 -050085 GetBazelBuildFileContents(c Config, path, name string) (string, error)
Jingwen Chena47f28d2021-11-02 16:43:57 +000086
Jingwen Chen84817de2021-11-17 10:57:35 +000087 // namespacedVariableProps is a map from a soong config variable namespace
88 // (e.g. acme, android) to a map of interfaces{}, which are really
89 // reflect.Struct pointers, representing the value of the
90 // soong_config_variables property of a module. The struct pointer is the
91 // one with the single member called Soong_config_variables, which itself is
92 // a struct containing fields for each supported feature in that namespace.
93 //
94 // The reason for using an slice of interface{} is to support defaults
95 // propagation of the struct pointers.
Jingwen Chena47f28d2021-11-02 16:43:57 +000096 namespacedVariableProps() namespacedVariableProperties
97 setNamespacedVariableProps(props namespacedVariableProperties)
98 BaseModuleType() string
Jingwen Chen84817de2021-11-17 10:57:35 +000099 SetBaseModuleType(baseModuleType string)
Liz Kammerea6666f2021-02-17 10:17:28 -0500100}
101
102// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
103type BazelModule interface {
104 Module
105 Bazelable
106}
107
108// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
109// properties.
110func InitBazelModule(module BazelModule) {
111 module.AddProperties(module.bazelProps())
112}
113
114// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -0500115func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -0500116 return &b.bazelProperties
117}
118
Jingwen Chena47f28d2021-11-02 16:43:57 +0000119func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
120 return b.namespacedVariableProperties
121}
122
123func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
124 b.namespacedVariableProperties = props
125}
126
127func (b *BazelModuleBase) BaseModuleType() string {
128 return b.baseModuleType
129}
130
131func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
132 b.baseModuleType = baseModuleType
133}
134
Liz Kammerba3ea162021-02-17 13:22:03 -0500135// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
136func (b *BazelModuleBase) HasHandcraftedLabel() bool {
137 return b.bazelProperties.Bazel_module.Label != nil
138}
139
140// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
141func (b *BazelModuleBase) HandcraftedLabel() string {
142 return proptools.String(b.bazelProperties.Bazel_module.Label)
143}
144
Liz Kammerea6666f2021-02-17 10:17:28 -0500145// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -0500146func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
147 if b.HasHandcraftedLabel() {
148 return b.HandcraftedLabel()
149 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500150 if b.ConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500151 return bp2buildModuleLabel(ctx, module)
152 }
153 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -0500154}
155
Jingwen Chen12b4c272021-03-10 02:05:59 -0500156// Configuration to decide if modules in a directory should default to true/false for bp2build_available
157type Bp2BuildConfig map[string]BazelConversionConfigEntry
158type BazelConversionConfigEntry int
159
160const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400161 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
162 // no package path. This is also the module dir for top level Android.bp
163 // modules.
164 BP2BUILD_TOPLEVEL = "."
165
Jingwen Chen12b4c272021-03-10 02:05:59 -0500166 // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
167 // which can also mean that the key doesn't exist in a lookup.
168
169 // all modules in this package and subpackages default to bp2build_available: true.
170 // allows modules to opt-out.
171 Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
172
Jingwen Chen294e7742021-08-31 05:58:01 +0000173 // all modules in this package (not recursively) default to bp2build_available: true.
174 // allows modules to opt-out.
175 Bp2BuildDefaultTrue
176
Jingwen Chen12b4c272021-03-10 02:05:59 -0500177 // all modules in this package (not recursively) default to bp2build_available: false.
178 // allows modules to opt-in.
179 Bp2BuildDefaultFalse
180)
181
182var (
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400183 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000184 // in the synthetic Bazel workspace.
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400185 bp2buildKeepExistingBuildFile = map[string]bool{
186 // This is actually build/bazel/build.BAZEL symlinked to ./BUILD
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000187 ".":/*recursive = */ false,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400188
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000189 // build/bazel/examples/apex/... BUILD files should be generated, so
190 // build/bazel is not recursive. Instead list each subdirectory under
191 // build/bazel explicitly.
192 "build/bazel":/* recursive = */ false,
193 "build/bazel/examples/android_app":/* recursive = */ true,
Romain Jobredeaux5cc9c9d2021-08-26 15:07:48 +0000194 "build/bazel/examples/java":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000195 "build/bazel/bazel_skylib":/* recursive = */ true,
196 "build/bazel/rules":/* recursive = */ true,
197 "build/bazel/rules_cc":/* recursive = */ true,
Jingwen Chen294e7742021-08-31 05:58:01 +0000198 "build/bazel/scripts":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000199 "build/bazel/tests":/* recursive = */ true,
200 "build/bazel/platforms":/* recursive = */ true,
201 "build/bazel/product_variables":/* recursive = */ true,
Jingwen Chen5e49b822021-08-24 05:14:22 +0000202 "build/bazel_common_rules":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000203 "build/make/tools":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400204 "build/pesto":/* recursive = */ true,
205
206 // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
207 // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed
208 "external/bazelbuild-rules_android":/* recursive = */ true,
Jingwen Chen91632252021-08-10 13:00:33 +0000209 "external/bazel-skylib":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000210 "external/guava":/* recursive = */ true,
211 "external/error_prone":/* recursive = */ true,
212 "external/jsr305":/* recursive = */ true,
213 "frameworks/ex/common":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400214
Romain Jobredeauxf1b0ac82021-08-12 14:39:00 +0000215 "packages/apps/Music":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000216 "packages/apps/QuickSearchBox":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400217 "packages/apps/WallpaperPicker":/* recursive = */ false,
218
Chris Parsons494eef32021-11-09 10:29:52 -0500219 "prebuilts/gcc":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400220 "prebuilts/sdk":/* recursive = */ false,
221 "prebuilts/sdk/current/extras/app-toolkit":/* recursive = */ false,
222 "prebuilts/sdk/current/support":/* recursive = */ false,
223 "prebuilts/sdk/tools":/* recursive = */ false,
224 "prebuilts/r8":/* recursive = */ false,
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400225 }
226
Jingwen Chen12b4c272021-03-10 02:05:59 -0500227 // Configure modules in these directories to enable bp2build_available: true or false by default.
228 bp2buildDefaultConfig = Bp2BuildConfig{
Chris Parsonsa37e1952021-09-28 16:47:36 -0400229 "bionic": Bp2BuildDefaultTrueRecursively,
230 "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
Rupert Shuttleworth2ac9c482021-11-10 10:37:05 -0500231 "build/soong/cc/libbuildversion": Bp2BuildDefaultTrue, // Skip tests subdir
Chris Parsonsa37e1952021-09-28 16:47:36 -0400232 "development/sdk": Bp2BuildDefaultTrueRecursively,
233 "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
234 "external/boringssl": Bp2BuildDefaultTrueRecursively,
235 "external/brotli": Bp2BuildDefaultTrue,
236 "external/fmtlib": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400237 "external/google-benchmark": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400238 "external/googletest/googletest": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400239 "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
240 "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400241 "external/jsoncpp": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400242 "external/libcap": Bp2BuildDefaultTrueRecursively,
243 "external/libcxx": Bp2BuildDefaultTrueRecursively,
244 "external/libcxxabi": Bp2BuildDefaultTrueRecursively,
245 "external/lz4/lib": Bp2BuildDefaultTrue,
Liz Kammer2fc34892021-10-11 12:56:48 -0400246 "external/mdnsresponder": Bp2BuildDefaultTrueRecursively,
247 "external/minijail": Bp2BuildDefaultTrueRecursively,
248 "external/pcre": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400249 "external/protobuf": Bp2BuildDefaultTrueRecursively,
250 "external/python/six": Bp2BuildDefaultTrueRecursively,
251 "external/scudo": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400252 "external/selinux/libselinux": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400253 "external/zlib": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400254 "external/zstd": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400255 "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively,
256 "packages/modules/adb": Bp2BuildDefaultTrue,
257 "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively,
258 "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively,
259 "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively,
260 "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively,
261 "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively,
262 "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively,
263 "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively,
Jingwen Chend43d4a42021-11-23 12:40:11 +0000264 "system/apex": Bp2BuildDefaultFalse, // TODO(b/207466993): flaky failures
Chris Parsonsa37e1952021-09-28 16:47:36 -0400265 "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively,
266 "system/core/libasyncio": Bp2BuildDefaultTrue,
267 "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively,
268 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400269 "system/core/libpackagelistparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400270 "system/core/libprocessgroup": Bp2BuildDefaultTrue,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400271 "system/core/libprocessgroup/cgrouprc": Bp2BuildDefaultTrue,
272 "system/core/libprocessgroup/cgrouprc_format": Bp2BuildDefaultTrue,
Lukacs T. Berki497f17d2021-04-26 12:15:57 +0200273 "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400274 "system/libbase": Bp2BuildDefaultTrueRecursively,
Chris Parsonsc39f6332021-10-01 18:00:31 -0400275 "system/libziparchive": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400276 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
277 "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively,
278 "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
279 "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500280 }
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000281
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400282 // Per-module denylist to always opt modules out of both bp2build and mixed builds.
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000283 bp2buildModuleDoNotConvertList = []string{
Chris Parsons494eef32021-11-09 10:29:52 -0500284 "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610.
285
Chris Parsonsc39f6332021-10-01 18:00:31 -0400286 "libc_malloc_debug", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Rupert Shuttleworth47aa5842021-04-30 04:04:15 -0400287
Chris Parsonsc39f6332021-10-01 18:00:31 -0400288 "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 +0000289
Liz Kammer0f3b7d22021-09-28 13:48:21 -0400290 "libprotobuf-python", // contains .proto sources
291 "libprotobuf-internal-protos", // we don't handle path property for fileegroups
292 "libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups
293
Chris Parsonsa37e1952021-09-28 16:47:36 -0400294 "libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds.
295 "libfdtrack", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200296
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400297 "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset
298
Chris Parsonsb97a8172021-10-04 12:48:29 -0400299 "brotli-fuzzer-corpus", // b/202015218: outputs are in location incompatible with bazel genrule handling.
Jingwen Chen294e7742021-08-31 05:58:01 +0000300
Jingwen Chendf27b7a2021-10-18 06:33:16 +0000301 // b/203369847: multiple genrules in the same package creating the same file
302 // //development/sdk/...
303 "platform_tools_properties",
304 "build_tools_source_properties",
305
Chris Parsons393002a2021-11-11 13:39:20 -0500306 "libminijail", // b/202491296: Uses unsupported c_std property.
307 "minijail0", // depends on unconverted modules: libminijail
Liz Kammer2b8004b2021-10-04 13:55:44 -0400308 "drop_privs", // depends on unconverted modules: libminijail
Liz Kammer2fc34892021-10-11 12:56:48 -0400309
Jingwen Chen790324e2021-04-30 08:20:01 +0000310 // Tests. Handle later.
311 "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
312 "libjemalloc5_integrationtest",
313 "libjemalloc5_stresstestlib",
314 "libjemalloc5_unittest",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400315
316 // APEX support
Chris Parsonsb97a8172021-10-04 12:48:29 -0400317 "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug'
Chris Parsonsa37e1952021-09-28 16:47:36 -0400318
319 "libadb_crypto", // Depends on libadb_protos
320 "libadb_crypto_static", // Depends on libadb_protos_static
321 "libadb_pairing_connection", // Depends on libadb_protos
322 "libadb_pairing_connection_static", // Depends on libadb_protos_static
323 "libadb_pairing_server", // Depends on libadb_protos
324 "libadb_pairing_server_static", // Depends on libadb_protos_static
325 "libadbd", // Depends on libadbd_core
326 "libadbd_core", // Depends on libadb_protos
327 "libadbd_services", // Depends on libadb_protos
328
329 "libadb_protos_static", // b/200601772: Requires cc_library proto support
330 "libadb_protos", // b/200601772: Requires cc_library proto support
331 "libapp_processes_protos_lite", // b/200601772: Requires cc_library proto support
Chris Parsonsc39f6332021-10-01 18:00:31 -0400332
333 "libgtest_ndk_c++", // b/201816222: Requires sdk_version support.
334 "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support.
Liz Kammer2b8004b2021-10-04 13:55:44 -0400335
336 "abb", // depends on unconverted modules: libadbd_core, libadbd_services, libcmd, libbinder, libutils, libselinux
337 "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
338 "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
339 "bionic_tests_zipalign", // depends on unconverted modules: libziparchive, libutils
340 "linker", // depends on unconverted modules: liblinker_debuggerd_stub, libdebuggerd_handler_fallback, libziparchive, liblinker_main, liblinker_malloc
341 "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_*
342 "sefcontext_compile", // depends on unconverted modules: libsepol
343 "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host
344
345 "linkerconfig", // http://b/202876379 has arch-variant static_executable
346 "mdnsd", // http://b/202876379 has arch-variant static_executable
347
348 "acvp_modulewrapper", // disabled for android x86/x86_64
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400349 }
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400350
Jingwen Chen179856a2021-05-03 09:15:48 +0000351 // Per-module denylist of cc_library modules to only generate the static
352 // variant if their shared variant isn't ready or buildable by Bazel.
353 bp2buildCcLibraryStaticOnlyList = []string{
Jingwen Chen53542922021-05-18 09:01:49 +0000354 "libjemalloc5", // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets.
Jingwen Chen179856a2021-05-03 09:15:48 +0000355 }
356
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400357 // Per-module denylist to opt modules out of mixed builds. Such modules will
358 // still be generated via bp2build.
Chris Parsons2c788392021-08-10 11:58:07 -0400359 mixedBuildsDisabledList = []string{
Wei Li455ba832021-11-04 22:58:12 +0000360 "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
361 "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 -0500362
363 "cap_names.h", // TODO(b/204913827) runfiles need to be handled in mixed builds
364 "libcap", // TODO(b/204913827) runfiles need to be handled in mixed builds
Chris Parsons2c788392021-08-10 11:58:07 -0400365 }
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000366
367 // Used for quicker lookups
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400368 bp2buildModuleDoNotConvert = map[string]bool{}
Jingwen Chen179856a2021-05-03 09:15:48 +0000369 bp2buildCcLibraryStaticOnly = map[string]bool{}
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400370 mixedBuildsDisabled = map[string]bool{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500371)
372
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000373func init() {
374 for _, moduleName := range bp2buildModuleDoNotConvertList {
375 bp2buildModuleDoNotConvert[moduleName] = true
376 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400377
Jingwen Chen179856a2021-05-03 09:15:48 +0000378 for _, moduleName := range bp2buildCcLibraryStaticOnlyList {
379 bp2buildCcLibraryStaticOnly[moduleName] = true
380 }
381
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400382 for _, moduleName := range mixedBuildsDisabledList {
383 mixedBuildsDisabled[moduleName] = true
384 }
385}
386
Chris Parsons953b3562021-09-20 15:14:39 -0400387func GenerateCcLibraryStaticOnly(moduleName string) bool {
388 return bp2buildCcLibraryStaticOnly[moduleName]
Jingwen Chen179856a2021-05-03 09:15:48 +0000389}
390
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400391func ShouldKeepExistingBuildFileForDir(dir string) bool {
392 if _, ok := bp2buildKeepExistingBuildFile[dir]; ok {
393 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400394 return true
395 }
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400396 // Check if subtree match
397 for prefix, recursive := range bp2buildKeepExistingBuildFile {
398 if recursive {
399 if strings.HasPrefix(dir, prefix+"/") {
400 return true
401 }
402 }
403 }
404 // Default
405 return false
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400406}
407
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400408// MixedBuildsEnabled checks that a module is ready to be replaced by a
409// converted or handcrafted Bazel target.
Chris Parsons494eef32021-11-09 10:29:52 -0500410func (b *BazelModuleBase) MixedBuildsEnabled(ctx ModuleContext) bool {
411 if ctx.Os() == Windows {
412 // Windows toolchains are not currently supported.
413 return false
414 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400415 if !ctx.Config().BazelContext.BazelEnabled() {
416 return false
417 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400418 if !convertedToBazel(ctx, ctx.Module()) {
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400419 return false
420 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400421
Chris Parsons953b3562021-09-20 15:14:39 -0400422 if GenerateCcLibraryStaticOnly(ctx.Module().Name()) {
Jingwen Chen179856a2021-05-03 09:15:48 +0000423 // Don't use partially-converted cc_library targets in mixed builds,
424 // since mixed builds would generally rely on both static and shared
425 // variants of a cc_library.
426 return false
427 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400428 return !mixedBuildsDisabled[ctx.Module().Name()]
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000429}
430
Liz Kammer6eff3232021-08-26 08:37:59 -0400431// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000432func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400433 b, ok := module.(Bazelable)
434 if !ok {
435 return false
436 }
437 return b.convertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
438}
439
Liz Kammerea6666f2021-02-17 10:17:28 -0500440// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000441func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionContext) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400442 return b.convertWithBp2build(ctx, ctx.Module())
443}
444
Jingwen Chen55bc8202021-11-02 06:40:51 +0000445func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400446 if bp2buildModuleDoNotConvert[module.Name()] {
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000447 return false
448 }
449
Jingwen Chen12b4c272021-03-10 02:05:59 -0500450 // Ensure that the module type of this module has a bp2build converter. This
451 // prevents mixed builds from using auto-converted modules just by matching
452 // the package dir; it also has to have a bp2build mutator as well.
Liz Kammer6eff3232021-08-26 08:37:59 -0400453 if ctx.Config().bp2buildModuleTypeConfig[ctx.OtherModuleType(module)] == false {
Jingwen Chena47f28d2021-11-02 16:43:57 +0000454 if b, ok := module.(Bazelable); ok && b.BaseModuleType() != "" {
455 // For modules with custom types from soong_config_module_types,
456 // check that their _base module type_ has a bp2build mutator.
457 if ctx.Config().bp2buildModuleTypeConfig[b.BaseModuleType()] == false {
458 return false
459 }
460 } else {
461 return false
462 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500463 }
464
Liz Kammer6eff3232021-08-26 08:37:59 -0400465 packagePath := ctx.OtherModuleDir(module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500466 config := ctx.Config().bp2buildPackageConfig
467
468 // This is a tristate value: true, false, or unset.
469 propValue := b.bazelProperties.Bazel_module.Bp2build_available
470 if bp2buildDefaultTrueRecursively(packagePath, config) {
471 // Allow modules to explicitly opt-out.
472 return proptools.BoolDefault(propValue, true)
473 }
474
475 // Allow modules to explicitly opt-in.
476 return proptools.BoolDefault(propValue, false)
477}
478
479// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
480// set of package prefixes where all modules must be converted. That is, if the
481// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
482// return true.
483//
484// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
485// exactly, this module will return false early.
486//
487// This function will also return false if the package doesn't match anything in
488// the config.
489func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
490 ret := false
491
Jingwen Chen294e7742021-08-31 05:58:01 +0000492 // Check if the package path has an exact match in the config.
493 if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively {
Jingwen Chen91220d72021-03-24 02:18:33 -0400494 return true
Jingwen Chen294e7742021-08-31 05:58:01 +0000495 } else if config[packagePath] == Bp2BuildDefaultFalse {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500496 return false
497 }
498
Jingwen Chen91220d72021-03-24 02:18:33 -0400499 // If not, check for the config recursively.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500500 packagePrefix := ""
501 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
502 for _, part := range strings.Split(packagePath, "/") {
503 packagePrefix += part
504 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
505 // package contains this prefix and this prefix should convert all modules
506 return true
507 }
508 // Continue to the next part of the package dir.
509 packagePrefix += "/"
510 }
511
512 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500513}
Liz Kammerba3ea162021-02-17 13:22:03 -0500514
515// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
516// an error if there are errors reading the file.
517// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
518// something more targeted based on the rule type and target.
519func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500520 if !strings.Contains(b.HandcraftedLabel(), path) {
521 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500522 }
523 name = filepath.Join(path, name)
524 f, err := c.fs.Open(name)
525 if err != nil {
526 return "", err
527 }
528 defer f.Close()
529
530 data, err := ioutil.ReadAll(f)
531 if err != nil {
532 return "", err
533 }
534 return string(data[:]), nil
535}