Remove global state from version mutator
A per-context variable is used to store the list of modules that
contain stubs and their available versions. Stores the list of the
stubs versions on the implementation module, and then use the new
return values from AddVariationDependencies to expand dependencies
on implementation libraries to also depend on the stubs libraries.
Adds a new mutator pass to propagate list of stub versions to llndk
libraries.
Also creates an alias version variation called "latest" to allow
depending on the latest version without having to know what it is.
Test: all Soong tests
Test: no change to build.ninja, Android-${TARGET_PRODUCT}.mk, make_vars-${TARGET_PRODUCT}.mk or late-${TARGET_PRODUCT}.mk
Change-Id: If19659e2e5828c860fd4d679ef79a414b7ea2efc
diff --git a/cc/library.go b/cc/library.go
index 92853b5..7b09b11 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -152,6 +152,8 @@
BuildStubs bool `blueprint:"mutated"`
// Version of the stubs lib
StubsVersion string `blueprint:"mutated"`
+ // List of all stubs versions associated with an implementation lib
+ AllStubsVersions []string `blueprint:"mutated"`
}
type FlagExporterProperties struct {
@@ -1517,26 +1519,6 @@
}
}
-var stubVersionsKey = android.NewOnceKey("stubVersions")
-
-// maps a module name to the list of stubs versions available for the module
-func stubsVersionsFor(config android.Config) map[string][]string {
- return config.Once(stubVersionsKey, func() interface{} {
- return make(map[string][]string)
- }).(map[string][]string)
-}
-
-var stubsVersionsLock sync.Mutex
-
-func LatestStubsVersionFor(config android.Config, name string) string {
- versions, ok := stubsVersionsFor(config)[name]
- if ok && len(versions) > 0 {
- // the versions are alreay sorted in ascending order
- return versions[len(versions)-1]
- }
- return ""
-}
-
func normalizeVersions(ctx android.BaseModuleContext, versions []string) {
numVersions := make([]int, len(versions))
for i, v := range versions {
@@ -1556,17 +1538,22 @@
}
func createVersionVariations(mctx android.BottomUpMutatorContext, versions []string) {
- // "" is for the non-stubs variant
- versions = append([]string{""}, versions...)
+ // "" is for the non-stubs (implementation) variant.
+ variants := append([]string{""}, versions...)
- modules := mctx.CreateLocalVariations(versions...)
+ modules := mctx.CreateLocalVariations(variants...)
for i, m := range modules {
- if versions[i] != "" {
+ if variants[i] != "" {
m.(LinkableInterface).SetBuildStubs()
- m.(LinkableInterface).SetStubsVersions(versions[i])
+ m.(LinkableInterface).SetStubsVersion(variants[i])
}
}
mctx.AliasVariation("")
+ latestVersion := ""
+ if len(versions) > 0 {
+ latestVersion = versions[len(versions)-1]
+ }
+ mctx.CreateAliasVariation("latest", latestVersion)
}
func VersionVariantAvailable(module interface {
@@ -1577,44 +1564,41 @@
return !module.Host() && !module.InRamdisk() && !module.InRecovery()
}
-// VersionMutator splits a module into the mandatory non-stubs variant
-// (which is unnamed) and zero or more stubs variants.
-func VersionMutator(mctx android.BottomUpMutatorContext) {
+// versionSelector normalizes the versions in the Stubs.Versions property into MutatedProperties.AllStubsVersions,
+// and propagates the value from implementation libraries to llndk libraries with the same name.
+func versionSelectorMutator(mctx android.BottomUpMutatorContext) {
if library, ok := mctx.Module().(LinkableInterface); ok && VersionVariantAvailable(library) {
if library.CcLibrary() && library.BuildSharedVariant() && len(library.StubsVersions()) > 0 &&
!library.IsSdkVariant() {
+
versions := library.StubsVersions()
normalizeVersions(mctx, versions)
if mctx.Failed() {
return
}
-
- stubsVersionsLock.Lock()
- defer stubsVersionsLock.Unlock()
- // save the list of versions for later use
- stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = versions
-
- createVersionVariations(mctx, versions)
+ // Set the versions on the pre-mutated module so they can be read by any llndk modules that
+ // depend on the implementation library and haven't been mutated yet.
+ library.SetAllStubsVersions(versions)
return
}
if c, ok := library.(*Module); ok && c.IsStubs() {
- stubsVersionsLock.Lock()
- defer stubsVersionsLock.Unlock()
- // For LLNDK llndk_library, we borrow stubs.versions from its implementation library.
- // Since llndk_library has dependency to its implementation library,
- // we can safely access stubsVersionsFor() with its baseModuleName.
- versions := stubsVersionsFor(mctx.Config())[c.BaseModuleName()]
- // save the list of versions for later use
- stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = versions
-
- createVersionVariations(mctx, versions)
- return
+ // Get the versions from the implementation module.
+ impls := mctx.GetDirectDepsWithTag(llndkImplDep)
+ if len(impls) > 1 {
+ panic(fmt.Errorf("Expected single implmenetation library, got %d", len(impls)))
+ } else if len(impls) == 1 {
+ c.SetAllStubsVersions(impls[0].(*Module).AllStubsVersions())
+ }
}
+ }
+}
- mctx.CreateLocalVariations("")
- mctx.AliasVariation("")
- return
+// versionMutator splits a module into the mandatory non-stubs variant
+// (which is unnamed) and zero or more stubs variants.
+func versionMutator(mctx android.BottomUpMutatorContext) {
+ if library, ok := mctx.Module().(LinkableInterface); ok && VersionVariantAvailable(library) {
+ createVersionVariations(mctx, library.AllStubsVersions())
}
}