| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. | 
 | 2 | // | 
 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 4 | // you may not use this file except in compliance with the License. | 
 | 5 | // You may obtain a copy of the License at | 
 | 6 | // | 
 | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 8 | // | 
 | 9 | // Unless required by applicable law or agreed to in writing, software | 
 | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 12 | // See the License for the specific language governing permissions and | 
 | 13 | // limitations under the License. | 
 | 14 |  | 
 | 15 | package java | 
 | 16 |  | 
 | 17 | // This file contains the module implementation for android_app_set. | 
 | 18 |  | 
 | 19 | import ( | 
 | 20 | 	"strconv" | 
 | 21 | 	"strings" | 
 | 22 |  | 
 | 23 | 	"github.com/google/blueprint/proptools" | 
 | 24 |  | 
 | 25 | 	"android/soong/android" | 
 | 26 | ) | 
 | 27 |  | 
 | 28 | func init() { | 
 | 29 | 	RegisterAppSetBuildComponents(android.InitRegistrationContext) | 
 | 30 | } | 
 | 31 |  | 
 | 32 | func RegisterAppSetBuildComponents(ctx android.RegistrationContext) { | 
 | 33 | 	ctx.RegisterModuleType("android_app_set", AndroidAppSetFactory) | 
 | 34 | } | 
 | 35 |  | 
 | 36 | type AndroidAppSetProperties struct { | 
 | 37 | 	// APK Set path | 
 | 38 | 	Set *string | 
 | 39 |  | 
 | 40 | 	// Specifies that this app should be installed to the priv-app directory, | 
 | 41 | 	// where the system will grant it additional privileges not available to | 
 | 42 | 	// normal apps. | 
 | 43 | 	Privileged *bool | 
 | 44 |  | 
 | 45 | 	// APKs in this set use prerelease SDK version | 
 | 46 | 	Prerelease *bool | 
 | 47 |  | 
 | 48 | 	// Names of modules to be overridden. Listed modules can only be other apps | 
 | 49 | 	//	(in Make or Soong). | 
 | 50 | 	Overrides []string | 
 | 51 | } | 
 | 52 |  | 
 | 53 | type AndroidAppSet struct { | 
 | 54 | 	android.ModuleBase | 
 | 55 | 	android.DefaultableModuleBase | 
 | 56 | 	prebuilt android.Prebuilt | 
 | 57 |  | 
| Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 58 | 	properties    AndroidAppSetProperties | 
 | 59 | 	packedOutput  android.WritablePath | 
 | 60 | 	primaryOutput android.WritablePath | 
 | 61 | 	apkcertsFile  android.ModuleOutPath | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 62 | } | 
 | 63 |  | 
 | 64 | func (as *AndroidAppSet) Name() string { | 
 | 65 | 	return as.prebuilt.Name(as.ModuleBase.Name()) | 
 | 66 | } | 
 | 67 |  | 
 | 68 | func (as *AndroidAppSet) IsInstallable() bool { | 
 | 69 | 	return true | 
 | 70 | } | 
 | 71 |  | 
 | 72 | func (as *AndroidAppSet) Prebuilt() *android.Prebuilt { | 
 | 73 | 	return &as.prebuilt | 
 | 74 | } | 
 | 75 |  | 
 | 76 | func (as *AndroidAppSet) Privileged() bool { | 
 | 77 | 	return Bool(as.properties.Privileged) | 
 | 78 | } | 
 | 79 |  | 
 | 80 | func (as *AndroidAppSet) OutputFile() android.Path { | 
| Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 81 | 	return as.primaryOutput | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 82 | } | 
 | 83 |  | 
| Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 84 | func (as *AndroidAppSet) PackedAdditionalOutputs() android.Path { | 
 | 85 | 	return as.packedOutput | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 86 | } | 
 | 87 |  | 
 | 88 | func (as *AndroidAppSet) APKCertsFile() android.Path { | 
 | 89 | 	return as.apkcertsFile | 
 | 90 | } | 
 | 91 |  | 
 | 92 | var TargetCpuAbi = map[string]string{ | 
| Colin Cross | 9f09316 | 2022-11-01 16:14:33 -0700 | [diff] [blame] | 93 | 	"arm":   "ARMEABI_V7A", | 
 | 94 | 	"arm64": "ARM64_V8A", | 
 | 95 | 	// TODO: use "RISCV64" when that is supported in bundles | 
 | 96 | 	"riscv64": "ARM64_V8A", | 
| Colin Cross | a2aaa2f | 2022-10-03 12:41:50 -0700 | [diff] [blame] | 97 | 	"x86":     "X86", | 
 | 98 | 	"x86_64":  "X86_64", | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 99 | } | 
 | 100 |  | 
