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