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
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 140b214..18ae82d 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -336,6 +336,8 @@
Api *string // File describing the APIs of this module
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 {
@@ -461,6 +463,10 @@
Api bazel.LabelAttribute
}
+func (m *customModule) dir() *string {
+ return m.props.Dir
+}
+
func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
if p := m.props.One_to_many_prop; p != nil && *p {
customBp2buildOneToMany(ctx, m)
@@ -508,7 +514,7 @@
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) {
m.createConfigSetting(ctx)