blob: bfd0f909a05cfc578aeff9e02be762ebbd04fbf7 [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 Chen12b4c272021-03-10 02:05:59 -050063 ConvertWithBp2build(ctx BazelConversionPathContext) bool
Liz Kammer6eff3232021-08-26 08:37:59 -040064 convertWithBp2build(ctx BazelConversionPathContext, 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
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400165 "prebuilts/sdk":/* recursive = */ false,
166 "prebuilts/sdk/tools":/* recursive = */ false,
Romain Jobredeaux8c98c252021-08-02 17:27:06 +0000167 "prebuilts/r8":/* recursive = */ false,
Romain Jobredeauxf1b0ac82021-08-12 14:39:00 +0000168 "packages/apps/Music":/* recursive = */ true,
Romain Jobredeaux9e09bba2021-09-08 18:09:02 +0000169 "packages/apps/QuickSearchBox":/* recursive = */ true,
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400170 }
171
Jingwen Chen12b4c272021-03-10 02:05:59 -0500172 // Configure modules in these directories to enable bp2build_available: true or false by default.
173 bp2buildDefaultConfig = Bp2BuildConfig{
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000174 "bionic": Bp2BuildDefaultTrueRecursively,
175 "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
Jingwen Chena26246d2021-08-31 05:26:41 +0000176 "development/sdk": Bp2BuildDefaultTrueRecursively,
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000177 "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
Jingwen Chen294e7742021-08-31 05:58:01 +0000178 "external/brotli": Bp2BuildDefaultTrue,
Jingwen Chenf59a8e12021-07-16 09:28:53 +0000179 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
Jingwen Chen294e7742021-08-31 05:58:01 +0000180 "system/core/libprocessgroup": Bp2BuildDefaultTrue,
Lukacs T. Berki497f17d2021-04-26 12:15:57 +0200181 "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
Jingwen Chen28288162021-04-28 07:19:54 +0000182 "system/libbase": Bp2BuildDefaultTrueRecursively,
183 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
Liz Kammer37997c42021-09-14 17:53:38 -0400184 "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively,
Jingwen Chen739d01e2021-07-28 15:26:20 +0000185 "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
186 "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
Jingwen Chen28288162021-04-28 07:19:54 +0000187 "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
Chris Parsons2c788392021-08-10 11:58:07 -0400188 "external/fmtlib": Bp2BuildDefaultTrueRecursively,
189 "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
Jingwen Chen294e7742021-08-31 05:58:01 +0000190 "external/libcxx": Bp2BuildDefaultTrueRecursively,
Chris Parsons2c788392021-08-10 11:58:07 -0400191 "external/libcxxabi": Bp2BuildDefaultTrueRecursively,
Jingwen Chen1e347862021-09-02 12:11:49 +0000192 "external/libcap": Bp2BuildDefaultTrueRecursively,
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000193 "external/scudo": Bp2BuildDefaultTrueRecursively,
Jingwen Chen49109762021-05-25 05:16:48 +0000194 "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500195 }
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000196
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400197 // Per-module denylist to always opt modules out of both bp2build and mixed builds.
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000198 bp2buildModuleDoNotConvertList = []string{
Jingwen Chen28288162021-04-28 07:19:54 +0000199 // Things that transitively depend on unconverted libc_* modules.
Rupert Shuttleworth47aa5842021-04-30 04:04:15 -0400200 "libbionic_spawn_benchmark", // http://b/186824595, cc_library_static, depends on //external/google-benchmark (http://b/186822740)
201 // also depends on //system/logging/liblog:liblog (http://b/186822772)
202
203 "libc_malloc_debug", // http://b/186824339, cc_library_static, depends on //system/libbase:libbase (http://b/186823646)
204 "libc_malloc_debug_backtrace", // http://b/186824112, cc_library_static, depends on //external/libcxxabi:libc++demangle (http://b/186823773)
205
206 "libcutils", // http://b/186827426, cc_library, depends on //system/core/libprocessgroup:libprocessgroup_headers (http://b/186826841)
207 "libcutils_sockets", // http://b/186826853, cc_library, depends on //system/libbase:libbase (http://b/186826479)
208
209 "liblinker_debuggerd_stub", // http://b/186824327, cc_library_static, depends on //external/zlib:libz (http://b/186823782)
210 // also depends on //system/libziparchive:libziparchive (http://b/186823656)
211 // also depends on //system/logging/liblog:liblog (http://b/186822772)
212 "liblinker_main", // http://b/186825989, cc_library_static, depends on //external/zlib:libz (http://b/186823782)
213 // also depends on //system/libziparchive:libziparchive (http://b/186823656)
214 // also depends on//system/logging/liblog:liblog (http://b/186822772)
215 "liblinker_malloc", // http://b/186826466, cc_library_static, depends on //external/zlib:libz (http://b/186823782)
216 // also depends on //system/libziparchive:libziparchive (http://b/186823656)
217 // also depends on //system/logging/liblog:liblog (http://b/186822772)
Jingwen Chen53542922021-05-18 09:01:49 +0000218 "libc_ndk", // http://b/187013218, cc_library_static, depends on //bionic/libm:libm (http://b/183064661)
Jingwen Chen53542922021-05-18 09:01:49 +0000219 "libc_malloc_hooks", // http://b/187016307, cc_library, ld.lld: error: undefined symbol: __malloc_hook
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200220
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000221 // There are unexported symbols that don't surface on a shared library build,
222 // from the source static archive
223 // e.g. _Unwind_{GetRegionStart,GetLanguageSpecificData,GetIP,Set{IP,GR},Resume,{Raise,Delete}Exception}, pthread_atfork
224 // ... from: cxa_{personality,exception}.o, system_error.o, wrappers_c_bionic.o
225 // cf. http://b/198403271
226 "libc++",
227
Jingwen Chen790324e2021-04-30 08:20:01 +0000228 // http://b/186823769: Needs C++ STL support, includes from unconverted standard libraries in //external/libcxx
229 // c++_static
Rupert Shuttleworthfb955382021-05-03 04:32:36 -0400230 "libbase_ndk", // http://b/186826477, cc_library, no such target '//build/bazel/platforms/os:darwin' when --platforms //build/bazel/platforms:android_x86 is added
Jingwen Chen790324e2021-04-30 08:20:01 +0000231 // libcxx
232 "libBionicBenchmarksUtils", // cc_library_static, fatal error: 'map' file not found, from libcxx
233 "fmtlib", // cc_library_static, fatal error: 'cassert' file not found, from libcxx
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400234 "fmtlib_ndk", // cc_library_static, fatal error: 'cassert' file not found
Chris Parsons51f8c392021-08-03 21:01:05 -0400235 "liblog", // http://b/186822772: cc_library, 'sys/cdefs.h' file not found
Chris Parsons69fa9f92021-07-13 11:47:44 -0400236 "libbase", // Requires liblog. http://b/186826479, cc_library, fatal error: 'memory' file not found, from libcxx.
Chris Parsons51f8c392021-08-03 21:01:05 -0400237 // Also depends on fmtlib.
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200238
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000239 "libfdtrack", // depends on STL
240
Chris Parsons51f8c392021-08-03 21:01:05 -0400241 "libseccomp_policy", // depends on libbase
Lukacs T. Berkib5ac5af2021-04-27 11:02:11 +0200242
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400243 "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset
244
Jingwen Chen294e7742021-08-31 05:58:01 +0000245 //system/core/libprocessgroup/...
246 "libprocessgroup", // depends on //system/core/libprocessgroup/cgrouprc:libcgrouprc
247
248 //external/brotli/...
249 "brotli-fuzzer-corpus", // "declared output 'external/brotli/c/fuzz/73231c6592f195ffd41100b8706d1138ff6893b9' was not created by genrule"
250
Jingwen Chen1e347862021-09-02 12:11:49 +0000251 // //external/libcap/...
252 "libcap", // http://b/198595332, depends on _makenames, a cc_binary
253 "cap_names.h", // http://b/198596102, depends on _makenames, a cc_binary
254
Jingwen Chen790324e2021-04-30 08:20:01 +0000255 // Tests. Handle later.
256 "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
257 "libjemalloc5_integrationtest",
258 "libjemalloc5_stresstestlib",
259 "libjemalloc5_unittest",
Rupert Shuttleworth6e4950a2021-07-27 01:34:59 -0400260
261 // APEX support
262 "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug' and 'libc_malloc_hooks'
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400263 }
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400264
Jingwen Chen179856a2021-05-03 09:15:48 +0000265 // Per-module denylist of cc_library modules to only generate the static
266 // variant if their shared variant isn't ready or buildable by Bazel.
267 bp2buildCcLibraryStaticOnlyList = []string{
Jingwen Chen53542922021-05-18 09:01:49 +0000268 "libjemalloc5", // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets.
Jingwen Chen179856a2021-05-03 09:15:48 +0000269 }
270
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400271 // Per-module denylist to opt modules out of mixed builds. Such modules will
272 // still be generated via bp2build.
Chris Parsons2c788392021-08-10 11:58:07 -0400273 mixedBuildsDisabledList = []string{
Jingwen Chen294e7742021-08-31 05:58:01 +0000274 "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
275 "libc++fs", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
276 "libc++_experimental", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
277 "libc++_static", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
278 "libc++abi", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects.
279 "libc++demangle", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects.
Chris Parsons2c788392021-08-10 11:58:07 -0400280 }
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000281
282 // Used for quicker lookups
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400283 bp2buildModuleDoNotConvert = map[string]bool{}
Jingwen Chen179856a2021-05-03 09:15:48 +0000284 bp2buildCcLibraryStaticOnly = map[string]bool{}
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400285 mixedBuildsDisabled = map[string]bool{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500286)
287
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000288func init() {
289 for _, moduleName := range bp2buildModuleDoNotConvertList {
290 bp2buildModuleDoNotConvert[moduleName] = true
291 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400292
Jingwen Chen179856a2021-05-03 09:15:48 +0000293 for _, moduleName := range bp2buildCcLibraryStaticOnlyList {
294 bp2buildCcLibraryStaticOnly[moduleName] = true
295 }
296
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400297 for _, moduleName := range mixedBuildsDisabledList {
298 mixedBuildsDisabled[moduleName] = true
299 }
300}
301
Chris Parsons953b3562021-09-20 15:14:39 -0400302func GenerateCcLibraryStaticOnly(moduleName string) bool {
303 return bp2buildCcLibraryStaticOnly[moduleName]
Jingwen Chen179856a2021-05-03 09:15:48 +0000304}
305
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400306func ShouldKeepExistingBuildFileForDir(dir string) bool {
307 if _, ok := bp2buildKeepExistingBuildFile[dir]; ok {
308 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400309 return true
310 }
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400311 // Check if subtree match
312 for prefix, recursive := range bp2buildKeepExistingBuildFile {
313 if recursive {
314 if strings.HasPrefix(dir, prefix+"/") {
315 return true
316 }
317 }
318 }
319 // Default
320 return false
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400321}
322
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400323// MixedBuildsEnabled checks that a module is ready to be replaced by a
324// converted or handcrafted Bazel target.
325func (b *BazelModuleBase) MixedBuildsEnabled(ctx BazelConversionPathContext) bool {
326 if !ctx.Config().BazelContext.BazelEnabled() {
327 return false
328 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400329 if !convertedToBazel(ctx, ctx.Module()) {
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400330 return false
331 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400332
Chris Parsons953b3562021-09-20 15:14:39 -0400333 if GenerateCcLibraryStaticOnly(ctx.Module().Name()) {
Jingwen Chen179856a2021-05-03 09:15:48 +0000334 // Don't use partially-converted cc_library targets in mixed builds,
335 // since mixed builds would generally rely on both static and shared
336 // variants of a cc_library.
337 return false
338 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400339 return !mixedBuildsDisabled[ctx.Module().Name()]
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000340}
341
Liz Kammer6eff3232021-08-26 08:37:59 -0400342// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
343func convertedToBazel(ctx BazelConversionPathContext, module blueprint.Module) bool {
344 b, ok := module.(Bazelable)
345 if !ok {
346 return false
347 }
348 return b.convertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
349}
350
Liz Kammerea6666f2021-02-17 10:17:28 -0500351// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500352func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400353 return b.convertWithBp2build(ctx, ctx.Module())
354}
355
356func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionPathContext, module blueprint.Module) bool {
357 if bp2buildModuleDoNotConvert[module.Name()] {
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000358 return false
359 }
360
Jingwen Chen12b4c272021-03-10 02:05:59 -0500361 // Ensure that the module type of this module has a bp2build converter. This
362 // prevents mixed builds from using auto-converted modules just by matching
363 // the package dir; it also has to have a bp2build mutator as well.
Liz Kammer6eff3232021-08-26 08:37:59 -0400364 if ctx.Config().bp2buildModuleTypeConfig[ctx.OtherModuleType(module)] == false {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500365 return false
366 }
367
Liz Kammer6eff3232021-08-26 08:37:59 -0400368 packagePath := ctx.OtherModuleDir(module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500369 config := ctx.Config().bp2buildPackageConfig
370
371 // This is a tristate value: true, false, or unset.
372 propValue := b.bazelProperties.Bazel_module.Bp2build_available
373 if bp2buildDefaultTrueRecursively(packagePath, config) {
374 // Allow modules to explicitly opt-out.
375 return proptools.BoolDefault(propValue, true)
376 }
377
378 // Allow modules to explicitly opt-in.
379 return proptools.BoolDefault(propValue, false)
380}
381
382// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
383// set of package prefixes where all modules must be converted. That is, if the
384// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
385// return true.
386//
387// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
388// exactly, this module will return false early.
389//
390// This function will also return false if the package doesn't match anything in
391// the config.
392func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
393 ret := false
394
Jingwen Chen294e7742021-08-31 05:58:01 +0000395 // Check if the package path has an exact match in the config.
396 if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively {
Jingwen Chen91220d72021-03-24 02:18:33 -0400397 return true
Jingwen Chen294e7742021-08-31 05:58:01 +0000398 } else if config[packagePath] == Bp2BuildDefaultFalse {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500399 return false
400 }
401
Jingwen Chen91220d72021-03-24 02:18:33 -0400402 // If not, check for the config recursively.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500403 packagePrefix := ""
404 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
405 for _, part := range strings.Split(packagePath, "/") {
406 packagePrefix += part
407 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
408 // package contains this prefix and this prefix should convert all modules
409 return true
410 }
411 // Continue to the next part of the package dir.
412 packagePrefix += "/"
413 }
414
415 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500416}
Liz Kammerba3ea162021-02-17 13:22:03 -0500417
418// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
419// an error if there are errors reading the file.
420// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
421// something more targeted based on the rule type and target.
422func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500423 if !strings.Contains(b.HandcraftedLabel(), path) {
424 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500425 }
426 name = filepath.Join(path, name)
427 f, err := c.fs.Open(name)
428 if err != nil {
429 return "", err
430 }
431 defer f.Close()
432
433 data, err := ioutil.ReadAll(f)
434 if err != nil {
435 return "", err
436 }
437 return string(data[:]), nil
438}