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:
Spandan Das
2023-08-03 22:33:47 +00:00
parent 2666c93fc1
commit 3131d679f2
5 changed files with 81 additions and 2 deletions

View File

@@ -1946,3 +1946,46 @@ func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
actual, _ := prettyPrintAttribute(lla, 0)
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"),
})
}