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 ( |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 18 | "android/soong/bazel" |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 19 | "fmt" |
| 20 | "io/ioutil" |
| 21 | "path/filepath" |
| 22 | "strings" |
| 23 | |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
| 26 | ) |
| 27 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 28 | // Properties contains common module properties for Bazel migration purposes. |
| 29 | type properties struct { |
| 30 | // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing |
| 31 | // this Soong module. |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 32 | Bazel_module bazel.BazelModuleProperties |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 33 | } |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 34 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 35 | // namespacedVariableProperties is a map from a string representing a Soong |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame^] | 36 | // config variable namespace, like "android" or "vendor_name" to a slice of |
| 37 | // pointer to a struct containing a single field called Soong_config_variables |
| 38 | // whose value mirrors the structure in the Blueprint file. |
| 39 | type namespacedVariableProperties map[string][]interface{} |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 40 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 41 | // BazelModuleBase contains the property structs with metadata for modules which can be converted to |
| 42 | // Bazel. |
| 43 | type BazelModuleBase struct { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 44 | bazelProperties properties |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 45 | |
| 46 | // namespacedVariableProperties is used for soong_config_module_type support |
| 47 | // in bp2build. Soong config modules allow users to set module properties |
| 48 | // based on custom product variables defined in Android.bp files. These |
| 49 | // variables are namespaced to prevent clobbering, especially when set from |
| 50 | // Makefiles. |
| 51 | namespacedVariableProperties namespacedVariableProperties |
| 52 | |
| 53 | // baseModuleType is set when this module was created from a module type |
| 54 | // defined by a soong_config_module_type. Every soong_config_module_type |
| 55 | // "wraps" another module type, e.g. a soong_config_module_type can wrap a |
| 56 | // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary. |
| 57 | // This baseModuleType is set to the wrapped module type. |
| 58 | baseModuleType string |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Bazelable is specifies the interface for modules that can be converted to Bazel. |
| 62 | type Bazelable interface { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 63 | bazelProps() *properties |
| 64 | HasHandcraftedLabel() bool |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 65 | HandcraftedLabel() string |
| 66 | GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 67 | ConvertWithBp2build(ctx BazelConversionContext) bool |
| 68 | convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 69 | GetBazelBuildFileContents(c Config, path, name string) (string, error) |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 70 | |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame^] | 71 | // namespacedVariableProps is a map from a soong config variable namespace |
| 72 | // (e.g. acme, android) to a map of interfaces{}, which are really |
| 73 | // reflect.Struct pointers, representing the value of the |
| 74 | // soong_config_variables property of a module. The struct pointer is the |
| 75 | // one with the single member called Soong_config_variables, which itself is |
| 76 | // a struct containing fields for each supported feature in that namespace. |
| 77 | // |
| 78 | // The reason for using an slice of interface{} is to support defaults |
| 79 | // propagation of the struct pointers. |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 80 | namespacedVariableProps() namespacedVariableProperties |
| 81 | setNamespacedVariableProps(props namespacedVariableProperties) |
| 82 | BaseModuleType() string |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame^] | 83 | SetBaseModuleType(baseModuleType string) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules. |
| 87 | type BazelModule interface { |
| 88 | Module |
| 89 | Bazelable |
| 90 | } |
| 91 | |
| 92 | // InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion |
| 93 | // properties. |
| 94 | func InitBazelModule(module BazelModule) { |
| 95 | module.AddProperties(module.bazelProps()) |
| 96 | } |
| 97 | |
| 98 | // bazelProps returns the Bazel properties for the given BazelModuleBase. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 99 | func (b *BazelModuleBase) bazelProps() *properties { |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 100 | return &b.bazelProperties |
| 101 | } |
| 102 | |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 103 | func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties { |
| 104 | return b.namespacedVariableProperties |
| 105 | } |
| 106 | |
| 107 | func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) { |
| 108 | b.namespacedVariableProperties = props |
| 109 | } |
| 110 | |
| 111 | func (b *BazelModuleBase) BaseModuleType() string { |
| 112 | return b.baseModuleType |
| 113 | } |
| 114 | |
| 115 | func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) { |
| 116 | b.baseModuleType = baseModuleType |
| 117 | } |
| 118 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 119 | // HasHandcraftedLabel returns whether this module has a handcrafted Bazel label. |
| 120 | func (b *BazelModuleBase) HasHandcraftedLabel() bool { |
| 121 | return b.bazelProperties.Bazel_module.Label != nil |
| 122 | } |
| 123 | |
| 124 | // HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none |
| 125 | func (b *BazelModuleBase) HandcraftedLabel() string { |
| 126 | return proptools.String(b.bazelProperties.Bazel_module.Label) |
| 127 | } |
| 128 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 129 | // GetBazelLabel returns the Bazel label for the given BazelModuleBase. |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 130 | func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string { |
| 131 | if b.HasHandcraftedLabel() { |
| 132 | return b.HandcraftedLabel() |
| 133 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 134 | if b.ConvertWithBp2build(ctx) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 135 | return bp2buildModuleLabel(ctx, module) |
| 136 | } |
| 137 | return "" // no label for unconverted module |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 140 | // Configuration to decide if modules in a directory should default to true/false for bp2build_available |
| 141 | type Bp2BuildConfig map[string]BazelConversionConfigEntry |
| 142 | type BazelConversionConfigEntry int |
| 143 | |
| 144 | const ( |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 145 | // A sentinel value to be used as a key in Bp2BuildConfig for modules with |
| 146 | // no package path. This is also the module dir for top level Android.bp |
| 147 | // modules. |
| 148 | BP2BUILD_TOPLEVEL = "." |
| 149 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 150 | // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map, |
| 151 | // which can also mean that the key doesn't exist in a lookup. |
| 152 | |
| 153 | // all modules in this package and subpackages default to bp2build_available: true. |
| 154 | // allows modules to opt-out. |
| 155 | Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1 |
| 156 | |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 157 | // all modules in this package (not recursively) default to bp2build_available: true. |
| 158 | // allows modules to opt-out. |
| 159 | Bp2BuildDefaultTrue |
| 160 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 161 | // all modules in this package (not recursively) default to bp2build_available: false. |
| 162 | // allows modules to opt-in. |
| 163 | Bp2BuildDefaultFalse |
| 164 | ) |
| 165 | |
| 166 | var ( |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 167 | // 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] | 168 | // in the synthetic Bazel workspace. |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 169 | bp2buildKeepExistingBuildFile = map[string]bool{ |
| 170 | // This is actually build/bazel/build.BAZEL symlinked to ./BUILD |
Jingwen Chen | f59a8e1 | 2021-07-16 09:28:53 +0000 | [diff] [blame] | 171 | ".":/*recursive = */ false, |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 172 | |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 173 | // build/bazel/examples/apex/... BUILD files should be generated, so |
| 174 | // build/bazel is not recursive. Instead list each subdirectory under |
| 175 | // build/bazel explicitly. |
| 176 | "build/bazel":/* recursive = */ false, |
| 177 | "build/bazel/examples/android_app":/* recursive = */ true, |
Romain Jobredeaux | 5cc9c9d | 2021-08-26 15:07:48 +0000 | [diff] [blame] | 178 | "build/bazel/examples/java":/* recursive = */ true, |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 179 | "build/bazel/bazel_skylib":/* recursive = */ true, |
| 180 | "build/bazel/rules":/* recursive = */ true, |
| 181 | "build/bazel/rules_cc":/* recursive = */ true, |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 182 | "build/bazel/scripts":/* recursive = */ true, |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 183 | "build/bazel/tests":/* recursive = */ true, |
| 184 | "build/bazel/platforms":/* recursive = */ true, |
| 185 | "build/bazel/product_variables":/* recursive = */ true, |
Jingwen Chen | 5e49b82 | 2021-08-24 05:14:22 +0000 | [diff] [blame] | 186 | "build/bazel_common_rules":/* recursive = */ true, |
Romain Jobredeaux | 9e09bba | 2021-09-08 18:09:02 +0000 | [diff] [blame] | 187 | "build/make/tools":/* recursive = */ true, |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 188 | "build/pesto":/* recursive = */ true, |
| 189 | |
| 190 | // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails |
| 191 | // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed |
| 192 | "external/bazelbuild-rules_android":/* recursive = */ true, |
Jingwen Chen | 9163225 | 2021-08-10 13:00:33 +0000 | [diff] [blame] | 193 | "external/bazel-skylib":/* recursive = */ true, |
Romain Jobredeaux | 9e09bba | 2021-09-08 18:09:02 +0000 | [diff] [blame] | 194 | "external/guava":/* recursive = */ true, |
| 195 | "external/error_prone":/* recursive = */ true, |
| 196 | "external/jsr305":/* recursive = */ true, |
| 197 | "frameworks/ex/common":/* recursive = */ true, |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 198 | |
Romain Jobredeaux | f1b0ac8 | 2021-08-12 14:39:00 +0000 | [diff] [blame] | 199 | "packages/apps/Music":/* recursive = */ true, |
Romain Jobredeaux | 9e09bba | 2021-09-08 18:09:02 +0000 | [diff] [blame] | 200 | "packages/apps/QuickSearchBox":/* recursive = */ true, |
Romain Jobredeaux | a2c0814 | 2021-09-22 14:43:43 -0400 | [diff] [blame] | 201 | "packages/apps/WallpaperPicker":/* recursive = */ false, |
| 202 | |
Chris Parsons | 494eef3 | 2021-11-09 10:29:52 -0500 | [diff] [blame] | 203 | "prebuilts/gcc":/* recursive = */ true, |
Romain Jobredeaux | a2c0814 | 2021-09-22 14:43:43 -0400 | [diff] [blame] | 204 | "prebuilts/sdk":/* recursive = */ false, |
| 205 | "prebuilts/sdk/current/extras/app-toolkit":/* recursive = */ false, |
| 206 | "prebuilts/sdk/current/support":/* recursive = */ false, |
| 207 | "prebuilts/sdk/tools":/* recursive = */ false, |
| 208 | "prebuilts/r8":/* recursive = */ false, |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 211 | // Configure modules in these directories to enable bp2build_available: true or false by default. |
| 212 | bp2buildDefaultConfig = Bp2BuildConfig{ |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 213 | "bionic": Bp2BuildDefaultTrueRecursively, |
| 214 | "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively, |
Rupert Shuttleworth | 2ac9c48 | 2021-11-10 10:37:05 -0500 | [diff] [blame] | 215 | "build/soong/cc/libbuildversion": Bp2BuildDefaultTrue, // Skip tests subdir |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 216 | "development/sdk": Bp2BuildDefaultTrueRecursively, |
| 217 | "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively, |
| 218 | "external/boringssl": Bp2BuildDefaultTrueRecursively, |
| 219 | "external/brotli": Bp2BuildDefaultTrue, |
| 220 | "external/fmtlib": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | b97a817 | 2021-10-04 12:48:29 -0400 | [diff] [blame] | 221 | "external/google-benchmark": Bp2BuildDefaultTrueRecursively, |
Liz Kammer | 2fc3489 | 2021-10-11 12:56:48 -0400 | [diff] [blame] | 222 | "external/googletest/googletest": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 223 | "external/gwp_asan": Bp2BuildDefaultTrueRecursively, |
| 224 | "external/jemalloc_new": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | b97a817 | 2021-10-04 12:48:29 -0400 | [diff] [blame] | 225 | "external/jsoncpp": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 226 | "external/libcap": Bp2BuildDefaultTrueRecursively, |
| 227 | "external/libcxx": Bp2BuildDefaultTrueRecursively, |
| 228 | "external/libcxxabi": Bp2BuildDefaultTrueRecursively, |
| 229 | "external/lz4/lib": Bp2BuildDefaultTrue, |
Liz Kammer | 2fc3489 | 2021-10-11 12:56:48 -0400 | [diff] [blame] | 230 | "external/mdnsresponder": Bp2BuildDefaultTrueRecursively, |
| 231 | "external/minijail": Bp2BuildDefaultTrueRecursively, |
| 232 | "external/pcre": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 233 | "external/protobuf": Bp2BuildDefaultTrueRecursively, |
| 234 | "external/python/six": Bp2BuildDefaultTrueRecursively, |
| 235 | "external/scudo": Bp2BuildDefaultTrueRecursively, |
Liz Kammer | 2fc3489 | 2021-10-11 12:56:48 -0400 | [diff] [blame] | 236 | "external/selinux/libselinux": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 237 | "external/zlib": Bp2BuildDefaultTrueRecursively, |
Liz Kammer | 2fc3489 | 2021-10-11 12:56:48 -0400 | [diff] [blame] | 238 | "external/zstd": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 239 | "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively, |
| 240 | "packages/modules/adb": Bp2BuildDefaultTrue, |
| 241 | "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively, |
| 242 | "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively, |
| 243 | "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively, |
| 244 | "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively, |
| 245 | "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively, |
| 246 | "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively, |
| 247 | "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively, |
| 248 | "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively, |
| 249 | "system/core/libasyncio": Bp2BuildDefaultTrue, |
| 250 | "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively, |
| 251 | "system/core/libcutils": Bp2BuildDefaultTrueRecursively, |
Liz Kammer | 2fc3489 | 2021-10-11 12:56:48 -0400 | [diff] [blame] | 252 | "system/core/libpackagelistparser": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 253 | "system/core/libprocessgroup": Bp2BuildDefaultTrue, |
Chris Parsons | b97a817 | 2021-10-04 12:48:29 -0400 | [diff] [blame] | 254 | "system/core/libprocessgroup/cgrouprc": Bp2BuildDefaultTrue, |
| 255 | "system/core/libprocessgroup/cgrouprc_format": Bp2BuildDefaultTrue, |
Lukacs T. Berki | 497f17d | 2021-04-26 12:15:57 +0200 | [diff] [blame] | 256 | "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 257 | "system/libbase": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | c39f633 | 2021-10-01 18:00:31 -0400 | [diff] [blame] | 258 | "system/libziparchive": Bp2BuildDefaultTrueRecursively, |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 259 | "system/logging/liblog": Bp2BuildDefaultTrueRecursively, |
| 260 | "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively, |
| 261 | "system/timezone/apex": Bp2BuildDefaultTrueRecursively, |
| 262 | "system/timezone/output_data": Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 263 | } |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 264 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 265 | // 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] | 266 | bp2buildModuleDoNotConvertList = []string{ |
Chris Parsons | 494eef3 | 2021-11-09 10:29:52 -0500 | [diff] [blame] | 267 | "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610. |
| 268 | |
Chris Parsons | c39f633 | 2021-10-01 18:00:31 -0400 | [diff] [blame] | 269 | "libc_malloc_debug", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics |
Rupert Shuttleworth | 47aa584 | 2021-04-30 04:04:15 -0400 | [diff] [blame] | 270 | |
Chris Parsons | c39f633 | 2021-10-01 18:00:31 -0400 | [diff] [blame] | 271 | "libbase_ndk", // http://b/186826477, fails to link libctscamera2_jni for device (required for CtsCameraTestCases) |
Alex Márquez Pérez MuñÃz DÃaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 272 | |
Liz Kammer | 0f3b7d2 | 2021-09-28 13:48:21 -0400 | [diff] [blame] | 273 | "libprotobuf-python", // contains .proto sources |
| 274 | "libprotobuf-internal-protos", // we don't handle path property for fileegroups |
| 275 | "libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups |
| 276 | |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 277 | "libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds. |
| 278 | "libfdtrack", // depends on libunwindstack, which depends on unsupported module art_cc_library_statics |
Lukacs T. Berki | b5ac5af | 2021-04-27 11:02:11 +0200 | [diff] [blame] | 279 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 280 | "gwp_asan_crash_handler", // cc_library, ld.lld: error: undefined symbol: memset |
| 281 | |
Chris Parsons | b97a817 | 2021-10-04 12:48:29 -0400 | [diff] [blame] | 282 | "brotli-fuzzer-corpus", // b/202015218: outputs are in location incompatible with bazel genrule handling. |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 283 | |
Jingwen Chen | df27b7a | 2021-10-18 06:33:16 +0000 | [diff] [blame] | 284 | // b/203369847: multiple genrules in the same package creating the same file |
| 285 | // //development/sdk/... |
| 286 | "platform_tools_properties", |
| 287 | "build_tools_source_properties", |
| 288 | |
Chris Parsons | 393002a | 2021-11-11 13:39:20 -0500 | [diff] [blame] | 289 | "libminijail", // b/202491296: Uses unsupported c_std property. |
| 290 | "minijail0", // depends on unconverted modules: libminijail |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 291 | "drop_privs", // depends on unconverted modules: libminijail |
Liz Kammer | 2fc3489 | 2021-10-11 12:56:48 -0400 | [diff] [blame] | 292 | |
Jingwen Chen | 790324e | 2021-04-30 08:20:01 +0000 | [diff] [blame] | 293 | // Tests. Handle later. |
| 294 | "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found |
| 295 | "libjemalloc5_integrationtest", |
| 296 | "libjemalloc5_stresstestlib", |
| 297 | "libjemalloc5_unittest", |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 298 | |
| 299 | // APEX support |
Chris Parsons | b97a817 | 2021-10-04 12:48:29 -0400 | [diff] [blame] | 300 | "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug' |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 301 | |
| 302 | "libadb_crypto", // Depends on libadb_protos |
| 303 | "libadb_crypto_static", // Depends on libadb_protos_static |
| 304 | "libadb_pairing_connection", // Depends on libadb_protos |
| 305 | "libadb_pairing_connection_static", // Depends on libadb_protos_static |
| 306 | "libadb_pairing_server", // Depends on libadb_protos |
| 307 | "libadb_pairing_server_static", // Depends on libadb_protos_static |
| 308 | "libadbd", // Depends on libadbd_core |
| 309 | "libadbd_core", // Depends on libadb_protos |
| 310 | "libadbd_services", // Depends on libadb_protos |
| 311 | |
| 312 | "libadb_protos_static", // b/200601772: Requires cc_library proto support |
| 313 | "libadb_protos", // b/200601772: Requires cc_library proto support |
| 314 | "libapp_processes_protos_lite", // b/200601772: Requires cc_library proto support |
Chris Parsons | c39f633 | 2021-10-01 18:00:31 -0400 | [diff] [blame] | 315 | |
| 316 | "libgtest_ndk_c++", // b/201816222: Requires sdk_version support. |
| 317 | "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support. |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 318 | |
| 319 | "abb", // depends on unconverted modules: libadbd_core, libadbd_services, libcmd, libbinder, libutils, libselinux |
| 320 | "adb", // depends on unconverted modules: bin2c_fastdeployagent, libadb_crypto, libadb_host, libadb_pairing_connection, libadb_protos, libandroidfw, libapp_processes_protos_full, libfastdeploy_host, libmdnssd, libopenscreen-discovery, libopenscreen-platform-impl, libusb, libutils, libziparchive, libzstd, AdbWinApi |
| 321 | "adbd", // depends on unconverted modules: libadb_crypto, libadb_pairing_connection, libadb_protos, libadbd, libadbd_core, libapp_processes_protos_lite, libmdnssd, libzstd, libadbd_services, libcap, libminijail, libselinux |
| 322 | "bionic_tests_zipalign", // depends on unconverted modules: libziparchive, libutils |
| 323 | "linker", // depends on unconverted modules: liblinker_debuggerd_stub, libdebuggerd_handler_fallback, libziparchive, liblinker_main, liblinker_malloc |
| 324 | "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_* |
| 325 | "sefcontext_compile", // depends on unconverted modules: libsepol |
| 326 | "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host |
| 327 | |
| 328 | "linkerconfig", // http://b/202876379 has arch-variant static_executable |
| 329 | "mdnsd", // http://b/202876379 has arch-variant static_executable |
| 330 | |
| 331 | "acvp_modulewrapper", // disabled for android x86/x86_64 |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 332 | } |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame] | 333 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 334 | // Per-module denylist of cc_library modules to only generate the static |
| 335 | // variant if their shared variant isn't ready or buildable by Bazel. |
| 336 | bp2buildCcLibraryStaticOnlyList = []string{ |
Jingwen Chen | 5354292 | 2021-05-18 09:01:49 +0000 | [diff] [blame] | 337 | "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] | 338 | } |
| 339 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 340 | // Per-module denylist to opt modules out of mixed builds. Such modules will |
| 341 | // still be generated via bp2build. |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 342 | mixedBuildsDisabledList = []string{ |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 343 | "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 |
| 344 | "minijail_constants_json", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module. |
Liz Kammer | c319299 | 2021-11-16 17:01:11 -0500 | [diff] [blame] | 345 | |
| 346 | "cap_names.h", // TODO(b/204913827) runfiles need to be handled in mixed builds |
| 347 | "libcap", // TODO(b/204913827) runfiles need to be handled in mixed builds |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 348 | } |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 349 | |
| 350 | // Used for quicker lookups |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 351 | bp2buildModuleDoNotConvert = map[string]bool{} |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 352 | bp2buildCcLibraryStaticOnly = map[string]bool{} |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 353 | mixedBuildsDisabled = map[string]bool{} |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 354 | ) |
| 355 | |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 356 | func init() { |
| 357 | for _, moduleName := range bp2buildModuleDoNotConvertList { |
| 358 | bp2buildModuleDoNotConvert[moduleName] = true |
| 359 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 360 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 361 | for _, moduleName := range bp2buildCcLibraryStaticOnlyList { |
| 362 | bp2buildCcLibraryStaticOnly[moduleName] = true |
| 363 | } |
| 364 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 365 | for _, moduleName := range mixedBuildsDisabledList { |
| 366 | mixedBuildsDisabled[moduleName] = true |
| 367 | } |
| 368 | } |
| 369 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 370 | func GenerateCcLibraryStaticOnly(moduleName string) bool { |
| 371 | return bp2buildCcLibraryStaticOnly[moduleName] |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 374 | func ShouldKeepExistingBuildFileForDir(dir string) bool { |
| 375 | if _, ok := bp2buildKeepExistingBuildFile[dir]; ok { |
| 376 | // Exact dir match |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 377 | return true |
| 378 | } |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 379 | // Check if subtree match |
| 380 | for prefix, recursive := range bp2buildKeepExistingBuildFile { |
| 381 | if recursive { |
| 382 | if strings.HasPrefix(dir, prefix+"/") { |
| 383 | return true |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | // Default |
| 388 | return false |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 391 | // MixedBuildsEnabled checks that a module is ready to be replaced by a |
| 392 | // converted or handcrafted Bazel target. |
Chris Parsons | 494eef3 | 2021-11-09 10:29:52 -0500 | [diff] [blame] | 393 | func (b *BazelModuleBase) MixedBuildsEnabled(ctx ModuleContext) bool { |
| 394 | if ctx.Os() == Windows { |
| 395 | // Windows toolchains are not currently supported. |
| 396 | return false |
| 397 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 398 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 399 | return false |
| 400 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 401 | if !convertedToBazel(ctx, ctx.Module()) { |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 402 | return false |
| 403 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 404 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 405 | if GenerateCcLibraryStaticOnly(ctx.Module().Name()) { |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 406 | // Don't use partially-converted cc_library targets in mixed builds, |
| 407 | // since mixed builds would generally rely on both static and shared |
| 408 | // variants of a cc_library. |
| 409 | return false |
| 410 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 411 | return !mixedBuildsDisabled[ctx.Module().Name()] |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 414 | // ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 415 | func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 416 | b, ok := module.(Bazelable) |
| 417 | if !ok { |
| 418 | return false |
| 419 | } |
| 420 | return b.convertWithBp2build(ctx, module) || b.HasHandcraftedLabel() |
| 421 | } |
| 422 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 423 | // ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 424 | func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionContext) bool { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 425 | return b.convertWithBp2build(ctx, ctx.Module()) |
| 426 | } |
| 427 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 428 | func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 429 | if bp2buildModuleDoNotConvert[module.Name()] { |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 430 | return false |
| 431 | } |
| 432 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 433 | // Ensure that the module type of this module has a bp2build converter. This |
| 434 | // prevents mixed builds from using auto-converted modules just by matching |
| 435 | // 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] | 436 | if ctx.Config().bp2buildModuleTypeConfig[ctx.OtherModuleType(module)] == false { |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 437 | if b, ok := module.(Bazelable); ok && b.BaseModuleType() != "" { |
| 438 | // For modules with custom types from soong_config_module_types, |
| 439 | // check that their _base module type_ has a bp2build mutator. |
| 440 | if ctx.Config().bp2buildModuleTypeConfig[b.BaseModuleType()] == false { |
| 441 | return false |
| 442 | } |
| 443 | } else { |
| 444 | return false |
| 445 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 446 | } |
| 447 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 448 | packagePath := ctx.OtherModuleDir(module) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 449 | config := ctx.Config().bp2buildPackageConfig |
| 450 | |
| 451 | // This is a tristate value: true, false, or unset. |
| 452 | propValue := b.bazelProperties.Bazel_module.Bp2build_available |
| 453 | if bp2buildDefaultTrueRecursively(packagePath, config) { |
| 454 | // Allow modules to explicitly opt-out. |
| 455 | return proptools.BoolDefault(propValue, true) |
| 456 | } |
| 457 | |
| 458 | // Allow modules to explicitly opt-in. |
| 459 | return proptools.BoolDefault(propValue, false) |
| 460 | } |
| 461 | |
| 462 | // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the |
| 463 | // set of package prefixes where all modules must be converted. That is, if the |
| 464 | // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will |
| 465 | // return true. |
| 466 | // |
| 467 | // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry |
| 468 | // exactly, this module will return false early. |
| 469 | // |
| 470 | // This function will also return false if the package doesn't match anything in |
| 471 | // the config. |
| 472 | func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool { |
| 473 | ret := false |
| 474 | |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 475 | // Check if the package path has an exact match in the config. |
| 476 | if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 477 | return true |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 478 | } else if config[packagePath] == Bp2BuildDefaultFalse { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 479 | return false |
| 480 | } |
| 481 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 482 | // If not, check for the config recursively. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 483 | packagePrefix := "" |
| 484 | // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist. |
| 485 | for _, part := range strings.Split(packagePath, "/") { |
| 486 | packagePrefix += part |
| 487 | if config[packagePrefix] == Bp2BuildDefaultTrueRecursively { |
| 488 | // package contains this prefix and this prefix should convert all modules |
| 489 | return true |
| 490 | } |
| 491 | // Continue to the next part of the package dir. |
| 492 | packagePrefix += "/" |
| 493 | } |
| 494 | |
| 495 | return ret |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 496 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 497 | |
| 498 | // GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or |
| 499 | // an error if there are errors reading the file. |
| 500 | // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do |
| 501 | // something more targeted based on the rule type and target. |
| 502 | func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 503 | if !strings.Contains(b.HandcraftedLabel(), path) { |
| 504 | 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] | 505 | } |
| 506 | name = filepath.Join(path, name) |
| 507 | f, err := c.fs.Open(name) |
| 508 | if err != nil { |
| 509 | return "", err |
| 510 | } |
| 511 | defer f.Close() |
| 512 | |
| 513 | data, err := ioutil.ReadAll(f) |
| 514 | if err != nil { |
| 515 | return "", err |
| 516 | } |
| 517 | return string(data[:]), nil |
| 518 | } |