blob: dd7d2db3384b43796de48ee9418f34fc32090ffa [file] [log] [blame]
Ronald Braunstein73b08ff2023-12-19 10:24:47 -08001package android
2
3import (
4 "android/soong/android/team_proto"
5 "path/filepath"
6
7 "google.golang.org/protobuf/proto"
8)
9
10const ownershipDirectory = "ownership"
11const allTeamsFile = "all_teams.pb"
12
13func AllTeamsFactory() Singleton {
14 return &allTeamsSingleton{}
15}
16
17func init() {
18 registerAllTeamBuildComponents(InitRegistrationContext)
19}
20
21func registerAllTeamBuildComponents(ctx RegistrationContext) {
22 ctx.RegisterParallelSingletonType("all_teams", AllTeamsFactory)
23}
24
25// For each module, list the team or the bpFile the module is defined in.
26type moduleTeamInfo struct {
27 teamName string
28 bpFile string
29}
30
31type allTeamsSingleton struct {
32 // Path where the collected metadata is stored after successful validation.
33 outputPath OutputPath
34
35 // Map of all package modules we visit during GenerateBuildActions
36 packages map[string]packageProperties
37 // Map of all team modules we visit during GenerateBuildActions
38 teams map[string]teamProperties
39 // Keeps track of team information or bp file for each module we visit.
40 teams_for_mods map[string]moduleTeamInfo
41}
42
43// See if there is a package module for the given bpFilePath with a team defined, if so return the team.
44// If not ascend up to the parent directory and do the same.
45func (this *allTeamsSingleton) lookupDefaultTeam(bpFilePath string) (teamProperties, bool) {
46 // return the Default_team listed in the package if is there.
47 if p, ok := this.packages[bpFilePath]; ok {
48 if t := p.Default_team; t != nil {
49 return this.teams[*p.Default_team], true
50 }
51 }
52 // Strip a directory and go up.
53 // Does android/paths.go basePath,SourcePath help?
54 current, base := filepath.Split(bpFilePath)
55 current = filepath.Clean(current) // removes trailing slash, convert "" -> "."
56 parent, _ := filepath.Split(current)
57 if current == "." {
58 return teamProperties{}, false
59 }
60 return this.lookupDefaultTeam(filepath.Join(parent, base))
61}
62
63// Create a rule to run a tool to collect all the intermediate files
64// which list the team per module into one proto file.
65func (this *allTeamsSingleton) GenerateBuildActions(ctx SingletonContext) {
66 this.packages = make(map[string]packageProperties)
67 this.teams = make(map[string]teamProperties)
68 this.teams_for_mods = make(map[string]moduleTeamInfo)
69
70 ctx.VisitAllModules(func(module Module) {
71 if !module.Enabled() {
72 return
73 }
74
75 bpFile := ctx.BlueprintFile(module)
76
77 // Package Modules and Team Modules are stored in a map so we can look them up by name for
78 // modules without a team.
79 if pack, ok := module.(*packageModule); ok {
80 // Packages don't have names, use the blueprint file as the key. we can't get qualifiedModuleId in this context.
81 pkgKey := bpFile
82 this.packages[pkgKey] = pack.properties
83 return
84 }
85 if team, ok := module.(*teamModule); ok {
86 this.teams[team.Name()] = team.properties
87 return
88 }
89
90 // If a team name is given for a module, store it.
91 // Otherwise store the bpFile so we can do a package walk later.
92 if module.base().Team() != "" {
93 this.teams_for_mods[module.Name()] = moduleTeamInfo{teamName: module.base().Team(), bpFile: bpFile}
94 } else {
95 this.teams_for_mods[module.Name()] = moduleTeamInfo{bpFile: bpFile}
96 }
97 })
98
99 // Visit all modules again and lookup the team name in the package or parent package if the team
100 // isn't assignged at the module level.
101 allTeams := this.lookupTeamForAllModules()
102
103 this.outputPath = PathForOutput(ctx, ownershipDirectory, allTeamsFile)
104 data, err := proto.Marshal(allTeams)
105 if err != nil {
106 ctx.Errorf("Unable to marshal team data. %s", err)
107 }
108
109 WriteFileRuleVerbatim(ctx, this.outputPath, string(data))
110 ctx.Phony("all_teams", this.outputPath)
111}
112
113func (this *allTeamsSingleton) MakeVars(ctx MakeVarsContext) {
114 ctx.DistForGoal("all_teams", this.outputPath)
115}
116
117// Visit every (non-package, non-team) module and write out a proto containing
118// either the declared team data for that module or the package default team data for that module.
119func (this *allTeamsSingleton) lookupTeamForAllModules() *team_proto.AllTeams {
120 teamsProto := make([]*team_proto.Team, len(this.teams_for_mods))
Spandan Das2268cd82024-02-05 22:06:54 +0000121 for i, moduleName := range SortedKeys(this.teams_for_mods) {
122 m, _ := this.teams_for_mods[moduleName]
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800123 teamName := m.teamName
124 var teamProperties teamProperties
125 found := false
126 if teamName != "" {
127 teamProperties, found = this.teams[teamName]
128 } else {
129 teamProperties, found = this.lookupDefaultTeam(m.bpFile)
130 }
131
132 trendy_team_id := ""
133 if found {
134 trendy_team_id = *teamProperties.Trendy_team_id
135 }
136
137 var files []string
138 teamData := new(team_proto.Team)
139 if trendy_team_id != "" {
140 *teamData = team_proto.Team{
141 TrendyTeamId: proto.String(trendy_team_id),
142 TargetName: proto.String(moduleName),
143 Path: proto.String(m.bpFile),
144 File: files,
145 }
146 } else {
147 // Clients rely on the TrendyTeamId optional field not being set.
148 *teamData = team_proto.Team{
149 TargetName: proto.String(moduleName),
150 Path: proto.String(m.bpFile),
151 File: files,
152 }
153 }
154 teamsProto[i] = teamData
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800155 }
156 return &team_proto.AllTeams{Teams: teamsProto}
157}