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