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 | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame^] | 64 | convertWithBp2build(ctx BazelConversionPathContext, module blueprint.Module) bool |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 65 | GetBazelBuildFileContents(c Config, path, name string) (string, error) |
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 | |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 123 | // all modules in this package (not recursively) default to bp2build_available: true. |
| 124 | // allows modules to opt-out. |
| 125 | Bp2BuildDefaultTrue |
| 126 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 127 | // all modules in this package (not recursively) default to bp2build_available: false. |
| 128 | // allows modules to opt-in. |
| 129 | Bp2BuildDefaultFalse |
| 130 | ) |
| 131 | |
| 132 | var ( |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 133 | // Keep any existing BUILD files (and do not generate new BUILD files) for these directories |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 134 | // in the synthetic Bazel workspace. |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 135 | bp2buildKeepExistingBuildFile = map[string]bool{ |
| 136 | // This is actually build/bazel/build.BAZEL symlinked to ./BUILD |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 137 | ".":/*recursive = */ false, |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 138 | |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 139 | // 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 Jobredeaux | 5cc9c9d | 2021-08-26 15:07:48 +0000 | [diff] [blame] | 144 | "build/bazel/examples/java":/* recursive = */ true, |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 145 | "build/bazel/bazel_skylib":/* recursive = */ true, |
| 146 | "build/bazel/rules":/* recursive = */ true, |
| 147 | "build/bazel/rules_cc":/* recursive = */ true, |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 148 | "build/bazel/scripts":/* recursive = */ true, |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 149 | "build/bazel/tests":/* recursive = */ true, |
| 150 | "build/bazel/platforms":/* recursive = */ true, |
| 151 | "build/bazel/product_variables":/* recursive = */ true, |
Jingwen Chen | 5e49b82 | 2021-08-24 05:14:22 +0000 | [diff] [blame] | 152 | "build/bazel_common_rules":/* recursive = */ true, |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 153 | "build/pesto":/* recursive = */ true, |
| 154 | |
| 155 | // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails |
| 156 | // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed |
| 157 | "external/bazelbuild-rules_android":/* recursive = */ true, |
Jingwen Chen | 9163225 | 2021-08-10 13:00:33 +0000 | [diff] [blame] | 158 | "external/bazel-skylib":/* recursive = */ true, |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 159 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 160 | "prebuilts/sdk":/* recursive = */ false, |
| 161 | "prebuilts/sdk/tools":/* recursive = */ false, |
Romain Jobredeaux | 8c98c25 | 2021-08-02 17:27:06 +0000 | [diff] [blame] | 162 | "prebuilts/r8":/* recursive = */ false, |
Romain Jobredeaux | f1b0ac8 | 2021-08-12 14:39:00 +0000 | [diff] [blame] | 163 | "packages/apps/Music":/* recursive = */ true, |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 166 | // Configure modules in these directories to enable bp2build_available: true or false by default. |
| 167 | bp2buildDefaultConfig = Bp2BuildConfig{ |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 168 | "bionic": Bp2BuildDefaultTrueRecursively, |
| 169 | "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | a26246d | 2021-08-31 05:26:41 +0000 | [diff] [blame] | 170 | "development/sdk": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 171 | "external/gwp_asan": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 172 | "external/brotli": Bp2BuildDefaultTrue, |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 173 | "system/core/libcutils": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 174 | "system/core/libprocessgroup": Bp2BuildDefaultTrue, |
Lukacs T. Berki | 497f17d | 2021-04-26 12:15:57 +0200 | [diff] [blame] | 175 | "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 176 | "system/libbase": Bp2BuildDefaultTrueRecursively, |
| 177 | "system/logging/liblog": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 739d01e | 2021-07-28 15:26:20 +0000 | [diff] [blame] | 178 | "system/timezone/apex": Bp2BuildDefaultTrueRecursively, |
| 179 | "system/timezone/output_data": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 180 | "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 181 | "external/fmtlib": Bp2BuildDefaultTrueRecursively, |
| 182 | "external/jemalloc_new": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 183 | "external/libcxx": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 184 | "external/libcxxabi": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 185 | "external/libcap": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 75be1ca | 2021-05-12 05:04:58 +0000 | [diff] [blame] | 186 | "external/scudo": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 187 | "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 188 | } |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 189 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 190 | // 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] | 191 | bp2buildModuleDoNotConvertList = []string{ |
Jingwen Chen | 2828816 | 2021-04-28 07:19:54 +0000 | [diff] [blame] | 192 | // Things that transitively depend on unconverted libc_* modules. |
Rupert Shuttleworth | 47aa584 | 2021-04-30 04:04:15 -0400 | [diff] [blame] | 193 | "libbionic_spawn_benchmark", // http://b/186824595, cc_library_static, depends on //external/google-benchmark (http://b/186822740) |
| 194 | // also depends on //system/logging/liblog:liblog (http://b/186822772) |
| 195 | |
| 196 | "libc_malloc_debug", // http://b/186824339, cc_library_static, depends on //system/libbase:libbase (http://b/186823646) |
| 197 | "libc_malloc_debug_backtrace", // http://b/186824112, cc_library_static, depends on //external/libcxxabi:libc++demangle (http://b/186823773) |
| 198 | |
| 199 | "libcutils", // http://b/186827426, cc_library, depends on //system/core/libprocessgroup:libprocessgroup_headers (http://b/186826841) |
| 200 | "libcutils_sockets", // http://b/186826853, cc_library, depends on //system/libbase:libbase (http://b/186826479) |
| 201 | |
| 202 | "liblinker_debuggerd_stub", // http://b/186824327, cc_library_static, depends on //external/zlib:libz (http://b/186823782) |
| 203 | // also depends on //system/libziparchive:libziparchive (http://b/186823656) |
| 204 | // also depends on //system/logging/liblog:liblog (http://b/186822772) |
| 205 | "liblinker_main", // http://b/186825989, cc_library_static, depends on //external/zlib:libz (http://b/186823782) |
| 206 | // also depends on //system/libziparchive:libziparchive (http://b/186823656) |
| 207 | // also depends on//system/logging/liblog:liblog (http://b/186822772) |
| 208 | "liblinker_malloc", // http://b/186826466, cc_library_static, depends on //external/zlib:libz (http://b/186823782) |
| 209 | // also depends on //system/libziparchive:libziparchive (http://b/186823656) |
| 210 | // also depends on //system/logging/liblog:liblog (http://b/186822772) |
Jingwen Chen | 5354292 | 2021-05-18 09:01:49 +0000 | [diff] [blame] | 211 | "libc_ndk", // http://b/187013218, cc_library_static, depends on //bionic/libm:libm (http://b/183064661) |
Jingwen Chen | 5354292 | 2021-05-18 09:01:49 +0000 | [diff] [blame] | 212 | "libc_malloc_hooks", // http://b/187016307, cc_library, ld.lld: error: undefined symbol: __malloc_hook |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 213 | |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 214 | // http://b/186823769: Needs C++ STL support, includes from unconverted standard libraries in //external/libcxx |
| 215 | // c++_static |
Rupert Shuttleworth | fb95538 | 2021-05-03 04:32:36 -0400 | [diff] [blame] | 216 | "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] | 217 | // libcxx |
| 218 | "libBionicBenchmarksUtils", // cc_library_static, fatal error: 'map' file not found, from libcxx |
| 219 | "fmtlib", // cc_library_static, fatal error: 'cassert' file not found, from libcxx |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 220 | "fmtlib_ndk", // cc_library_static, fatal error: 'cassert' file not found |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 221 | "liblog", // http://b/186822772: cc_library, 'sys/cdefs.h' file not found |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 222 | "libbase", // Requires liblog. http://b/186826479, cc_library, fatal error: 'memory' file not found, from libcxx. |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 223 | // Also depends on fmtlib. |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 224 | |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 225 | "libseccomp_policy", // depends on libbase |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 226 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 227 | "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset |
| 228 | |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 229 | //system/core/libprocessgroup/... |
| 230 | "libprocessgroup", // depends on //system/core/libprocessgroup/cgrouprc:libcgrouprc |
| 231 | |
| 232 | //external/brotli/... |
| 233 | "brotli-fuzzer-corpus", // "declared output 'external/brotli/c/fuzz/73231c6592f195ffd41100b8706d1138ff6893b9' was not created by genrule" |
| 234 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 235 | // //external/libcap/... |
| 236 | "libcap", // http://b/198595332, depends on _makenames, a cc_binary |
| 237 | "cap_names.h", // http://b/198596102, depends on _makenames, a cc_binary |
| 238 | |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 239 | // Tests. Handle later. |
| 240 | "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found |
| 241 | "libjemalloc5_integrationtest", |
| 242 | "libjemalloc5_stresstestlib", |
| 243 | "libjemalloc5_unittest", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 244 | |
| 245 | // APEX support |
| 246 | "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug' and 'libc_malloc_hooks' |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 247 | } |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame] | 248 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 249 | // Per-module denylist of cc_library modules to only generate the static |
| 250 | // variant if their shared variant isn't ready or buildable by Bazel. |
| 251 | bp2buildCcLibraryStaticOnlyList = []string{ |
Jingwen Chen | 5354292 | 2021-05-18 09:01:49 +0000 | [diff] [blame] | 252 | "libstdc++", // http://b/186822597, cc_library, ld.lld: error: undefined symbol: __errno |
| 253 | "libjemalloc5", // http://b/188503688, cc_library, `target: { android: { enabled: false } }` for android targets. |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 256 | // Per-module denylist to opt modules out of mixed builds. Such modules will |
| 257 | // still be generated via bp2build. |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 258 | mixedBuildsDisabledList = []string{ |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 259 | "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 |
| 260 | "libc++fs", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes |
| 261 | "libc++_experimental", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes |
| 262 | "libc++_static", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes |
| 263 | "libc++abi", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects. |
| 264 | "libc++demangle", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects. |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 265 | } |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 266 | |
| 267 | // Used for quicker lookups |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 268 | bp2buildModuleDoNotConvert = map[string]bool{} |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 269 | bp2buildCcLibraryStaticOnly = map[string]bool{} |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 270 | mixedBuildsDisabled = map[string]bool{} |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 271 | ) |
| 272 | |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 273 | func init() { |
| 274 | for _, moduleName := range bp2buildModuleDoNotConvertList { |
| 275 | bp2buildModuleDoNotConvert[moduleName] = true |
| 276 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 277 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 278 | for _, moduleName := range bp2buildCcLibraryStaticOnlyList { |
| 279 | bp2buildCcLibraryStaticOnly[moduleName] = true |
| 280 | } |
| 281 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 282 | for _, moduleName := range mixedBuildsDisabledList { |
| 283 | mixedBuildsDisabled[moduleName] = true |
| 284 | } |
| 285 | } |
| 286 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 287 | func GenerateCcLibraryStaticOnly(ctx BazelConversionPathContext) bool { |
| 288 | return bp2buildCcLibraryStaticOnly[ctx.Module().Name()] |
| 289 | } |
| 290 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 291 | func ShouldKeepExistingBuildFileForDir(dir string) bool { |
| 292 | if _, ok := bp2buildKeepExistingBuildFile[dir]; ok { |
| 293 | // Exact dir match |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 294 | return true |
| 295 | } |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 296 | // Check if subtree match |
| 297 | for prefix, recursive := range bp2buildKeepExistingBuildFile { |
| 298 | if recursive { |
| 299 | if strings.HasPrefix(dir, prefix+"/") { |
| 300 | return true |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | // Default |
| 305 | return false |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 306 | } |
| 307 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 308 | // MixedBuildsEnabled checks that a module is ready to be replaced by a |
| 309 | // converted or handcrafted Bazel target. |
| 310 | func (b *BazelModuleBase) MixedBuildsEnabled(ctx BazelConversionPathContext) bool { |
| 311 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 312 | return false |
| 313 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame^] | 314 | if !convertedToBazel(ctx, ctx.Module()) { |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 315 | return false |
| 316 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame^] | 317 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 318 | if GenerateCcLibraryStaticOnly(ctx) { |
| 319 | // Don't use partially-converted cc_library targets in mixed builds, |
| 320 | // since mixed builds would generally rely on both static and shared |
| 321 | // variants of a cc_library. |
| 322 | return false |
| 323 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 324 | return !mixedBuildsDisabled[ctx.Module().Name()] |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame^] | 327 | // ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel. |
| 328 | func convertedToBazel(ctx BazelConversionPathContext, module blueprint.Module) bool { |
| 329 | b, ok := module.(Bazelable) |
| 330 | if !ok { |
| 331 | return false |
| 332 | } |
| 333 | return b.convertWithBp2build(ctx, module) || b.HasHandcraftedLabel() |
| 334 | } |
| 335 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 336 | // ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 337 | func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame^] | 338 | return b.convertWithBp2build(ctx, ctx.Module()) |
| 339 | } |
| 340 | |
| 341 | func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionPathContext, module blueprint.Module) bool { |
| 342 | if bp2buildModuleDoNotConvert[module.Name()] { |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 343 | return false |
| 344 | } |
| 345 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 346 | // Ensure that the module type of this module has a bp2build converter. This |
| 347 | // prevents mixed builds from using auto-converted modules just by matching |
| 348 | // the package dir; it also has to have a bp2build mutator as well. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame^] | 349 | if ctx.Config().bp2buildModuleTypeConfig[ctx.OtherModuleType(module)] == false { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 350 | return false |
| 351 | } |
| 352 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame^] | 353 | packagePath := ctx.OtherModuleDir(module) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 354 | config := ctx.Config().bp2buildPackageConfig |
| 355 | |
| 356 | // This is a tristate value: true, false, or unset. |
| 357 | propValue := b.bazelProperties.Bazel_module.Bp2build_available |
| 358 | if bp2buildDefaultTrueRecursively(packagePath, config) { |
| 359 | // Allow modules to explicitly opt-out. |
| 360 | return proptools.BoolDefault(propValue, true) |
| 361 | } |
| 362 | |
| 363 | // Allow modules to explicitly opt-in. |
| 364 | return proptools.BoolDefault(propValue, false) |
| 365 | } |
| 366 | |
| 367 | // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the |
| 368 | // set of package prefixes where all modules must be converted. That is, if the |
| 369 | // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will |
| 370 | // return true. |
| 371 | // |
| 372 | // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry |
| 373 | // exactly, this module will return false early. |
| 374 | // |
| 375 | // This function will also return false if the package doesn't match anything in |
| 376 | // the config. |
| 377 | func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool { |
| 378 | ret := false |
| 379 | |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 380 | // Check if the package path has an exact match in the config. |
| 381 | if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 382 | return true |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 383 | } else if config[packagePath] == Bp2BuildDefaultFalse { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 384 | return false |
| 385 | } |
| 386 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 387 | // If not, check for the config recursively. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 388 | packagePrefix := "" |
| 389 | // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist. |
| 390 | for _, part := range strings.Split(packagePath, "/") { |
| 391 | packagePrefix += part |
| 392 | if config[packagePrefix] == Bp2BuildDefaultTrueRecursively { |
| 393 | // package contains this prefix and this prefix should convert all modules |
| 394 | return true |
| 395 | } |
| 396 | // Continue to the next part of the package dir. |
| 397 | packagePrefix += "/" |
| 398 | } |
| 399 | |
| 400 | return ret |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 401 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 402 | |
| 403 | // GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or |
| 404 | // an error if there are errors reading the file. |
| 405 | // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do |
| 406 | // something more targeted based on the rule type and target. |
| 407 | func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 408 | if !strings.Contains(b.HandcraftedLabel(), path) { |
| 409 | 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] | 410 | } |
| 411 | name = filepath.Join(path, name) |
| 412 | f, err := c.fs.Open(name) |
| 413 | if err != nil { |
| 414 | return "", err |
| 415 | } |
| 416 | defer f.Close() |
| 417 | |
| 418 | data, err := ioutil.ReadAll(f) |
| 419 | if err != nil { |
| 420 | return "", err |
| 421 | } |
| 422 | return string(data[:]), nil |
| 423 | } |