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

Commit c71b175e authored by Cole Faust's avatar Cole Faust
Browse files

Make the "apk" property configurable

On android_app_import and android_test_import.

Unfortunately I had to add a mutator because you can't evaluate
configurable properties during defaultable hooks.

Bug: 373414956
Test: m nothing --no-skip-soong-tests
Change-Id: Ic42d558e4f2222091aac250b9513d867fb8d6984
parent 8017cca9
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -272,6 +272,25 @@ func InitConfigurablePrebuiltModule(module PrebuiltInterface, srcs *proptools.Co
	InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
}

// InitConfigurablePrebuiltModuleString is the same as InitPrebuiltModule, but uses a
// Configurable string property instead of a regular list of strings. It only produces a single
// source file.
func InitConfigurablePrebuiltModuleString(module PrebuiltInterface, srcs *proptools.Configurable[string], propertyName string) {
	if srcs == nil {
		panic(fmt.Errorf("%s must not be nil", propertyName))
	}

	srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
		src := srcs.GetOrDefault(ctx, "")
		if src == "" {
			return nil
		}
		return []string{src}
	}

	InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, propertyName)
}

func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
	srcPropsValue := reflect.ValueOf(srcProps).Elem()
	srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
+31 −11
Original line number Diff line number Diff line
@@ -61,6 +61,9 @@ var (
func RegisterAppImportBuildComponents(ctx android.RegistrationContext) {
	ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
	ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
	ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
		ctx.BottomUp("disable_prebuilts_without_apk", disablePrebuiltsWithoutApkMutator)
	})
}

type AndroidAppImport struct {
@@ -90,7 +93,7 @@ type AndroidAppImport struct {

type AndroidAppImportProperties struct {
	// A prebuilt apk to import
	Apk *string `android:"path"`
	Apk proptools.Configurable[string] `android:"path,replace_instead_of_append"`

	// The name of a certificate in the default certificate directory or an android_app_certificate
	// module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
@@ -193,13 +196,6 @@ func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
			}
		}
	}

	if String(a.properties.Apk) == "" {
		// Disable this module since the apk property is still empty after processing all matching
		// variants. This likely means there is no matching variant, and the default variant doesn't
		// have an apk property value either.
		a.Disable()
	}
}

func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
@@ -219,6 +215,30 @@ func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
	}
}

// disablePrebuiltsWithoutApkMutator is a pre-arch mutator that disables AndroidAppImport or
// AndroidTestImport modules that don't have an apk set. We need this separate mutator instead
// of doing it in processVariants because processVariants is a defaultable hook, and configurable
// properties can only be evaluated after the defaults (and eventually, base configurabtion)
// mutators.
func disablePrebuiltsWithoutApkMutator(ctx android.BottomUpMutatorContext) {
	switch a := ctx.Module().(type) {
	case *AndroidAppImport:
		if a.properties.Apk.GetOrDefault(ctx, "") == "" {
			// Disable this module since the apk property is still empty after processing all
			// matching variants. This likely means there is no matching variant, and the default
			// variant doesn't have an apk property value either.
			a.Disable()
		}
	case *AndroidTestImport:
		if a.properties.Apk.GetOrDefault(ctx, "") == "" {
			// Disable this module since the apk property is still empty after processing all
			// matching variants. This likely means there is no matching variant, and the default
			// variant doesn't have an apk property value either.
			a.Disable()
		}
	}
}

func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
	cert := android.SrcIsModule(String(a.properties.Certificate))
	if cert != "" {
@@ -409,7 +429,7 @@ func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext

	if apexInfo.IsForPlatform() {
		a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
		artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk)
		artifactPath := android.PathForModuleSrc(ctx, a.properties.Apk.GetOrDefault(ctx, ""))
		a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
	}

@@ -633,7 +653,7 @@ func AndroidAppImportFactory() android.Module {
	android.InitApexModule(module)
	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
	android.InitDefaultableModule(module)
	android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
	android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")

	module.usesLibrary.enforce = true

@@ -686,7 +706,7 @@ func AndroidTestImportFactory() android.Module {
	android.InitApexModule(module)
	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
	android.InitDefaultableModule(module)
	android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
	android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")

	return module
}