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