From 42a49f1278b50b6cd4971f9d40505a278461154e Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Wed, 17 Aug 2022 22:09:55 +0000 Subject: [PATCH] Add current build release Previously, the build releases only included named releases so did not have a way to represent the latest build release. This adds the current build release (named after the current API level) to represent that. Bug: 240406019 Test: m nothing Change-Id: Ib8336da716b447448b23bc9684ce3be1ab78648a --- sdk/build_release.go | 52 ++++++++++++++++++++++++++++++--------- sdk/build_release_test.go | 4 +-- sdk/testing.go | 3 +-- sdk/update.go | 7 +++--- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/sdk/build_release.go b/sdk/build_release.go index 20b33d6aa..ac57a3279 100644 --- a/sdk/build_release.go +++ b/sdk/build_release.go @@ -16,6 +16,7 @@ package sdk import ( "fmt" + "math" "reflect" "strings" ) @@ -29,7 +30,10 @@ type buildRelease struct { // The name of the release, e.g. S, Tiramisu, etc. name string - // The index of this structure within the buildReleases list. + // The index of this structure within the dessertBuildReleases list. + // + // The buildReleaseCurrent does not appear in the dessertBuildReleases list as it has an ordinal value + // that is larger than the size of the dessertBuildReleases. ordinal int } @@ -56,7 +60,7 @@ func (s *buildReleaseSet) addItem(release *buildRelease) { // addRange adds all the build releases from start (inclusive) to end (inclusive). func (s *buildReleaseSet) addRange(start *buildRelease, end *buildRelease) { for i := start.ordinal; i <= end.ordinal; i += 1 { - s.addItem(buildReleases[i]) + s.addItem(dessertBuildReleases[i]) } } @@ -69,11 +73,17 @@ func (s *buildReleaseSet) contains(release *buildRelease) bool { // String returns a string representation of the set, sorted from earliest to latest release. func (s *buildReleaseSet) String() string { list := []string{} - for _, release := range buildReleases { + addRelease := func(release *buildRelease) { if _, ok := s.contents[release]; ok { list = append(list, release.name) } } + // Add the names of the build releases in this set in the order in which they were created. + for _, release := range dessertBuildReleases { + addRelease(release) + } + // Always add "current" to the list of names last if it is present in the set. + addRelease(buildReleaseCurrent) return fmt.Sprintf("[%s]", strings.Join(list, ",")) } @@ -81,30 +91,46 @@ var ( // nameToBuildRelease contains a map from name to build release. nameToBuildRelease = map[string]*buildRelease{} - // buildReleases lists all the available build releases. - buildReleases = []*buildRelease{} + // dessertBuildReleases lists all the available dessert build releases, i.e. excluding current. + dessertBuildReleases = []*buildRelease{} // allBuildReleaseSet is the set of all build releases. allBuildReleaseSet = &buildReleaseSet{contents: map[*buildRelease]struct{}{}} - // Add the build releases from oldest to newest. + // Add the dessert build releases from oldest to newest. buildReleaseS = initBuildRelease("S") buildReleaseT = initBuildRelease("Tiramisu") + + // Add the current build release which is always treated as being more recent than any other + // build release, including those added in tests. + buildReleaseCurrent = initBuildRelease("current") ) // initBuildRelease creates a new build release with the specified name. func initBuildRelease(name string) *buildRelease { - ordinal := len(nameToBuildRelease) + ordinal := len(dessertBuildReleases) + if name == "current" { + // The current build release is more recent than all other build releases, including those + // created in tests so use the max int value. It cannot just rely on being created after all + // the other build releases as some are created in tests which run after the current build + // release has been created. + ordinal = math.MaxInt + } release := &buildRelease{name: name, ordinal: ordinal} nameToBuildRelease[name] = release - buildReleases = append(buildReleases, release) allBuildReleaseSet.addItem(release) + if name != "current" { + // As the current build release has an ordinal value that does not correspond to its position + // in the dessertBuildReleases list do not add it to the list. + dessertBuildReleases = append(dessertBuildReleases, release) + } return release } -// latestBuildRelease returns the latest build release, i.e. the last one added. -func latestBuildRelease() *buildRelease { - return buildReleases[len(buildReleases)-1] +// latestDessertBuildRelease returns the latest dessert release build name, i.e. the last dessert +// release added to the list, which does not include current. +func latestDessertBuildRelease() *buildRelease { + return dessertBuildReleases[len(dessertBuildReleases)-1] } // nameToRelease maps from build release name to the corresponding build release (if it exists) or @@ -134,8 +160,10 @@ func parseBuildReleaseSet(specification string) (*buildReleaseSet, error) { if err != nil { return nil, err } - end := latestBuildRelease() + end := latestDessertBuildRelease() set.addRange(start, end) + // An open-ended range always includes the current release. + set.addItem(buildReleaseCurrent) } else if strings.Contains(specification, "-") { limits := strings.SplitN(specification, "-", 2) start, err := nameToRelease(limits[0]) diff --git a/sdk/build_release_test.go b/sdk/build_release_test.go index 6f1ef9e30..13730cbca 100644 --- a/sdk/build_release_test.go +++ b/sdk/build_release_test.go @@ -42,7 +42,7 @@ func TestNameToRelease(t *testing.T) { android.AssertDeepEquals(t, "release", (*buildRelease)(nil), release) // Uses a wildcard in the error message to allow for additional build releases to be added to // the supported set without breaking this test. - android.FailIfNoMatchingErrors(t, `unknown release "A", expected one of \[S,T.*,F1,F2\]`, []error{err}) + android.FailIfNoMatchingErrors(t, `unknown release "A", expected one of \[S,Tiramisu,F1,F2,current\]`, []error{err}) }) } @@ -55,7 +55,7 @@ func TestParseBuildReleaseSet(t *testing.T) { t.Run("open range", func(t *testing.T) { set, err := parseBuildReleaseSet("F1+") android.AssertDeepEquals(t, "errors", nil, err) - android.AssertStringEquals(t, "set", "[F1,F2]", set.String()) + android.AssertStringEquals(t, "set", "[F1,F2,current]", set.String()) }) t.Run("closed range", func(t *testing.T) { set, err := parseBuildReleaseSet("S-F1") diff --git a/sdk/testing.go b/sdk/testing.go index bed11b27d..f4e2b031b 100644 --- a/sdk/testing.go +++ b/sdk/testing.go @@ -262,8 +262,7 @@ func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir st // If the generated snapshot builders not for the current release then it cannot be loaded by // the current release. - currentBuildRelease := latestBuildRelease() - if snapshotBuildInfo.targetBuildRelease != currentBuildRelease { + if snapshotBuildInfo.targetBuildRelease != buildReleaseCurrent { return } diff --git a/sdk/update.go b/sdk/update.go index c555ddc7a..5c9376b5d 100644 --- a/sdk/update.go +++ b/sdk/update.go @@ -387,12 +387,11 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) { // Always add -current to the end snapshotFileSuffix := "-current" - currentBuildRelease := latestBuildRelease() - targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", currentBuildRelease.name) + targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", buildReleaseCurrent.name) targetBuildRelease, err := nameToRelease(targetBuildReleaseEnv) if err != nil { ctx.ModuleErrorf("invalid SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE: %s", err) - targetBuildRelease = currentBuildRelease + targetBuildRelease = buildReleaseCurrent } builder := &snapshotBuilder{ @@ -472,7 +471,7 @@ be unnecessary as every module in the sdk already has its own licenses property. contents := bp.content.String() // If the snapshot is being generated for the current build release then check the syntax to make // sure that it is compatible. - if targetBuildRelease == currentBuildRelease { + if targetBuildRelease == buildReleaseCurrent { syntaxCheckSnapshotBpFile(ctx, contents) }