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