Introduce BazelStringOrLabelFromProp.

Soong supports string properties, but they are overloaded, and can mean
one of three things:

* path reference
* module reference
* string literal

Bazel has different types: label and string attributes. Thus there needs
to be a way to categorize them correctly in bp2build.

This CL introduces a new function to be used on properties like
apex_key.private_key / apex_key.public_key, as well as
android_app.certificate / apex.certificate.

It is important to disambiguate the prop betenn a string literal
attribute or file/rule target label attribute, so this functions does
just that.  The new attributes are then further handled by their
respective macros (apex_key, android_binary, apex).

Bug: 253557437
Fixes: 253557437
Test: presubmits, new tests

Change-Id: Id8111cdd60d3aabcae7d17fe9da84d0ee3966023
This commit is contained in:
Jingwen Chen
2022-10-14 09:56:07 +00:00
parent a9e1df1522
commit 6817bbb3c8
6 changed files with 173 additions and 53 deletions

View File

@@ -534,3 +534,55 @@ func PathsForBazelOut(ctx PathContext, paths []string) Paths {
}
return outs
}
// BazelStringOrLabelFromProp splits a Soong module property that can be
// either a string literal, path (with android:path tag) or a module reference
// into separate bazel string or label attributes. Bazel treats string and label
// attributes as distinct types, so this function categorizes a string property
// into either one of them.
//
// e.g. apex.private_key = "foo.pem" can either refer to:
//
// 1. "foo.pem" in the current directory -> file target
// 2. "foo.pem" module -> rule target
// 3. "foo.pem" file in a different directory, prefixed by a product variable handled
// in a bazel macro. -> string literal
//
// For the first two cases, they are defined using the label attribute. For the third case,
// it's defined with the string attribute.
func BazelStringOrLabelFromProp(
ctx TopDownMutatorContext,
propToDistinguish *string) (bazel.LabelAttribute, bazel.StringAttribute) {
var labelAttr bazel.LabelAttribute
var strAttr bazel.StringAttribute
if propToDistinguish == nil {
// nil pointer
return labelAttr, strAttr
}
prop := String(propToDistinguish)
if SrcIsModule(prop) != "" {
// If it's a module (SrcIsModule will return the module name), set the
// resolved label to the label attribute.
labelAttr.SetValue(BazelLabelForModuleDepSingle(ctx, prop))
} else {
// Not a module name. This could be a string literal or a file target in
// the current dir. Check if the path exists:
path := ExistentPathForSource(ctx, ctx.ModuleDir(), prop)
if path.Valid() && parentDir(path.String()) == ctx.ModuleDir() {
// If it exists and the path is relative to the current dir, resolve the bazel label
// for the _file target_ and set it to the label attribute.
//
// Resolution is necessary because this could be a file in a subpackage.
labelAttr.SetValue(BazelLabelForModuleSrcSingle(ctx, prop))
} else {
// Otherwise, treat it as a string literal and assign to the string attribute.
strAttr.Value = propToDistinguish
}
}
return labelAttr, strAttr
}