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