blob: 44d55646e3a40338cb8ab9f5c224e123f45111cf [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 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800209}
210
Jiyong Park92315372021-04-02 08:45:46 +0900211func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
212 return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800213}
214
Jiyong Parkf1691d22021-03-29 20:11:58 +0900215func (r *RuntimeResourceOverlay) SystemModules() string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800216 return ""
217}
218
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000219func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800220 if r.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000221 return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800222 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000223 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800224}
225
Spandan Dasa26eda72023-03-02 00:56:06 +0000226func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
227 return android.SdkSpecPrivate.ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000228}
229
Spandan Dasca70fc42023-03-01 23:38:49 +0000230func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
231 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800232}
233
234func (r *RuntimeResourceOverlay) Certificate() Certificate {
235 return r.certificate
236}
237
238func (r *RuntimeResourceOverlay) OutputFile() android.Path {
239 return r.outputFile
240}
241
242func (r *RuntimeResourceOverlay) Theme() string {
243 return String(r.properties.Theme)
244}
245
246// runtime_resource_overlay generates a resource-only apk file that can overlay application and
247// system resources at run time.
248func RuntimeResourceOverlayFactory() android.Module {
249 module := &RuntimeResourceOverlay{}
250 module.AddProperties(
251 &module.properties,
252 &module.aaptProperties,
253 &module.overridableProperties)
254
255 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
256 android.InitDefaultableModule(module)
257 android.InitOverridableModule(module, &module.properties.Overrides)
258 return module
259}
260
261// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
262type OverridableRuntimeResourceOverlayProperties struct {
263 // the package name of this app. The package name in the manifest file is used if one was not given.
264 Package_name *string
265
266 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
267 Target_package_name *string
Jeremy Meyer7e671292022-10-07 18:21:34 +0000268
269 // the rro category of this overlay. The category in the manifest file is used if one was not given.
270 Category *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800271}
272
273type OverrideRuntimeResourceOverlay struct {
274 android.ModuleBase
275 android.OverrideModuleBase
276}
277
278func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
279 // All the overrides happen in the base module.
280 // TODO(jungjw): Check the base module type.
281}
282
283// override_runtime_resource_overlay is used to create a module based on another
284// runtime_resource_overlay module by overriding some of its properties.
285func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
286 m := &OverrideRuntimeResourceOverlay{}
287 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
288
289 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
290 android.InitOverrideModule(m)
291 return m
292}
Spandan Dase2f98da2024-11-18 19:22:39 +0000293
294var (
295 generateOverlayManifestFile = pctx.AndroidStaticRule("generate_overlay_manifest",
296 blueprint.RuleParams{
297 Command: "build/make/tools/generate-enforce-rro-android-manifest.py " +
298 "--package-info $in " +
299 "--partition ${partition} " +
300 "--priority ${priority} -o $out",
301 CommandDeps: []string{"build/make/tools/generate-enforce-rro-android-manifest.py"},
302 }, "partition", "priority",
303 )
304)
305
306type AutogenRuntimeResourceOverlay struct {
307 android.ModuleBase
308 aapt
309
310 properties AutogenRuntimeResourceOverlayProperties
311
Spandan Dasaf8a3f52024-12-08 18:22:45 +0000312 certificate Certificate
313 outputFile android.Path
Spandan Dase2f98da2024-11-18 19:22:39 +0000314}
315
316type AutogenRuntimeResourceOverlayProperties struct {
317 Base *string
318 Sdk_version *string
319 Manifest *string `android:"path"`
320}
321
322func AutogenRuntimeResourceOverlayFactory() android.Module {
323 m := &AutogenRuntimeResourceOverlay{}
324 m.AddProperties(&m.properties)
325 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
326
327 return m
328}
329
330type rroDependencyTag struct {
331 blueprint.DependencyTag
332}
333
334// Autogenerated RROs should always depend on the source android_app that created it.
335func (tag rroDependencyTag) ReplaceSourceWithPrebuilt() bool {
336 return false
337}
338
339var rroDepTag = rroDependencyTag{}
340
341func (a *AutogenRuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
342 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
343 if sdkDep.hasFrameworkLibs() {
344 a.aapt.deps(ctx, sdkDep)
345 }
346 ctx.AddDependency(ctx.Module(), rroDepTag, proptools.String(a.properties.Base))
347}
348
349func (a *AutogenRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
350 if !a.Enabled(ctx) {
351 return
352 }
353 var rroDirs android.Paths
354 // Get rro dirs of the base app
355 ctx.VisitDirectDepsWithTag(rroDepTag, func(m android.Module) {
356 aarDep, _ := m.(AndroidLibraryDependency)
357 if ctx.InstallInProduct() {
358 rroDirs = filterRRO(aarDep.RRODirsDepSet(), product)
359 } else {
360 rroDirs = filterRRO(aarDep.RRODirsDepSet(), device)
361 }
362 })
363
364 if len(rroDirs) == 0 {
365 return
366 }
367
368 // Generate a manifest file
369 genManifest := android.PathForModuleGen(ctx, "AndroidManifest.xml")
370 partition := "vendor"
371 priority := "0"
372 if ctx.InstallInProduct() {
373 partition = "product"
374 priority = "1"
375 }
376 ctx.Build(pctx, android.BuildParams{
377 Rule: generateOverlayManifestFile,
378 Input: android.PathForModuleSrc(ctx, proptools.String(a.properties.Manifest)),
379 Output: genManifest,
380 Args: map[string]string{
381 "partition": partition,
382 "priority": priority,
383 },
384 })
385
386 // Compile and link resources into package-res.apk
387 a.aapt.hasNoCode = true
388 aaptLinkFlags := []string{"--auto-add-overlay", "--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
389
390 a.aapt.buildActions(ctx,
391 aaptBuildActionOptions{
392 sdkContext: a,
393 extraLinkFlags: aaptLinkFlags,
394 rroDirs: &rroDirs,
395 manifestForAapt: genManifest,
396 },
397 )
398
399 if a.exportPackage == nil {
400 return
401 }
402 // Sign the built package
Spandan Dasaf8a3f52024-12-08 18:22:45 +0000403 var certificates []Certificate
404 a.certificate, certificates = processMainCert(a.ModuleBase, "", nil, ctx)
Spandan Dase2f98da2024-11-18 19:22:39 +0000405 signed := android.PathForModuleOut(ctx, "signed", a.Name()+".apk")
406 SignAppPackage(ctx, signed, a.exportPackage, certificates, nil, nil, "")
407 a.outputFile = signed
408
409 // Install the signed apk
410 installDir := android.PathForModuleInstall(ctx, "overlay")
411 ctx.InstallFile(installDir, signed.Base(), signed)
412}
413
414func (a *AutogenRuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
415 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
416}
417
418func (a *AutogenRuntimeResourceOverlay) SystemModules() string {
419 return ""
420}
421
422func (a *AutogenRuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
423 return a.SdkVersion(ctx).ApiLevel
424}
425
426func (r *AutogenRuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
427 return android.SdkSpecPrivate.ApiLevel
428}
429
430func (a *AutogenRuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
431 return a.SdkVersion(ctx).ApiLevel
432}
433
434func (a *AutogenRuntimeResourceOverlay) InstallInProduct() bool {
435 return a.ProductSpecific()
436}