blob: 7ffcfa4a35b12a6df6af32ff72ef4c1fc98b899b [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"
Jingwen Chen40067de2021-01-26 21:58:43 -050022 "strconv"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080023 "strings"
24
25 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27)
28
29type BazelAttributes struct {
30 Attrs map[string]string
31}
32
33type BazelTarget struct {
Jingwen Chen40067de2021-01-26 21:58:43 -050034 name string
35 content string
36 ruleClass string
37 bzlLoadLocation string
38}
39
40// IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file,
41// as opposed to a native rule built into Bazel.
42func (t BazelTarget) IsLoadedFromStarlark() bool {
43 return t.bzlLoadLocation != ""
44}
45
46// BazelTargets is a typedef for a slice of BazelTarget objects.
47type BazelTargets []BazelTarget
48
49// String returns the string representation of BazelTargets, without load
50// statements (use LoadStatements for that), since the targets are usually not
51// adjacent to the load statements at the top of the BUILD file.
52func (targets BazelTargets) String() string {
53 var res string
54 for i, target := range targets {
55 res += target.content
56 if i != len(targets)-1 {
57 res += "\n\n"
58 }
59 }
60 return res
61}
62
63// LoadStatements return the string representation of the sorted and deduplicated
64// Starlark rule load statements needed by a group of BazelTargets.
65func (targets BazelTargets) LoadStatements() string {
66 bzlToLoadedSymbols := map[string][]string{}
67 for _, target := range targets {
68 if target.IsLoadedFromStarlark() {
69 bzlToLoadedSymbols[target.bzlLoadLocation] =
70 append(bzlToLoadedSymbols[target.bzlLoadLocation], target.ruleClass)
71 }
72 }
73
74 var loadStatements []string
75 for bzl, ruleClasses := range bzlToLoadedSymbols {
76 loadStatement := "load(\""
77 loadStatement += bzl
78 loadStatement += "\", "
79 ruleClasses = android.SortedUniqueStrings(ruleClasses)
80 for i, ruleClass := range ruleClasses {
81 loadStatement += "\"" + ruleClass + "\""
82 if i != len(ruleClasses)-1 {
83 loadStatement += ", "
84 }
85 }
86 loadStatement += ")"
87 loadStatements = append(loadStatements, loadStatement)
88 }
89 return strings.Join(android.SortedUniqueStrings(loadStatements), "\n")
Liz Kammer2dd9ca42020-11-25 16:06:39 -080090}
91
92type bpToBuildContext interface {
93 ModuleName(module blueprint.Module) string
94 ModuleDir(module blueprint.Module) string
95 ModuleSubDir(module blueprint.Module) string
96 ModuleType(module blueprint.Module) string
97
Jingwen Chendaa54bc2020-12-14 02:58:54 -050098 VisitAllModules(visit func(blueprint.Module))
99 VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module))
100}
101
102type CodegenContext struct {
103 config android.Config
104 context android.Context
Jingwen Chen33832f92021-01-24 22:55:54 -0500105 mode CodegenMode
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500106}
107
Jingwen Chen33832f92021-01-24 22:55:54 -0500108// CodegenMode is an enum to differentiate code-generation modes.
109type CodegenMode int
110
111const (
112 // Bp2Build: generate BUILD files with targets buildable by Bazel directly.
113 //
114 // This mode is used for the Soong->Bazel build definition conversion.
115 Bp2Build CodegenMode = iota
116
117 // QueryView: generate BUILD files with targets representing fully mutated
118 // Soong modules, representing the fully configured Soong module graph with
119 // variants and dependency endges.
120 //
121 // This mode is used for discovering and introspecting the existing Soong
122 // module graph.
123 QueryView
124)
125
Jingwen Chendcc329a2021-01-26 02:49:03 -0500126func (mode CodegenMode) String() string {
127 switch mode {
128 case Bp2Build:
129 return "Bp2Build"
130 case QueryView:
131 return "QueryView"
132 default:
133 return fmt.Sprintf("%d", mode)
134 }
135}
136
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500137func (ctx CodegenContext) AddNinjaFileDeps(...string) {}
138func (ctx CodegenContext) Config() android.Config { return ctx.config }
139func (ctx CodegenContext) Context() android.Context { return ctx.context }
140
141// NewCodegenContext creates a wrapper context that conforms to PathContext for
142// writing BUILD files in the output directory.
Jingwen Chen33832f92021-01-24 22:55:54 -0500143func NewCodegenContext(config android.Config, context android.Context, mode CodegenMode) CodegenContext {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500144 return CodegenContext{
145 context: context,
146 config: config,
Jingwen Chen33832f92021-01-24 22:55:54 -0500147 mode: mode,
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500148 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800149}
150
151// props is an unsorted map. This function ensures that
152// the generated attributes are sorted to ensure determinism.
153func propsToAttributes(props map[string]string) string {
154 var attributes string
155 for _, propName := range android.SortedStringKeys(props) {
156 if shouldGenerateAttribute(propName) {
157 attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName])
158 }
159 }
160 return attributes
161}
162
Jingwen Chen4d2c0872021-02-02 07:06:56 -0500163func GenerateBazelTargets(ctx bpToBuildContext, codegenMode CodegenMode) map[string]BazelTargets {
Jingwen Chen40067de2021-01-26 21:58:43 -0500164 buildFileToTargets := make(map[string]BazelTargets)
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500165 ctx.VisitAllModules(func(m blueprint.Module) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800166 dir := ctx.ModuleDir(m)
Jingwen Chen73850672020-12-14 08:25:34 -0500167 var t BazelTarget
168
Jingwen Chen33832f92021-01-24 22:55:54 -0500169 switch codegenMode {
170 case Bp2Build:
Jingwen Chen73850672020-12-14 08:25:34 -0500171 if _, ok := m.(android.BazelTargetModule); !ok {
172 return
173 }
174 t = generateBazelTarget(ctx, m)
Jingwen Chen33832f92021-01-24 22:55:54 -0500175 case QueryView:
Jingwen Chen96af35b2021-02-08 00:49:32 -0500176 // Blocklist certain module types from being generated.
177 if canonicalizeModuleType(ctx.ModuleType(m)) == "package" {
178 // package module name contain slashes, and thus cannot
179 // be mapped cleanly to a bazel label.
180 return
181 }
Jingwen Chen73850672020-12-14 08:25:34 -0500182 t = generateSoongModuleTarget(ctx, m)
Jingwen Chen33832f92021-01-24 22:55:54 -0500183 default:
184 panic(fmt.Errorf("Unknown code-generation mode: %s", codegenMode))
Jingwen Chen73850672020-12-14 08:25:34 -0500185 }
186
Liz Kammer356f7d42021-01-26 09:18:53 -0500187 buildFileToTargets[dir] = append(buildFileToTargets[dir], t)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800188 })
189 return buildFileToTargets
190}
191
Jingwen Chen40067de2021-01-26 21:58:43 -0500192// Helper method to trim quotes around strings.
193func trimQuotes(s string) string {
194 if s == "" {
195 // strconv.Unquote would error out on empty strings, but this method
196 // allows them, so return the empty string directly.
197 return ""
198 }
199 ret, err := strconv.Unquote(s)
200 if err != nil {
201 // Panic the error immediately.
202 panic(fmt.Errorf("Trying to unquote '%s', but got error: %s", s, err))
203 }
204 return ret
205}
206
Jingwen Chen73850672020-12-14 08:25:34 -0500207func generateBazelTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
208 // extract the bazel attributes from the module.
209 props := getBuildProperties(ctx, m)
210
211 // extract the rule class name from the attributes. Since the string value
212 // will be string-quoted, remove the quotes here.
Jingwen Chen40067de2021-01-26 21:58:43 -0500213 ruleClass := trimQuotes(props.Attrs["rule_class"])
Jingwen Chen73850672020-12-14 08:25:34 -0500214 // Delete it from being generated in the BUILD file.
215 delete(props.Attrs, "rule_class")
216
Jingwen Chen40067de2021-01-26 21:58:43 -0500217 // extract the bzl_load_location, and also remove the quotes around it here.
218 bzlLoadLocation := trimQuotes(props.Attrs["bzl_load_location"])
219 // Delete it from being generated in the BUILD file.
220 delete(props.Attrs, "bzl_load_location")
221
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500222 delete(props.Attrs, "bp2build_available")
223
Jingwen Chen73850672020-12-14 08:25:34 -0500224 // Return the Bazel target with rule class and attributes, ready to be
225 // code-generated.
226 attributes := propsToAttributes(props.Attrs)
227 targetName := targetNameForBp2Build(ctx, m)
228 return BazelTarget{
Jingwen Chen40067de2021-01-26 21:58:43 -0500229 name: targetName,
230 ruleClass: ruleClass,
231 bzlLoadLocation: bzlLoadLocation,
Jingwen Chen73850672020-12-14 08:25:34 -0500232 content: fmt.Sprintf(
233 bazelTarget,
234 ruleClass,
235 targetName,
236 attributes,
237 ),
238 }
239}
240
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800241// Convert a module and its deps and props into a Bazel macro/rule
242// representation in the BUILD file.
243func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
244 props := getBuildProperties(ctx, m)
245
246 // TODO(b/163018919): DirectDeps can have duplicate (module, variant)
247 // items, if the modules are added using different DependencyTag. Figure
248 // out the implications of that.
249 depLabels := map[string]bool{}
250 if aModule, ok := m.(android.Module); ok {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500251 ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800252 depLabels[qualifiedTargetLabel(ctx, depModule)] = true
253 })
254 }
255 attributes := propsToAttributes(props.Attrs)
256
257 depLabelList := "[\n"
258 for depLabel, _ := range depLabels {
259 depLabelList += fmt.Sprintf(" %q,\n", depLabel)
260 }
261 depLabelList += " ]"
262
263 targetName := targetNameWithVariant(ctx, m)
264 return BazelTarget{
265 name: targetName,
266 content: fmt.Sprintf(
267 soongModuleTarget,
268 targetName,
269 ctx.ModuleName(m),
270 canonicalizeModuleType(ctx.ModuleType(m)),
271 ctx.ModuleSubDir(m),
272 depLabelList,
273 attributes),
274 }
275}
276
277func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) BazelAttributes {
278 var allProps map[string]string
279 // TODO: this omits properties for blueprint modules (blueprint_go_binary,
280 // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately.
281 if aModule, ok := m.(android.Module); ok {
282 allProps = ExtractModuleProperties(aModule)
283 }
284
285 return BazelAttributes{
286 Attrs: allProps,
287 }
288}
289
290// Generically extract module properties and types into a map, keyed by the module property name.
291func ExtractModuleProperties(aModule android.Module) map[string]string {
292 ret := map[string]string{}
293
294 // Iterate over this android.Module's property structs.
295 for _, properties := range aModule.GetProperties() {
296 propertiesValue := reflect.ValueOf(properties)
297 // Check that propertiesValue is a pointer to the Properties struct, like
298 // *cc.BaseLinkerProperties or *java.CompilerProperties.
299 //
300 // propertiesValue can also be type-asserted to the structs to
301 // manipulate internal props, if needed.
302 if isStructPtr(propertiesValue.Type()) {
303 structValue := propertiesValue.Elem()
304 for k, v := range extractStructProperties(structValue, 0) {
305 ret[k] = v
306 }
307 } else {
308 panic(fmt.Errorf(
309 "properties must be a pointer to a struct, got %T",
310 propertiesValue.Interface()))
311 }
312 }
313
314 return ret
315}
316
317func isStructPtr(t reflect.Type) bool {
318 return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
319}
320
321// prettyPrint a property value into the equivalent Starlark representation
322// recursively.
323func prettyPrint(propertyValue reflect.Value, indent int) (string, error) {
324 if isZero(propertyValue) {
325 // A property value being set or unset actually matters -- Soong does set default
326 // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at
327 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480
328 //
329 // In Bazel-parlance, we would use "attr.<type>(default = <default value>)" to set the default
330 // value of unset attributes.
331 return "", nil
332 }
333
334 var ret string
335 switch propertyValue.Kind() {
336 case reflect.String:
337 ret = fmt.Sprintf("\"%v\"", escapeString(propertyValue.String()))
338 case reflect.Bool:
339 ret = strings.Title(fmt.Sprintf("%v", propertyValue.Interface()))
340 case reflect.Int, reflect.Uint, reflect.Int64:
341 ret = fmt.Sprintf("%v", propertyValue.Interface())
342 case reflect.Ptr:
343 return prettyPrint(propertyValue.Elem(), indent)
344 case reflect.Slice:
345 ret = "[\n"
346 for i := 0; i < propertyValue.Len(); i++ {
347 indexedValue, err := prettyPrint(propertyValue.Index(i), indent+1)
348 if err != nil {
349 return "", err
350 }
351
352 if indexedValue != "" {
353 ret += makeIndent(indent + 1)
354 ret += indexedValue
355 ret += ",\n"
356 }
357 }
358 ret += makeIndent(indent)
359 ret += "]"
360 case reflect.Struct:
Liz Kammer356f7d42021-01-26 09:18:53 -0500361 if labels, ok := propertyValue.Interface().(bazel.LabelList); ok {
362 // TODO(b/165114590): convert glob syntax
363 return prettyPrint(reflect.ValueOf(labels.Includes), indent)
364 } else if label, ok := propertyValue.Interface().(bazel.Label); ok {
365 return fmt.Sprintf("%q", label.Label), nil
366 }
367
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800368 ret = "{\n"
369 // Sort and print the struct props by the key.
370 structProps := extractStructProperties(propertyValue, indent)
371 for _, k := range android.SortedStringKeys(structProps) {
372 ret += makeIndent(indent + 1)
373 ret += fmt.Sprintf("%q: %s,\n", k, structProps[k])
374 }
375 ret += makeIndent(indent)
376 ret += "}"
377 case reflect.Interface:
378 // TODO(b/164227191): implement pretty print for interfaces.
379 // Interfaces are used for for arch, multilib and target properties.
380 return "", nil
381 default:
382 return "", fmt.Errorf(
383 "unexpected kind for property struct field: %s", propertyValue.Kind())
384 }
385 return ret, nil
386}
387
388// Converts a reflected property struct value into a map of property names and property values,
389// which each property value correctly pretty-printed and indented at the right nest level,
390// since property structs can be nested. In Starlark, nested structs are represented as nested
391// dicts: https://docs.bazel.build/skylark/lib/dict.html
392func extractStructProperties(structValue reflect.Value, indent int) map[string]string {
393 if structValue.Kind() != reflect.Struct {
394 panic(fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind()))
395 }
396
397 ret := map[string]string{}
398 structType := structValue.Type()
399 for i := 0; i < structValue.NumField(); i++ {
400 field := structType.Field(i)
401 if shouldSkipStructField(field) {
402 continue
403 }
404
405 fieldValue := structValue.Field(i)
406 if isZero(fieldValue) {
407 // Ignore zero-valued fields
408 continue
409 }
410
411 propertyName := proptools.PropertyNameForField(field.Name)
412 prettyPrintedValue, err := prettyPrint(fieldValue, indent+1)
413 if err != nil {
414 panic(
415 fmt.Errorf(
416 "Error while parsing property: %q. %s",
417 propertyName,
418 err))
419 }
420 if prettyPrintedValue != "" {
421 ret[propertyName] = prettyPrintedValue
422 }
423 }
424
425 return ret
426}
427
428func isZero(value reflect.Value) bool {
429 switch value.Kind() {
430 case reflect.Func, reflect.Map, reflect.Slice:
431 return value.IsNil()
432 case reflect.Array:
433 valueIsZero := true
434 for i := 0; i < value.Len(); i++ {
435 valueIsZero = valueIsZero && isZero(value.Index(i))
436 }
437 return valueIsZero
438 case reflect.Struct:
439 valueIsZero := true
440 for i := 0; i < value.NumField(); i++ {
441 if value.Field(i).CanSet() {
442 valueIsZero = valueIsZero && isZero(value.Field(i))
443 }
444 }
445 return valueIsZero
446 case reflect.Ptr:
447 if !value.IsNil() {
448 return isZero(reflect.Indirect(value))
449 } else {
450 return true
451 }
452 default:
453 zeroValue := reflect.Zero(value.Type())
454 result := value.Interface() == zeroValue.Interface()
455 return result
456 }
457}
458
459func escapeString(s string) string {
460 s = strings.ReplaceAll(s, "\\", "\\\\")
461 return strings.ReplaceAll(s, "\"", "\\\"")
462}
463
464func makeIndent(indent int) string {
465 if indent < 0 {
466 panic(fmt.Errorf("indent column cannot be less than 0, but got %d", indent))
467 }
468 return strings.Repeat(" ", indent)
469}
470
Jingwen Chen73850672020-12-14 08:25:34 -0500471func targetNameForBp2Build(c bpToBuildContext, logicModule blueprint.Module) string {
Jingwen Chenfb4692a2021-02-07 10:05:16 -0500472 return strings.Replace(c.ModuleName(logicModule), bazel.BazelTargetModuleNamePrefix, "", 1)
Jingwen Chen73850672020-12-14 08:25:34 -0500473}
474
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800475func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
476 name := ""
477 if c.ModuleSubDir(logicModule) != "" {
478 // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes.
479 name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule)
480 } else {
481 name = c.ModuleName(logicModule)
482 }
483
484 return strings.Replace(name, "//", "", 1)
485}
486
487func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string {
488 return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule))
489}