blob: fd3f23e62686e89ce86be38e2936805a7895e063 [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
Liz Kammerbe46fcc2021-11-01 15:32:43 -040042
43 // CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to
44 // Bazel with Bp2build.
45 CanConvertToBazel bool `blueprint:"mutated"`
Jingwen Chen01812022021-11-19 14:29:43 +000046}
47
Liz Kammerba3ea162021-02-17 13:22:03 -050048// Properties contains common module properties for Bazel migration purposes.
49type properties struct {
50 // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
51 // this Soong module.
Jingwen Chen01812022021-11-19 14:29:43 +000052 Bazel_module bazelModuleProperties
Liz Kammerba3ea162021-02-17 13:22:03 -050053}
Liz Kammerea6666f2021-02-17 10:17:28 -050054
Jingwen Chen25825ca2021-11-15 12:28:43 +000055// namespacedVariableProperties is a map from a string representing a Soong
Jingwen Chen84817de2021-11-17 10:57:35 +000056// config variable namespace, like "android" or "vendor_name" to a slice of
57// pointer to a struct containing a single field called Soong_config_variables
58// whose value mirrors the structure in the Blueprint file.
59type namespacedVariableProperties map[string][]interface{}
Jingwen Chena47f28d2021-11-02 16:43:57 +000060
Liz Kammerea6666f2021-02-17 10:17:28 -050061// BazelModuleBase contains the property structs with metadata for modules which can be converted to
62// Bazel.
63type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050064 bazelProperties properties
Jingwen Chena47f28d2021-11-02 16:43:57 +000065
66 // namespacedVariableProperties is used for soong_config_module_type support
67 // in bp2build. Soong config modules allow users to set module properties
68 // based on custom product variables defined in Android.bp files. These
69 // variables are namespaced to prevent clobbering, especially when set from
70 // Makefiles.
71 namespacedVariableProperties namespacedVariableProperties
72
73 // baseModuleType is set when this module was created from a module type
74 // defined by a soong_config_module_type. Every soong_config_module_type
75 // "wraps" another module type, e.g. a soong_config_module_type can wrap a
76 // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
77 // This baseModuleType is set to the wrapped module type.
78 baseModuleType string
Liz Kammerea6666f2021-02-17 10:17:28 -050079}
80
81// Bazelable is specifies the interface for modules that can be converted to Bazel.
82type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -050083 bazelProps() *properties
84 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -050085 HandcraftedLabel() string
86 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Liz Kammerbe46fcc2021-11-01 15:32:43 -040087 ShouldConvertWithBp2build(ctx BazelConversionContext) bool
88 shouldConvertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool
Liz Kammerba3ea162021-02-17 13:22:03 -050089 GetBazelBuildFileContents(c Config, path, name string) (string, error)
Liz Kammerbe46fcc2021-11-01 15:32:43 -040090 ConvertWithBp2build(ctx TopDownMutatorContext)
Jingwen Chena47f28d2021-11-02 16:43:57 +000091
Jingwen Chen84817de2021-11-17 10:57:35 +000092 // namespacedVariableProps is a map from a soong config variable namespace
93 // (e.g. acme, android) to a map of interfaces{}, which are really
94 // reflect.Struct pointers, representing the value of the
95 // soong_config_variables property of a module. The struct pointer is the
96 // one with the single member called Soong_config_variables, which itself is
97 // a struct containing fields for each supported feature in that namespace.
98 //
99 // The reason for using an slice of interface{} is to support defaults
100 // propagation of the struct pointers.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000101 namespacedVariableProps() namespacedVariableProperties
102 setNamespacedVariableProps(props namespacedVariableProperties)
103 BaseModuleType() string
Jingwen Chen84817de2021-11-17 10:57:35 +0000104 SetBaseModuleType(baseModuleType string)
Liz Kammerea6666f2021-02-17 10:17:28 -0500105}
106
107// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
108type BazelModule interface {
109 Module
110 Bazelable
111}
112
113// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
114// properties.
115func InitBazelModule(module BazelModule) {
116 module.AddProperties(module.bazelProps())
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400117 module.bazelProps().Bazel_module.CanConvertToBazel = true
Liz Kammerea6666f2021-02-17 10:17:28 -0500118}
119
120// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -0500121func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -0500122 return &b.bazelProperties
123}
124
Jingwen Chena47f28d2021-11-02 16:43:57 +0000125func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
126 return b.namespacedVariableProperties
127}
128
129func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
130 b.namespacedVariableProperties = props
131}
132
133func (b *BazelModuleBase) BaseModuleType() string {
134 return b.baseModuleType
135}
136
137func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
138 b.baseModuleType = baseModuleType
139}
140
Liz Kammerba3ea162021-02-17 13:22:03 -0500141// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
142func (b *BazelModuleBase) HasHandcraftedLabel() bool {
143 return b.bazelProperties.Bazel_module.Label != nil
144}
145
146// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
147func (b *BazelModuleBase) HandcraftedLabel() string {
148 return proptools.String(b.bazelProperties.Bazel_module.Label)
149}
150
Liz Kammerea6666f2021-02-17 10:17:28 -0500151// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -0500152func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
153 if b.HasHandcraftedLabel() {
154 return b.HandcraftedLabel()
155 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400156 if b.ShouldConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500157 return bp2buildModuleLabel(ctx, module)
158 }
159 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -0500160}
161
Jingwen Chen12b4c272021-03-10 02:05:59 -0500162// Configuration to decide if modules in a directory should default to true/false for bp2build_available
163type Bp2BuildConfig map[string]BazelConversionConfigEntry
164type BazelConversionConfigEntry int
165
166const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400167 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
168 // no package path. This is also the module dir for top level Android.bp
169 // modules.
170 BP2BUILD_TOPLEVEL = "."
171
Jingwen Chen12b4c272021-03-10 02:05:59 -0500172 // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
173 // which can also mean that the key doesn't exist in a lookup.
174
175 // all modules in this package and subpackages default to bp2build_available: true.
176 // allows modules to opt-out.
177 Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
178
Jingwen Chen294e7742021-08-31 05:58:01 +0000179 // all modules in this package (not recursively) default to bp2build_available: true.
180 // allows modules to opt-out.
181 Bp2BuildDefaultTrue
182
Jingwen Chen12b4c272021-03-10 02:05:59 -0500183 // all modules in this package (not recursively) default to bp2build_available: false.
184 // allows modules to opt-in.
185 Bp2BuildDefaultFalse
186)
187
188var (
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400189 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000190 // in the synthetic Bazel workspace.
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400191 bp2buildKeepExistingBuildFile = map[string]bool{
192 // This is actually build/bazel/build.BAZEL symlinked to ./BUILD
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000193 ".":/*recursive = */ false,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400194
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000195 // build/bazel/examples/apex/... BUILD files should be generated, so
196 // build/bazel is not recursive. Instead list each subdirectory under
197 // build/bazel explicitly.
198 "build/bazel":/* recursive = */ false,
199 "build/bazel/examples/android_app":/* recursive = */ true,
Romain Jobredeaux5cc9c9d2021-08-26 15:07:48 +0000200 "build/bazel/examples/java":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000201 "build/bazel/bazel_skylib":/* recursive = */ true,
202 "build/bazel/rules":/* recursive = */ true,
203 "build/bazel/rules_cc":/* recursive = */ true,
Jingwen Chen294e7742021-08-31 05:58:01 +0000204 "build/bazel/scripts":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000205 "build/bazel/tests":/* recursive = */ true,
206 "build/bazel/platforms":/* recursive = */ true,
207 "build/bazel/product_variables":/* recursive = */ true,
Jingwen Chen5e49b822021-08-24 05:14:22 +0000208 "build/bazel_common_rules":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000209 "build/make/tools":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400210 "build/pesto":/* recursive = */ true,
211
212 // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
213 // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed
214 "external/bazelbuild-rules_android":/* recursive = */ true,
Jingwen Chen91632252021-08-10 13:00:33 +0000215 "external/bazel-skylib":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000216 "external/guava":/* recursive = */ true,
217 "external/error_prone":/* recursive = */ true,
218 "external/jsr305":/* recursive = */ true,
219 "frameworks/ex/common":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400220
Romain Jobredeauxf1b0ac82021-08-12 14:39:00 +0000221 "packages/apps/Music":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000222 "packages/apps/QuickSearchBox":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400223 "packages/apps/WallpaperPicker":/* recursive = */ false,
224
Chris Parsons494eef32021-11-09 10:29:52 -0500225 "prebuilts/gcc":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400226 "prebuilts/sdk":/* recursive = */ false,
227 "prebuilts/sdk/current/extras/app-toolkit":/* recursive = */ false,
228 "prebuilts/sdk/current/support":/* recursive = */ false,
229 "prebuilts/sdk/tools":/* recursive = */ false,
230 "prebuilts/r8":/* recursive = */ false,
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400231 }
232
Jingwen Chen12b4c272021-03-10 02:05:59 -0500233 // Configure modules in these directories to enable bp2build_available: true or false by default.
234 bp2buildDefaultConfig = Bp2BuildConfig{
Romain Jobredeaux1282c422021-10-29 10:52:59 -0400235 "art/libdexfile": Bp2BuildDefaultTrueRecursively,
236 "bionic": Bp2BuildDefaultTrueRecursively,
237 "bootable/recovery/tools/recovery_l10n": Bp2BuildDefaultTrue,
Jingwen Chen0a9d09f2021-11-24 07:00:11 +0000238 "build/bazel/examples/soong_config_variables": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400239 "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
Chris Parsons8a498162021-11-18 17:19:12 -0500240 "build/soong": Bp2BuildDefaultTrue,
Rupert Shuttleworth2ac9c482021-11-10 10:37:05 -0500241 "build/soong/cc/libbuildversion": Bp2BuildDefaultTrue, // Skip tests subdir
Jingwen Chen89e21882021-11-30 11:16:06 +0000242 "build/soong/cc/ndkstubgen": Bp2BuildDefaultTrue,
243 "build/soong/cc/symbolfile": Bp2BuildDefaultTrue,
Chris Parsons8a498162021-11-18 17:19:12 -0500244 "cts/common/device-side/nativetesthelper/jni": Bp2BuildDefaultTrueRecursively,
Romain Jobredeaux1282c422021-10-29 10:52:59 -0400245 "development/apps/DevelopmentSettings": Bp2BuildDefaultTrue,
246 "development/apps/Fallback": Bp2BuildDefaultTrue,
247 "development/apps/WidgetPreview": Bp2BuildDefaultTrue,
248 "development/samples/BasicGLSurfaceView": Bp2BuildDefaultTrue,
249 "development/samples/BluetoothChat": Bp2BuildDefaultTrue,
250 "development/samples/BrokenKeyDerivation": Bp2BuildDefaultTrue,
251 "development/samples/Compass": Bp2BuildDefaultTrue,
252 "development/samples/ContactManager": Bp2BuildDefaultTrue,
253 "development/samples/FixedGridLayout": Bp2BuildDefaultTrue,
254 "development/samples/HelloEffects": Bp2BuildDefaultTrue,
255 "development/samples/Home": Bp2BuildDefaultTrue,
256 "development/samples/HoneycombGallery": Bp2BuildDefaultTrue,
257 "development/samples/JetBoy": Bp2BuildDefaultTrue,
258 "development/samples/KeyChainDemo": Bp2BuildDefaultTrue,
259 "development/samples/LceDemo": Bp2BuildDefaultTrue,
260 "development/samples/LunarLander": Bp2BuildDefaultTrue,
261 "development/samples/MultiResolution": Bp2BuildDefaultTrue,
262 "development/samples/MultiWindow": Bp2BuildDefaultTrue,
263 "development/samples/NotePad": Bp2BuildDefaultTrue,
264 "development/samples/Obb": Bp2BuildDefaultTrue,
265 "development/samples/RSSReader": Bp2BuildDefaultTrue,
266 "development/samples/ReceiveShareDemo": Bp2BuildDefaultTrue,
267 "development/samples/SearchableDictionary": Bp2BuildDefaultTrue,
268 "development/samples/SipDemo": Bp2BuildDefaultTrue,
269 "development/samples/SkeletonApp": Bp2BuildDefaultTrue,
270 "development/samples/Snake": Bp2BuildDefaultTrue,
271 "development/samples/SpellChecker/": Bp2BuildDefaultTrueRecursively,
272 "development/samples/ThemedNavBarKeyboard": Bp2BuildDefaultTrue,
273 "development/samples/ToyVpn": Bp2BuildDefaultTrue,
274 "development/samples/TtsEngine": Bp2BuildDefaultTrue,
275 "development/samples/USB/AdbTest": Bp2BuildDefaultTrue,
276 "development/samples/USB/MissileLauncher": Bp2BuildDefaultTrue,
277 "development/samples/VoiceRecognitionService": Bp2BuildDefaultTrue,
278 "development/samples/VoicemailProviderDemo": Bp2BuildDefaultTrue,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400279 "development/sdk": Bp2BuildDefaultTrueRecursively,
280 "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
281 "external/boringssl": Bp2BuildDefaultTrueRecursively,
282 "external/brotli": Bp2BuildDefaultTrue,
283 "external/fmtlib": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400284 "external/google-benchmark": Bp2BuildDefaultTrueRecursively,
Chris Parsons8a498162021-11-18 17:19:12 -0500285 "external/googletest": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400286 "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
287 "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400288 "external/jsoncpp": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400289 "external/libcap": Bp2BuildDefaultTrueRecursively,
290 "external/libcxx": Bp2BuildDefaultTrueRecursively,
291 "external/libcxxabi": Bp2BuildDefaultTrueRecursively,
Chris Parsons8a498162021-11-18 17:19:12 -0500292 "external/libevent": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400293 "external/lz4/lib": Bp2BuildDefaultTrue,
Chris Parsons8a498162021-11-18 17:19:12 -0500294 "external/lzma/C": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400295 "external/mdnsresponder": Bp2BuildDefaultTrueRecursively,
296 "external/minijail": Bp2BuildDefaultTrueRecursively,
297 "external/pcre": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400298 "external/protobuf": Bp2BuildDefaultTrueRecursively,
299 "external/python/six": Bp2BuildDefaultTrueRecursively,
Chris Parsons8a498162021-11-18 17:19:12 -0500300 "external/selinux/libsepol": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400301 "external/scudo": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400302 "external/selinux/libselinux": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400303 "external/zlib": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400304 "external/zstd": Bp2BuildDefaultTrueRecursively,
Romain Jobredeaux1282c422021-10-29 10:52:59 -0400305 "frameworks/base/media/tests/MediaDump": Bp2BuildDefaultTrue,
306 "frameworks/base/startop/apps/test": Bp2BuildDefaultTrue,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400307 "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively,
Romain Jobredeaux1282c422021-10-29 10:52:59 -0400308 "frameworks/native/opengl/tests/gl2_cameraeye": Bp2BuildDefaultTrue,
309 "frameworks/native/opengl/tests/gl2_java": Bp2BuildDefaultTrue,
310 "frameworks/native/opengl/tests/testLatency": Bp2BuildDefaultTrue,
311 "frameworks/native/opengl/tests/testPauseResume": Bp2BuildDefaultTrue,
312 "frameworks/native/opengl/tests/testViewport": Bp2BuildDefaultTrue,
Chris Parsons8a498162021-11-18 17:19:12 -0500313 "frameworks/proto_logging/stats/stats_log_api_gen": Bp2BuildDefaultTrueRecursively,
314 "libnativehelper": Bp2BuildDefaultTrueRecursively,
Romain Jobredeaux1282c422021-10-29 10:52:59 -0400315 "packages/apps/DevCamera": Bp2BuildDefaultTrue,
316 "packages/apps/HTMLViewer": Bp2BuildDefaultTrue,
317 "packages/apps/Protips": Bp2BuildDefaultTrue,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400318 "packages/modules/adb": Bp2BuildDefaultTrue,
319 "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively,
320 "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively,
321 "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively,
322 "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively,
323 "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively,
324 "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively,
Romain Jobredeaux1282c422021-10-29 10:52:59 -0400325 "packages/providers/MediaProvider/tools/dialogs": Bp2BuildDefaultTrue,
326 "packages/screensavers/Basic": Bp2BuildDefaultTrue,
327 "packages/services/Car/tests/SampleRearViewCamera": Bp2BuildDefaultTrue,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400328 "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively,
Jingwen Chend43d4a42021-11-23 12:40:11 +0000329 "system/apex": Bp2BuildDefaultFalse, // TODO(b/207466993): flaky failures
Chris Parsons8a498162021-11-18 17:19:12 -0500330 "system/core/debuggerd": Bp2BuildDefaultTrue,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400331 "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively,
332 "system/core/libasyncio": Bp2BuildDefaultTrue,
333 "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively,
334 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400335 "system/core/libpackagelistparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400336 "system/core/libprocessgroup": Bp2BuildDefaultTrue,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400337 "system/core/libprocessgroup/cgrouprc": Bp2BuildDefaultTrue,
338 "system/core/libprocessgroup/cgrouprc_format": Bp2BuildDefaultTrue,
Chris Parsons8a498162021-11-18 17:19:12 -0500339 "system/core/libsystem": Bp2BuildDefaultTrueRecursively,
340 "system/core/libutils": Bp2BuildDefaultTrueRecursively,
341 "system/core/libvndksupport": Bp2BuildDefaultTrueRecursively,
Lukacs T. Berki497f17d2021-04-26 12:15:57 +0200342 "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400343 "system/libbase": Bp2BuildDefaultTrueRecursively,
Chris Parsons8a498162021-11-18 17:19:12 -0500344 "system/libprocinfo": Bp2BuildDefaultTrue,
Chris Parsonsc39f6332021-10-01 18:00:31 -0400345 "system/libziparchive": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400346 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
347 "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively,
348 "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
349 "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
Chris Parsons8a498162021-11-18 17:19:12 -0500350 "system/unwinding/libbacktrace": Bp2BuildDefaultTrueRecursively,
351 "system/unwinding/libunwindstack": Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500352 }
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000353
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400354 // Per-module denylist to always opt modules out of both bp2build and mixed builds.
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000355 bp2buildModuleDoNotConvertList = []string{
Chris Parsons8a498162021-11-18 17:19:12 -0500356 "libnativehelper_compat_libc++", // Broken compile: implicit declaration of function 'strerror_r' is invalid in C99
357 "art_libdexfile_dex_instruction_list_header", // breaks libart_mterp.armng, header not found
358
359 "libandroid_runtime_lazy", // depends on unconverted modules: libbinder_headers
360 "libcmd", // depends on unconverted modules: libbinder
361
362 "chkcon", "sefcontext_compile", // depends on unconverted modules: libsepol
363
364 "libsepol", // TODO(b/207408632): Unsupported case of .l sources in cc library rules
365
366 "get_clang_version_test", // depends on unconverted module: get_clang_version
367
368 "libbinder", // TODO(b/188503688): Disabled for some archs,
369 "libactivitymanager_aidl", // TODO(b/207426160): Depends on activity_manager_procstate_aidl, which is an aidl filegroup.
370
371 "libnativehelper_lazy_mts_jni", // depends on unconverted modules: libgmock_ndk
372 "libnativehelper_mts_jni", // depends on unconverted modules: libgmock_ndk
373 "libnativetesthelper_jni", // depends on unconverted modules: libgtest_ndk_c++
374
375 "statslog-framework-java-gen", "statslog.cpp", "statslog.h", "statslog.rs", "statslog_header.rs", // depends on unconverted modules: stats-log-api-gen
376
377 "stats-log-api-gen", // depends on unconverted modules: libstats_proto_host, libprotobuf-cpp-full
378
379 "libstatslog", // depends on unconverted modules: statslog.cpp, statslog.h, ...
380
381 "libgmock_main_ndk", "libgmock_ndk", // depends on unconverted module: libgtest_ndk_c++
382
383 "cmd", // depends on unconverted module packagemanager_aidl-cpp, of unsupported type aidl_interface
384 "servicedispatcher", // depends on unconverted module android.debug_aidl, of unsupported type aidl_interface
385 "libutilscallstack", // depends on unconverted module libbacktrace
386 "libbacktrace", // depends on unconverted module libunwindstack
387 "libdebuggerd_handler", // depends on unconverted module libdebuggerd_handler_core
388 "libdebuggerd_handler_core", "libdebuggerd_handler_fallback", // depends on unconverted module libdebuggerd
389 "unwind_for_offline", // depends on unconverted module libunwindstack_utils
390 "libdebuggerd", // depends on unconverted modules libdexfile_support, libunwindstack, gwp_asan_crash_handler, libtombstone_proto, libprotobuf-cpp-lite
391 "libdexfile_static", // depends on libartpalette, libartbase, libdexfile, which are of unsupported type: art_cc_library.
392 "host_bionic_linker_asm", // depends on extract_linker, a go binary.
393 "host_bionic_linker_script", // depends on extract_linker, a go binary.
394
Liz Kammer12615db2021-09-28 09:19:17 -0400395 "pbtombstone", // depends on libprotobuf-cpp-lite, libtombstone_proto
396 "crash_dump", // depends on unconverted module libprotobuf-cpp-lite
Chris Parsons494eef32021-11-09 10:29:52 -0500397
Chris Parsons8a498162021-11-18 17:19:12 -0500398 "libunwindstack_local", "libunwindstack_utils", // depends on unconverted module libunwindstack
399 "libunwindstack", // depends on libdexfile_support, of unsupported module type art_cc_library_static
400 "libc_malloc_debug", // depends on unconverted module libunwindstack
Rupert Shuttleworth47aa5842021-04-30 04:04:15 -0400401
Chris Parsonsc39f6332021-10-01 18:00:31 -0400402 "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 +0000403
Chris Parsons8a498162021-11-18 17:19:12 -0500404 "lib_linker_config_proto_lite", // contains .proto sources
405
Liz Kammer0f3b7d22021-09-28 13:48:21 -0400406 "libprotobuf-python", // contains .proto sources
407 "libprotobuf-internal-protos", // we don't handle path property for fileegroups
408 "libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups
409
Chris Parsonsa37e1952021-09-28 16:47:36 -0400410 "libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds.
Chris Parsons8a498162021-11-18 17:19:12 -0500411 "libfdtrack", // depends on unconverted module libunwindstack
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200412
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400413 "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset
414
Chris Parsonsb97a8172021-10-04 12:48:29 -0400415 "brotli-fuzzer-corpus", // b/202015218: outputs are in location incompatible with bazel genrule handling.
Jingwen Chen294e7742021-08-31 05:58:01 +0000416
Jingwen Chendf27b7a2021-10-18 06:33:16 +0000417 // b/203369847: multiple genrules in the same package creating the same file
418 // //development/sdk/...
419 "platform_tools_properties",
420 "build_tools_source_properties",
421
Jingwen Chen790324e2021-04-30 08:20:01 +0000422 // Tests. Handle later.
423 "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
424 "libjemalloc5_integrationtest",
425 "libjemalloc5_stresstestlib",
426 "libjemalloc5_unittest",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400427
428 // APEX support
Chris Parsonsb97a8172021-10-04 12:48:29 -0400429 "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug'
Chris Parsonsa37e1952021-09-28 16:47:36 -0400430
Chris Parsonsc39f6332021-10-01 18:00:31 -0400431 "libgtest_ndk_c++", // b/201816222: Requires sdk_version support.
432 "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support.
Liz Kammer2b8004b2021-10-04 13:55:44 -0400433
Liz Kammer1263d9b2021-12-10 14:28:20 -0500434 "abb", // depends on unconverted modules: libcmd, libbinder
435 "adb", // depends on unconverted modules: AdbWinApi, libadb_host, libandroidfw, libapp_processes_protos_full, libfastdeploy_host, libopenscreen-discovery, libopenscreen-platform-impl, libusb, bin2c_fastdeployagent, AdbWinUsbApi
Chris Parsons8a498162021-11-18 17:19:12 -0500436 "linker", // depends on unconverted modules: libdebuggerd_handler_fallback
Liz Kammer2b8004b2021-10-04 13:55:44 -0400437 "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_*
Chris Parsons8a498162021-11-18 17:19:12 -0500438 "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host, of unsupported type llvm_host_prebuilt_library_shared
Liz Kammer2b8004b2021-10-04 13:55:44 -0400439
440 "linkerconfig", // http://b/202876379 has arch-variant static_executable
441 "mdnsd", // http://b/202876379 has arch-variant static_executable
442
443 "acvp_modulewrapper", // disabled for android x86/x86_64
Romain Jobredeaux1282c422021-10-29 10:52:59 -0400444 "CarHTMLViewer", // depends on unconverted modules android.car-stubs, car-ui-lib
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400445
446 "libdexfile", // depends on unconverted modules: dexfile_operator_srcs, libartbase, libartpalette,
447 "libdexfiled", // depends on unconverted modules: dexfile_operator_srcs, libartbased, libartpalette
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400448 }
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400449
Jingwen Chen179856a2021-05-03 09:15:48 +0000450 // Per-module denylist of cc_library modules to only generate the static
451 // variant if their shared variant isn't ready or buildable by Bazel.
452 bp2buildCcLibraryStaticOnlyList = []string{
Jingwen Chen53542922021-05-18 09:01:49 +0000453 "libjemalloc5", // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets.
Jingwen Chen179856a2021-05-03 09:15:48 +0000454 }
455
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400456 // Per-module denylist to opt modules out of mixed builds. Such modules will
457 // still be generated via bp2build.
Chris Parsons2c788392021-08-10 11:58:07 -0400458 mixedBuildsDisabledList = []string{
Wei Li455ba832021-11-04 22:58:12 +0000459 "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
460 "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 -0500461
Jingwen Chend4808592021-11-26 05:39:07 +0000462 "cap_names.h", // TODO(b/204913827) runfiles need to be handled in mixed builds
463 "libcap", // TODO(b/204913827) runfiles need to be handled in mixed builds
464 "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610.
Liz Kammer12615db2021-09-28 09:19:17 -0400465
466 // Depends on libprotobuf-cpp-*
Liz Kammereb2d6d12021-12-06 14:56:25 -0500467 "libadb_pairing_connection",
Liz Kammer12615db2021-09-28 09:19:17 -0400468 "libadb_pairing_connection_static",
469 "libadb_pairing_server", "libadb_pairing_server_static",
Chris Parsons2c788392021-08-10 11:58:07 -0400470 }
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000471
472 // Used for quicker lookups
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400473 bp2buildModuleDoNotConvert = map[string]bool{}
Jingwen Chen179856a2021-05-03 09:15:48 +0000474 bp2buildCcLibraryStaticOnly = map[string]bool{}
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400475 mixedBuildsDisabled = map[string]bool{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500476)
477
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000478func init() {
479 for _, moduleName := range bp2buildModuleDoNotConvertList {
480 bp2buildModuleDoNotConvert[moduleName] = true
481 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400482
Jingwen Chen179856a2021-05-03 09:15:48 +0000483 for _, moduleName := range bp2buildCcLibraryStaticOnlyList {
484 bp2buildCcLibraryStaticOnly[moduleName] = true
485 }
486
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400487 for _, moduleName := range mixedBuildsDisabledList {
488 mixedBuildsDisabled[moduleName] = true
489 }
490}
491
Chris Parsons953b3562021-09-20 15:14:39 -0400492func GenerateCcLibraryStaticOnly(moduleName string) bool {
493 return bp2buildCcLibraryStaticOnly[moduleName]
Jingwen Chen179856a2021-05-03 09:15:48 +0000494}
495
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400496func ShouldKeepExistingBuildFileForDir(dir string) bool {
497 if _, ok := bp2buildKeepExistingBuildFile[dir]; ok {
498 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400499 return true
500 }
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400501 // Check if subtree match
502 for prefix, recursive := range bp2buildKeepExistingBuildFile {
503 if recursive {
504 if strings.HasPrefix(dir, prefix+"/") {
505 return true
506 }
507 }
508 }
509 // Default
510 return false
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400511}
512
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400513// MixedBuildsEnabled checks that a module is ready to be replaced by a
514// converted or handcrafted Bazel target.
Chris Parsons494eef32021-11-09 10:29:52 -0500515func (b *BazelModuleBase) MixedBuildsEnabled(ctx ModuleContext) bool {
516 if ctx.Os() == Windows {
517 // Windows toolchains are not currently supported.
518 return false
519 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400520 if !ctx.Config().BazelContext.BazelEnabled() {
521 return false
522 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400523 if !convertedToBazel(ctx, ctx.Module()) {
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400524 return false
525 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400526
Chris Parsons953b3562021-09-20 15:14:39 -0400527 if GenerateCcLibraryStaticOnly(ctx.Module().Name()) {
Jingwen Chen179856a2021-05-03 09:15:48 +0000528 // Don't use partially-converted cc_library targets in mixed builds,
529 // since mixed builds would generally rely on both static and shared
530 // variants of a cc_library.
531 return false
532 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400533 return !mixedBuildsDisabled[ctx.Module().Name()]
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000534}
535
Liz Kammer6eff3232021-08-26 08:37:59 -0400536// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000537func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400538 b, ok := module.(Bazelable)
539 if !ok {
540 return false
541 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400542 return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
Liz Kammer6eff3232021-08-26 08:37:59 -0400543}
544
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400545// ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
546func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool {
547 return b.shouldConvertWithBp2build(ctx, ctx.Module())
Liz Kammer6eff3232021-08-26 08:37:59 -0400548}
549
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400550func (b *BazelModuleBase) shouldConvertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400551 if bp2buildModuleDoNotConvert[module.Name()] {
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000552 return false
553 }
554
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400555 if !b.bazelProps().Bazel_module.CanConvertToBazel {
556 return false
Jingwen Chen12b4c272021-03-10 02:05:59 -0500557 }
558
Liz Kammer6eff3232021-08-26 08:37:59 -0400559 packagePath := ctx.OtherModuleDir(module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500560 config := ctx.Config().bp2buildPackageConfig
561
562 // This is a tristate value: true, false, or unset.
563 propValue := b.bazelProperties.Bazel_module.Bp2build_available
564 if bp2buildDefaultTrueRecursively(packagePath, config) {
565 // Allow modules to explicitly opt-out.
566 return proptools.BoolDefault(propValue, true)
567 }
568
569 // Allow modules to explicitly opt-in.
570 return proptools.BoolDefault(propValue, false)
571}
572
573// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
574// set of package prefixes where all modules must be converted. That is, if the
575// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
576// return true.
577//
578// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
579// exactly, this module will return false early.
580//
581// This function will also return false if the package doesn't match anything in
582// the config.
583func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
584 ret := false
585
Jingwen Chen294e7742021-08-31 05:58:01 +0000586 // Check if the package path has an exact match in the config.
587 if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively {
Jingwen Chen91220d72021-03-24 02:18:33 -0400588 return true
Jingwen Chen294e7742021-08-31 05:58:01 +0000589 } else if config[packagePath] == Bp2BuildDefaultFalse {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500590 return false
591 }
592
Jingwen Chen91220d72021-03-24 02:18:33 -0400593 // If not, check for the config recursively.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500594 packagePrefix := ""
595 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
596 for _, part := range strings.Split(packagePath, "/") {
597 packagePrefix += part
598 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
599 // package contains this prefix and this prefix should convert all modules
600 return true
601 }
602 // Continue to the next part of the package dir.
603 packagePrefix += "/"
604 }
605
606 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500607}
Liz Kammerba3ea162021-02-17 13:22:03 -0500608
609// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
610// an error if there are errors reading the file.
611// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
612// something more targeted based on the rule type and target.
613func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500614 if !strings.Contains(b.HandcraftedLabel(), path) {
615 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500616 }
617 name = filepath.Join(path, name)
618 f, err := c.fs.Open(name)
619 if err != nil {
620 return "", err
621 }
622 defer f.Close()
623
624 data, err := ioutil.ReadAll(f)
625 if err != nil {
626 return "", err
627 }
628 return string(data[:]), nil
629}
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400630
631func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
632 ctx.TopDown("bp2build_conversion", convertWithBp2build).Parallel()
633}
634
635func convertWithBp2build(ctx TopDownMutatorContext) {
636 bModule, ok := ctx.Module().(Bazelable)
637 if !ok || !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
638 return
639 }
640
641 bModule.ConvertWithBp2build(ctx)
642}