| Anton Hansson | 805e0a5 | 2022-11-25 14:06:46 +0000 | [diff] [blame] | 101 | func SupportedAbis(ctx android.ModuleContext, excludeNativeBridgeAbis bool) []string { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 102 | 	abiName := func(targetIdx int, deviceArch string) string { | 
 | 103 | 		if abi, found := TargetCpuAbi[deviceArch]; found { | 
 | 104 | 			return abi | 
 | 105 | 		} | 
 | 106 | 		ctx.ModuleErrorf("Target %d has invalid Arch: %s", targetIdx, deviceArch) | 
 | 107 | 		return "BAD_ABI" | 
 | 108 | 	} | 
 | 109 |  | 
 | 110 | 	var result []string | 
 | 111 | 	for i, target := range ctx.Config().Targets[android.Android] { | 
| Anton Hansson | 805e0a5 | 2022-11-25 14:06:46 +0000 | [diff] [blame] | 112 | 		if target.NativeBridge == android.NativeBridgeEnabled && excludeNativeBridgeAbis { | 
 | 113 | 			continue | 
 | 114 | 		} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 115 | 		result = append(result, abiName(i, target.Arch.ArchType.String())) | 
 | 116 | 	} | 
 | 117 | 	return result | 
 | 118 | } | 
 | 119 |  | 
 | 120 | func (as *AndroidAppSet) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
 | 121 | 	as.packedOutput = android.PathForModuleOut(ctx, ctx.ModuleName()+".zip") | 
| Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 122 | 	as.primaryOutput = android.PathForModuleOut(ctx, as.BaseModuleName()+".apk") | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 123 | 	as.apkcertsFile = android.PathForModuleOut(ctx, "apkcerts.txt") | 
 | 124 | 	// We are assuming here that the install file in the APK | 
 | 125 | 	// set has `.apk` suffix. If it doesn't the build will fail. | 
 | 126 | 	// APK sets containing APEX files are handled elsewhere. | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 127 | 	screenDensities := "all" | 
 | 128 | 	if dpis := ctx.Config().ProductAAPTPrebuiltDPI(); len(dpis) > 0 { | 
 | 129 | 		screenDensities = strings.ToUpper(strings.Join(dpis, ",")) | 
 | 130 | 	} | 
 | 131 | 	// TODO(asmundak): handle locales. | 
 | 132 | 	// TODO(asmundak): do we support device features | 
 | 133 | 	ctx.Build(pctx, | 
 | 134 | 		android.BuildParams{ | 
| Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 135 | 			Rule:            extractMatchingApks, | 
 | 136 | 			Description:     "Extract APKs from APK set", | 
 | 137 | 			Output:          as.primaryOutput, | 
 | 138 | 			ImplicitOutputs: android.WritablePaths{as.packedOutput, as.apkcertsFile}, | 
 | 139 | 			Inputs:          android.Paths{as.prebuilt.SingleSourcePath(ctx)}, | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 140 | 			Args: map[string]string{ | 
| Anton Hansson | 805e0a5 | 2022-11-25 14:06:46 +0000 | [diff] [blame] | 141 | 				"abis":              strings.Join(SupportedAbis(ctx, false), ","), | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 142 | 				"allow-prereleased": strconv.FormatBool(proptools.Bool(as.properties.Prerelease)), | 
 | 143 | 				"screen-densities":  screenDensities, | 
 | 144 | 				"sdk-version":       ctx.Config().PlatformSdkVersion().String(), | 
| Pranav Gupta | 51645ff | 2023-03-20 16:19:53 -0700 | [diff] [blame] | 145 | 				"skip-sdk-check":    strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")), | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 146 | 				"stem":              as.BaseModuleName(), | 
 | 147 | 				"apkcerts":          as.apkcertsFile.String(), | 
 | 148 | 				"partition":         as.PartitionTag(ctx.DeviceConfig()), | 
| Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 149 | 				"zip":               as.packedOutput.String(), | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 150 | 			}, | 
 | 151 | 		}) | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 152 |  | 
 | 153 | 	var installDir android.InstallPath | 
 | 154 | 	if as.Privileged() { | 
 | 155 | 		installDir = android.PathForModuleInstall(ctx, "priv-app", as.BaseModuleName()) | 
 | 156 | 	} else { | 
 | 157 | 		installDir = android.PathForModuleInstall(ctx, "app", as.BaseModuleName()) | 
 | 158 | 	} | 
 | 159 | 	ctx.InstallFileWithExtraFilesZip(installDir, as.BaseModuleName()+".apk", as.primaryOutput, as.packedOutput) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 160 | } | 
 | 161 |  | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 162 | func (as *AndroidAppSet) InstallBypassMake() bool { return true } | 
 | 163 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 164 | // android_app_set extracts a set of APKs based on the target device | 
 | 165 | // configuration and installs this set as "split APKs". | 
 | 166 | // The extracted set always contains an APK whose name is | 
 | 167 | // _module_name_.apk and every split APK matching target device. | 
 | 168 | // The extraction of the density-specific splits depends on | 
 | 169 | // PRODUCT_AAPT_PREBUILT_DPI variable. If present (its value should | 
 | 170 | // be a list density names: LDPI, MDPI, HDPI, etc.), only listed | 
 | 171 | // splits will be extracted. Otherwise all density-specific splits | 
 | 172 | // will be extracted. | 
 | 173 | func AndroidAppSetFactory() android.Module { | 
 | 174 | 	module := &AndroidAppSet{} | 
 | 175 | 	module.AddProperties(&module.properties) | 
 | 176 | 	InitJavaModule(module, android.DeviceSupported) | 
 | 177 | 	android.InitSingleSourcePrebuiltModule(module, &module.properties, "Set") | 
 | 178 | 	return module | 
 | 179 | } |