blob: cf27cb464aa9b5a35cfd1ef9d44e975489468295 [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
27type 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.
Jingwen Chen12b4c272021-03-10 02:05:59 -050035 //
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 Kammerba3ea162021-02-17 13:22:03 -050042}
43
44// 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.
48 Bazel_module bazelModuleProperties
49}
Liz Kammerea6666f2021-02-17 10:17:28 -050050
51// BazelModuleBase contains the property structs with metadata for modules which can be converted to
52// Bazel.
53type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050054 bazelProperties properties
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)
Liz Kammerea6666f2021-02-17 10:17:28 -050066}
67
68// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
69type BazelModule interface {
70 Module
71 Bazelable
72}
73
74// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
75// properties.
76func InitBazelModule(module BazelModule) {
77 module.AddProperties(module.bazelProps())
78}
79
80// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -050081func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -050082 return &b.bazelProperties
83}
84
Liz Kammerba3ea162021-02-17 13:22:03 -050085// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
86func (b *BazelModuleBase) HasHandcraftedLabel() bool {
87 return b.bazelProperties.Bazel_module.Label != nil
88}
89
90// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
91func (b *BazelModuleBase) HandcraftedLabel() string {
92 return proptools.String(b.bazelProperties.Bazel_module.Label)
93}
94
Liz Kammerea6666f2021-02-17 10:17:28 -050095// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -050096func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
97 if b.HasHandcraftedLabel() {
98 return b.HandcraftedLabel()
99 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500100 if b.ConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500101 return bp2buildModuleLabel(ctx, module)
102 }
103 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -0500104}
105
Jingwen Chen12b4c272021-03-10 02:05:59 -0500106// Configuration to decide if modules in a directory should default to true/false for bp2build_available
107type Bp2BuildConfig map[string]BazelConversionConfigEntry
108type BazelConversionConfigEntry int
109
110const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400111 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
112 // no package path. This is also the module dir for top level Android.bp
113 // modules.
114 BP2BUILD_TOPLEVEL = "."
115
Jingwen Chen12b4c272021-03-10 02:05:59 -0500116 // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
117 // which can also mean that the key doesn't exist in a lookup.
118
119 // all modules in this package and subpackages default to bp2build_available: true.
120 // allows modules to opt-out.
121 Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
122
Jingwen Chen294e7742021-08-31 05:58:01 +0000123 // all modules in this package (not recursively) default to bp2build_available: true.
124 // allows modules to opt-out.
125 Bp2BuildDefaultTrue
126
Jingwen Chen12b4c272021-03-10 02:05:59 -0500127 // all modules in this package (not recursively) default to bp2build_available: false.
128 // allows modules to opt-in.
129 Bp2BuildDefaultFalse
130)
131
132var (
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400133 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000134 // in the synthetic Bazel workspace.
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400135 bp2buildKeepExistingBuildFile = map[string]bool{
136 // This is actually build/bazel/build.BAZEL symlinked to ./BUILD
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000137 ".":/*recursive = */ false,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400138
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000139 // build/bazel/examples/apex/... BUILD files should be generated, so
140 // build/bazel is not recursive. Instead list each subdirectory under
141 // build/bazel explicitly.
142 "build/bazel":/* recursive = */ false,
143 "build/bazel/examples/android_app":/* recursive = */ true,
Romain Jobredeaux5cc9c9d2021-08-26 15:07:48 +0000144 "build/bazel/examples/java":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000145 "build/bazel/bazel_skylib":/* recursive = */ true,
146 "build/bazel/rules":/* recursive = */ true,
147 "build/bazel/rules_cc":/* recursive = */ true,
Jingwen Chen294e7742021-08-31 05:58:01 +0000148 "build/bazel/scripts":/* recursive = */ true,
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000149 "build/bazel/tests":/* recursive = */ true,
150 "build/bazel/platforms":/* recursive = */ true,
151 "build/bazel/product_variables":/* recursive = */ true,
Jingwen Chen5e49b822021-08-24 05:14:22 +0000152 "build/bazel_common_rules":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000153 "build/make/tools":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400154 "build/pesto":/* recursive = */ true,
155
156 // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
157 // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed
158 "external/bazelbuild-rules_android":/* recursive = */ true,
Jingwen Chen91632252021-08-10 13:00:33 +0000159 "external/bazel-skylib":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000160 "external/guava":/* recursive = */ true,
161 "external/error_prone":/* recursive = */ true,
162 "external/jsr305":/* recursive = */ true,
163 "frameworks/ex/common":/* recursive = */ true,
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400164
Romain Jobredeauxf1b0ac82021-08-12 14:39:00 +0000165 "packages/apps/Music":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000166 "packages/apps/QuickSearchBox":/* recursive = */ true,
Romain Jobredeauxa2c08142021-09-22 14:43:43 -0400167 "packages/apps/WallpaperPicker":/* recursive = */ false,
168
169 "prebuilts/sdk":/* recursive = */ false,
170 "prebuilts/sdk/current/extras/app-toolkit":/* recursive = */ false,
171 "prebuilts/sdk/current/support":/* recursive = */ false,
172 "prebuilts/sdk/tools":/* recursive = */ false,
173 "prebuilts/r8":/* recursive = */ false,
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400174 }
175
Jingwen Chen12b4c272021-03-10 02:05:59 -0500176 // Configure modules in these directories to enable bp2build_available: true or false by default.
177 bp2buildDefaultConfig = Bp2BuildConfig{
Chris Parsonsa37e1952021-09-28 16:47:36 -0400178 "bionic": Bp2BuildDefaultTrueRecursively,
179 "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
180 "development/sdk": Bp2BuildDefaultTrueRecursively,
181 "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
182 "external/boringssl": Bp2BuildDefaultTrueRecursively,
183 "external/brotli": Bp2BuildDefaultTrue,
184 "external/fmtlib": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400185 "external/google-benchmark": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400186 "external/googletest/googletest": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400187 "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
188 "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400189 "external/jsoncpp": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400190 "external/libcap": Bp2BuildDefaultTrueRecursively,
191 "external/libcxx": Bp2BuildDefaultTrueRecursively,
192 "external/libcxxabi": Bp2BuildDefaultTrueRecursively,
193 "external/lz4/lib": Bp2BuildDefaultTrue,
Liz Kammer2fc34892021-10-11 12:56:48 -0400194 "external/mdnsresponder": Bp2BuildDefaultTrueRecursively,
195 "external/minijail": Bp2BuildDefaultTrueRecursively,
196 "external/pcre": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400197 "external/protobuf": Bp2BuildDefaultTrueRecursively,
198 "external/python/six": Bp2BuildDefaultTrueRecursively,
199 "external/scudo": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400200 "external/selinux/libselinux": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400201 "external/zlib": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400202 "external/zstd": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400203 "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively,
204 "packages/modules/adb": Bp2BuildDefaultTrue,
205 "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively,
206 "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively,
207 "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively,
208 "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively,
209 "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively,
210 "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively,
211 "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively,
212 "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively,
213 "system/core/libasyncio": Bp2BuildDefaultTrue,
214 "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively,
215 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
Liz Kammer2fc34892021-10-11 12:56:48 -0400216 "system/core/libpackagelistparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400217 "system/core/libprocessgroup": Bp2BuildDefaultTrue,
Chris Parsonsb97a8172021-10-04 12:48:29 -0400218 "system/core/libprocessgroup/cgrouprc": Bp2BuildDefaultTrue,
219 "system/core/libprocessgroup/cgrouprc_format": Bp2BuildDefaultTrue,
Lukacs T. Berki497f17d2021-04-26 12:15:57 +0200220 "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400221 "system/libbase": Bp2BuildDefaultTrueRecursively,
Chris Parsonsc39f6332021-10-01 18:00:31 -0400222 "system/libziparchive": Bp2BuildDefaultTrueRecursively,
Chris Parsonsa37e1952021-09-28 16:47:36 -0400223 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
224 "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively,
225 "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
226 "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500227 }
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000228
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400229 // Per-module denylist to always opt modules out of both bp2build and mixed builds.
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000230 bp2buildModuleDoNotConvertList = []string{
Chris Parsonsc39f6332021-10-01 18:00:31 -0400231 "libc_malloc_debug", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Rupert Shuttleworth47aa5842021-04-30 04:04:15 -0400232
Chris Parsonsc39f6332021-10-01 18:00:31 -0400233 "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 +0000234
Liz Kammer0f3b7d22021-09-28 13:48:21 -0400235 "libprotobuf-python", // contains .proto sources
236 "libprotobuf-internal-protos", // we don't handle path property for fileegroups
237 "libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups
238
Chris Parsonsa37e1952021-09-28 16:47:36 -0400239 "libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds.
240 "libfdtrack", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200241
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400242 "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset
243
Chris Parsonsb97a8172021-10-04 12:48:29 -0400244 "brotli-fuzzer-corpus", // b/202015218: outputs are in location incompatible with bazel genrule handling.
Jingwen Chen294e7742021-08-31 05:58:01 +0000245
Jingwen Chendf27b7a2021-10-18 06:33:16 +0000246 // b/203369847: multiple genrules in the same package creating the same file
247 // //development/sdk/...
248 "platform_tools_properties",
249 "build_tools_source_properties",
250
Jingwen Chen1e347862021-09-02 12:11:49 +0000251 // //external/libcap/...
Liz Kammerb84b8c92021-11-02 15:03:12 -0400252 "cap_names.h", // http://b/196105070 host toolchain misconfigurations for mixed builds
253 "libcap", // http://b/196105070 host toolchain misconfigurations for mixed builds
Jingwen Chen1e347862021-09-02 12:11:49 +0000254
Liz Kammer2fc34892021-10-11 12:56:48 -0400255 "libminijail", // depends on unconverted modules: libcap
Liz Kammer2b8004b2021-10-04 13:55:44 -0400256 "getcap", // depends on unconverted modules: libcap
257 "setcap", // depends on unconverted modules: libcap
258 "minijail0", // depends on unconverted modules: libcap, libminijail
259 "drop_privs", // depends on unconverted modules: libminijail
Liz Kammer2fc34892021-10-11 12:56:48 -0400260
Jingwen Chen790324e2021-04-30 08:20:01 +0000261 // Tests. Handle later.
262 "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
263 "libjemalloc5_integrationtest",
264 "libjemalloc5_stresstestlib",
265 "libjemalloc5_unittest",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400266
267 // APEX support
Chris Parsonsb97a8172021-10-04 12:48:29 -0400268 "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug'
Chris Parsonsa37e1952021-09-28 16:47:36 -0400269
270 "libadb_crypto", // Depends on libadb_protos
271 "libadb_crypto_static", // Depends on libadb_protos_static
272 "libadb_pairing_connection", // Depends on libadb_protos
273 "libadb_pairing_connection_static", // Depends on libadb_protos_static
274 "libadb_pairing_server", // Depends on libadb_protos
275 "libadb_pairing_server_static", // Depends on libadb_protos_static
276 "libadbd", // Depends on libadbd_core
277 "libadbd_core", // Depends on libadb_protos
278 "libadbd_services", // Depends on libadb_protos
279
280 "libadb_protos_static", // b/200601772: Requires cc_library proto support
281 "libadb_protos", // b/200601772: Requires cc_library proto support
282 "libapp_processes_protos_lite", // b/200601772: Requires cc_library proto support
Chris Parsonsc39f6332021-10-01 18:00:31 -0400283
284 "libgtest_ndk_c++", // b/201816222: Requires sdk_version support.
285 "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support.
Liz Kammer2b8004b2021-10-04 13:55:44 -0400286
287 "abb", // depends on unconverted modules: libadbd_core, libadbd_services, libcmd, libbinder, libutils, libselinux
288 "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
289 "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
290 "bionic_tests_zipalign", // depends on unconverted modules: libziparchive, libutils
291 "linker", // depends on unconverted modules: liblinker_debuggerd_stub, libdebuggerd_handler_fallback, libziparchive, liblinker_main, liblinker_malloc
292 "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_*
293 "sefcontext_compile", // depends on unconverted modules: libsepol
294 "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host
295
296 "linkerconfig", // http://b/202876379 has arch-variant static_executable
297 "mdnsd", // http://b/202876379 has arch-variant static_executable
298
299 "acvp_modulewrapper", // disabled for android x86/x86_64
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400300 }
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400301
Jingwen Chen179856a2021-05-03 09:15:48 +0000302 // Per-module denylist of cc_library modules to only generate the static
303 // variant if their shared variant isn't ready or buildable by Bazel.
304 bp2buildCcLibraryStaticOnlyList = []string{
Jingwen Chen53542922021-05-18 09:01:49 +0000305 "libjemalloc5", // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets.
Jingwen Chen179856a2021-05-03 09:15:48 +0000306 }
307
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400308 // Per-module denylist to opt modules out of mixed builds. Such modules will
309 // still be generated via bp2build.
Chris Parsons2c788392021-08-10 11:58:07 -0400310 mixedBuildsDisabledList = []string{
Wei Lic989eaf2021-11-03 01:48:03 +0000311 "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
312 "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.
313 "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.
314 "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.
315 "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.
316 "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 -0400317 }
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000318
319 // Used for quicker lookups
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400320 bp2buildModuleDoNotConvert = map[string]bool{}
Jingwen Chen179856a2021-05-03 09:15:48 +0000321 bp2buildCcLibraryStaticOnly = map[string]bool{}
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400322 mixedBuildsDisabled = map[string]bool{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500323)
324
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000325func init() {
326 for _, moduleName := range bp2buildModuleDoNotConvertList {
327 bp2buildModuleDoNotConvert[moduleName] = true
328 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400329
Jingwen Chen179856a2021-05-03 09:15:48 +0000330 for _, moduleName := range bp2buildCcLibraryStaticOnlyList {
331 bp2buildCcLibraryStaticOnly[moduleName] = true
332 }
333
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400334 for _, moduleName := range mixedBuildsDisabledList {
335 mixedBuildsDisabled[moduleName] = true
336 }
337}
338
Chris Parsons953b3562021-09-20 15:14:39 -0400339func GenerateCcLibraryStaticOnly(moduleName string) bool {
340 return bp2buildCcLibraryStaticOnly[moduleName]
Jingwen Chen179856a2021-05-03 09:15:48 +0000341}
342
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400343func ShouldKeepExistingBuildFileForDir(dir string) bool {
344 if _, ok := bp2buildKeepExistingBuildFile[dir]; ok {
345 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400346 return true
347 }
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400348 // Check if subtree match
349 for prefix, recursive := range bp2buildKeepExistingBuildFile {
350 if recursive {
351 if strings.HasPrefix(dir, prefix+"/") {
352 return true
353 }
354 }
355 }
356 // Default
357 return false
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400358}
359
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400360// MixedBuildsEnabled checks that a module is ready to be replaced by a
361// converted or handcrafted Bazel target.
362func (b *BazelModuleBase) MixedBuildsEnabled(ctx BazelConversionPathContext) bool {
363 if !ctx.Config().BazelContext.BazelEnabled() {
364 return false
365 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400366 if !convertedToBazel(ctx, ctx.Module()) {
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400367 return false
368 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400369
Chris Parsons953b3562021-09-20 15:14:39 -0400370 if GenerateCcLibraryStaticOnly(ctx.Module().Name()) {
Jingwen Chen179856a2021-05-03 09:15:48 +0000371 // Don't use partially-converted cc_library targets in mixed builds,
372 // since mixed builds would generally rely on both static and shared
373 // variants of a cc_library.
374 return false
375 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400376 return !mixedBuildsDisabled[ctx.Module().Name()]
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000377}
378
Liz Kammer6eff3232021-08-26 08:37:59 -0400379// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000380func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400381 b, ok := module.(Bazelable)
382 if !ok {
383 return false
384 }
385 return b.convertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
386}
387
Liz Kammerea6666f2021-02-17 10:17:28 -0500388// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000389func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionContext) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400390 return b.convertWithBp2build(ctx, ctx.Module())
391}
392
Jingwen Chen55bc8202021-11-02 06:40:51 +0000393func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400394 if bp2buildModuleDoNotConvert[module.Name()] {
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000395 return false
396 }
397
Jingwen Chen12b4c272021-03-10 02:05:59 -0500398 // Ensure that the module type of this module has a bp2build converter. This
399 // prevents mixed builds from using auto-converted modules just by matching
400 // the package dir; it also has to have a bp2build mutator as well.
Liz Kammer6eff3232021-08-26 08:37:59 -0400401 if ctx.Config().bp2buildModuleTypeConfig[ctx.OtherModuleType(module)] == false {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500402 return false
403 }
404
Liz Kammer6eff3232021-08-26 08:37:59 -0400405 packagePath := ctx.OtherModuleDir(module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500406 config := ctx.Config().bp2buildPackageConfig
407
408 // This is a tristate value: true, false, or unset.
409 propValue := b.bazelProperties.Bazel_module.Bp2build_available
410 if bp2buildDefaultTrueRecursively(packagePath, config) {
411 // Allow modules to explicitly opt-out.
412 return proptools.BoolDefault(propValue, true)
413 }
414
415 // Allow modules to explicitly opt-in.
416 return proptools.BoolDefault(propValue, false)
417}
418
419// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
420// set of package prefixes where all modules must be converted. That is, if the
421// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
422// return true.
423//
424// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
425// exactly, this module will return false early.
426//
427// This function will also return false if the package doesn't match anything in
428// the config.
429func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
430 ret := false
431
Jingwen Chen294e7742021-08-31 05:58:01 +0000432 // Check if the package path has an exact match in the config.
433 if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively {
Jingwen Chen91220d72021-03-24 02:18:33 -0400434 return true
Jingwen Chen294e7742021-08-31 05:58:01 +0000435 } else if config[packagePath] == Bp2BuildDefaultFalse {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500436 return false
437 }
438
Jingwen Chen91220d72021-03-24 02:18:33 -0400439 // If not, check for the config recursively.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500440 packagePrefix := ""
441 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
442 for _, part := range strings.Split(packagePath, "/") {
443 packagePrefix += part
444 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
445 // package contains this prefix and this prefix should convert all modules
446 return true
447 }
448 // Continue to the next part of the package dir.
449 packagePrefix += "/"
450 }
451
452 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500453}
Liz Kammerba3ea162021-02-17 13:22:03 -0500454
455// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
456// an error if there are errors reading the file.
457// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
458// something more targeted based on the rule type and target.
459func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500460 if !strings.Contains(b.HandcraftedLabel(), path) {
461 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500462 }
463 name = filepath.Join(path, name)
464 f, err := c.fs.Open(name)
465 if err != nil {
466 return "", err
467 }
468 defer f.Close()
469
470 data, err := ioutil.ReadAll(f)
471 if err != nil {
472 return "", err
473 }
474 return string(data[:]), nil
475}