blob: 07f492efbc427174d27c11f017c4f17a3c33a059 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// 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
15package bp2build
16
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0da7ce62021-08-23 17:04:20 +000017/*
18For shareable/common functionality for conversion from soong-module to build files
19for queryview/bp2build
20*/
21
Liz Kammer2dd9ca42020-11-25 16:06:39 -080022import (
Liz Kammer2dd9ca42020-11-25 16:06:39 -080023 "fmt"
24 "reflect"
Jingwen Chen49109762021-05-25 05:16:48 +000025 "sort"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080026 "strings"
27
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0da7ce62021-08-23 17:04:20 +000028 "android/soong/android"
29 "android/soong/bazel"
30
Liz Kammer2dd9ca42020-11-25 16:06:39 -080031 "github.com/google/blueprint"
32 "github.com/google/blueprint/proptools"
33)
34
35type BazelAttributes struct {
36 Attrs map[string]string
37}
38
39type BazelTarget struct {
Jingwen Chen40067de2021-01-26 21:58:43 -050040 name string
Jingwen Chenc63677b2021-06-17 05:43:19 +000041 packageName string
Jingwen Chen40067de2021-01-26 21:58:43 -050042 content string
43 ruleClass string
44 bzlLoadLocation string
Jingwen Chen49109762021-05-25 05:16:48 +000045 handcrafted bool
Jingwen Chen40067de2021-01-26 21:58:43 -050046}
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.
50func (t BazelTarget) IsLoadedFromStarlark() bool {
51 return t.bzlLoadLocation != ""
52}
53
Jingwen Chenc63677b2021-06-17 05:43:19 +000054// Label is the fully qualified Bazel label constructed from the BazelTarget's
55// package name and target name.
56func (t BazelTarget) Label() string {
57 if t.packageName == "." {
58 return "//:" + t.name
59 } else {
60 return "//" + t.packageName + ":" + t.name
61 }
62}
63
Jingwen Chen40067de2021-01-26 21:58:43 -050064// BazelTargets is a typedef for a slice of BazelTarget objects.
65type BazelTargets []BazelTarget
66
Jingwen Chen49109762021-05-25 05:16:48 +000067// HasHandcraftedTargetsreturns true if a set of bazel targets contain
68// handcrafted ones.
69func (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.
79func (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 Chen40067de2021-01-26 21:58:43 -050090// 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.
93func (targets BazelTargets) String() string {
94 var res string
95 for i, target := range targets {
Jingwen Chen49109762021-05-25 05:16:48 +000096 // 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 Chen40067de2021-01-26 21:58:43 -0500108 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.
118func (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 Kammer2dd9ca42020-11-25 16:06:39 -0800143}
144
145type 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 Chendaa54bc2020-12-14 02:58:54 -0500151 VisitAllModules(visit func(blueprint.Module))
152 VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module))
153}
154
155type CodegenContext struct {
Jingwen Chen16d90a82021-09-17 07:16:13 +0000156 config android.Config
157 context android.Context
158 mode CodegenMode
159 additionalDeps []string
Liz Kammer6eff3232021-08-26 08:37:59 -0400160 unconvertedDepMode unconvertedDepsMode
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500161}
162
Jingwen Chen164e0862021-02-19 00:48:40 -0500163func (c *CodegenContext) Mode() CodegenMode {
164 return c.mode
165}
166
Jingwen Chen33832f92021-01-24 22:55:54 -0500167// CodegenMode is an enum to differentiate code-generation modes.
168type CodegenMode int
169
170const (
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 Kammer6eff3232021-08-26 08:37:59 -0400185type unconvertedDepsMode int
186
187const (
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 Chendcc329a2021-01-26 02:49:03 -0500195func (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 Kammerba3ea162021-02-17 13:22:03 -0500206// 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.
210func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) {
211 ctx.additionalDeps = append(ctx.additionalDeps, deps...)
212}
213
214// AdditionalNinjaDeps returns additional ninja deps added by CodegenContext
215func (ctx *CodegenContext) AdditionalNinjaDeps() []string {
216 return ctx.additionalDeps
217}
218
219func (ctx *CodegenContext) Config() android.Config { return ctx.config }
220func (ctx *CodegenContext) Context() android.Context { return ctx.context }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500221
222// NewCodegenContext creates a wrapper context that conforms to PathContext for
223// writing BUILD files in the output directory.
Liz Kammerba3ea162021-02-17 13:22:03 -0500224func NewCodegenContext(config android.Config, context android.Context, mode CodegenMode) *CodegenContext {
Liz Kammer6eff3232021-08-26 08:37:59 -0400225 var unconvertedDeps unconvertedDepsMode
226 if config.IsEnvTrue("BP2BUILD_ERROR_UNCONVERTED") {
227 unconvertedDeps = errorModulesUnconvertedDeps
228 }
Liz Kammerba3ea162021-02-17 13:22:03 -0500229 return &CodegenContext{
Liz Kammer6eff3232021-08-26 08:37:59 -0400230 context: context,
231 config: config,
232 mode: mode,
233 unconvertedDepMode: unconvertedDeps,
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500234 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800235}
236
237// props is an unsorted map. This function ensures that
238// the generated attributes are sorted to ensure determinism.
239func propsToAttributes(props map[string]string) string {
240 var attributes string
241 for _, propName := range android.SortedStringKeys(props) {
242 if shouldGenerateAttribute(propName) {
243 attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName])
244 }
245 }
246 return attributes
247}
248
Liz Kammer6eff3232021-08-26 08:37:59 -0400249type conversionResults struct {
250 buildFileToTargets map[string]BazelTargets
251 metrics CodegenMetrics
Liz Kammer6eff3232021-08-26 08:37:59 -0400252}
253
254func (r conversionResults) BuildDirToTargets() map[string]BazelTargets {
255 return r.buildFileToTargets
256}
257
258func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) {
Jingwen Chen40067de2021-01-26 21:58:43 -0500259 buildFileToTargets := make(map[string]BazelTargets)
Liz Kammerba3ea162021-02-17 13:22:03 -0500260 buildFileToAppend := make(map[string]bool)
Jingwen Chen164e0862021-02-19 00:48:40 -0500261
262 // Simple metrics tracking for bp2build
Liz Kammerba3ea162021-02-17 13:22:03 -0500263 metrics := CodegenMetrics{
264 RuleClassCount: make(map[string]int),
265 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500266
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400267 dirs := make(map[string]bool)
268
Liz Kammer6eff3232021-08-26 08:37:59 -0400269 var errs []error
270
Jingwen Chen164e0862021-02-19 00:48:40 -0500271 bpCtx := ctx.Context()
272 bpCtx.VisitAllModules(func(m blueprint.Module) {
273 dir := bpCtx.ModuleDir(m)
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400274 dirs[dir] = true
275
Liz Kammer2ada09a2021-08-11 00:17:36 -0400276 var targets []BazelTarget
Jingwen Chen73850672020-12-14 08:25:34 -0500277
Jingwen Chen164e0862021-02-19 00:48:40 -0500278 switch ctx.Mode() {
Jingwen Chen33832f92021-01-24 22:55:54 -0500279 case Bp2Build:
Liz Kammerba3ea162021-02-17 13:22:03 -0500280 if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
281 metrics.handCraftedTargetCount += 1
282 metrics.TotalModuleCount += 1
Jingwen Chen61174502021-09-17 08:40:45 +0000283 metrics.AddConvertedModule(m.Name())
Liz Kammerba3ea162021-02-17 13:22:03 -0500284 pathToBuildFile := getBazelPackagePath(b)
285 // We are using the entire contents of handcrafted build file, so if multiple targets within
286 // a package have handcrafted targets, we only want to include the contents one time.
287 if _, exists := buildFileToAppend[pathToBuildFile]; exists {
288 return
289 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400290 t, err := getHandcraftedBuildContent(ctx, b, pathToBuildFile)
Liz Kammerba3ea162021-02-17 13:22:03 -0500291 if err != nil {
Liz Kammer6eff3232021-08-26 08:37:59 -0400292 errs = append(errs, fmt.Errorf("Error converting %s: %s", bpCtx.ModuleName(m), err))
293 return
Liz Kammerba3ea162021-02-17 13:22:03 -0500294 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400295 targets = append(targets, t)
Liz Kammerba3ea162021-02-17 13:22:03 -0500296 // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
297 // something more targeted based on the rule type and target
298 buildFileToAppend[pathToBuildFile] = true
Liz Kammer2ada09a2021-08-11 00:17:36 -0400299 } else if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
Liz Kammer6eff3232021-08-26 08:37:59 -0400300 if unconvertedDeps := aModule.GetUnconvertedBp2buildDeps(); len(unconvertedDeps) > 0 {
301 msg := fmt.Sprintf("%q depends on unconverted modules: %s", m.Name(), strings.Join(unconvertedDeps, ", "))
302 if ctx.unconvertedDepMode == warnUnconvertedDeps {
303 metrics.moduleWithUnconvertedDepsMsgs = append(metrics.moduleWithUnconvertedDepsMsgs, msg)
304 } else if ctx.unconvertedDepMode == errorModulesUnconvertedDeps {
305 metrics.TotalModuleCount += 1
306 errs = append(errs, fmt.Errorf(msg))
307 return
308 }
309 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400310 targets = generateBazelTargets(bpCtx, aModule)
Jingwen Chenafb84bd2021-09-20 10:31:46 +0000311 metrics.AddConvertedModule(m.Name())
Liz Kammer2ada09a2021-08-11 00:17:36 -0400312 for _, t := range targets {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400313 metrics.RuleClassCount[t.ruleClass] += 1
314 }
Liz Kammerfc46bc12021-02-19 11:06:17 -0500315 } else {
Liz Kammerba3ea162021-02-17 13:22:03 -0500316 metrics.TotalModuleCount += 1
317 return
Jingwen Chen73850672020-12-14 08:25:34 -0500318 }
Jingwen Chen33832f92021-01-24 22:55:54 -0500319 case QueryView:
Jingwen Chen96af35b2021-02-08 00:49:32 -0500320 // Blocklist certain module types from being generated.
Jingwen Chen164e0862021-02-19 00:48:40 -0500321 if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" {
Jingwen Chen96af35b2021-02-08 00:49:32 -0500322 // package module name contain slashes, and thus cannot
323 // be mapped cleanly to a bazel label.
324 return
325 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400326 t := generateSoongModuleTarget(bpCtx, m)
327 targets = append(targets, t)
Jingwen Chen33832f92021-01-24 22:55:54 -0500328 default:
Liz Kammer6eff3232021-08-26 08:37:59 -0400329 errs = append(errs, fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode()))
330 return
Jingwen Chen73850672020-12-14 08:25:34 -0500331 }
332
Liz Kammer2ada09a2021-08-11 00:17:36 -0400333 buildFileToTargets[dir] = append(buildFileToTargets[dir], targets...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800334 })
Liz Kammer6eff3232021-08-26 08:37:59 -0400335
336 if len(errs) > 0 {
337 return conversionResults{}, errs
338 }
339
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400340 if generateFilegroups {
341 // Add a filegroup target that exposes all sources in the subtree of this package
342 // NOTE: This also means we generate a BUILD file for every Android.bp file (as long as it has at least one module)
343 for dir, _ := range dirs {
344 buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{
345 name: "bp2build_all_srcs",
346 content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]))`,
347 ruleClass: "filegroup",
348 })
349 }
350 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500351
Liz Kammer6eff3232021-08-26 08:37:59 -0400352 return conversionResults{
353 buildFileToTargets: buildFileToTargets,
354 metrics: metrics,
Liz Kammer6eff3232021-08-26 08:37:59 -0400355 }, errs
Jingwen Chen164e0862021-02-19 00:48:40 -0500356}
357
Liz Kammerba3ea162021-02-17 13:22:03 -0500358func getBazelPackagePath(b android.Bazelable) string {
Liz Kammerbdc60992021-02-24 16:55:11 -0500359 label := b.HandcraftedLabel()
Liz Kammerba3ea162021-02-17 13:22:03 -0500360 pathToBuildFile := strings.TrimPrefix(label, "//")
361 pathToBuildFile = strings.Split(pathToBuildFile, ":")[0]
362 return pathToBuildFile
363}
364
365func getHandcraftedBuildContent(ctx *CodegenContext, b android.Bazelable, pathToBuildFile string) (BazelTarget, error) {
366 p := android.ExistentPathForSource(ctx, pathToBuildFile, HandcraftedBuildFileName)
367 if !p.Valid() {
368 return BazelTarget{}, fmt.Errorf("Could not find file %q for handcrafted target.", pathToBuildFile)
369 }
370 c, err := b.GetBazelBuildFileContents(ctx.Config(), pathToBuildFile, HandcraftedBuildFileName)
371 if err != nil {
372 return BazelTarget{}, err
373 }
374 // TODO(b/181575318): once this is more targeted, we need to include name, rule class, etc
375 return BazelTarget{
Jingwen Chen49109762021-05-25 05:16:48 +0000376 content: c,
377 handcrafted: true,
Liz Kammerba3ea162021-02-17 13:22:03 -0500378 }, nil
379}
380
Liz Kammer2ada09a2021-08-11 00:17:36 -0400381func generateBazelTargets(ctx bpToBuildContext, m android.Module) []BazelTarget {
382 var targets []BazelTarget
383 for _, m := range m.Bp2buildTargets() {
384 targets = append(targets, generateBazelTarget(ctx, m))
385 }
386 return targets
387}
388
389type bp2buildModule interface {
390 TargetName() string
391 TargetPackage() string
392 BazelRuleClass() string
393 BazelRuleLoadLocation() string
394 BazelAttributes() interface{}
395}
396
397func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) BazelTarget {
398 ruleClass := m.BazelRuleClass()
399 bzlLoadLocation := m.BazelRuleLoadLocation()
Jingwen Chen40067de2021-01-26 21:58:43 -0500400
Jingwen Chen73850672020-12-14 08:25:34 -0500401 // extract the bazel attributes from the module.
Liz Kammer2ada09a2021-08-11 00:17:36 -0400402 props := extractModuleProperties([]interface{}{m.BazelAttributes()})
Jingwen Chen73850672020-12-14 08:25:34 -0500403
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500404 delete(props.Attrs, "bp2build_available")
405
Jingwen Chen73850672020-12-14 08:25:34 -0500406 // Return the Bazel target with rule class and attributes, ready to be
407 // code-generated.
408 attributes := propsToAttributes(props.Attrs)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400409 targetName := m.TargetName()
Jingwen Chen73850672020-12-14 08:25:34 -0500410 return BazelTarget{
Jingwen Chen40067de2021-01-26 21:58:43 -0500411 name: targetName,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400412 packageName: m.TargetPackage(),
Jingwen Chen40067de2021-01-26 21:58:43 -0500413 ruleClass: ruleClass,
414 bzlLoadLocation: bzlLoadLocation,
Jingwen Chen73850672020-12-14 08:25:34 -0500415 content: fmt.Sprintf(
416 bazelTarget,
417 ruleClass,
418 targetName,
419 attributes,
420 ),
Jingwen Chen49109762021-05-25 05:16:48 +0000421 handcrafted: false,
Jingwen Chen73850672020-12-14 08:25:34 -0500422 }
423}
424
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800425// Convert a module and its deps and props into a Bazel macro/rule
426// representation in the BUILD file.
427func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
428 props := getBuildProperties(ctx, m)
429
430 // TODO(b/163018919): DirectDeps can have duplicate (module, variant)
431 // items, if the modules are added using different DependencyTag. Figure
432 // out the implications of that.
433 depLabels := map[string]bool{}
434 if aModule, ok := m.(android.Module); ok {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500435 ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800436 depLabels[qualifiedTargetLabel(ctx, depModule)] = true
437 })
438 }
439 attributes := propsToAttributes(props.Attrs)
440
441 depLabelList := "[\n"
442 for depLabel, _ := range depLabels {
443 depLabelList += fmt.Sprintf(" %q,\n", depLabel)
444 }
445 depLabelList += " ]"
446
447 targetName := targetNameWithVariant(ctx, m)
448 return BazelTarget{
449 name: targetName,
450 content: fmt.Sprintf(
451 soongModuleTarget,
452 targetName,
453 ctx.ModuleName(m),
454 canonicalizeModuleType(ctx.ModuleType(m)),
455 ctx.ModuleSubDir(m),
456 depLabelList,
457 attributes),
458 }
459}
460
461func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) BazelAttributes {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800462 // TODO: this omits properties for blueprint modules (blueprint_go_binary,
463 // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately.
464 if aModule, ok := m.(android.Module); ok {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400465 return extractModuleProperties(aModule.GetProperties())
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800466 }
467
Liz Kammer2ada09a2021-08-11 00:17:36 -0400468 return BazelAttributes{}
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800469}
470
471// Generically extract module properties and types into a map, keyed by the module property name.
Liz Kammer2ada09a2021-08-11 00:17:36 -0400472func extractModuleProperties(props []interface{}) BazelAttributes {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800473 ret := map[string]string{}
474
475 // Iterate over this android.Module's property structs.
Liz Kammer2ada09a2021-08-11 00:17:36 -0400476 for _, properties := range props {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800477 propertiesValue := reflect.ValueOf(properties)
478 // Check that propertiesValue is a pointer to the Properties struct, like
479 // *cc.BaseLinkerProperties or *java.CompilerProperties.
480 //
481 // propertiesValue can also be type-asserted to the structs to
482 // manipulate internal props, if needed.
483 if isStructPtr(propertiesValue.Type()) {
484 structValue := propertiesValue.Elem()
485 for k, v := range extractStructProperties(structValue, 0) {
486 ret[k] = v
487 }
488 } else {
489 panic(fmt.Errorf(
490 "properties must be a pointer to a struct, got %T",
491 propertiesValue.Interface()))
492 }
493 }
494
Liz Kammer2ada09a2021-08-11 00:17:36 -0400495 return BazelAttributes{
496 Attrs: ret,
497 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800498}
499
500func isStructPtr(t reflect.Type) bool {
501 return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
502}
503
504// prettyPrint a property value into the equivalent Starlark representation
505// recursively.
506func prettyPrint(propertyValue reflect.Value, indent int) (string, error) {
507 if isZero(propertyValue) {
508 // A property value being set or unset actually matters -- Soong does set default
509 // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at
510 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480
511 //
Jingwen Chenfc490bd2021-03-30 10:24:19 +0000512 // In Bazel-parlance, we would use "attr.<type>(default = <default
513 // value>)" to set the default value of unset attributes. In the cases
514 // where the bp2build converter didn't set the default value within the
515 // mutator when creating the BazelTargetModule, this would be a zero
Jingwen Chen63930982021-03-24 10:04:33 -0400516 // value. For those cases, we return an empty string so we don't
517 // unnecessarily generate empty values.
518 return "", nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800519 }
520
521 var ret string
522 switch propertyValue.Kind() {
523 case reflect.String:
524 ret = fmt.Sprintf("\"%v\"", escapeString(propertyValue.String()))
525 case reflect.Bool:
526 ret = strings.Title(fmt.Sprintf("%v", propertyValue.Interface()))
527 case reflect.Int, reflect.Uint, reflect.Int64:
528 ret = fmt.Sprintf("%v", propertyValue.Interface())
529 case reflect.Ptr:
530 return prettyPrint(propertyValue.Elem(), indent)
531 case reflect.Slice:
Jingwen Chen63930982021-03-24 10:04:33 -0400532 if propertyValue.Len() == 0 {
Liz Kammer2b07ec72021-05-26 15:08:27 -0400533 return "[]", nil
Jingwen Chen63930982021-03-24 10:04:33 -0400534 }
535
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000536 if propertyValue.Len() == 1 {
537 // Single-line list for list with only 1 element
538 ret += "["
539 indexedValue, err := prettyPrint(propertyValue.Index(0), indent)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800540 if err != nil {
541 return "", err
542 }
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000543 ret += indexedValue
544 ret += "]"
545 } else {
546 // otherwise, use a multiline list.
547 ret += "[\n"
548 for i := 0; i < propertyValue.Len(); i++ {
549 indexedValue, err := prettyPrint(propertyValue.Index(i), indent+1)
550 if err != nil {
551 return "", err
552 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800553
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000554 if indexedValue != "" {
555 ret += makeIndent(indent + 1)
556 ret += indexedValue
557 ret += ",\n"
558 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800559 }
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000560 ret += makeIndent(indent)
561 ret += "]"
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800562 }
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000563
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800564 case reflect.Struct:
Jingwen Chen5d864492021-02-24 07:20:12 -0500565 // Special cases where the bp2build sends additional information to the codegenerator
566 // by wrapping the attributes in a custom struct type.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000567 if attr, ok := propertyValue.Interface().(bazel.Attribute); ok {
568 return prettyPrintAttribute(attr, indent)
Liz Kammer356f7d42021-01-26 09:18:53 -0500569 } else if label, ok := propertyValue.Interface().(bazel.Label); ok {
570 return fmt.Sprintf("%q", label.Label), nil
571 }
572
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800573 ret = "{\n"
574 // Sort and print the struct props by the key.
575 structProps := extractStructProperties(propertyValue, indent)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000576 if len(structProps) == 0 {
577 return "", nil
578 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800579 for _, k := range android.SortedStringKeys(structProps) {
580 ret += makeIndent(indent + 1)
581 ret += fmt.Sprintf("%q: %s,\n", k, structProps[k])
582 }
583 ret += makeIndent(indent)
584 ret += "}"
585 case reflect.Interface:
586 // TODO(b/164227191): implement pretty print for interfaces.
587 // Interfaces are used for for arch, multilib and target properties.
588 return "", nil
589 default:
590 return "", fmt.Errorf(
591 "unexpected kind for property struct field: %s", propertyValue.Kind())
592 }
593 return ret, nil
594}
595
596// Converts a reflected property struct value into a map of property names and property values,
597// which each property value correctly pretty-printed and indented at the right nest level,
598// since property structs can be nested. In Starlark, nested structs are represented as nested
599// dicts: https://docs.bazel.build/skylark/lib/dict.html
600func extractStructProperties(structValue reflect.Value, indent int) map[string]string {
601 if structValue.Kind() != reflect.Struct {
602 panic(fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind()))
603 }
604
605 ret := map[string]string{}
606 structType := structValue.Type()
607 for i := 0; i < structValue.NumField(); i++ {
608 field := structType.Field(i)
609 if shouldSkipStructField(field) {
610 continue
611 }
612
613 fieldValue := structValue.Field(i)
614 if isZero(fieldValue) {
615 // Ignore zero-valued fields
616 continue
617 }
Liz Kammer32a03392021-09-14 11:17:21 -0400618 // if the struct is embedded (anonymous), flatten the properties into the containing struct
619 if field.Anonymous {
620 if field.Type.Kind() == reflect.Ptr {
621 fieldValue = fieldValue.Elem()
622 }
623 if fieldValue.Type().Kind() == reflect.Struct {
624 propsToMerge := extractStructProperties(fieldValue, indent)
625 for prop, value := range propsToMerge {
626 ret[prop] = value
627 }
628 continue
629 }
630 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800631
632 propertyName := proptools.PropertyNameForField(field.Name)
633 prettyPrintedValue, err := prettyPrint(fieldValue, indent+1)
634 if err != nil {
635 panic(
636 fmt.Errorf(
637 "Error while parsing property: %q. %s",
638 propertyName,
639 err))
640 }
641 if prettyPrintedValue != "" {
642 ret[propertyName] = prettyPrintedValue
643 }
644 }
645
646 return ret
647}
648
649func isZero(value reflect.Value) bool {
650 switch value.Kind() {
651 case reflect.Func, reflect.Map, reflect.Slice:
652 return value.IsNil()
653 case reflect.Array:
654 valueIsZero := true
655 for i := 0; i < value.Len(); i++ {
656 valueIsZero = valueIsZero && isZero(value.Index(i))
657 }
658 return valueIsZero
659 case reflect.Struct:
660 valueIsZero := true
661 for i := 0; i < value.NumField(); i++ {
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200662 valueIsZero = valueIsZero && isZero(value.Field(i))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800663 }
664 return valueIsZero
665 case reflect.Ptr:
666 if !value.IsNil() {
667 return isZero(reflect.Indirect(value))
668 } else {
669 return true
670 }
Liz Kammerd366c902021-06-03 13:43:01 -0400671 // Always print bools, if you want a bool attribute to be able to take the default value, use a
672 // bool pointer instead
673 case reflect.Bool:
674 return false
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800675 default:
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400676 if !value.IsValid() {
677 return true
678 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800679 zeroValue := reflect.Zero(value.Type())
680 result := value.Interface() == zeroValue.Interface()
681 return result
682 }
683}
684
685func escapeString(s string) string {
686 s = strings.ReplaceAll(s, "\\", "\\\\")
Jingwen Chen58a12b82021-03-30 13:08:36 +0000687
688 // b/184026959: Reverse the application of some common control sequences.
689 // These must be generated literally in the BUILD file.
690 s = strings.ReplaceAll(s, "\t", "\\t")
691 s = strings.ReplaceAll(s, "\n", "\\n")
692 s = strings.ReplaceAll(s, "\r", "\\r")
693
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800694 return strings.ReplaceAll(s, "\"", "\\\"")
695}
696
697func makeIndent(indent int) string {
698 if indent < 0 {
699 panic(fmt.Errorf("indent column cannot be less than 0, but got %d", indent))
700 }
701 return strings.Repeat(" ", indent)
702}
703
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800704func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
705 name := ""
706 if c.ModuleSubDir(logicModule) != "" {
707 // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes.
708 name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule)
709 } else {
710 name = c.ModuleName(logicModule)
711 }
712
713 return strings.Replace(name, "//", "", 1)
714}
715
716func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string {
717 return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule))
718}