Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 17 | import ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "path/filepath" |
| 21 | "strings" |
| 22 | |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | type 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 Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 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 Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // Properties contains common module properties for Bazel migration purposes. |
| 45 | type 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 Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 50 | |
| 51 | // BazelModuleBase contains the property structs with metadata for modules which can be converted to |
| 52 | // Bazel. |
| 53 | type BazelModuleBase struct { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 54 | bazelProperties properties |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Bazelable is specifies the interface for modules that can be converted to Bazel. |
| 58 | type Bazelable interface { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 59 | bazelProps() *properties |
| 60 | HasHandcraftedLabel() bool |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 61 | HandcraftedLabel() string |
| 62 | GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 63 | ConvertWithBp2build(ctx BazelConversionPathContext) bool |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 64 | GetBazelBuildFileContents(c Config, path, name string) (string, error) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 65 | ConvertedToBazel(ctx BazelConversionPathContext) bool |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules. |
| 69 | type BazelModule interface { |
| 70 | Module |
| 71 | Bazelable |
| 72 | } |
| 73 | |
| 74 | // InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion |
| 75 | // properties. |
| 76 | func InitBazelModule(module BazelModule) { |
| 77 | module.AddProperties(module.bazelProps()) |
| 78 | } |
| 79 | |
| 80 | // bazelProps returns the Bazel properties for the given BazelModuleBase. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 81 | func (b *BazelModuleBase) bazelProps() *properties { |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 82 | return &b.bazelProperties |
| 83 | } |
| 84 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 85 | // HasHandcraftedLabel returns whether this module has a handcrafted Bazel label. |
| 86 | func (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 |
| 91 | func (b *BazelModuleBase) HandcraftedLabel() string { |
| 92 | return proptools.String(b.bazelProperties.Bazel_module.Label) |
| 93 | } |
| 94 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 95 | // GetBazelLabel returns the Bazel label for the given BazelModuleBase. |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 96 | func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string { |
| 97 | if b.HasHandcraftedLabel() { |
| 98 | return b.HandcraftedLabel() |
| 99 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 100 | if b.ConvertWithBp2build(ctx) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 101 | return bp2buildModuleLabel(ctx, module) |
| 102 | } |
| 103 | return "" // no label for unconverted module |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 106 | // Configuration to decide if modules in a directory should default to true/false for bp2build_available |
| 107 | type Bp2BuildConfig map[string]BazelConversionConfigEntry |
| 108 | type BazelConversionConfigEntry int |
| 109 | |
| 110 | const ( |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 111 | // 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 Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 116 | // 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 | |
| 123 | // all modules in this package (not recursively) default to bp2build_available: false. |
| 124 | // allows modules to opt-in. |
| 125 | Bp2BuildDefaultFalse |
| 126 | ) |
| 127 | |
| 128 | var ( |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 129 | // Do not write BUILD files for these directories |
| 130 | // NOTE: this is not recursive |
| 131 | bp2buildDoNotWriteBuildFileList = []string{ |
| 132 | // Don't generate these BUILD files - because external BUILD files already exist |
| 133 | "external/boringssl", |
| 134 | "external/brotli", |
| 135 | "external/dagger2", |
| 136 | "external/flatbuffers", |
| 137 | "external/gflags", |
| 138 | "external/google-fruit", |
| 139 | "external/grpc-grpc", |
| 140 | "external/grpc-grpc/test/core/util", |
| 141 | "external/grpc-grpc/test/cpp/common", |
| 142 | "external/grpc-grpc/third_party/address_sorting", |
| 143 | "external/nanopb-c", |
| 144 | "external/nos/host/generic", |
| 145 | "external/nos/host/generic/libnos", |
| 146 | "external/nos/host/generic/libnos/generator", |
| 147 | "external/nos/host/generic/libnos_datagram", |
| 148 | "external/nos/host/generic/libnos_transport", |
| 149 | "external/nos/host/generic/nugget/proto", |
| 150 | "external/perfetto", |
| 151 | "external/protobuf", |
| 152 | "external/rust/cxx", |
| 153 | "external/rust/cxx/demo", |
| 154 | "external/ruy", |
| 155 | "external/tensorflow", |
| 156 | "external/tensorflow/tensorflow/lite", |
| 157 | "external/tensorflow/tensorflow/lite/java", |
| 158 | "external/tensorflow/tensorflow/lite/kernels", |
| 159 | "external/tflite-support", |
| 160 | "external/tinyalsa_new", |
| 161 | "external/wycheproof", |
| 162 | "external/libyuv", |
| 163 | } |
| 164 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 165 | // Configure modules in these directories to enable bp2build_available: true or false by default. |
| 166 | bp2buildDefaultConfig = Bp2BuildConfig{ |
| 167 | "bionic": Bp2BuildDefaultTrueRecursively, |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame] | 168 | "external/gwp_asan": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 169 | "system/core/libcutils": Bp2BuildDefaultTrueRecursively, |
Lukacs T. Berki | 497f17d | 2021-04-26 12:15:57 +0200 | [diff] [blame] | 170 | "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame^] | 171 | "system/libbase": Bp2BuildDefaultTrueRecursively, |
| 172 | "system/logging/liblog": Bp2BuildDefaultTrueRecursively, |
| 173 | "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 174 | } |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 175 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 176 | // Per-module denylist to always opt modules out of both bp2build and mixed builds. |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 177 | bp2buildModuleDoNotConvertList = []string{ |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame^] | 178 | // Things that transitively depend on unconverted libc_* modules. |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 179 | "libc_common", // ruperts@, cc_library_static, depends on //bionic/libc:libc_nopthread |
| 180 | "libc_common_static", // ruperts@, cc_library_static, depends on //bionic/libc:libc_common |
| 181 | "libc_common_shared", // ruperts@, cc_library_static, depends on //bionic/libc:libc_common |
| 182 | "libc_nomalloc", // ruperts@, cc_library_static, depends on //bionic/libc:libc_common |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame^] | 183 | "libc_nopthread", // ruperts@, cc_library_static, depends on lib_bionic_ndk, libc_syscalls, libc_tzcode, libstdc++ |
Jingwen Chen | 0a92ed7 | 2021-04-12 05:37:42 +0000 | [diff] [blame] | 184 | |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 185 | // Things that transitively depend on //system/libbase. libbase doesn't work because: |
Liz Kammer | 2b50ce6 | 2021-04-26 15:47:28 -0400 | [diff] [blame] | 186 | // fmtlib: fatal error: 'cassert' file not found |
Liz Kammer | a3b5dcf | 2021-04-27 15:58:10 -0400 | [diff] [blame] | 187 | "libbase", // eakammer@, cc_library, no such target '//build/bazel/platforms/os:darwin': target 'darwin' not declared |
| 188 | "libbase_ndk", // eakammer@, cc_library, no such target '//build/bazel/platforms/os:darwin': target 'darwin' not declared |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 189 | "libbionic_spawn_benchmark", // ruperts@, cc_library_static, depends on libbase, libgoogle-benchmark |
| 190 | "libc_malloc_debug", // ruperts@, cc_library_static, depends on libbase |
| 191 | "libc_malloc_debug_backtrace", // ruperts@, cc_library_static, depends on libbase |
Liz Kammer | a3b5dcf | 2021-04-27 15:58:10 -0400 | [diff] [blame] | 192 | "libcutils", // eakammer@, cc_library, depends on libbase, liblog |
| 193 | "libcutils_sockets", // eakammer@, cc_library, depends on libbase, liblog |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 194 | "liblinker_debuggerd_stub", // ruperts@, cc_library_static, depends on libbase, libz, libziparchive |
| 195 | "liblinker_main", // ruperts@, cc_library_static, depends on libbase, libz, libziparchive |
| 196 | "liblinker_malloc", // ruperts@, cc_library_static, depends on libziparchive, libz, libbase |
| 197 | |
| 198 | // Requires non-libc targets, but otherwise works |
| 199 | "libc_jemalloc_wrapper", // ruperts@, cc_library_static, depends on //external/jemalloc_new |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 200 | |
| 201 | // Compilation error, seems to be fixable by changing the toolchain definition |
| 202 | "libc_bionic_ndk", // ruperts@, cc_library_static, error: ISO C++ requires field designators... |
| 203 | "libc_tzcode", // ruperts@, cc_library_static, error: expected expression |
| 204 | "libm", // jingwen@, cc_library, error: "expected register here" (and many others) |
| 205 | |
| 206 | // Linker error |
| 207 | "libc_malloc_hooks", // jingwen@, cc_library, undefined symbol: __malloc_hook, etc. |
Rupert Shuttleworth | 6036497 | 2021-04-27 11:03:39 -0400 | [diff] [blame] | 208 | "libdl", // jingwen@, cc_library, clang failed |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 209 | "libstdc++", // jingwen@, cc_library, undefined symbol: free |
| 210 | |
| 211 | // Includes not found |
| 212 | "libbionic_tests_headers_posix", // ruperts@, cc_library_static, 'dirent.h' not found |
Liz Kammer | a3b5dcf | 2021-04-27 15:58:10 -0400 | [diff] [blame] | 213 | "liblog", // eakammer@, cc_library, 'sys/cdefs.h' file not found, missing -isystem bionic/libc/include through the libc/libm/libdl default dependencies if system_shared_libs unset |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 214 | "libseccomp_policy", // jingwen@, cc_library, 'linux/filter.h' not found, missing -isystem bionic/libc/kernel/uapi/asm-arm, probably due to us not handling arch { ... { export_system_include_dirs } } correctly |
| 215 | "note_memtag_heap_async", // lberki@, cc_library_static, error: feature.h not found, missing -isystem bionic/libc/include through the libc/libm/libdl default dependencies if system_shared_libs unset |
| 216 | "note_memtag_heap_sync", // lberki@, cc_library_static, error: feature.h not found, missing -isystem bionic/libc/include through the libc/libm/libdl default dependencies if system_shared_libs unset |
| 217 | |
| 218 | // Other |
| 219 | "libBionicBenchmarksUtils", // ruperts@, cc_library_static, 'map' file not found |
Lukacs T. Berki | 667bb27 | 2021-04-28 11:54:46 +0200 | [diff] [blame] | 220 | "libc_ndk", // ruperts@, cc_library_static, depends on libc_bionic_ndk, libc_jemalloc_wrapper, libc_tzcode, libstdc++ |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 221 | |
| 222 | "libc", // jingwen@, cc_library, depends on //external/gwp_asan |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 223 | } |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame] | 224 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 225 | // Per-module denylist to opt modules out of mixed builds. Such modules will |
| 226 | // still be generated via bp2build. |
| 227 | mixedBuildsDisabledList = []string{ |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame^] | 228 | "libc_gdtoa", // ruperts@, cc_library_static, OK for bp2build but undefined symbol: __strtorQ for mixed builds |
| 229 | "libc_netbsd", // lberki@, cc_library_static, version script assignment of 'LIBC_PRIVATE' to symbol 'SHA1Final' failed: symbol not defined |
| 230 | "libc_openbsd", // ruperts@, cc_library_static, OK for bp2build but error: duplicate symbol: strcpy for mixed builds |
| 231 | "libsystemproperties", // cparsons@, cc_library_static, wrong include paths |
| 232 | "libpropertyinfoparser", // cparsons@, cc_library_static, wrong include paths |
| 233 | "libarm-optimized-routines-string", // jingwen@, cc_library_static, OK for bp2build but b/186615213 (asflags not handled in bp2build), version script assignment of 'LIBC' to symbol 'memcmp' failed: symbol not defined (also for memrchr, strnlen) |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 234 | } |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 235 | |
| 236 | // Used for quicker lookups |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 237 | bp2buildDoNotWriteBuildFile = map[string]bool{} |
| 238 | bp2buildModuleDoNotConvert = map[string]bool{} |
| 239 | mixedBuildsDisabled = map[string]bool{} |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 240 | ) |
| 241 | |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 242 | func init() { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 243 | for _, moduleName := range bp2buildDoNotWriteBuildFileList { |
| 244 | bp2buildDoNotWriteBuildFile[moduleName] = true |
| 245 | } |
| 246 | |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 247 | for _, moduleName := range bp2buildModuleDoNotConvertList { |
| 248 | bp2buildModuleDoNotConvert[moduleName] = true |
| 249 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 250 | |
| 251 | for _, moduleName := range mixedBuildsDisabledList { |
| 252 | mixedBuildsDisabled[moduleName] = true |
| 253 | } |
| 254 | } |
| 255 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 256 | func ShouldWriteBuildFileForDir(dir string) bool { |
| 257 | if _, ok := bp2buildDoNotWriteBuildFile[dir]; ok { |
| 258 | return false |
| 259 | } else { |
| 260 | return true |
| 261 | } |
| 262 | } |
| 263 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 264 | // MixedBuildsEnabled checks that a module is ready to be replaced by a |
| 265 | // converted or handcrafted Bazel target. |
| 266 | func (b *BazelModuleBase) MixedBuildsEnabled(ctx BazelConversionPathContext) bool { |
| 267 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 268 | return false |
| 269 | } |
| 270 | if len(b.GetBazelLabel(ctx, ctx.Module())) == 0 { |
| 271 | return false |
| 272 | } |
| 273 | return !mixedBuildsDisabled[ctx.Module().Name()] |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 276 | // ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 277 | func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool { |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 278 | if bp2buildModuleDoNotConvert[ctx.Module().Name()] { |
| 279 | return false |
| 280 | } |
| 281 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 282 | // Ensure that the module type of this module has a bp2build converter. This |
| 283 | // prevents mixed builds from using auto-converted modules just by matching |
| 284 | // the package dir; it also has to have a bp2build mutator as well. |
| 285 | if ctx.Config().bp2buildModuleTypeConfig[ctx.ModuleType()] == false { |
| 286 | return false |
| 287 | } |
| 288 | |
| 289 | packagePath := ctx.ModuleDir() |
| 290 | config := ctx.Config().bp2buildPackageConfig |
| 291 | |
| 292 | // This is a tristate value: true, false, or unset. |
| 293 | propValue := b.bazelProperties.Bazel_module.Bp2build_available |
| 294 | if bp2buildDefaultTrueRecursively(packagePath, config) { |
| 295 | // Allow modules to explicitly opt-out. |
| 296 | return proptools.BoolDefault(propValue, true) |
| 297 | } |
| 298 | |
| 299 | // Allow modules to explicitly opt-in. |
| 300 | return proptools.BoolDefault(propValue, false) |
| 301 | } |
| 302 | |
| 303 | // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the |
| 304 | // set of package prefixes where all modules must be converted. That is, if the |
| 305 | // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will |
| 306 | // return true. |
| 307 | // |
| 308 | // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry |
| 309 | // exactly, this module will return false early. |
| 310 | // |
| 311 | // This function will also return false if the package doesn't match anything in |
| 312 | // the config. |
| 313 | func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool { |
| 314 | ret := false |
| 315 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 316 | // Return exact matches in the config. |
| 317 | if config[packagePath] == Bp2BuildDefaultTrueRecursively { |
| 318 | return true |
| 319 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 320 | if config[packagePath] == Bp2BuildDefaultFalse { |
| 321 | return false |
| 322 | } |
| 323 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 324 | // If not, check for the config recursively. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 325 | packagePrefix := "" |
| 326 | // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist. |
| 327 | for _, part := range strings.Split(packagePath, "/") { |
| 328 | packagePrefix += part |
| 329 | if config[packagePrefix] == Bp2BuildDefaultTrueRecursively { |
| 330 | // package contains this prefix and this prefix should convert all modules |
| 331 | return true |
| 332 | } |
| 333 | // Continue to the next part of the package dir. |
| 334 | packagePrefix += "/" |
| 335 | } |
| 336 | |
| 337 | return ret |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 338 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 339 | |
| 340 | // GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or |
| 341 | // an error if there are errors reading the file. |
| 342 | // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do |
| 343 | // something more targeted based on the rule type and target. |
| 344 | func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 345 | if !strings.Contains(b.HandcraftedLabel(), path) { |
| 346 | return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel()) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 347 | } |
| 348 | name = filepath.Join(path, name) |
| 349 | f, err := c.fs.Open(name) |
| 350 | if err != nil { |
| 351 | return "", err |
| 352 | } |
| 353 | defer f.Close() |
| 354 | |
| 355 | data, err := ioutil.ReadAll(f) |
| 356 | if err != nil { |
| 357 | return "", err |
| 358 | } |
| 359 | return string(data[:]), nil |
| 360 | } |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 361 | |
| 362 | // ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically |
| 363 | // or manually |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 364 | func (b *BazelModuleBase) ConvertedToBazel(ctx BazelConversionPathContext) bool { |
| 365 | return b.ConvertWithBp2build(ctx) || b.HasHandcraftedLabel() |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 366 | } |