Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "android/soong/android/team_proto" |
| 5 | "path/filepath" |
| 6 | |
| 7 | "google.golang.org/protobuf/proto" |
| 8 | ) |
| 9 | |
| 10 | const ownershipDirectory = "ownership" |
| 11 | const allTeamsFile = "all_teams.pb" |
| 12 | |
| 13 | func AllTeamsFactory() Singleton { |
| 14 | return &allTeamsSingleton{} |
| 15 | } |
| 16 | |
| 17 | func init() { |
| 18 | registerAllTeamBuildComponents(InitRegistrationContext) |
| 19 | } |
| 20 | |
| 21 | func 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. |
| 26 | type moduleTeamInfo struct { |
| 27 | teamName string |
| 28 | bpFile string |
| 29 | } |
| 30 | |
| 31 | type 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. |
| 45 | func (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. |
| 65 | func (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) { |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 71 | bpFile := ctx.BlueprintFile(module) |
| 72 | |
| 73 | // Package Modules and Team Modules are stored in a map so we can look them up by name for |
| 74 | // modules without a team. |
| 75 | if pack, ok := module.(*packageModule); ok { |
| 76 | // Packages don't have names, use the blueprint file as the key. we can't get qualifiedModuleId in this context. |
| 77 | pkgKey := bpFile |
| 78 | this.packages[pkgKey] = pack.properties |
| 79 | return |
| 80 | } |
| 81 | if team, ok := module.(*teamModule); ok { |
| 82 | this.teams[team.Name()] = team.properties |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | // If a team name is given for a module, store it. |
| 87 | // Otherwise store the bpFile so we can do a package walk later. |
| 88 | if module.base().Team() != "" { |
| 89 | this.teams_for_mods[module.Name()] = moduleTeamInfo{teamName: module.base().Team(), bpFile: bpFile} |
| 90 | } else { |
| 91 | this.teams_for_mods[module.Name()] = moduleTeamInfo{bpFile: bpFile} |
| 92 | } |
| 93 | }) |
| 94 | |
| 95 | // Visit all modules again and lookup the team name in the package or parent package if the team |
| 96 | // isn't assignged at the module level. |
| 97 | allTeams := this.lookupTeamForAllModules() |
| 98 | |
| 99 | this.outputPath = PathForOutput(ctx, ownershipDirectory, allTeamsFile) |
| 100 | data, err := proto.Marshal(allTeams) |
| 101 | if err != nil { |
| 102 | ctx.Errorf("Unable to marshal team data. %s", err) |
| 103 | } |
| 104 | |
| 105 | WriteFileRuleVerbatim(ctx, this.outputPath, string(data)) |
| 106 | ctx.Phony("all_teams", this.outputPath) |
| 107 | } |
| 108 | |
| 109 | func (this *allTeamsSingleton) MakeVars(ctx MakeVarsContext) { |
| 110 | ctx.DistForGoal("all_teams", this.outputPath) |
| 111 | } |
| 112 | |
| 113 | // Visit every (non-package, non-team) module and write out a proto containing |
| 114 | // either the declared team data for that module or the package default team data for that module. |
| 115 | func (this *allTeamsSingleton) lookupTeamForAllModules() *team_proto.AllTeams { |
| 116 | teamsProto := make([]*team_proto.Team, len(this.teams_for_mods)) |
Spandan Das | 2268cd8 | 2024-02-05 22:06:54 +0000 | [diff] [blame] | 117 | for i, moduleName := range SortedKeys(this.teams_for_mods) { |
| 118 | m, _ := this.teams_for_mods[moduleName] |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 119 | teamName := m.teamName |
| 120 | var teamProperties teamProperties |
| 121 | found := false |
| 122 | if teamName != "" { |
| 123 | teamProperties, found = this.teams[teamName] |
| 124 | } else { |
| 125 | teamProperties, found = this.lookupDefaultTeam(m.bpFile) |
| 126 | } |
| 127 | |
| 128 | trendy_team_id := "" |
| 129 | if found { |
| 130 | trendy_team_id = *teamProperties.Trendy_team_id |
| 131 | } |
| 132 | |
| 133 | var files []string |
| 134 | teamData := new(team_proto.Team) |
| 135 | if trendy_team_id != "" { |
| 136 | *teamData = team_proto.Team{ |
| 137 | TrendyTeamId: proto.String(trendy_team_id), |
| 138 | TargetName: proto.String(moduleName), |
| 139 | Path: proto.String(m.bpFile), |
| 140 | File: files, |
| 141 | } |
| 142 | } else { |
| 143 | // Clients rely on the TrendyTeamId optional field not being set. |
| 144 | *teamData = team_proto.Team{ |
| 145 | TargetName: proto.String(moduleName), |
| 146 | Path: proto.String(m.bpFile), |
| 147 | File: files, |
| 148 | } |
| 149 | } |
| 150 | teamsProto[i] = teamData |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 151 | } |
| 152 | return &team_proto.AllTeams{Teams: teamsProto} |
| 153 | } |