Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 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 apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 19 | "path/filepath" |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 20 | "strconv" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 21 | "strings" |
| 22 | |
| 23 | "android/soong/android" |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 24 | "android/soong/java" |
| 25 | "github.com/google/blueprint" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 26 | |
| 27 | "github.com/google/blueprint/proptools" |
| 28 | ) |
| 29 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 30 | var ( |
| 31 | extractMatchingApex = pctx.StaticRule( |
| 32 | "extractMatchingApex", |
| 33 | blueprint.RuleParams{ |
| 34 | Command: `rm -rf "$out" && ` + |
| 35 | `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` + |
| 36 | `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` + |
| 37 | `${in}`, |
| 38 | CommandDeps: []string{"${extract_apks}"}, |
| 39 | }, |
| 40 | "abis", "allow-prereleased", "sdk-version") |
| 41 | ) |
| 42 | |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 43 | type prebuilt interface { |
| 44 | isForceDisabled() bool |
| 45 | InstallFilename() string |
| 46 | } |
| 47 | |
| 48 | type prebuiltCommon struct { |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 49 | prebuilt android.Prebuilt |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 50 | |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 51 | // Properties common to both prebuilt_apex and apex_set. |
| 52 | prebuiltCommonProperties prebuiltCommonProperties |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 55 | type sanitizedPrebuilt interface { |
| 56 | hasSanitizedSource(sanitizer string) bool |
| 57 | } |
| 58 | |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 59 | type prebuiltCommonProperties struct { |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 60 | SelectedApexProperties |
| 61 | |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 62 | ForceDisable bool `blueprint:"mutated"` |
Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 63 | |
| 64 | // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this |
| 65 | // APEX bundle will create an APEX variant and provide dex implementation jars for use by |
| 66 | // dexpreopt and boot jars package check. |
| 67 | Exported_java_libs []string |
| 68 | |
| 69 | // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX |
| 70 | // bundle will create an APEX variant. |
| 71 | Exported_bootclasspath_fragments []string |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func (p *prebuiltCommon) Prebuilt() *android.Prebuilt { |
| 75 | return &p.prebuilt |
| 76 | } |
| 77 | |
| 78 | func (p *prebuiltCommon) isForceDisabled() bool { |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 79 | return p.prebuiltCommonProperties.ForceDisable |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool { |
| 83 | // If the device is configured to use flattened APEX, force disable the prebuilt because |
| 84 | // the prebuilt is a non-flattened one. |
| 85 | forceDisable := ctx.Config().FlattenApex() |
| 86 | |
| 87 | // Force disable the prebuilts when we are doing unbundled build. We do unbundled build |
| 88 | // to build the prebuilts themselves. |
| 89 | forceDisable = forceDisable || ctx.Config().UnbundledBuild() |
| 90 | |
| 91 | // Force disable the prebuilts when coverage is enabled. |
| 92 | forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled() |
| 93 | forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") |
| 94 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 95 | // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source |
| 96 | sanitized := ctx.Module().(sanitizedPrebuilt) |
| 97 | forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address")) |
| 98 | forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress")) |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 99 | |
| 100 | if forceDisable && p.prebuilt.SourceExists() { |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 101 | p.prebuiltCommonProperties.ForceDisable = true |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 102 | return true |
| 103 | } |
| 104 | return false |
| 105 | } |
| 106 | |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 107 | // prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and |
| 108 | // apex_set in order to create the modules needed to provide access to the prebuilt .apex file. |
| 109 | type prebuiltApexModuleCreator interface { |
| 110 | createPrebuiltApexModules(ctx android.TopDownMutatorContext) |
| 111 | } |
| 112 | |
| 113 | // prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the |
| 114 | // prebuiltApexModuleCreator's createPrebuiltApexModules method. |
| 115 | // |
| 116 | // It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it |
| 117 | // will need to access dependencies added by that (exported modules) but must run before the |
| 118 | // DepsMutator so that the deapexer module it creates can add dependencies onto itself from the |
| 119 | // exported modules. |
| 120 | func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) { |
| 121 | module := ctx.Module() |
| 122 | if creator, ok := module.(prebuiltApexModuleCreator); ok { |
| 123 | creator.createPrebuiltApexModules(ctx) |
| 124 | } |
| 125 | } |
| 126 | |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 127 | // prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents. |
| 128 | func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) { |
| 129 | module := ctx.Module() |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 130 | // Add dependencies onto the java modules that represent the java libraries that are provided by |
| 131 | // and exported from this prebuilt apex. |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 132 | for _, exported := range p.prebuiltCommonProperties.Exported_java_libs { |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 133 | dep := android.PrebuiltNameFromSource(exported) |
| 134 | ctx.AddDependency(module, exportedJavaLibTag, dep) |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 135 | } |
Paul Duffin | 023dba0 | 2021-04-22 01:45:29 +0100 | [diff] [blame] | 136 | |
| 137 | // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt |
| 138 | // apex. |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 139 | for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments { |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 140 | dep := android.PrebuiltNameFromSource(exported) |
| 141 | ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep) |
Paul Duffin | 023dba0 | 2021-04-22 01:45:29 +0100 | [diff] [blame] | 142 | } |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Paul Duffin | b17d044 | 2021-05-05 12:07:00 +0100 | [diff] [blame] | 145 | // Implements android.DepInInSameApex |
| 146 | func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { |
| 147 | tag := ctx.OtherModuleDependencyTag(dep) |
| 148 | _, ok := tag.(exportedDependencyTag) |
| 149 | return ok |
| 150 | } |
| 151 | |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 152 | // apexInfoMutator marks any modules for which this apex exports a file as requiring an apex |
| 153 | // specific variant and checks that they are supported. |
| 154 | // |
| 155 | // The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are |
| 156 | // associated with the apex specific variant using the ApexInfoProvider for later retrieval. |
| 157 | // |
| 158 | // Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants |
| 159 | // across prebuilt_apex modules. That is because there is no way to determine whether two |
| 160 | // prebuilt_apex modules that export files for the same module are compatible. e.g. they could have |
| 161 | // been built from different source at different times or they could have been built with different |
| 162 | // build options that affect the libraries. |
| 163 | // |
| 164 | // While it may be possible to provide sufficient information to determine whether two prebuilt_apex |
| 165 | // modules were compatible it would be a lot of work and would not provide much benefit for a couple |
| 166 | // of reasons: |
| 167 | // * The number of prebuilt_apex modules that will be exporting files for the same module will be |
| 168 | // low as the prebuilt_apex only exports files for the direct dependencies that require it and |
| 169 | // very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a |
| 170 | // few com.android.art* apex files that contain the same contents and could export files for the |
| 171 | // same modules but only one of them needs to do so. Contrast that with source apex modules which |
| 172 | // need apex specific variants for every module that contributes code to the apex, whether direct |
| 173 | // or indirect. |
| 174 | // * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some |
| 175 | // extra copying of files. Contrast that with source apex modules that has to build each variant |
| 176 | // from source. |
| 177 | func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) { |
| 178 | |
| 179 | // Collect direct dependencies into contents. |
| 180 | contents := make(map[string]android.ApexMembership) |
| 181 | |
| 182 | // Collect the list of dependencies. |
| 183 | var dependencies []android.ApexModule |
Paul Duffin | b17d044 | 2021-05-05 12:07:00 +0100 | [diff] [blame] | 184 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 185 | // If the child is not in the same apex as the parent then exit immediately and do not visit |
| 186 | // any of the child's dependencies. |
| 187 | if !android.IsDepInSameApex(mctx, parent, child) { |
| 188 | return false |
| 189 | } |
| 190 | |
| 191 | tag := mctx.OtherModuleDependencyTag(child) |
| 192 | depName := mctx.OtherModuleName(child) |
Paul Duffin | 023dba0 | 2021-04-22 01:45:29 +0100 | [diff] [blame] | 193 | if exportedTag, ok := tag.(exportedDependencyTag); ok { |
| 194 | propertyName := exportedTag.name |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 195 | |
| 196 | // It is an error if the other module is not a prebuilt. |
Paul Duffin | b17d044 | 2021-05-05 12:07:00 +0100 | [diff] [blame] | 197 | if !android.IsModulePrebuilt(child) { |
Paul Duffin | 023dba0 | 2021-04-22 01:45:29 +0100 | [diff] [blame] | 198 | mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName) |
Paul Duffin | b17d044 | 2021-05-05 12:07:00 +0100 | [diff] [blame] | 199 | return false |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // It is an error if the other module is not an ApexModule. |
Paul Duffin | b17d044 | 2021-05-05 12:07:00 +0100 | [diff] [blame] | 203 | if _, ok := child.(android.ApexModule); !ok { |
Paul Duffin | 023dba0 | 2021-04-22 01:45:29 +0100 | [diff] [blame] | 204 | mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName) |
Paul Duffin | b17d044 | 2021-05-05 12:07:00 +0100 | [diff] [blame] | 205 | return false |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 206 | } |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 207 | } |
Paul Duffin | b17d044 | 2021-05-05 12:07:00 +0100 | [diff] [blame] | 208 | |
| 209 | // Strip off the prebuilt_ prefix if present before storing content to ensure consistent |
| 210 | // behavior whether there is a corresponding source module present or not. |
| 211 | depName = android.RemoveOptionalPrebuiltPrefix(depName) |
| 212 | |
| 213 | // Remember if this module was added as a direct dependency. |
| 214 | direct := parent == mctx.Module() |
| 215 | contents[depName] = contents[depName].Add(direct) |
| 216 | |
| 217 | // Add the module to the list of dependencies that need to have an APEX variant. |
| 218 | dependencies = append(dependencies, child.(android.ApexModule)) |
| 219 | |
| 220 | return true |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 221 | }) |
| 222 | |
| 223 | // Create contents for the prebuilt_apex and store it away for later use. |
| 224 | apexContents := android.NewApexContents(contents) |
| 225 | mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{ |
| 226 | Contents: apexContents, |
| 227 | }) |
| 228 | |
| 229 | // Create an ApexInfo for the prebuilt_apex. |
Martin Stjernholm | c4f4ced | 2021-05-27 11:17:00 +0000 | [diff] [blame] | 230 | apexVariationName := android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()) |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 231 | apexInfo := android.ApexInfo{ |
Martin Stjernholm | c4f4ced | 2021-05-27 11:17:00 +0000 | [diff] [blame] | 232 | ApexVariationName: apexVariationName, |
| 233 | InApexVariants: []string{apexVariationName}, |
| 234 | InApexModules: []string{apexVariationName}, |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 235 | ApexContents: []*android.ApexContents{apexContents}, |
| 236 | ForPrebuiltApex: true, |
| 237 | } |
| 238 | |
| 239 | // Mark the dependencies of this module as requiring a variant for this module. |
| 240 | for _, am := range dependencies { |
| 241 | am.BuildForApex(apexInfo) |
| 242 | } |
| 243 | } |
| 244 | |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 245 | // prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex |
| 246 | // module. It selects the apex to use and makes it available for use by prebuilt_apex and the |
| 247 | // deapexer. |
| 248 | type prebuiltApexSelectorModule struct { |
| 249 | android.ModuleBase |
| 250 | |
| 251 | apexFileProperties ApexFileProperties |
| 252 | |
| 253 | inputApex android.Path |
| 254 | } |
| 255 | |
| 256 | func privateApexSelectorModuleFactory() android.Module { |
| 257 | module := &prebuiltApexSelectorModule{} |
| 258 | module.AddProperties( |
| 259 | &module.apexFileProperties, |
| 260 | ) |
| 261 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 262 | return module |
| 263 | } |
| 264 | |
| 265 | func (p *prebuiltApexSelectorModule) Srcs() android.Paths { |
| 266 | return android.Paths{p.inputApex} |
| 267 | } |
| 268 | |
| 269 | func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 270 | p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src") |
| 271 | } |
| 272 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 273 | type Prebuilt struct { |
| 274 | android.ModuleBase |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 275 | prebuiltCommon |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 276 | |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 277 | properties PrebuiltProperties |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 278 | |
| 279 | inputApex android.Path |
| 280 | installDir android.InstallPath |
| 281 | installFilename string |
| 282 | outputApex android.WritablePath |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 283 | |
| 284 | // list of commands to create symlinks for backward compatibility. |
| 285 | // these commands will be attached as LOCAL_POST_INSTALL_CMD |
| 286 | compatSymlinks []string |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 289 | type ApexFileProperties struct { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 290 | // the path to the prebuilt .apex file to import. |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 291 | // |
| 292 | // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated |
| 293 | // for android_common. That is so that it will have the same arch variant as, and so be compatible |
| 294 | // with, the source `apex` module type that it replaces. |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 295 | Src *string `android:"path"` |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 296 | Arch struct { |
| 297 | Arm struct { |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 298 | Src *string `android:"path"` |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 299 | } |
| 300 | Arm64 struct { |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 301 | Src *string `android:"path"` |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 302 | } |
| 303 | X86 struct { |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 304 | Src *string `android:"path"` |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 305 | } |
| 306 | X86_64 struct { |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 307 | Src *string `android:"path"` |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 308 | } |
| 309 | } |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 312 | // prebuiltApexSelector selects the correct prebuilt APEX file for the build target. |
| 313 | // |
| 314 | // The ctx parameter can be for any module not just the prebuilt module so care must be taken not |
| 315 | // to use methods on it that are specific to the current module. |
| 316 | // |
| 317 | // See the ApexFileProperties.Src property. |
| 318 | func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string { |
| 319 | multiTargets := prebuilt.MultiTargets() |
| 320 | if len(multiTargets) != 1 { |
| 321 | ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex") |
| 322 | return nil |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 323 | } |
| 324 | var src string |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 325 | switch multiTargets[0].Arch.ArchType { |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 326 | case android.Arm: |
| 327 | src = String(p.Arch.Arm.Src) |
| 328 | case android.Arm64: |
| 329 | src = String(p.Arch.Arm64.Src) |
| 330 | case android.X86: |
| 331 | src = String(p.Arch.X86.Src) |
| 332 | case android.X86_64: |
| 333 | src = String(p.Arch.X86_64.Src) |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 334 | } |
| 335 | if src == "" { |
| 336 | src = String(p.Src) |
| 337 | } |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 338 | |
Paul Duffin | c0609c6 | 2021-03-01 17:27:16 +0000 | [diff] [blame] | 339 | if src == "" { |
| 340 | ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String()) |
| 341 | // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt |
| 342 | // logic from reporting a more general, less useful message. |
| 343 | } |
| 344 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 345 | return []string{src} |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | type PrebuiltProperties struct { |
| 349 | ApexFileProperties |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 350 | |
| 351 | Installable *bool |
| 352 | // Optional name for the installed apex. If unspecified, name of the |
| 353 | // module is used as the file name |
| 354 | Filename *string |
| 355 | |
| 356 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 357 | // (in Make or Soong). |
| 358 | // This does not completely prevent installation of the overridden binaries, but if both |
| 359 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 360 | // from PRODUCT_PACKAGES. |
| 361 | Overrides []string |
| 362 | } |
| 363 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 364 | func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool { |
| 365 | return false |
| 366 | } |
| 367 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 368 | func (p *Prebuilt) installable() bool { |
| 369 | return p.properties.Installable == nil || proptools.Bool(p.properties.Installable) |
| 370 | } |
| 371 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 372 | func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) { |
| 373 | switch tag { |
| 374 | case "": |
| 375 | return android.Paths{p.outputApex}, nil |
| 376 | default: |
| 377 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | func (p *Prebuilt) InstallFilename() string { |
| 382 | return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix) |
| 383 | } |
| 384 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 385 | func (p *Prebuilt) Name() string { |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 386 | return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex. |
| 390 | func PrebuiltFactory() android.Module { |
| 391 | module := &Prebuilt{} |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 392 | module.AddProperties(&module.properties, &module.prebuiltCommonProperties) |
| 393 | android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 394 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 395 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 396 | return module |
| 397 | } |
| 398 | |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 399 | func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) { |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 400 | props := struct { |
| 401 | Name *string |
| 402 | }{ |
| 403 | Name: proptools.StringPtr(name), |
| 404 | } |
| 405 | |
| 406 | ctx.CreateModule(privateApexSelectorModuleFactory, |
| 407 | &props, |
| 408 | apexFileProperties, |
| 409 | ) |
| 410 | } |
| 411 | |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 412 | // createDeapexerModuleIfNeeded will create a deapexer module if it is needed. |
| 413 | // |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 414 | // A deapexer module is only needed when the prebuilt apex specifies one or more modules in either |
| 415 | // the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that |
| 416 | // the listed modules need access to files from within the prebuilt .apex file. |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 417 | func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *prebuiltCommonProperties) { |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 418 | // Only create the deapexer module if it is needed. |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 419 | if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 { |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 420 | return |
| 421 | } |
| 422 | |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 423 | // Compute the deapexer properties from the transitive dependencies of this module. |
Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 424 | javaModules := []string{} |
| 425 | exportedFiles := map[string]string{} |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 426 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 427 | tag := ctx.OtherModuleDependencyTag(child) |
| 428 | |
| 429 | name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child)) |
| 430 | if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag { |
Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 431 | javaModules = append(javaModules, name) |
| 432 | |
| 433 | // Add the dex implementation jar to the set of exported files. The path here must match the |
| 434 | // path of the file in the APEX created by apexFileForJavaModule(...). |
| 435 | exportedFiles[name+"{.dexjar}"] = filepath.Join("javalib", name+".jar") |
| 436 | |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 437 | } else if tag == exportedBootclasspathFragmentTag { |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 438 | // Only visit the children of the bootclasspath_fragment for now. |
| 439 | return true |
| 440 | } |
| 441 | |
| 442 | return false |
| 443 | }) |
| 444 | |
Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 445 | // Create properties for deapexer module. |
| 446 | deapexerProperties := &DeapexerProperties{ |
| 447 | // Remove any duplicates from the java modules lists as a module may be included via a direct |
| 448 | // dependency as well as transitive ones. |
| 449 | CommonModules: android.SortedUniqueStrings(javaModules), |
| 450 | } |
| 451 | |
| 452 | // Populate the exported files property in a fixed order. |
| 453 | for _, tag := range android.SortedStringKeys(exportedFiles) { |
| 454 | deapexerProperties.ExportedFiles = append(deapexerProperties.ExportedFiles, DeapexerExportedFile{ |
| 455 | Tag: tag, |
| 456 | Path: exportedFiles[tag], |
| 457 | }) |
| 458 | } |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 459 | |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 460 | props := struct { |
| 461 | Name *string |
| 462 | Selected_apex *string |
| 463 | }{ |
| 464 | Name: proptools.StringPtr(deapexerName), |
| 465 | Selected_apex: proptools.StringPtr(apexFileSource), |
| 466 | } |
| 467 | ctx.CreateModule(privateDeapexerFactory, |
| 468 | &props, |
| 469 | deapexerProperties, |
| 470 | ) |
| 471 | } |
| 472 | |
| 473 | func deapexerModuleName(baseModuleName string) string { |
| 474 | return baseModuleName + ".deapexer" |
| 475 | } |
| 476 | |
| 477 | func apexSelectorModuleName(baseModuleName string) string { |
| 478 | return baseModuleName + ".apex.selector" |
| 479 | } |
| 480 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 481 | func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string { |
| 482 | // The prebuilt_apex should be depending on prebuilt modules but as this runs after |
| 483 | // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So, |
| 484 | // check to see if the prefixed name is in use first, if it is then use that, otherwise assume |
| 485 | // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module |
| 486 | // and not a renamed prebuilt module then that will be detected and reported as an error when |
| 487 | // processing the dependency in ApexInfoMutator(). |
Paul Duffin | 864116c | 2021-04-02 10:24:13 +0100 | [diff] [blame] | 488 | prebuiltName := android.PrebuiltNameFromSource(name) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 489 | if ctx.OtherModuleExists(prebuiltName) { |
| 490 | name = prebuiltName |
| 491 | } |
| 492 | return name |
| 493 | } |
| 494 | |
Paul Duffin | a713942 | 2021-02-08 11:01:58 +0000 | [diff] [blame] | 495 | type exportedDependencyTag struct { |
| 496 | blueprint.BaseDependencyTag |
| 497 | name string |
| 498 | } |
| 499 | |
| 500 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 501 | // |
| 502 | // This does allow any prebuilt_apex to reference any module which does open up a small window for |
| 503 | // restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so |
| 504 | // avoids opening up a much bigger window by widening the visibility of modules that need files |
| 505 | // provided by the prebuilt_apex to include all the possible locations they may be defined, which |
| 506 | // could include everything below vendor/. |
| 507 | // |
| 508 | // A prebuilt_apex that references a module via this tag will have to contain the appropriate files |
| 509 | // corresponding to that module, otherwise it will fail when attempting to retrieve the files from |
| 510 | // the .apex file. It will also have to be included in the module's apex_available property too. |
| 511 | // That makes it highly unlikely that a prebuilt_apex would reference a restricted module |
| 512 | // incorrectly. |
| 513 | func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {} |
| 514 | |
| 515 | var ( |
Paul Duffin | 023dba0 | 2021-04-22 01:45:29 +0100 | [diff] [blame] | 516 | exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"} |
| 517 | exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"} |
Paul Duffin | a713942 | 2021-02-08 11:01:58 +0000 | [diff] [blame] | 518 | ) |
| 519 | |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 520 | var _ prebuiltApexModuleCreator = (*Prebuilt)(nil) |
| 521 | |
| 522 | // createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the |
| 523 | // build. |
| 524 | // |
| 525 | // If this needs to make files from within a `.apex` file available for use by other Soong modules, |
| 526 | // e.g. make dex implementation jars available for java_import modules listed in exported_java_libs, |
| 527 | // it does so as follows: |
| 528 | // |
| 529 | // 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and |
| 530 | // makes them available for use by other modules, at both Soong and ninja levels. |
| 531 | // |
| 532 | // 2. It adds a dependency onto those modules and creates an apex specific variant similar to what |
| 533 | // an `apex` module does. That ensures that code which looks for specific apex variant, e.g. |
| 534 | // dexpreopt, will work the same way from source and prebuilt. |
| 535 | // |
| 536 | // 3. The `deapexer` module adds a dependency from the modules that require the exported files onto |
| 537 | // itself so that they can retrieve the file paths to those files. |
| 538 | // |
| 539 | // It also creates a child module `selector` that is responsible for selecting the appropriate |
| 540 | // input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons: |
| 541 | // 1. To dedup the selection logic so it only runs in one module. |
| 542 | // 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an |
| 543 | // `apex_set`. |
| 544 | // |
| 545 | // prebuilt_apex |
| 546 | // / | \ |
| 547 | // / | \ |
| 548 | // V V V |
| 549 | // selector <--- deapexer <--- exported java lib |
| 550 | // |
| 551 | func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) { |
| 552 | baseModuleName := p.BaseModuleName() |
| 553 | |
| 554 | apexSelectorModuleName := apexSelectorModuleName(baseModuleName) |
| 555 | createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties) |
| 556 | |
| 557 | apexFileSource := ":" + apexSelectorModuleName |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 558 | createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &p.prebuiltCommonProperties) |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 559 | |
| 560 | // Add a source reference to retrieve the selected apex from the selector module. |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 561 | p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource) |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 564 | func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) { |
| 565 | p.prebuiltApexContentsDeps(ctx) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | var _ ApexInfoMutator = (*Prebuilt)(nil) |
| 569 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 570 | func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) { |
Paul Duffin | dfd3326 | 2021-04-06 17:02:08 +0100 | [diff] [blame] | 571 | p.apexInfoMutator(mctx) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 575 | // TODO(jungjw): Check the key validity. |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 576 | p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path() |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 577 | p.installDir = android.PathForModuleInstall(ctx, "apex") |
| 578 | p.installFilename = p.InstallFilename() |
| 579 | if !strings.HasSuffix(p.installFilename, imageApexSuffix) { |
| 580 | ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix) |
| 581 | } |
| 582 | p.outputApex = android.PathForModuleOut(ctx, p.installFilename) |
| 583 | ctx.Build(pctx, android.BuildParams{ |
| 584 | Rule: android.Cp, |
| 585 | Input: p.inputApex, |
| 586 | Output: p.outputApex, |
| 587 | }) |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 588 | |
| 589 | if p.prebuiltCommon.checkForceDisable(ctx) { |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 590 | p.HideFromMake() |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 591 | return |
| 592 | } |
| 593 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 594 | if p.installable() { |
| 595 | ctx.InstallFile(p.installDir, p.installFilename, p.inputApex) |
| 596 | } |
| 597 | |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 598 | // in case that prebuilt_apex replaces source apex (using prefer: prop) |
| 599 | p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx) |
| 600 | // or that prebuilt_apex overrides other apexes (using overrides: prop) |
| 601 | for _, overridden := range p.properties.Overrides { |
| 602 | p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...) |
| 603 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 604 | } |
| 605 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 606 | func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries { |
| 607 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 608 | Class: "ETC", |
| 609 | OutputFile: android.OptionalPathForPath(p.inputApex), |
| 610 | Include: "$(BUILD_PREBUILT)", |
| 611 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | f79fee8 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 612 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 613 | entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String()) |
| 614 | entries.SetString("LOCAL_MODULE_STEM", p.installFilename) |
| 615 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable()) |
| 616 | entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...) |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 617 | if len(p.compatSymlinks) > 0 { |
| 618 | entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && ")) |
| 619 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 620 | }, |
| 621 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 622 | }} |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 623 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 624 | |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 625 | // prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex |
| 626 | // module. It extracts the correct apex to use and makes it available for use by apex_set. |
| 627 | type prebuiltApexExtractorModule struct { |
| 628 | android.ModuleBase |
| 629 | |
| 630 | properties ApexExtractorProperties |
| 631 | |
| 632 | extractedApex android.WritablePath |
| 633 | } |
| 634 | |
| 635 | func privateApexExtractorModuleFactory() android.Module { |
| 636 | module := &prebuiltApexExtractorModule{} |
| 637 | module.AddProperties( |
| 638 | &module.properties, |
| 639 | ) |
| 640 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 641 | return module |
| 642 | } |
| 643 | |
| 644 | func (p *prebuiltApexExtractorModule) Srcs() android.Paths { |
| 645 | return android.Paths{p.extractedApex} |
| 646 | } |
| 647 | |
| 648 | func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 649 | srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string { |
| 650 | return p.properties.prebuiltSrcs(ctx) |
| 651 | } |
| 652 | apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set") |
| 653 | p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base()) |
| 654 | ctx.Build(pctx, |
| 655 | android.BuildParams{ |
| 656 | Rule: extractMatchingApex, |
| 657 | Description: "Extract an apex from an apex set", |
| 658 | Inputs: android.Paths{apexSet}, |
| 659 | Output: p.extractedApex, |
| 660 | Args: map[string]string{ |
| 661 | "abis": strings.Join(java.SupportedAbis(ctx), ","), |
| 662 | "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)), |
| 663 | "sdk-version": ctx.Config().PlatformSdkVersion().String(), |
| 664 | }, |
| 665 | }) |
| 666 | } |
| 667 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 668 | type ApexSet struct { |
| 669 | android.ModuleBase |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 670 | prebuiltCommon |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 671 | |
| 672 | properties ApexSetProperties |
| 673 | |
| 674 | installDir android.InstallPath |
| 675 | installFilename string |
| 676 | outputApex android.WritablePath |
| 677 | |
| 678 | // list of commands to create symlinks for backward compatibility. |
| 679 | // these commands will be attached as LOCAL_POST_INSTALL_CMD |
| 680 | compatSymlinks []string |
| 681 | } |
| 682 | |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 683 | type ApexExtractorProperties struct { |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 684 | // the .apks file path that contains prebuilt apex files to be extracted. |
| 685 | Set *string |
| 686 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 687 | Sanitized struct { |
| 688 | None struct { |
| 689 | Set *string |
| 690 | } |
| 691 | Address struct { |
| 692 | Set *string |
| 693 | } |
| 694 | Hwaddress struct { |
| 695 | Set *string |
| 696 | } |
| 697 | } |
| 698 | |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 699 | // apexes in this set use prerelease SDK version |
| 700 | Prerelease *bool |
| 701 | } |
| 702 | |
| 703 | func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string { |
| 704 | var srcs []string |
| 705 | if e.Set != nil { |
| 706 | srcs = append(srcs, *e.Set) |
| 707 | } |
| 708 | |
| 709 | var sanitizers []string |
| 710 | if ctx.Host() { |
| 711 | sanitizers = ctx.Config().SanitizeHost() |
| 712 | } else { |
| 713 | sanitizers = ctx.Config().SanitizeDevice() |
| 714 | } |
| 715 | |
| 716 | if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil { |
| 717 | srcs = append(srcs, *e.Sanitized.Address.Set) |
| 718 | } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil { |
| 719 | srcs = append(srcs, *e.Sanitized.Hwaddress.Set) |
| 720 | } else if e.Sanitized.None.Set != nil { |
| 721 | srcs = append(srcs, *e.Sanitized.None.Set) |
| 722 | } |
| 723 | |
| 724 | return srcs |
| 725 | } |
| 726 | |
| 727 | type ApexSetProperties struct { |
| 728 | ApexExtractorProperties |
| 729 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 730 | // whether the extracted apex file installable. |
| 731 | Installable *bool |
| 732 | |
| 733 | // optional name for the installed apex. If unspecified, name of the |
| 734 | // module is used as the file name |
| 735 | Filename *string |
| 736 | |
| 737 | // names of modules to be overridden. Listed modules can only be other binaries |
| 738 | // (in Make or Soong). |
| 739 | // This does not completely prevent installation of the overridden binaries, but if both |
| 740 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 741 | // from PRODUCT_PACKAGES. |
| 742 | Overrides []string |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | func (a *ApexSet) hasSanitizedSource(sanitizer string) bool { |
| 746 | if sanitizer == "address" { |
| 747 | return a.properties.Sanitized.Address.Set != nil |
| 748 | } |
| 749 | if sanitizer == "hwaddress" { |
| 750 | return a.properties.Sanitized.Hwaddress.Set != nil |
| 751 | } |
| 752 | |
| 753 | return false |
| 754 | } |
| 755 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 756 | func (a *ApexSet) installable() bool { |
| 757 | return a.properties.Installable == nil || proptools.Bool(a.properties.Installable) |
| 758 | } |
| 759 | |
| 760 | func (a *ApexSet) InstallFilename() string { |
| 761 | return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix) |
| 762 | } |
| 763 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 764 | func (a *ApexSet) Name() string { |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 765 | return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name()) |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 768 | func (a *ApexSet) Overrides() []string { |
| 769 | return a.properties.Overrides |
| 770 | } |
| 771 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 772 | // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex. |
| 773 | func apexSetFactory() android.Module { |
| 774 | module := &ApexSet{} |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 775 | module.AddProperties(&module.properties, &module.prebuiltCommonProperties) |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 776 | |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 777 | android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex") |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 778 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 779 | |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 780 | return module |
| 781 | } |
| 782 | |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 783 | func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) { |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 784 | props := struct { |
| 785 | Name *string |
| 786 | }{ |
| 787 | Name: proptools.StringPtr(name), |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 790 | ctx.CreateModule(privateApexExtractorModuleFactory, |
| 791 | &props, |
| 792 | apexExtractorProperties, |
| 793 | ) |
| 794 | } |
| 795 | |
| 796 | func apexExtractorModuleName(baseModuleName string) string { |
| 797 | return baseModuleName + ".apex.extractor" |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 800 | var _ prebuiltApexModuleCreator = (*ApexSet)(nil) |
| 801 | |
| 802 | // createPrebuiltApexModules creates modules necessary to export files from the apex set to other |
| 803 | // modules. |
| 804 | // |
| 805 | // This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a |
| 806 | // prebuilt_apex except that instead of creating a selector module which selects one .apex file |
| 807 | // from those provided this creates an extractor module which extracts the appropriate .apex file |
| 808 | // from the zip file containing them. |
| 809 | func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) { |
| 810 | baseModuleName := a.BaseModuleName() |
| 811 | |
| 812 | apexExtractorModuleName := apexExtractorModuleName(baseModuleName) |
| 813 | createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties) |
| 814 | |
| 815 | apexFileSource := ":" + apexExtractorModuleName |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 816 | createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &a.prebuiltCommonProperties) |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 817 | |
| 818 | // After passing the arch specific src properties to the creating the apex selector module |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 819 | a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource) |
Paul Duffin | 5dda3e3 | 2021-05-05 14:13:27 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Paul Duffin | 57f8359 | 2021-05-05 15:09:44 +0100 | [diff] [blame] | 822 | func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) { |
| 823 | a.prebuiltApexContentsDeps(ctx) |
Paul Duffin | f58fd9a | 2021-04-06 16:00:22 +0100 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | var _ ApexInfoMutator = (*ApexSet)(nil) |
| 827 | |
| 828 | func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) { |
| 829 | a.apexInfoMutator(mctx) |
| 830 | } |
| 831 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 832 | func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 833 | a.installFilename = a.InstallFilename() |
| 834 | if !strings.HasSuffix(a.installFilename, imageApexSuffix) { |
| 835 | ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix) |
| 836 | } |
| 837 | |
Paul Duffin | bb0dc13 | 2021-05-05 16:58:08 +0100 | [diff] [blame] | 838 | inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path() |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 839 | a.outputApex = android.PathForModuleOut(ctx, a.installFilename) |
Paul Duffin | 2470467 | 2021-04-06 16:09:30 +0100 | [diff] [blame] | 840 | ctx.Build(pctx, android.BuildParams{ |
| 841 | Rule: android.Cp, |
| 842 | Input: inputApex, |
| 843 | Output: a.outputApex, |
| 844 | }) |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 845 | |
| 846 | if a.prebuiltCommon.checkForceDisable(ctx) { |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 847 | a.HideFromMake() |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 848 | return |
| 849 | } |
| 850 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 851 | a.installDir = android.PathForModuleInstall(ctx, "apex") |
| 852 | if a.installable() { |
| 853 | ctx.InstallFile(a.installDir, a.installFilename, a.outputApex) |
| 854 | } |
| 855 | |
| 856 | // in case that apex_set replaces source apex (using prefer: prop) |
| 857 | a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx) |
| 858 | // or that apex_set overrides other apexes (using overrides: prop) |
| 859 | for _, overridden := range a.properties.Overrides { |
| 860 | a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...) |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries { |
| 865 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 866 | Class: "ETC", |
| 867 | OutputFile: android.OptionalPathForPath(a.outputApex), |
| 868 | Include: "$(BUILD_PREBUILT)", |
| 869 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | f79fee8 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 870 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 871 | entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String()) |
| 872 | entries.SetString("LOCAL_MODULE_STEM", a.installFilename) |
| 873 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable()) |
| 874 | entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...) |
| 875 | if len(a.compatSymlinks) > 0 { |
| 876 | entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(a.compatSymlinks, " && ")) |
| 877 | } |
| 878 | }, |
| 879 | }, |
| 880 | }} |
| 881 | } |