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/android/mutator.go b/android/mutator.go
index 2ec051e..6bcac93 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -17,6 +17,7 @@
import (
"android/soong/bazel"
"android/soong/ui/metrics/bp2build_metrics_proto"
+ "path/filepath"
"github.com/google/blueprint"
)
@@ -757,6 +758,27 @@
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(
csa bazel.ConfigSettingAttributes,
ca CommonAttributes,
@@ -851,7 +873,7 @@
constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
mod := t.Module()
info := bp2buildInfo{
- Dir: t.OtherModuleDir(mod),
+ Dir: dirForBazelTargetGeneration(t, &commonAttrs),
BazelProps: bazelProps,
CommonAttrs: commonAttrs,
ConstraintAttrs: constraintAttributes,