blob: d9f4ff7c8d554e406451e9cedb3ea8b9e83c8a3a [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"}
Artem Anashkin038866a2024-12-16 17:31:55 +0300142
143 // Add TARGET_AAPT_CHARACTERISTICS values to AAPT link flags if they exist and --product flags were not provided.
144 hasProduct := android.PrefixInList(r.aaptProperties.Aaptflags, "--product")
145 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
146 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
147 }
148
149 if !Bool(r.aaptProperties.Aapt_include_all_resources) {
150 // Product AAPT config
151 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
152 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
153 }
154
155 // Product AAPT preferred config
156 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
157 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
158 }
159 }
160
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800161 // Allow the override of "package name" and "overlay target package name"
162 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
163 if overridden || r.overridableProperties.Package_name != nil {
164 // The product override variable has a priority over the package_name property.
165 if !overridden {
166 manifestPackageName = *r.overridableProperties.Package_name
167 }
168 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
169 }
170 if r.overridableProperties.Target_package_name != nil {
171 aaptLinkFlags = append(aaptLinkFlags,
172 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
173 }
Jeremy Meyer7e671292022-10-07 18:21:34 +0000174 if r.overridableProperties.Category != nil {
175 aaptLinkFlags = append(aaptLinkFlags,
176 "--rename-overlay-category "+*r.overridableProperties.Category)
177 }
Jihoon Kang9aef7772024-06-14 23:45:06 +0000178 aconfigTextFilePaths := getAconfigFilePaths(ctx)
Alixf7a10272023-09-27 16:47:56 +0000179 r.aapt.buildActions(ctx,
180 aaptBuildActionOptions{
181 sdkContext: r,
182 enforceDefaultTargetSdkVersion: false,
183 extraLinkFlags: aaptLinkFlags,
Jihoon Kang9aef7772024-06-14 23:45:06 +0000184 aconfigTextFiles: aconfigTextFilePaths,
Alixf7a10272023-09-27 16:47:56 +0000185 },
186 )
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800187
188 // Sign the built package
Sam Delmerico82602492022-06-10 17:05:42 +0000189 _, _, certificates := collectAppDeps(ctx, r, false, false)
Cole Fausteb9c1482024-11-18 16:49:19 -0800190 r.certificate, certificates = processMainCert(r.ModuleBase, r.properties.Certificate.GetOrDefault(ctx, ""), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800191 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
192 var lineageFile android.Path
193 if lineage := String(r.properties.Lineage); lineage != "" {
194 lineageFile = android.PathForModuleSrc(ctx, lineage)
195 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400196
197 rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion)
198
199 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800200
201 r.outputFile = signed
Spandan Das5d1b9292021-06-03 19:36:41 +0000202 partition := rroPartition(ctx)
203 r.installDir = android.PathForModuleInPartitionInstall(ctx, partition, "overlay", String(r.properties.Theme))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800204 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
Jihoon Kang9aef7772024-06-14 23:45:06 +0000205
206 android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{
207 AconfigTextFiles: aconfigTextFilePaths,
208 })
Wei Li986fe742025-01-30 15:14:42 -0800209
210 buildComplianceMetadata(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800211}
212
Jiyong Park92315372021-04-02 08:45:46 +0900213func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
214 return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800215}
216
Jiyong Parkf1691d22021-03-29 20:11:58 +0900217func (r *RuntimeResourceOverlay) SystemModules() string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800218 return ""
219}
220
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000221func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800222 if r.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000223 return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800224 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000225 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800226}
227
Spandan Dasa26eda72023-03-02 00:56:06 +0000228func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
229 return android.SdkSpecPrivate.ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000230}
231
Spandan Dasca70fc42023-03-01 23:38:49 +0000232func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
233 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800234}
235
236func (r *RuntimeResourceOverlay) Certificate() Certificate {
237 return r.certificate
238}
239
240func (r *RuntimeResourceOverlay) OutputFile() android.Path {
241 return r.outputFile
242}
243
244func (r *RuntimeResourceOverlay) Theme() string {
245 return String(r.properties.Theme)
246}
247
248// runtime_resource_overlay generates a resource-only apk file that can overlay application and
249// system resources at run time.
250func RuntimeResourceOverlayFactory() android.Module {
251 module := &RuntimeResourceOverlay{}
252 module.AddProperties(
253 &module.properties,
254 &module.aaptProperties,
255 &module.overridableProperties)
256
257 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
258 android.InitDefaultableModule(module)
259 android.InitOverridableModule(module, &module.properties.Overrides)
260 return module
261}
262
263// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
264type OverridableRuntimeResourceOverlayProperties struct {
265 // the package name of this app. The package name in the manifest file is used if one was not given.
266 Package_name *string
267
268 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
269 Target_package_name *string
Jeremy Meyer7e671292022-10-07 18:21:34 +0000270
271 // the rro category of this overlay. The category in the manifest file is used if one was not given.
272 Category *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800273}
274
275type OverrideRuntimeResourceOverlay struct {
276 android.ModuleBase
277 android.OverrideModuleBase
278}
279
280func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
281 // All the overrides happen in the base module.
282 // TODO(jungjw): Check the base module type.
283}
284
285// override_runtime_resource_overlay is used to create a module based on another
286// runtime_resource_overlay module by overriding some of its properties.
287func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
288 m := &OverrideRuntimeResourceOverlay{}
289 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
290
291 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
292 android.InitOverrideModule(m)
293 return m
294}
Spandan Dase2f98da2024-11-18 19:22:39 +0000295
296var (
297 generateOverlayManifestFile = pctx.AndroidStaticRule("generate_overlay_manifest",
298 blueprint.RuleParams{
299 Command: "build/make/tools/generate-enforce-rro-android-manifest.py " +
300 "--package-info $in " +
301 "--partition ${partition} " +
302 "--priority ${priority} -o $out",
303 CommandDeps: []string{"build/make/tools/generate-enforce-rro-android-manifest.py"},
304 }, "partition", "priority",
305 )
306)
307
308type AutogenRuntimeResourceOverlay struct {
309 android.ModuleBase
310 aapt
311
312 properties AutogenRuntimeResourceOverlayProperties
313
Spandan Dasaf8a3f52024-12-08 18:22:45 +0000314 certificate Certificate
315 outputFile android.Path
Spandan Dase2f98da2024-11-18 19:22:39 +0000316}
317
318type AutogenRuntimeResourceOverlayProperties struct {
319 Base *string
320 Sdk_version *string
321 Manifest *string `android:"path"`
322}
323
324func AutogenRuntimeResourceOverlayFactory() android.Module {
325 m := &AutogenRuntimeResourceOverlay{}
326 m.AddProperties(&m.properties)
327 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
328
329 return m
330}
331
332type rroDependencyTag struct {
333 blueprint.DependencyTag
334}
335
336// Autogenerated RROs should always depend on the source android_app that created it.
337func (tag rroDependencyTag) ReplaceSourceWithPrebuilt() bool {
338 return false
339}
340
341var rroDepTag = rroDependencyTag{}
342
343func (a *AutogenRuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
344 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
345 if sdkDep.hasFrameworkLibs() {
346 a.aapt.deps(ctx, sdkDep)
347 }
348 ctx.AddDependency(ctx.Module(), rroDepTag, proptools.String(a.properties.Base))
349}
350
351func (a *AutogenRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
352 if !a.Enabled(ctx) {
353 return
354 }
355 var rroDirs android.Paths
356 // Get rro dirs of the base app
357 ctx.VisitDirectDepsWithTag(rroDepTag, func(m android.Module) {
358 aarDep, _ := m.(AndroidLibraryDependency)
359 if ctx.InstallInProduct() {
360 rroDirs = filterRRO(aarDep.RRODirsDepSet(), product)
361 } else {
362 rroDirs = filterRRO(aarDep.RRODirsDepSet(), device)
363 }
364 })
365
366 if len(rroDirs) == 0 {
367 return
368 }
369
370 // Generate a manifest file
371 genManifest := android.PathForModuleGen(ctx, "AndroidManifest.xml")
372 partition := "vendor"
373 priority := "0"
374 if ctx.InstallInProduct() {
375 partition = "product"
376 priority = "1"
377 }
378 ctx.Build(pctx, android.BuildParams{
379 Rule: generateOverlayManifestFile,
380 Input: android.PathForModuleSrc(ctx, proptools.String(a.properties.Manifest)),
381 Output: genManifest,
382 Args: map[string]string{
383 "partition": partition,
384 "priority": priority,
385 },
386 })
387
388 // Compile and link resources into package-res.apk
389 a.aapt.hasNoCode = true
390 aaptLinkFlags := []string{"--auto-add-overlay", "--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
391
392 a.aapt.buildActions(ctx,
393 aaptBuildActionOptions{
394 sdkContext: a,
395 extraLinkFlags: aaptLinkFlags,
396 rroDirs: &rroDirs,
397 manifestForAapt: genManifest,
398 },
399 )
400
401 if a.exportPackage == nil {
402 return
403 }
404 // Sign the built package
Spandan Dasaf8a3f52024-12-08 18:22:45 +0000405 var certificates []Certificate
406 a.certificate, certificates = processMainCert(a.ModuleBase, "", nil, ctx)
Spandan Dase2f98da2024-11-18 19:22:39 +0000407 signed := android.PathForModuleOut(ctx, "signed", a.Name()+".apk")
408 SignAppPackage(ctx, signed, a.exportPackage, certificates, nil, nil, "")
409 a.outputFile = signed
410
411 // Install the signed apk
412 installDir := android.PathForModuleInstall(ctx, "overlay")
413 ctx.InstallFile(installDir, signed.Base(), signed)
414}
415
416func (a *AutogenRuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
417 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
418}
419
420func (a *AutogenRuntimeResourceOverlay) SystemModules() string {
421 return ""
422}
423
424func (a *AutogenRuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
425 return a.SdkVersion(ctx).ApiLevel
426}
427
428func (r *AutogenRuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
429 return android.SdkSpecPrivate.ApiLevel
430}
431
432func (a *AutogenRuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
433 return a.SdkVersion(ctx).ApiLevel
434}
435
436func (a *AutogenRuntimeResourceOverlay) InstallInProduct() bool {
437 return a.ProductSpecific()
438}