2020-11-26 01:06:39 +01:00
|
|
|
// Copyright 2020 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package bp2build
|
|
|
|
|
2021-08-23 19:04:20 +02:00
|
|
|
/*
|
|
|
|
For shareable/common functionality for conversion from soong-module to build files
|
|
|
|
for queryview/bp2build
|
|
|
|
*/
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2021-05-25 07:16:48 +02:00
|
|
|
"sort"
|
2020-11-26 01:06:39 +01:00
|
|
|
"strings"
|
|
|
|
|
2021-08-23 19:04:20 +02:00
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/bazel"
|
2022-02-03 14:42:10 +01:00
|
|
|
"android/soong/starlark_fmt"
|
2021-08-23 19:04:20 +02:00
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BazelAttributes struct {
|
|
|
|
Attrs map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BazelTarget struct {
|
2021-01-27 03:58:43 +01:00
|
|
|
name string
|
2021-06-17 07:43:19 +02:00
|
|
|
packageName string
|
2021-01-27 03:58:43 +01:00
|
|
|
content string
|
|
|
|
ruleClass string
|
|
|
|
bzlLoadLocation string
|
2021-05-25 07:16:48 +02:00
|
|
|
handcrafted bool
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file,
|
|
|
|
// as opposed to a native rule built into Bazel.
|
|
|
|
func (t BazelTarget) IsLoadedFromStarlark() bool {
|
|
|
|
return t.bzlLoadLocation != ""
|
|
|
|
}
|
|
|
|
|
2021-06-17 07:43:19 +02:00
|
|
|
// Label is the fully qualified Bazel label constructed from the BazelTarget's
|
|
|
|
// package name and target name.
|
|
|
|
func (t BazelTarget) Label() string {
|
|
|
|
if t.packageName == "." {
|
|
|
|
return "//:" + t.name
|
|
|
|
} else {
|
|
|
|
return "//" + t.packageName + ":" + t.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:58:43 +01:00
|
|
|
// BazelTargets is a typedef for a slice of BazelTarget objects.
|
|
|
|
type BazelTargets []BazelTarget
|
|
|
|
|
2021-05-25 07:16:48 +02:00
|
|
|
// HasHandcraftedTargetsreturns true if a set of bazel targets contain
|
|
|
|
// handcrafted ones.
|
|
|
|
func (targets BazelTargets) hasHandcraftedTargets() bool {
|
|
|
|
for _, target := range targets {
|
|
|
|
if target.handcrafted {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort a list of BazelTargets in-place, by name, and by generated/handcrafted types.
|
|
|
|
func (targets BazelTargets) sort() {
|
|
|
|
sort.Slice(targets, func(i, j int) bool {
|
|
|
|
if targets[i].handcrafted != targets[j].handcrafted {
|
|
|
|
// Handcrafted targets will be generated after the bp2build generated targets.
|
|
|
|
return targets[j].handcrafted
|
|
|
|
}
|
|
|
|
// This will cover all bp2build generated targets.
|
|
|
|
return targets[i].name < targets[j].name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:58:43 +01:00
|
|
|
// String returns the string representation of BazelTargets, without load
|
|
|
|
// statements (use LoadStatements for that), since the targets are usually not
|
|
|
|
// adjacent to the load statements at the top of the BUILD file.
|
|
|
|
func (targets BazelTargets) String() string {
|
|
|
|
var res string
|
|
|
|
for i, target := range targets {
|
2021-05-25 07:16:48 +02:00
|
|
|
// There is only at most 1 handcrafted "target", because its contents
|
|
|
|
// represent the entire BUILD file content from the tree. See
|
|
|
|
// build_conversion.go#getHandcraftedBuildContent for more information.
|
|
|
|
//
|
|
|
|
// Add a header to make it easy to debug where the handcrafted targets
|
|
|
|
// are in a generated BUILD file.
|
|
|
|
if target.handcrafted {
|
|
|
|
res += "# -----------------------------\n"
|
|
|
|
res += "# Section: Handcrafted targets. \n"
|
|
|
|
res += "# -----------------------------\n\n"
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:58:43 +01:00
|
|
|
res += target.content
|
|
|
|
if i != len(targets)-1 {
|
|
|
|
res += "\n\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadStatements return the string representation of the sorted and deduplicated
|
|
|
|
// Starlark rule load statements needed by a group of BazelTargets.
|
|
|
|
func (targets BazelTargets) LoadStatements() string {
|
|
|
|
bzlToLoadedSymbols := map[string][]string{}
|
|
|
|
for _, target := range targets {
|
|
|
|
if target.IsLoadedFromStarlark() {
|
|
|
|
bzlToLoadedSymbols[target.bzlLoadLocation] =
|
|
|
|
append(bzlToLoadedSymbols[target.bzlLoadLocation], target.ruleClass)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var loadStatements []string
|
|
|
|
for bzl, ruleClasses := range bzlToLoadedSymbols {
|
|
|
|
loadStatement := "load(\""
|
|
|
|
loadStatement += bzl
|
|
|
|
loadStatement += "\", "
|
|
|
|
ruleClasses = android.SortedUniqueStrings(ruleClasses)
|
|
|
|
for i, ruleClass := range ruleClasses {
|
|
|
|
loadStatement += "\"" + ruleClass + "\""
|
|
|
|
if i != len(ruleClasses)-1 {
|
|
|
|
loadStatement += ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadStatement += ")"
|
|
|
|
loadStatements = append(loadStatements, loadStatement)
|
|
|
|
}
|
|
|
|
return strings.Join(android.SortedUniqueStrings(loadStatements), "\n")
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type bpToBuildContext interface {
|
|
|
|
ModuleName(module blueprint.Module) string
|
|
|
|
ModuleDir(module blueprint.Module) string
|
|
|
|
ModuleSubDir(module blueprint.Module) string
|
|
|
|
ModuleType(module blueprint.Module) string
|
|
|
|
|
2020-12-14 08:58:54 +01:00
|
|
|
VisitAllModules(visit func(blueprint.Module))
|
|
|
|
VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module))
|
|
|
|
}
|
|
|
|
|
|
|
|
type CodegenContext struct {
|
2021-09-17 09:16:13 +02:00
|
|
|
config android.Config
|
|
|
|
context android.Context
|
|
|
|
mode CodegenMode
|
|
|
|
additionalDeps []string
|
2021-08-26 14:37:59 +02:00
|
|
|
unconvertedDepMode unconvertedDepsMode
|
2020-12-14 08:58:54 +01:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:29:56 +02:00
|
|
|
func (ctx *CodegenContext) Mode() CodegenMode {
|
|
|
|
return ctx.mode
|
2021-02-19 06:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 04:55:54 +01:00
|
|
|
// CodegenMode is an enum to differentiate code-generation modes.
|
|
|
|
type CodegenMode int
|
|
|
|
|
|
|
|
const (
|
2022-07-11 17:29:56 +02:00
|
|
|
// Bp2Build - generate BUILD files with targets buildable by Bazel directly.
|
2021-01-25 04:55:54 +01:00
|
|
|
//
|
|
|
|
// This mode is used for the Soong->Bazel build definition conversion.
|
|
|
|
Bp2Build CodegenMode = iota
|
|
|
|
|
2022-07-11 17:29:56 +02:00
|
|
|
// QueryView - generate BUILD files with targets representing fully mutated
|
2021-01-25 04:55:54 +01:00
|
|
|
// Soong modules, representing the fully configured Soong module graph with
|
2022-07-11 17:29:56 +02:00
|
|
|
// variants and dependency edges.
|
2021-01-25 04:55:54 +01:00
|
|
|
//
|
|
|
|
// This mode is used for discovering and introspecting the existing Soong
|
|
|
|
// module graph.
|
|
|
|
QueryView
|
|
|
|
)
|
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
type unconvertedDepsMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Include a warning in conversion metrics about converted modules with unconverted direct deps
|
|
|
|
warnUnconvertedDeps unconvertedDepsMode = iota
|
|
|
|
// Error and fail conversion if encountering a module with unconverted direct deps
|
|
|
|
// Enabled by setting environment variable `BP2BUILD_ERROR_UNCONVERTED`
|
|
|
|
errorModulesUnconvertedDeps
|
|
|
|
)
|
|
|
|
|
2021-01-26 08:49:03 +01:00
|
|
|
func (mode CodegenMode) String() string {
|
|
|
|
switch mode {
|
|
|
|
case Bp2Build:
|
|
|
|
return "Bp2Build"
|
|
|
|
case QueryView:
|
|
|
|
return "QueryView"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%d", mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
// AddNinjaFileDeps adds dependencies on the specified files to be added to the ninja manifest. The
|
|
|
|
// primary builder will be rerun whenever the specified files are modified. Allows us to fulfill the
|
|
|
|
// PathContext interface in order to add dependencies on hand-crafted BUILD files. Note: must also
|
|
|
|
// call AdditionalNinjaDeps and add them manually to the ninja file.
|
|
|
|
func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) {
|
|
|
|
ctx.additionalDeps = append(ctx.additionalDeps, deps...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdditionalNinjaDeps returns additional ninja deps added by CodegenContext
|
|
|
|
func (ctx *CodegenContext) AdditionalNinjaDeps() []string {
|
|
|
|
return ctx.additionalDeps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *CodegenContext) Config() android.Config { return ctx.config }
|
|
|
|
func (ctx *CodegenContext) Context() android.Context { return ctx.context }
|
2020-12-14 08:58:54 +01:00
|
|
|
|
|
|
|
// NewCodegenContext creates a wrapper context that conforms to PathContext for
|
|
|
|
// writing BUILD files in the output directory.
|
2021-02-17 19:22:03 +01:00
|
|
|
func NewCodegenContext(config android.Config, context android.Context, mode CodegenMode) *CodegenContext {
|
2021-08-26 14:37:59 +02:00
|
|
|
var unconvertedDeps unconvertedDepsMode
|
|
|
|
if config.IsEnvTrue("BP2BUILD_ERROR_UNCONVERTED") {
|
|
|
|
unconvertedDeps = errorModulesUnconvertedDeps
|
|
|
|
}
|
2021-02-17 19:22:03 +01:00
|
|
|
return &CodegenContext{
|
2021-08-26 14:37:59 +02:00
|
|
|
context: context,
|
|
|
|
config: config,
|
|
|
|
mode: mode,
|
|
|
|
unconvertedDepMode: unconvertedDeps,
|
2020-12-14 08:58:54 +01:00
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// props is an unsorted map. This function ensures that
|
|
|
|
// the generated attributes are sorted to ensure determinism.
|
|
|
|
func propsToAttributes(props map[string]string) string {
|
|
|
|
var attributes string
|
|
|
|
for _, propName := range android.SortedStringKeys(props) {
|
2021-10-06 16:32:26 +02:00
|
|
|
attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName])
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
return attributes
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
type conversionResults struct {
|
|
|
|
buildFileToTargets map[string]BazelTargets
|
|
|
|
metrics CodegenMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r conversionResults) BuildDirToTargets() map[string]BazelTargets {
|
|
|
|
return r.buildFileToTargets
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) {
|
2021-01-27 03:58:43 +01:00
|
|
|
buildFileToTargets := make(map[string]BazelTargets)
|
2021-02-17 19:22:03 +01:00
|
|
|
buildFileToAppend := make(map[string]bool)
|
2021-02-19 06:48:40 +01:00
|
|
|
|
|
|
|
// Simple metrics tracking for bp2build
|
2021-02-17 19:22:03 +01:00
|
|
|
metrics := CodegenMetrics{
|
2022-01-20 18:55:05 +01:00
|
|
|
ruleClassCount: make(map[string]uint64),
|
|
|
|
convertedModuleTypeCount: make(map[string]uint64),
|
|
|
|
totalModuleTypeCount: make(map[string]uint64),
|
2021-02-17 19:22:03 +01:00
|
|
|
}
|
2021-02-19 06:48:40 +01:00
|
|
|
|
2021-04-21 13:10:09 +02:00
|
|
|
dirs := make(map[string]bool)
|
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
var errs []error
|
|
|
|
|
2021-02-19 06:48:40 +01:00
|
|
|
bpCtx := ctx.Context()
|
|
|
|
bpCtx.VisitAllModules(func(m blueprint.Module) {
|
|
|
|
dir := bpCtx.ModuleDir(m)
|
2022-01-20 18:55:05 +01:00
|
|
|
moduleType := bpCtx.ModuleType(m)
|
2021-04-21 13:10:09 +02:00
|
|
|
dirs[dir] = true
|
|
|
|
|
2021-08-11 06:17:36 +02:00
|
|
|
var targets []BazelTarget
|
2020-12-14 14:25:34 +01:00
|
|
|
|
2021-02-19 06:48:40 +01:00
|
|
|
switch ctx.Mode() {
|
2021-01-25 04:55:54 +01:00
|
|
|
case Bp2Build:
|
2021-09-20 12:54:27 +02:00
|
|
|
// There are two main ways of converting a Soong module to Bazel:
|
|
|
|
// 1) Manually handcrafting a Bazel target and associating the module with its label
|
|
|
|
// 2) Automatically generating with bp2build converters
|
|
|
|
//
|
|
|
|
// bp2build converters are used for the majority of modules.
|
2021-02-17 19:22:03 +01:00
|
|
|
if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
|
2021-09-20 12:54:27 +02:00
|
|
|
// Handle modules converted to handcrafted targets.
|
|
|
|
//
|
|
|
|
// Since these modules are associated with some handcrafted
|
|
|
|
// target in a BUILD file, we simply append the entire contents
|
|
|
|
// of that BUILD file to the generated BUILD file.
|
|
|
|
//
|
|
|
|
// The append operation is only done once, even if there are
|
|
|
|
// multiple modules from the same directory associated to
|
|
|
|
// targets in the same BUILD file (or package).
|
|
|
|
|
|
|
|
// Log the module.
|
2022-01-20 18:55:05 +01:00
|
|
|
metrics.AddConvertedModule(m, moduleType, Handcrafted)
|
2021-09-20 12:54:27 +02:00
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
pathToBuildFile := getBazelPackagePath(b)
|
|
|
|
if _, exists := buildFileToAppend[pathToBuildFile]; exists {
|
2021-09-20 12:54:27 +02:00
|
|
|
// Append the BUILD file content once per package, at most.
|
2021-02-17 19:22:03 +01:00
|
|
|
return
|
|
|
|
}
|
2021-08-11 06:17:36 +02:00
|
|
|
t, err := getHandcraftedBuildContent(ctx, b, pathToBuildFile)
|
2021-02-17 19:22:03 +01:00
|
|
|
if err != nil {
|
2021-08-26 14:37:59 +02:00
|
|
|
errs = append(errs, fmt.Errorf("Error converting %s: %s", bpCtx.ModuleName(m), err))
|
|
|
|
return
|
2021-02-17 19:22:03 +01:00
|
|
|
}
|
2021-08-11 06:17:36 +02:00
|
|
|
targets = append(targets, t)
|
2021-02-17 19:22:03 +01:00
|
|
|
// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
|
|
|
|
// something more targeted based on the rule type and target
|
|
|
|
buildFileToAppend[pathToBuildFile] = true
|
2021-08-11 06:17:36 +02:00
|
|
|
} else if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
|
2021-09-20 12:54:27 +02:00
|
|
|
// Handle modules converted to generated targets.
|
|
|
|
|
|
|
|
// Log the module.
|
2022-01-20 18:55:05 +01:00
|
|
|
metrics.AddConvertedModule(aModule, moduleType, Generated)
|
2021-09-20 12:54:27 +02:00
|
|
|
|
|
|
|
// Handle modules with unconverted deps. By default, emit a warning.
|
2021-08-26 14:37:59 +02:00
|
|
|
if unconvertedDeps := aModule.GetUnconvertedBp2buildDeps(); len(unconvertedDeps) > 0 {
|
|
|
|
msg := fmt.Sprintf("%q depends on unconverted modules: %s", m.Name(), strings.Join(unconvertedDeps, ", "))
|
|
|
|
if ctx.unconvertedDepMode == warnUnconvertedDeps {
|
|
|
|
metrics.moduleWithUnconvertedDepsMsgs = append(metrics.moduleWithUnconvertedDepsMsgs, msg)
|
|
|
|
} else if ctx.unconvertedDepMode == errorModulesUnconvertedDeps {
|
|
|
|
errs = append(errs, fmt.Errorf(msg))
|
|
|
|
return
|
2021-12-15 21:35:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if unconvertedDeps := aModule.GetMissingBp2buildDeps(); len(unconvertedDeps) > 0 {
|
|
|
|
msg := fmt.Sprintf("%q depends on missing modules: %s", m.Name(), strings.Join(unconvertedDeps, ", "))
|
|
|
|
if ctx.unconvertedDepMode == warnUnconvertedDeps {
|
|
|
|
metrics.moduleWithMissingDepsMsgs = append(metrics.moduleWithMissingDepsMsgs, msg)
|
|
|
|
} else if ctx.unconvertedDepMode == errorModulesUnconvertedDeps {
|
|
|
|
errs = append(errs, fmt.Errorf(msg))
|
|
|
|
return
|
2021-08-26 14:37:59 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-11 06:17:36 +02:00
|
|
|
targets = generateBazelTargets(bpCtx, aModule)
|
|
|
|
for _, t := range targets {
|
2021-09-20 12:54:27 +02:00
|
|
|
// A module can potentially generate more than 1 Bazel
|
|
|
|
// target, each of a different rule class.
|
|
|
|
metrics.IncrementRuleClassCount(t.ruleClass)
|
2021-08-11 06:17:36 +02:00
|
|
|
}
|
2021-02-19 17:06:17 +01:00
|
|
|
} else {
|
2022-01-20 18:55:05 +01:00
|
|
|
metrics.AddUnconvertedModule(moduleType)
|
2021-02-17 19:22:03 +01:00
|
|
|
return
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
2021-01-25 04:55:54 +01:00
|
|
|
case QueryView:
|
2021-02-08 06:49:32 +01:00
|
|
|
// Blocklist certain module types from being generated.
|
2021-02-19 06:48:40 +01:00
|
|
|
if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" {
|
2021-02-08 06:49:32 +01:00
|
|
|
// package module name contain slashes, and thus cannot
|
|
|
|
// be mapped cleanly to a bazel label.
|
|
|
|
return
|
|
|
|
}
|
2021-08-11 06:17:36 +02:00
|
|
|
t := generateSoongModuleTarget(bpCtx, m)
|
|
|
|
targets = append(targets, t)
|
2021-01-25 04:55:54 +01:00
|
|
|
default:
|
2021-08-26 14:37:59 +02:00
|
|
|
errs = append(errs, fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode()))
|
|
|
|
return
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:17:36 +02:00
|
|
|
buildFileToTargets[dir] = append(buildFileToTargets[dir], targets...)
|
2020-11-26 01:06:39 +01:00
|
|
|
})
|
2021-08-26 14:37:59 +02:00
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return conversionResults{}, errs
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:10:09 +02:00
|
|
|
if generateFilegroups {
|
|
|
|
// Add a filegroup target that exposes all sources in the subtree of this package
|
|
|
|
// NOTE: This also means we generate a BUILD file for every Android.bp file (as long as it has at least one module)
|
|
|
|
for dir, _ := range dirs {
|
|
|
|
buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{
|
|
|
|
name: "bp2build_all_srcs",
|
|
|
|
content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]))`,
|
|
|
|
ruleClass: "filegroup",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 06:48:40 +01:00
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
return conversionResults{
|
|
|
|
buildFileToTargets: buildFileToTargets,
|
|
|
|
metrics: metrics,
|
|
|
|
}, errs
|
2021-02-19 06:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
func getBazelPackagePath(b android.Bazelable) string {
|
2021-02-24 22:55:11 +01:00
|
|
|
label := b.HandcraftedLabel()
|
2021-02-17 19:22:03 +01:00
|
|
|
pathToBuildFile := strings.TrimPrefix(label, "//")
|
|
|
|
pathToBuildFile = strings.Split(pathToBuildFile, ":")[0]
|
|
|
|
return pathToBuildFile
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHandcraftedBuildContent(ctx *CodegenContext, b android.Bazelable, pathToBuildFile string) (BazelTarget, error) {
|
|
|
|
p := android.ExistentPathForSource(ctx, pathToBuildFile, HandcraftedBuildFileName)
|
|
|
|
if !p.Valid() {
|
|
|
|
return BazelTarget{}, fmt.Errorf("Could not find file %q for handcrafted target.", pathToBuildFile)
|
|
|
|
}
|
|
|
|
c, err := b.GetBazelBuildFileContents(ctx.Config(), pathToBuildFile, HandcraftedBuildFileName)
|
|
|
|
if err != nil {
|
|
|
|
return BazelTarget{}, err
|
|
|
|
}
|
|
|
|
// TODO(b/181575318): once this is more targeted, we need to include name, rule class, etc
|
|
|
|
return BazelTarget{
|
2021-05-25 07:16:48 +02:00
|
|
|
content: c,
|
|
|
|
handcrafted: true,
|
2021-02-17 19:22:03 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:17:36 +02:00
|
|
|
func generateBazelTargets(ctx bpToBuildContext, m android.Module) []BazelTarget {
|
|
|
|
var targets []BazelTarget
|
|
|
|
for _, m := range m.Bp2buildTargets() {
|
|
|
|
targets = append(targets, generateBazelTarget(ctx, m))
|
|
|
|
}
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
|
|
|
type bp2buildModule interface {
|
|
|
|
TargetName() string
|
|
|
|
TargetPackage() string
|
|
|
|
BazelRuleClass() string
|
|
|
|
BazelRuleLoadLocation() string
|
2021-08-31 22:30:36 +02:00
|
|
|
BazelAttributes() []interface{}
|
2021-08-11 06:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) BazelTarget {
|
|
|
|
ruleClass := m.BazelRuleClass()
|
|
|
|
bzlLoadLocation := m.BazelRuleLoadLocation()
|
2021-01-27 03:58:43 +01:00
|
|
|
|
2020-12-14 14:25:34 +01:00
|
|
|
// extract the bazel attributes from the module.
|
2021-08-31 22:30:36 +02:00
|
|
|
attrs := m.BazelAttributes()
|
|
|
|
props := extractModuleProperties(attrs, true)
|
2020-12-14 14:25:34 +01:00
|
|
|
|
2021-10-06 16:32:26 +02:00
|
|
|
// name is handled in a special manner
|
|
|
|
delete(props.Attrs, "name")
|
2021-02-05 09:03:24 +01:00
|
|
|
|
2020-12-14 14:25:34 +01:00
|
|
|
// Return the Bazel target with rule class and attributes, ready to be
|
|
|
|
// code-generated.
|
|
|
|
attributes := propsToAttributes(props.Attrs)
|
2021-08-11 06:17:36 +02:00
|
|
|
targetName := m.TargetName()
|
2020-12-14 14:25:34 +01:00
|
|
|
return BazelTarget{
|
2021-01-27 03:58:43 +01:00
|
|
|
name: targetName,
|
2021-08-11 06:17:36 +02:00
|
|
|
packageName: m.TargetPackage(),
|
2021-01-27 03:58:43 +01:00
|
|
|
ruleClass: ruleClass,
|
|
|
|
bzlLoadLocation: bzlLoadLocation,
|
2020-12-14 14:25:34 +01:00
|
|
|
content: fmt.Sprintf(
|
|
|
|
bazelTarget,
|
|
|
|
ruleClass,
|
|
|
|
targetName,
|
|
|
|
attributes,
|
|
|
|
),
|
2021-05-25 07:16:48 +02:00
|
|
|
handcrafted: false,
|
2020-12-14 14:25:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
// Convert a module and its deps and props into a Bazel macro/rule
|
|
|
|
// representation in the BUILD file.
|
|
|
|
func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
|
|
|
|
props := getBuildProperties(ctx, m)
|
|
|
|
|
|
|
|
// TODO(b/163018919): DirectDeps can have duplicate (module, variant)
|
|
|
|
// items, if the modules are added using different DependencyTag. Figure
|
|
|
|
// out the implications of that.
|
|
|
|
depLabels := map[string]bool{}
|
|
|
|
if aModule, ok := m.(android.Module); ok {
|
2020-12-14 08:58:54 +01:00
|
|
|
ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) {
|
2020-11-26 01:06:39 +01:00
|
|
|
depLabels[qualifiedTargetLabel(ctx, depModule)] = true
|
|
|
|
})
|
|
|
|
}
|
2021-10-06 16:32:26 +02:00
|
|
|
|
2022-07-11 17:29:56 +02:00
|
|
|
for p := range ignoredPropNames {
|
2021-10-06 16:32:26 +02:00
|
|
|
delete(props.Attrs, p)
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
attributes := propsToAttributes(props.Attrs)
|
|
|
|
|
|
|
|
depLabelList := "[\n"
|
2022-07-11 17:29:56 +02:00
|
|
|
for depLabel := range depLabels {
|
2020-11-26 01:06:39 +01:00
|
|
|
depLabelList += fmt.Sprintf(" %q,\n", depLabel)
|
|
|
|
}
|
|
|
|
depLabelList += " ]"
|
|
|
|
|
|
|
|
targetName := targetNameWithVariant(ctx, m)
|
|
|
|
return BazelTarget{
|
|
|
|
name: targetName,
|
|
|
|
content: fmt.Sprintf(
|
|
|
|
soongModuleTarget,
|
|
|
|
targetName,
|
|
|
|
ctx.ModuleName(m),
|
|
|
|
canonicalizeModuleType(ctx.ModuleType(m)),
|
|
|
|
ctx.ModuleSubDir(m),
|
|
|
|
depLabelList,
|
|
|
|
attributes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) BazelAttributes {
|
|
|
|
// TODO: this omits properties for blueprint modules (blueprint_go_binary,
|
|
|
|
// bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately.
|
|
|
|
if aModule, ok := m.(android.Module); ok {
|
2021-08-31 22:30:36 +02:00
|
|
|
return extractModuleProperties(aModule.GetProperties(), false)
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:17:36 +02:00
|
|
|
return BazelAttributes{}
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generically extract module properties and types into a map, keyed by the module property name.
|
2021-08-31 22:30:36 +02:00
|
|
|
func extractModuleProperties(props []interface{}, checkForDuplicateProperties bool) BazelAttributes {
|
2020-11-26 01:06:39 +01:00
|
|
|
ret := map[string]string{}
|
|
|
|
|
|
|
|
// Iterate over this android.Module's property structs.
|
2021-08-11 06:17:36 +02:00
|
|
|
for _, properties := range props {
|
2020-11-26 01:06:39 +01:00
|
|
|
propertiesValue := reflect.ValueOf(properties)
|
|
|
|
// Check that propertiesValue is a pointer to the Properties struct, like
|
|
|
|
// *cc.BaseLinkerProperties or *java.CompilerProperties.
|
|
|
|
//
|
|
|
|
// propertiesValue can also be type-asserted to the structs to
|
|
|
|
// manipulate internal props, if needed.
|
|
|
|
if isStructPtr(propertiesValue.Type()) {
|
|
|
|
structValue := propertiesValue.Elem()
|
|
|
|
for k, v := range extractStructProperties(structValue, 0) {
|
2021-08-31 22:30:36 +02:00
|
|
|
if existing, exists := ret[k]; checkForDuplicateProperties && exists {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"%s (%v) is present in properties whereas it should be consolidated into a commonAttributes",
|
|
|
|
k, existing))
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
ret[k] = v
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"properties must be a pointer to a struct, got %T",
|
|
|
|
propertiesValue.Interface()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:17:36 +02:00
|
|
|
return BazelAttributes{
|
|
|
|
Attrs: ret,
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func isStructPtr(t reflect.Type) bool {
|
|
|
|
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
// prettyPrint a property value into the equivalent Starlark representation
|
|
|
|
// recursively.
|
2021-11-17 13:14:41 +01:00
|
|
|
func prettyPrint(propertyValue reflect.Value, indent int, emitZeroValues bool) (string, error) {
|
|
|
|
if !emitZeroValues && isZero(propertyValue) {
|
2020-11-26 01:06:39 +01:00
|
|
|
// A property value being set or unset actually matters -- Soong does set default
|
|
|
|
// values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at
|
|
|
|
// https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480
|
|
|
|
//
|
2021-03-30 12:24:19 +02:00
|
|
|
// In Bazel-parlance, we would use "attr.<type>(default = <default
|
|
|
|
// value>)" to set the default value of unset attributes. In the cases
|
|
|
|
// where the bp2build converter didn't set the default value within the
|
|
|
|
// mutator when creating the BazelTargetModule, this would be a zero
|
2021-03-24 15:04:33 +01:00
|
|
|
// value. For those cases, we return an empty string so we don't
|
|
|
|
// unnecessarily generate empty values.
|
|
|
|
return "", nil
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch propertyValue.Kind() {
|
|
|
|
case reflect.String:
|
2022-02-03 14:42:10 +01:00
|
|
|
return fmt.Sprintf("\"%v\"", escapeString(propertyValue.String())), nil
|
2020-11-26 01:06:39 +01:00
|
|
|
case reflect.Bool:
|
2022-02-03 14:42:10 +01:00
|
|
|
return starlark_fmt.PrintBool(propertyValue.Bool()), nil
|
2020-11-26 01:06:39 +01:00
|
|
|
case reflect.Int, reflect.Uint, reflect.Int64:
|
2022-02-03 14:42:10 +01:00
|
|
|
return fmt.Sprintf("%v", propertyValue.Interface()), nil
|
2020-11-26 01:06:39 +01:00
|
|
|
case reflect.Ptr:
|
2021-11-17 13:14:41 +01:00
|
|
|
return prettyPrint(propertyValue.Elem(), indent, emitZeroValues)
|
2020-11-26 01:06:39 +01:00
|
|
|
case reflect.Slice:
|
2022-02-03 14:42:10 +01:00
|
|
|
elements := make([]string, 0, propertyValue.Len())
|
|
|
|
for i := 0; i < propertyValue.Len(); i++ {
|
|
|
|
val, err := prettyPrint(propertyValue.Index(i), indent, emitZeroValues)
|
2020-11-26 01:06:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-02-03 14:42:10 +01:00
|
|
|
if val != "" {
|
|
|
|
elements = append(elements, val)
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-25 17:33:26 +01:00
|
|
|
return starlark_fmt.PrintList(elements, indent, func(s string) string {
|
|
|
|
return "%s"
|
|
|
|
}), nil
|
2021-04-08 16:40:57 +02:00
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
case reflect.Struct:
|
2021-02-24 13:20:12 +01:00
|
|
|
// Special cases where the bp2build sends additional information to the codegenerator
|
|
|
|
// by wrapping the attributes in a custom struct type.
|
2021-04-05 12:35:13 +02:00
|
|
|
if attr, ok := propertyValue.Interface().(bazel.Attribute); ok {
|
|
|
|
return prettyPrintAttribute(attr, indent)
|
2021-01-26 15:18:53 +01:00
|
|
|
} else if label, ok := propertyValue.Interface().(bazel.Label); ok {
|
|
|
|
return fmt.Sprintf("%q", label.Label), nil
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
// Sort and print the struct props by the key.
|
|
|
|
structProps := extractStructProperties(propertyValue, indent)
|
2021-06-09 09:18:37 +02:00
|
|
|
if len(structProps) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-02-03 14:42:10 +01:00
|
|
|
return starlark_fmt.PrintDict(structProps, indent), nil
|
2020-11-26 01:06:39 +01:00
|
|
|
case reflect.Interface:
|
|
|
|
// TODO(b/164227191): implement pretty print for interfaces.
|
|
|
|
// Interfaces are used for for arch, multilib and target properties.
|
|
|
|
return "", nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"unexpected kind for property struct field: %s", propertyValue.Kind())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a reflected property struct value into a map of property names and property values,
|
|
|
|
// which each property value correctly pretty-printed and indented at the right nest level,
|
|
|
|
// since property structs can be nested. In Starlark, nested structs are represented as nested
|
|
|
|
// dicts: https://docs.bazel.build/skylark/lib/dict.html
|
|
|
|
func extractStructProperties(structValue reflect.Value, indent int) map[string]string {
|
|
|
|
if structValue.Kind() != reflect.Struct {
|
|
|
|
panic(fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind()))
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := map[string]string{}
|
|
|
|
structType := structValue.Type()
|
|
|
|
for i := 0; i < structValue.NumField(); i++ {
|
|
|
|
field := structType.Field(i)
|
|
|
|
if shouldSkipStructField(field) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldValue := structValue.Field(i)
|
|
|
|
if isZero(fieldValue) {
|
|
|
|
// Ignore zero-valued fields
|
|
|
|
continue
|
|
|
|
}
|
2021-09-22 21:52:58 +02:00
|
|
|
|
2021-09-14 17:17:21 +02:00
|
|
|
// if the struct is embedded (anonymous), flatten the properties into the containing struct
|
|
|
|
if field.Anonymous {
|
|
|
|
if field.Type.Kind() == reflect.Ptr {
|
|
|
|
fieldValue = fieldValue.Elem()
|
|
|
|
}
|
|
|
|
if fieldValue.Type().Kind() == reflect.Struct {
|
|
|
|
propsToMerge := extractStructProperties(fieldValue, indent)
|
|
|
|
for prop, value := range propsToMerge {
|
|
|
|
ret[prop] = value
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
|
|
|
|
propertyName := proptools.PropertyNameForField(field.Name)
|
2021-11-17 13:14:41 +01:00
|
|
|
prettyPrintedValue, err := prettyPrint(fieldValue, indent+1, false)
|
2020-11-26 01:06:39 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(
|
|
|
|
fmt.Errorf(
|
|
|
|
"Error while parsing property: %q. %s",
|
|
|
|
propertyName,
|
|
|
|
err))
|
|
|
|
}
|
|
|
|
if prettyPrintedValue != "" {
|
|
|
|
ret[propertyName] = prettyPrintedValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func isZero(value reflect.Value) bool {
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Func, reflect.Map, reflect.Slice:
|
|
|
|
return value.IsNil()
|
|
|
|
case reflect.Array:
|
|
|
|
valueIsZero := true
|
|
|
|
for i := 0; i < value.Len(); i++ {
|
|
|
|
valueIsZero = valueIsZero && isZero(value.Index(i))
|
|
|
|
}
|
|
|
|
return valueIsZero
|
|
|
|
case reflect.Struct:
|
|
|
|
valueIsZero := true
|
|
|
|
for i := 0; i < value.NumField(); i++ {
|
2021-04-30 15:35:09 +02:00
|
|
|
valueIsZero = valueIsZero && isZero(value.Field(i))
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
return valueIsZero
|
|
|
|
case reflect.Ptr:
|
|
|
|
if !value.IsNil() {
|
|
|
|
return isZero(reflect.Indirect(value))
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
2021-12-01 16:09:34 +01:00
|
|
|
// Always print bool/strings, if you want a bool/string attribute to be able to take the default value, use a
|
|
|
|
// pointer instead
|
|
|
|
case reflect.Bool, reflect.String:
|
2021-06-03 19:43:01 +02:00
|
|
|
return false
|
2020-11-26 01:06:39 +01:00
|
|
|
default:
|
2021-05-19 12:49:02 +02:00
|
|
|
if !value.IsValid() {
|
|
|
|
return true
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
zeroValue := reflect.Zero(value.Type())
|
|
|
|
result := value.Interface() == zeroValue.Interface()
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func escapeString(s string) string {
|
|
|
|
s = strings.ReplaceAll(s, "\\", "\\\\")
|
2021-03-30 15:08:36 +02:00
|
|
|
|
|
|
|
// b/184026959: Reverse the application of some common control sequences.
|
|
|
|
// These must be generated literally in the BUILD file.
|
|
|
|
s = strings.ReplaceAll(s, "\t", "\\t")
|
|
|
|
s = strings.ReplaceAll(s, "\n", "\\n")
|
|
|
|
s = strings.ReplaceAll(s, "\r", "\\r")
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
return strings.ReplaceAll(s, "\"", "\\\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
|
|
|
|
name := ""
|
|
|
|
if c.ModuleSubDir(logicModule) != "" {
|
|
|
|
// TODO(b/162720883): Figure out a way to drop the "--" variant suffixes.
|
|
|
|
name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule)
|
|
|
|
} else {
|
|
|
|
name = c.ModuleName(logicModule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Replace(name, "//", "", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string {
|
|
|
|
return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule))
|
|
|
|
}
|