This CL adds support to bp2build for declaring the location of the Starlark rule definition when creating BazelTargetModules. This is needed for non-native rules that needs to be loaded from .bzl files somewhere in the tree. Since load statements are aggregated at the top of the BUILD file, away from the targets that actually use them, this CL also introduces an abstraction to group BazelTargets together and compute their load statements and target string representations separately, allowing load statements to be decoupled and written into a BUILD file before the targets themselves. Test: soong tests Test: TH Test: GENERATE_BAZEL_FILES=true m nothing && build/bazel/scripts/bp2build-sync.sh write && bazel cquery //bionic/... Fixes: 178531760 Test: TH Change-Id: Ie5f793a00006eb024eaef07ddd9fde7aaefc054e
166 lines
4.3 KiB
Go
166 lines
4.3 KiB
Go
package bp2build
|
|
|
|
import (
|
|
"android/soong/android"
|
|
"android/soong/bazel"
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
)
|
|
|
|
type nestedProps struct {
|
|
Nested_prop string
|
|
}
|
|
|
|
type customProps struct {
|
|
Bool_prop bool
|
|
Bool_ptr_prop *bool
|
|
// Ensure that properties tagged `blueprint:mutated` are omitted
|
|
Int_prop int `blueprint:"mutated"`
|
|
Int64_ptr_prop *int64
|
|
String_prop string
|
|
String_ptr_prop *string
|
|
String_list_prop []string
|
|
|
|
Nested_props nestedProps
|
|
Nested_props_ptr *nestedProps
|
|
}
|
|
|
|
type customModule struct {
|
|
android.ModuleBase
|
|
|
|
props customProps
|
|
}
|
|
|
|
// OutputFiles is needed because some instances of this module use dist with a
|
|
// tag property which requires the module implements OutputFileProducer.
|
|
func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
|
|
return android.PathsForTesting("path" + tag), nil
|
|
}
|
|
|
|
func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
// nothing for now.
|
|
}
|
|
|
|
func customModuleFactoryBase() android.Module {
|
|
module := &customModule{}
|
|
module.AddProperties(&module.props)
|
|
return module
|
|
}
|
|
|
|
func customModuleFactory() android.Module {
|
|
m := customModuleFactoryBase()
|
|
android.InitAndroidModule(m)
|
|
return m
|
|
}
|
|
|
|
type testProps struct {
|
|
Test_prop struct {
|
|
Test_string_prop string
|
|
}
|
|
}
|
|
|
|
type customTestModule struct {
|
|
android.ModuleBase
|
|
|
|
props customProps
|
|
test_props testProps
|
|
}
|
|
|
|
func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
// nothing for now.
|
|
}
|
|
|
|
func customTestModuleFactoryBase() android.Module {
|
|
m := &customTestModule{}
|
|
m.AddProperties(&m.props)
|
|
m.AddProperties(&m.test_props)
|
|
return m
|
|
}
|
|
|
|
func customTestModuleFactory() android.Module {
|
|
m := customTestModuleFactoryBase()
|
|
android.InitAndroidModule(m)
|
|
return m
|
|
}
|
|
|
|
type customDefaultsModule struct {
|
|
android.ModuleBase
|
|
android.DefaultsModuleBase
|
|
}
|
|
|
|
func customDefaultsModuleFactoryBase() android.DefaultsModule {
|
|
module := &customDefaultsModule{}
|
|
module.AddProperties(&customProps{})
|
|
return module
|
|
}
|
|
|
|
func customDefaultsModuleFactoryBasic() android.Module {
|
|
return customDefaultsModuleFactoryBase()
|
|
}
|
|
|
|
func customDefaultsModuleFactory() android.Module {
|
|
m := customDefaultsModuleFactoryBase()
|
|
android.InitDefaultsModule(m)
|
|
return m
|
|
}
|
|
|
|
type customBazelModuleAttributes struct {
|
|
Name *string
|
|
String_prop string
|
|
String_list_prop []string
|
|
}
|
|
|
|
type customBazelModule struct {
|
|
android.BazelTargetModuleBase
|
|
customBazelModuleAttributes
|
|
}
|
|
|
|
func customBazelModuleFactory() android.Module {
|
|
module := &customBazelModule{}
|
|
module.AddProperties(&module.customBazelModuleAttributes)
|
|
android.InitBazelTargetModule(module)
|
|
return module
|
|
}
|
|
|
|
func (m *customBazelModule) Name() string { return m.BaseModuleName() }
|
|
func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
|
|
|
|
func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
|
|
if m, ok := ctx.Module().(*customModule); ok {
|
|
name := "__bp2build__" + m.Name()
|
|
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
|
Name: proptools.StringPtr(name),
|
|
String_prop: m.props.String_prop,
|
|
String_list_prop: m.props.String_list_prop,
|
|
}, &bazel.BazelTargetModuleProperties{
|
|
Rule_class: "custom",
|
|
})
|
|
}
|
|
}
|
|
|
|
// A bp2build mutator that uses load statements and creates a 1:M mapping from
|
|
// module to target.
|
|
func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
|
|
if m, ok := ctx.Module().(*customModule); ok {
|
|
baseName := "__bp2build__" + m.Name()
|
|
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
|
Name: proptools.StringPtr(baseName),
|
|
}, &bazel.BazelTargetModuleProperties{
|
|
Rule_class: "my_library",
|
|
Bzl_load_location: "//build/bazel/rules:rules.bzl",
|
|
})
|
|
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
|
Name: proptools.StringPtr(baseName + "_proto_library_deps"),
|
|
}, &bazel.BazelTargetModuleProperties{
|
|
Rule_class: "proto_library",
|
|
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
|
})
|
|
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
|
Name: proptools.StringPtr(baseName + "_my_proto_library_deps"),
|
|
}, &bazel.BazelTargetModuleProperties{
|
|
Rule_class: "my_proto_library",
|
|
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
|
})
|
|
}
|
|
}
|