diff options
| -rw-r--r-- | android/prebuilt.go | 19 | ||||
| -rw-r--r-- | java/app_import.go | 42 |
2 files changed, 50 insertions, 11 deletions
diff --git a/android/prebuilt.go b/android/prebuilt.go index 19f12f035..5d75b62fe 100644 --- a/android/prebuilt.go +++ b/android/prebuilt.go @@ -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) diff --git a/java/app_import.go b/java/app_import.go index a54cf2fc3..f5d9f3e79 100644 --- a/java/app_import.go +++ b/java/app_import.go @@ -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 } |