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 ( |
| 111 | // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map, |
| 112 | // which can also mean that the key doesn't exist in a lookup. |
| 113 | |
| 114 | // all modules in this package and subpackages default to bp2build_available: true. |
| 115 | // allows modules to opt-out. |
| 116 | Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1 |
| 117 | |
| 118 | // all modules in this package (not recursively) default to bp2build_available: false. |
| 119 | // allows modules to opt-in. |
| 120 | Bp2BuildDefaultFalse |
| 121 | ) |
| 122 | |
| 123 | var ( |
| 124 | // Configure modules in these directories to enable bp2build_available: true or false by default. |
| 125 | bp2buildDefaultConfig = Bp2BuildConfig{ |
| 126 | "bionic": Bp2BuildDefaultTrueRecursively, |
| 127 | "system/core/libcutils": Bp2BuildDefaultTrueRecursively, |
| 128 | "system/logging/liblog": Bp2BuildDefaultTrueRecursively, |
| 129 | } |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame^] | 130 | |
| 131 | // Per-module denylist to always opt modules out. |
| 132 | bp2buildModuleDoNotConvert = map[string]bool{ |
| 133 | "libBionicBenchmarksUtils": true, |
| 134 | "libbionic_spawn_benchmark": true, |
| 135 | "libc_jemalloc_wrapper": true, |
| 136 | "libc_bootstrap": true, |
| 137 | "libc_init_static": true, |
| 138 | "libc_init_dynamic": true, |
| 139 | "libc_tzcode": true, |
| 140 | "lib_dns": true, |
| 141 | "libc_freebsd": true, |
| 142 | "libc_freebsd_large_stack": true, |
| 143 | "libc_netbsd": true, |
| 144 | "libc_openbsd_ndk": true, |
| 145 | "libc_openbsd_large_stack": true, |
| 146 | "libc_openbsd": true, |
| 147 | "libc_gdtoa": true, |
| 148 | "libc_fortify": true, |
| 149 | "libc_bionic": true, |
| 150 | "libc_bionic_ndk": true, |
| 151 | "libc_bionic_systrace": true, |
| 152 | "libc_pthread": true, |
| 153 | "libc_syscalls": true, |
| 154 | "libc_aeabi": true, |
| 155 | "libc_ndk": true, |
| 156 | "libc_nopthread": true, |
| 157 | "libc_common": true, |
| 158 | "libc_static_dispatch": true, |
| 159 | "libc_dynamic_dispatch": true, |
| 160 | "libc_common_static": true, |
| 161 | "libc_common_shared": true, |
| 162 | "libc_unwind_static": true, |
| 163 | "libc_nomalloc": true, |
| 164 | "libasync_safe": true, |
| 165 | "libc_malloc_debug_backtrace": true, |
| 166 | "libsystemproperties": true, |
| 167 | "libdl_static": true, |
| 168 | "liblinker_main": true, |
| 169 | "liblinker_malloc": true, |
| 170 | "liblinker_debuggerd_stub": true, |
| 171 | "libbionic_tests_headers_posix": true, |
| 172 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 173 | ) |
| 174 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 175 | // ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 176 | func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool { |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame^] | 177 | if bp2buildModuleDoNotConvert[ctx.Module().Name()] { |
| 178 | return false |
| 179 | } |
| 180 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 181 | // Ensure that the module type of this module has a bp2build converter. This |
| 182 | // prevents mixed builds from using auto-converted modules just by matching |
| 183 | // the package dir; it also has to have a bp2build mutator as well. |
| 184 | if ctx.Config().bp2buildModuleTypeConfig[ctx.ModuleType()] == false { |
| 185 | return false |
| 186 | } |
| 187 | |
| 188 | packagePath := ctx.ModuleDir() |
| 189 | config := ctx.Config().bp2buildPackageConfig |
| 190 | |
| 191 | // This is a tristate value: true, false, or unset. |
| 192 | propValue := b.bazelProperties.Bazel_module.Bp2build_available |
| 193 | if bp2buildDefaultTrueRecursively(packagePath, config) { |
| 194 | // Allow modules to explicitly opt-out. |
| 195 | return proptools.BoolDefault(propValue, true) |
| 196 | } |
| 197 | |
| 198 | // Allow modules to explicitly opt-in. |
| 199 | return proptools.BoolDefault(propValue, false) |
| 200 | } |
| 201 | |
| 202 | // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the |
| 203 | // set of package prefixes where all modules must be converted. That is, if the |
| 204 | // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will |
| 205 | // return true. |
| 206 | // |
| 207 | // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry |
| 208 | // exactly, this module will return false early. |
| 209 | // |
| 210 | // This function will also return false if the package doesn't match anything in |
| 211 | // the config. |
| 212 | func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool { |
| 213 | ret := false |
| 214 | |
| 215 | if config[packagePath] == Bp2BuildDefaultFalse { |
| 216 | return false |
| 217 | } |
| 218 | |
| 219 | packagePrefix := "" |
| 220 | // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist. |
| 221 | for _, part := range strings.Split(packagePath, "/") { |
| 222 | packagePrefix += part |
| 223 | if config[packagePrefix] == Bp2BuildDefaultTrueRecursively { |
| 224 | // package contains this prefix and this prefix should convert all modules |
| 225 | return true |
| 226 | } |
| 227 | // Continue to the next part of the package dir. |
| 228 | packagePrefix += "/" |
| 229 | } |
| 230 | |
| 231 | return ret |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 232 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 233 | |
| 234 | // GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or |
| 235 | // an error if there are errors reading the file. |
| 236 | // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do |
| 237 | // something more targeted based on the rule type and target. |
| 238 | func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 239 | if !strings.Contains(b.HandcraftedLabel(), path) { |
| 240 | 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] | 241 | } |
| 242 | name = filepath.Join(path, name) |
| 243 | f, err := c.fs.Open(name) |
| 244 | if err != nil { |
| 245 | return "", err |
| 246 | } |
| 247 | defer f.Close() |
| 248 | |
| 249 | data, err := ioutil.ReadAll(f) |
| 250 | if err != nil { |
| 251 | return "", err |
| 252 | } |
| 253 | return string(data[:]), nil |
| 254 | } |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 255 | |
| 256 | // ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically |
| 257 | // or manually |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 258 | func (b *BazelModuleBase) ConvertedToBazel(ctx BazelConversionPathContext) bool { |
| 259 | return b.ConvertWithBp2build(ctx) || b.HasHandcraftedLabel() |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 260 | } |