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" |
| 30 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 31 | "github.com/google/blueprint" |
| 32 | "github.com/google/blueprint/proptools" |
| 33 | ) |
| 34 | |
| 35 | type BazelAttributes struct { |
| 36 | Attrs map[string]string |
| 37 | } |
| 38 | |
| 39 | type BazelTarget struct { |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 40 | name string |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 41 | packageName string |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 42 | content string |
| 43 | ruleClass string |
| 44 | bzlLoadLocation string |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 45 | handcrafted bool |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file, |
| 49 | // as opposed to a native rule built into Bazel. |
| 50 | func (t BazelTarget) IsLoadedFromStarlark() bool { |
| 51 | return t.bzlLoadLocation != "" |
| 52 | } |
| 53 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 54 | // Label is the fully qualified Bazel label constructed from the BazelTarget's |
| 55 | // package name and target name. |
| 56 | func (t BazelTarget) Label() string { |
| 57 | if t.packageName == "." { |
| 58 | return "//:" + t.name |
| 59 | } else { |
| 60 | return "//" + t.packageName + ":" + t.name |
| 61 | } |
| 62 | } |
| 63 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 64 | // BazelTargets is a typedef for a slice of BazelTarget objects. |
| 65 | type BazelTargets []BazelTarget |
| 66 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 67 | // HasHandcraftedTargetsreturns true if a set of bazel targets contain |
| 68 | // handcrafted ones. |
| 69 | func (targets BazelTargets) hasHandcraftedTargets() bool { |
| 70 | for _, target := range targets { |
| 71 | if target.handcrafted { |
| 72 | return true |
| 73 | } |
| 74 | } |
| 75 | return false |
| 76 | } |
| 77 | |
| 78 | // sort a list of BazelTargets in-place, by name, and by generated/handcrafted types. |
| 79 | func (targets BazelTargets) sort() { |
| 80 | sort.Slice(targets, func(i, j int) bool { |
| 81 | if targets[i].handcrafted != targets[j].handcrafted { |
| 82 | // Handcrafted targets will be generated after the bp2build generated targets. |
| 83 | return targets[j].handcrafted |
| 84 | } |
| 85 | // This will cover all bp2build generated targets. |
| 86 | return targets[i].name < targets[j].name |
| 87 | }) |
| 88 | } |
| 89 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 90 | // String returns the string representation of BazelTargets, without load |
| 91 | // statements (use LoadStatements for that), since the targets are usually not |
| 92 | // adjacent to the load statements at the top of the BUILD file. |
| 93 | func (targets BazelTargets) String() string { |
| 94 | var res string |
| 95 | for i, target := range targets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 96 | // There is only at most 1 handcrafted "target", because its contents |
| 97 | // represent the entire BUILD file content from the tree. See |
| 98 | // build_conversion.go#getHandcraftedBuildContent for more information. |
| 99 | // |
| 100 | // Add a header to make it easy to debug where the handcrafted targets |
| 101 | // are in a generated BUILD file. |
| 102 | if target.handcrafted { |
| 103 | res += "# -----------------------------\n" |
| 104 | res += "# Section: Handcrafted targets. \n" |
| 105 | res += "# -----------------------------\n\n" |
| 106 | } |
| 107 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 108 | res += target.content |
| 109 | if i != len(targets)-1 { |
| 110 | res += "\n\n" |
| 111 | } |
| 112 | } |
| 113 | return res |
| 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 { |
| 119 | bzlToLoadedSymbols := map[string][]string{} |
| 120 | for _, target := range targets { |
| 121 | if target.IsLoadedFromStarlark() { |
| 122 | bzlToLoadedSymbols[target.bzlLoadLocation] = |
| 123 | append(bzlToLoadedSymbols[target.bzlLoadLocation], target.ruleClass) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | var loadStatements []string |
| 128 | for bzl, ruleClasses := range bzlToLoadedSymbols { |
| 129 | loadStatement := "load(\"" |
| 130 | loadStatement += bzl |
| 131 | loadStatement += "\", " |
| 132 | ruleClasses = android.SortedUniqueStrings(ruleClasses) |
| 133 | for i, ruleClass := range ruleClasses { |
| 134 | loadStatement += "\"" + ruleClass + "\"" |
| 135 | if i != len(ruleClasses)-1 { |
| 136 | loadStatement += ", " |
| 137 | } |
| 138 | } |
| 139 | loadStatement += ")" |
| 140 | loadStatements = append(loadStatements, loadStatement) |
| 141 | } |
| 142 | return strings.Join(android.SortedUniqueStrings(loadStatements), "\n") |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | type bpToBuildContext interface { |
| 146 | ModuleName(module blueprint.Module) string |
| 147 | ModuleDir(module blueprint.Module) string |
| 148 | ModuleSubDir(module blueprint.Module) string |
| 149 | ModuleType(module blueprint.Module) string |
| 150 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 151 | VisitAllModules(visit func(blueprint.Module)) |
| 152 | VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module)) |
| 153 | } |
| 154 | |
| 155 | type CodegenContext struct { |
Jingwen Chen | 16d90a8 | 2021-09-17 07:16:13 +0000 | [diff] [blame] | 156 | config android.Config |
| 157 | context android.Context |
| 158 | mode CodegenMode |
| 159 | additionalDeps []string |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 160 | unconvertedDepMode unconvertedDepsMode |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 163 | func (c *CodegenContext) Mode() CodegenMode { |
| 164 | return c.mode |
| 165 | } |
| 166 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 167 | // CodegenMode is an enum to differentiate code-generation modes. |
| 168 | type CodegenMode int |
| 169 | |
| 170 | const ( |
| 171 | // Bp2Build: generate BUILD files with targets buildable by Bazel directly. |
| 172 | // |
| 173 | // This mode is used for the Soong->Bazel build definition conversion. |
| 174 | Bp2Build CodegenMode = iota |
| 175 | |
| 176 | // QueryView: generate BUILD files with targets representing fully mutated |
| 177 | // Soong modules, representing the fully configured Soong module graph with |
| 178 | // variants and dependency endges. |
| 179 | // |
| 180 | // This mode is used for discovering and introspecting the existing Soong |
| 181 | // module graph. |
| 182 | QueryView |
| 183 | ) |
| 184 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 185 | type unconvertedDepsMode int |
| 186 | |
| 187 | const ( |
| 188 | // Include a warning in conversion metrics about converted modules with unconverted direct deps |
| 189 | warnUnconvertedDeps unconvertedDepsMode = iota |
| 190 | // Error and fail conversion if encountering a module with unconverted direct deps |
| 191 | // Enabled by setting environment variable `BP2BUILD_ERROR_UNCONVERTED` |
| 192 | errorModulesUnconvertedDeps |
| 193 | ) |
| 194 | |
Jingwen Chen | dcc329a | 2021-01-26 02:49:03 -0500 | [diff] [blame] | 195 | func (mode CodegenMode) String() string { |
| 196 | switch mode { |
| 197 | case Bp2Build: |
| 198 | return "Bp2Build" |
| 199 | case QueryView: |
| 200 | return "QueryView" |
| 201 | default: |
| 202 | return fmt.Sprintf("%d", mode) |
| 203 | } |
| 204 | } |
| 205 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 206 | // AddNinjaFileDeps adds dependencies on the specified files to be added to the ninja manifest. The |
| 207 | // primary builder will be rerun whenever the specified files are modified. Allows us to fulfill the |
| 208 | // PathContext interface in order to add dependencies on hand-crafted BUILD files. Note: must also |
| 209 | // call AdditionalNinjaDeps and add them manually to the ninja file. |
| 210 | func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) { |
| 211 | ctx.additionalDeps = append(ctx.additionalDeps, deps...) |
| 212 | } |
| 213 | |
| 214 | // AdditionalNinjaDeps returns additional ninja deps added by CodegenContext |
| 215 | func (ctx *CodegenContext) AdditionalNinjaDeps() []string { |
| 216 | return ctx.additionalDeps |
| 217 | } |
| 218 | |
| 219 | func (ctx *CodegenContext) Config() android.Config { return ctx.config } |
| 220 | func (ctx *CodegenContext) Context() android.Context { return ctx.context } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 221 | |
| 222 | // NewCodegenContext creates a wrapper context that conforms to PathContext for |
| 223 | // writing BUILD files in the output directory. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 224 | func NewCodegenContext(config android.Config, context android.Context, mode CodegenMode) *CodegenContext { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 225 | var unconvertedDeps unconvertedDepsMode |
| 226 | if config.IsEnvTrue("BP2BUILD_ERROR_UNCONVERTED") { |
| 227 | unconvertedDeps = errorModulesUnconvertedDeps |
| 228 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 229 | return &CodegenContext{ |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 230 | context: context, |
| 231 | config: config, |
| 232 | mode: mode, |
| 233 | unconvertedDepMode: unconvertedDeps, |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 234 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // props is an unsorted map. This function ensures that |
| 238 | // the generated attributes are sorted to ensure determinism. |
| 239 | func propsToAttributes(props map[string]string) string { |
| 240 | var attributes string |
| 241 | for _, propName := range android.SortedStringKeys(props) { |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 242 | attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName]) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 243 | } |
| 244 | return attributes |
| 245 | } |
| 246 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 247 | type conversionResults struct { |
| 248 | buildFileToTargets map[string]BazelTargets |
| 249 | metrics CodegenMetrics |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | func (r conversionResults) BuildDirToTargets() map[string]BazelTargets { |
| 253 | return r.buildFileToTargets |
| 254 | } |
| 255 | |
| 256 | func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) { |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 257 | buildFileToTargets := make(map[string]BazelTargets) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 258 | buildFileToAppend := make(map[string]bool) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 259 | |
| 260 | // Simple metrics tracking for bp2build |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 261 | metrics := CodegenMetrics{ |
Chris Parsons | 492bd91 | 2022-01-20 12:55:05 -0500 | [diff] [blame^] | 262 | ruleClassCount: make(map[string]uint64), |
| 263 | convertedModuleTypeCount: make(map[string]uint64), |
| 264 | totalModuleTypeCount: make(map[string]uint64), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 265 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 266 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 267 | dirs := make(map[string]bool) |
| 268 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 269 | var errs []error |
| 270 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 271 | bpCtx := ctx.Context() |
| 272 | bpCtx.VisitAllModules(func(m blueprint.Module) { |
| 273 | dir := bpCtx.ModuleDir(m) |
Chris Parsons | 492bd91 | 2022-01-20 12:55:05 -0500 | [diff] [blame^] | 274 | moduleType := bpCtx.ModuleType(m) |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 275 | dirs[dir] = true |
| 276 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 277 | var targets []BazelTarget |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 278 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 279 | switch ctx.Mode() { |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 280 | case Bp2Build: |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 281 | // There are two main ways of converting a Soong module to Bazel: |
| 282 | // 1) Manually handcrafting a Bazel target and associating the module with its label |
| 283 | // 2) Automatically generating with bp2build converters |
| 284 | // |
| 285 | // bp2build converters are used for the majority of modules. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 286 | if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() { |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 287 | // Handle modules converted to handcrafted targets. |
| 288 | // |
| 289 | // Since these modules are associated with some handcrafted |
| 290 | // target in a BUILD file, we simply append the entire contents |
| 291 | // of that BUILD file to the generated BUILD file. |
| 292 | // |
| 293 | // The append operation is only done once, even if there are |
| 294 | // multiple modules from the same directory associated to |
| 295 | // targets in the same BUILD file (or package). |
| 296 | |
| 297 | // Log the module. |
Chris Parsons | 492bd91 | 2022-01-20 12:55:05 -0500 | [diff] [blame^] | 298 | metrics.AddConvertedModule(m, moduleType, Handcrafted) |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 299 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 300 | pathToBuildFile := getBazelPackagePath(b) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 301 | if _, exists := buildFileToAppend[pathToBuildFile]; exists { |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 302 | // Append the BUILD file content once per package, at most. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 303 | return |
| 304 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 305 | t, err := getHandcraftedBuildContent(ctx, b, pathToBuildFile) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 306 | if err != nil { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 307 | errs = append(errs, fmt.Errorf("Error converting %s: %s", bpCtx.ModuleName(m), err)) |
| 308 | return |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 309 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 310 | targets = append(targets, t) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 311 | // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do |
| 312 | // something more targeted based on the rule type and target |
| 313 | buildFileToAppend[pathToBuildFile] = true |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 314 | } else if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() { |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 315 | // Handle modules converted to generated targets. |
| 316 | |
| 317 | // Log the module. |
Chris Parsons | 492bd91 | 2022-01-20 12:55:05 -0500 | [diff] [blame^] | 318 | metrics.AddConvertedModule(aModule, moduleType, Generated) |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 319 | |
| 320 | // Handle modules with unconverted deps. By default, emit a warning. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 321 | if unconvertedDeps := aModule.GetUnconvertedBp2buildDeps(); len(unconvertedDeps) > 0 { |
| 322 | msg := fmt.Sprintf("%q depends on unconverted modules: %s", m.Name(), strings.Join(unconvertedDeps, ", ")) |
| 323 | if ctx.unconvertedDepMode == warnUnconvertedDeps { |
| 324 | metrics.moduleWithUnconvertedDepsMsgs = append(metrics.moduleWithUnconvertedDepsMsgs, msg) |
| 325 | } else if ctx.unconvertedDepMode == errorModulesUnconvertedDeps { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 326 | errs = append(errs, fmt.Errorf(msg)) |
| 327 | return |
| 328 | } |
| 329 | } |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 330 | if unconvertedDeps := aModule.GetMissingBp2buildDeps(); len(unconvertedDeps) > 0 { |
| 331 | msg := fmt.Sprintf("%q depends on missing modules: %s", m.Name(), strings.Join(unconvertedDeps, ", ")) |
| 332 | if ctx.unconvertedDepMode == warnUnconvertedDeps { |
| 333 | metrics.moduleWithMissingDepsMsgs = append(metrics.moduleWithMissingDepsMsgs, msg) |
| 334 | } else if ctx.unconvertedDepMode == errorModulesUnconvertedDeps { |
| 335 | errs = append(errs, fmt.Errorf(msg)) |
| 336 | return |
| 337 | } |
| 338 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 339 | targets = generateBazelTargets(bpCtx, aModule) |
| 340 | for _, t := range targets { |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 341 | // A module can potentially generate more than 1 Bazel |
| 342 | // target, each of a different rule class. |
| 343 | metrics.IncrementRuleClassCount(t.ruleClass) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 344 | } |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 345 | } else { |
Chris Parsons | 492bd91 | 2022-01-20 12:55:05 -0500 | [diff] [blame^] | 346 | metrics.AddUnconvertedModule(moduleType) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 347 | return |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 348 | } |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 349 | case QueryView: |
Jingwen Chen | 96af35b | 2021-02-08 00:49:32 -0500 | [diff] [blame] | 350 | // Blocklist certain module types from being generated. |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 351 | if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" { |
Jingwen Chen | 96af35b | 2021-02-08 00:49:32 -0500 | [diff] [blame] | 352 | // package module name contain slashes, and thus cannot |
| 353 | // be mapped cleanly to a bazel label. |
| 354 | return |
| 355 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 356 | t := generateSoongModuleTarget(bpCtx, m) |
| 357 | targets = append(targets, t) |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 358 | default: |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 359 | errs = append(errs, fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode())) |
| 360 | return |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 361 | } |
| 362 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 363 | buildFileToTargets[dir] = append(buildFileToTargets[dir], targets...) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 364 | }) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 365 | |
| 366 | if len(errs) > 0 { |
| 367 | return conversionResults{}, errs |
| 368 | } |
| 369 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 370 | if generateFilegroups { |
| 371 | // Add a filegroup target that exposes all sources in the subtree of this package |
| 372 | // NOTE: This also means we generate a BUILD file for every Android.bp file (as long as it has at least one module) |
| 373 | for dir, _ := range dirs { |
| 374 | buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{ |
| 375 | name: "bp2build_all_srcs", |
| 376 | content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]))`, |
| 377 | ruleClass: "filegroup", |
| 378 | }) |
| 379 | } |
| 380 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 381 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 382 | return conversionResults{ |
| 383 | buildFileToTargets: buildFileToTargets, |
| 384 | metrics: metrics, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 385 | }, errs |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 386 | } |
| 387 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 388 | func getBazelPackagePath(b android.Bazelable) string { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 389 | label := b.HandcraftedLabel() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 390 | pathToBuildFile := strings.TrimPrefix(label, "//") |
| 391 | pathToBuildFile = strings.Split(pathToBuildFile, ":")[0] |
| 392 | return pathToBuildFile |
| 393 | } |
| 394 | |
| 395 | func getHandcraftedBuildContent(ctx *CodegenContext, b android.Bazelable, pathToBuildFile string) (BazelTarget, error) { |
| 396 | p := android.ExistentPathForSource(ctx, pathToBuildFile, HandcraftedBuildFileName) |
| 397 | if !p.Valid() { |
| 398 | return BazelTarget{}, fmt.Errorf("Could not find file %q for handcrafted target.", pathToBuildFile) |
| 399 | } |
| 400 | c, err := b.GetBazelBuildFileContents(ctx.Config(), pathToBuildFile, HandcraftedBuildFileName) |
| 401 | if err != nil { |
| 402 | return BazelTarget{}, err |
| 403 | } |
| 404 | // TODO(b/181575318): once this is more targeted, we need to include name, rule class, etc |
| 405 | return BazelTarget{ |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 406 | content: c, |
| 407 | handcrafted: true, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 408 | }, nil |
| 409 | } |
| 410 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 411 | func generateBazelTargets(ctx bpToBuildContext, m android.Module) []BazelTarget { |
| 412 | var targets []BazelTarget |
| 413 | for _, m := range m.Bp2buildTargets() { |
| 414 | targets = append(targets, generateBazelTarget(ctx, m)) |
| 415 | } |
| 416 | return targets |
| 417 | } |
| 418 | |
| 419 | type bp2buildModule interface { |
| 420 | TargetName() string |
| 421 | TargetPackage() string |
| 422 | BazelRuleClass() string |
| 423 | BazelRuleLoadLocation() string |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 424 | BazelAttributes() []interface{} |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) BazelTarget { |
| 428 | ruleClass := m.BazelRuleClass() |
| 429 | bzlLoadLocation := m.BazelRuleLoadLocation() |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 430 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 431 | // 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] | 432 | attrs := m.BazelAttributes() |
| 433 | props := extractModuleProperties(attrs, true) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 434 | |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 435 | // name is handled in a special manner |
| 436 | delete(props.Attrs, "name") |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 437 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 438 | // Return the Bazel target with rule class and attributes, ready to be |
| 439 | // code-generated. |
| 440 | attributes := propsToAttributes(props.Attrs) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 441 | targetName := m.TargetName() |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 442 | return BazelTarget{ |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 443 | name: targetName, |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 444 | packageName: m.TargetPackage(), |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 445 | ruleClass: ruleClass, |
| 446 | bzlLoadLocation: bzlLoadLocation, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 447 | content: fmt.Sprintf( |
| 448 | bazelTarget, |
| 449 | ruleClass, |
| 450 | targetName, |
| 451 | attributes, |
| 452 | ), |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 453 | handcrafted: false, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 457 | // Convert a module and its deps and props into a Bazel macro/rule |
| 458 | // representation in the BUILD file. |
| 459 | func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget { |
| 460 | props := getBuildProperties(ctx, m) |
| 461 | |
| 462 | // TODO(b/163018919): DirectDeps can have duplicate (module, variant) |
| 463 | // items, if the modules are added using different DependencyTag. Figure |
| 464 | // out the implications of that. |
| 465 | depLabels := map[string]bool{} |
| 466 | if aModule, ok := m.(android.Module); ok { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 467 | ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 468 | depLabels[qualifiedTargetLabel(ctx, depModule)] = true |
| 469 | }) |
| 470 | } |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 471 | |
| 472 | for p, _ := range ignoredPropNames { |
| 473 | delete(props.Attrs, p) |
| 474 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 475 | attributes := propsToAttributes(props.Attrs) |
| 476 | |
| 477 | depLabelList := "[\n" |
| 478 | for depLabel, _ := range depLabels { |
| 479 | depLabelList += fmt.Sprintf(" %q,\n", depLabel) |
| 480 | } |
| 481 | depLabelList += " ]" |
| 482 | |
| 483 | targetName := targetNameWithVariant(ctx, m) |
| 484 | return BazelTarget{ |
| 485 | name: targetName, |
| 486 | content: fmt.Sprintf( |
| 487 | soongModuleTarget, |
| 488 | targetName, |
| 489 | ctx.ModuleName(m), |
| 490 | canonicalizeModuleType(ctx.ModuleType(m)), |
| 491 | ctx.ModuleSubDir(m), |
| 492 | depLabelList, |
| 493 | attributes), |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) BazelAttributes { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 498 | // TODO: this omits properties for blueprint modules (blueprint_go_binary, |
| 499 | // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately. |
| 500 | 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] | 501 | return extractModuleProperties(aModule.GetProperties(), false) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 504 | return BazelAttributes{} |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // Generically extract module properties and types into a map, keyed by the module property name. |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 508 | func extractModuleProperties(props []interface{}, checkForDuplicateProperties bool) BazelAttributes { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 509 | ret := map[string]string{} |
| 510 | |
| 511 | // Iterate over this android.Module's property structs. |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 512 | for _, properties := range props { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 513 | propertiesValue := reflect.ValueOf(properties) |
| 514 | // Check that propertiesValue is a pointer to the Properties struct, like |
| 515 | // *cc.BaseLinkerProperties or *java.CompilerProperties. |
| 516 | // |
| 517 | // propertiesValue can also be type-asserted to the structs to |
| 518 | // manipulate internal props, if needed. |
| 519 | if isStructPtr(propertiesValue.Type()) { |
| 520 | structValue := propertiesValue.Elem() |
| 521 | for k, v := range extractStructProperties(structValue, 0) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 522 | if existing, exists := ret[k]; checkForDuplicateProperties && exists { |
| 523 | panic(fmt.Errorf( |
| 524 | "%s (%v) is present in properties whereas it should be consolidated into a commonAttributes", |
| 525 | k, existing)) |
| 526 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 527 | ret[k] = v |
| 528 | } |
| 529 | } else { |
| 530 | panic(fmt.Errorf( |
| 531 | "properties must be a pointer to a struct, got %T", |
| 532 | propertiesValue.Interface())) |
| 533 | } |
| 534 | } |
| 535 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 536 | return BazelAttributes{ |
| 537 | Attrs: ret, |
| 538 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | func isStructPtr(t reflect.Type) bool { |
| 542 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct |
| 543 | } |
| 544 | |
| 545 | // prettyPrint a property value into the equivalent Starlark representation |
| 546 | // recursively. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 547 | func prettyPrint(propertyValue reflect.Value, indent int, emitZeroValues bool) (string, error) { |
| 548 | if !emitZeroValues && isZero(propertyValue) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 549 | // A property value being set or unset actually matters -- Soong does set default |
| 550 | // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at |
| 551 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480 |
| 552 | // |
Jingwen Chen | fc490bd | 2021-03-30 10:24:19 +0000 | [diff] [blame] | 553 | // In Bazel-parlance, we would use "attr.<type>(default = <default |
| 554 | // value>)" to set the default value of unset attributes. In the cases |
| 555 | // where the bp2build converter didn't set the default value within the |
| 556 | // mutator when creating the BazelTargetModule, this would be a zero |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 557 | // value. For those cases, we return an empty string so we don't |
| 558 | // unnecessarily generate empty values. |
| 559 | return "", nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | var ret string |
| 563 | switch propertyValue.Kind() { |
| 564 | case reflect.String: |
| 565 | ret = fmt.Sprintf("\"%v\"", escapeString(propertyValue.String())) |
| 566 | case reflect.Bool: |
| 567 | ret = strings.Title(fmt.Sprintf("%v", propertyValue.Interface())) |
| 568 | case reflect.Int, reflect.Uint, reflect.Int64: |
| 569 | ret = fmt.Sprintf("%v", propertyValue.Interface()) |
| 570 | case reflect.Ptr: |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 571 | return prettyPrint(propertyValue.Elem(), indent, emitZeroValues) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 572 | case reflect.Slice: |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 573 | if propertyValue.Len() == 0 { |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 574 | return "[]", nil |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 575 | } |
| 576 | |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 577 | if propertyValue.Len() == 1 { |
| 578 | // Single-line list for list with only 1 element |
| 579 | ret += "[" |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 580 | indexedValue, err := prettyPrint(propertyValue.Index(0), indent, emitZeroValues) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 581 | if err != nil { |
| 582 | return "", err |
| 583 | } |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 584 | ret += indexedValue |
| 585 | ret += "]" |
| 586 | } else { |
| 587 | // otherwise, use a multiline list. |
| 588 | ret += "[\n" |
| 589 | for i := 0; i < propertyValue.Len(); i++ { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 590 | indexedValue, err := prettyPrint(propertyValue.Index(i), indent+1, emitZeroValues) |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 591 | if err != nil { |
| 592 | return "", err |
| 593 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 594 | |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 595 | if indexedValue != "" { |
| 596 | ret += makeIndent(indent + 1) |
| 597 | ret += indexedValue |
| 598 | ret += ",\n" |
| 599 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 600 | } |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 601 | ret += makeIndent(indent) |
| 602 | ret += "]" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 603 | } |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 604 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 605 | case reflect.Struct: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 606 | // Special cases where the bp2build sends additional information to the codegenerator |
| 607 | // by wrapping the attributes in a custom struct type. |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 608 | if attr, ok := propertyValue.Interface().(bazel.Attribute); ok { |
| 609 | return prettyPrintAttribute(attr, indent) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 610 | } else if label, ok := propertyValue.Interface().(bazel.Label); ok { |
| 611 | return fmt.Sprintf("%q", label.Label), nil |
| 612 | } |
| 613 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 614 | ret = "{\n" |
| 615 | // Sort and print the struct props by the key. |
| 616 | structProps := extractStructProperties(propertyValue, indent) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 617 | if len(structProps) == 0 { |
| 618 | return "", nil |
| 619 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 620 | for _, k := range android.SortedStringKeys(structProps) { |
| 621 | ret += makeIndent(indent + 1) |
| 622 | ret += fmt.Sprintf("%q: %s,\n", k, structProps[k]) |
| 623 | } |
| 624 | ret += makeIndent(indent) |
| 625 | ret += "}" |
| 626 | case reflect.Interface: |
| 627 | // TODO(b/164227191): implement pretty print for interfaces. |
| 628 | // Interfaces are used for for arch, multilib and target properties. |
| 629 | return "", nil |
| 630 | default: |
| 631 | return "", fmt.Errorf( |
| 632 | "unexpected kind for property struct field: %s", propertyValue.Kind()) |
| 633 | } |
| 634 | return ret, nil |
| 635 | } |
| 636 | |
| 637 | // Converts a reflected property struct value into a map of property names and property values, |
| 638 | // which each property value correctly pretty-printed and indented at the right nest level, |
| 639 | // since property structs can be nested. In Starlark, nested structs are represented as nested |
| 640 | // dicts: https://docs.bazel.build/skylark/lib/dict.html |
| 641 | func extractStructProperties(structValue reflect.Value, indent int) map[string]string { |
| 642 | if structValue.Kind() != reflect.Struct { |
| 643 | panic(fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind())) |
| 644 | } |
| 645 | |
| 646 | ret := map[string]string{} |
| 647 | structType := structValue.Type() |
| 648 | for i := 0; i < structValue.NumField(); i++ { |
| 649 | field := structType.Field(i) |
| 650 | if shouldSkipStructField(field) { |
| 651 | continue |
| 652 | } |
| 653 | |
| 654 | fieldValue := structValue.Field(i) |
| 655 | if isZero(fieldValue) { |
| 656 | // Ignore zero-valued fields |
| 657 | continue |
| 658 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 659 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 660 | // if the struct is embedded (anonymous), flatten the properties into the containing struct |
| 661 | if field.Anonymous { |
| 662 | if field.Type.Kind() == reflect.Ptr { |
| 663 | fieldValue = fieldValue.Elem() |
| 664 | } |
| 665 | if fieldValue.Type().Kind() == reflect.Struct { |
| 666 | propsToMerge := extractStructProperties(fieldValue, indent) |
| 667 | for prop, value := range propsToMerge { |
| 668 | ret[prop] = value |
| 669 | } |
| 670 | continue |
| 671 | } |
| 672 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 673 | |
| 674 | propertyName := proptools.PropertyNameForField(field.Name) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 675 | prettyPrintedValue, err := prettyPrint(fieldValue, indent+1, false) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 676 | if err != nil { |
| 677 | panic( |
| 678 | fmt.Errorf( |
| 679 | "Error while parsing property: %q. %s", |
| 680 | propertyName, |
| 681 | err)) |
| 682 | } |
| 683 | if prettyPrintedValue != "" { |
| 684 | ret[propertyName] = prettyPrintedValue |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | return ret |
| 689 | } |
| 690 | |
| 691 | func isZero(value reflect.Value) bool { |
| 692 | switch value.Kind() { |
| 693 | case reflect.Func, reflect.Map, reflect.Slice: |
| 694 | return value.IsNil() |
| 695 | case reflect.Array: |
| 696 | valueIsZero := true |
| 697 | for i := 0; i < value.Len(); i++ { |
| 698 | valueIsZero = valueIsZero && isZero(value.Index(i)) |
| 699 | } |
| 700 | return valueIsZero |
| 701 | case reflect.Struct: |
| 702 | valueIsZero := true |
| 703 | for i := 0; i < value.NumField(); i++ { |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 704 | valueIsZero = valueIsZero && isZero(value.Field(i)) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 705 | } |
| 706 | return valueIsZero |
| 707 | case reflect.Ptr: |
| 708 | if !value.IsNil() { |
| 709 | return isZero(reflect.Indirect(value)) |
| 710 | } else { |
| 711 | return true |
| 712 | } |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 713 | // Always print bool/strings, if you want a bool/string attribute to be able to take the default value, use a |
| 714 | // pointer instead |
| 715 | case reflect.Bool, reflect.String: |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 716 | return false |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 717 | default: |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 718 | if !value.IsValid() { |
| 719 | return true |
| 720 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 721 | zeroValue := reflect.Zero(value.Type()) |
| 722 | result := value.Interface() == zeroValue.Interface() |
| 723 | return result |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | func escapeString(s string) string { |
| 728 | s = strings.ReplaceAll(s, "\\", "\\\\") |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 729 | |
| 730 | // b/184026959: Reverse the application of some common control sequences. |
| 731 | // These must be generated literally in the BUILD file. |
| 732 | s = strings.ReplaceAll(s, "\t", "\\t") |
| 733 | s = strings.ReplaceAll(s, "\n", "\\n") |
| 734 | s = strings.ReplaceAll(s, "\r", "\\r") |
| 735 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 736 | return strings.ReplaceAll(s, "\"", "\\\"") |
| 737 | } |
| 738 | |
| 739 | func makeIndent(indent int) string { |
| 740 | if indent < 0 { |
| 741 | panic(fmt.Errorf("indent column cannot be less than 0, but got %d", indent)) |
| 742 | } |
| 743 | return strings.Repeat(" ", indent) |
| 744 | } |
| 745 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 746 | func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string { |
| 747 | name := "" |
| 748 | if c.ModuleSubDir(logicModule) != "" { |
| 749 | // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes. |
| 750 | name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule) |
| 751 | } else { |
| 752 | name = c.ModuleName(logicModule) |
| 753 | } |
| 754 | |
| 755 | return strings.Replace(name, "//", "", 1) |
| 756 | } |
| 757 | |
| 758 | func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string { |
| 759 | return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule)) |
| 760 | } |