blob: d277e4ab7e97fc1d94519588c3593f33d1bf2422 [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 implementations for runtime_resource_overlay and
18// override_runtime_resource_overlay.
19
Cole Faustb7493472024-08-28 11:55:52 -070020import (
21 "android/soong/android"
22
Spandan Dase2f98da2024-11-18 19:22:39 +000023 "github.com/google/blueprint"
Cole Faustb7493472024-08-28 11:55:52 -070024 "github.com/google/blueprint/proptools"
25)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080026
27func init() {
28 RegisterRuntimeResourceOverlayBuildComponents(android.InitRegistrationContext)
29}
30
31func RegisterRuntimeResourceOverlayBuildComponents(ctx android.RegistrationContext) {
32 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Spandan Dase2f98da2024-11-18 19:22:39 +000033 ctx.RegisterModuleType("autogen_runtime_resource_overlay", AutogenRuntimeResourceOverlayFactory)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080034 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
35}
36
37type RuntimeResourceOverlay struct {
38 android.ModuleBase
39 android.DefaultableModuleBase
40 android.OverridableModuleBase
41 aapt
42
43 properties RuntimeResourceOverlayProperties
44 overridableProperties OverridableRuntimeResourceOverlayProperties
45
46 certificate Certificate
47
48 outputFile android.Path
49 installDir android.InstallPath
50}
51
52type RuntimeResourceOverlayProperties struct {
53 // the name of a certificate in the default certificate directory or an android_app_certificate
54 // module name in the form ":module".
Cole Fausteb9c1482024-11-18 16:49:19 -080055 Certificate proptools.Configurable[string] `android:"replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -080056
57 // Name of the signing certificate lineage file.
58 Lineage *string
59
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040060 // For overriding the --rotation-min-sdk-version property of apksig
61 RotationMinSdkVersion *string
62
Jaewoong Jungf9b44652020-12-21 12:29:12 -080063 // optional theme name. If specified, the overlay package will be applied
64 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
65 Theme *string
66
Tobias Thierer1b3e9492021-01-02 19:01:16 +000067 // If not blank, set to the version of the sdk to compile against. This
68 // can be either an API version (e.g. "29" for API level 29 AKA Android 10)
69 // or special subsets of the current platform, for example "none", "current",
70 // "core", "system", "test". See build/soong/java/sdk.go for the full and
71 // up-to-date list of possible values.
Jaewoong Jungf9b44652020-12-21 12:29:12 -080072 // Defaults to compiling against the current platform.
73 Sdk_version *string
74
75 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
76 // Defaults to sdk_version if not set.
77 Min_sdk_version *string
78
79 // list of android_library modules whose resources are extracted and linked against statically
Cole Faustb7493472024-08-28 11:55:52 -070080 Static_libs proptools.Configurable[[]string]
Jaewoong Jungf9b44652020-12-21 12:29:12 -080081
82 // list of android_app modules whose resources are extracted and linked against
83 Resource_libs []string
84
85 // Names of modules to be overridden. Listed modules can only be other overlays
86 // (in Make or Soong).
87 // This does not completely prevent installation of the overridden overlays, but if both
88 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
89 // from PRODUCT_PACKAGES.
90 Overrides []string
91}
92
93// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
94// a RuntimeResourceOverlay module.
95type RuntimeResourceOverlayModule interface {
96 android.Module
97 OutputFile() android.Path
98 Certificate() Certificate
99 Theme() string
100}
101
Spandan Das5d1b9292021-06-03 19:36:41 +0000102// RRO's partition logic is different from the partition logic of other modules defined in soong/android/paths.go
103// The default partition for RRO is "/product" and not "/system"
104func rroPartition(ctx android.ModuleContext) string {
105 var partition string
106 if ctx.DeviceSpecific() {
107 partition = ctx.DeviceConfig().OdmPath()
108 } else if ctx.SocSpecific() {
109 partition = ctx.DeviceConfig().VendorPath()
110 } else if ctx.SystemExtSpecific() {
111 partition = ctx.DeviceConfig().SystemExtPath()
112 } else {
113 partition = ctx.DeviceConfig().ProductPath()
114 }
115 return partition
116}
117
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800118func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900119 sdkDep := decodeSdkDep(ctx, android.SdkContext(r))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800120 if sdkDep.hasFrameworkLibs() {
121 r.aapt.deps(ctx, sdkDep)
122 }
123
Cole Fausteb9c1482024-11-18 16:49:19 -0800124 cert := android.SrcIsModule(r.properties.Certificate.GetOrDefault(ctx, ""))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800125 if cert != "" {
126 ctx.AddDependency(ctx.Module(), certificateTag, cert)
127 }
128
Cole Faustb7493472024-08-28 11:55:52 -0700129 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs.GetOrDefault(ctx, nil)...)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800130 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jihoon Kang9f442dc2024-03-20 22:09:04 +0000131
132 for _, aconfig_declaration := range r.aaptProperties.Flags_packages {
133 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
134 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800135}
136
137func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
138 // Compile and link resources
139 r.aapt.hasNoCode = true
140 // Do not remove resources without default values nor dedupe resource configurations with the same value
141 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
142 // Allow the override of "package name" and "overlay target package name"
143 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
144 if overridden || r.overridableProperties.Package_name != nil {
145 // The product override variable has a priority over the package_name property.
146 if !overridden {
147 manifestPackageName = *r.overridableProperties.Package_name
148 }
149 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
150 }
151 if r.overridableProperties.Target_package_name != nil {
152 aaptLinkFlags = append(aaptLinkFlags,
153 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
154 }
Jeremy Meyer7e671292022-10-07 18:21:34 +0000155 if r.overridableProperties.Category != nil {
156 aaptLinkFlags = append(aaptLinkFlags,
157 "--rename-overlay-category "+*r.overridableProperties.Category)
158 }
Jihoon Kang9aef7772024-06-14 23:45:06 +0000159 aconfigTextFilePaths := getAconfigFilePaths(ctx)
Alixf7a10272023-09-27 16:47:56 +0000160 r.aapt.buildActions(ctx,
161 aaptBuildActionOptions{
162 sdkContext: r,
163 enforceDefaultTargetSdkVersion: false,
164 extraLinkFlags: aaptLinkFlags,
Jihoon Kang9aef7772024-06-14 23:45:06 +0000165 aconfigTextFiles: aconfigTextFilePaths,
Alixf7a10272023-09-27 16:47:56 +0000166 },
167 )
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800168
169 // Sign the built package
Sam Delmerico82602492022-06-10 17:05:42 +0000170 _, _, certificates := collectAppDeps(ctx, r, false, false)
Cole Fausteb9c1482024-11-18 16:49:19 -0800171 r.certificate, certificates = processMainCert(r.ModuleBase, r.properties.Certificate.GetOrDefault(ctx, ""), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800172 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
173 var lineageFile android.Path
174 if lineage := String(r.properties.Lineage); lineage != "" {
175 lineageFile = android.PathForModuleSrc(ctx, lineage)
176 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400177
178 rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion)
179
180 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800181
182 r.outputFile = signed
Spandan Das5d1b9292021-06-03 19:36:41 +0000183 partition := rroPartition(ctx)
184 r.installDir = android.PathForModuleInPartitionInstall(ctx, partition, "overlay", String(r.properties.Theme))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800185 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
Jihoon Kang9aef7772024-06-14 23:45:06 +0000186
187 android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{
188 AconfigTextFiles: aconfigTextFilePaths,
189 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800190}
191
Jiyong Park92315372021-04-02 08:45:46 +0900192func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
193 return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800194}
195
Jiyong Parkf1691d22021-03-29 20:11:58 +0900196func (r *RuntimeResourceOverlay) SystemModules() string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800197 return ""
198}
199
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000200func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800201 if r.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000202 return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800203 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000204 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800205}
206
Spandan Dasa26eda72023-03-02 00:56:06 +0000207func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
208 return android.SdkSpecPrivate.ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000209}
210
Spandan Dasca70fc42023-03-01 23:38:49 +0000211func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
212 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800213}
214
215func (r *RuntimeResourceOverlay) Certificate() Certificate {
216 return r.certificate
217}
218
219func (r *RuntimeResourceOverlay) OutputFile() android.Path {
220 return r.outputFile
221}
222
223func (r *RuntimeResourceOverlay) Theme() string {
224 return String(r.properties.Theme)
225}
226
227// runtime_resource_overlay generates a resource-only apk file that can overlay application and
228// system resources at run time.
229func RuntimeResourceOverlayFactory() android.Module {
230 module := &RuntimeResourceOverlay{}
231 module.AddProperties(
232 &module.properties,
233 &module.aaptProperties,
234 &module.overridableProperties)
235
236 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
237 android.InitDefaultableModule(module)
238 android.InitOverridableModule(module, &module.properties.Overrides)
239 return module
240}
241
242// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
243type OverridableRuntimeResourceOverlayProperties struct {
244 // the package name of this app. The package name in the manifest file is used if one was not given.
245 Package_name *string
246
247 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
248 Target_package_name *string
Jeremy Meyer7e671292022-10-07 18:21:34 +0000249
250 // the rro category of this overlay. The category in the manifest file is used if one was not given.
251 Category *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800252}
253
254type OverrideRuntimeResourceOverlay struct {
255 android.ModuleBase
256 android.OverrideModuleBase
257}
258
259func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
260 // All the overrides happen in the base module.
261 // TODO(jungjw): Check the base module type.
262}
263
264// override_runtime_resource_overlay is used to create a module based on another
265// runtime_resource_overlay module by overriding some of its properties.
266func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
267 m := &OverrideRuntimeResourceOverlay{}
268 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
269
270 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
271 android.InitOverrideModule(m)
272 return m
273}
Spandan Dase2f98da2024-11-18 19:22:39 +0000274
275var (
276 generateOverlayManifestFile = pctx.AndroidStaticRule("generate_overlay_manifest",
277 blueprint.RuleParams{
278 Command: "build/make/tools/generate-enforce-rro-android-manifest.py " +
279 "--package-info $in " +
280 "--partition ${partition} " +
281 "--priority ${priority} -o $out",
282 CommandDeps: []string{"build/make/tools/generate-enforce-rro-android-manifest.py"},
283 }, "partition", "priority",
284 )
285)
286
287type AutogenRuntimeResourceOverlay struct {
288 android.ModuleBase
289 aapt
290
291 properties AutogenRuntimeResourceOverlayProperties
292
293 outputFile android.Path
294}
295
296type AutogenRuntimeResourceOverlayProperties struct {
297 Base *string
298 Sdk_version *string
299 Manifest *string `android:"path"`
300}
301
302func AutogenRuntimeResourceOverlayFactory() android.Module {
303 m := &AutogenRuntimeResourceOverlay{}
304 m.AddProperties(&m.properties)
305 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
306
307 return m
308}
309
310type rroDependencyTag struct {
311 blueprint.DependencyTag
312}
313
314// Autogenerated RROs should always depend on the source android_app that created it.
315func (tag rroDependencyTag) ReplaceSourceWithPrebuilt() bool {
316 return false
317}
318
319var rroDepTag = rroDependencyTag{}
320
321func (a *AutogenRuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
322 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
323 if sdkDep.hasFrameworkLibs() {
324 a.aapt.deps(ctx, sdkDep)
325 }
326 ctx.AddDependency(ctx.Module(), rroDepTag, proptools.String(a.properties.Base))
327}
328
329func (a *AutogenRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
330 if !a.Enabled(ctx) {
331 return
332 }
333 var rroDirs android.Paths
334 // Get rro dirs of the base app
335 ctx.VisitDirectDepsWithTag(rroDepTag, func(m android.Module) {
336 aarDep, _ := m.(AndroidLibraryDependency)
337 if ctx.InstallInProduct() {
338 rroDirs = filterRRO(aarDep.RRODirsDepSet(), product)
339 } else {
340 rroDirs = filterRRO(aarDep.RRODirsDepSet(), device)
341 }
342 })
343
344 if len(rroDirs) == 0 {
345 return
346 }
347
348 // Generate a manifest file
349 genManifest := android.PathForModuleGen(ctx, "AndroidManifest.xml")
350 partition := "vendor"
351 priority := "0"
352 if ctx.InstallInProduct() {
353 partition = "product"
354 priority = "1"
355 }
356 ctx.Build(pctx, android.BuildParams{
357 Rule: generateOverlayManifestFile,
358 Input: android.PathForModuleSrc(ctx, proptools.String(a.properties.Manifest)),
359 Output: genManifest,
360 Args: map[string]string{
361 "partition": partition,
362 "priority": priority,
363 },
364 })
365
366 // Compile and link resources into package-res.apk
367 a.aapt.hasNoCode = true
368 aaptLinkFlags := []string{"--auto-add-overlay", "--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
369
370 a.aapt.buildActions(ctx,
371 aaptBuildActionOptions{
372 sdkContext: a,
373 extraLinkFlags: aaptLinkFlags,
374 rroDirs: &rroDirs,
375 manifestForAapt: genManifest,
376 },
377 )
378
379 if a.exportPackage == nil {
380 return
381 }
382 // Sign the built package
383 _, certificates := processMainCert(a.ModuleBase, "", nil, ctx)
384 signed := android.PathForModuleOut(ctx, "signed", a.Name()+".apk")
385 SignAppPackage(ctx, signed, a.exportPackage, certificates, nil, nil, "")
386 a.outputFile = signed
387
388 // Install the signed apk
389 installDir := android.PathForModuleInstall(ctx, "overlay")
390 ctx.InstallFile(installDir, signed.Base(), signed)
391}
392
393func (a *AutogenRuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
394 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
395}
396
397func (a *AutogenRuntimeResourceOverlay) SystemModules() string {
398 return ""
399}
400
401func (a *AutogenRuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
402 return a.SdkVersion(ctx).ApiLevel
403}
404
405func (r *AutogenRuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
406 return android.SdkSpecPrivate.ApiLevel
407}
408
409func (a *AutogenRuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
410 return a.SdkVersion(ctx).ApiLevel
411}
412
413func (a *AutogenRuntimeResourceOverlay) InstallInProduct() bool {
414 return a.ProductSpecific()
415}