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 | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 129 | // Keep any existing BUILD files (and do not generate new BUILD files) for these directories |
| 130 | bp2buildKeepExistingBuildFile = map[string]bool{ |
| 131 | // This is actually build/bazel/build.BAZEL symlinked to ./BUILD |
| 132 | ".":/*recrusive = */ false, |
| 133 | |
| 134 | "build/bazel":/* recursive = */ true, |
| 135 | "build/pesto":/* recursive = */ true, |
| 136 | |
| 137 | // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails |
| 138 | // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed |
| 139 | "external/bazelbuild-rules_android":/* recursive = */ true, |
| 140 | |
| 141 | "prebuilts/clang/host/linux-x86":/* recursive = */ false, |
| 142 | "prebuilts/sdk":/* recursive = */ false, |
| 143 | "prebuilts/sdk/tools":/* recursive = */ false, |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 146 | // Configure modules in these directories to enable bp2build_available: true or false by default. |
| 147 | bp2buildDefaultConfig = Bp2BuildConfig{ |
| 148 | "bionic": Bp2BuildDefaultTrueRecursively, |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame] | 149 | "external/gwp_asan": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 150 | "system/core/libcutils": Bp2BuildDefaultTrueRecursively, |
Lukacs T. Berki | 497f17d | 2021-04-26 12:15:57 +0200 | [diff] [blame] | 151 | "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 152 | "system/libbase": Bp2BuildDefaultTrueRecursively, |
| 153 | "system/logging/liblog": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 154 | "external/jemalloc_new": Bp2BuildDefaultTrueRecursively, |
| 155 | "external/fmtlib": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 156 | "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 75be1ca | 2021-05-12 05:04:58 +0000 | [diff] [blame] | 157 | "external/scudo": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 158 | } |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 159 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 160 | // 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] | 161 | bp2buildModuleDoNotConvertList = []string{ |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 162 | // Things that transitively depend on unconverted libc_* modules. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 163 | "libc_nomalloc", // http://b/186825031, cc_library_static, depends on //bionic/libc:libc_common (http://b/186821517) |
Jingwen Chen | 0a92ed7 | 2021-04-12 05:37:42 +0000 | [diff] [blame] | 164 | |
Rupert Shuttleworth | 47aa584 | 2021-04-30 04:04:15 -0400 | [diff] [blame] | 165 | "libbionic_spawn_benchmark", // http://b/186824595, cc_library_static, depends on //external/google-benchmark (http://b/186822740) |
| 166 | // also depends on //system/logging/liblog:liblog (http://b/186822772) |
| 167 | |
| 168 | "libc_malloc_debug", // http://b/186824339, cc_library_static, depends on //system/libbase:libbase (http://b/186823646) |
| 169 | "libc_malloc_debug_backtrace", // http://b/186824112, cc_library_static, depends on //external/libcxxabi:libc++demangle (http://b/186823773) |
| 170 | |
| 171 | "libcutils", // http://b/186827426, cc_library, depends on //system/core/libprocessgroup:libprocessgroup_headers (http://b/186826841) |
| 172 | "libcutils_sockets", // http://b/186826853, cc_library, depends on //system/libbase:libbase (http://b/186826479) |
| 173 | |
| 174 | "liblinker_debuggerd_stub", // http://b/186824327, cc_library_static, depends on //external/zlib:libz (http://b/186823782) |
| 175 | // also depends on //system/libziparchive:libziparchive (http://b/186823656) |
| 176 | // also depends on //system/logging/liblog:liblog (http://b/186822772) |
| 177 | "liblinker_main", // http://b/186825989, cc_library_static, depends on //external/zlib:libz (http://b/186823782) |
| 178 | // also depends on //system/libziparchive:libziparchive (http://b/186823656) |
| 179 | // also depends on//system/logging/liblog:liblog (http://b/186822772) |
| 180 | "liblinker_malloc", // http://b/186826466, cc_library_static, depends on //external/zlib:libz (http://b/186823782) |
| 181 | // also depends on //system/libziparchive:libziparchive (http://b/186823656) |
| 182 | // also depends on //system/logging/liblog:liblog (http://b/186822772) |
Rupert Shuttleworth | fb95538 | 2021-05-03 04:32:36 -0400 | [diff] [blame] | 183 | "libc_jemalloc_wrapper", // http://b/187012490, cc_library_static, depends on //external/jemalloc_new:libjemalloc5 (http://b/186828626) |
| 184 | "libc_ndk", // http://b/187013218, cc_library_static, depends on //bionic/libm:libm (http://b/183064661) |
| 185 | "libc", // http://b/183064430, cc_library, depends on //external/jemalloc_new:libjemalloc5 (http://b/186828626) |
Rupert Shuttleworth | fb95538 | 2021-05-03 04:32:36 -0400 | [diff] [blame] | 186 | "libc_malloc_hooks", // http://b/187016307, cc_library, ld.lld: error: undefined symbol: __malloc_hook |
Rupert Shuttleworth | fb95538 | 2021-05-03 04:32:36 -0400 | [diff] [blame] | 187 | "libm", // http://b/183064661, cc_library, math.h:25:16: error: unexpected token in argument list |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 188 | |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 189 | // http://b/186823769: Needs C++ STL support, includes from unconverted standard libraries in //external/libcxx |
| 190 | // c++_static |
Rupert Shuttleworth | fb95538 | 2021-05-03 04:32:36 -0400 | [diff] [blame] | 191 | "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 Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 192 | // libcxx |
| 193 | "libBionicBenchmarksUtils", // cc_library_static, fatal error: 'map' file not found, from libcxx |
| 194 | "fmtlib", // cc_library_static, fatal error: 'cassert' file not found, from libcxx |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 195 | "fmtlib_ndk", // cc_library_static, fatal error: 'cassert' file not found |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 196 | "libbase", // http://b/186826479, cc_library, fatal error: 'memory' file not found, from libcxx |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 197 | |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 198 | // http://b/186024507: Includes errors because of the system_shared_libs default value. |
| 199 | // Missing -isystem bionic/libc/include through the libc/libm/libdl |
| 200 | // default dependencies if system_shared_libs is unset. |
| 201 | "liblog", // http://b/186822772: cc_library, 'sys/cdefs.h' file not found |
| 202 | "libjemalloc5_jet", // cc_library, 'sys/cdefs.h' file not found |
| 203 | "libseccomp_policy", // http://b/186476753: cc_library, 'linux/filter.h' not found |
| 204 | "note_memtag_heap_async", // http://b/185127353: cc_library_static, error: feature.h not found |
| 205 | "note_memtag_heap_sync", // http://b/185127353: cc_library_static, error: feature.h not found |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 206 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 207 | "libjemalloc5", // cc_library, ld.lld: error: undefined symbol: memset |
| 208 | "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset |
| 209 | |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 210 | // Tests. Handle later. |
| 211 | "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found |
| 212 | "libjemalloc5_integrationtest", |
| 213 | "libjemalloc5_stresstestlib", |
| 214 | "libjemalloc5_unittest", |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 215 | } |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame] | 216 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 217 | // Per-module denylist of cc_library modules to only generate the static |
| 218 | // variant if their shared variant isn't ready or buildable by Bazel. |
| 219 | bp2buildCcLibraryStaticOnlyList = []string{ |
| 220 | "libstdc++", // http://b/186822597, cc_library, ld.lld: error: undefined symbol: __errno |
| 221 | } |
| 222 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 223 | // Per-module denylist to opt modules out of mixed builds. Such modules will |
| 224 | // still be generated via bp2build. |
| 225 | mixedBuildsDisabledList = []string{ |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 226 | "libc_bionic_ndk", // cparsons@, http://b/183213331, Handle generated headers in mixed builds. |
| 227 | "libc_common", // cparsons@ cc_library_static, depends on //bionic/libc:libc_nopthread |
| 228 | "libc_common_static", // cparsons@ cc_library_static, depends on //bionic/libc:libc_common |
| 229 | "libc_common_shared", // cparsons@ cc_library_static, depends on //bionic/libc:libc_common |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 230 | "libc_netbsd", // lberki@, cc_library_static, version script assignment of 'LIBC_PRIVATE' to symbol 'SHA1Final' failed: symbol not defined |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 231 | "libc_nopthread", // cparsons@ cc_library_static, depends on //bionic/libc:libc_bionic_ndk |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 232 | "libc_openbsd", // ruperts@, cc_library_static, OK for bp2build but error: duplicate symbol: strcpy for mixed builds |
| 233 | "libsystemproperties", // cparsons@, cc_library_static, wrong include paths |
| 234 | "libpropertyinfoparser", // cparsons@, cc_library_static, wrong include paths |
| 235 | "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) |
Rupert Shuttleworth | 52e6672 | 2021-05-03 09:59:38 -0400 | [diff] [blame] | 236 | "fmtlib_ndk", // http://b/187040371, cc_library_static, OK for bp2build but format-inl.h:11:10: fatal error: 'cassert' file not found for mixed builds |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 237 | } |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 238 | |
| 239 | // Used for quicker lookups |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 240 | bp2buildModuleDoNotConvert = map[string]bool{} |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 241 | bp2buildCcLibraryStaticOnly = map[string]bool{} |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 242 | mixedBuildsDisabled = map[string]bool{} |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 243 | ) |
| 244 | |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 245 | func init() { |
| 246 | for _, moduleName := range bp2buildModuleDoNotConvertList { |
| 247 | bp2buildModuleDoNotConvert[moduleName] = true |
| 248 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 249 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 250 | for _, moduleName := range bp2buildCcLibraryStaticOnlyList { |
| 251 | bp2buildCcLibraryStaticOnly[moduleName] = true |
| 252 | } |
| 253 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 254 | for _, moduleName := range mixedBuildsDisabledList { |
| 255 | mixedBuildsDisabled[moduleName] = true |
| 256 | } |
| 257 | } |
| 258 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 259 | func GenerateCcLibraryStaticOnly(ctx BazelConversionPathContext) bool { |
| 260 | return bp2buildCcLibraryStaticOnly[ctx.Module().Name()] |
| 261 | } |
| 262 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 263 | func ShouldKeepExistingBuildFileForDir(dir string) bool { |
| 264 | if _, ok := bp2buildKeepExistingBuildFile[dir]; ok { |
| 265 | // Exact dir match |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 266 | return true |
| 267 | } |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 268 | // Check if subtree match |
| 269 | for prefix, recursive := range bp2buildKeepExistingBuildFile { |
| 270 | if recursive { |
| 271 | if strings.HasPrefix(dir, prefix+"/") { |
| 272 | return true |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | // Default |
| 277 | return false |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 280 | // MixedBuildsEnabled checks that a module is ready to be replaced by a |
| 281 | // converted or handcrafted Bazel target. |
| 282 | func (b *BazelModuleBase) MixedBuildsEnabled(ctx BazelConversionPathContext) bool { |
| 283 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 284 | return false |
| 285 | } |
| 286 | if len(b.GetBazelLabel(ctx, ctx.Module())) == 0 { |
| 287 | return false |
| 288 | } |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 289 | if GenerateCcLibraryStaticOnly(ctx) { |
| 290 | // Don't use partially-converted cc_library targets in mixed builds, |
| 291 | // since mixed builds would generally rely on both static and shared |
| 292 | // variants of a cc_library. |
| 293 | return false |
| 294 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 295 | return !mixedBuildsDisabled[ctx.Module().Name()] |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 298 | // ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 299 | func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool { |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 300 | if bp2buildModuleDoNotConvert[ctx.Module().Name()] { |
| 301 | return false |
| 302 | } |
| 303 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 304 | // Ensure that the module type of this module has a bp2build converter. This |
| 305 | // prevents mixed builds from using auto-converted modules just by matching |
| 306 | // the package dir; it also has to have a bp2build mutator as well. |
| 307 | if ctx.Config().bp2buildModuleTypeConfig[ctx.ModuleType()] == false { |
| 308 | return false |
| 309 | } |
| 310 | |
| 311 | packagePath := ctx.ModuleDir() |
| 312 | config := ctx.Config().bp2buildPackageConfig |
| 313 | |
| 314 | // This is a tristate value: true, false, or unset. |
| 315 | propValue := b.bazelProperties.Bazel_module.Bp2build_available |
| 316 | if bp2buildDefaultTrueRecursively(packagePath, config) { |
| 317 | // Allow modules to explicitly opt-out. |
| 318 | return proptools.BoolDefault(propValue, true) |
| 319 | } |
| 320 | |
| 321 | // Allow modules to explicitly opt-in. |
| 322 | return proptools.BoolDefault(propValue, false) |
| 323 | } |
| 324 | |
| 325 | // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the |
| 326 | // set of package prefixes where all modules must be converted. That is, if the |
| 327 | // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will |
| 328 | // return true. |
| 329 | // |
| 330 | // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry |
| 331 | // exactly, this module will return false early. |
| 332 | // |
| 333 | // This function will also return false if the package doesn't match anything in |
| 334 | // the config. |
| 335 | func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool { |
| 336 | ret := false |
| 337 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 338 | // Return exact matches in the config. |
| 339 | if config[packagePath] == Bp2BuildDefaultTrueRecursively { |
| 340 | return true |
| 341 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 342 | if config[packagePath] == Bp2BuildDefaultFalse { |
| 343 | return false |
| 344 | } |
| 345 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 346 | // If not, check for the config recursively. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 347 | packagePrefix := "" |
| 348 | // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist. |
| 349 | for _, part := range strings.Split(packagePath, "/") { |
| 350 | packagePrefix += part |
| 351 | if config[packagePrefix] == Bp2BuildDefaultTrueRecursively { |
| 352 | // package contains this prefix and this prefix should convert all modules |
| 353 | return true |
| 354 | } |
| 355 | // Continue to the next part of the package dir. |
| 356 | packagePrefix += "/" |
| 357 | } |
| 358 | |
| 359 | return ret |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 360 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 361 | |
| 362 | // GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or |
| 363 | // an error if there are errors reading the file. |
| 364 | // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do |
| 365 | // something more targeted based on the rule type and target. |
| 366 | func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 367 | if !strings.Contains(b.HandcraftedLabel(), path) { |
| 368 | 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] | 369 | } |
| 370 | name = filepath.Join(path, name) |
| 371 | f, err := c.fs.Open(name) |
| 372 | if err != nil { |
| 373 | return "", err |
| 374 | } |
| 375 | defer f.Close() |
| 376 | |
| 377 | data, err := ioutil.ReadAll(f) |
| 378 | if err != nil { |
| 379 | return "", err |
| 380 | } |
| 381 | return string(data[:]), nil |
| 382 | } |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 383 | |
| 384 | // ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically |
| 385 | // or manually |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 386 | func (b *BazelModuleBase) ConvertedToBazel(ctx BazelConversionPathContext) bool { |
| 387 | return b.ConvertWithBp2build(ctx) || b.HasHandcraftedLabel() |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 388 | } |