Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b68039ca authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add team property to all modules." into udc-mainline-prod

parents 74965459 57f23498
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ bootstrap_go_package {
        "blueprint-metrics",
        "sbox_proto",
        "soong",
        "soong-android_team_proto",
        "soong-android-soongconfig",
        "soong-bazel",
        "soong-cquery",
@@ -28,6 +29,7 @@ bootstrap_go_package {
        "androidmk-parser",
    ],
    srcs: [
        "all_teams.go",
        "androidmk.go",
        "apex.go",
        "api_domain.go",
@@ -88,6 +90,7 @@ bootstrap_go_package {
        "singleton.go",
        "singleton_module.go",
        "soong_config_modules.go",
        "team.go",
        "test_asserts.go",
        "test_suites.go",
        "testing.go",

android/all_teams.go

0 → 100644
+151 −0
Original line number Diff line number Diff line
package android

import (
	"android/soong/android/team_proto"
	"path/filepath"

	"github.com/google/blueprint/proptools"
	"google.golang.org/protobuf/encoding/prototext"
	"google.golang.org/protobuf/proto"
)

const ownershipDirectory = "ownership"
const allTeamsFile = "all_teams.pb"

func AllTeamsFactory() Singleton {
	return &allTeamsSingleton{}
}

func init() {
	registerAllTeamBuildComponents(InitRegistrationContext)
}

func registerAllTeamBuildComponents(ctx RegistrationContext) {
	ctx.RegisterSingletonType("all_teams", AllTeamsFactory)
}

// For each module, list the team or the bpFile the module is defined in.
type moduleTeamInfo struct {
	teamName string
	bpFile   string
}

type allTeamsSingleton struct {
	// Path where the collected metadata is stored after successful validation.
	outputPath OutputPath

	// Map of all package modules we visit during GenerateBuildActions
	packages map[string]packageProperties
	// Map of all team modules we visit during GenerateBuildActions
	teams map[string]teamProperties
	// Keeps track of team information or bp file for each module we visit.
	teams_for_mods map[string]moduleTeamInfo
}

// See if there is a package module for the given bpFilePath with a team defined, if so return the team.
// If not ascend up to the parent directory and do the same.
func (this *allTeamsSingleton) lookupDefaultTeam(bpFilePath string) (teamProperties, bool) {
	// return the Default_team listed in the package if is there.
	if p, ok := this.packages[bpFilePath]; ok {
		if t := p.Default_team; t != nil {
			return this.teams[*p.Default_team], true
		}
	}
	// Strip a directory and go up.
	// Does android/paths.go basePath,SourcePath help?
	current, base := filepath.Split(bpFilePath)
	current = filepath.Clean(current) // removes trailing slash, convert "" -> "."
	parent, _ := filepath.Split(current)
	if current == "." {
		return teamProperties{}, false
	}
	return this.lookupDefaultTeam(filepath.Join(parent, base))
}

// Create a rule to run a tool to collect all the intermediate files
// which list the team per module into one proto file.
func (this *allTeamsSingleton) GenerateBuildActions(ctx SingletonContext) {
	this.packages = make(map[string]packageProperties)
	this.teams = make(map[string]teamProperties)
	this.teams_for_mods = make(map[string]moduleTeamInfo)

	ctx.VisitAllModules(func(module Module) {
		if !module.Enabled() {
			return
		}

		bpFile := ctx.BlueprintFile(module)

		// Package Modules and Team Modules are stored in a map so we can look them up by name for
		// modules without a team.
		if pack, ok := module.(*packageModule); ok {
			// Packages don't have names, use the blueprint file as the key. we can't get qualifiedModuleId in this context.
			pkgKey := bpFile
			this.packages[pkgKey] = pack.properties
			return
		}
		if team, ok := module.(*teamModule); ok {
			this.teams[team.Name()] = team.properties
			return
		}

		// If a team name is given for a module, store it.
		// Otherwise store the bpFile so we can do a package walk later.
		if module.base().Team() != "" {
			this.teams_for_mods[module.Name()] = moduleTeamInfo{teamName: module.base().Team(), bpFile: bpFile}
		} else {
			this.teams_for_mods[module.Name()] = moduleTeamInfo{bpFile: bpFile}
		}
	})

	// Visit all modules again and lookup the team name in the package or parent package if the team
	// isn't assignged at the module level.
	allTeams := this.lookupTeamForAllModules()

	this.outputPath = PathForOutput(ctx, ownershipDirectory, allTeamsFile)
	// udc branch diff, use textproto and encode it.
	data, err := prototext.Marshal(allTeams)
	if err != nil {
		ctx.Errorf("Unable to marshal team data. %s", err)
	}

	WriteFileRuleVerbatim(ctx, this.outputPath, proptools.NinjaEscape(string(data)))
	ctx.Phony("all_teams", this.outputPath)
}

func (this *allTeamsSingleton) MakeVars(ctx MakeVarsContext) {
	ctx.DistForGoal("all_teams", this.outputPath)
}

// Visit every (non-package, non-team) module and write out a proto containing
// either the declared team data for that module or the package default team data for that module.
func (this *allTeamsSingleton) lookupTeamForAllModules() *team_proto.AllTeams {
	teamsProto := make([]*team_proto.Team, len(this.teams_for_mods))
	i := 0
	for moduleName, m := range this.teams_for_mods {
		teamName := m.teamName
		var teamProperties teamProperties
		found := false
		if teamName != "" {
			teamProperties, found = this.teams[teamName]
		} else {
			teamProperties, found = this.lookupDefaultTeam(m.bpFile)
		}

		// udc branch diff, only write modules with teams to keep ninja file smaller.
		if found {
			trendy_team_id := *teamProperties.Trendy_team_id
			var files []string
			teamData := new(team_proto.Team)
			*teamData = team_proto.Team{
				TrendyTeamId: proto.String(trendy_team_id),
				TargetName:   proto.String(moduleName),
				Path:         proto.String(m.bpFile),
				File:         files,
			}
			teamsProto[i] = teamData
			i++
		}
	}
	return &team_proto.AllTeams{Teams: teamsProto[:i]}
}
+208 −0
Original line number Diff line number Diff line
// Copyright 2024 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android

import (
	"android/soong/android/team_proto"
	"log"
	"testing"

	"google.golang.org/protobuf/encoding/prototext"
	"google.golang.org/protobuf/proto"
)

func TestAllTeams(t *testing.T) {
	t.Parallel()
	ctx := GroupFixturePreparers(
		PrepareForTestWithTeamBuildComponents,
		FixtureRegisterWithContext(func(ctx RegistrationContext) {
			ctx.RegisterModuleType("fake", fakeModuleFactory)
			ctx.RegisterSingletonType("all_teams", AllTeamsFactory)
		}),
	).RunTestWithBp(t, `
		fake {
			name: "main_test",
			team: "someteam",
		}
		team {
			name: "someteam",
			trendy_team_id: "cool_team",
		}

		team {
			name: "team2",
			trendy_team_id: "22222",
		}

		fake {
			name: "tool",
			team: "team2",
		}

		fake {
			name: "noteam",
		}
	`)

	var teams *team_proto.AllTeams
	teams = getTeamProtoOutput(t, ctx)

	// map of module name -> trendy team name.
	actualTeams := make(map[string]*string)
	for _, teamProto := range teams.Teams {
		actualTeams[teamProto.GetTargetName()] = teamProto.TrendyTeamId
	}
	expectedTeams := map[string]*string{
		"main_test": proto.String("cool_team"),
		"tool":      proto.String("22222"),
	}

	AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams)
}

func getTeamProtoOutput(t *testing.T, ctx *TestResult) *team_proto.AllTeams {
	teams := new(team_proto.AllTeams)
	config := ctx.SingletonForTests("all_teams")
	allOutputs := config.AllOutputs()

	protoPath := allOutputs[0]

	out := config.MaybeOutput(protoPath)
	outProto := []byte(ContentFromFileRuleForTests(t, out))
	// udc diff
	if err := prototext.Unmarshal(outProto, teams); err != nil {
		log.Fatalln("Failed to parse teams proto:", err)
	}
	return teams
}

// Android.bp
//
//	team: team_top
//
// # dir1 has no modules with teams,
// # but has a dir with no Android.bp
// dir1/Android.bp
//
//	module_dir1
//
// # dirs without and Android.bp should be fine.
// dir1/dir2/dir3/Android.bp
//
//	package {}
//	module_dir123
//
// teams_dir/Android.bp
//
//	module_with_team1: team1
//	team1: 111
//
// # team comes from upper package default
// teams_dir/deeper/Android.bp
//
//	module2_with_team1: team1
//
// package_defaults/Android.bp
// package_defaults/pd2/Android.bp
//
//	package{ default_team: team_top}
//	module_pd2   ## should get team_top
//
// package_defaults/pd2/pd3/Android.bp
//
//	module_pd3  ## should get team_top
func TestPackageLookup(t *testing.T) {
	t.Parallel()
	rootBp := `
		team {
			name: "team_top",
			trendy_team_id: "trendy://team_top",
		} `

	dir1Bp := `
		fake {
			name: "module_dir1",
		} `
	dir3Bp := `
                package {}
		fake {
			name: "module_dir123",
		} `
	teamsDirBp := `
		fake {
			name: "module_with_team1",
                        team: "team1"

		}
		team {
			name: "team1",
			trendy_team_id: "111",
		} `
	teamsDirDeeper := `
		fake {
			name: "module2_with_team1",
                        team: "team1"
		} `
	// create an empty one.
	packageDefaultsBp := ""
	packageDefaultspd2 := `
                package { default_team: "team_top"}
		fake {
			name: "modulepd2",
		} `

	packageDefaultspd3 := `
		fake {
			name: "modulepd3",
		}
		fake {
			name: "modulepd3b",
			team: "team1"
		} `

	ctx := GroupFixturePreparers(
		PrepareForTestWithTeamBuildComponents,
		PrepareForTestWithPackageModule,
		FixtureRegisterWithContext(func(ctx RegistrationContext) {
			ctx.RegisterModuleType("fake", fakeModuleFactory)
			ctx.RegisterSingletonType("all_teams", AllTeamsFactory)
		}),
		FixtureAddTextFile("Android.bp", rootBp),
		FixtureAddTextFile("dir1/Android.bp", dir1Bp),
		FixtureAddTextFile("dir1/dir2/dir3/Android.bp", dir3Bp),
		FixtureAddTextFile("teams_dir/Android.bp", teamsDirBp),
		FixtureAddTextFile("teams_dir/deeper/Android.bp", teamsDirDeeper),
		FixtureAddTextFile("package_defaults/Android.bp", packageDefaultsBp),
		FixtureAddTextFile("package_defaults/pd2/Android.bp", packageDefaultspd2),
		FixtureAddTextFile("package_defaults/pd2/pd3/Android.bp", packageDefaultspd3),
	).RunTest(t)

	var teams *team_proto.AllTeams
	teams = getTeamProtoOutput(t, ctx)

	// map of module name -> trendy team name.
	actualTeams := make(map[string]*string)
	for _, teamProto := range teams.Teams {
		actualTeams[teamProto.GetTargetName()] = teamProto.TrendyTeamId
	}
	expectedTeams := map[string]*string{
		"module_with_team1":  proto.String("111"),
		"module2_with_team1": proto.String("111"),
		"modulepd2":          proto.String("trendy://team_top"),
		"modulepd3":          proto.String("trendy://team_top"),
		"modulepd3b":         proto.String("111"),
		// udc diff, no nil teams
	}
	AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams)
}
+20 −0
Original line number Diff line number Diff line
@@ -953,6 +953,10 @@ type commonProperties struct {

	// Bazel conversion status
	BazelConversionStatus BazelConversionStatus `blueprint:"mutated"`

	// The team (defined by the owner/vendor) who owns the property.
	Team *string `android:"path"`

}

// CommonAttributes represents the common Bazel attributes from which properties
@@ -995,6 +999,12 @@ type distProperties struct {
	Dists []Dist `android:"arch_variant"`
}

type TeamDepTagType struct {
	blueprint.BaseDependencyTag
}

var teamDepTag = TeamDepTagType{}

// CommonTestOptions represents the common `test_options` properties in
// Android.bp.
type CommonTestOptions struct {
@@ -1699,6 +1709,12 @@ func (m *ModuleBase) ComponentDepsMutator(BottomUpMutatorContext) {}

func (m *ModuleBase) DepsMutator(BottomUpMutatorContext) {}

func (m *ModuleBase) baseDepsMutator(ctx BottomUpMutatorContext) {
	if m.Team() != "" {
		ctx.AddDependency(ctx.Module(), teamDepTag, m.Team())
	}
}

// AddProperties "registers" the provided props
// each value in props MUST be a pointer to a struct
func (m *ModuleBase) AddProperties(props ...interface{}) {
@@ -2136,6 +2152,10 @@ func (m *ModuleBase) Owner() string {
	return String(m.commonProperties.Owner)
}

func (m *ModuleBase) Team() string {
	return String(m.commonProperties.Team)
}

func (m *ModuleBase) setImageVariation(variant string) {
	m.commonProperties.ImageVariation = variant
}
+1 −0
Original line number Diff line number Diff line
@@ -681,6 +681,7 @@ func componentDepsMutator(ctx BottomUpMutatorContext) {

func depsMutator(ctx BottomUpMutatorContext) {
	if m := ctx.Module(); m.Enabled() {
		m.base().baseDepsMutator(ctx)
		m.DepsMutator(ctx)
	}
}
Loading