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