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