blob: 9921a7f23247075f109eb180b63d8f7cba85982e [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"
Liz Kammer72beb342022-02-03 08:42:10 -050030 "android/soong/starlark_fmt"
Chris Parsons39a16972023-06-08 14:28:51 +000031 "android/soong/ui/metrics/bp2build_metrics_proto"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032 "github.com/google/blueprint"
Spandan Dasea2abba2023-06-14 21:30:38 +000033 "github.com/google/blueprint/bootstrap"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080034 "github.com/google/blueprint/proptools"
35)
36
37type BazelAttributes struct {
38 Attrs map[string]string
39}
40
41type BazelTarget struct {
Jingwen Chen40067de2021-01-26 21:58:43 -050042 name string
Jingwen Chenc63677b2021-06-17 05:43:19 +000043 packageName string
Jingwen Chen40067de2021-01-26 21:58:43 -050044 content string
45 ruleClass string
46 bzlLoadLocation string
47}
48
49// IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file,
50// as opposed to a native rule built into Bazel.
51func (t BazelTarget) IsLoadedFromStarlark() bool {
52 return t.bzlLoadLocation != ""
53}
54
Jingwen Chenc63677b2021-06-17 05:43:19 +000055// Label is the fully qualified Bazel label constructed from the BazelTarget's
56// package name and target name.
57func (t BazelTarget) Label() string {
58 if t.packageName == "." {
59 return "//:" + t.name
60 } else {
61 return "//" + t.packageName + ":" + t.name
62 }
63}
64
Spandan Dasabedff02023-03-07 19:24:34 +000065// PackageName returns the package of the Bazel target.
66// Defaults to root of tree.
67func (t BazelTarget) PackageName() string {
68 if t.packageName == "" {
69 return "."
70 }
71 return t.packageName
72}
73
Jingwen Chen40067de2021-01-26 21:58:43 -050074// BazelTargets is a typedef for a slice of BazelTarget objects.
75type BazelTargets []BazelTarget
76
Sasha Smundak8bea2672022-08-04 13:31:14 -070077func (targets BazelTargets) packageRule() *BazelTarget {
78 for _, target := range targets {
79 if target.ruleClass == "package" {
80 return &target
81 }
82 }
83 return nil
84}
85
86// sort a list of BazelTargets in-place, by name, and by generated/handcrafted types.
Jingwen Chen49109762021-05-25 05:16:48 +000087func (targets BazelTargets) sort() {
88 sort.Slice(targets, func(i, j int) bool {
Jingwen Chen49109762021-05-25 05:16:48 +000089 return targets[i].name < targets[j].name
90 })
91}
92
Jingwen Chen40067de2021-01-26 21:58:43 -050093// String returns the string representation of BazelTargets, without load
94// statements (use LoadStatements for that), since the targets are usually not
95// adjacent to the load statements at the top of the BUILD file.
96func (targets BazelTargets) String() string {
97 var res string
98 for i, target := range targets {
Sasha Smundak8bea2672022-08-04 13:31:14 -070099 if target.ruleClass != "package" {
100 res += target.content
101 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500102 if i != len(targets)-1 {
103 res += "\n\n"
104 }
105 }
106 return res
107}
108
109// LoadStatements return the string representation of the sorted and deduplicated
110// Starlark rule load statements needed by a group of BazelTargets.
111func (targets BazelTargets) LoadStatements() string {
112 bzlToLoadedSymbols := map[string][]string{}
113 for _, target := range targets {
114 if target.IsLoadedFromStarlark() {
115 bzlToLoadedSymbols[target.bzlLoadLocation] =
116 append(bzlToLoadedSymbols[target.bzlLoadLocation], target.ruleClass)
117 }
118 }
119
120 var loadStatements []string
121 for bzl, ruleClasses := range bzlToLoadedSymbols {
122 loadStatement := "load(\""
123 loadStatement += bzl
124 loadStatement += "\", "
125 ruleClasses = android.SortedUniqueStrings(ruleClasses)
126 for i, ruleClass := range ruleClasses {
127 loadStatement += "\"" + ruleClass + "\""
128 if i != len(ruleClasses)-1 {
129 loadStatement += ", "
130 }
131 }
132 loadStatement += ")"
133 loadStatements = append(loadStatements, loadStatement)
134 }
135 return strings.Join(android.SortedUniqueStrings(loadStatements), "\n")
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800136}
137
138type bpToBuildContext interface {
139 ModuleName(module blueprint.Module) string
140 ModuleDir(module blueprint.Module) string
141 ModuleSubDir(module blueprint.Module) string
142 ModuleType(module blueprint.Module) string
143
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500144 VisitAllModules(visit func(blueprint.Module))
145 VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module))
146}
147
148type CodegenContext struct {
Jingwen Chen16d90a82021-09-17 07:16:13 +0000149 config android.Config
Paul Duffinc6390592022-11-04 13:35:21 +0000150 context *android.Context
Jingwen Chen16d90a82021-09-17 07:16:13 +0000151 mode CodegenMode
152 additionalDeps []string
Liz Kammer6eff3232021-08-26 08:37:59 -0400153 unconvertedDepMode unconvertedDepsMode
Cole Faustb85d1a12022-11-08 18:14:01 -0800154 topDir string
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500155}
156
Usta Shresthadb46a9b2022-07-11 11:29:56 -0400157func (ctx *CodegenContext) Mode() CodegenMode {
158 return ctx.mode
Jingwen Chen164e0862021-02-19 00:48:40 -0500159}
160
Jingwen Chen33832f92021-01-24 22:55:54 -0500161// CodegenMode is an enum to differentiate code-generation modes.
162type CodegenMode int
163
164const (
Usta Shresthadb46a9b2022-07-11 11:29:56 -0400165 // Bp2Build - generate BUILD files with targets buildable by Bazel directly.
Jingwen Chen33832f92021-01-24 22:55:54 -0500166 //
167 // This mode is used for the Soong->Bazel build definition conversion.
168 Bp2Build CodegenMode = iota
169
Usta Shresthadb46a9b2022-07-11 11:29:56 -0400170 // QueryView - generate BUILD files with targets representing fully mutated
Jingwen Chen33832f92021-01-24 22:55:54 -0500171 // Soong modules, representing the fully configured Soong module graph with
Usta Shresthadb46a9b2022-07-11 11:29:56 -0400172 // variants and dependency edges.
Jingwen Chen33832f92021-01-24 22:55:54 -0500173 //
174 // This mode is used for discovering and introspecting the existing Soong
175 // module graph.
176 QueryView
Spandan Das5af0bd32022-09-28 20:43:08 +0000177
178 // ApiBp2build - generate BUILD files for API contribution targets
179 ApiBp2build
Jingwen Chen33832f92021-01-24 22:55:54 -0500180)
181
Liz Kammer6eff3232021-08-26 08:37:59 -0400182type unconvertedDepsMode int
183
184const (
185 // Include a warning in conversion metrics about converted modules with unconverted direct deps
186 warnUnconvertedDeps unconvertedDepsMode = iota
187 // Error and fail conversion if encountering a module with unconverted direct deps
188 // Enabled by setting environment variable `BP2BUILD_ERROR_UNCONVERTED`
189 errorModulesUnconvertedDeps
190)
191
Jingwen Chendcc329a2021-01-26 02:49:03 -0500192func (mode CodegenMode) String() string {
193 switch mode {
194 case Bp2Build:
195 return "Bp2Build"
196 case QueryView:
197 return "QueryView"
Spandan Das5af0bd32022-09-28 20:43:08 +0000198 case ApiBp2build:
199 return "ApiBp2build"
Jingwen Chendcc329a2021-01-26 02:49:03 -0500200 default:
201 return fmt.Sprintf("%d", mode)
202 }
203}
204
Liz Kammerba3ea162021-02-17 13:22:03 -0500205// AddNinjaFileDeps adds dependencies on the specified files to be added to the ninja manifest. The
206// primary builder will be rerun whenever the specified files are modified. Allows us to fulfill the
207// PathContext interface in order to add dependencies on hand-crafted BUILD files. Note: must also
208// call AdditionalNinjaDeps and add them manually to the ninja file.
209func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) {
210 ctx.additionalDeps = append(ctx.additionalDeps, deps...)
211}
212
213// AdditionalNinjaDeps returns additional ninja deps added by CodegenContext
214func (ctx *CodegenContext) AdditionalNinjaDeps() []string {
215 return ctx.additionalDeps
216}
217
Paul Duffinc6390592022-11-04 13:35:21 +0000218func (ctx *CodegenContext) Config() android.Config { return ctx.config }
219func (ctx *CodegenContext) Context() *android.Context { return ctx.context }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500220
221// NewCodegenContext creates a wrapper context that conforms to PathContext for
222// writing BUILD files in the output directory.
Cole Faustb85d1a12022-11-08 18:14:01 -0800223func NewCodegenContext(config android.Config, context *android.Context, mode CodegenMode, topDir string) *CodegenContext {
Liz Kammer6eff3232021-08-26 08:37:59 -0400224 var unconvertedDeps unconvertedDepsMode
225 if config.IsEnvTrue("BP2BUILD_ERROR_UNCONVERTED") {
226 unconvertedDeps = errorModulesUnconvertedDeps
227 }
Liz Kammerba3ea162021-02-17 13:22:03 -0500228 return &CodegenContext{
Liz Kammer6eff3232021-08-26 08:37:59 -0400229 context: context,
230 config: config,
231 mode: mode,
232 unconvertedDepMode: unconvertedDeps,
Cole Faustb85d1a12022-11-08 18:14:01 -0800233 topDir: topDir,
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
Cole Faust18994c72023-02-28 16:02:16 -0800241 for _, propName := range android.SortedKeys(props) {
Liz Kammer0eae52e2021-10-06 10:32:26 -0400242 attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName])
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800243 }
244 return attributes
245}
246
Liz Kammer6eff3232021-08-26 08:37:59 -0400247type conversionResults struct {
248 buildFileToTargets map[string]BazelTargets
249 metrics CodegenMetrics
Liz Kammer6eff3232021-08-26 08:37:59 -0400250}
251
252func (r conversionResults) BuildDirToTargets() map[string]BazelTargets {
253 return r.buildFileToTargets
254}
255
Spandan Dasea2abba2023-06-14 21:30:38 +0000256// struct to store state of go bazel targets
257// this implements bp2buildModule interface and is passed to generateBazelTargets
258type goBazelTarget struct {
259 targetName string
260 targetPackage string
261 bazelRuleClass string
262 bazelRuleLoadLocation string
263 bazelAttributes []interface{}
264}
265
266var _ bp2buildModule = (*goBazelTarget)(nil)
267
268func (g goBazelTarget) TargetName() string {
269 return g.targetName
270}
271
272func (g goBazelTarget) TargetPackage() string {
273 return g.targetPackage
274}
275
276func (g goBazelTarget) BazelRuleClass() string {
277 return g.bazelRuleClass
278}
279
280func (g goBazelTarget) BazelRuleLoadLocation() string {
281 return g.bazelRuleLoadLocation
282}
283
284func (g goBazelTarget) BazelAttributes() []interface{} {
285 return g.bazelAttributes
286}
287
288// Creates a target_compatible_with entry that is *not* compatible with android
289func targetNotCompatibleWithAndroid() bazel.LabelListAttribute {
290 ret := bazel.LabelListAttribute{}
291 ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid,
292 bazel.MakeLabelList(
293 []bazel.Label{
294 bazel.Label{
295 Label: "@platforms//:incompatible",
296 },
297 },
298 ),
299 )
300 return ret
301}
302
303// helper function to return labels for srcs used in bootstrap_go_package and bootstrap_go_binary
304// this function has the following limitations which make it unsuitable for widespread use
305// 1. wildcard patterns in srcs
306// 2. package boundary violations
307// (1) is ok for go since build/blueprint does not support it. (2) _might_ be ok too.
308//
309// Prefer to use `BazelLabelForModuleSrc` instead
310func goSrcLabels(srcs []string, linuxSrcs, darwinSrcs []string) bazel.LabelListAttribute {
311 labels := func(srcs []string) bazel.LabelList {
312 ret := []bazel.Label{}
313 for _, src := range srcs {
314 srcLabel := bazel.Label{
315 Label: ":" + src, // TODO - b/284483729: Fix for possible package boundary violations
316 }
317 ret = append(ret, srcLabel)
318 }
319 return bazel.MakeLabelList(ret)
320 }
321
322 ret := bazel.LabelListAttribute{}
323 // common
324 ret.SetSelectValue(bazel.NoConfigAxis, "", labels(srcs))
325 // linux
326 ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsLinux, labels(linuxSrcs))
327 // darwin
328 ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsDarwin, labels(darwinSrcs))
329 return ret
330}
331
332func goDepLabels(deps []string, goModulesMap nameToGoLibraryModule) bazel.LabelListAttribute {
333 labels := []bazel.Label{}
334 for _, dep := range deps {
335 moduleDir := goModulesMap[dep].Dir
336 if moduleDir == "." {
337 moduleDir = ""
338 }
339 label := bazel.Label{
340 Label: fmt.Sprintf("//%s:%s", moduleDir, dep),
341 }
342 labels = append(labels, label)
343 }
344 return bazel.MakeLabelListAttribute(bazel.MakeLabelList(labels))
345}
346
347// attributes common to blueprint_go_binary and bootstap_go_package
348type goAttributes struct {
349 Importpath bazel.StringAttribute
350 Srcs bazel.LabelListAttribute
351 Deps bazel.LabelListAttribute
352 Target_compatible_with bazel.LabelListAttribute
353}
354
355func generateBazelTargetsGoPackage(ctx *android.Context, g *bootstrap.GoPackage, goModulesMap nameToGoLibraryModule) ([]BazelTarget, []error) {
356 ca := android.CommonAttributes{
357 Name: g.Name(),
358 }
Spandan Dasde623292023-06-14 21:30:38 +0000359
360 // For this bootstrap_go_package dep chain,
361 // A --> B --> C ( ---> depends on)
362 // Soong provides the convenience of only listing B as deps of A even if a src file of A imports C
363 // Bazel OTOH
364 // 1. requires C to be listed in `deps` expllicity.
365 // 2. does not require C to be listed if src of A does not import C
366 //
367 // bp2build does not have sufficient info on whether C is a direct dep of A or not, so for now collect all transitive deps and add them to deps
368 transitiveDeps := transitiveGoDeps(g.Deps(), goModulesMap)
369
Spandan Dasea2abba2023-06-14 21:30:38 +0000370 ga := goAttributes{
371 Importpath: bazel.StringAttribute{
372 Value: proptools.StringPtr(g.GoPkgPath()),
373 },
374 Srcs: goSrcLabels(g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
Spandan Dasde623292023-06-14 21:30:38 +0000375 Deps: goDepLabels(transitiveDeps, goModulesMap),
Spandan Dasea2abba2023-06-14 21:30:38 +0000376 Target_compatible_with: targetNotCompatibleWithAndroid(),
377 }
378
379 lib := goBazelTarget{
380 targetName: g.Name(),
381 targetPackage: ctx.ModuleDir(g),
382 bazelRuleClass: "go_library",
383 bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl",
384 bazelAttributes: []interface{}{&ca, &ga},
385 }
386 // TODO - b/284483729: Create go_test target from testSrcs
387 libTarget, err := generateBazelTarget(ctx, lib)
388 if err != nil {
389 return []BazelTarget{}, []error{err}
390 }
391 return []BazelTarget{libTarget}, nil
392}
393
394type goLibraryModule struct {
395 Dir string
396 Deps []string
397}
398
399type nameToGoLibraryModule map[string]goLibraryModule
400
401// Visit each module in the graph
402// If a module is of type `bootstrap_go_package`, return a map containing metadata like its dir and deps
403func createGoLibraryModuleMap(ctx *android.Context) nameToGoLibraryModule {
404 ret := nameToGoLibraryModule{}
405 ctx.VisitAllModules(func(m blueprint.Module) {
406 moduleType := ctx.ModuleType(m)
407 // We do not need to store information about blueprint_go_binary since it does not have any rdeps
408 if moduleType == "bootstrap_go_package" {
409 ret[m.Name()] = goLibraryModule{
410 Dir: ctx.ModuleDir(m),
411 Deps: m.(*bootstrap.GoPackage).Deps(),
412 }
413 }
414 })
415 return ret
416}
417
Spandan Dasde623292023-06-14 21:30:38 +0000418// Returns the deps in the transitive closure of a go target
419func transitiveGoDeps(directDeps []string, goModulesMap nameToGoLibraryModule) []string {
420 allDeps := directDeps
421 i := 0
422 for i < len(allDeps) {
423 curr := allDeps[i]
424 allDeps = append(allDeps, goModulesMap[curr].Deps...)
425 i += 1
426 }
427 allDeps = android.SortedUniqueStrings(allDeps)
428 return allDeps
429}
430
431func generateBazelTargetsGoBinary(ctx *android.Context, g *bootstrap.GoBinary, goModulesMap nameToGoLibraryModule) ([]BazelTarget, []error) {
432 ca := android.CommonAttributes{
433 Name: g.Name(),
434 }
435
436 // For this bootstrap_go_package dep chain,
437 // A --> B --> C ( ---> depends on)
438 // Soong provides the convenience of only listing B as deps of A even if a src file of A imports C
439 // Bazel OTOH
440 // 1. requires C to be listed in `deps` expllicity.
441 // 2. does not require C to be listed if src of A does not import C
442 //
443 // bp2build does not have sufficient info on whether C is a direct dep of A or not, so for now collect all transitive deps and add them to deps
444 transitiveDeps := transitiveGoDeps(g.Deps(), goModulesMap)
445
446 ga := goAttributes{
447 Srcs: goSrcLabels(g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
448 Deps: goDepLabels(transitiveDeps, goModulesMap),
449 Target_compatible_with: targetNotCompatibleWithAndroid(),
450 }
451
452 bin := goBazelTarget{
453 targetName: g.Name(),
454 targetPackage: ctx.ModuleDir(g),
455 bazelRuleClass: "go_binary",
456 bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl",
457 bazelAttributes: []interface{}{&ca, &ga},
458 }
459 // TODO - b/284483729: Create go_test target from testSrcs
460 binTarget, err := generateBazelTarget(ctx, bin)
461 if err != nil {
462 return []BazelTarget{}, []error{err}
463 }
464 return []BazelTarget{binTarget}, nil
465}
466
Spandan Das69afa982023-07-11 22:22:17 +0000467var (
468 // TODO - b/284483729: Remove this denyilst
469 // Temporary denylist of go binaries that are currently used in mixed builds
470 // This denylist allows us to rollout bp2build converters for go targets without affecting mixed builds
471 goBinaryDenylist = []string{
472 "soong_zip",
473 "zip2zip",
474 "bazel_notice_gen",
475 }
476)
477
Liz Kammer6eff3232021-08-26 08:37:59 -0400478func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) {
Jingwen Chen40067de2021-01-26 21:58:43 -0500479 buildFileToTargets := make(map[string]BazelTargets)
Jingwen Chen164e0862021-02-19 00:48:40 -0500480
481 // Simple metrics tracking for bp2build
usta4f5d2c12022-10-28 23:32:01 -0400482 metrics := CreateCodegenMetrics()
Jingwen Chen164e0862021-02-19 00:48:40 -0500483
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400484 dirs := make(map[string]bool)
485
Liz Kammer6eff3232021-08-26 08:37:59 -0400486 var errs []error
487
Spandan Dasea2abba2023-06-14 21:30:38 +0000488 // Visit go libraries in a pre-run and store its state in a map
489 // The time complexity remains O(N), and this does not add significant wall time.
490 nameToGoLibMap := createGoLibraryModuleMap(ctx.Context())
491
Jingwen Chen164e0862021-02-19 00:48:40 -0500492 bpCtx := ctx.Context()
493 bpCtx.VisitAllModules(func(m blueprint.Module) {
494 dir := bpCtx.ModuleDir(m)
Chris Parsons492bd912022-01-20 12:55:05 -0500495 moduleType := bpCtx.ModuleType(m)
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400496 dirs[dir] = true
497
Liz Kammer2ada09a2021-08-11 00:17:36 -0400498 var targets []BazelTarget
Spandan Dasea2abba2023-06-14 21:30:38 +0000499 var targetErrs []error
Jingwen Chen73850672020-12-14 08:25:34 -0500500
Jingwen Chen164e0862021-02-19 00:48:40 -0500501 switch ctx.Mode() {
Jingwen Chen33832f92021-01-24 22:55:54 -0500502 case Bp2Build:
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000503 // There are two main ways of converting a Soong module to Bazel:
504 // 1) Manually handcrafting a Bazel target and associating the module with its label
505 // 2) Automatically generating with bp2build converters
506 //
507 // bp2build converters are used for the majority of modules.
Liz Kammerba3ea162021-02-17 13:22:03 -0500508 if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000509 // Handle modules converted to handcrafted targets.
510 //
511 // Since these modules are associated with some handcrafted
Cole Faustea602c52022-08-31 14:48:26 -0700512 // target in a BUILD file, we don't autoconvert them.
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000513
514 // Log the module.
Chris Parsons39a16972023-06-08 14:28:51 +0000515 metrics.AddUnconvertedModule(m, moduleType, dir,
516 android.UnconvertedReason{
517 ReasonType: int(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE),
518 })
Liz Kammer2ada09a2021-08-11 00:17:36 -0400519 } else if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000520 // Handle modules converted to generated targets.
521
522 // Log the module.
Chris Parsons39a16972023-06-08 14:28:51 +0000523 metrics.AddConvertedModule(aModule, moduleType, dir)
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000524
525 // Handle modules with unconverted deps. By default, emit a warning.
Liz Kammer6eff3232021-08-26 08:37:59 -0400526 if unconvertedDeps := aModule.GetUnconvertedBp2buildDeps(); len(unconvertedDeps) > 0 {
Sasha Smundakf2bb26f2022-08-04 11:28:15 -0700527 msg := fmt.Sprintf("%s %s:%s depends on unconverted modules: %s",
528 moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", "))
Usta Shresthac6057152022-09-24 00:23:31 -0400529 switch ctx.unconvertedDepMode {
530 case warnUnconvertedDeps:
Liz Kammer6eff3232021-08-26 08:37:59 -0400531 metrics.moduleWithUnconvertedDepsMsgs = append(metrics.moduleWithUnconvertedDepsMsgs, msg)
Usta Shresthac6057152022-09-24 00:23:31 -0400532 case errorModulesUnconvertedDeps:
Liz Kammer6eff3232021-08-26 08:37:59 -0400533 errs = append(errs, fmt.Errorf(msg))
534 return
535 }
536 }
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500537 if unconvertedDeps := aModule.GetMissingBp2buildDeps(); len(unconvertedDeps) > 0 {
Sasha Smundakf2bb26f2022-08-04 11:28:15 -0700538 msg := fmt.Sprintf("%s %s:%s depends on missing modules: %s",
539 moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", "))
Usta Shresthac6057152022-09-24 00:23:31 -0400540 switch ctx.unconvertedDepMode {
541 case warnUnconvertedDeps:
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500542 metrics.moduleWithMissingDepsMsgs = append(metrics.moduleWithMissingDepsMsgs, msg)
Usta Shresthac6057152022-09-24 00:23:31 -0400543 case errorModulesUnconvertedDeps:
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500544 errs = append(errs, fmt.Errorf(msg))
545 return
546 }
547 }
Alix94e26032022-08-16 20:37:33 +0000548 targets, targetErrs = generateBazelTargets(bpCtx, aModule)
549 errs = append(errs, targetErrs...)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400550 for _, t := range targets {
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000551 // A module can potentially generate more than 1 Bazel
552 // target, each of a different rule class.
553 metrics.IncrementRuleClassCount(t.ruleClass)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400554 }
MarkDacek9c094ca2023-03-16 19:15:19 +0000555 } else if _, ok := ctx.Config().BazelModulesForceEnabledByFlag()[m.Name()]; ok && m.Name() != "" {
556 err := fmt.Errorf("Force Enabled Module %s not converted", m.Name())
557 errs = append(errs, err)
Chris Parsons39a16972023-06-08 14:28:51 +0000558 } else if aModule, ok := m.(android.Module); ok {
559 reason := aModule.GetUnconvertedReason()
560 if reason == nil {
561 panic(fmt.Errorf("module '%s' was neither converted nor marked unconvertible with bp2build", aModule.Name()))
562 } else {
563 metrics.AddUnconvertedModule(m, moduleType, dir, *reason)
564 }
565 return
Spandan Dasea2abba2023-06-14 21:30:38 +0000566 } else if glib, ok := m.(*bootstrap.GoPackage); ok {
567 targets, targetErrs = generateBazelTargetsGoPackage(bpCtx, glib, nameToGoLibMap)
568 errs = append(errs, targetErrs...)
569 metrics.IncrementRuleClassCount("go_library")
Spandan Das69afa982023-07-11 22:22:17 +0000570 } else if gbin, ok := m.(*bootstrap.GoBinary); ok && !android.InList(m.Name(), goBinaryDenylist) {
Spandan Dasde623292023-06-14 21:30:38 +0000571 targets, targetErrs = generateBazelTargetsGoBinary(bpCtx, gbin, nameToGoLibMap)
572 errs = append(errs, targetErrs...)
573 metrics.IncrementRuleClassCount("go_binary")
Liz Kammerfc46bc12021-02-19 11:06:17 -0500574 } else {
Chris Parsons39a16972023-06-08 14:28:51 +0000575 metrics.AddUnconvertedModule(m, moduleType, dir, android.UnconvertedReason{
576 ReasonType: int(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED),
577 })
Liz Kammerba3ea162021-02-17 13:22:03 -0500578 return
Jingwen Chen73850672020-12-14 08:25:34 -0500579 }
Jingwen Chen33832f92021-01-24 22:55:54 -0500580 case QueryView:
Jingwen Chen96af35b2021-02-08 00:49:32 -0500581 // Blocklist certain module types from being generated.
Jingwen Chen164e0862021-02-19 00:48:40 -0500582 if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" {
Jingwen Chen96af35b2021-02-08 00:49:32 -0500583 // package module name contain slashes, and thus cannot
584 // be mapped cleanly to a bazel label.
585 return
586 }
Alix94e26032022-08-16 20:37:33 +0000587 t, err := generateSoongModuleTarget(bpCtx, m)
588 if err != nil {
589 errs = append(errs, err)
590 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400591 targets = append(targets, t)
Spandan Das5af0bd32022-09-28 20:43:08 +0000592 case ApiBp2build:
593 if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
594 targets, errs = generateBazelTargets(bpCtx, aModule)
595 }
Jingwen Chen33832f92021-01-24 22:55:54 -0500596 default:
Liz Kammer6eff3232021-08-26 08:37:59 -0400597 errs = append(errs, fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode()))
598 return
Jingwen Chen73850672020-12-14 08:25:34 -0500599 }
600
Spandan Dasabedff02023-03-07 19:24:34 +0000601 for _, target := range targets {
602 targetDir := target.PackageName()
603 buildFileToTargets[targetDir] = append(buildFileToTargets[targetDir], target)
604 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800605 })
Liz Kammer6eff3232021-08-26 08:37:59 -0400606
607 if len(errs) > 0 {
608 return conversionResults{}, errs
609 }
610
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400611 if generateFilegroups {
612 // Add a filegroup target that exposes all sources in the subtree of this package
613 // NOTE: This also means we generate a BUILD file for every Android.bp file (as long as it has at least one module)
Cole Faust324a92e2022-08-23 15:29:05 -0700614 //
615 // This works because: https://bazel.build/reference/be/functions#exports_files
616 // "As a legacy behaviour, also files mentioned as input to a rule are exported with the
617 // default visibility until the flag --incompatible_no_implicit_file_export is flipped. However, this behavior
618 // should not be relied upon and actively migrated away from."
619 //
620 // TODO(b/198619163): We should change this to export_files(glob(["**/*"])) instead, but doing that causes these errors:
621 // "Error in exports_files: generated label '//external/avb:avbtool' conflicts with existing py_binary rule"
622 // So we need to solve all the "target ... is both a rule and a file" warnings first.
Usta Shresthac6057152022-09-24 00:23:31 -0400623 for dir := range dirs {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400624 buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{
625 name: "bp2build_all_srcs",
626 content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]))`,
627 ruleClass: "filegroup",
628 })
629 }
630 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500631
Liz Kammer6eff3232021-08-26 08:37:59 -0400632 return conversionResults{
633 buildFileToTargets: buildFileToTargets,
634 metrics: metrics,
Liz Kammer6eff3232021-08-26 08:37:59 -0400635 }, errs
Jingwen Chen164e0862021-02-19 00:48:40 -0500636}
637
Alix94e26032022-08-16 20:37:33 +0000638func generateBazelTargets(ctx bpToBuildContext, m android.Module) ([]BazelTarget, []error) {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400639 var targets []BazelTarget
Alix94e26032022-08-16 20:37:33 +0000640 var errs []error
Liz Kammer2ada09a2021-08-11 00:17:36 -0400641 for _, m := range m.Bp2buildTargets() {
Alix94e26032022-08-16 20:37:33 +0000642 target, err := generateBazelTarget(ctx, m)
643 if err != nil {
644 errs = append(errs, err)
645 return targets, errs
646 }
647 targets = append(targets, target)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400648 }
Alix94e26032022-08-16 20:37:33 +0000649 return targets, errs
Liz Kammer2ada09a2021-08-11 00:17:36 -0400650}
651
652type bp2buildModule interface {
653 TargetName() string
654 TargetPackage() string
655 BazelRuleClass() string
656 BazelRuleLoadLocation() string
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000657 BazelAttributes() []interface{}
Liz Kammer2ada09a2021-08-11 00:17:36 -0400658}
659
Alix94e26032022-08-16 20:37:33 +0000660func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) (BazelTarget, error) {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400661 ruleClass := m.BazelRuleClass()
662 bzlLoadLocation := m.BazelRuleLoadLocation()
Jingwen Chen40067de2021-01-26 21:58:43 -0500663
Jingwen Chen73850672020-12-14 08:25:34 -0500664 // extract the bazel attributes from the module.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000665 attrs := m.BazelAttributes()
Alix94e26032022-08-16 20:37:33 +0000666 props, err := extractModuleProperties(attrs, true)
667 if err != nil {
668 return BazelTarget{}, err
669 }
Jingwen Chen73850672020-12-14 08:25:34 -0500670
Liz Kammer0eae52e2021-10-06 10:32:26 -0400671 // name is handled in a special manner
672 delete(props.Attrs, "name")
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500673
Jingwen Chen73850672020-12-14 08:25:34 -0500674 // Return the Bazel target with rule class and attributes, ready to be
675 // code-generated.
676 attributes := propsToAttributes(props.Attrs)
Sasha Smundakfb589492022-08-04 11:13:27 -0700677 var content string
Liz Kammer2ada09a2021-08-11 00:17:36 -0400678 targetName := m.TargetName()
Sasha Smundakfb589492022-08-04 11:13:27 -0700679 if targetName != "" {
680 content = fmt.Sprintf(ruleTargetTemplate, ruleClass, targetName, attributes)
681 } else {
682 content = fmt.Sprintf(unnamedRuleTargetTemplate, ruleClass, attributes)
683 }
Jingwen Chen73850672020-12-14 08:25:34 -0500684 return BazelTarget{
Jingwen Chen40067de2021-01-26 21:58:43 -0500685 name: targetName,
Liz Kammer2ada09a2021-08-11 00:17:36 -0400686 packageName: m.TargetPackage(),
Jingwen Chen40067de2021-01-26 21:58:43 -0500687 ruleClass: ruleClass,
688 bzlLoadLocation: bzlLoadLocation,
Sasha Smundakfb589492022-08-04 11:13:27 -0700689 content: content,
Alix94e26032022-08-16 20:37:33 +0000690 }, nil
Jingwen Chen73850672020-12-14 08:25:34 -0500691}
692
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800693// Convert a module and its deps and props into a Bazel macro/rule
694// representation in the BUILD file.
Alix94e26032022-08-16 20:37:33 +0000695func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) (BazelTarget, error) {
696 props, err := getBuildProperties(ctx, m)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800697
698 // TODO(b/163018919): DirectDeps can have duplicate (module, variant)
699 // items, if the modules are added using different DependencyTag. Figure
700 // out the implications of that.
701 depLabels := map[string]bool{}
702 if aModule, ok := m.(android.Module); ok {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500703 ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800704 depLabels[qualifiedTargetLabel(ctx, depModule)] = true
705 })
706 }
Liz Kammer0eae52e2021-10-06 10:32:26 -0400707
Usta Shresthadb46a9b2022-07-11 11:29:56 -0400708 for p := range ignoredPropNames {
Liz Kammer0eae52e2021-10-06 10:32:26 -0400709 delete(props.Attrs, p)
710 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800711 attributes := propsToAttributes(props.Attrs)
712
713 depLabelList := "[\n"
Usta Shresthadb46a9b2022-07-11 11:29:56 -0400714 for depLabel := range depLabels {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800715 depLabelList += fmt.Sprintf(" %q,\n", depLabel)
716 }
717 depLabelList += " ]"
718
719 targetName := targetNameWithVariant(ctx, m)
720 return BazelTarget{
Spandan Dasabedff02023-03-07 19:24:34 +0000721 name: targetName,
722 packageName: ctx.ModuleDir(m),
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800723 content: fmt.Sprintf(
Sasha Smundakfb589492022-08-04 11:13:27 -0700724 soongModuleTargetTemplate,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800725 targetName,
726 ctx.ModuleName(m),
727 canonicalizeModuleType(ctx.ModuleType(m)),
728 ctx.ModuleSubDir(m),
729 depLabelList,
730 attributes),
Alix94e26032022-08-16 20:37:33 +0000731 }, err
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800732}
733
Alix94e26032022-08-16 20:37:33 +0000734func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) (BazelAttributes, error) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800735 // TODO: this omits properties for blueprint modules (blueprint_go_binary,
736 // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately.
737 if aModule, ok := m.(android.Module); ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000738 return extractModuleProperties(aModule.GetProperties(), false)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800739 }
740
Alix94e26032022-08-16 20:37:33 +0000741 return BazelAttributes{}, nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800742}
743
744// Generically extract module properties and types into a map, keyed by the module property name.
Alix94e26032022-08-16 20:37:33 +0000745func extractModuleProperties(props []interface{}, checkForDuplicateProperties bool) (BazelAttributes, error) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800746 ret := map[string]string{}
747
748 // Iterate over this android.Module's property structs.
Liz Kammer2ada09a2021-08-11 00:17:36 -0400749 for _, properties := range props {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800750 propertiesValue := reflect.ValueOf(properties)
751 // Check that propertiesValue is a pointer to the Properties struct, like
752 // *cc.BaseLinkerProperties or *java.CompilerProperties.
753 //
754 // propertiesValue can also be type-asserted to the structs to
755 // manipulate internal props, if needed.
756 if isStructPtr(propertiesValue.Type()) {
757 structValue := propertiesValue.Elem()
Alix94e26032022-08-16 20:37:33 +0000758 ok, err := extractStructProperties(structValue, 0)
759 if err != nil {
760 return BazelAttributes{}, err
761 }
762 for k, v := range ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000763 if existing, exists := ret[k]; checkForDuplicateProperties && exists {
Alix94e26032022-08-16 20:37:33 +0000764 return BazelAttributes{}, fmt.Errorf(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000765 "%s (%v) is present in properties whereas it should be consolidated into a commonAttributes",
Alix94e26032022-08-16 20:37:33 +0000766 k, existing)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000767 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800768 ret[k] = v
769 }
770 } else {
Alix94e26032022-08-16 20:37:33 +0000771 return BazelAttributes{},
772 fmt.Errorf(
773 "properties must be a pointer to a struct, got %T",
774 propertiesValue.Interface())
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800775 }
776 }
777
Liz Kammer2ada09a2021-08-11 00:17:36 -0400778 return BazelAttributes{
779 Attrs: ret,
Alix94e26032022-08-16 20:37:33 +0000780 }, nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800781}
782
783func isStructPtr(t reflect.Type) bool {
784 return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
785}
786
787// prettyPrint a property value into the equivalent Starlark representation
788// recursively.
Jingwen Chen58ff6802021-11-17 12:14:41 +0000789func prettyPrint(propertyValue reflect.Value, indent int, emitZeroValues bool) (string, error) {
790 if !emitZeroValues && isZero(propertyValue) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800791 // A property value being set or unset actually matters -- Soong does set default
792 // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at
793 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480
794 //
Jingwen Chenfc490bd2021-03-30 10:24:19 +0000795 // In Bazel-parlance, we would use "attr.<type>(default = <default
796 // value>)" to set the default value of unset attributes. In the cases
797 // where the bp2build converter didn't set the default value within the
798 // mutator when creating the BazelTargetModule, this would be a zero
Jingwen Chen63930982021-03-24 10:04:33 -0400799 // value. For those cases, we return an empty string so we don't
800 // unnecessarily generate empty values.
801 return "", nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800802 }
803
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800804 switch propertyValue.Kind() {
805 case reflect.String:
Liz Kammer72beb342022-02-03 08:42:10 -0500806 return fmt.Sprintf("\"%v\"", escapeString(propertyValue.String())), nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800807 case reflect.Bool:
Liz Kammer72beb342022-02-03 08:42:10 -0500808 return starlark_fmt.PrintBool(propertyValue.Bool()), nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800809 case reflect.Int, reflect.Uint, reflect.Int64:
Liz Kammer72beb342022-02-03 08:42:10 -0500810 return fmt.Sprintf("%v", propertyValue.Interface()), nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800811 case reflect.Ptr:
Jingwen Chen58ff6802021-11-17 12:14:41 +0000812 return prettyPrint(propertyValue.Elem(), indent, emitZeroValues)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800813 case reflect.Slice:
Liz Kammer72beb342022-02-03 08:42:10 -0500814 elements := make([]string, 0, propertyValue.Len())
815 for i := 0; i < propertyValue.Len(); i++ {
816 val, err := prettyPrint(propertyValue.Index(i), indent, emitZeroValues)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800817 if err != nil {
818 return "", err
819 }
Liz Kammer72beb342022-02-03 08:42:10 -0500820 if val != "" {
821 elements = append(elements, val)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800822 }
823 }
Sam Delmerico932c01c2022-03-25 16:33:26 +0000824 return starlark_fmt.PrintList(elements, indent, func(s string) string {
825 return "%s"
826 }), nil
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000827
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800828 case reflect.Struct:
Jingwen Chen5d864492021-02-24 07:20:12 -0500829 // Special cases where the bp2build sends additional information to the codegenerator
830 // by wrapping the attributes in a custom struct type.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000831 if attr, ok := propertyValue.Interface().(bazel.Attribute); ok {
832 return prettyPrintAttribute(attr, indent)
Liz Kammer356f7d42021-01-26 09:18:53 -0500833 } else if label, ok := propertyValue.Interface().(bazel.Label); ok {
834 return fmt.Sprintf("%q", label.Label), nil
835 }
836
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800837 // Sort and print the struct props by the key.
Alix94e26032022-08-16 20:37:33 +0000838 structProps, err := extractStructProperties(propertyValue, indent)
839
840 if err != nil {
841 return "", err
842 }
843
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000844 if len(structProps) == 0 {
845 return "", nil
846 }
Liz Kammer72beb342022-02-03 08:42:10 -0500847 return starlark_fmt.PrintDict(structProps, indent), nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800848 case reflect.Interface:
849 // TODO(b/164227191): implement pretty print for interfaces.
850 // Interfaces are used for for arch, multilib and target properties.
851 return "", nil
Spandan Das6a448ec2023-04-19 17:36:12 +0000852 case reflect.Map:
853 if v, ok := propertyValue.Interface().(bazel.StringMapAttribute); ok {
854 return starlark_fmt.PrintStringStringDict(v, indent), nil
855 }
856 return "", fmt.Errorf("bp2build expects map of type map[string]string for field: %s", propertyValue)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800857 default:
858 return "", fmt.Errorf(
859 "unexpected kind for property struct field: %s", propertyValue.Kind())
860 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800861}
862
863// Converts a reflected property struct value into a map of property names and property values,
864// which each property value correctly pretty-printed and indented at the right nest level,
865// since property structs can be nested. In Starlark, nested structs are represented as nested
866// dicts: https://docs.bazel.build/skylark/lib/dict.html
Alix94e26032022-08-16 20:37:33 +0000867func extractStructProperties(structValue reflect.Value, indent int) (map[string]string, error) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800868 if structValue.Kind() != reflect.Struct {
Alix94e26032022-08-16 20:37:33 +0000869 return map[string]string{}, fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind())
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800870 }
871
Alix94e26032022-08-16 20:37:33 +0000872 var err error
873
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800874 ret := map[string]string{}
875 structType := structValue.Type()
876 for i := 0; i < structValue.NumField(); i++ {
877 field := structType.Field(i)
878 if shouldSkipStructField(field) {
879 continue
880 }
881
882 fieldValue := structValue.Field(i)
883 if isZero(fieldValue) {
884 // Ignore zero-valued fields
885 continue
886 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400887
Liz Kammer32a03392021-09-14 11:17:21 -0400888 // if the struct is embedded (anonymous), flatten the properties into the containing struct
889 if field.Anonymous {
890 if field.Type.Kind() == reflect.Ptr {
891 fieldValue = fieldValue.Elem()
892 }
893 if fieldValue.Type().Kind() == reflect.Struct {
Alix94e26032022-08-16 20:37:33 +0000894 propsToMerge, err := extractStructProperties(fieldValue, indent)
895 if err != nil {
896 return map[string]string{}, err
897 }
Liz Kammer32a03392021-09-14 11:17:21 -0400898 for prop, value := range propsToMerge {
899 ret[prop] = value
900 }
901 continue
902 }
903 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800904
905 propertyName := proptools.PropertyNameForField(field.Name)
Alix94e26032022-08-16 20:37:33 +0000906 var prettyPrintedValue string
907 prettyPrintedValue, err = prettyPrint(fieldValue, indent+1, false)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800908 if err != nil {
Alix94e26032022-08-16 20:37:33 +0000909 return map[string]string{}, fmt.Errorf(
910 "Error while parsing property: %q. %s",
911 propertyName,
912 err)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800913 }
914 if prettyPrintedValue != "" {
915 ret[propertyName] = prettyPrintedValue
916 }
917 }
918
Alix94e26032022-08-16 20:37:33 +0000919 return ret, nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800920}
921
922func isZero(value reflect.Value) bool {
923 switch value.Kind() {
924 case reflect.Func, reflect.Map, reflect.Slice:
925 return value.IsNil()
926 case reflect.Array:
927 valueIsZero := true
928 for i := 0; i < value.Len(); i++ {
929 valueIsZero = valueIsZero && isZero(value.Index(i))
930 }
931 return valueIsZero
932 case reflect.Struct:
933 valueIsZero := true
934 for i := 0; i < value.NumField(); i++ {
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200935 valueIsZero = valueIsZero && isZero(value.Field(i))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800936 }
937 return valueIsZero
938 case reflect.Ptr:
939 if !value.IsNil() {
940 return isZero(reflect.Indirect(value))
941 } else {
942 return true
943 }
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500944 // Always print bool/strings, if you want a bool/string attribute to be able to take the default value, use a
945 // pointer instead
946 case reflect.Bool, reflect.String:
Liz Kammerd366c902021-06-03 13:43:01 -0400947 return false
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800948 default:
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400949 if !value.IsValid() {
950 return true
951 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800952 zeroValue := reflect.Zero(value.Type())
953 result := value.Interface() == zeroValue.Interface()
954 return result
955 }
956}
957
958func escapeString(s string) string {
959 s = strings.ReplaceAll(s, "\\", "\\\\")
Jingwen Chen58a12b82021-03-30 13:08:36 +0000960
961 // b/184026959: Reverse the application of some common control sequences.
962 // These must be generated literally in the BUILD file.
963 s = strings.ReplaceAll(s, "\t", "\\t")
964 s = strings.ReplaceAll(s, "\n", "\\n")
965 s = strings.ReplaceAll(s, "\r", "\\r")
966
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800967 return strings.ReplaceAll(s, "\"", "\\\"")
968}
969
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800970func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
971 name := ""
972 if c.ModuleSubDir(logicModule) != "" {
973 // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes.
974 name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule)
975 } else {
976 name = c.ModuleName(logicModule)
977 }
978
979 return strings.Replace(name, "//", "", 1)
980}
981
982func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string {
983 return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule))
984}