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