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"
|
2023-06-08 16:28:51 +02:00
|
|
|
"android/soong/ui/metrics/bp2build_metrics_proto"
|
2023-08-08 06:29:08 +02:00
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
"github.com/google/blueprint"
|
2023-06-14 23:30:38 +02:00
|
|
|
"github.com/google/blueprint/bootstrap"
|
2020-11-26 01:06:39 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BazelAttributes struct {
|
|
|
|
Attrs map[string]string
|
|
|
|
}
|
|
|
|
|
2023-09-15 00:16:58 +02:00
|
|
|
type BazelLoadSymbol struct {
|
|
|
|
// The name of the symbol in the file being loaded
|
|
|
|
symbol string
|
|
|
|
// The name the symbol wil have in this file. Can be left blank to use the same name as symbol.
|
|
|
|
alias string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BazelLoad struct {
|
|
|
|
file string
|
|
|
|
symbols []BazelLoadSymbol
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-15 00:16:58 +02:00
|
|
|
type BazelTarget struct {
|
|
|
|
name string
|
|
|
|
packageName string
|
|
|
|
content string
|
|
|
|
ruleClass string
|
|
|
|
loads []BazelLoad
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 20:24:34 +01:00
|
|
|
// PackageName returns the package of the Bazel target.
|
|
|
|
// Defaults to root of tree.
|
|
|
|
func (t BazelTarget) PackageName() string {
|
|
|
|
if t.packageName == "" {
|
|
|
|
return "."
|
|
|
|
}
|
|
|
|
return t.packageName
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:58:43 +01:00
|
|
|
// BazelTargets is a typedef for a slice of BazelTarget objects.
|
|
|
|
type BazelTargets []BazelTarget
|
|
|
|
|
2022-08-04 22:31:14 +02:00
|
|
|
func (targets BazelTargets) packageRule() *BazelTarget {
|
|
|
|
for _, target := range targets {
|
|
|
|
if target.ruleClass == "package" {
|
|
|
|
return &target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort a list of BazelTargets in-place, by name, and by generated/handcrafted types.
|
2021-05-25 07:16:48 +02:00
|
|
|
func (targets BazelTargets) sort() {
|
|
|
|
sort.Slice(targets, func(i, j int) bool {
|
|
|
|
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 {
|
2023-08-08 06:29:08 +02:00
|
|
|
var res strings.Builder
|
2021-01-27 03:58:43 +01:00
|
|
|
for i, target := range targets {
|
2022-08-04 22:31:14 +02:00
|
|
|
if target.ruleClass != "package" {
|
2023-08-08 06:29:08 +02:00
|
|
|
res.WriteString(target.content)
|
2022-08-04 22:31:14 +02:00
|
|
|
}
|
2021-01-27 03:58:43 +01:00
|
|
|
if i != len(targets)-1 {
|
2023-08-08 06:29:08 +02:00
|
|
|
res.WriteString("\n\n")
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 06:29:08 +02:00
|
|
|
return res.String()
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-09-15 00:16:58 +02:00
|
|
|
// First, merge all the load statements from all the targets onto one list
|
|
|
|
bzlToLoadedSymbols := map[string][]BazelLoadSymbol{}
|
2021-01-27 03:58:43 +01:00
|
|
|
for _, target := range targets {
|
2023-09-15 00:16:58 +02:00
|
|
|
for _, load := range target.loads {
|
|
|
|
outer:
|
|
|
|
for _, symbol := range load.symbols {
|
|
|
|
alias := symbol.alias
|
|
|
|
if alias == "" {
|
|
|
|
alias = symbol.symbol
|
|
|
|
}
|
|
|
|
for _, otherSymbol := range bzlToLoadedSymbols[load.file] {
|
|
|
|
otherAlias := otherSymbol.alias
|
|
|
|
if otherAlias == "" {
|
|
|
|
otherAlias = otherSymbol.symbol
|
|
|
|
}
|
|
|
|
if symbol.symbol == otherSymbol.symbol && alias == otherAlias {
|
|
|
|
continue outer
|
|
|
|
} else if alias == otherAlias {
|
|
|
|
panic(fmt.Sprintf("Conflicting destination (%s) for loads of %s and %s", alias, symbol.symbol, otherSymbol.symbol))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bzlToLoadedSymbols[load.file] = append(bzlToLoadedSymbols[load.file], symbol)
|
|
|
|
}
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 00:16:58 +02:00
|
|
|
var loadStatements strings.Builder
|
|
|
|
for i, bzl := range android.SortedKeys(bzlToLoadedSymbols) {
|
|
|
|
symbols := bzlToLoadedSymbols[bzl]
|
|
|
|
loadStatements.WriteString("load(\"")
|
|
|
|
loadStatements.WriteString(bzl)
|
|
|
|
loadStatements.WriteString("\", ")
|
|
|
|
sort.Slice(symbols, func(i, j int) bool {
|
|
|
|
if symbols[i].symbol < symbols[j].symbol {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return symbols[i].alias < symbols[j].alias
|
|
|
|
})
|
|
|
|
for j, symbol := range symbols {
|
|
|
|
if symbol.alias != "" && symbol.alias != symbol.symbol {
|
|
|
|
loadStatements.WriteString(symbol.alias)
|
|
|
|
loadStatements.WriteString(" = ")
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
2023-09-15 00:16:58 +02:00
|
|
|
loadStatements.WriteString("\"")
|
|
|
|
loadStatements.WriteString(symbol.symbol)
|
|
|
|
loadStatements.WriteString("\"")
|
|
|
|
if j != len(symbols)-1 {
|
|
|
|
loadStatements.WriteString(", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadStatements.WriteString(")")
|
|
|
|
if i != len(bzlToLoadedSymbols)-1 {
|
|
|
|
loadStatements.WriteString("\n")
|
2021-01-27 03:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-15 00:16:58 +02:00
|
|
|
return loadStatements.String()
|
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
|
2022-11-04 14:35:21 +01:00
|
|
|
context *android.Context
|
2021-09-17 09:16:13 +02:00
|
|
|
mode CodegenMode
|
|
|
|
additionalDeps []string
|
2021-08-26 14:37:59 +02:00
|
|
|
unconvertedDepMode unconvertedDepsMode
|
2022-11-09 03:14:01 +01:00
|
|
|
topDir string
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-04 14:35:21 +01:00
|
|
|
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.
|
2022-11-09 03:14:01 +01:00
|
|
|
func NewCodegenContext(config android.Config, context *android.Context, mode CodegenMode, topDir string) *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,
|
2022-11-09 03:14:01 +01:00
|
|
|
topDir: topDir,
|
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
|
2023-03-01 01:02:16 +01:00
|
|
|
for _, propName := range android.SortedKeys(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
|
|
|
|
}
|
|
|
|
|
2023-06-14 23:30:38 +02:00
|
|
|
// struct to store state of go bazel targets
|
|
|
|
// this implements bp2buildModule interface and is passed to generateBazelTargets
|
|
|
|
type goBazelTarget struct {
|
|
|
|
targetName string
|
|
|
|
targetPackage string
|
|
|
|
bazelRuleClass string
|
|
|
|
bazelRuleLoadLocation string
|
|
|
|
bazelAttributes []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ bp2buildModule = (*goBazelTarget)(nil)
|
|
|
|
|
|
|
|
func (g goBazelTarget) TargetName() string {
|
|
|
|
return g.targetName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g goBazelTarget) TargetPackage() string {
|
|
|
|
return g.targetPackage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g goBazelTarget) BazelRuleClass() string {
|
|
|
|
return g.bazelRuleClass
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g goBazelTarget) BazelRuleLoadLocation() string {
|
|
|
|
return g.bazelRuleLoadLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g goBazelTarget) BazelAttributes() []interface{} {
|
|
|
|
return g.bazelAttributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a target_compatible_with entry that is *not* compatible with android
|
|
|
|
func targetNotCompatibleWithAndroid() bazel.LabelListAttribute {
|
|
|
|
ret := bazel.LabelListAttribute{}
|
|
|
|
ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid,
|
|
|
|
bazel.MakeLabelList(
|
|
|
|
[]bazel.Label{
|
|
|
|
bazel.Label{
|
|
|
|
Label: "@platforms//:incompatible",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to return labels for srcs used in bootstrap_go_package and bootstrap_go_binary
|
|
|
|
// this function has the following limitations which make it unsuitable for widespread use
|
2023-06-21 03:50:33 +02:00
|
|
|
// - wildcard patterns in srcs
|
|
|
|
// This is ok for go since build/blueprint does not support it.
|
2023-06-14 23:30:38 +02:00
|
|
|
//
|
|
|
|
// Prefer to use `BazelLabelForModuleSrc` instead
|
2023-06-21 03:50:33 +02:00
|
|
|
func goSrcLabels(cfg android.Config, moduleDir string, srcs []string, linuxSrcs, darwinSrcs []string) bazel.LabelListAttribute {
|
2023-06-14 23:30:38 +02:00
|
|
|
labels := func(srcs []string) bazel.LabelList {
|
|
|
|
ret := []bazel.Label{}
|
|
|
|
for _, src := range srcs {
|
|
|
|
srcLabel := bazel.Label{
|
2023-06-21 03:50:33 +02:00
|
|
|
Label: src,
|
2023-06-14 23:30:38 +02:00
|
|
|
}
|
|
|
|
ret = append(ret, srcLabel)
|
|
|
|
}
|
2023-06-21 03:50:33 +02:00
|
|
|
// Respect package boundaries
|
|
|
|
return android.TransformSubpackagePaths(
|
|
|
|
cfg,
|
|
|
|
moduleDir,
|
|
|
|
bazel.MakeLabelList(ret),
|
|
|
|
)
|
2023-06-14 23:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret := bazel.LabelListAttribute{}
|
|
|
|
// common
|
|
|
|
ret.SetSelectValue(bazel.NoConfigAxis, "", labels(srcs))
|
|
|
|
// linux
|
|
|
|
ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsLinux, labels(linuxSrcs))
|
|
|
|
// darwin
|
|
|
|
ret.SetSelectValue(bazel.OsConfigurationAxis, bazel.OsDarwin, labels(darwinSrcs))
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func goDepLabels(deps []string, goModulesMap nameToGoLibraryModule) bazel.LabelListAttribute {
|
|
|
|
labels := []bazel.Label{}
|
|
|
|
for _, dep := range deps {
|
|
|
|
moduleDir := goModulesMap[dep].Dir
|
|
|
|
if moduleDir == "." {
|
|
|
|
moduleDir = ""
|
|
|
|
}
|
|
|
|
label := bazel.Label{
|
|
|
|
Label: fmt.Sprintf("//%s:%s", moduleDir, dep),
|
|
|
|
}
|
|
|
|
labels = append(labels, label)
|
|
|
|
}
|
|
|
|
return bazel.MakeLabelListAttribute(bazel.MakeLabelList(labels))
|
|
|
|
}
|
|
|
|
|
|
|
|
// attributes common to blueprint_go_binary and bootstap_go_package
|
|
|
|
type goAttributes struct {
|
|
|
|
Importpath bazel.StringAttribute
|
|
|
|
Srcs bazel.LabelListAttribute
|
|
|
|
Deps bazel.LabelListAttribute
|
2023-06-23 00:22:11 +02:00
|
|
|
Data bazel.LabelListAttribute
|
2023-06-14 23:30:38 +02:00
|
|
|
Target_compatible_with bazel.LabelListAttribute
|
2023-06-23 00:22:11 +02:00
|
|
|
|
|
|
|
// attributes for the dynamically generated go_test target
|
|
|
|
Embed bazel.LabelListAttribute
|
|
|
|
}
|
|
|
|
|
|
|
|
type goTestProperties struct {
|
|
|
|
name string
|
|
|
|
dir string
|
|
|
|
testSrcs []string
|
|
|
|
linuxTestSrcs []string
|
|
|
|
darwinTestSrcs []string
|
|
|
|
testData []string
|
|
|
|
// Name of the target that should be compiled together with the test
|
|
|
|
embedName string
|
2023-06-14 23:30:38 +02:00
|
|
|
}
|
|
|
|
|
2023-06-23 00:22:11 +02:00
|
|
|
// Creates a go_test target for bootstrap_go_package / blueprint_go_binary
|
|
|
|
func generateBazelTargetsGoTest(ctx *android.Context, goModulesMap nameToGoLibraryModule, gp goTestProperties) (BazelTarget, error) {
|
|
|
|
ca := android.CommonAttributes{
|
|
|
|
Name: gp.name,
|
|
|
|
}
|
|
|
|
ga := goAttributes{
|
|
|
|
Srcs: goSrcLabels(ctx.Config(), gp.dir, gp.testSrcs, gp.linuxTestSrcs, gp.darwinTestSrcs),
|
|
|
|
Data: goSrcLabels(ctx.Config(), gp.dir, gp.testData, []string{}, []string{}),
|
|
|
|
Embed: bazel.MakeLabelListAttribute(
|
|
|
|
bazel.MakeLabelList(
|
|
|
|
[]bazel.Label{bazel.Label{Label: ":" + gp.embedName}},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Target_compatible_with: targetNotCompatibleWithAndroid(),
|
|
|
|
}
|
|
|
|
|
|
|
|
libTest := goBazelTarget{
|
|
|
|
targetName: gp.name,
|
|
|
|
targetPackage: gp.dir,
|
|
|
|
bazelRuleClass: "go_test",
|
|
|
|
bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl",
|
|
|
|
bazelAttributes: []interface{}{&ca, &ga},
|
|
|
|
}
|
|
|
|
return generateBazelTarget(ctx, libTest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - b/288491147: testSrcs of certain bootstrap_go_package/blueprint_go_binary are not hermetic and depend on
|
|
|
|
// testdata checked into the filesystem.
|
|
|
|
// Denylist the generation of go_test targets for these Soong modules.
|
|
|
|
// The go_library/go_binary will still be generated, since those are hermitic.
|
|
|
|
var (
|
|
|
|
goTestsDenylist = []string{
|
|
|
|
"android-archive-zip",
|
|
|
|
"bazel_notice_gen",
|
|
|
|
"blueprint-bootstrap-bpdoc",
|
|
|
|
"blueprint-microfactory",
|
|
|
|
"blueprint-pathtools",
|
|
|
|
"bssl_ar",
|
|
|
|
"compliance_checkmetadata",
|
|
|
|
"compliance_checkshare",
|
|
|
|
"compliance_dumpgraph",
|
|
|
|
"compliance_dumpresolutions",
|
|
|
|
"compliance_listshare",
|
|
|
|
"compliance-module",
|
|
|
|
"compliancenotice_bom",
|
|
|
|
"compliancenotice_shippedlibs",
|
|
|
|
"compliance_rtrace",
|
|
|
|
"compliance_sbom",
|
|
|
|
"golang-protobuf-internal-fuzz-jsonfuzz",
|
|
|
|
"golang-protobuf-internal-fuzz-textfuzz",
|
|
|
|
"golang-protobuf-internal-fuzz-wirefuzz",
|
|
|
|
"htmlnotice",
|
|
|
|
"protoc-gen-go",
|
|
|
|
"rbcrun-module",
|
|
|
|
"spdx-tools-builder",
|
|
|
|
"spdx-tools-builder2v1",
|
|
|
|
"spdx-tools-builder2v2",
|
|
|
|
"spdx-tools-builder2v3",
|
|
|
|
"spdx-tools-idsearcher",
|
|
|
|
"spdx-tools-spdx-json",
|
|
|
|
"spdx-tools-utils",
|
|
|
|
"soong-ui-build",
|
|
|
|
"textnotice",
|
|
|
|
"xmlnotice",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-06-30 22:18:39 +02:00
|
|
|
func testOfGoPackageIsIncompatible(g *bootstrap.GoPackage) bool {
|
|
|
|
return android.InList(g.Name(), goTestsDenylist) ||
|
|
|
|
// Denylist tests of soong_build
|
|
|
|
// Theses tests have a guard that prevent usage outside a test environment
|
|
|
|
// The guard (`ensureTestOnly`) looks for a `-test` in os.Args, which is present in soong's gotestrunner, but missing in `b test`
|
|
|
|
g.IsPluginFor("soong_build") ||
|
|
|
|
// soong-android is a dep of soong_build
|
|
|
|
// This dependency is created by soong_build by listing it in its deps explicitly in Android.bp, and not via `plugin_for` in `soong-android`
|
|
|
|
g.Name() == "soong-android"
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOfGoBinaryIsIncompatible(g *bootstrap.GoBinary) bool {
|
|
|
|
return android.InList(g.Name(), goTestsDenylist)
|
|
|
|
}
|
|
|
|
|
2023-06-14 23:30:38 +02:00
|
|
|
func generateBazelTargetsGoPackage(ctx *android.Context, g *bootstrap.GoPackage, goModulesMap nameToGoLibraryModule) ([]BazelTarget, []error) {
|
|
|
|
ca := android.CommonAttributes{
|
|
|
|
Name: g.Name(),
|
|
|
|
}
|
2023-06-14 23:30:38 +02:00
|
|
|
|
|
|
|
// For this bootstrap_go_package dep chain,
|
|
|
|
// A --> B --> C ( ---> depends on)
|
|
|
|
// Soong provides the convenience of only listing B as deps of A even if a src file of A imports C
|
|
|
|
// Bazel OTOH
|
|
|
|
// 1. requires C to be listed in `deps` expllicity.
|
|
|
|
// 2. does not require C to be listed if src of A does not import C
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
transitiveDeps := transitiveGoDeps(g.Deps(), goModulesMap)
|
|
|
|
|
2023-06-14 23:30:38 +02:00
|
|
|
ga := goAttributes{
|
|
|
|
Importpath: bazel.StringAttribute{
|
|
|
|
Value: proptools.StringPtr(g.GoPkgPath()),
|
|
|
|
},
|
2023-06-21 03:50:33 +02:00
|
|
|
Srcs: goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
|
|
|
|
Deps: goDepLabels(
|
|
|
|
android.FirstUniqueStrings(transitiveDeps),
|
|
|
|
goModulesMap,
|
|
|
|
),
|
2023-06-14 23:30:38 +02:00
|
|
|
Target_compatible_with: targetNotCompatibleWithAndroid(),
|
|
|
|
}
|
|
|
|
|
|
|
|
lib := goBazelTarget{
|
|
|
|
targetName: g.Name(),
|
|
|
|
targetPackage: ctx.ModuleDir(g),
|
|
|
|
bazelRuleClass: "go_library",
|
|
|
|
bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl",
|
|
|
|
bazelAttributes: []interface{}{&ca, &ga},
|
|
|
|
}
|
2023-06-23 00:22:11 +02:00
|
|
|
retTargets := []BazelTarget{}
|
|
|
|
var retErrs []error
|
|
|
|
if libTarget, err := generateBazelTarget(ctx, lib); err == nil {
|
|
|
|
retTargets = append(retTargets, libTarget)
|
|
|
|
} else {
|
|
|
|
retErrs = []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the library contains test srcs, create an additional go_test target
|
2023-06-30 22:18:39 +02:00
|
|
|
if !testOfGoPackageIsIncompatible(g) && (len(g.TestSrcs()) > 0 || len(g.LinuxTestSrcs()) > 0 || len(g.DarwinTestSrcs()) > 0) {
|
2023-06-23 00:22:11 +02:00
|
|
|
gp := goTestProperties{
|
|
|
|
name: g.Name() + "-test",
|
|
|
|
dir: ctx.ModuleDir(g),
|
|
|
|
testSrcs: g.TestSrcs(),
|
|
|
|
linuxTestSrcs: g.LinuxTestSrcs(),
|
|
|
|
darwinTestSrcs: g.DarwinTestSrcs(),
|
|
|
|
testData: g.TestData(),
|
|
|
|
embedName: g.Name(), // embed the source go_library in the test so that its .go files are included in the compilation unit
|
|
|
|
}
|
|
|
|
if libTestTarget, err := generateBazelTargetsGoTest(ctx, goModulesMap, gp); err == nil {
|
|
|
|
retTargets = append(retTargets, libTestTarget)
|
|
|
|
} else {
|
|
|
|
retErrs = append(retErrs, err)
|
|
|
|
}
|
2023-06-14 23:30:38 +02:00
|
|
|
}
|
2023-06-23 00:22:11 +02:00
|
|
|
|
|
|
|
return retTargets, retErrs
|
2023-06-14 23:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type goLibraryModule struct {
|
|
|
|
Dir string
|
|
|
|
Deps []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type nameToGoLibraryModule map[string]goLibraryModule
|
|
|
|
|
|
|
|
// Visit each module in the graph
|
|
|
|
// If a module is of type `bootstrap_go_package`, return a map containing metadata like its dir and deps
|
|
|
|
func createGoLibraryModuleMap(ctx *android.Context) nameToGoLibraryModule {
|
|
|
|
ret := nameToGoLibraryModule{}
|
|
|
|
ctx.VisitAllModules(func(m blueprint.Module) {
|
|
|
|
moduleType := ctx.ModuleType(m)
|
|
|
|
// We do not need to store information about blueprint_go_binary since it does not have any rdeps
|
|
|
|
if moduleType == "bootstrap_go_package" {
|
|
|
|
ret[m.Name()] = goLibraryModule{
|
|
|
|
Dir: ctx.ModuleDir(m),
|
|
|
|
Deps: m.(*bootstrap.GoPackage).Deps(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-06-14 23:30:38 +02:00
|
|
|
// Returns the deps in the transitive closure of a go target
|
|
|
|
func transitiveGoDeps(directDeps []string, goModulesMap nameToGoLibraryModule) []string {
|
|
|
|
allDeps := directDeps
|
|
|
|
i := 0
|
|
|
|
for i < len(allDeps) {
|
|
|
|
curr := allDeps[i]
|
|
|
|
allDeps = append(allDeps, goModulesMap[curr].Deps...)
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
allDeps = android.SortedUniqueStrings(allDeps)
|
|
|
|
return allDeps
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBazelTargetsGoBinary(ctx *android.Context, g *bootstrap.GoBinary, goModulesMap nameToGoLibraryModule) ([]BazelTarget, []error) {
|
|
|
|
ca := android.CommonAttributes{
|
|
|
|
Name: g.Name(),
|
|
|
|
}
|
|
|
|
|
2023-06-23 00:22:11 +02:00
|
|
|
retTargets := []BazelTarget{}
|
|
|
|
var retErrs []error
|
|
|
|
|
2023-06-14 23:30:38 +02:00
|
|
|
// For this bootstrap_go_package dep chain,
|
|
|
|
// A --> B --> C ( ---> depends on)
|
|
|
|
// Soong provides the convenience of only listing B as deps of A even if a src file of A imports C
|
|
|
|
// Bazel OTOH
|
|
|
|
// 1. requires C to be listed in `deps` expllicity.
|
|
|
|
// 2. does not require C to be listed if src of A does not import C
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
transitiveDeps := transitiveGoDeps(g.Deps(), goModulesMap)
|
|
|
|
|
2023-06-23 00:22:11 +02:00
|
|
|
goSource := ""
|
|
|
|
// If the library contains test srcs, create an additional go_test target
|
|
|
|
// The go_test target will embed a go_source containining the source .go files it tests
|
2023-06-30 22:18:39 +02:00
|
|
|
if !testOfGoBinaryIsIncompatible(g) && (len(g.TestSrcs()) > 0 || len(g.LinuxTestSrcs()) > 0 || len(g.DarwinTestSrcs()) > 0) {
|
2023-06-23 00:22:11 +02:00
|
|
|
// Create a go_source containing the source .go files of go_library
|
|
|
|
// This target will be an `embed` of the go_binary and go_test
|
|
|
|
goSource = g.Name() + "-source"
|
|
|
|
ca := android.CommonAttributes{
|
|
|
|
Name: goSource,
|
|
|
|
}
|
|
|
|
ga := goAttributes{
|
|
|
|
Srcs: goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
|
|
|
|
Deps: goDepLabels(transitiveDeps, goModulesMap),
|
|
|
|
Target_compatible_with: targetNotCompatibleWithAndroid(),
|
|
|
|
}
|
|
|
|
libTestSource := goBazelTarget{
|
|
|
|
targetName: goSource,
|
|
|
|
targetPackage: ctx.ModuleDir(g),
|
|
|
|
bazelRuleClass: "go_source",
|
|
|
|
bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl",
|
|
|
|
bazelAttributes: []interface{}{&ca, &ga},
|
|
|
|
}
|
|
|
|
if libSourceTarget, err := generateBazelTarget(ctx, libTestSource); err == nil {
|
|
|
|
retTargets = append(retTargets, libSourceTarget)
|
|
|
|
} else {
|
|
|
|
retErrs = append(retErrs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a go_test target
|
|
|
|
gp := goTestProperties{
|
|
|
|
name: g.Name() + "-test",
|
|
|
|
dir: ctx.ModuleDir(g),
|
|
|
|
testSrcs: g.TestSrcs(),
|
|
|
|
linuxTestSrcs: g.LinuxTestSrcs(),
|
|
|
|
darwinTestSrcs: g.DarwinTestSrcs(),
|
|
|
|
testData: g.TestData(),
|
|
|
|
// embed the go_source in the test
|
|
|
|
embedName: g.Name() + "-source",
|
|
|
|
}
|
|
|
|
if libTestTarget, err := generateBazelTargetsGoTest(ctx, goModulesMap, gp); err == nil {
|
|
|
|
retTargets = append(retTargets, libTestTarget)
|
|
|
|
} else {
|
|
|
|
retErrs = append(retErrs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a go_binary target
|
2023-06-14 23:30:38 +02:00
|
|
|
ga := goAttributes{
|
|
|
|
Deps: goDepLabels(transitiveDeps, goModulesMap),
|
|
|
|
Target_compatible_with: targetNotCompatibleWithAndroid(),
|
|
|
|
}
|
|
|
|
|
2023-06-23 00:22:11 +02:00
|
|
|
// If the binary has testSrcs, embed the common `go_source`
|
|
|
|
if goSource != "" {
|
|
|
|
ga.Embed = bazel.MakeLabelListAttribute(
|
|
|
|
bazel.MakeLabelList(
|
|
|
|
[]bazel.Label{bazel.Label{Label: ":" + goSource}},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
ga.Srcs = goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs())
|
|
|
|
}
|
|
|
|
|
2023-06-14 23:30:38 +02:00
|
|
|
bin := goBazelTarget{
|
|
|
|
targetName: g.Name(),
|
|
|
|
targetPackage: ctx.ModuleDir(g),
|
|
|
|
bazelRuleClass: "go_binary",
|
|
|
|
bazelRuleLoadLocation: "@io_bazel_rules_go//go:def.bzl",
|
|
|
|
bazelAttributes: []interface{}{&ca, &ga},
|
|
|
|
}
|
2023-06-23 00:22:11 +02:00
|
|
|
|
|
|
|
if binTarget, err := generateBazelTarget(ctx, bin); err == nil {
|
|
|
|
retTargets = append(retTargets, binTarget)
|
|
|
|
} else {
|
|
|
|
retErrs = []error{err}
|
2023-06-14 23:30:38 +02:00
|
|
|
}
|
2023-06-23 00:22:11 +02:00
|
|
|
|
|
|
|
return retTargets, retErrs
|
2023-06-14 23:30:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) {
|
2023-07-01 17:40:36 +02:00
|
|
|
ctx.Context().BeginEvent("GenerateBazelTargets")
|
|
|
|
defer ctx.Context().EndEvent("GenerateBazelTargets")
|
2021-01-27 03:58:43 +01:00
|
|
|
buildFileToTargets := make(map[string]BazelTargets)
|
2021-02-19 06:48:40 +01:00
|
|
|
|
|
|
|
// Simple metrics tracking for bp2build
|
2022-10-29 05:32:01 +02:00
|
|
|
metrics := CreateCodegenMetrics()
|
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
|
|
|
|
|
2023-06-14 23:30:38 +02:00
|
|
|
// Visit go libraries in a pre-run and store its state in a map
|
|
|
|
// The time complexity remains O(N), and this does not add significant wall time.
|
|
|
|
nameToGoLibMap := createGoLibraryModuleMap(ctx.Context())
|
|
|
|
|
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
|
2023-06-14 23:30:38 +02:00
|
|
|
var targetErrs []error
|
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:
|
2023-09-21 22:36:35 +02:00
|
|
|
if aModule, ok := m.(android.Module); ok {
|
|
|
|
reason := aModule.GetUnconvertedReason()
|
|
|
|
if reason != nil {
|
|
|
|
// If this module was force-enabled, cause an error.
|
|
|
|
if _, ok := ctx.Config().BazelModulesForceEnabledByFlag()[m.Name()]; ok && m.Name() != "" {
|
|
|
|
err := fmt.Errorf("Force Enabled Module %s not converted", m.Name())
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log the module isn't to be converted by bp2build.
|
|
|
|
// TODO: b/291598248 - Log handcrafted modules differently than other unconverted modules.
|
|
|
|
metrics.AddUnconvertedModule(m, moduleType, dir, *reason)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(aModule.Bp2buildTargets()) == 0 {
|
|
|
|
panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", aModule.Name()))
|
2023-08-11 22:15:12 +02:00
|
|
|
}
|
2021-09-20 12:54:27 +02:00
|
|
|
|
|
|
|
// Handle modules converted to generated targets.
|
2023-09-21 22:36:35 +02:00
|
|
|
targets, targetErrs = generateBazelTargets(bpCtx, aModule)
|
|
|
|
errs = append(errs, targetErrs...)
|
|
|
|
for _, t := range targets {
|
|
|
|
// A module can potentially generate more than 1 Bazel
|
|
|
|
// target, each of a different rule class.
|
|
|
|
metrics.IncrementRuleClassCount(t.ruleClass)
|
|
|
|
}
|
2021-09-20 12:54:27 +02:00
|
|
|
|
|
|
|
// Log the module.
|
2023-06-08 16:28:51 +02:00
|
|
|
metrics.AddConvertedModule(aModule, moduleType, dir)
|
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 {
|
2022-08-04 20:28:15 +02:00
|
|
|
msg := fmt.Sprintf("%s %s:%s depends on unconverted modules: %s",
|
|
|
|
moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", "))
|
2022-09-24 06:23:31 +02:00
|
|
|
switch ctx.unconvertedDepMode {
|
|
|
|
case warnUnconvertedDeps:
|
2021-08-26 14:37:59 +02:00
|
|
|
metrics.moduleWithUnconvertedDepsMsgs = append(metrics.moduleWithUnconvertedDepsMsgs, msg)
|
2022-09-24 06:23:31 +02:00
|
|
|
case errorModulesUnconvertedDeps:
|
2021-08-26 14:37:59 +02:00
|
|
|
errs = append(errs, fmt.Errorf(msg))
|
|
|
|
return
|
2021-12-15 21:35:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if unconvertedDeps := aModule.GetMissingBp2buildDeps(); len(unconvertedDeps) > 0 {
|
2022-08-04 20:28:15 +02:00
|
|
|
msg := fmt.Sprintf("%s %s:%s depends on missing modules: %s",
|
|
|
|
moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", "))
|
2022-09-24 06:23:31 +02:00
|
|
|
switch ctx.unconvertedDepMode {
|
|
|
|
case warnUnconvertedDeps:
|
2021-12-15 21:35:38 +01:00
|
|
|
metrics.moduleWithMissingDepsMsgs = append(metrics.moduleWithMissingDepsMsgs, msg)
|
2022-09-24 06:23:31 +02:00
|
|
|
case errorModulesUnconvertedDeps:
|
2021-12-15 21:35:38 +01:00
|
|
|
errs = append(errs, fmt.Errorf(msg))
|
|
|
|
return
|
2021-08-26 14:37:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 23:30:38 +02:00
|
|
|
} else if glib, ok := m.(*bootstrap.GoPackage); ok {
|
|
|
|
targets, targetErrs = generateBazelTargetsGoPackage(bpCtx, glib, nameToGoLibMap)
|
|
|
|
errs = append(errs, targetErrs...)
|
|
|
|
metrics.IncrementRuleClassCount("go_library")
|
2023-08-02 00:28:16 +02:00
|
|
|
metrics.AddConvertedModule(glib, "go_library", dir)
|
2023-06-14 19:56:10 +02:00
|
|
|
} else if gbin, ok := m.(*bootstrap.GoBinary); ok {
|
2023-06-14 23:30:38 +02:00
|
|
|
targets, targetErrs = generateBazelTargetsGoBinary(bpCtx, gbin, nameToGoLibMap)
|
|
|
|
errs = append(errs, targetErrs...)
|
|
|
|
metrics.IncrementRuleClassCount("go_binary")
|
2023-08-02 00:28:16 +02:00
|
|
|
metrics.AddConvertedModule(gbin, "go_binary", dir)
|
2021-02-19 17:06:17 +01:00
|
|
|
} else {
|
2023-06-08 16:28:51 +02:00
|
|
|
metrics.AddUnconvertedModule(m, moduleType, dir, android.UnconvertedReason{
|
|
|
|
ReasonType: int(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED),
|
|
|
|
})
|
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
|
|
|
|
}
|
2022-08-16 22:37:33 +02:00
|
|
|
t, err := generateSoongModuleTarget(bpCtx, m)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2021-08-11 06:17:36 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-07 20:24:34 +01:00
|
|
|
for _, target := range targets {
|
|
|
|
targetDir := target.PackageName()
|
|
|
|
buildFileToTargets[targetDir] = append(buildFileToTargets[targetDir], target)
|
|
|
|
}
|
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)
|
2022-08-24 00:29:05 +02:00
|
|
|
//
|
|
|
|
// This works because: https://bazel.build/reference/be/functions#exports_files
|
|
|
|
// "As a legacy behaviour, also files mentioned as input to a rule are exported with the
|
|
|
|
// default visibility until the flag --incompatible_no_implicit_file_export is flipped. However, this behavior
|
|
|
|
// should not be relied upon and actively migrated away from."
|
|
|
|
//
|
|
|
|
// TODO(b/198619163): We should change this to export_files(glob(["**/*"])) instead, but doing that causes these errors:
|
|
|
|
// "Error in exports_files: generated label '//external/avb:avbtool' conflicts with existing py_binary rule"
|
|
|
|
// So we need to solve all the "target ... is both a rule and a file" warnings first.
|
2022-09-24 06:23:31 +02:00
|
|
|
for dir := range dirs {
|
2021-04-21 13:10:09 +02:00
|
|
|
buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{
|
|
|
|
name: "bp2build_all_srcs",
|
2023-09-20 12:25:09 +02:00
|
|
|
content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]), tags = ["manual"])`,
|
2021-04-21 13:10:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-16 22:37:33 +02:00
|
|
|
func generateBazelTargets(ctx bpToBuildContext, m android.Module) ([]BazelTarget, []error) {
|
2021-08-11 06:17:36 +02:00
|
|
|
var targets []BazelTarget
|
2022-08-16 22:37:33 +02:00
|
|
|
var errs []error
|
2021-08-11 06:17:36 +02:00
|
|
|
for _, m := range m.Bp2buildTargets() {
|
2022-08-16 22:37:33 +02:00
|
|
|
target, err := generateBazelTarget(ctx, m)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
return targets, errs
|
|
|
|
}
|
|
|
|
targets = append(targets, target)
|
2021-08-11 06:17:36 +02:00
|
|
|
}
|
2022-08-16 22:37:33 +02:00
|
|
|
return targets, errs
|
2021-08-11 06:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-16 22:37:33 +02:00
|
|
|
func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) (BazelTarget, error) {
|
2021-08-11 06:17:36 +02:00
|
|
|
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()
|
2022-08-16 22:37:33 +02:00
|
|
|
props, err := extractModuleProperties(attrs, true)
|
|
|
|
if err != nil {
|
|
|
|
return BazelTarget{}, err
|
|
|
|
}
|
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)
|
2022-08-04 20:13:27 +02:00
|
|
|
var content string
|
2021-08-11 06:17:36 +02:00
|
|
|
targetName := m.TargetName()
|
2022-08-04 20:13:27 +02:00
|
|
|
if targetName != "" {
|
|
|
|
content = fmt.Sprintf(ruleTargetTemplate, ruleClass, targetName, attributes)
|
|
|
|
} else {
|
|
|
|
content = fmt.Sprintf(unnamedRuleTargetTemplate, ruleClass, attributes)
|
|
|
|
}
|
2023-09-15 00:16:58 +02:00
|
|
|
var loads []BazelLoad
|
|
|
|
if bzlLoadLocation != "" {
|
|
|
|
loads = append(loads, BazelLoad{
|
|
|
|
file: bzlLoadLocation,
|
|
|
|
symbols: []BazelLoadSymbol{{symbol: ruleClass}},
|
|
|
|
})
|
|
|
|
}
|
2020-12-14 14:25:34 +01:00
|
|
|
return BazelTarget{
|
2023-09-15 00:16:58 +02:00
|
|
|
name: targetName,
|
|
|
|
packageName: m.TargetPackage(),
|
|
|
|
ruleClass: ruleClass,
|
|
|
|
loads: loads,
|
|
|
|
content: content,
|
2022-08-16 22:37:33 +02:00
|
|
|
}, nil
|
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.
|
2022-08-16 22:37:33 +02:00
|
|
|
func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) (BazelTarget, error) {
|
|
|
|
props, err := getBuildProperties(ctx, m)
|
2020-11-26 01:06:39 +01:00
|
|
|
|
|
|
|
// 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{
|
2023-03-07 20:24:34 +01:00
|
|
|
name: targetName,
|
|
|
|
packageName: ctx.ModuleDir(m),
|
2020-11-26 01:06:39 +01:00
|
|
|
content: fmt.Sprintf(
|
2022-08-04 20:13:27 +02:00
|
|
|
soongModuleTargetTemplate,
|
2020-11-26 01:06:39 +01:00
|
|
|
targetName,
|
|
|
|
ctx.ModuleName(m),
|
|
|
|
canonicalizeModuleType(ctx.ModuleType(m)),
|
|
|
|
ctx.ModuleSubDir(m),
|
|
|
|
depLabelList,
|
|
|
|
attributes),
|
2022-08-16 22:37:33 +02:00
|
|
|
}, err
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
2022-08-16 22:37:33 +02:00
|
|
|
func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) (BazelAttributes, error) {
|
2020-11-26 01:06:39 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-08-16 22:37:33 +02:00
|
|
|
return BazelAttributes{}, nil
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generically extract module properties and types into a map, keyed by the module property name.
|
2022-08-16 22:37:33 +02:00
|
|
|
func extractModuleProperties(props []interface{}, checkForDuplicateProperties bool) (BazelAttributes, error) {
|
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()
|
2022-08-16 22:37:33 +02:00
|
|
|
ok, err := extractStructProperties(structValue, 0)
|
|
|
|
if err != nil {
|
|
|
|
return BazelAttributes{}, err
|
|
|
|
}
|
|
|
|
for k, v := range ok {
|
2021-08-31 22:30:36 +02:00
|
|
|
if existing, exists := ret[k]; checkForDuplicateProperties && exists {
|
2022-08-16 22:37:33 +02:00
|
|
|
return BazelAttributes{}, fmt.Errorf(
|
2021-08-31 22:30:36 +02:00
|
|
|
"%s (%v) is present in properties whereas it should be consolidated into a commonAttributes",
|
2022-08-16 22:37:33 +02:00
|
|
|
k, existing)
|
2021-08-31 22:30:36 +02:00
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
ret[k] = v
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-16 22:37:33 +02:00
|
|
|
return BazelAttributes{},
|
|
|
|
fmt.Errorf(
|
|
|
|
"properties must be a pointer to a struct, got %T",
|
|
|
|
propertiesValue.Interface())
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:17:36 +02:00
|
|
|
return BazelAttributes{
|
|
|
|
Attrs: ret,
|
2022-08-16 22:37:33 +02:00
|
|
|
}, nil
|
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.
|
2022-08-16 22:37:33 +02:00
|
|
|
structProps, err := extractStructProperties(propertyValue, indent)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
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
|
2023-04-19 19:36:12 +02:00
|
|
|
case reflect.Map:
|
|
|
|
if v, ok := propertyValue.Interface().(bazel.StringMapAttribute); ok {
|
|
|
|
return starlark_fmt.PrintStringStringDict(v, indent), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("bp2build expects map of type map[string]string for field: %s", propertyValue)
|
2020-11-26 01:06:39 +01:00
|
|
|
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
|
2022-08-16 22:37:33 +02:00
|
|
|
func extractStructProperties(structValue reflect.Value, indent int) (map[string]string, error) {
|
2020-11-26 01:06:39 +01:00
|
|
|
if structValue.Kind() != reflect.Struct {
|
2022-08-16 22:37:33 +02:00
|
|
|
return map[string]string{}, fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind())
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
2022-08-16 22:37:33 +02:00
|
|
|
var err error
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
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 {
|
2022-08-16 22:37:33 +02:00
|
|
|
propsToMerge, err := extractStructProperties(fieldValue, indent)
|
|
|
|
if err != nil {
|
|
|
|
return map[string]string{}, err
|
|
|
|
}
|
2021-09-14 17:17:21 +02:00
|
|
|
for prop, value := range propsToMerge {
|
|
|
|
ret[prop] = value
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
|
|
|
|
propertyName := proptools.PropertyNameForField(field.Name)
|
2022-08-16 22:37:33 +02:00
|
|
|
var prettyPrintedValue string
|
|
|
|
prettyPrintedValue, err = prettyPrint(fieldValue, indent+1, false)
|
2020-11-26 01:06:39 +01:00
|
|
|
if err != nil {
|
2022-08-16 22:37:33 +02:00
|
|
|
return map[string]string{}, fmt.Errorf(
|
|
|
|
"Error while parsing property: %q. %s",
|
|
|
|
propertyName,
|
|
|
|
err)
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
if prettyPrintedValue != "" {
|
|
|
|
ret[propertyName] = prettyPrintedValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 22:37:33 +02:00
|
|
|
return ret, nil
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|