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" |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 29 | "android/soong/starlark_fmt" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 30 | "github.com/google/blueprint" |
| 31 | "github.com/google/blueprint/proptools" |
| 32 | ) |
| 33 | |
| 34 | type BazelAttributes struct { |
| 35 | Attrs map[string]string |
| 36 | } |
| 37 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 38 | type BazelLoadSymbol struct { |
| 39 | // The name of the symbol in the file being loaded |
| 40 | symbol string |
| 41 | // The name the symbol wil have in this file. Can be left blank to use the same name as symbol. |
| 42 | alias string |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 45 | type BazelLoad struct { |
| 46 | file string |
| 47 | symbols []BazelLoadSymbol |
| 48 | } |
| 49 | |
| 50 | type BazelTarget struct { |
| 51 | name string |
| 52 | packageName string |
| 53 | content string |
| 54 | ruleClass string |
| 55 | loads []BazelLoad |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 58 | // Label is the fully qualified Bazel label constructed from the BazelTarget's |
| 59 | // package name and target name. |
| 60 | func (t BazelTarget) Label() string { |
| 61 | if t.packageName == "." { |
| 62 | return "//:" + t.name |
| 63 | } else { |
| 64 | return "//" + t.packageName + ":" + t.name |
| 65 | } |
| 66 | } |
| 67 | |
Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 68 | // PackageName returns the package of the Bazel target. |
| 69 | // Defaults to root of tree. |
| 70 | func (t BazelTarget) PackageName() string { |
| 71 | if t.packageName == "" { |
| 72 | return "." |
| 73 | } |
| 74 | return t.packageName |
| 75 | } |
| 76 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 77 | // BazelTargets is a typedef for a slice of BazelTarget objects. |
| 78 | type BazelTargets []BazelTarget |
| 79 | |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 80 | func (targets BazelTargets) packageRule() *BazelTarget { |
| 81 | for _, target := range targets { |
| 82 | if target.ruleClass == "package" { |
| 83 | return &target |
| 84 | } |
| 85 | } |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | // 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] | 90 | func (targets BazelTargets) sort() { |
| 91 | sort.Slice(targets, func(i, j int) bool { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 92 | return targets[i].name < targets[j].name |
| 93 | }) |
| 94 | } |
| 95 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 96 | // String returns the string representation of BazelTargets, without load |
| 97 | // statements (use LoadStatements for that), since the targets are usually not |
| 98 | // adjacent to the load statements at the top of the BUILD file. |
| 99 | func (targets BazelTargets) String() string { |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 100 | var res strings.Builder |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 101 | for i, target := range targets { |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 102 | if target.ruleClass != "package" { |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 103 | res.WriteString(target.content) |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 104 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 105 | if i != len(targets)-1 { |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 106 | res.WriteString("\n\n") |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
usta | da2a211 | 2023-08-08 00:29:08 -0400 | [diff] [blame] | 109 | return res.String() |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // LoadStatements return the string representation of the sorted and deduplicated |
| 113 | // Starlark rule load statements needed by a group of BazelTargets. |
| 114 | func (targets BazelTargets) LoadStatements() string { |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 115 | // First, merge all the load statements from all the targets onto one list |
| 116 | bzlToLoadedSymbols := map[string][]BazelLoadSymbol{} |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 117 | for _, target := range targets { |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 118 | for _, load := range target.loads { |
| 119 | outer: |
| 120 | for _, symbol := range load.symbols { |
| 121 | alias := symbol.alias |
| 122 | if alias == "" { |
| 123 | alias = symbol.symbol |
| 124 | } |
| 125 | for _, otherSymbol := range bzlToLoadedSymbols[load.file] { |
| 126 | otherAlias := otherSymbol.alias |
| 127 | if otherAlias == "" { |
| 128 | otherAlias = otherSymbol.symbol |
| 129 | } |
| 130 | if symbol.symbol == otherSymbol.symbol && alias == otherAlias { |
| 131 | continue outer |
| 132 | } else if alias == otherAlias { |
| 133 | panic(fmt.Sprintf("Conflicting destination (%s) for loads of %s and %s", alias, symbol.symbol, otherSymbol.symbol)) |
| 134 | } |
| 135 | } |
| 136 | bzlToLoadedSymbols[load.file] = append(bzlToLoadedSymbols[load.file], symbol) |
| 137 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 141 | var loadStatements strings.Builder |
| 142 | for i, bzl := range android.SortedKeys(bzlToLoadedSymbols) { |
| 143 | symbols := bzlToLoadedSymbols[bzl] |
| 144 | loadStatements.WriteString("load(\"") |
| 145 | loadStatements.WriteString(bzl) |
| 146 | loadStatements.WriteString("\", ") |
| 147 | sort.Slice(symbols, func(i, j int) bool { |
| 148 | if symbols[i].symbol < symbols[j].symbol { |
| 149 | return true |
| 150 | } |
| 151 | return symbols[i].alias < symbols[j].alias |
| 152 | }) |
| 153 | for j, symbol := range symbols { |
| 154 | if symbol.alias != "" && symbol.alias != symbol.symbol { |
| 155 | loadStatements.WriteString(symbol.alias) |
| 156 | loadStatements.WriteString(" = ") |
| 157 | } |
| 158 | loadStatements.WriteString("\"") |
| 159 | loadStatements.WriteString(symbol.symbol) |
| 160 | loadStatements.WriteString("\"") |
| 161 | if j != len(symbols)-1 { |
| 162 | loadStatements.WriteString(", ") |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 165 | loadStatements.WriteString(")") |
| 166 | if i != len(bzlToLoadedSymbols)-1 { |
| 167 | loadStatements.WriteString("\n") |
| 168 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 169 | } |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 170 | return loadStatements.String() |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | type bpToBuildContext interface { |
| 174 | ModuleName(module blueprint.Module) string |
| 175 | ModuleDir(module blueprint.Module) string |
| 176 | ModuleSubDir(module blueprint.Module) string |
| 177 | ModuleType(module blueprint.Module) string |
| 178 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 179 | VisitAllModules(visit func(blueprint.Module)) |
| 180 | VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module)) |
| 181 | } |
| 182 | |
| 183 | type CodegenContext struct { |
Colin Cross | b8083bb | 2024-10-02 16:07:43 -0700 | [diff] [blame] | 184 | config android.Config |
| 185 | context *android.Context |
| 186 | mode CodegenMode |
| 187 | additionalDeps []string |
| 188 | topDir string |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 191 | func (ctx *CodegenContext) Mode() CodegenMode { |
| 192 | return ctx.mode |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 195 | // CodegenMode is an enum to differentiate code-generation modes. |
| 196 | type CodegenMode int |
| 197 | |
| 198 | const ( |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 199 | // QueryView - generate BUILD files with targets representing fully mutated |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 200 | // Soong modules, representing the fully configured Soong module graph with |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 201 | // variants and dependency edges. |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 202 | // |
| 203 | // This mode is used for discovering and introspecting the existing Soong |
| 204 | // module graph. |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 205 | QueryView CodegenMode = iota |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 206 | ) |
| 207 | |
Jingwen Chen | dcc329a | 2021-01-26 02:49:03 -0500 | [diff] [blame] | 208 | func (mode CodegenMode) String() string { |
| 209 | switch mode { |
Jingwen Chen | dcc329a | 2021-01-26 02:49:03 -0500 | [diff] [blame] | 210 | case QueryView: |
| 211 | return "QueryView" |
| 212 | default: |
| 213 | return fmt.Sprintf("%d", mode) |
| 214 | } |
| 215 | } |
| 216 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 217 | // AddNinjaFileDeps adds dependencies on the specified files to be added to the ninja manifest. The |
| 218 | // primary builder will be rerun whenever the specified files are modified. Allows us to fulfill the |
| 219 | // PathContext interface in order to add dependencies on hand-crafted BUILD files. Note: must also |
| 220 | // call AdditionalNinjaDeps and add them manually to the ninja file. |
| 221 | func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) { |
| 222 | ctx.additionalDeps = append(ctx.additionalDeps, deps...) |
| 223 | } |
| 224 | |
| 225 | // AdditionalNinjaDeps returns additional ninja deps added by CodegenContext |
| 226 | func (ctx *CodegenContext) AdditionalNinjaDeps() []string { |
| 227 | return ctx.additionalDeps |
| 228 | } |
| 229 | |
Paul Duffin | c639059 | 2022-11-04 13:35:21 +0000 | [diff] [blame] | 230 | func (ctx *CodegenContext) Config() android.Config { return ctx.config } |
| 231 | func (ctx *CodegenContext) Context() *android.Context { return ctx.context } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 232 | |
| 233 | // NewCodegenContext creates a wrapper context that conforms to PathContext for |
| 234 | // writing BUILD files in the output directory. |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 235 | func NewCodegenContext(config android.Config, context *android.Context, mode CodegenMode, topDir string) *CodegenContext { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 236 | return &CodegenContext{ |
Colin Cross | b8083bb | 2024-10-02 16:07:43 -0700 | [diff] [blame] | 237 | context: context, |
| 238 | config: config, |
| 239 | mode: mode, |
| 240 | topDir: topDir, |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 241 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // props is an unsorted map. This function ensures that |
| 245 | // the generated attributes are sorted to ensure determinism. |
| 246 | func propsToAttributes(props map[string]string) string { |
| 247 | var attributes string |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 248 | for _, propName := range android.SortedKeys(props) { |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 249 | attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName]) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 250 | } |
| 251 | return attributes |
| 252 | } |
| 253 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 254 | type conversionResults struct { |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 255 | buildFileToTargets map[string]BazelTargets |
| 256 | moduleNameToPartition map[string]string |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | func (r conversionResults) BuildDirToTargets() map[string]BazelTargets { |
| 260 | return r.buildFileToTargets |
| 261 | } |
| 262 | |
| 263 | func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) { |
usta | aaf2fd1 | 2023-07-01 11:40:36 -0400 | [diff] [blame] | 264 | ctx.Context().BeginEvent("GenerateBazelTargets") |
| 265 | defer ctx.Context().EndEvent("GenerateBazelTargets") |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 266 | buildFileToTargets := make(map[string]BazelTargets) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 267 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 268 | dirs := make(map[string]bool) |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 269 | moduleNameToPartition := make(map[string]string) |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 270 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 271 | var errs []error |
| 272 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 273 | bpCtx := ctx.Context() |
| 274 | bpCtx.VisitAllModules(func(m blueprint.Module) { |
| 275 | dir := bpCtx.ModuleDir(m) |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 276 | dirs[dir] = true |
| 277 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 278 | var targets []BazelTarget |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 279 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 280 | switch ctx.Mode() { |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 281 | case QueryView: |
Jingwen Chen | 96af35b | 2021-02-08 00:49:32 -0500 | [diff] [blame] | 282 | // Blocklist certain module types from being generated. |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 283 | if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" { |
Jingwen Chen | 96af35b | 2021-02-08 00:49:32 -0500 | [diff] [blame] | 284 | // package module name contain slashes, and thus cannot |
| 285 | // be mapped cleanly to a bazel label. |
| 286 | return |
| 287 | } |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 288 | t, err := generateSoongModuleTarget(bpCtx, m) |
| 289 | if err != nil { |
| 290 | errs = append(errs, err) |
| 291 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 292 | targets = append(targets, t) |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 293 | default: |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 294 | errs = append(errs, fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode())) |
| 295 | return |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 298 | for _, target := range targets { |
| 299 | targetDir := target.PackageName() |
| 300 | buildFileToTargets[targetDir] = append(buildFileToTargets[targetDir], target) |
| 301 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 302 | }) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 303 | |
| 304 | if len(errs) > 0 { |
| 305 | return conversionResults{}, errs |
| 306 | } |
| 307 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 308 | if generateFilegroups { |
| 309 | // Add a filegroup target that exposes all sources in the subtree of this package |
| 310 | // 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] | 311 | // |
| 312 | // This works because: https://bazel.build/reference/be/functions#exports_files |
| 313 | // "As a legacy behaviour, also files mentioned as input to a rule are exported with the |
| 314 | // default visibility until the flag --incompatible_no_implicit_file_export is flipped. However, this behavior |
| 315 | // should not be relied upon and actively migrated away from." |
| 316 | // |
| 317 | // TODO(b/198619163): We should change this to export_files(glob(["**/*"])) instead, but doing that causes these errors: |
| 318 | // "Error in exports_files: generated label '//external/avb:avbtool' conflicts with existing py_binary rule" |
| 319 | // 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] | 320 | for dir := range dirs { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 321 | buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{ |
| 322 | name: "bp2build_all_srcs", |
Jingwen Chen | 5802d07 | 2023-09-20 10:25:09 +0000 | [diff] [blame] | 323 | content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]), tags = ["manual"])`, |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 324 | ruleClass: "filegroup", |
| 325 | }) |
| 326 | } |
| 327 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 328 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 329 | return conversionResults{ |
Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 330 | buildFileToTargets: buildFileToTargets, |
| 331 | moduleNameToPartition: moduleNameToPartition, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 332 | }, errs |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 335 | // Convert a module and its deps and props into a Bazel macro/rule |
| 336 | // representation in the BUILD file. |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 337 | func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) (BazelTarget, error) { |
| 338 | props, err := getBuildProperties(ctx, m) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 339 | |
| 340 | // TODO(b/163018919): DirectDeps can have duplicate (module, variant) |
| 341 | // items, if the modules are added using different DependencyTag. Figure |
| 342 | // out the implications of that. |
| 343 | depLabels := map[string]bool{} |
| 344 | if aModule, ok := m.(android.Module); ok { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 345 | ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 346 | depLabels[qualifiedTargetLabel(ctx, depModule)] = true |
| 347 | }) |
| 348 | } |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 349 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 350 | for p := range ignoredPropNames { |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 351 | delete(props.Attrs, p) |
| 352 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 353 | attributes := propsToAttributes(props.Attrs) |
| 354 | |
| 355 | depLabelList := "[\n" |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 356 | for depLabel := range depLabels { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 357 | depLabelList += fmt.Sprintf(" %q,\n", depLabel) |
| 358 | } |
| 359 | depLabelList += " ]" |
| 360 | |
| 361 | targetName := targetNameWithVariant(ctx, m) |
| 362 | return BazelTarget{ |
Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 363 | name: targetName, |
| 364 | packageName: ctx.ModuleDir(m), |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 365 | content: fmt.Sprintf( |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 366 | soongModuleTargetTemplate, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 367 | targetName, |
| 368 | ctx.ModuleName(m), |
| 369 | canonicalizeModuleType(ctx.ModuleType(m)), |
| 370 | ctx.ModuleSubDir(m), |
| 371 | depLabelList, |
| 372 | attributes), |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 373 | }, err |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 376 | func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) (BazelAttributes, error) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 377 | // TODO: this omits properties for blueprint modules (blueprint_go_binary, |
| 378 | // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately. |
| 379 | 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] | 380 | return extractModuleProperties(aModule.GetProperties(), false) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 383 | return BazelAttributes{}, nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // 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] | 387 | func extractModuleProperties(props []interface{}, checkForDuplicateProperties bool) (BazelAttributes, error) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 388 | ret := map[string]string{} |
| 389 | |
| 390 | // Iterate over this android.Module's property structs. |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 391 | for _, properties := range props { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 392 | propertiesValue := reflect.ValueOf(properties) |
| 393 | // Check that propertiesValue is a pointer to the Properties struct, like |
| 394 | // *cc.BaseLinkerProperties or *java.CompilerProperties. |
| 395 | // |
| 396 | // propertiesValue can also be type-asserted to the structs to |
| 397 | // manipulate internal props, if needed. |
| 398 | if isStructPtr(propertiesValue.Type()) { |
| 399 | structValue := propertiesValue.Elem() |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 400 | ok, err := extractStructProperties(structValue, 0) |
| 401 | if err != nil { |
| 402 | return BazelAttributes{}, err |
| 403 | } |
| 404 | 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] | 405 | if existing, exists := ret[k]; checkForDuplicateProperties && exists { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 406 | 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] | 407 | "%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] | 408 | k, existing) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 409 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 410 | ret[k] = v |
| 411 | } |
| 412 | } else { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 413 | return BazelAttributes{}, |
| 414 | fmt.Errorf( |
| 415 | "properties must be a pointer to a struct, got %T", |
| 416 | propertiesValue.Interface()) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 420 | return BazelAttributes{ |
| 421 | Attrs: ret, |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 422 | }, nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | func isStructPtr(t reflect.Type) bool { |
| 426 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct |
| 427 | } |
| 428 | |
| 429 | // prettyPrint a property value into the equivalent Starlark representation |
| 430 | // recursively. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 431 | func prettyPrint(propertyValue reflect.Value, indent int, emitZeroValues bool) (string, error) { |
| 432 | if !emitZeroValues && isZero(propertyValue) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 433 | // A property value being set or unset actually matters -- Soong does set default |
| 434 | // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at |
Elliott Hughes | 1036316 | 2024-01-09 22:02:03 +0000 | [diff] [blame] | 435 | // https://cs.android.com/android/platform/superproject/+/main:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480 |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 436 | // |
Jingwen Chen | fc490bd | 2021-03-30 10:24:19 +0000 | [diff] [blame] | 437 | // In Bazel-parlance, we would use "attr.<type>(default = <default |
| 438 | // value>)" to set the default value of unset attributes. In the cases |
| 439 | // where the bp2build converter didn't set the default value within the |
| 440 | // mutator when creating the BazelTargetModule, this would be a zero |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 441 | // value. For those cases, we return an empty string so we don't |
| 442 | // unnecessarily generate empty values. |
| 443 | return "", nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 446 | switch propertyValue.Kind() { |
| 447 | case reflect.String: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 448 | return fmt.Sprintf("\"%v\"", escapeString(propertyValue.String())), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 449 | case reflect.Bool: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 450 | return starlark_fmt.PrintBool(propertyValue.Bool()), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 451 | case reflect.Int, reflect.Uint, reflect.Int64: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 452 | return fmt.Sprintf("%v", propertyValue.Interface()), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 453 | case reflect.Ptr: |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 454 | return prettyPrint(propertyValue.Elem(), indent, emitZeroValues) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 455 | case reflect.Slice: |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 456 | elements := make([]string, 0, propertyValue.Len()) |
| 457 | for i := 0; i < propertyValue.Len(); i++ { |
| 458 | val, err := prettyPrint(propertyValue.Index(i), indent, emitZeroValues) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 459 | if err != nil { |
| 460 | return "", err |
| 461 | } |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 462 | if val != "" { |
| 463 | elements = append(elements, val) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 466 | return starlark_fmt.PrintList(elements, indent, func(s string) string { |
| 467 | return "%s" |
| 468 | }), nil |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 469 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 470 | case reflect.Struct: |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 471 | // Sort and print the struct props by the key. |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 472 | structProps, err := extractStructProperties(propertyValue, indent) |
| 473 | |
| 474 | if err != nil { |
| 475 | return "", err |
| 476 | } |
| 477 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 478 | if len(structProps) == 0 { |
| 479 | return "", nil |
| 480 | } |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 481 | return starlark_fmt.PrintDict(structProps, indent), nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 482 | case reflect.Interface: |
| 483 | // TODO(b/164227191): implement pretty print for interfaces. |
| 484 | // Interfaces are used for for arch, multilib and target properties. |
| 485 | return "", nil |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 486 | case reflect.Map: |
Colin Cross | b8083bb | 2024-10-02 16:07:43 -0700 | [diff] [blame] | 487 | if v, ok := propertyValue.Interface().(map[string]string); ok { |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 488 | return starlark_fmt.PrintStringStringDict(v, indent), nil |
| 489 | } |
| 490 | 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] | 491 | default: |
| 492 | return "", fmt.Errorf( |
| 493 | "unexpected kind for property struct field: %s", propertyValue.Kind()) |
| 494 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Converts a reflected property struct value into a map of property names and property values, |
| 498 | // which each property value correctly pretty-printed and indented at the right nest level, |
| 499 | // since property structs can be nested. In Starlark, nested structs are represented as nested |
| 500 | // dicts: https://docs.bazel.build/skylark/lib/dict.html |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 501 | func extractStructProperties(structValue reflect.Value, indent int) (map[string]string, error) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 502 | if structValue.Kind() != reflect.Struct { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 503 | 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] | 504 | } |
| 505 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 506 | var err error |
| 507 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 508 | ret := map[string]string{} |
| 509 | structType := structValue.Type() |
| 510 | for i := 0; i < structValue.NumField(); i++ { |
| 511 | field := structType.Field(i) |
| 512 | if shouldSkipStructField(field) { |
| 513 | continue |
| 514 | } |
| 515 | |
| 516 | fieldValue := structValue.Field(i) |
| 517 | if isZero(fieldValue) { |
| 518 | // Ignore zero-valued fields |
| 519 | continue |
| 520 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 521 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 522 | // if the struct is embedded (anonymous), flatten the properties into the containing struct |
| 523 | if field.Anonymous { |
| 524 | if field.Type.Kind() == reflect.Ptr { |
| 525 | fieldValue = fieldValue.Elem() |
| 526 | } |
| 527 | if fieldValue.Type().Kind() == reflect.Struct { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 528 | propsToMerge, err := extractStructProperties(fieldValue, indent) |
| 529 | if err != nil { |
| 530 | return map[string]string{}, err |
| 531 | } |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 532 | for prop, value := range propsToMerge { |
| 533 | ret[prop] = value |
| 534 | } |
| 535 | continue |
| 536 | } |
| 537 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 538 | |
| 539 | propertyName := proptools.PropertyNameForField(field.Name) |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 540 | var prettyPrintedValue string |
| 541 | prettyPrintedValue, err = prettyPrint(fieldValue, indent+1, false) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 542 | if err != nil { |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 543 | return map[string]string{}, fmt.Errorf( |
| 544 | "Error while parsing property: %q. %s", |
| 545 | propertyName, |
| 546 | err) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 547 | } |
| 548 | if prettyPrintedValue != "" { |
| 549 | ret[propertyName] = prettyPrintedValue |
| 550 | } |
| 551 | } |
| 552 | |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 553 | return ret, nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | func isZero(value reflect.Value) bool { |
| 557 | switch value.Kind() { |
| 558 | case reflect.Func, reflect.Map, reflect.Slice: |
| 559 | return value.IsNil() |
| 560 | case reflect.Array: |
| 561 | valueIsZero := true |
| 562 | for i := 0; i < value.Len(); i++ { |
| 563 | valueIsZero = valueIsZero && isZero(value.Index(i)) |
| 564 | } |
| 565 | return valueIsZero |
| 566 | case reflect.Struct: |
| 567 | valueIsZero := true |
| 568 | for i := 0; i < value.NumField(); i++ { |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 569 | valueIsZero = valueIsZero && isZero(value.Field(i)) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 570 | } |
| 571 | return valueIsZero |
| 572 | case reflect.Ptr: |
| 573 | if !value.IsNil() { |
| 574 | return isZero(reflect.Indirect(value)) |
| 575 | } else { |
| 576 | return true |
| 577 | } |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 578 | // Always print bool/strings, if you want a bool/string attribute to be able to take the default value, use a |
| 579 | // pointer instead |
| 580 | case reflect.Bool, reflect.String: |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 581 | return false |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 582 | default: |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 583 | if !value.IsValid() { |
| 584 | return true |
| 585 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 586 | zeroValue := reflect.Zero(value.Type()) |
| 587 | result := value.Interface() == zeroValue.Interface() |
| 588 | return result |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | func escapeString(s string) string { |
| 593 | s = strings.ReplaceAll(s, "\\", "\\\\") |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 594 | |
| 595 | // b/184026959: Reverse the application of some common control sequences. |
| 596 | // These must be generated literally in the BUILD file. |
| 597 | s = strings.ReplaceAll(s, "\t", "\\t") |
| 598 | s = strings.ReplaceAll(s, "\n", "\\n") |
| 599 | s = strings.ReplaceAll(s, "\r", "\\r") |
| 600 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 601 | return strings.ReplaceAll(s, "\"", "\\\"") |
| 602 | } |
| 603 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 604 | func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string { |
| 605 | name := "" |
| 606 | if c.ModuleSubDir(logicModule) != "" { |
| 607 | // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes. |
| 608 | name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule) |
| 609 | } else { |
| 610 | name = c.ModuleName(logicModule) |
| 611 | } |
| 612 | |
| 613 | return strings.Replace(name, "//", "", 1) |
| 614 | } |
| 615 | |
| 616 | func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string { |
| 617 | return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule)) |
| 618 | } |