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