blob: e3c2e70bdfba7d94a186ca458908579dd89b0fa3 [file] [log] [blame]
Ronald Braunstein73b08ff2023-12-19 10:24:47 -08001package android
2
3import (
Ronald Braunstein73b08ff2023-12-19 10:24:47 -08004 "path/filepath"
5
Yu Liu663e4502024-08-12 18:23:59 +00006 "android/soong/android/team_proto"
7
Ronald Braunstein73b08ff2023-12-19 10:24:47 -08008 "google.golang.org/protobuf/proto"
9)
10
11const ownershipDirectory = "ownership"
12const allTeamsFile = "all_teams.pb"
13
14func AllTeamsFactory() Singleton {
15 return &allTeamsSingleton{}
16}
17
18func init() {
19 registerAllTeamBuildComponents(InitRegistrationContext)
20}
21
22func 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 Braunsteinc5603092024-03-27 06:46:47 -070027type moduleTeamAndTestInfo struct {
28 // Name field from bp file for the team
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080029 teamName string
Ronald Braunsteinc5603092024-03-27 06:46:47 -070030 // 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 Braunstein73b08ff2023-12-19 10:24:47 -080039}
40
41type 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 Braunsteinc5603092024-03-27 06:46:47 -070050 teams_for_mods map[string]moduleTeamAndTestInfo
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080051}
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 Braunsteinc5603092024-03-27 06:46:47 -070055func (t *allTeamsSingleton) lookupDefaultTeam(bpFilePath string) (teamProperties, bool) {
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080056 // return the Default_team listed in the package if is there.
Ronald Braunsteinc5603092024-03-27 06:46:47 -070057 if p, ok := t.packages[bpFilePath]; ok {
58 if defaultTeam := p.Default_team; defaultTeam != nil {
59 return t.teams[*defaultTeam], true
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080060 }
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 Braunsteinc5603092024-03-27 06:46:47 -070070 return t.lookupDefaultTeam(filepath.Join(parent, base))
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080071}
72
Ronald Braunsteinc5603092024-03-27 06:46:47 -070073// Visit all modules and collect all teams and use WriteFileRuleVerbatim
74// to write it out.
75func (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 Braunstein73b08ff2023-12-19 10:24:47 -080079
80 ctx.VisitAllModules(func(module Module) {
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080081 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 Braunsteinc5603092024-03-27 06:46:47 -070086 // Packages don't have names, use the blueprint file as the key. we can't get qualifiedModuleId in t context.
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080087 pkgKey := bpFile
Ronald Braunsteinc5603092024-03-27 06:46:47 -070088 t.packages[pkgKey] = pack.properties
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080089 return
90 }
91 if team, ok := module.(*teamModule); ok {
Ronald Braunsteinc5603092024-03-27 06:46:47 -070092 t.teams[team.Name()] = team.properties
Ronald Braunstein73b08ff2023-12-19 10:24:47 -080093 return
94 }
95
Ronald Braunsteinc864b242024-04-16 07:33:52 -070096 testModInfo := TestModuleInformation{}
Yu Liu663e4502024-08-12 18:23:59 +000097 if tmi, ok := OtherModuleProvider(ctx, module, TestOnlyProviderKey); ok {
Ronald Braunsteinc864b242024-04-16 07:33:52 -070098 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 Braunsteinc5603092024-03-27 06:46:47 -0700113 entry := moduleTeamAndTestInfo{
114 bpFile: bpFile,
115 testOnly: testModInfo.TestOnly,
116 topLevelTestTarget: testModInfo.TopLevelTarget,
117 kind: ctx.ModuleType(module),
118 teamName: module.base().Team(),
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800119 }
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700120 t.teams_for_mods[module.Name()] = entry
121
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800122 })
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 Braunsteinc5603092024-03-27 06:46:47 -0700126 allTeams := t.lookupTeamForAllModules()
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800127
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700128 t.outputPath = PathForOutput(ctx, ownershipDirectory, allTeamsFile)
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800129 data, err := proto.Marshal(allTeams)
130 if err != nil {
131 ctx.Errorf("Unable to marshal team data. %s", err)
132 }
133
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700134 WriteFileRuleVerbatim(ctx, t.outputPath, string(data))
135 ctx.Phony("all_teams", t.outputPath)
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800136}
137
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700138func (t *allTeamsSingleton) MakeVars(ctx MakeVarsContext) {
139 ctx.DistForGoal("all_teams", t.outputPath)
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800140}
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 Braunsteinc5603092024-03-27 06:46:47 -0700144func (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 Braunstein73b08ff2023-12-19 10:24:47 -0800148 teamName := m.teamName
149 var teamProperties teamProperties
150 found := false
151 if teamName != "" {
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700152 teamProperties, found = t.teams[teamName]
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800153 } else {
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700154 teamProperties, found = t.lookupDefaultTeam(m.bpFile)
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800155 }
156
157 trendy_team_id := ""
158 if found {
159 trendy_team_id = *teamProperties.Trendy_team_id
160 }
161
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800162 teamData := new(team_proto.Team)
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700163 *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 Braunstein73b08ff2023-12-19 10:24:47 -0800170 if trendy_team_id != "" {
Ronald Braunsteinc5603092024-03-27 06:46:47 -0700171 teamData.TrendyTeamId = proto.String(trendy_team_id)
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800172 } else {
173 // Clients rely on the TrendyTeamId optional field not being set.
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800174 }
175 teamsProto[i] = teamData
Ronald Braunstein73b08ff2023-12-19 10:24:47 -0800176 }
177 return &team_proto.AllTeams{Teams: teamsProto}
178}