| 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 implementations for runtime_resource_overlay and | 
|  | 18 | // override_runtime_resource_overlay. | 
|  | 19 |  | 
| Cole Faust | b749347 | 2024-08-28 11:55:52 -0700 | [diff] [blame] | 20 | import ( | 
|  | 21 | "android/soong/android" | 
|  | 22 |  | 
|  | 23 | "github.com/google/blueprint/proptools" | 
|  | 24 | ) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 25 |  | 
|  | 26 | func init() { | 
|  | 27 | RegisterRuntimeResourceOverlayBuildComponents(android.InitRegistrationContext) | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | func RegisterRuntimeResourceOverlayBuildComponents(ctx android.RegistrationContext) { | 
|  | 31 | ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory) | 
|  | 32 | ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory) | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | type RuntimeResourceOverlay struct { | 
|  | 36 | android.ModuleBase | 
|  | 37 | android.DefaultableModuleBase | 
|  | 38 | android.OverridableModuleBase | 
|  | 39 | aapt | 
|  | 40 |  | 
|  | 41 | properties            RuntimeResourceOverlayProperties | 
|  | 42 | overridableProperties OverridableRuntimeResourceOverlayProperties | 
|  | 43 |  | 
|  | 44 | certificate Certificate | 
|  | 45 |  | 
|  | 46 | outputFile android.Path | 
|  | 47 | installDir android.InstallPath | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | type RuntimeResourceOverlayProperties struct { | 
|  | 51 | // the name of a certificate in the default certificate directory or an android_app_certificate | 
|  | 52 | // module name in the form ":module". | 
|  | 53 | Certificate *string | 
|  | 54 |  | 
|  | 55 | // Name of the signing certificate lineage file. | 
|  | 56 | Lineage *string | 
|  | 57 |  | 
| Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 58 | // For overriding the --rotation-min-sdk-version property of apksig | 
|  | 59 | RotationMinSdkVersion *string | 
|  | 60 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 61 | // optional theme name. If specified, the overlay package will be applied | 
|  | 62 | // only when the ro.boot.vendor.overlay.theme system property is set to the same value. | 
|  | 63 | Theme *string | 
|  | 64 |  | 
| Tobias Thierer | 1b3e949 | 2021-01-02 19:01:16 +0000 | [diff] [blame] | 65 | // If not blank, set to the version of the sdk to compile against. This | 
|  | 66 | // can be either an API version (e.g. "29" for API level 29 AKA Android 10) | 
|  | 67 | // or special subsets of the current platform, for example "none", "current", | 
|  | 68 | // "core", "system", "test". See build/soong/java/sdk.go for the full and | 
|  | 69 | // up-to-date list of possible values. | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 70 | // Defaults to compiling against the current platform. | 
|  | 71 | Sdk_version *string | 
|  | 72 |  | 
|  | 73 | // if not blank, set the minimum version of the sdk that the compiled artifacts will run against. | 
|  | 74 | // Defaults to sdk_version if not set. | 
|  | 75 | Min_sdk_version *string | 
|  | 76 |  | 
|  | 77 | // list of android_library modules whose resources are extracted and linked against statically | 
| Cole Faust | b749347 | 2024-08-28 11:55:52 -0700 | [diff] [blame] | 78 | Static_libs proptools.Configurable[[]string] | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | // list of android_app modules whose resources are extracted and linked against | 
|  | 81 | Resource_libs []string | 
|  | 82 |  | 
|  | 83 | // Names of modules to be overridden. Listed modules can only be other overlays | 
|  | 84 | // (in Make or Soong). | 
|  | 85 | // This does not completely prevent installation of the overridden overlays, but if both | 
|  | 86 | // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed | 
|  | 87 | // from PRODUCT_PACKAGES. | 
|  | 88 | Overrides []string | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | // RuntimeResourceOverlayModule interface is used by the apex package to gather information from | 
|  | 92 | // a RuntimeResourceOverlay module. | 
|  | 93 | type RuntimeResourceOverlayModule interface { | 
|  | 94 | android.Module | 
|  | 95 | OutputFile() android.Path | 
|  | 96 | Certificate() Certificate | 
|  | 97 | Theme() string | 
|  | 98 | } | 
|  | 99 |  | 
| Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 100 | // RRO's partition logic is different from the partition logic of other modules defined in soong/android/paths.go | 
|  | 101 | // The default partition for RRO is "/product" and not "/system" | 
|  | 102 | func rroPartition(ctx android.ModuleContext) string { | 
|  | 103 | var partition string | 
|  | 104 | if ctx.DeviceSpecific() { | 
|  | 105 | partition = ctx.DeviceConfig().OdmPath() | 
|  | 106 | } else if ctx.SocSpecific() { | 
|  | 107 | partition = ctx.DeviceConfig().VendorPath() | 
|  | 108 | } else if ctx.SystemExtSpecific() { | 
|  | 109 | partition = ctx.DeviceConfig().SystemExtPath() | 
|  | 110 | } else { | 
|  | 111 | partition = ctx.DeviceConfig().ProductPath() | 
|  | 112 | } | 
|  | 113 | return partition | 
|  | 114 | } | 
|  | 115 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 116 | func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 117 | sdkDep := decodeSdkDep(ctx, android.SdkContext(r)) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 118 | if sdkDep.hasFrameworkLibs() { | 
|  | 119 | r.aapt.deps(ctx, sdkDep) | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | cert := android.SrcIsModule(String(r.properties.Certificate)) | 
|  | 123 | if cert != "" { | 
|  | 124 | ctx.AddDependency(ctx.Module(), certificateTag, cert) | 
|  | 125 | } | 
|  | 126 |  | 
| Cole Faust | b749347 | 2024-08-28 11:55:52 -0700 | [diff] [blame] | 127 | ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs.GetOrDefault(ctx, nil)...) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 128 | ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...) | 
| Jihoon Kang | 9f442dc | 2024-03-20 22:09:04 +0000 | [diff] [blame] | 129 |  | 
|  | 130 | for _, aconfig_declaration := range r.aaptProperties.Flags_packages { | 
|  | 131 | ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration) | 
|  | 132 | } | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
|  | 135 | func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 136 | // Compile and link resources | 
|  | 137 | r.aapt.hasNoCode = true | 
|  | 138 | // Do not remove resources without default values nor dedupe resource configurations with the same value | 
|  | 139 | aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"} | 
|  | 140 | // Allow the override of "package name" and "overlay target package name" | 
|  | 141 | manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName()) | 
|  | 142 | if overridden || r.overridableProperties.Package_name != nil { | 
|  | 143 | // The product override variable has a priority over the package_name property. | 
|  | 144 | if !overridden { | 
|  | 145 | manifestPackageName = *r.overridableProperties.Package_name | 
|  | 146 | } | 
|  | 147 | aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...) | 
|  | 148 | } | 
|  | 149 | if r.overridableProperties.Target_package_name != nil { | 
|  | 150 | aaptLinkFlags = append(aaptLinkFlags, | 
|  | 151 | "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name) | 
|  | 152 | } | 
| Jeremy Meyer | 7e67129 | 2022-10-07 18:21:34 +0000 | [diff] [blame] | 153 | if r.overridableProperties.Category != nil { | 
|  | 154 | aaptLinkFlags = append(aaptLinkFlags, | 
|  | 155 | "--rename-overlay-category "+*r.overridableProperties.Category) | 
|  | 156 | } | 
| Jihoon Kang | 9aef777 | 2024-06-14 23:45:06 +0000 | [diff] [blame] | 157 | aconfigTextFilePaths := getAconfigFilePaths(ctx) | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 158 | r.aapt.buildActions(ctx, | 
|  | 159 | aaptBuildActionOptions{ | 
|  | 160 | sdkContext:                     r, | 
|  | 161 | enforceDefaultTargetSdkVersion: false, | 
|  | 162 | extraLinkFlags:                 aaptLinkFlags, | 
| Jihoon Kang | 9aef777 | 2024-06-14 23:45:06 +0000 | [diff] [blame] | 163 | aconfigTextFiles:               aconfigTextFilePaths, | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 164 | }, | 
|  | 165 | ) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 166 |  | 
|  | 167 | // Sign the built package | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 168 | _, _, certificates := collectAppDeps(ctx, r, false, false) | 
| Colin Cross | bc2c8a7 | 2022-09-14 12:45:42 -0700 | [diff] [blame] | 169 | r.certificate, certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 170 | signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk") | 
|  | 171 | var lineageFile android.Path | 
|  | 172 | if lineage := String(r.properties.Lineage); lineage != "" { | 
|  | 173 | lineageFile = android.PathForModuleSrc(ctx, lineage) | 
|  | 174 | } | 
| Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 175 |  | 
|  | 176 | rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion) | 
|  | 177 |  | 
|  | 178 | SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 179 |  | 
|  | 180 | r.outputFile = signed | 
| Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 181 | partition := rroPartition(ctx) | 
|  | 182 | r.installDir = android.PathForModuleInPartitionInstall(ctx, partition, "overlay", String(r.properties.Theme)) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 183 | ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile) | 
| Jihoon Kang | 9aef777 | 2024-06-14 23:45:06 +0000 | [diff] [blame] | 184 |  | 
|  | 185 | android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{ | 
|  | 186 | AconfigTextFiles: aconfigTextFilePaths, | 
|  | 187 | }) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 190 | func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { | 
|  | 191 | return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version)) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 194 | func (r *RuntimeResourceOverlay) SystemModules() string { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 195 | return "" | 
|  | 196 | } | 
|  | 197 |  | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 198 | func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 199 | if r.properties.Min_sdk_version != nil { | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 200 | return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 201 | } | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 202 | return r.SdkVersion(ctx).ApiLevel | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 205 | func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel { | 
|  | 206 | return android.SdkSpecPrivate.ApiLevel | 
| William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Spandan Das | ca70fc4 | 2023-03-01 23:38:49 +0000 | [diff] [blame] | 209 | func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { | 
|  | 210 | return r.SdkVersion(ctx).ApiLevel | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
|  | 213 | func (r *RuntimeResourceOverlay) Certificate() Certificate { | 
|  | 214 | return r.certificate | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | func (r *RuntimeResourceOverlay) OutputFile() android.Path { | 
|  | 218 | return r.outputFile | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | func (r *RuntimeResourceOverlay) Theme() string { | 
|  | 222 | return String(r.properties.Theme) | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | // runtime_resource_overlay generates a resource-only apk file that can overlay application and | 
|  | 226 | // system resources at run time. | 
|  | 227 | func RuntimeResourceOverlayFactory() android.Module { | 
|  | 228 | module := &RuntimeResourceOverlay{} | 
|  | 229 | module.AddProperties( | 
|  | 230 | &module.properties, | 
|  | 231 | &module.aaptProperties, | 
|  | 232 | &module.overridableProperties) | 
|  | 233 |  | 
|  | 234 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
|  | 235 | android.InitDefaultableModule(module) | 
|  | 236 | android.InitOverridableModule(module, &module.properties.Overrides) | 
|  | 237 | return module | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | // runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay | 
|  | 241 | type OverridableRuntimeResourceOverlayProperties struct { | 
|  | 242 | // the package name of this app. The package name in the manifest file is used if one was not given. | 
|  | 243 | Package_name *string | 
|  | 244 |  | 
|  | 245 | // the target package name of this overlay app. The target package name in the manifest file is used if one was not given. | 
|  | 246 | Target_package_name *string | 
| Jeremy Meyer | 7e67129 | 2022-10-07 18:21:34 +0000 | [diff] [blame] | 247 |  | 
|  | 248 | // the rro category of this overlay. The category in the manifest file is used if one was not given. | 
|  | 249 | Category *string | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
|  | 252 | type OverrideRuntimeResourceOverlay struct { | 
|  | 253 | android.ModuleBase | 
|  | 254 | android.OverrideModuleBase | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) { | 
|  | 258 | // All the overrides happen in the base module. | 
|  | 259 | // TODO(jungjw): Check the base module type. | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | // override_runtime_resource_overlay is used to create a module based on another | 
|  | 263 | // runtime_resource_overlay module by overriding some of its properties. | 
|  | 264 | func OverrideRuntimeResourceOverlayModuleFactory() android.Module { | 
|  | 265 | m := &OverrideRuntimeResourceOverlay{} | 
|  | 266 | m.AddProperties(&OverridableRuntimeResourceOverlayProperties{}) | 
|  | 267 |  | 
|  | 268 | android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon) | 
|  | 269 | android.InitOverrideModule(m) | 
|  | 270 | return m | 
|  | 271 | } |