Plumb module tags through to ninja.

Bug: http://b/259130368
Test: built android, saw tags set in perfetto trace
Change-Id: I09618114832b143a10a2f84baccfaf3aa0fd5bd8
This commit is contained in:
Dan Albert 2023-06-21 00:17:22 +00:00
parent 6126fe8067
commit 629a6a3bc3
4 changed files with 47 additions and 4 deletions

View file

@ -139,6 +139,14 @@ type EarlyModuleContext interface {
// Context.RegisterModuleType(). // Context.RegisterModuleType().
ModuleType() string ModuleType() string
// ModuleTags returns the tags for this module that should be passed to
// ninja for analysis. For example:
// [
// "module_name": "libfoo",
// "module_type": "cc_library",
// ]
ModuleTags() map[string]string
// BlueprintsFile returns the name of the blueprint file that contains the definition of this // BlueprintsFile returns the name of the blueprint file that contains the definition of this
// module. // module.
BlueprintsFile() string BlueprintsFile() string
@ -406,6 +414,13 @@ func (d *baseModuleContext) ModuleType() string {
return d.module.typeName return d.module.typeName
} }
func (d *baseModuleContext) ModuleTags() map[string]string {
return map[string]string{
"module_name": d.ModuleName(),
"module_type": d.ModuleType(),
}
}
func (d *baseModuleContext) ContainsProperty(name string) bool { func (d *baseModuleContext) ContainsProperty(name string) bool {
_, ok := d.module.propertyPos[name] _, ok := d.module.propertyPos[name]
return ok return ok
@ -796,7 +811,7 @@ func (m *moduleContext) Rule(pctx PackageContext, name string,
func (m *moduleContext) Build(pctx PackageContext, params BuildParams) { func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
m.scope.ReparentTo(pctx) m.scope.ReparentTo(pctx)
def, err := parseBuildParams(m.scope, &params) def, err := parseBuildParams(m.scope, &params, m.ModuleTags())
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -275,8 +275,25 @@ type buildDef struct {
Optional bool Optional bool
} }
func parseBuildParams(scope scope, params *BuildParams) (*buildDef, func formatTags(tags map[string]string, rule Rule) string {
error) { // Maps in golang do not have a guaranteed iteration order, nor is there an
// ordered map type in the stdlib, but we need to deterministically generate
// the ninja file.
keys := make([]string, 0, len(tags))
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
pairs := make([]string, 0, len(keys))
for _, k := range keys {
pairs = append(pairs, k+"="+tags[k])
}
pairs = append(pairs, "rule_name="+rule.name())
return strings.Join(pairs, ";")
}
func parseBuildParams(scope scope, params *BuildParams,
tags map[string]string) (*buildDef, error) {
comment := params.Comment comment := params.Comment
rule := params.Rule rule := params.Rule
@ -360,6 +377,10 @@ func parseBuildParams(scope scope, params *BuildParams) (*buildDef,
simpleNinjaString(strings.Join(params.SymlinkOutputs, " "))) simpleNinjaString(strings.Join(params.SymlinkOutputs, " ")))
} }
if len(tags) > 0 {
setVariable("tags", simpleNinjaString(formatTags(tags, rule)))
}
argNameScope := rule.scope() argNameScope := rule.scope()
if len(params.Args) > 0 { if len(params.Args) > 0 {

View file

@ -394,6 +394,10 @@ func validateArgName(argName string) error {
return fmt.Errorf("%q contains a '.' character", argName) return fmt.Errorf("%q contains a '.' character", argName)
} }
if argName == "tags" {
return fmt.Errorf("\"tags\" is a reserved argument name")
}
for _, builtin := range builtinRuleArgs { for _, builtin := range builtinRuleArgs {
if argName == builtin { if argName == builtin {
return fmt.Errorf("%q conflicts with Ninja built-in", argName) return fmt.Errorf("%q conflicts with Ninja built-in", argName)

View file

@ -265,7 +265,10 @@ func (s *singletonContext) Rule(pctx PackageContext, name string,
func (s *singletonContext) Build(pctx PackageContext, params BuildParams) { func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
s.scope.ReparentTo(pctx) s.scope.ReparentTo(pctx)
def, err := parseBuildParams(s.scope, &params) def, err := parseBuildParams(s.scope, &params, map[string]string{
"module_name": s.name,
"module_type": "singleton",
})
if err != nil { if err != nil {
panic(err) panic(err)
} }