blob: 08790d13e8a44d14109a63937d93a1cb45e8cfa0 [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
17import (
18 "android/soong/android"
Liz Kammer356f7d42021-01-26 09:18:53 -050019 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080020 "fmt"
21 "reflect"
22 "strings"
23
24 "github.com/google/blueprint"
25 "github.com/google/blueprint/proptools"
26)
27
28type BazelAttributes struct {
29 Attrs map[string]string
30}
31
32type BazelTarget struct {
Jingwen Chen40067de2021-01-26 21:58:43 -050033 name string
34 content string
35 ruleClass string
36 bzlLoadLocation string
37}
38
39// IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file,
40// as opposed to a native rule built into Bazel.
41func (t BazelTarget) IsLoadedFromStarlark() bool {
42 return t.bzlLoadLocation != ""
43}
44
45// BazelTargets is a typedef for a slice of BazelTarget objects.
46type BazelTargets []BazelTarget
47
48// String returns the string representation of BazelTargets, without load
49// statements (use LoadStatements for that), since the targets are usually not
50// adjacent to the load statements at the top of the BUILD file.
51func (targets BazelTargets) String() string {
52 var res string
53 for i, target := range targets {
54 res += target.content
55 if i != len(targets)-1 {
56 res += "\n\n"
57 }
58 }
59 return res
60}
61
62// LoadStatements return the string representation of the sorted and deduplicated
63// Starlark rule load statements needed by a group of BazelTargets.
64func (targets BazelTargets) LoadStatements() string {
65 bzlToLoadedSymbols := map[string][]string{}
66 for _, target := range targets {
67 if target.IsLoadedFromStarlark() {
68 bzlToLoadedSymbols[target.bzlLoadLocation] =
69 append(bzlToLoadedSymbols[target.bzlLoadLocation], target.ruleClass)
70 }
71 }
72
73 var loadStatements []string
74 for bzl, ruleClasses := range bzlToLoadedSymbols {
75 loadStatement := "load(\""
76 loadStatement += bzl
77 loadStatement += "\", "
78 ruleClasses = android.SortedUniqueStrings(ruleClasses)
79 for i, ruleClass := range ruleClasses {
80 loadStatement += "\"" + ruleClass + "\""
81 if i != len(ruleClasses)-1 {
82 loadStatement += ", "
83 }
84 }
85 loadStatement += ")"
86 loadStatements = append(loadStatements, loadStatement)
87 }
88 return strings.Join(android.SortedUniqueStrings(loadStatements), "\n")
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089}
90
91type bpToBuildContext interface {
92 ModuleName(module blueprint.Module) string
93 ModuleDir(module blueprint.Module) string
94 ModuleSubDir(module blueprint.Module) string
95 ModuleType(module blueprint.Module) string
96
Jingwen Chendaa54bc2020-12-14 02:58:54 -050097 VisitAllModules(visit func(blueprint.Module))
98 VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module))
99}
100
101type CodegenContext struct {
Liz Kammerba3ea162021-02-17 13:22:03 -0500102 config android.Config
103 context android.Context
104 mode CodegenMode
105 additionalDeps []string
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500106}
107
Jingwen Chen164e0862021-02-19 00:48:40 -0500108func (c *CodegenContext) Mode() CodegenMode {
109 return c.mode
110}
111
Jingwen Chen33832f92021-01-24 22:55:54 -0500112// CodegenMode is an enum to differentiate code-generation modes.
113type CodegenMode int
114
115const (
116 // Bp2Build: generate BUILD files with targets buildable by Bazel directly.
117 //
118 // This mode is used for the Soong->Bazel build definition conversion.
119 Bp2Build CodegenMode = iota
120
121 // QueryView: generate BUILD files with targets representing fully mutated
122 // Soong modules, representing the fully configured Soong module graph with
123 // variants and dependency endges.
124 //
125 // This mode is used for discovering and introspecting the existing Soong
126 // module graph.
127 QueryView
128)
129
Jingwen Chendcc329a2021-01-26 02:49:03 -0500130func (mode CodegenMode) String() string {
131 switch mode {
132 case Bp2Build:
133 return "Bp2Build"
134 case QueryView:
135 return "QueryView"
136 default:
137 return fmt.Sprintf("%d", mode)
138 }
139}
140
Liz Kammerba3ea162021-02-17 13:22:03 -0500141// AddNinjaFileDeps adds dependencies on the specified files to be added to the ninja manifest. The
142// primary builder will be rerun whenever the specified files are modified. Allows us to fulfill the
143// PathContext interface in order to add dependencies on hand-crafted BUILD files. Note: must also
144// call AdditionalNinjaDeps and add them manually to the ninja file.
145func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) {
146 ctx.additionalDeps = append(ctx.additionalDeps, deps...)
147}
148
149// AdditionalNinjaDeps returns additional ninja deps added by CodegenContext
150func (ctx *CodegenContext) AdditionalNinjaDeps() []string {
151 return ctx.additionalDeps
152}
153
154func (ctx *CodegenContext) Config() android.Config { return ctx.config }
155func (ctx *CodegenContext) Context() android.Context { return ctx.context }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500156
157// NewCodegenContext creates a wrapper context that conforms to PathContext for
158// writing BUILD files in the output directory.
Liz Kammerba3ea162021-02-17 13:22:03 -0500159func NewCodegenContext(config android.Config, context android.Context, mode CodegenMode) *CodegenContext {
160 return &CodegenContext{
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500161 context: context,
162 config: config,
Jingwen Chen33832f92021-01-24 22:55:54 -0500163 mode: mode,
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500164 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800165}
166
167// props is an unsorted map. This function ensures that
168// the generated attributes are sorted to ensure determinism.
169func propsToAttributes(props map[string]string) string {
170 var attributes string
171 for _, propName := range android.SortedStringKeys(props) {
172 if shouldGenerateAttribute(propName) {
173 attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName])
174 }
175 }
176 return attributes
177}
178
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400179func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (map[string]BazelTargets, CodegenMetrics) {
Jingwen Chen40067de2021-01-26 21:58:43 -0500180 buildFileToTargets := make(map[string]BazelTargets)
Liz Kammerba3ea162021-02-17 13:22:03 -0500181 buildFileToAppend := make(map[string]bool)
Jingwen Chen164e0862021-02-19 00:48:40 -0500182
183 // Simple metrics tracking for bp2build
Liz Kammerba3ea162021-02-17 13:22:03 -0500184 metrics := CodegenMetrics{
185 RuleClassCount: make(map[string]int),
186 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500187
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400188 dirs := make(map[string]bool)
189
Jingwen Chen164e0862021-02-19 00:48:40 -0500190 bpCtx := ctx.Context()
191 bpCtx.VisitAllModules(func(m blueprint.Module) {
192 dir := bpCtx.ModuleDir(m)
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400193 dirs[dir] = true
194
Jingwen Chen73850672020-12-14 08:25:34 -0500195 var t BazelTarget
196
Jingwen Chen164e0862021-02-19 00:48:40 -0500197 switch ctx.Mode() {
Jingwen Chen33832f92021-01-24 22:55:54 -0500198 case Bp2Build:
Liz Kammerba3ea162021-02-17 13:22:03 -0500199 if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
200 metrics.handCraftedTargetCount += 1
201 metrics.TotalModuleCount += 1
202 pathToBuildFile := getBazelPackagePath(b)
203 // We are using the entire contents of handcrafted build file, so if multiple targets within
204 // a package have handcrafted targets, we only want to include the contents one time.
205 if _, exists := buildFileToAppend[pathToBuildFile]; exists {
206 return
207 }
208 var err error
209 t, err = getHandcraftedBuildContent(ctx, b, pathToBuildFile)
210 if err != nil {
211 panic(fmt.Errorf("Error converting %s: %s", bpCtx.ModuleName(m), err))
212 }
213 // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
214 // something more targeted based on the rule type and target
215 buildFileToAppend[pathToBuildFile] = true
216 } else if btm, ok := m.(android.BazelTargetModule); ok {
217 t = generateBazelTarget(bpCtx, m, btm)
218 metrics.RuleClassCount[t.ruleClass] += 1
Liz Kammerfc46bc12021-02-19 11:06:17 -0500219 } else {
Liz Kammerba3ea162021-02-17 13:22:03 -0500220 metrics.TotalModuleCount += 1
221 return
Jingwen Chen73850672020-12-14 08:25:34 -0500222 }
Jingwen Chen33832f92021-01-24 22:55:54 -0500223 case QueryView:
Jingwen Chen96af35b2021-02-08 00:49:32 -0500224 // Blocklist certain module types from being generated.
Jingwen Chen164e0862021-02-19 00:48:40 -0500225 if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" {
Jingwen Chen96af35b2021-02-08 00:49:32 -0500226 // package module name contain slashes, and thus cannot
227 // be mapped cleanly to a bazel label.
228 return
229 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500230 t = generateSoongModuleTarget(bpCtx, m)
Jingwen Chen33832f92021-01-24 22:55:54 -0500231 default:
Jingwen Chen164e0862021-02-19 00:48:40 -0500232 panic(fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode()))
Jingwen Chen73850672020-12-14 08:25:34 -0500233 }
234
Liz Kammer356f7d42021-01-26 09:18:53 -0500235 buildFileToTargets[dir] = append(buildFileToTargets[dir], t)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800236 })
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400237 if generateFilegroups {
238 // Add a filegroup target that exposes all sources in the subtree of this package
239 // NOTE: This also means we generate a BUILD file for every Android.bp file (as long as it has at least one module)
240 for dir, _ := range dirs {
241 buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{
242 name: "bp2build_all_srcs",
243 content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]))`,
244 ruleClass: "filegroup",
245 })
246 }
247 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500248
Jingwen Chen164e0862021-02-19 00:48:40 -0500249 return buildFileToTargets, metrics
250}
251
Liz Kammerba3ea162021-02-17 13:22:03 -0500252func getBazelPackagePath(b android.Bazelable) string {
Liz Kammerbdc60992021-02-24 16:55:11 -0500253 label := b.HandcraftedLabel()
Liz Kammerba3ea162021-02-17 13:22:03 -0500254 pathToBuildFile := strings.TrimPrefix(label, "//")
255 pathToBuildFile = strings.Split(pathToBuildFile, ":")[0]
256 return pathToBuildFile
257}
258
259func getHandcraftedBuildContent(ctx *CodegenContext, b android.Bazelable, pathToBuildFile string) (BazelTarget, error) {
260 p := android.ExistentPathForSource(ctx, pathToBuildFile, HandcraftedBuildFileName)
261 if !p.Valid() {
262 return BazelTarget{}, fmt.Errorf("Could not find file %q for handcrafted target.", pathToBuildFile)
263 }
264 c, err := b.GetBazelBuildFileContents(ctx.Config(), pathToBuildFile, HandcraftedBuildFileName)
265 if err != nil {
266 return BazelTarget{}, err
267 }
268 // TODO(b/181575318): once this is more targeted, we need to include name, rule class, etc
269 return BazelTarget{
270 content: c,
271 }, nil
272}
273
274func generateBazelTarget(ctx bpToBuildContext, m blueprint.Module, btm android.BazelTargetModule) BazelTarget {
275 ruleClass := btm.RuleClass()
276 bzlLoadLocation := btm.BzlLoadLocation()
Jingwen Chen40067de2021-01-26 21:58:43 -0500277
Jingwen Chen73850672020-12-14 08:25:34 -0500278 // extract the bazel attributes from the module.
279 props := getBuildProperties(ctx, m)
280
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500281 delete(props.Attrs, "bp2build_available")
282
Jingwen Chen73850672020-12-14 08:25:34 -0500283 // Return the Bazel target with rule class and attributes, ready to be
284 // code-generated.
285 attributes := propsToAttributes(props.Attrs)
286 targetName := targetNameForBp2Build(ctx, m)
287 return BazelTarget{
Jingwen Chen40067de2021-01-26 21:58:43 -0500288 name: targetName,
289 ruleClass: ruleClass,
290 bzlLoadLocation: bzlLoadLocation,
Jingwen Chen73850672020-12-14 08:25:34 -0500291 content: fmt.Sprintf(
292 bazelTarget,
293 ruleClass,
294 targetName,
295 attributes,
296 ),
297 }
298}
299
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800300// Convert a module and its deps and props into a Bazel macro/rule
301// representation in the BUILD file.
302func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
303 props := getBuildProperties(ctx, m)
304
305 // TODO(b/163018919): DirectDeps can have duplicate (module, variant)
306 // items, if the modules are added using different DependencyTag. Figure
307 // out the implications of that.
308 depLabels := map[string]bool{}
309 if aModule, ok := m.(android.Module); ok {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500310 ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800311 depLabels[qualifiedTargetLabel(ctx, depModule)] = true
312 })
313 }
314 attributes := propsToAttributes(props.Attrs)
315
316 depLabelList := "[\n"
317 for depLabel, _ := range depLabels {
318 depLabelList += fmt.Sprintf(" %q,\n", depLabel)
319 }
320 depLabelList += " ]"
321
322 targetName := targetNameWithVariant(ctx, m)
323 return BazelTarget{
324 name: targetName,
325 content: fmt.Sprintf(
326 soongModuleTarget,
327 targetName,
328 ctx.ModuleName(m),
329 canonicalizeModuleType(ctx.ModuleType(m)),
330 ctx.ModuleSubDir(m),
331 depLabelList,
332 attributes),
333 }
334}
335
336func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) BazelAttributes {
337 var allProps map[string]string
338 // TODO: this omits properties for blueprint modules (blueprint_go_binary,
339 // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately.
340 if aModule, ok := m.(android.Module); ok {
341 allProps = ExtractModuleProperties(aModule)
342 }
343
344 return BazelAttributes{
345 Attrs: allProps,
346 }
347}
348
349// Generically extract module properties and types into a map, keyed by the module property name.
350func ExtractModuleProperties(aModule android.Module) map[string]string {
351 ret := map[string]string{}
352
353 // Iterate over this android.Module's property structs.
354 for _, properties := range aModule.GetProperties() {
355 propertiesValue := reflect.ValueOf(properties)
356 // Check that propertiesValue is a pointer to the Properties struct, like
357 // *cc.BaseLinkerProperties or *java.CompilerProperties.
358 //
359 // propertiesValue can also be type-asserted to the structs to
360 // manipulate internal props, if needed.
361 if isStructPtr(propertiesValue.Type()) {
362 structValue := propertiesValue.Elem()
363 for k, v := range extractStructProperties(structValue, 0) {
364 ret[k] = v
365 }
366 } else {
367 panic(fmt.Errorf(
368 "properties must be a pointer to a struct, got %T",
369 propertiesValue.Interface()))
370 }
371 }
372
373 return ret
374}
375
376func isStructPtr(t reflect.Type) bool {
377 return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
378}
379
380// prettyPrint a property value into the equivalent Starlark representation
381// recursively.
382func prettyPrint(propertyValue reflect.Value, indent int) (string, error) {
383 if isZero(propertyValue) {
384 // A property value being set or unset actually matters -- Soong does set default
385 // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at
386 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480
387 //
Jingwen Chenfc490bd2021-03-30 10:24:19 +0000388 // In Bazel-parlance, we would use "attr.<type>(default = <default
389 // value>)" to set the default value of unset attributes. In the cases
390 // where the bp2build converter didn't set the default value within the
391 // mutator when creating the BazelTargetModule, this would be a zero
Jingwen Chen63930982021-03-24 10:04:33 -0400392 // value. For those cases, we return an empty string so we don't
393 // unnecessarily generate empty values.
394 return "", nil
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800395 }
396
397 var ret string
398 switch propertyValue.Kind() {
399 case reflect.String:
400 ret = fmt.Sprintf("\"%v\"", escapeString(propertyValue.String()))
401 case reflect.Bool:
402 ret = strings.Title(fmt.Sprintf("%v", propertyValue.Interface()))
403 case reflect.Int, reflect.Uint, reflect.Int64:
404 ret = fmt.Sprintf("%v", propertyValue.Interface())
405 case reflect.Ptr:
406 return prettyPrint(propertyValue.Elem(), indent)
407 case reflect.Slice:
Jingwen Chen63930982021-03-24 10:04:33 -0400408 if propertyValue.Len() == 0 {
409 return "", nil
410 }
411
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000412 if propertyValue.Len() == 1 {
413 // Single-line list for list with only 1 element
414 ret += "["
415 indexedValue, err := prettyPrint(propertyValue.Index(0), indent)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800416 if err != nil {
417 return "", err
418 }
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000419 ret += indexedValue
420 ret += "]"
421 } else {
422 // otherwise, use a multiline list.
423 ret += "[\n"
424 for i := 0; i < propertyValue.Len(); i++ {
425 indexedValue, err := prettyPrint(propertyValue.Index(i), indent+1)
426 if err != nil {
427 return "", err
428 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800429
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000430 if indexedValue != "" {
431 ret += makeIndent(indent + 1)
432 ret += indexedValue
433 ret += ",\n"
434 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800435 }
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000436 ret += makeIndent(indent)
437 ret += "]"
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800438 }
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000439
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800440 case reflect.Struct:
Jingwen Chen5d864492021-02-24 07:20:12 -0500441 // Special cases where the bp2build sends additional information to the codegenerator
442 // by wrapping the attributes in a custom struct type.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000443 if attr, ok := propertyValue.Interface().(bazel.Attribute); ok {
444 return prettyPrintAttribute(attr, indent)
Liz Kammer356f7d42021-01-26 09:18:53 -0500445 } else if label, ok := propertyValue.Interface().(bazel.Label); ok {
446 return fmt.Sprintf("%q", label.Label), nil
447 }
448
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800449 ret = "{\n"
450 // Sort and print the struct props by the key.
451 structProps := extractStructProperties(propertyValue, indent)
452 for _, k := range android.SortedStringKeys(structProps) {
453 ret += makeIndent(indent + 1)
454 ret += fmt.Sprintf("%q: %s,\n", k, structProps[k])
455 }
456 ret += makeIndent(indent)
457 ret += "}"
458 case reflect.Interface:
459 // TODO(b/164227191): implement pretty print for interfaces.
460 // Interfaces are used for for arch, multilib and target properties.
461 return "", nil
462 default:
463 return "", fmt.Errorf(
464 "unexpected kind for property struct field: %s", propertyValue.Kind())
465 }
466 return ret, nil
467}
468
469// Converts a reflected property struct value into a map of property names and property values,
470// which each property value correctly pretty-printed and indented at the right nest level,
471// since property structs can be nested. In Starlark, nested structs are represented as nested
472// dicts: https://docs.bazel.build/skylark/lib/dict.html
473func extractStructProperties(structValue reflect.Value, indent int) map[string]string {
474 if structValue.Kind() != reflect.Struct {
475 panic(fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind()))
476 }
477
478 ret := map[string]string{}
479 structType := structValue.Type()
480 for i := 0; i < structValue.NumField(); i++ {
481 field := structType.Field(i)
482 if shouldSkipStructField(field) {
483 continue
484 }
485
486 fieldValue := structValue.Field(i)
487 if isZero(fieldValue) {
488 // Ignore zero-valued fields
489 continue
490 }
491
492 propertyName := proptools.PropertyNameForField(field.Name)
493 prettyPrintedValue, err := prettyPrint(fieldValue, indent+1)
494 if err != nil {
495 panic(
496 fmt.Errorf(
497 "Error while parsing property: %q. %s",
498 propertyName,
499 err))
500 }
501 if prettyPrintedValue != "" {
502 ret[propertyName] = prettyPrintedValue
503 }
504 }
505
506 return ret
507}
508
509func isZero(value reflect.Value) bool {
510 switch value.Kind() {
511 case reflect.Func, reflect.Map, reflect.Slice:
512 return value.IsNil()
513 case reflect.Array:
514 valueIsZero := true
515 for i := 0; i < value.Len(); i++ {
516 valueIsZero = valueIsZero && isZero(value.Index(i))
517 }
518 return valueIsZero
519 case reflect.Struct:
520 valueIsZero := true
521 for i := 0; i < value.NumField(); i++ {
522 if value.Field(i).CanSet() {
523 valueIsZero = valueIsZero && isZero(value.Field(i))
524 }
525 }
526 return valueIsZero
527 case reflect.Ptr:
528 if !value.IsNil() {
529 return isZero(reflect.Indirect(value))
530 } else {
531 return true
532 }
533 default:
534 zeroValue := reflect.Zero(value.Type())
535 result := value.Interface() == zeroValue.Interface()
536 return result
537 }
538}
539
540func escapeString(s string) string {
541 s = strings.ReplaceAll(s, "\\", "\\\\")
Jingwen Chen58a12b82021-03-30 13:08:36 +0000542
543 // b/184026959: Reverse the application of some common control sequences.
544 // These must be generated literally in the BUILD file.
545 s = strings.ReplaceAll(s, "\t", "\\t")
546 s = strings.ReplaceAll(s, "\n", "\\n")
547 s = strings.ReplaceAll(s, "\r", "\\r")
548
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800549 return strings.ReplaceAll(s, "\"", "\\\"")
550}
551
552func makeIndent(indent int) string {
553 if indent < 0 {
554 panic(fmt.Errorf("indent column cannot be less than 0, but got %d", indent))
555 }
556 return strings.Repeat(" ", indent)
557}
558
Jingwen Chen73850672020-12-14 08:25:34 -0500559func targetNameForBp2Build(c bpToBuildContext, logicModule blueprint.Module) string {
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500560 return strings.Replace(c.ModuleName(logicModule), bazel.BazelTargetModuleNamePrefix, "", 1)
Jingwen Chen73850672020-12-14 08:25:34 -0500561}
562
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800563func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
564 name := ""
565 if c.ModuleSubDir(logicModule) != "" {
566 // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes.
567 name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule)
568 } else {
569 name = c.ModuleName(logicModule)
570 }
571
572 return strings.Replace(name, "//", "", 1)
573}
574
575func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string {
576 return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule))
577}