Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | // Copyright 2020 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 bp2build |
| 16 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0da7ce6 | 2021-08-23 17:04:20 +0000 | [diff] [blame] | 17 | /* |
| 18 | For shareable/common functionality for conversion from soong-module to build files |
| 19 | for queryview/bp2build |
| 20 | */ |
| 21 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 22 | import ( |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 23 | "fmt" |
| 24 | "reflect" |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 25 | "sort" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 26 | "strings" |
| 27 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0da7ce6 | 2021-08-23 17:04:20 +0000 | [diff] [blame] | 28 | "android/soong/android" |
| 29 | "android/soong/bazel" |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 30 | "android/soong/starlark_fmt" |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 31 | "android/soong/ui/metrics/bp2build_metrics_proto" |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 32 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 33 | "github.com/google/blueprint" |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 34 | "github.com/google/blueprint/bootstrap" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 35 | "github.com/google/blueprint/proptools" |
| 36 | ) |
| 37 | |
| 38 | type BazelAttributes struct { |
| 39 | Attrs map[string]string |
| 40 | } |
| 41 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 42 | type BazelLoadSymbol struct { |
| 43 | // The name of the symbol in the file being loaded |
| 44 | symbol string |
| 45 | // The name the symbol wil have in this file. Can be left blank to use the same name as symbol. |
| 46 | alias string |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 49 | type BazelLoad struct { |
| 50 | file string |
| 51 | symbols []BazelLoadSymbol |
| 52 | } |
| 53 | |
| 54 | type BazelTarget struct { |
| 55 | name string |
| 56 | packageName string |
| 57 | content string |
| 58 | ruleClass string |
| 59 | loads []BazelLoad |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 62 | // Label is the fully qualified Bazel label constructed from the BazelTarget's |
| 63 | // package name and target name. |
| 64 | func (t BazelTarget) Label() string { |
| 65 | if t.packageName == "." { |
| 66 | return "//:" + t.name |
| 67 | } else { |
| 68 | return "//" + t.packageName + ":" + t.name |
| 69 | } |
| 70 | } |
| 71 | |
Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 72 | // PackageName returns the package of the Bazel target. |
| 73 | // Defaults to root of tree. |
| 74 | func (t BazelTarget) PackageName() string { |
| 75 | if t.packageName == "" { |
| 76 | return "." |
| 77 | } |
| 78 | return t.packageName |
| 79 | } |
| 80 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 81 | // BazelTargets is a typedef for a slice of BazelTarget objects. |
| 82 | type BazelTargets []BazelTarget |
| 83 | |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 84 | func (targets BazelTargets) packageRule() *BazelTarget { |
| 85 | for _, target := range targets { |
| 86 | if target.ruleClass == "package" { |
| 87 | return &target |
| 88 | } |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // sort a list of BazelTargets in-place, by name, and by generated/handcrafted types. |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 94 | func (targets BazelTargets) sort() { |
| 95 | sort.Slice(targets, func(i, j int) bool { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 96 | return targets[i].name < targets[j].name |
| 97 | }) |
| 98 | } |
| 99 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 100 | // String returns the string representation of BazelTargets, without load |
| 101 | // statements (use LoadStatements for that), since the targets are usually not |
| 102 | // adjacent to the load statements at the top of the BUILD file. |
| 103 | func (targets BazelTargets) String() string { |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 104 | var res strings.Builder |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 105 | for i, target := range targets { |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 106 | if target.ruleClass != "package" { |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 107 | res.WriteString(target.content) |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 108 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 109 | if i != len(targets)-1 { |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 110 | res.WriteString("\n\n") |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 111 | } |
| 112 | } |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 113 | return res.String() |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // LoadStatements return the string representation of the sorted and deduplicated |
| 117 | // Starlark rule load statements needed by a group of BazelTargets. |
| 118 | func (targets BazelTargets) LoadStatements() string { |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 119 | // First, merge all the load statements from all the targets onto one list |
| 120 | bzlToLoadedSymbols := map[string][]BazelLoadSymbol{} |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 121 | for _, target := range targets { |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 122 | for _, load := range target.loads { |
| 123 | outer: |
| 124 | for _, symbol := range load.symbols { |
| 125 | alias := symbol.alias |
| 126 | if alias == "" { |
| 127 | alias = symbol.symbol |
| 128 | } |
| 129 | for _, otherSymbol := range bzlToLoadedSymbols[load.file] { |
| 130 | otherAlias := otherSymbol.alias |
| 131 | if otherAlias == "" { |
| 132 | otherAlias = otherSymbol.symbol |
| 133 | } |
| 134 | if symbol.symbol == otherSymbol.symbol && alias == otherAlias { |
| 135 | continue outer |
| 136 | } else if alias == otherAlias { |
| 137 | panic(fmt.Sprintf("Conflicting destination (%s) for loads of %s and %s", alias, symbol.symbol, otherSymbol.symbol)) |
| 138 | } |
| 139 | } |
| 140 | bzlToLoadedSymbols[load.file] = append(bzlToLoadedSymbols[load.file], symbol) |
| 141 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 145 | var loadStatements strings.Builder |
| 146 | for i, bzl := range android.SortedKeys(bzlToLoadedSymbols) { |
| 147 | symbols := bzlToLoadedSymbols[bzl] |
| 148 | loadStatements.WriteString("load(\"") |
| 149 | loadStatements.WriteString(bzl) |
| 150 | loadStatements.WriteString("\", ") |
| 151 | sort.Slice(symbols, func(i, j int) bool { |
| 152 | if symbols[i].symbol < symbols[j].symbol { |
| 153 | return true |
| 154 | } |
| 155 | return symbols[i].alias < symbols[j].alias |
| 156 | }) |
| 157 | for j, symbol := range symbols { |
| 158 | if symbol.alias != "" && symbol.alias != symbol.symbol { |
| 159 | loadStatements.WriteString(symbol.alias) |
| 160 | loadStatements.WriteString(" = ") |
| 161 | } |
| 162 | loadStatements.WriteString("\"") |
| 163 | loadStatements.WriteString(symbol.symbol) |
| 164 | loadStatements.WriteString("\"") |
| 165 | if j != len(symbols)-1 { |
| 166 | loadStatements.WriteString(", ") |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 167 | } |
| 168 | } |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 169 | loadStatements.WriteString(")") |
| 170 | if i != len(bzlToLoadedSymbols)-1 { |
| 171 | loadStatements.WriteString("\n") |
| 172 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 173 | } |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 174 | return loadStatements.String() |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | type bpToBuildContext interface { |
| 178 | ModuleName(module blueprint.Module) string |
| 179 | ModuleDir(module blueprint.Module) string |
| 180 | ModuleSubDir(module blueprint.Module) string |
| 181 | ModuleType(module blueprint.Module) string |
| 182 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 183 | VisitAllModules(visit func(blueprint.Module)) |
| 184 | VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module)) |
| 185 | } |
| 186 | |
| 187 | type CodegenContext struct { |
Jingwen Chen | 16d90a8 | 2021-09-17 07:16:13 +0000 | [diff] [blame] | 188 | config android.Config |
Paul Duffin | c639059 | 2022-11-04 13:35:21 +0000 | [diff] [blame] | 189 | context *android.Context |
Jingwen Chen | 16d90a8 | 2021-09-17 07:16:13 +0000 | [diff] [blame] | 190 | mode CodegenMode |
| 191 | additionalDeps []string |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 192 | unconvertedDepMode unconvertedDepsMode |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 193 | topDir string |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 196 | func (ctx *CodegenContext) Mode() CodegenMode { |
| 197 | return ctx.mode |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 200 | // CodegenMode is an enum to differentiate code-generation modes. |
| 201 | type CodegenMode int |
| 202 | |
| 203 | const ( |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 204 | // Bp2Build - generate BUILD files with targets buildable by Bazel directly. |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 205 | // |
| 206 | // This mode is used for the Soong->Bazel build definition conversion. |
| 207 | Bp2Build CodegenMode = iota |
| 208 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 209 | // QueryView - generate BUILD files with targets representing fully mutated |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 210 | // Soong modules, representing the fully configured Soong module graph with |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 211 | // variants and dependency edges. |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 212 | // |
| 213 | // This mode is used for discovering and introspecting the existing Soong |
| 214 | // module graph. |
| 215 | QueryView |
| 216 | ) |
| 217 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 218 | type unconvertedDepsMode int |
| 219 | |
| 220 | const ( |
| 221 | // Include a warning in conversion metrics about converted modules with unconverted direct deps |
| 222 | warnUnconvertedDeps unconvertedDepsMode = iota |
| 223 | // Error and fail conversion if encountering a module with unconverted direct deps |
| 224 | // Enabled by setting environment variable `BP2BUILD_ERROR_UNCONVERTED` |
| 225 | errorModulesUnconvertedDeps |
| 226 | ) |
| 227 | |
Jingwen Chen | dcc329a | 2021-01-26 02:49:03 -0500 | [diff] [blame] | 228 | func (mode CodegenMode) String() string { |
| 229 | switch mode { |
| 230 | case Bp2Build: |
| 231 | return "Bp2Build" |
| 232 | case QueryView: |
| 233 | return "QueryView" |
| 234 | default: |
| 235 | return fmt.Sprintf("%d", mode) |
| 236 | } |
| 237 | } |
| 238 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 239 | // AddNinjaFileDeps adds dependencies on the specified files to be added to the ninja manifest. The |
| 240 | // primary builder will be rerun whenever the specified files are modified. Allows us to fulfill the |
| 241 | // PathContext interface in order to add dependencies on hand-crafted BUILD files. Note: must also |
| 242 | // call AdditionalNinjaDeps and add them manually to the ninja file. |
| 243 | func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) { |
| 244 | ctx.additionalDeps = append(ctx.additionalDeps, deps...) |
| 245 | } |
| 246 | |
| 247 | // AdditionalNinjaDeps returns additional ninja deps added by CodegenContext |
| 248 | func (ctx *CodegenContext) AdditionalNinjaDeps() []string { |
| 249 | return ctx.additionalDeps |
| 250 | } |
| 251 | |
Paul Duffin | c639059 | 2022-11-04 13:35:21 +0000 | [diff] [blame] | 252 | func (ctx *CodegenContext) Config() android.Config { return ctx.config } |
| 253 | func (ctx *CodegenContext) Context() *android.Context { return ctx.context } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 254 | |
| 255 | // NewCodegenContext creates a wrapper context that conforms to PathContext for |
| 256 | // writing BUILD files in the output directory. |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 257 | func NewCodegenContext(config android.Config, context *android.Context, mode CodegenMode, topDir string) *CodegenContext { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 258 | var unconvertedDeps unconvertedDepsMode |
| 259 | if config.IsEnvTrue("BP2BUILD_ERROR_UNCONVERTED") { |
| 260 | unconvertedDeps = errorModulesUnconvertedDeps |
| 261 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 262 | return &CodegenContext{ |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 263 | context: context, |
| 264 | config: config, |
| 265 | mode: mode, |
| 266 | unconvertedDepMode: unconvertedDeps, |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 267 | topDir: topDir, |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 268 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // props is an unsorted map. This function ensures that |
| 272 | // the generated attributes are sorted to ensure determinism. |
| 273 | func propsToAttributes(props map[string]string) string { |
| 274 | var attributes string |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 275 | for _, propName := range android.SortedKeys(props) { |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 276 | attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName]) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 277 | } |
| 278 | return attributes |
| 279 | } |
| 280 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 281 | type conversionResults struct { |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 282 | buildFileToTargets map[string]BazelTargets |
| 283 | moduleNameToPartition map[string]string |
| 284 | metrics CodegenMetrics |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | func (r conversionResults) BuildDirToTargets() map[string]BazelTargets { |
| 288 | return r.buildFileToTargets |
| 289 | } |
| 290 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 291 | // struct to store state of b bazel targets (e.g. go targets which do not implement android.Module) |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 292 | // this implements bp2buildModule interface and is passed to generateBazelTargets |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 293 | type bTarget struct { |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 294 | targetName string |
| 295 | targetPackage string |
| 296 | bazelRuleClass string |
| 297 | bazelRuleLoadLocation string |
| 298 | bazelAttributes []interface{} |
| 299 | } |
| 300 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 301 | var _ bp2buildModule = (*bTarget)(nil) |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 302 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 303 | func (b bTarget) TargetName() string { |
| 304 | return b.targetName |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 307 | func (b bTarget) TargetPackage() string { |
| 308 | return b.targetPackage |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 311 | func (b bTarget) BazelRuleClass() string { |
| 312 | return b.bazelRuleClass |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 315 | func (b bTarget) BazelRuleLoadLocation() string { |
| 316 | return b.bazelRuleLoadLocation |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 319 | func (b bTarget) BazelAttributes() []interface{} { |
| 320 | return b.bazelAttributes |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Creates a target_compatible_with entry that is *not* compatible with android |
| 324 | func targetNotCompatibleWithAndroid() bazel.LabelListAttribute { |
| 325 | ret := bazel.LabelListAttribute{} |
| 326 | ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid, |
| 327 | bazel.MakeLabelList( |
| 328 | []bazel.Label{ |
| 329 | bazel.Label{ |
| 330 | Label: "@platforms//:incompatible", |
| 331 | }, |
| 332 | }, |
| 333 | ), |
| 334 | ) |
| 335 | return ret |
| 336 | } |
| 337 | |
| 338 | // helper function to return labels for srcs used in bootstrap_go_package and bootstrap_go_binary |
| 339 | // this function has the following limitations which make it unsuitable for widespread use |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 340 | // - wildcard patterns in srcs |
| 341 | // This is ok for go since build/blueprint does not support it. |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 342 | // |
| 343 | // Prefer to use `BazelLabelForModuleSrc` instead |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 344 | func goSrcLabels(cfg android.Config, moduleDir string, srcs []string, linuxSrcs, darwinSrcs []string) bazel.LabelListAttribute { |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 345 | labels := func(srcs []string) bazel.LabelList { |
| 346 | ret := []bazel.Label{} |
| 347 | for _, src := range srcs { |
| 348 | srcLabel := bazel.Label{ |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 349 | Label: src, |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 350 | } |
| 351 | ret = append(ret, srcLabel) |
| 352 | } |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 353 | // Respect package boundaries |
| 354 | return android.TransformSubpackagePaths( |
| 355 | cfg, |
| 356 | moduleDir, |
| 357 | bazel.MakeLabelList(ret), |
| 358 | ) |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | ret := bazel.LabelListAttribute{} |
| 362 | // common |
| 363 | ret.SetSelectValue(bazel.NoConfigAxis, "", labels(srcs)) |
| 364 | // linux |
| 365 | ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsLinux, labels(linuxSrcs)) |
| 366 | // darwin |
| 367 | ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsDarwin, labels(darwinSrcs)) |
| 368 | return ret |
| 369 | } |
| 370 | |
| 371 | func goDepLabels(deps []string, goModulesMap nameToGoLibraryModule) bazel.LabelListAttribute { |
| 372 | labels := []bazel.Label{} |
| 373 | for _, dep := range deps { |
| 374 | moduleDir := goModulesMap[dep].Dir |
| 375 | if moduleDir == "." { |
| 376 | moduleDir = "" |
| 377 | } |
| 378 | label := bazel.Label{ |
| 379 | Label: fmt.Sprintf("//%s:%s", moduleDir, dep), |
| 380 | } |
| 381 | labels = append(labels, label) |
| 382 | } |
| 383 | return bazel.MakeLabelListAttribute(bazel.MakeLabelList(labels)) |
| 384 | } |
| 385 | |
| 386 | // attributes common to blueprint_go_binary and bootstap_go_package |
| 387 | type goAttributes struct { |
| 388 | Importpath bazel.StringAttribute |
| 389 | Srcs bazel.LabelListAttribute |
| 390 | Deps bazel.LabelListAttribute |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 391 | Data bazel.LabelListAttribute |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 392 | Target_compatible_with bazel.LabelListAttribute |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 393 | |
| 394 | // attributes for the dynamically generated go_test target |
| 395 | Embed bazel.LabelListAttribute |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 398 | type goTestProperties struct { |
| 399 | name string |
| 400 | dir string |
| 401 | testSrcs []string |
| 402 | linuxTestSrcs []string |
| 403 | darwinTestSrcs []string |
| 404 | testData []string |
| 405 | // Name of the target that should be compiled together with the test |
| 406 | embedName string |
| 407 | } |
| 408 | |
| 409 | // Creates a go_test target for bootstrap_go_package / blueprint_go_binary |
| 410 | func generateBazelTargetsGoTest(ctx *android.Context, goModulesMap nameToGoLibraryModule, gp goTestProperties) (BazelTarget, error) { |
| 411 | ca := android.CommonAttributes{ |
| 412 | Name: gp.name, |
| 413 | } |
| 414 | ga := goAttributes{ |
| 415 | Srcs: goSrcLabels(ctx.Config(), gp.dir, gp.testSrcs, gp.linuxTestSrcs, gp.darwinTestSrcs), |
| 416 | Data: goSrcLabels(ctx.Config(), gp.dir, gp.testData, []string{}, []string{}), |
| 417 | Embed: bazel.MakeLabelListAttribute( |
| 418 | bazel.MakeLabelList( |
| 419 | []bazel.Label{bazel.Label{Label: ":" + gp.embedName}}, |
| 420 | ), |
| 421 | ), |
| 422 | Target_compatible_with: targetNotCompatibleWithAndroid(), |
| 423 | } |
| 424 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 425 | libTest := bTarget{ |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 426 | targetName: gp.name, |
| 427 | targetPackage: gp.dir, |
| 428 | bazelRuleClass: "go_test", |
| 429 | bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl", |
| 430 | bazelAttributes: []interface{}{&ca, &ga}, |
| 431 | } |
| 432 | return generateBazelTarget(ctx, libTest) |
| 433 | } |
| 434 | |
| 435 | // TODO - b/288491147: testSrcs of certain bootstrap_go_package/blueprint_go_binary are not hermetic and depend on |
| 436 | // testdata checked into the filesystem. |
| 437 | // Denylist the generation of go_test targets for these Soong modules. |
| 438 | // The go_library/go_binary will still be generated, since those are hermitic. |
| 439 | var ( |
| 440 | goTestsDenylist = []string{ |
| 441 | "android-archive-zip", |
| 442 | "bazel_notice_gen", |
| 443 | "blueprint-bootstrap-bpdoc", |
| 444 | "blueprint-microfactory", |
| 445 | "blueprint-pathtools", |
| 446 | "bssl_ar", |
| 447 | "compliance_checkmetadata", |
| 448 | "compliance_checkshare", |
| 449 | "compliance_dumpgraph", |
| 450 | "compliance_dumpresolutions", |
| 451 | "compliance_listshare", |
| 452 | "compliance-module", |
| 453 | "compliancenotice_bom", |
| 454 | "compliancenotice_shippedlibs", |
| 455 | "compliance_rtrace", |
| 456 | "compliance_sbom", |
| 457 | "golang-protobuf-internal-fuzz-jsonfuzz", |
| 458 | "golang-protobuf-internal-fuzz-textfuzz", |
| 459 | "golang-protobuf-internal-fuzz-wirefuzz", |
| 460 | "htmlnotice", |
| 461 | "protoc-gen-go", |
| 462 | "rbcrun-module", |
| 463 | "spdx-tools-builder", |
| 464 | "spdx-tools-builder2v1", |
| 465 | "spdx-tools-builder2v2", |
| 466 | "spdx-tools-builder2v3", |
| 467 | "spdx-tools-idsearcher", |
| 468 | "spdx-tools-spdx-json", |
| 469 | "spdx-tools-utils", |
| 470 | "soong-ui-build", |
| 471 | "textnotice", |
| 472 | "xmlnotice", |
| 473 | } |
| 474 | ) |
| 475 | |
Spandan Das | 89aa0f7 | 2023-06-30 20:18:39 +0000 | [diff] [blame] | 476 | func testOfGoPackageIsIncompatible(g *bootstrap.GoPackage) bool { |
| 477 | return android.InList(g.Name(), goTestsDenylist) || |
| 478 | // Denylist tests of soong_build |
| 479 | // Theses tests have a guard that prevent usage outside a test environment |
| 480 | // The guard (`ensureTestOnly`) looks for a `-test` in os.Args, which is present in soong's gotestrunner, but missing in `b test` |
| 481 | g.IsPluginFor("soong_build") || |
| 482 | // soong-android is a dep of soong_build |
| 483 | // This dependency is created by soong_build by listing it in its deps explicitly in Android.bp, and not via `plugin_for` in `soong-android` |
| 484 | g.Name() == "soong-android" |
| 485 | } |
| 486 | |
| 487 | func testOfGoBinaryIsIncompatible(g *bootstrap.GoBinary) bool { |
| 488 | return android.InList(g.Name(), goTestsDenylist) |
| 489 | } |
| 490 | |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 491 | func generateBazelTargetsGoPackage(ctx *android.Context, g *bootstrap.GoPackage, goModulesMap nameToGoLibraryModule) ([]BazelTarget, []error) { |
| 492 | ca := android.CommonAttributes{ |
| 493 | Name: g.Name(), |
| 494 | } |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 495 | |
| 496 | // For this bootstrap_go_package dep chain, |
| 497 | // A --> B --> C ( ---> depends on) |
| 498 | // Soong provides the convenience of only listing B as deps of A even if a src file of A imports C |
| 499 | // Bazel OTOH |
| 500 | // 1. requires C to be listed in `deps` expllicity. |
| 501 | // 2. does not require C to be listed if src of A does not import C |
| 502 | // |
| 503 | // bp2build does not have sufficient info on whether C is a direct dep of A or not, so for now collect all transitive deps and add them to deps |
| 504 | transitiveDeps := transitiveGoDeps(g.Deps(), goModulesMap) |
| 505 | |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 506 | ga := goAttributes{ |
| 507 | Importpath: bazel.StringAttribute{ |
| 508 | Value: proptools.StringPtr(g.GoPkgPath()), |
| 509 | }, |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 510 | Srcs: goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()), |
| 511 | Deps: goDepLabels( |
| 512 | android.FirstUniqueStrings(transitiveDeps), |
| 513 | goModulesMap, |
| 514 | ), |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 515 | Target_compatible_with: targetNotCompatibleWithAndroid(), |
| 516 | } |
| 517 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 518 | lib := bTarget{ |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 519 | targetName: g.Name(), |
| 520 | targetPackage: ctx.ModuleDir(g), |
| 521 | bazelRuleClass: "go_library", |
| 522 | bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl", |
| 523 | bazelAttributes: []interface{}{&ca, &ga}, |
| 524 | } |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 525 | retTargets := []BazelTarget{} |
| 526 | var retErrs []error |
| 527 | if libTarget, err := generateBazelTarget(ctx, lib); err == nil { |
| 528 | retTargets = append(retTargets, libTarget) |
| 529 | } else { |
| 530 | retErrs = []error{err} |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 531 | } |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 532 | |
| 533 | // If the library contains test srcs, create an additional go_test target |
Spandan Das | 89aa0f7 | 2023-06-30 20:18:39 +0000 | [diff] [blame] | 534 | if !testOfGoPackageIsIncompatible(g) && (len(g.TestSrcs()) > 0 || len(g.LinuxTestSrcs()) > 0 || len(g.DarwinTestSrcs()) > 0) { |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 535 | gp := goTestProperties{ |
| 536 | name: g.Name() + "-test", |
| 537 | dir: ctx.ModuleDir(g), |
| 538 | testSrcs: g.TestSrcs(), |
| 539 | linuxTestSrcs: g.LinuxTestSrcs(), |
| 540 | darwinTestSrcs: g.DarwinTestSrcs(), |
| 541 | testData: g.TestData(), |
| 542 | embedName: g.Name(), // embed the source go_library in the test so that its .go files are included in the compilation unit |
| 543 | } |
| 544 | if libTestTarget, err := generateBazelTargetsGoTest(ctx, goModulesMap, gp); err == nil { |
| 545 | retTargets = append(retTargets, libTestTarget) |
| 546 | } else { |
| 547 | retErrs = append(retErrs, err) |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return retTargets, retErrs |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | type goLibraryModule struct { |
| 555 | Dir string |
| 556 | Deps []string |
| 557 | } |
| 558 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 559 | type buildConversionMetadata struct { |
| 560 | nameToGoLibraryModule nameToGoLibraryModule |
| 561 | ndkHeaders []blueprint.Module |
| 562 | } |
| 563 | |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 564 | type nameToGoLibraryModule map[string]goLibraryModule |
| 565 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 566 | // Visit each module in the graph, and collect metadata about the build graph |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 567 | // If a module is of type `bootstrap_go_package`, return a map containing metadata like its dir and deps |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 568 | // If a module is of type `ndk_headers`, add it to a list and return the list |
| 569 | func createBuildConversionMetadata(ctx *android.Context) buildConversionMetadata { |
| 570 | goMap := nameToGoLibraryModule{} |
| 571 | ndkHeaders := []blueprint.Module{} |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 572 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 573 | moduleType := ctx.ModuleType(m) |
| 574 | // We do not need to store information about blueprint_go_binary since it does not have any rdeps |
| 575 | if moduleType == "bootstrap_go_package" { |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 576 | goMap[m.Name()] = goLibraryModule{ |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 577 | Dir: ctx.ModuleDir(m), |
| 578 | Deps: m.(*bootstrap.GoPackage).Deps(), |
| 579 | } |
Spandan Das | a7da3f0 | 2023-09-28 19:30:51 +0000 | [diff] [blame] | 580 | } else if moduleType == "ndk_headers" || moduleType == "versioned_ndk_headers" { |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 581 | ndkHeaders = append(ndkHeaders, m) |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 582 | } |
| 583 | }) |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 584 | return buildConversionMetadata{ |
| 585 | nameToGoLibraryModule: goMap, |
| 586 | ndkHeaders: ndkHeaders, |
| 587 | } |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 590 | // Returns the deps in the transitive closure of a go target |
| 591 | func transitiveGoDeps(directDeps []string, goModulesMap nameToGoLibraryModule) []string { |
| 592 | allDeps := directDeps |
| 593 | i := 0 |
| 594 | for i < len(allDeps) { |
| 595 | curr := allDeps[i] |
| 596 | allDeps = append(allDeps, goModulesMap[curr].Deps...) |
| 597 | i += 1 |
| 598 | } |
| 599 | allDeps = android.SortedUniqueStrings(allDeps) |
| 600 | return allDeps |
| 601 | } |
| 602 | |
| 603 | func generateBazelTargetsGoBinary(ctx *android.Context, g *bootstrap.GoBinary, goModulesMap nameToGoLibraryModule) ([]BazelTarget, []error) { |
| 604 | ca := android.CommonAttributes{ |
| 605 | Name: g.Name(), |
| 606 | } |
| 607 | |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 608 | retTargets := []BazelTarget{} |
| 609 | var retErrs []error |
| 610 | |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 611 | // For this bootstrap_go_package dep chain, |
| 612 | // A --> B --> C ( ---> depends on) |
| 613 | // Soong provides the convenience of only listing B as deps of A even if a src file of A imports C |
| 614 | // Bazel OTOH |
| 615 | // 1. requires C to be listed in `deps` expllicity. |
| 616 | // 2. does not require C to be listed if src of A does not import C |
| 617 | // |
| 618 | // bp2build does not have sufficient info on whether C is a direct dep of A or not, so for now collect all transitive deps and add them to deps |
| 619 | transitiveDeps := transitiveGoDeps(g.Deps(), goModulesMap) |
| 620 | |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 621 | goSource := "" |
| 622 | // If the library contains test srcs, create an additional go_test target |
| 623 | // The go_test target will embed a go_source containining the source .go files it tests |
Spandan Das | 89aa0f7 | 2023-06-30 20:18:39 +0000 | [diff] [blame] | 624 | if !testOfGoBinaryIsIncompatible(g) && (len(g.TestSrcs()) > 0 || len(g.LinuxTestSrcs()) > 0 || len(g.DarwinTestSrcs()) > 0) { |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 625 | // Create a go_source containing the source .go files of go_library |
| 626 | // This target will be an `embed` of the go_binary and go_test |
| 627 | goSource = g.Name() + "-source" |
| 628 | ca := android.CommonAttributes{ |
| 629 | Name: goSource, |
| 630 | } |
| 631 | ga := goAttributes{ |
| 632 | Srcs: goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()), |
| 633 | Deps: goDepLabels(transitiveDeps, goModulesMap), |
| 634 | Target_compatible_with: targetNotCompatibleWithAndroid(), |
| 635 | } |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 636 | libTestSource := bTarget{ |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 637 | targetName: goSource, |
| 638 | targetPackage: ctx.ModuleDir(g), |
| 639 | bazelRuleClass: "go_source", |
| 640 | bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl", |
| 641 | bazelAttributes: []interface{}{&ca, &ga}, |
| 642 | } |
| 643 | if libSourceTarget, err := generateBazelTarget(ctx, libTestSource); err == nil { |
| 644 | retTargets = append(retTargets, libSourceTarget) |
| 645 | } else { |
| 646 | retErrs = append(retErrs, err) |
| 647 | } |
| 648 | |
| 649 | // Create a go_test target |
| 650 | gp := goTestProperties{ |
| 651 | name: g.Name() + "-test", |
| 652 | dir: ctx.ModuleDir(g), |
| 653 | testSrcs: g.TestSrcs(), |
| 654 | linuxTestSrcs: g.LinuxTestSrcs(), |
| 655 | darwinTestSrcs: g.DarwinTestSrcs(), |
| 656 | testData: g.TestData(), |
| 657 | // embed the go_source in the test |
| 658 | embedName: g.Name() + "-source", |
| 659 | } |
| 660 | if libTestTarget, err := generateBazelTargetsGoTest(ctx, goModulesMap, gp); err == nil { |
| 661 | retTargets = append(retTargets, libTestTarget) |
| 662 | } else { |
| 663 | retErrs = append(retErrs, err) |
| 664 | } |
| 665 | |
| 666 | } |
| 667 | |
| 668 | // Create a go_binary target |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 669 | ga := goAttributes{ |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 670 | Deps: goDepLabels(transitiveDeps, goModulesMap), |
| 671 | Target_compatible_with: targetNotCompatibleWithAndroid(), |
| 672 | } |
| 673 | |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 674 | // If the binary has testSrcs, embed the common `go_source` |
| 675 | if goSource != "" { |
| 676 | ga.Embed = bazel.MakeLabelListAttribute( |
| 677 | bazel.MakeLabelList( |
| 678 | []bazel.Label{bazel.Label{Label: ":" + goSource}}, |
| 679 | ), |
| 680 | ) |
| 681 | } else { |
| 682 | ga.Srcs = goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()) |
| 683 | } |
| 684 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 685 | bin := bTarget{ |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 686 | targetName: g.Name(), |
| 687 | targetPackage: ctx.ModuleDir(g), |
| 688 | bazelRuleClass: "go_binary", |
| 689 | bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl", |
| 690 | bazelAttributes: []interface{}{&ca, &ga}, |
| 691 | } |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 692 | |
| 693 | if binTarget, err := generateBazelTarget(ctx, bin); err == nil { |
| 694 | retTargets = append(retTargets, binTarget) |
| 695 | } else { |
| 696 | retErrs = []error{err} |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 697 | } |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 698 | |
| 699 | return retTargets, retErrs |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 702 | func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) { |
usta | aaf2fd1 | 2023-07-01 11:40:36 -0400 | [diff] [blame] | 703 | ctx.Context().BeginEvent("GenerateBazelTargets") |
| 704 | defer ctx.Context().EndEvent("GenerateBazelTargets") |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 705 | buildFileToTargets := make(map[string]BazelTargets) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 706 | |
| 707 | // Simple metrics tracking for bp2build |
usta | 4f5d2c1 | 2022-10-28 23:32:01 -0400 | [diff] [blame] | 708 | metrics := CreateCodegenMetrics() |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 709 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 710 | dirs := make(map[string]bool) |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 711 | moduleNameToPartition := make(map[string]string) |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 712 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 713 | var errs []error |
| 714 | |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 715 | // Visit go libraries in a pre-run and store its state in a map |
| 716 | // The time complexity remains O(N), and this does not add significant wall time. |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 717 | meta := createBuildConversionMetadata(ctx.Context()) |
| 718 | nameToGoLibMap := meta.nameToGoLibraryModule |
| 719 | ndkHeaders := meta.ndkHeaders |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 720 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 721 | bpCtx := ctx.Context() |
| 722 | bpCtx.VisitAllModules(func(m blueprint.Module) { |
| 723 | dir := bpCtx.ModuleDir(m) |
Chris Parsons | 492bd91 | 2022-01-20 12:55:05 -0500 | [diff] [blame] | 724 | moduleType := bpCtx.ModuleType(m) |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 725 | dirs[dir] = true |
| 726 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 727 | var targets []BazelTarget |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 728 | var targetErrs []error |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 729 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 730 | switch ctx.Mode() { |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 731 | case Bp2Build: |
Chris Parsons | 0c4de1f | 2023-09-21 20:36:35 +0000 | [diff] [blame] | 732 | if aModule, ok := m.(android.Module); ok { |
| 733 | reason := aModule.GetUnconvertedReason() |
| 734 | if reason != nil { |
| 735 | // If this module was force-enabled, cause an error. |
| 736 | if _, ok := ctx.Config().BazelModulesForceEnabledByFlag()[m.Name()]; ok && m.Name() != "" { |
| 737 | err := fmt.Errorf("Force Enabled Module %s not converted", m.Name()) |
| 738 | errs = append(errs, err) |
| 739 | } |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 740 | |
Chris Parsons | 0c4de1f | 2023-09-21 20:36:35 +0000 | [diff] [blame] | 741 | // Log the module isn't to be converted by bp2build. |
| 742 | // TODO: b/291598248 - Log handcrafted modules differently than other unconverted modules. |
| 743 | metrics.AddUnconvertedModule(m, moduleType, dir, *reason) |
| 744 | return |
| 745 | } |
| 746 | if len(aModule.Bp2buildTargets()) == 0 { |
| 747 | panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", aModule.Name())) |
| 748 | } |
| 749 | |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 750 | // Handle modules converted to generated targets. |
Chris Parsons | 0c4de1f | 2023-09-21 20:36:35 +0000 | [diff] [blame] | 751 | targets, targetErrs = generateBazelTargets(bpCtx, aModule) |
| 752 | errs = append(errs, targetErrs...) |
| 753 | for _, t := range targets { |
| 754 | // A module can potentially generate more than 1 Bazel |
| 755 | // target, each of a different rule class. |
| 756 | metrics.IncrementRuleClassCount(t.ruleClass) |
| 757 | } |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 758 | |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 759 | // record the partition |
| 760 | moduleNameToPartition[android.RemoveOptionalPrebuiltPrefix(aModule.Name())] = aModule.GetPartitionForBp2build() |
| 761 | |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 762 | // Log the module. |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 763 | metrics.AddConvertedModule(aModule, moduleType, dir) |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 764 | |
| 765 | // Handle modules with unconverted deps. By default, emit a warning. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 766 | if unconvertedDeps := aModule.GetUnconvertedBp2buildDeps(); len(unconvertedDeps) > 0 { |
Sasha Smundak | f2bb26f | 2022-08-04 11:28:15 -0700 | [diff] [blame] | 767 | msg := fmt.Sprintf("%s %s:%s depends on unconverted modules: %s", |
| 768 | moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", ")) |
Usta Shrestha | c605715 | 2022-09-24 00:23:31 -0400 | [diff] [blame] | 769 | switch ctx.unconvertedDepMode { |
| 770 | case warnUnconvertedDeps: |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 771 | metrics.moduleWithUnconvertedDepsMsgs = append(metrics.moduleWithUnconvertedDepsMsgs, msg) |
Usta Shrestha | c605715 | 2022-09-24 00:23:31 -0400 | [diff] [blame] | 772 | case errorModulesUnconvertedDeps: |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 773 | errs = append(errs, fmt.Errorf(msg)) |
| 774 | return |
| 775 | } |
| 776 | } |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 777 | if unconvertedDeps := aModule.GetMissingBp2buildDeps(); len(unconvertedDeps) > 0 { |
Sasha Smundak | f2bb26f | 2022-08-04 11:28:15 -0700 | [diff] [blame] | 778 | msg := fmt.Sprintf("%s %s:%s depends on missing modules: %s", |
| 779 | moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", ")) |
Usta Shrestha | c605715 | 2022-09-24 00:23:31 -0400 | [diff] [blame] | 780 | switch ctx.unconvertedDepMode { |
| 781 | case warnUnconvertedDeps: |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 782 | metrics.moduleWithMissingDepsMsgs = append(metrics.moduleWithMissingDepsMsgs, msg) |
Usta Shrestha | c605715 | 2022-09-24 00:23:31 -0400 | [diff] [blame] | 783 | case errorModulesUnconvertedDeps: |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 784 | errs = append(errs, fmt.Errorf(msg)) |
| 785 | return |
| 786 | } |
| 787 | } |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 788 | } else if glib, ok := m.(*bootstrap.GoPackage); ok { |
| 789 | targets, targetErrs = generateBazelTargetsGoPackage(bpCtx, glib, nameToGoLibMap) |
| 790 | errs = append(errs, targetErrs...) |
Liz Kammer | 15d7b0b | 2023-09-27 09:38:41 -0400 | [diff] [blame] | 791 | metrics.IncrementRuleClassCount("bootstrap_go_package") |
| 792 | metrics.AddConvertedModule(glib, "bootstrap_go_package", dir) |
Spandan Das | 2a55cea | 2023-06-14 17:56:10 +0000 | [diff] [blame] | 793 | } else if gbin, ok := m.(*bootstrap.GoBinary); ok { |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 794 | targets, targetErrs = generateBazelTargetsGoBinary(bpCtx, gbin, nameToGoLibMap) |
| 795 | errs = append(errs, targetErrs...) |
Liz Kammer | 15d7b0b | 2023-09-27 09:38:41 -0400 | [diff] [blame] | 796 | metrics.IncrementRuleClassCount("blueprint_go_binary") |
| 797 | metrics.AddConvertedModule(gbin, "blueprint_go_binary", dir) |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 798 | } else { |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 799 | metrics.AddUnconvertedModule(m, moduleType, dir, android.UnconvertedReason{ |
| 800 | ReasonType: int(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED), |
| 801 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 802 | return |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 803 | } |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 804 | case QueryView: |
Jingwen Chen | 96af35b | 2021-02-08 00:49:32 -0500 | [diff] [blame] | 805 | // Blocklist certain module types from being generated. |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 806 | if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" { |
Jingwen Chen | 96af35b | 2021-02-08 00:49:32 -0500 | [diff] [blame] | 807 | // package module name contain slashes, and thus cannot |
| 808 | // be mapped cleanly to a bazel label. |
| 809 | return |
| 810 | } |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 811 | t, err := generateSoongModuleTarget(bpCtx, m) |
| 812 | if err != nil { |
| 813 | errs = append(errs, err) |
| 814 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 815 | targets = append(targets, t) |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 816 | default: |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 817 | errs = append(errs, fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode())) |
| 818 | return |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 819 | } |
| 820 | |
Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 821 | for _, target := range targets { |
| 822 | targetDir := target.PackageName() |
| 823 | buildFileToTargets[targetDir] = append(buildFileToTargets[targetDir], target) |
| 824 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 825 | }) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 826 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 827 | // Create an ndk_sysroot target that has a dependency edge on every target corresponding to Soong's ndk_headers |
| 828 | // This root target will provide headers to sdk variants of jni libraries |
| 829 | if ctx.Mode() == Bp2Build { |
| 830 | var depLabels bazel.LabelList |
| 831 | for _, ndkHeader := range ndkHeaders { |
| 832 | depLabel := bazel.Label{ |
| 833 | Label: "//" + bpCtx.ModuleDir(ndkHeader) + ":" + ndkHeader.Name(), |
| 834 | } |
| 835 | depLabels.Add(&depLabel) |
| 836 | } |
| 837 | a := struct { |
Liz Kammer | 96da2ba | 2023-10-13 16:22:24 -0400 | [diff] [blame] | 838 | Deps bazel.LabelListAttribute |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 839 | }{ |
Liz Kammer | 96da2ba | 2023-10-13 16:22:24 -0400 | [diff] [blame] | 840 | Deps: bazel.MakeLabelListAttribute(bazel.UniqueSortedBazelLabelList(depLabels)), |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 841 | } |
| 842 | ndkSysroot := bTarget{ |
| 843 | targetName: "ndk_sysroot", |
| 844 | targetPackage: "build/bazel/rules/cc", // The location is subject to change, use build/bazel for now |
| 845 | bazelRuleClass: "cc_library_headers", |
| 846 | bazelRuleLoadLocation: "//build/bazel/rules/cc:cc_library_headers.bzl", |
| 847 | bazelAttributes: []interface{}{&a}, |
| 848 | } |
| 849 | |
| 850 | if t, err := generateBazelTarget(bpCtx, ndkSysroot); err == nil { |
| 851 | dir := ndkSysroot.targetPackage |
| 852 | buildFileToTargets[dir] = append(buildFileToTargets[dir], t) |
| 853 | } else { |
| 854 | errs = append(errs, err) |
| 855 | } |
| 856 | } |
| 857 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 858 | if len(errs) > 0 { |
| 859 | return conversionResults{}, errs |
| 860 | } |
| 861 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 862 | if generateFilegroups { |
| 863 | // Add a filegroup target that exposes all sources in the subtree of this package |
| 864 | // NOTE: This also means we generate a BUILD file for every Android.bp file (as long as it has at least one module) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 865 | // |
| 866 | // This works because: https://bazel.build/reference/be/functions#exports_files |
| 867 | // "As a legacy behaviour, also files mentioned as input to a rule are exported with the |
| 868 | // default visibility until the flag --incompatible_no_implicit_file_export is flipped. However, this behavior |
| 869 | // should not be relied upon and actively migrated away from." |
| 870 | // |
| 871 | // TODO(b/198619163): We should change this to export_files(glob(["**/*"])) instead, but doing that causes these errors: |
| 872 | // "Error in exports_files: generated label '//external/avb:avbtool' conflicts with existing py_binary rule" |
| 873 | // So we need to solve all the "target ... is both a rule and a file" warnings first. |
Usta Shrestha | c605715 | 2022-09-24 00:23:31 -0400 | [diff] [blame] | 874 | for dir := range dirs { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 875 | buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{ |
| 876 | name: "bp2build_all_srcs", |
Jingwen Chen | 5802d07 | 2023-09-20 10:25:09 +0000 | [diff] [blame] | 877 | content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]), tags = ["manual"])`, |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 878 | ruleClass: "filegroup", |
| 879 | }) |
| 880 | } |
| 881 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 882 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 883 | return conversionResults{ |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame^] | 884 | buildFileToTargets: buildFileToTargets, |
| 885 | moduleNameToPartition: moduleNameToPartition, |
| 886 | metrics: metrics, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 887 | }, errs |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 888 | } |
| 889 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 890 | func generateBazelTargets(ctx bpToBuildContext, m android.Module) ([]BazelTarget, []error) { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 891 | var targets []BazelTarget |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 892 | var errs []error |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 893 | for _, m := range m.Bp2buildTargets() { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 894 | target, err := generateBazelTarget(ctx, m) |
| 895 | if err != nil { |
| 896 | errs = append(errs, err) |
| 897 | return targets, errs |
| 898 | } |
| 899 | targets = append(targets, target) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 900 | } |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 901 | return targets, errs |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | type bp2buildModule interface { |
| 905 | TargetName() string |
| 906 | TargetPackage() string |
| 907 | BazelRuleClass() string |
| 908 | BazelRuleLoadLocation() string |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 909 | BazelAttributes() []interface{} |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 910 | } |
| 911 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 912 | func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) (BazelTarget, error) { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 913 | ruleClass := m.BazelRuleClass() |
| 914 | bzlLoadLocation := m.BazelRuleLoadLocation() |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 915 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 916 | // extract the bazel attributes from the module. |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 917 | attrs := m.BazelAttributes() |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 918 | props, err := extractModuleProperties(attrs, true) |
| 919 | if err != nil { |
| 920 | return BazelTarget{}, err |
| 921 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 922 | |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 923 | // name is handled in a special manner |
| 924 | delete(props.Attrs, "name") |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 925 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 926 | // Return the Bazel target with rule class and attributes, ready to be |
| 927 | // code-generated. |
| 928 | attributes := propsToAttributes(props.Attrs) |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 929 | var content string |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 930 | targetName := m.TargetName() |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 931 | if targetName != "" { |
| 932 | content = fmt.Sprintf(ruleTargetTemplate, ruleClass, targetName, attributes) |
| 933 | } else { |
| 934 | content = fmt.Sprintf(unnamedRuleTargetTemplate, ruleClass, attributes) |
| 935 | } |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 936 | var loads []BazelLoad |
| 937 | if bzlLoadLocation != "" { |
| 938 | loads = append(loads, BazelLoad{ |
| 939 | file: bzlLoadLocation, |
| 940 | symbols: []BazelLoadSymbol{{symbol: ruleClass}}, |
| 941 | }) |
| 942 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 943 | return BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 944 | name: targetName, |
| 945 | packageName: m.TargetPackage(), |
| 946 | ruleClass: ruleClass, |
| 947 | loads: loads, |
| 948 | content: content, |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 949 | }, nil |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 950 | } |
| 951 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 952 | // Convert a module and its deps and props into a Bazel macro/rule |
| 953 | // representation in the BUILD file. |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 954 | func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) (BazelTarget, error) { |
| 955 | props, err := getBuildProperties(ctx, m) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 956 | |
| 957 | // TODO(b/163018919): DirectDeps can have duplicate (module, variant) |
| 958 | // items, if the modules are added using different DependencyTag. Figure |
| 959 | // out the implications of that. |
| 960 | depLabels := map[string]bool{} |
| 961 | if aModule, ok := m.(android.Module); ok { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 962 | ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 963 | depLabels[qualifiedTargetLabel(ctx, depModule)] = true |
| 964 | }) |
| 965 | } |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 966 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 967 | for p := range ignoredPropNames { |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 968 | delete(props.Attrs, p) |
| 969 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 970 | attributes := propsToAttributes(props.Attrs) |
| 971 | |
| 972 | depLabelList := "[\n" |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 973 | for depLabel := range depLabels { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 974 | depLabelList += fmt.Sprintf(" %q,\n", depLabel) |
| 975 | } |
| 976 | depLabelList += " ]" |
| 977 | |
| 978 | targetName := targetNameWithVariant(ctx, m) |
| 979 | return BazelTarget{ |
Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 980 | name: targetName, |
| 981 | packageName: ctx.ModuleDir(m), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 982 | content: fmt.Sprintf( |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 983 | soongModuleTargetTemplate, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 984 | targetName, |
| 985 | ctx.ModuleName(m), |
| 986 | canonicalizeModuleType(ctx.ModuleType(m)), |
| 987 | ctx.ModuleSubDir(m), |
| 988 | depLabelList, |
| 989 | attributes), |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 990 | }, err |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 993 | func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) (BazelAttributes, error) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 994 | // TODO: this omits properties for blueprint modules (blueprint_go_binary, |
| 995 | // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately. |
| 996 | if aModule, ok := m.(android.Module); ok { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 997 | return extractModuleProperties(aModule.GetProperties(), false) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1000 | return BazelAttributes{}, nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | // Generically extract module properties and types into a map, keyed by the module property name. |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1004 | func extractModuleProperties(props []interface{}, checkForDuplicateProperties bool) (BazelAttributes, error) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1005 | ret := map[string]string{} |
| 1006 | |
| 1007 | // Iterate over this android.Module's property structs. |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1008 | for _, properties := range props { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1009 | propertiesValue := reflect.ValueOf(properties) |
| 1010 | // Check that propertiesValue is a pointer to the Properties struct, like |
| 1011 | // *cc.BaseLinkerProperties or *java.CompilerProperties. |
| 1012 | // |
| 1013 | // propertiesValue can also be type-asserted to the structs to |
| 1014 | // manipulate internal props, if needed. |
| 1015 | if isStructPtr(propertiesValue.Type()) { |
| 1016 | structValue := propertiesValue.Elem() |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1017 | ok, err := extractStructProperties(structValue, 0) |
| 1018 | if err != nil { |
| 1019 | return BazelAttributes{}, err |
| 1020 | } |
| 1021 | for k, v := range ok { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1022 | if existing, exists := ret[k]; checkForDuplicateProperties && exists { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1023 | return BazelAttributes{}, fmt.Errorf( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1024 | "%s (%v) is present in properties whereas it should be consolidated into a commonAttributes", |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1025 | k, existing) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1026 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1027 | ret[k] = v |
| 1028 | } |
| 1029 | } else { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1030 | return BazelAttributes{}, |
| 1031 | fmt.Errorf( |
| 1032 | "properties must be a pointer to a struct, got %T", |
| 1033 | propertiesValue.Interface()) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1037 | return BazelAttributes{ |
| 1038 | Attrs: ret, |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1039 | }, nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | func isStructPtr(t reflect.Type) bool { |
| 1043 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct |
| 1044 | } |
| 1045 | |
| 1046 | // prettyPrint a property value into the equivalent Starlark representation |
| 1047 | // recursively. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 1048 | func prettyPrint(propertyValue reflect.Value, indent int, emitZeroValues bool) (string, error) { |
| 1049 | if !emitZeroValues && isZero(propertyValue) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1050 | // A property value being set or unset actually matters -- Soong does set default |
| 1051 | // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at |
| 1052 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480 |
| 1053 | // |
Jingwen Chen | fc490bd | 2021-03-30 10:24:19 +0000 | [diff] [blame] | 1054 | // In Bazel-parlance, we would use "attr.<type>(default = <default |
| 1055 | // value>)" to set the default value of unset attributes. In the cases |
| 1056 | // where the bp2build converter didn't set the default value within the |
| 1057 | // mutator when creating the BazelTargetModule, this would be a zero |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 1058 | // value. For those cases, we return an empty string so we don't |
| 1059 | // unnecessarily generate empty values. |
| 1060 | return "", nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1063 | switch propertyValue.Kind() { |
| 1064 | case reflect.String: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 1065 | return fmt.Sprintf("\"%v\"", escapeString(propertyValue.String())), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1066 | case reflect.Bool: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 1067 | return starlark_fmt.PrintBool(propertyValue.Bool()), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1068 | case reflect.Int, reflect.Uint, reflect.Int64: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 1069 | return fmt.Sprintf("%v", propertyValue.Interface()), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1070 | case reflect.Ptr: |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 1071 | return prettyPrint(propertyValue.Elem(), indent, emitZeroValues) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1072 | case reflect.Slice: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 1073 | elements := make([]string, 0, propertyValue.Len()) |
| 1074 | for i := 0; i < propertyValue.Len(); i++ { |
| 1075 | val, err := prettyPrint(propertyValue.Index(i), indent, emitZeroValues) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1076 | if err != nil { |
| 1077 | return "", err |
| 1078 | } |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 1079 | if val != "" { |
| 1080 | elements = append(elements, val) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1081 | } |
| 1082 | } |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 1083 | return starlark_fmt.PrintList(elements, indent, func(s string) string { |
| 1084 | return "%s" |
| 1085 | }), nil |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 1086 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1087 | case reflect.Struct: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 1088 | // Special cases where the bp2build sends additional information to the codegenerator |
| 1089 | // by wrapping the attributes in a custom struct type. |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 1090 | if attr, ok := propertyValue.Interface().(bazel.Attribute); ok { |
| 1091 | return prettyPrintAttribute(attr, indent) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1092 | } else if label, ok := propertyValue.Interface().(bazel.Label); ok { |
| 1093 | return fmt.Sprintf("%q", label.Label), nil |
| 1094 | } |
| 1095 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1096 | // Sort and print the struct props by the key. |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1097 | structProps, err := extractStructProperties(propertyValue, indent) |
| 1098 | |
| 1099 | if err != nil { |
| 1100 | return "", err |
| 1101 | } |
| 1102 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1103 | if len(structProps) == 0 { |
| 1104 | return "", nil |
| 1105 | } |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 1106 | return starlark_fmt.PrintDict(structProps, indent), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1107 | case reflect.Interface: |
| 1108 | // TODO(b/164227191): implement pretty print for interfaces. |
| 1109 | // Interfaces are used for for arch, multilib and target properties. |
| 1110 | return "", nil |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 1111 | case reflect.Map: |
| 1112 | if v, ok := propertyValue.Interface().(bazel.StringMapAttribute); ok { |
| 1113 | return starlark_fmt.PrintStringStringDict(v, indent), nil |
| 1114 | } |
| 1115 | return "", fmt.Errorf("bp2build expects map of type map[string]string for field: %s", propertyValue) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1116 | default: |
| 1117 | return "", fmt.Errorf( |
| 1118 | "unexpected kind for property struct field: %s", propertyValue.Kind()) |
| 1119 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // Converts a reflected property struct value into a map of property names and property values, |
| 1123 | // which each property value correctly pretty-printed and indented at the right nest level, |
| 1124 | // since property structs can be nested. In Starlark, nested structs are represented as nested |
| 1125 | // dicts: https://docs.bazel.build/skylark/lib/dict.html |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1126 | func extractStructProperties(structValue reflect.Value, indent int) (map[string]string, error) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1127 | if structValue.Kind() != reflect.Struct { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1128 | return map[string]string{}, fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind()) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1131 | var err error |
| 1132 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1133 | ret := map[string]string{} |
| 1134 | structType := structValue.Type() |
| 1135 | for i := 0; i < structValue.NumField(); i++ { |
| 1136 | field := structType.Field(i) |
| 1137 | if shouldSkipStructField(field) { |
| 1138 | continue |
| 1139 | } |
| 1140 | |
| 1141 | fieldValue := structValue.Field(i) |
| 1142 | if isZero(fieldValue) { |
| 1143 | // Ignore zero-valued fields |
| 1144 | continue |
| 1145 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 1146 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 1147 | // if the struct is embedded (anonymous), flatten the properties into the containing struct |
| 1148 | if field.Anonymous { |
| 1149 | if field.Type.Kind() == reflect.Ptr { |
| 1150 | fieldValue = fieldValue.Elem() |
| 1151 | } |
| 1152 | if fieldValue.Type().Kind() == reflect.Struct { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1153 | propsToMerge, err := extractStructProperties(fieldValue, indent) |
| 1154 | if err != nil { |
| 1155 | return map[string]string{}, err |
| 1156 | } |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 1157 | for prop, value := range propsToMerge { |
| 1158 | ret[prop] = value |
| 1159 | } |
| 1160 | continue |
| 1161 | } |
| 1162 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1163 | |
| 1164 | propertyName := proptools.PropertyNameForField(field.Name) |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1165 | var prettyPrintedValue string |
| 1166 | prettyPrintedValue, err = prettyPrint(fieldValue, indent+1, false) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1167 | if err != nil { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1168 | return map[string]string{}, fmt.Errorf( |
| 1169 | "Error while parsing property: %q. %s", |
| 1170 | propertyName, |
| 1171 | err) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1172 | } |
| 1173 | if prettyPrintedValue != "" { |
| 1174 | ret[propertyName] = prettyPrintedValue |
| 1175 | } |
| 1176 | } |
| 1177 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 1178 | return ret, nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | func isZero(value reflect.Value) bool { |
| 1182 | switch value.Kind() { |
| 1183 | case reflect.Func, reflect.Map, reflect.Slice: |
| 1184 | return value.IsNil() |
| 1185 | case reflect.Array: |
| 1186 | valueIsZero := true |
| 1187 | for i := 0; i < value.Len(); i++ { |
| 1188 | valueIsZero = valueIsZero && isZero(value.Index(i)) |
| 1189 | } |
| 1190 | return valueIsZero |
| 1191 | case reflect.Struct: |
| 1192 | valueIsZero := true |
| 1193 | for i := 0; i < value.NumField(); i++ { |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 1194 | valueIsZero = valueIsZero && isZero(value.Field(i)) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1195 | } |
| 1196 | return valueIsZero |
| 1197 | case reflect.Ptr: |
| 1198 | if !value.IsNil() { |
| 1199 | return isZero(reflect.Indirect(value)) |
| 1200 | } else { |
| 1201 | return true |
| 1202 | } |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 1203 | // Always print bool/strings, if you want a bool/string attribute to be able to take the default value, use a |
| 1204 | // pointer instead |
| 1205 | case reflect.Bool, reflect.String: |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 1206 | return false |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1207 | default: |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 1208 | if !value.IsValid() { |
| 1209 | return true |
| 1210 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1211 | zeroValue := reflect.Zero(value.Type()) |
| 1212 | result := value.Interface() == zeroValue.Interface() |
| 1213 | return result |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | func escapeString(s string) string { |
| 1218 | s = strings.ReplaceAll(s, "\\", "\\\\") |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 1219 | |
| 1220 | // b/184026959: Reverse the application of some common control sequences. |
| 1221 | // These must be generated literally in the BUILD file. |
| 1222 | s = strings.ReplaceAll(s, "\t", "\\t") |
| 1223 | s = strings.ReplaceAll(s, "\n", "\\n") |
| 1224 | s = strings.ReplaceAll(s, "\r", "\\r") |
| 1225 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1226 | return strings.ReplaceAll(s, "\"", "\\\"") |
| 1227 | } |
| 1228 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1229 | func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string { |
| 1230 | name := "" |
| 1231 | if c.ModuleSubDir(logicModule) != "" { |
| 1232 | // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes. |
| 1233 | name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule) |
| 1234 | } else { |
| 1235 | name = c.ModuleName(logicModule) |
| 1236 | } |
| 1237 | |
| 1238 | return strings.Replace(name, "//", "", 1) |
| 1239 | } |
| 1240 | |
| 1241 | func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string { |
| 1242 | return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule)) |
| 1243 | } |