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