blob: 33d3adec224a7025953da08bc68433e6f274e29b [file] [log] [blame]
Jaewoong Jungf9b44652020-12-21 12:29:12 -08001// 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
15package java
16
17// This file contains the module implementation for android_app_set.
18
19import (
20 "strconv"
21 "strings"
22
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26)
27
28func init() {
29 RegisterAppSetBuildComponents(android.InitRegistrationContext)
30}
31
32func RegisterAppSetBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("android_app_set", AndroidAppSetFactory)
34}
35
36type 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
Spandan Das3490dfd2024-03-11 21:37:25 +000051
52 // Path to the .prebuilt_info file of the prebuilt app.
53 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used
54 // to generate the prebuilt.
55 Prebuilt_info *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -080056}
57
58type AndroidAppSet struct {
59 android.ModuleBase
60 android.DefaultableModuleBase
61 prebuilt android.Prebuilt
62
Colin Crossffbcd1d2021-11-12 12:19:42 -080063 properties AndroidAppSetProperties
64 packedOutput android.WritablePath
65 primaryOutput android.WritablePath
66 apkcertsFile android.ModuleOutPath
Jaewoong Jungf9b44652020-12-21 12:29:12 -080067}
68
69func (as *AndroidAppSet) Name() string {
70 return as.prebuilt.Name(as.ModuleBase.Name())
71}
72
73func (as *AndroidAppSet) IsInstallable() bool {
74 return true
75}
76
77func (as *AndroidAppSet) Prebuilt() *android.Prebuilt {
78 return &as.prebuilt
79}
80
81func (as *AndroidAppSet) Privileged() bool {
82 return Bool(as.properties.Privileged)
83}
84
85func (as *AndroidAppSet) OutputFile() android.Path {
Colin Crossffbcd1d2021-11-12 12:19:42 -080086 return as.primaryOutput
Jaewoong Jungf9b44652020-12-21 12:29:12 -080087}
88
Colin Crossffbcd1d2021-11-12 12:19:42 -080089func (as *AndroidAppSet) PackedAdditionalOutputs() android.Path {
90 return as.packedOutput
Jaewoong Jungf9b44652020-12-21 12:29:12 -080091}
92
93func (as *AndroidAppSet) APKCertsFile() android.Path {
94 return as.apkcertsFile
95}
96
97var TargetCpuAbi = map[string]string{
Colin Cross9f093162022-11-01 16:14:33 -070098 "arm": "ARMEABI_V7A",
99 "arm64": "ARM64_V8A",
100 // TODO: use "RISCV64" when that is supported in bundles
101 "riscv64": "ARM64_V8A",
Colin Crossa2aaa2f2022-10-03 12:41:50 -0700102 "x86": "X86",
103 "x86_64": "X86_64",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800104}
105
Anton Hansson805e0a52022-11-25 14:06:46 +0000106func SupportedAbis(ctx android.ModuleContext, excludeNativeBridgeAbis bool) []string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800107 abiName := func(targetIdx int, deviceArch string) string {
108 if abi, found := TargetCpuAbi[deviceArch]; found {
109 return abi
110 }
111 ctx.ModuleErrorf("Target %d has invalid Arch: %s", targetIdx, deviceArch)
112 return "BAD_ABI"
113 }
114
115 var result []string
116 for i, target := range ctx.Config().Targets[android.Android] {
Anton Hansson805e0a52022-11-25 14:06:46 +0000117 if target.NativeBridge == android.NativeBridgeEnabled && excludeNativeBridgeAbis {
118 continue
119 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800120 result = append(result, abiName(i, target.Arch.ArchType.String()))
121 }
122 return result
123}
124
Spandan Das3490dfd2024-03-11 21:37:25 +0000125type prebuiltInfoProps struct {
126 baseModuleName string
127 isPrebuilt bool
128 prebuiltInfo *string
129}
130
131// Set prebuiltInfoProvider. This will be used by `apex_prebuiltinfo_singleton` to print out a metadata file
132// with information about whether source or prebuilt of an apex was used during the build.
133func providePrebuiltInfo(ctx android.ModuleContext, p prebuiltInfoProps) {
134 info := android.PrebuiltInfo{
135 Name: p.baseModuleName,
136 Is_prebuilt: p.isPrebuilt,
137 }
138 // If Prebuilt_info information is available in the soong module definition, add it to prebuilt_info.json.
139 if p.prebuiltInfo != nil {
140 prebuiltInfoFile := android.PathForModuleSrc(ctx, *p.prebuiltInfo)
141 info.Prebuilt_info_file_path = prebuiltInfoFile.String()
142 }
143 android.SetProvider(ctx, android.PrebuiltInfoProvider, info)
144}
145
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800146func (as *AndroidAppSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
147 as.packedOutput = android.PathForModuleOut(ctx, ctx.ModuleName()+".zip")
Colin Crossffbcd1d2021-11-12 12:19:42 -0800148 as.primaryOutput = android.PathForModuleOut(ctx, as.BaseModuleName()+".apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800149 as.apkcertsFile = android.PathForModuleOut(ctx, "apkcerts.txt")
150 // We are assuming here that the install file in the APK
151 // set has `.apk` suffix. If it doesn't the build will fail.
152 // APK sets containing APEX files are handled elsewhere.
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800153 screenDensities := "all"
154 if dpis := ctx.Config().ProductAAPTPrebuiltDPI(); len(dpis) > 0 {
155 screenDensities = strings.ToUpper(strings.Join(dpis, ","))
156 }
157 // TODO(asmundak): handle locales.
158 // TODO(asmundak): do we support device features
159 ctx.Build(pctx,
160 android.BuildParams{
Colin Crossffbcd1d2021-11-12 12:19:42 -0800161 Rule: extractMatchingApks,
162 Description: "Extract APKs from APK set",
163 Output: as.primaryOutput,
164 ImplicitOutputs: android.WritablePaths{as.packedOutput, as.apkcertsFile},
165 Inputs: android.Paths{as.prebuilt.SingleSourcePath(ctx)},
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800166 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000167 "abis": strings.Join(SupportedAbis(ctx, false), ","),
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800168 "allow-prereleased": strconv.FormatBool(proptools.Bool(as.properties.Prerelease)),
169 "screen-densities": screenDensities,
170 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700171 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800172 "stem": as.BaseModuleName(),
173 "apkcerts": as.apkcertsFile.String(),
174 "partition": as.PartitionTag(ctx.DeviceConfig()),
Colin Crossffbcd1d2021-11-12 12:19:42 -0800175 "zip": as.packedOutput.String(),
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800176 },
177 })
Colin Cross50ed1f92021-11-12 17:41:02 -0800178
179 var installDir android.InstallPath
180 if as.Privileged() {
181 installDir = android.PathForModuleInstall(ctx, "priv-app", as.BaseModuleName())
182 } else {
183 installDir = android.PathForModuleInstall(ctx, "app", as.BaseModuleName())
184 }
185 ctx.InstallFileWithExtraFilesZip(installDir, as.BaseModuleName()+".apk", as.primaryOutput, as.packedOutput)
Spandan Das3490dfd2024-03-11 21:37:25 +0000186
187 providePrebuiltInfo(ctx,
188 prebuiltInfoProps{
189 baseModuleName: as.BaseModuleName(),
190 isPrebuilt: true,
191 prebuiltInfo: as.properties.Prebuilt_info,
192 },
193 )
194
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800195}
196
Colin Cross50ed1f92021-11-12 17:41:02 -0800197func (as *AndroidAppSet) InstallBypassMake() bool { return true }
198
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800199// android_app_set extracts a set of APKs based on the target device
200// configuration and installs this set as "split APKs".
201// The extracted set always contains an APK whose name is
202// _module_name_.apk and every split APK matching target device.
203// The extraction of the density-specific splits depends on
204// PRODUCT_AAPT_PREBUILT_DPI variable. If present (its value should
205// be a list density names: LDPI, MDPI, HDPI, etc.), only listed
206// splits will be extracted. Otherwise all density-specific splits
207// will be extracted.
208func AndroidAppSetFactory() android.Module {
209 module := &AndroidAppSet{}
210 module.AddProperties(&module.properties)
211 InitJavaModule(module, android.DeviceSupported)
212 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Set")
213 return module
214}