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. |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 26 | type moduleTeamAndTestInfo struct { |
| 27 | // Name field from bp file for the team |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 28 | teamName string |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 29 | // Blueprint file the module is located in. |
| 30 | bpFile string |
| 31 | // Is this module only used by tests. |
| 32 | testOnly bool |
| 33 | // Is this a directly testable target by running the module directly |
| 34 | // or via tradefed. |
| 35 | topLevelTestTarget bool |
| 36 | // String name indicating the module, like `java_library` for reporting. |
| 37 | kind string |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type allTeamsSingleton struct { |
| 41 | // Path where the collected metadata is stored after successful validation. |
| 42 | outputPath OutputPath |
| 43 | |
| 44 | // Map of all package modules we visit during GenerateBuildActions |
| 45 | packages map[string]packageProperties |
| 46 | // Map of all team modules we visit during GenerateBuildActions |
| 47 | teams map[string]teamProperties |
| 48 | // Keeps track of team information or bp file for each module we visit. |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 49 | teams_for_mods map[string]moduleTeamAndTestInfo |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // See if there is a package module for the given bpFilePath with a team defined, if so return the team. |
| 53 | // If not ascend up to the parent directory and do the same. |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 54 | func (t *allTeamsSingleton) lookupDefaultTeam(bpFilePath string) (teamProperties, bool) { |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 55 | // return the Default_team listed in the package if is there. |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 56 | if p, ok := t.packages[bpFilePath]; ok { |
| 57 | if defaultTeam := p.Default_team; defaultTeam != nil { |
| 58 | return t.teams[*defaultTeam], true |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | // Strip a directory and go up. |
| 62 | // Does android/paths.go basePath,SourcePath help? |
| 63 | current, base := filepath.Split(bpFilePath) |
| 64 | current = filepath.Clean(current) // removes trailing slash, convert "" -> "." |
| 65 | parent, _ := filepath.Split(current) |
| 66 | if current == "." { |
| 67 | return teamProperties{}, false |
| 68 | } |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 69 | return t.lookupDefaultTeam(filepath.Join(parent, base)) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 72 | // Visit all modules and collect all teams and use WriteFileRuleVerbatim |
| 73 | // to write it out. |
| 74 | func (t *allTeamsSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 75 | t.packages = make(map[string]packageProperties) |
| 76 | t.teams = make(map[string]teamProperties) |
| 77 | t.teams_for_mods = make(map[string]moduleTeamAndTestInfo) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 78 | |
| 79 | ctx.VisitAllModules(func(module Module) { |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 80 | bpFile := ctx.BlueprintFile(module) |
| 81 | |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 82 | testModInfo := TestModuleInformation{} |
| 83 | if tmi, ok := SingletonModuleProvider(ctx, module, TestOnlyProviderKey); ok { |
| 84 | testModInfo = tmi |
| 85 | } |
| 86 | |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 87 | // Package Modules and Team Modules are stored in a map so we can look them up by name for |
| 88 | // modules without a team. |
| 89 | if pack, ok := module.(*packageModule); ok { |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 90 | // Packages don't have names, use the blueprint file as the key. we can't get qualifiedModuleId in t context. |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 91 | pkgKey := bpFile |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 92 | t.packages[pkgKey] = pack.properties |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 93 | return |
| 94 | } |
| 95 | if team, ok := module.(*teamModule); ok { |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 96 | t.teams[team.Name()] = team.properties |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 97 | return |
| 98 | } |
| 99 | |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 100 | entry := moduleTeamAndTestInfo{ |
| 101 | bpFile: bpFile, |
| 102 | testOnly: testModInfo.TestOnly, |
| 103 | topLevelTestTarget: testModInfo.TopLevelTarget, |
| 104 | kind: ctx.ModuleType(module), |
| 105 | teamName: module.base().Team(), |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 106 | } |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 107 | t.teams_for_mods[module.Name()] = entry |
| 108 | |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 109 | }) |
| 110 | |
| 111 | // Visit all modules again and lookup the team name in the package or parent package if the team |
| 112 | // isn't assignged at the module level. |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 113 | allTeams := t.lookupTeamForAllModules() |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 114 | |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 115 | t.outputPath = PathForOutput(ctx, ownershipDirectory, allTeamsFile) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 116 | data, err := proto.Marshal(allTeams) |
| 117 | if err != nil { |
| 118 | ctx.Errorf("Unable to marshal team data. %s", err) |
| 119 | } |
| 120 | |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 121 | WriteFileRuleVerbatim(ctx, t.outputPath, string(data)) |
| 122 | ctx.Phony("all_teams", t.outputPath) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 125 | func (t *allTeamsSingleton) MakeVars(ctx MakeVarsContext) { |
| 126 | ctx.DistForGoal("all_teams", t.outputPath) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Visit every (non-package, non-team) module and write out a proto containing |
| 130 | // either the declared team data for that module or the package default team data for that module. |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 131 | func (t *allTeamsSingleton) lookupTeamForAllModules() *team_proto.AllTeams { |
| 132 | teamsProto := make([]*team_proto.Team, len(t.teams_for_mods)) |
| 133 | for i, moduleName := range SortedKeys(t.teams_for_mods) { |
| 134 | m, _ := t.teams_for_mods[moduleName] |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 135 | teamName := m.teamName |
| 136 | var teamProperties teamProperties |
| 137 | found := false |
| 138 | if teamName != "" { |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 139 | teamProperties, found = t.teams[teamName] |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 140 | } else { |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 141 | teamProperties, found = t.lookupDefaultTeam(m.bpFile) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | trendy_team_id := "" |
| 145 | if found { |
| 146 | trendy_team_id = *teamProperties.Trendy_team_id |
| 147 | } |
| 148 | |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 149 | teamData := new(team_proto.Team) |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 150 | *teamData = team_proto.Team{ |
| 151 | TargetName: proto.String(moduleName), |
| 152 | Path: proto.String(m.bpFile), |
| 153 | TestOnly: proto.Bool(m.testOnly), |
| 154 | TopLevelTarget: proto.Bool(m.topLevelTestTarget), |
| 155 | Kind: proto.String(m.kind), |
| 156 | } |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 157 | if trendy_team_id != "" { |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame] | 158 | teamData.TrendyTeamId = proto.String(trendy_team_id) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 159 | } else { |
| 160 | // Clients rely on the TrendyTeamId optional field not being set. |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 161 | } |
| 162 | teamsProto[i] = teamData |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 163 | } |
| 164 | return &team_proto.AllTeams{Teams: teamsProto} |
| 165 | } |