Add SetDefaultDependencyVariation

SetDefaultDependencyVariation sets the variation name that will be used
when a dangling dependency is found while a module is being split. A
dangling dependency can occur if a module is split to a variant that one
of its dependencies is not split into. When the default variation is not
set, such dangling dependency is a hard error. But with the new
function, the default variation can be set and subsequent calls to
CreateVariations and its variations on the same context uses the default
variation when necessary.

(If even the default variation does not exist for the dependent module,
it is an hard error)

Note that this is different from calling SetDependencyVariation("foo")
followed by CreateVariations("foo", "bar"). In that case, regardless of
whether a dependency of the current module has the variant 'bar' or not,
only the 'foo' variant is chosen.

With SetDefaultDependencyVariation("foo") followed by
CreateVariations("foo", "bar"), 'foo' variant is used only when the
'bar' variant of the current module depends on a module that does not
have 'bar' variant.

Bug: 138103882
Test: m
Merged-In: I2c79c3c9e0437fd785b06ca20f43da2963d50b68
(cherry picked from commit 1e2e56dc624bd93845288d803be588c597f0bd6f)
Change-Id: I4520ca87487994de024fdbacda3bef6636225f0d
diff --git a/context.go b/context.go
index 324d1ec..23d9262 100644
--- a/context.go
+++ b/context.go
@@ -1147,7 +1147,7 @@
 }
 
 func (c *Context) createVariations(origModule *moduleInfo, mutatorName string,
-	variationNames []string) ([]*moduleInfo, []error) {
+	defaultVariationName *string, variationNames []string) ([]*moduleInfo, []error) {
 
 	if len(variationNames) == 0 {
 		panic(fmt.Errorf("mutator %q passed zero-length variation list for module %q",
@@ -1192,7 +1192,7 @@
 
 		newModules = append(newModules, newModule)
 
-		newErrs := c.convertDepsToVariation(newModule, mutatorName, variationName)
+		newErrs := c.convertDepsToVariation(newModule, mutatorName, variationName, defaultVariationName)
 		if len(newErrs) > 0 {
 			errs = append(errs, newErrs...)
 		}
@@ -1209,7 +1209,7 @@
 }
 
 func (c *Context) convertDepsToVariation(module *moduleInfo,
-	mutatorName, variationName string) (errs []error) {
+	mutatorName, variationName string, defaultVariationName *string) (errs []error) {
 
 	for i, dep := range module.directDeps {
 		if dep.module.logicModule == nil {
@@ -1220,6 +1220,15 @@
 					break
 				}
 			}
+			if newDep == nil && defaultVariationName != nil {
+				// give it a second chance; match with defaultVariationName
+				for _, m := range dep.module.splitModules {
+					if m.variant[mutatorName] == *defaultVariationName {
+						newDep = m
+						break
+					}
+				}
+			}
 			if newDep == nil {
 				errs = append(errs, &BlueprintError{
 					Err: fmt.Errorf("failed to find variation %q for module %q needed by %q",
diff --git a/module_ctx.go b/module_ctx.go
index d127c0e..bdb14ad 100644
--- a/module_ctx.go
+++ b/module_ctx.go
@@ -555,12 +555,13 @@
 
 type mutatorContext struct {
 	baseModuleContext
-	name          string
-	reverseDeps   []reverseDep
-	rename        []rename
-	replace       []replace
-	newVariations []*moduleInfo // new variants of existing modules
-	newModules    []*moduleInfo // brand new modules
+	name             string
+	reverseDeps      []reverseDep
+	rename           []rename
+	replace          []replace
+	newVariations    []*moduleInfo // new variants of existing modules
+	newModules       []*moduleInfo // brand new modules
+	defaultVariation *string
 }
 
 type baseMutatorContext interface {
@@ -607,7 +608,15 @@
 	AddReverseDependency(module Module, tag DependencyTag, name string)
 	CreateVariations(...string) []Module
 	CreateLocalVariations(...string) []Module
+
+	// SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
+	// with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
 	SetDependencyVariation(string)
+
+	// SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
+	// during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
+	SetDefaultDependencyVariation(*string)
+
 	AddVariationDependencies([]Variation, DependencyTag, ...string)
 	AddFarVariationDependencies([]Variation, DependencyTag, ...string)
 	AddInterVariantDependency(tag DependencyTag, from, to Module)
@@ -671,7 +680,7 @@
 
 func (mctx *mutatorContext) createVariations(variationNames []string, local bool) []Module {
 	ret := []Module{}
-	modules, errs := mctx.context.createVariations(mctx.module, mctx.name, variationNames)
+	modules, errs := mctx.context.createVariations(mctx.module, mctx.name, mctx.defaultVariation, variationNames)
 	if len(errs) > 0 {
 		mctx.errs = append(mctx.errs, errs...)
 	}
@@ -698,7 +707,11 @@
 // Set all dangling dependencies on the current module to point to the variation
 // with given name.
 func (mctx *mutatorContext) SetDependencyVariation(variationName string) {
-	mctx.context.convertDepsToVariation(mctx.module, mctx.name, variationName)
+	mctx.context.convertDepsToVariation(mctx.module, mctx.name, variationName, nil)
+}
+
+func (mctx *mutatorContext) SetDefaultDependencyVariation(variationName *string) {
+	mctx.defaultVariation = variationName
 }
 
 func (mctx *mutatorContext) Module() Module {