Allow creation of BazelTargets in a different directory
The current API restricts creation of targets to the directory of the visited soong module. This CL proposes adding a `Dir` property in `CommonAttributes` that can be used to create a bazel target in a specific dir. The use case for this is to dynamically create additional targets for proto_library that are adjacent to .proto files (Bazel poses a strict requirement about proto_library being in the same package as the .proto file, but Soong does not) Usage is restricted to dirs that have an existing Android.bp file. There are some places in bp2build where we use existence of Android.bp/BUILD on filesystem to curate a compatible fully qualified path (e.g. headers). If we use `CommonAttributes.Dir` to arbritraily create BUILD files, then it might render those curated labels incompatible. Test: go test ./bp2build Change-Id: If9446700457eddfb389be9d9bde39087f67daa60
This commit is contained in:
@@ -1021,6 +1021,11 @@ type CommonAttributes struct {
|
|||||||
Applicable_licenses bazel.LabelListAttribute
|
Applicable_licenses bazel.LabelListAttribute
|
||||||
|
|
||||||
Testonly *bool
|
Testonly *bool
|
||||||
|
|
||||||
|
// Dir is neither a Soong nor Bazel target attribute
|
||||||
|
// If set, the bazel target will be created in this directory
|
||||||
|
// If unset, the bazel target will default to be created in the directory of the visited soong module
|
||||||
|
Dir *string
|
||||||
}
|
}
|
||||||
|
|
||||||
// constraintAttributes represents Bazel attributes pertaining to build constraints,
|
// constraintAttributes represents Bazel attributes pertaining to build constraints,
|
||||||
|
@@ -17,6 +17,7 @@ package android
|
|||||||
import (
|
import (
|
||||||
"android/soong/bazel"
|
"android/soong/bazel"
|
||||||
"android/soong/ui/metrics/bp2build_metrics_proto"
|
"android/soong/ui/metrics/bp2build_metrics_proto"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/google/blueprint"
|
"github.com/google/blueprint"
|
||||||
)
|
)
|
||||||
@@ -757,6 +758,27 @@ func (t *topDownMutatorContext) CreateBazelTargetAliasInDir(
|
|||||||
mod.base().addBp2buildInfo(info)
|
mod.base().addBp2buildInfo(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the directory in which the bazel target will be generated
|
||||||
|
// If ca.Dir is not nil, use that
|
||||||
|
// Otherwise default to the directory of the soong module
|
||||||
|
func dirForBazelTargetGeneration(t *topDownMutatorContext, ca *CommonAttributes) string {
|
||||||
|
dir := t.OtherModuleDir(t.Module())
|
||||||
|
if ca.Dir != nil {
|
||||||
|
dir = *ca.Dir
|
||||||
|
// Restrict its use to dirs that contain an Android.bp file.
|
||||||
|
// There are several places in bp2build where we use the existence of Android.bp/BUILD on the filesystem
|
||||||
|
// to curate a compatible label for src files (e.g. headers for cc).
|
||||||
|
// If we arbritrarily create BUILD files, then it might render those curated labels incompatible.
|
||||||
|
if exists, _, _ := t.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists {
|
||||||
|
t.ModuleErrorf("Cannot use ca.Dir to create a BazelTarget in dir: %v since it does not contain an Android.bp file", dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set ca.Dir to nil so that it does not get emitted to the BUILD files
|
||||||
|
ca.Dir = nil
|
||||||
|
}
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
func (t *topDownMutatorContext) CreateBazelConfigSetting(
|
func (t *topDownMutatorContext) CreateBazelConfigSetting(
|
||||||
csa bazel.ConfigSettingAttributes,
|
csa bazel.ConfigSettingAttributes,
|
||||||
ca CommonAttributes,
|
ca CommonAttributes,
|
||||||
@@ -851,7 +873,7 @@ func (t *topDownMutatorContext) createBazelTargetModule(
|
|||||||
constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
|
constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
|
||||||
mod := t.Module()
|
mod := t.Module()
|
||||||
info := bp2buildInfo{
|
info := bp2buildInfo{
|
||||||
Dir: t.OtherModuleDir(mod),
|
Dir: dirForBazelTargetGeneration(t, &commonAttrs),
|
||||||
BazelProps: bazelProps,
|
BazelProps: bazelProps,
|
||||||
CommonAttrs: commonAttrs,
|
CommonAttrs: commonAttrs,
|
||||||
ConstraintAttrs: constraintAttributes,
|
ConstraintAttrs: constraintAttributes,
|
||||||
|
@@ -1946,3 +1946,46 @@ func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
|
|||||||
actual, _ := prettyPrintAttribute(lla, 0)
|
actual, _ := prettyPrintAttribute(lla, 0)
|
||||||
android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
|
android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If CommonAttributes.Dir is set, the bazel target should be created in that dir
|
||||||
|
func TestCreateBazelTargetInDifferentDir(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
bp := `
|
||||||
|
custom {
|
||||||
|
name: "foo",
|
||||||
|
dir: "subdir",
|
||||||
|
}
|
||||||
|
`
|
||||||
|
registerCustomModule := func(ctx android.RegistrationContext) {
|
||||||
|
ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
|
||||||
|
}
|
||||||
|
// Check that foo is not created in root dir
|
||||||
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
||||||
|
Description: "foo is not created in root dir because it sets dir explicitly",
|
||||||
|
Blueprint: bp,
|
||||||
|
Filesystem: map[string]string{
|
||||||
|
"subdir/Android.bp": "",
|
||||||
|
},
|
||||||
|
ExpectedBazelTargets: []string{},
|
||||||
|
})
|
||||||
|
// Check that foo is created in `subdir`
|
||||||
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
||||||
|
Description: "foo is created in `subdir` because it sets dir explicitly",
|
||||||
|
Blueprint: bp,
|
||||||
|
Filesystem: map[string]string{
|
||||||
|
"subdir/Android.bp": "",
|
||||||
|
},
|
||||||
|
Dir: "subdir",
|
||||||
|
ExpectedBazelTargets: []string{
|
||||||
|
MakeBazelTarget("custom", "foo", AttrNameToString{}),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
// Check that we cannot create target in different dir if it is does not an Android.bp
|
||||||
|
RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
|
||||||
|
Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file",
|
||||||
|
Blueprint: bp,
|
||||||
|
Dir: "subdir",
|
||||||
|
ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"),
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
@@ -94,6 +94,7 @@ custom = rule(
|
|||||||
# bazel_module end
|
# bazel_module end
|
||||||
"bool_prop": attr.bool(),
|
"bool_prop": attr.bool(),
|
||||||
"bool_ptr_prop": attr.bool(),
|
"bool_ptr_prop": attr.bool(),
|
||||||
|
"dir": attr.string(),
|
||||||
"embedded_prop": attr.string(),
|
"embedded_prop": attr.string(),
|
||||||
"int64_ptr_prop": attr.int(),
|
"int64_ptr_prop": attr.int(),
|
||||||
# nested_props start
|
# nested_props start
|
||||||
@@ -126,6 +127,7 @@ custom_defaults = rule(
|
|||||||
"arch_paths_exclude": attr.string_list(),
|
"arch_paths_exclude": attr.string_list(),
|
||||||
"bool_prop": attr.bool(),
|
"bool_prop": attr.bool(),
|
||||||
"bool_ptr_prop": attr.bool(),
|
"bool_ptr_prop": attr.bool(),
|
||||||
|
"dir": attr.string(),
|
||||||
"embedded_prop": attr.string(),
|
"embedded_prop": attr.string(),
|
||||||
"int64_ptr_prop": attr.int(),
|
"int64_ptr_prop": attr.int(),
|
||||||
# nested_props start
|
# nested_props start
|
||||||
@@ -158,6 +160,7 @@ custom_test_ = rule(
|
|||||||
"arch_paths_exclude": attr.string_list(),
|
"arch_paths_exclude": attr.string_list(),
|
||||||
"bool_prop": attr.bool(),
|
"bool_prop": attr.bool(),
|
||||||
"bool_ptr_prop": attr.bool(),
|
"bool_ptr_prop": attr.bool(),
|
||||||
|
"dir": attr.string(),
|
||||||
"embedded_prop": attr.string(),
|
"embedded_prop": attr.string(),
|
||||||
"int64_ptr_prop": attr.int(),
|
"int64_ptr_prop": attr.int(),
|
||||||
# nested_props start
|
# nested_props start
|
||||||
|
@@ -336,6 +336,8 @@ type customProps struct {
|
|||||||
Api *string // File describing the APIs of this module
|
Api *string // File describing the APIs of this module
|
||||||
|
|
||||||
Test_config_setting *bool // Used to test generation of config_setting targets
|
Test_config_setting *bool // Used to test generation of config_setting targets
|
||||||
|
|
||||||
|
Dir *string // Dir in which the Bazel Target will be created
|
||||||
}
|
}
|
||||||
|
|
||||||
type customModule struct {
|
type customModule struct {
|
||||||
@@ -461,6 +463,10 @@ type customBazelModuleAttributes struct {
|
|||||||
Api bazel.LabelAttribute
|
Api bazel.LabelAttribute
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *customModule) dir() *string {
|
||||||
|
return m.props.Dir
|
||||||
|
}
|
||||||
|
|
||||||
func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
|
func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
|
||||||
if p := m.props.One_to_many_prop; p != nil && *p {
|
if p := m.props.One_to_many_prop; p != nil && *p {
|
||||||
customBp2buildOneToMany(ctx, m)
|
customBp2buildOneToMany(ctx, m)
|
||||||
@@ -508,7 +514,7 @@ func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
|
|||||||
Rule_class: "custom",
|
Rule_class: "custom",
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
|
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name(), Dir: m.dir()}, attrs)
|
||||||
|
|
||||||
if proptools.Bool(m.props.Test_config_setting) {
|
if proptools.Bool(m.props.Test_config_setting) {
|
||||||
m.createConfigSetting(ctx)
|
m.createConfigSetting(ctx)
|
||||||
|
Reference in New Issue
Block a user