2021-02-17 16:17:28 +01:00
|
|
|
// Copyright 2021 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 android
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
import (
|
2021-12-10 12:14:59 +01:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
2021-02-17 19:22:03 +01:00
|
|
|
"strings"
|
|
|
|
|
2021-02-24 22:55:11 +01:00
|
|
|
"github.com/google/blueprint"
|
2021-02-17 19:22:03 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
2022-03-28 21:53:03 +02:00
|
|
|
|
|
|
|
"android/soong/android/allowlists"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// A sentinel value to be used as a key in Bp2BuildConfig for modules with
|
|
|
|
// no package path. This is also the module dir for top level Android.bp
|
|
|
|
// modules.
|
|
|
|
Bp2BuildTopLevel = "."
|
2021-02-17 19:22:03 +01:00
|
|
|
)
|
|
|
|
|
2022-09-01 20:54:47 +02:00
|
|
|
// FileGroupAsLibrary describes a filegroup module that is converted to some library
|
|
|
|
// such as aidl_library or proto_library.
|
|
|
|
type FileGroupAsLibrary interface {
|
2022-08-16 19:10:31 +02:00
|
|
|
ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool
|
2022-09-01 20:54:47 +02:00
|
|
|
ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool
|
2022-08-16 19:10:31 +02:00
|
|
|
GetAidlLibraryLabel(ctx BazelConversionPathContext) string
|
2022-09-01 20:54:47 +02:00
|
|
|
GetProtoLibraryLabel(ctx BazelConversionPathContext) string
|
2022-08-16 19:10:31 +02:00
|
|
|
}
|
|
|
|
|
2022-08-03 03:23:58 +02:00
|
|
|
type BazelConversionStatus struct {
|
|
|
|
// Information about _all_ bp2build targets generated by this module. Multiple targets are
|
|
|
|
// supported as Soong handles some things within a single target that we may choose to split into
|
|
|
|
// multiple targets, e.g. renderscript, protos, yacc within a cc module.
|
|
|
|
Bp2buildInfo []bp2buildInfo `blueprint:"mutated"`
|
|
|
|
|
|
|
|
// UnconvertedBp2buildDep stores the module names of direct dependency that were not converted to
|
|
|
|
// Bazel
|
|
|
|
UnconvertedDeps []string `blueprint:"mutated"`
|
|
|
|
|
|
|
|
// MissingBp2buildDep stores the module names of direct dependency that were not found
|
|
|
|
MissingDeps []string `blueprint:"mutated"`
|
|
|
|
}
|
|
|
|
|
2021-11-19 15:29:43 +01:00
|
|
|
type bazelModuleProperties struct {
|
|
|
|
// The label of the Bazel target replacing this Soong module. When run in conversion mode, this
|
|
|
|
// will import the handcrafted build target into the autogenerated file. Note: this may result in
|
|
|
|
// a conflict due to duplicate targets if bp2build_available is also set.
|
|
|
|
Label *string
|
|
|
|
|
|
|
|
// If true, bp2build will generate the converted Bazel target for this module. Note: this may
|
|
|
|
// cause a conflict due to the duplicate targets if label is also set.
|
|
|
|
//
|
|
|
|
// This is a bool pointer to support tristates: true, false, not set.
|
|
|
|
//
|
2022-12-30 02:11:49 +01:00
|
|
|
// To opt in a module, set bazel_module: { bp2build_available: true }
|
|
|
|
// To opt out a module, set bazel_module: { bp2build_available: false }
|
2021-11-19 15:29:43 +01:00
|
|
|
// To defer the default setting for the directory, do not set the value.
|
|
|
|
Bp2build_available *bool
|
2021-11-01 20:32:43 +01:00
|
|
|
|
|
|
|
// CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to
|
|
|
|
// Bazel with Bp2build.
|
|
|
|
CanConvertToBazel bool `blueprint:"mutated"`
|
2021-11-19 15:29:43 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
// Properties contains common module properties for Bazel migration purposes.
|
|
|
|
type properties struct {
|
2022-08-30 19:37:57 +02:00
|
|
|
// In "Bazel mixed build" mode, this represents the Bazel target replacing
|
2021-02-17 19:22:03 +01:00
|
|
|
// this Soong module.
|
2021-11-19 15:29:43 +01:00
|
|
|
Bazel_module bazelModuleProperties
|
2021-02-17 19:22:03 +01:00
|
|
|
}
|
2021-02-17 16:17:28 +01:00
|
|
|
|
2021-11-15 13:28:43 +01:00
|
|
|
// namespacedVariableProperties is a map from a string representing a Soong
|
2021-11-17 11:57:35 +01:00
|
|
|
// config variable namespace, like "android" or "vendor_name" to a slice of
|
|
|
|
// pointer to a struct containing a single field called Soong_config_variables
|
|
|
|
// whose value mirrors the structure in the Blueprint file.
|
|
|
|
type namespacedVariableProperties map[string][]interface{}
|
2021-11-02 17:43:57 +01:00
|
|
|
|
2021-02-17 16:17:28 +01:00
|
|
|
// BazelModuleBase contains the property structs with metadata for modules which can be converted to
|
|
|
|
// Bazel.
|
|
|
|
type BazelModuleBase struct {
|
2021-02-17 19:22:03 +01:00
|
|
|
bazelProperties properties
|
2021-11-02 17:43:57 +01:00
|
|
|
|
|
|
|
// namespacedVariableProperties is used for soong_config_module_type support
|
|
|
|
// in bp2build. Soong config modules allow users to set module properties
|
|
|
|
// based on custom product variables defined in Android.bp files. These
|
|
|
|
// variables are namespaced to prevent clobbering, especially when set from
|
|
|
|
// Makefiles.
|
|
|
|
namespacedVariableProperties namespacedVariableProperties
|
|
|
|
|
|
|
|
// baseModuleType is set when this module was created from a module type
|
|
|
|
// defined by a soong_config_module_type. Every soong_config_module_type
|
|
|
|
// "wraps" another module type, e.g. a soong_config_module_type can wrap a
|
|
|
|
// cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
|
|
|
|
// This baseModuleType is set to the wrapped module type.
|
|
|
|
baseModuleType string
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bazelable is specifies the interface for modules that can be converted to Bazel.
|
|
|
|
type Bazelable interface {
|
2021-02-17 19:22:03 +01:00
|
|
|
bazelProps() *properties
|
|
|
|
HasHandcraftedLabel() bool
|
2021-02-24 22:55:11 +01:00
|
|
|
HandcraftedLabel() string
|
|
|
|
GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
|
2021-11-01 20:32:43 +01:00
|
|
|
ShouldConvertWithBp2build(ctx BazelConversionContext) bool
|
2022-03-28 21:53:03 +02:00
|
|
|
shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool
|
2021-11-01 20:32:43 +01:00
|
|
|
ConvertWithBp2build(ctx TopDownMutatorContext)
|
2021-11-02 17:43:57 +01:00
|
|
|
|
2021-11-17 11:57:35 +01:00
|
|
|
// namespacedVariableProps is a map from a soong config variable namespace
|
|
|
|
// (e.g. acme, android) to a map of interfaces{}, which are really
|
|
|
|
// reflect.Struct pointers, representing the value of the
|
|
|
|
// soong_config_variables property of a module. The struct pointer is the
|
|
|
|
// one with the single member called Soong_config_variables, which itself is
|
|
|
|
// a struct containing fields for each supported feature in that namespace.
|
|
|
|
//
|
2022-12-30 02:11:49 +01:00
|
|
|
// The reason for using a slice of interface{} is to support defaults
|
2021-11-17 11:57:35 +01:00
|
|
|
// propagation of the struct pointers.
|
2021-11-02 17:43:57 +01:00
|
|
|
namespacedVariableProps() namespacedVariableProperties
|
|
|
|
setNamespacedVariableProps(props namespacedVariableProperties)
|
|
|
|
BaseModuleType() string
|
2021-11-17 11:57:35 +01:00
|
|
|
SetBaseModuleType(baseModuleType string)
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 22:43:08 +02:00
|
|
|
// ApiProvider is implemented by modules that contribute to an API surface
|
|
|
|
type ApiProvider interface {
|
|
|
|
ConvertWithApiBp2build(ctx TopDownMutatorContext)
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:50:12 +02:00
|
|
|
// MixedBuildBuildable is an interface that module types should implement in order
|
|
|
|
// to be "handled by Bazel" in a mixed build.
|
|
|
|
type MixedBuildBuildable interface {
|
|
|
|
// IsMixedBuildSupported returns true if and only if this module should be
|
|
|
|
// "handled by Bazel" in a mixed build.
|
|
|
|
// This "escape hatch" allows modules with corner-case scenarios to opt out
|
|
|
|
// of being built with Bazel.
|
|
|
|
IsMixedBuildSupported(ctx BaseModuleContext) bool
|
|
|
|
|
|
|
|
// QueueBazelCall invokes request-queueing functions on the BazelContext
|
|
|
|
// so that these requests are handled when Bazel's cquery is invoked.
|
|
|
|
QueueBazelCall(ctx BaseModuleContext)
|
|
|
|
|
|
|
|
// ProcessBazelQueryResponse uses Bazel information (obtained from the BazelContext)
|
|
|
|
// to set module fields and providers to propagate this module's metadata upstream.
|
|
|
|
// This effectively "bridges the gap" between Bazel and Soong in a mixed build.
|
|
|
|
// Soong modules depending on this module should be oblivious to the fact that
|
|
|
|
// this module was handled by Bazel.
|
|
|
|
ProcessBazelQueryResponse(ctx ModuleContext)
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:17:28 +01:00
|
|
|
// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
|
|
|
|
type BazelModule interface {
|
|
|
|
Module
|
|
|
|
Bazelable
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
|
|
|
|
// properties.
|
|
|
|
func InitBazelModule(module BazelModule) {
|
|
|
|
module.AddProperties(module.bazelProps())
|
2021-11-01 20:32:43 +01:00
|
|
|
module.bazelProps().Bazel_module.CanConvertToBazel = true
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// bazelProps returns the Bazel properties for the given BazelModuleBase.
|
2021-02-17 19:22:03 +01:00
|
|
|
func (b *BazelModuleBase) bazelProps() *properties {
|
2021-02-17 16:17:28 +01:00
|
|
|
return &b.bazelProperties
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:43:57 +01:00
|
|
|
func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
|
|
|
|
return b.namespacedVariableProperties
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
|
|
|
|
b.namespacedVariableProperties = props
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BazelModuleBase) BaseModuleType() string {
|
|
|
|
return b.baseModuleType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
|
|
|
|
b.baseModuleType = baseModuleType
|
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
|
|
|
|
func (b *BazelModuleBase) HasHandcraftedLabel() bool {
|
|
|
|
return b.bazelProperties.Bazel_module.Label != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
|
|
|
|
func (b *BazelModuleBase) HandcraftedLabel() string {
|
|
|
|
return proptools.String(b.bazelProperties.Bazel_module.Label)
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:17:28 +01:00
|
|
|
// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
|
2021-02-24 22:55:11 +01:00
|
|
|
func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
|
|
|
|
if b.HasHandcraftedLabel() {
|
|
|
|
return b.HandcraftedLabel()
|
|
|
|
}
|
2021-11-01 20:32:43 +01:00
|
|
|
if b.ShouldConvertWithBp2build(ctx) {
|
2021-02-24 22:55:11 +01:00
|
|
|
return bp2buildModuleLabel(ctx, module)
|
|
|
|
}
|
|
|
|
return "" // no label for unconverted module
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
|
|
|
|
2022-08-24 00:29:05 +02:00
|
|
|
type Bp2BuildConversionAllowlist struct {
|
2022-03-28 21:53:03 +02:00
|
|
|
// Configure modules in these directories to enable bp2build_available: true or false by default.
|
|
|
|
defaultConfig allowlists.Bp2BuildConfig
|
2021-03-10 08:05:59 +01:00
|
|
|
|
2021-05-13 03:20:13 +02:00
|
|
|
// Keep any existing BUILD files (and do not generate new BUILD files) for these directories
|
2021-07-26 06:45:48 +02:00
|
|
|
// in the synthetic Bazel workspace.
|
2022-03-28 21:53:03 +02:00
|
|
|
keepExistingBuildFile map[string]bool
|
2021-03-25 10:28:38 +01:00
|
|
|
|
2022-08-19 04:04:11 +02:00
|
|
|
// Per-module allowlist to always opt modules into both bp2build and Bazel Dev Mode mixed
|
|
|
|
// builds. These modules are usually in directories with many other modules that are not ready
|
|
|
|
// for conversion.
|
2022-03-04 08:01:29 +01:00
|
|
|
//
|
|
|
|
// A module can either be in this list or its directory allowlisted entirely
|
|
|
|
// in bp2buildDefaultConfig, but not both at the same time.
|
2022-03-28 21:53:03 +02:00
|
|
|
moduleAlwaysConvert map[string]bool
|
2022-02-22 19:07:55 +01:00
|
|
|
|
2022-08-19 04:04:11 +02:00
|
|
|
// Per-module-type allowlist to always opt modules in to both bp2build and
|
|
|
|
// Bazel Dev Mode mixed builds when they have the same type as one listed.
|
2022-03-28 21:53:03 +02:00
|
|
|
moduleTypeAlwaysConvert map[string]bool
|
2022-03-07 20:12:42 +01:00
|
|
|
|
2022-08-20 20:48:32 +02:00
|
|
|
// Per-module denylist to always opt modules out of bp2build conversion.
|
2022-03-28 21:53:03 +02:00
|
|
|
moduleDoNotConvert map[string]bool
|
|
|
|
}
|
2021-03-10 08:05:59 +01:00
|
|
|
|
2022-08-24 00:29:05 +02:00
|
|
|
// NewBp2BuildAllowlist creates a new, empty Bp2BuildConversionAllowlist
|
2022-03-28 21:53:03 +02:00
|
|
|
// which can be populated using builder pattern Set* methods
|
2022-08-24 00:29:05 +02:00
|
|
|
func NewBp2BuildAllowlist() Bp2BuildConversionAllowlist {
|
|
|
|
return Bp2BuildConversionAllowlist{
|
2022-03-28 21:53:03 +02:00
|
|
|
allowlists.Bp2BuildConfig{},
|
|
|
|
map[string]bool{},
|
|
|
|
map[string]bool{},
|
|
|
|
map[string]bool{},
|
|
|
|
map[string]bool{},
|
2022-02-22 19:07:55 +01:00
|
|
|
}
|
2022-03-28 21:53:03 +02:00
|
|
|
}
|
2022-02-22 19:07:55 +01:00
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
// SetDefaultConfig copies the entries from defaultConfig into the allowlist
|
2022-08-24 00:29:05 +02:00
|
|
|
func (a Bp2BuildConversionAllowlist) SetDefaultConfig(defaultConfig allowlists.Bp2BuildConfig) Bp2BuildConversionAllowlist {
|
2022-03-28 21:53:03 +02:00
|
|
|
if a.defaultConfig == nil {
|
|
|
|
a.defaultConfig = allowlists.Bp2BuildConfig{}
|
2022-03-07 20:12:42 +01:00
|
|
|
}
|
2022-03-28 21:53:03 +02:00
|
|
|
for k, v := range defaultConfig {
|
|
|
|
a.defaultConfig[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
2022-03-07 20:12:42 +01:00
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
// SetKeepExistingBuildFile copies the entries from keepExistingBuildFile into the allowlist
|
2022-08-24 00:29:05 +02:00
|
|
|
func (a Bp2BuildConversionAllowlist) SetKeepExistingBuildFile(keepExistingBuildFile map[string]bool) Bp2BuildConversionAllowlist {
|
2022-03-28 21:53:03 +02:00
|
|
|
if a.keepExistingBuildFile == nil {
|
|
|
|
a.keepExistingBuildFile = map[string]bool{}
|
2021-03-30 16:13:16 +02:00
|
|
|
}
|
2022-03-28 21:53:03 +02:00
|
|
|
for k, v := range keepExistingBuildFile {
|
|
|
|
a.keepExistingBuildFile[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
2021-04-15 23:27:08 +02:00
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
// SetModuleAlwaysConvertList copies the entries from moduleAlwaysConvert into the allowlist
|
2022-08-24 00:29:05 +02:00
|
|
|
func (a Bp2BuildConversionAllowlist) SetModuleAlwaysConvertList(moduleAlwaysConvert []string) Bp2BuildConversionAllowlist {
|
2022-03-28 21:53:03 +02:00
|
|
|
if a.moduleAlwaysConvert == nil {
|
|
|
|
a.moduleAlwaysConvert = map[string]bool{}
|
2021-05-03 11:15:48 +02:00
|
|
|
}
|
2022-03-28 21:53:03 +02:00
|
|
|
for _, m := range moduleAlwaysConvert {
|
|
|
|
a.moduleAlwaysConvert[m] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
2021-05-03 11:15:48 +02:00
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
// SetModuleTypeAlwaysConvertList copies the entries from moduleTypeAlwaysConvert into the allowlist
|
2022-08-24 00:29:05 +02:00
|
|
|
func (a Bp2BuildConversionAllowlist) SetModuleTypeAlwaysConvertList(moduleTypeAlwaysConvert []string) Bp2BuildConversionAllowlist {
|
2022-03-28 21:53:03 +02:00
|
|
|
if a.moduleTypeAlwaysConvert == nil {
|
|
|
|
a.moduleTypeAlwaysConvert = map[string]bool{}
|
2021-04-15 23:27:08 +02:00
|
|
|
}
|
2022-03-28 21:53:03 +02:00
|
|
|
for _, m := range moduleTypeAlwaysConvert {
|
|
|
|
a.moduleTypeAlwaysConvert[m] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
2021-04-15 23:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
// SetModuleDoNotConvertList copies the entries from moduleDoNotConvert into the allowlist
|
2022-08-24 00:29:05 +02:00
|
|
|
func (a Bp2BuildConversionAllowlist) SetModuleDoNotConvertList(moduleDoNotConvert []string) Bp2BuildConversionAllowlist {
|
2022-03-28 21:53:03 +02:00
|
|
|
if a.moduleDoNotConvert == nil {
|
|
|
|
a.moduleDoNotConvert = map[string]bool{}
|
|
|
|
}
|
|
|
|
for _, m := range moduleDoNotConvert {
|
|
|
|
a.moduleDoNotConvert[m] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldKeepExistingBuildFileForDir returns whether an existing BUILD file should be
|
|
|
|
// added to the build symlink forest based on the current global configuration.
|
2022-08-24 00:29:05 +02:00
|
|
|
func (a Bp2BuildConversionAllowlist) ShouldKeepExistingBuildFileForDir(dir string) bool {
|
|
|
|
if _, ok := a.keepExistingBuildFile[dir]; ok {
|
2021-05-13 03:20:13 +02:00
|
|
|
// Exact dir match
|
2021-04-21 13:10:09 +02:00
|
|
|
return true
|
|
|
|
}
|
2022-11-02 06:03:07 +01:00
|
|
|
var i int
|
2021-05-13 03:20:13 +02:00
|
|
|
// Check if subtree match
|
2022-11-02 06:03:07 +01:00
|
|
|
for {
|
|
|
|
j := strings.Index(dir[i:], "/")
|
|
|
|
if j == -1 {
|
|
|
|
return false //default
|
|
|
|
}
|
|
|
|
prefix := dir[0 : i+j]
|
|
|
|
i = i + j + 1 // skip the "/"
|
|
|
|
if recursive, ok := a.keepExistingBuildFile[prefix]; ok && recursive {
|
|
|
|
return true
|
2021-05-13 03:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 13:10:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 00:29:05 +02:00
|
|
|
var bp2BuildAllowListKey = NewOnceKey("Bp2BuildAllowlist")
|
|
|
|
var bp2buildAllowlist OncePer
|
|
|
|
|
|
|
|
func GetBp2BuildAllowList() Bp2BuildConversionAllowlist {
|
|
|
|
return bp2buildAllowlist.Once(bp2BuildAllowListKey, func() interface{} {
|
|
|
|
return NewBp2BuildAllowlist().SetDefaultConfig(allowlists.Bp2buildDefaultConfig).
|
|
|
|
SetKeepExistingBuildFile(allowlists.Bp2buildKeepExistingBuildFile).
|
|
|
|
SetModuleAlwaysConvertList(allowlists.Bp2buildModuleAlwaysConvertList).
|
|
|
|
SetModuleTypeAlwaysConvertList(allowlists.Bp2buildModuleTypeAlwaysConvertList).
|
2022-12-30 02:11:49 +01:00
|
|
|
SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList)
|
2022-08-24 00:29:05 +02:00
|
|
|
}).(Bp2BuildConversionAllowlist)
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:33:17 +02:00
|
|
|
// MixedBuildsEnabled returns true if a module is ready to be replaced by a
|
|
|
|
// converted or handcrafted Bazel target. As a side effect, calling this
|
|
|
|
// method will also log whether this module is mixed build enabled for
|
|
|
|
// metrics reporting.
|
2022-05-10 19:50:12 +02:00
|
|
|
func MixedBuildsEnabled(ctx BaseModuleContext) bool {
|
2022-12-30 02:11:49 +01:00
|
|
|
module := ctx.Module()
|
|
|
|
mixedBuildEnabled := ctx.Config().IsMixedBuildsEnabled() &&
|
|
|
|
ctx.Os() != Windows && // Windows toolchains are not currently supported.
|
|
|
|
module.Enabled() &&
|
|
|
|
convertedToBazel(ctx, module) &&
|
|
|
|
ctx.Config().BazelContext.IsModuleNameAllowed(module.Name())
|
2022-04-21 20:33:17 +02:00
|
|
|
ctx.Config().LogMixedBuild(ctx, mixedBuildEnabled)
|
|
|
|
return mixedBuildEnabled
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
|
2021-11-02 07:40:51 +01:00
|
|
|
func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
|
2021-08-26 14:37:59 +02:00
|
|
|
b, ok := module.(Bazelable)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-11-01 20:32:43 +01:00
|
|
|
return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
|
2021-08-26 14:37:59 +02:00
|
|
|
}
|
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
// ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build
|
2021-11-01 20:32:43 +01:00
|
|
|
func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool {
|
|
|
|
return b.shouldConvertWithBp2build(ctx, ctx.Module())
|
2021-08-26 14:37:59 +02:00
|
|
|
}
|
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
type bazelOtherModuleContext interface {
|
|
|
|
ModuleErrorf(format string, args ...interface{})
|
|
|
|
Config() Config
|
|
|
|
OtherModuleType(m blueprint.Module) string
|
|
|
|
OtherModuleName(m blueprint.Module) string
|
|
|
|
OtherModuleDir(m blueprint.Module) string
|
|
|
|
}
|
2021-03-25 10:28:38 +01:00
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
func (b *BazelModuleBase) shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool {
|
2021-11-01 20:32:43 +01:00
|
|
|
if !b.bazelProps().Bazel_module.CanConvertToBazel {
|
|
|
|
return false
|
2021-03-10 08:05:59 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 22:43:08 +02:00
|
|
|
// In api_bp2build mode, all soong modules that can provide API contributions should be converted
|
|
|
|
// This is irrespective of its presence/absence in bp2build allowlists
|
|
|
|
if ctx.Config().BuildMode == ApiBp2build {
|
|
|
|
_, providesApis := module.(ApiProvider)
|
|
|
|
return providesApis
|
|
|
|
}
|
|
|
|
|
2022-03-07 20:12:42 +01:00
|
|
|
propValue := b.bazelProperties.Bazel_module.Bp2build_available
|
2021-08-26 14:37:59 +02:00
|
|
|
packagePath := ctx.OtherModuleDir(module)
|
2022-03-28 21:53:03 +02:00
|
|
|
|
2022-03-07 20:12:42 +01:00
|
|
|
// Modules in unit tests which are enabled in the allowlist by type or name
|
|
|
|
// trigger this conditional because unit tests run under the "." package path
|
2022-03-28 21:53:03 +02:00
|
|
|
isTestModule := packagePath == Bp2BuildTopLevel && proptools.BoolDefault(propValue, false)
|
|
|
|
if isTestModule {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleName := module.Name()
|
2022-08-24 00:29:05 +02:00
|
|
|
allowlist := ctx.Config().Bp2buildPackageConfig
|
2022-03-28 21:53:03 +02:00
|
|
|
moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName]
|
|
|
|
moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[ctx.OtherModuleType(module)]
|
|
|
|
allowlistConvert := moduleNameAllowed || moduleTypeAllowed
|
|
|
|
if moduleNameAllowed && moduleTypeAllowed {
|
|
|
|
ctx.ModuleErrorf("A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if allowlist.moduleDoNotConvert[moduleName] {
|
2022-03-07 20:12:42 +01:00
|
|
|
if moduleNameAllowed {
|
2022-03-28 21:53:03 +02:00
|
|
|
ctx.ModuleErrorf("a module cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert")
|
2022-03-07 20:12:42 +01:00
|
|
|
}
|
2022-02-25 22:34:51 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:05:59 +01:00
|
|
|
// This is a tristate value: true, false, or unset.
|
2022-03-28 21:53:03 +02:00
|
|
|
if ok, directoryPath := bp2buildDefaultTrueRecursively(packagePath, allowlist.defaultConfig); ok {
|
2022-03-07 20:12:42 +01:00
|
|
|
if moduleNameAllowed {
|
2022-03-28 21:53:03 +02:00
|
|
|
ctx.ModuleErrorf("A module cannot be in a directory marked Bp2BuildDefaultTrue"+
|
2022-09-15 01:05:22 +02:00
|
|
|
" or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: '%s'"+
|
|
|
|
" Module: '%s'", directoryPath, moduleName)
|
2022-03-28 21:53:03 +02:00
|
|
|
return false
|
2022-02-22 19:07:55 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 08:05:59 +01:00
|
|
|
// Allow modules to explicitly opt-out.
|
|
|
|
return proptools.BoolDefault(propValue, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow modules to explicitly opt-in.
|
2022-03-07 20:12:42 +01:00
|
|
|
return proptools.BoolDefault(propValue, allowlistConvert)
|
2021-03-10 08:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
|
|
|
|
// set of package prefixes where all modules must be converted. That is, if the
|
|
|
|
// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
|
|
|
|
// return true.
|
|
|
|
//
|
|
|
|
// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
|
|
|
|
// exactly, this module will return false early.
|
|
|
|
//
|
|
|
|
// This function will also return false if the package doesn't match anything in
|
|
|
|
// the config.
|
2022-03-28 21:53:03 +02:00
|
|
|
//
|
|
|
|
// This function will also return the allowlist entry which caused a particular
|
|
|
|
// package to be enabled. Since packages can be enabled via a recursive declaration,
|
|
|
|
// the path returned will not always be the same as the one provided.
|
|
|
|
func bp2buildDefaultTrueRecursively(packagePath string, config allowlists.Bp2BuildConfig) (bool, string) {
|
2021-08-31 07:58:01 +02:00
|
|
|
// Check if the package path has an exact match in the config.
|
2022-03-28 21:53:03 +02:00
|
|
|
if config[packagePath] == allowlists.Bp2BuildDefaultTrue || config[packagePath] == allowlists.Bp2BuildDefaultTrueRecursively {
|
|
|
|
return true, packagePath
|
2022-10-13 19:50:17 +02:00
|
|
|
} else if config[packagePath] == allowlists.Bp2BuildDefaultFalse || config[packagePath] == allowlists.Bp2BuildDefaultFalseRecursively {
|
2022-03-28 21:53:03 +02:00
|
|
|
return false, packagePath
|
2021-03-10 08:05:59 +01:00
|
|
|
}
|
|
|
|
|
Add os/target configurable selects for label list attributes.
This CL is pretty large, so I recommend starting with reading the newly
added tests for the expected behavior.
This change works in conjunction with the linked CLs in the Gerrit topic.
Those CLs add support for new platform() definitions for OS targets
specified in Soong's arch.go, which are configurable through
Android.bp's `target {}` property. It works similary to previous CLs
adding support for the `arch {}` property.
These configurable props are keyed by the OS: android, linux_bionic,
windows, and so on. They map to `select` statements in label list
attributes, which this CL enables for cc_library_headers' header_libs
and export_header_lib_headers props.
This enables //bionic/libc:libc_headers to be generated correctly, from:
cc_library_headers {
name: "libc_headers",
target: {
android: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
linux_bionic: {
header_libs: ["libc_headers_arch"],
export_header_lib_headers: ["libc_headers_arch"],
},
},
// omitted props
}
to:
cc_library_headers(
name = "libc_headers",
deps = [] + select({
"//build/bazel/platforms/os:android": [
":libc_headers_arch",
],
"//build/bazel/platforms/os:linux_bionic": [
":libc_headers_arch",
],
"//conditions:default": [],
}),
)
Test: TH
Test: Verify generated //bionic/libc:libc_headers
Fixes: 183597786
Change-Id: I01016cc2cc9a71449f02300d747f01decebf3f6e
2021-03-24 07:18:33 +01:00
|
|
|
// If not, check for the config recursively.
|
2022-10-13 19:50:17 +02:00
|
|
|
packagePrefix := packagePath
|
|
|
|
|
|
|
|
// e.g. for x/y/z, iterate over x/y, then x, taking the most-specific value from the allowlist.
|
|
|
|
for strings.Contains(packagePrefix, "/") {
|
|
|
|
dirIndex := strings.LastIndex(packagePrefix, "/")
|
|
|
|
packagePrefix = packagePrefix[:dirIndex]
|
|
|
|
switch value := config[packagePrefix]; value {
|
|
|
|
case allowlists.Bp2BuildDefaultTrueRecursively:
|
2021-03-10 08:05:59 +01:00
|
|
|
// package contains this prefix and this prefix should convert all modules
|
2022-03-28 21:53:03 +02:00
|
|
|
return true, packagePrefix
|
2022-10-13 19:50:17 +02:00
|
|
|
case allowlists.Bp2BuildDefaultFalseRecursively:
|
|
|
|
//package contains this prefix and this prefix should NOT convert any modules
|
|
|
|
return false, packagePrefix
|
2021-03-10 08:05:59 +01:00
|
|
|
}
|
|
|
|
// Continue to the next part of the package dir.
|
2022-10-13 19:50:17 +02:00
|
|
|
|
2021-03-10 08:05:59 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 21:53:03 +02:00
|
|
|
return false, packagePath
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
2021-02-17 19:22:03 +01:00
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
|
|
|
|
ctx.TopDown("bp2build_conversion", convertWithBp2build).Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertWithBp2build(ctx TopDownMutatorContext) {
|
|
|
|
bModule, ok := ctx.Module().(Bazelable)
|
|
|
|
if !ok || !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bModule.ConvertWithBp2build(ctx)
|
|
|
|
}
|
2021-12-10 12:14:59 +01:00
|
|
|
|
2022-09-28 22:43:08 +02:00
|
|
|
func registerApiBp2buildConversionMutator(ctx RegisterMutatorsContext) {
|
|
|
|
ctx.TopDown("apiBp2build_conversion", convertWithApiBp2build).Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate API contribution targets if the Soong module provides APIs
|
|
|
|
func convertWithApiBp2build(ctx TopDownMutatorContext) {
|
|
|
|
if m, ok := ctx.Module().(ApiProvider); ok {
|
|
|
|
m.ConvertWithApiBp2build(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 12:14:59 +01:00
|
|
|
// GetMainClassInManifest scans the manifest file specified in filepath and returns
|
|
|
|
// the value of attribute Main-Class in the manifest file if it exists, or returns error.
|
|
|
|
// WARNING: this is for bp2build converters of java_* modules only.
|
|
|
|
func GetMainClassInManifest(c Config, filepath string) (string, error) {
|
|
|
|
file, err := c.fs.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-02-07 16:17:35 +01:00
|
|
|
defer file.Close()
|
2021-12-10 12:14:59 +01:00
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "Main-Class:") {
|
|
|
|
return strings.TrimSpace(line[len("Main-Class:"):]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("Main-Class is not found.")
|
|
|
|
}
|
2023-02-04 00:12:15 +01:00
|
|
|
|
|
|
|
func AttachValidationActions(ctx ModuleContext, outputFilePath Path, validations Paths) ModuleOutPath {
|
|
|
|
validatedOutputFilePath := PathForModuleOut(ctx, "validated", outputFilePath.Base())
|
|
|
|
ctx.Build(pctx, BuildParams{
|
|
|
|
Rule: CpNoPreserveSymlink,
|
|
|
|
Description: "run validations " + outputFilePath.Base(),
|
|
|
|
Output: validatedOutputFilePath,
|
|
|
|
Input: outputFilePath,
|
|
|
|
Validations: validations,
|
|
|
|
})
|
|
|
|
return validatedOutputFilePath
|
|
|
|
}
|