deapexer.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2021 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. package apex
  15. import (
  16. "strings"
  17. "android/soong/android"
  18. )
  19. // Contains 'deapexer' a private module type used by 'prebuilt_apex' to make dex files contained
  20. // within a .apex file referenced by `prebuilt_apex` available for use by their associated
  21. // `java_import` modules.
  22. //
  23. // An 'apex' module references `java_library` modules from which .dex files are obtained that are
  24. // stored in the resulting `.apex` file. The resulting `.apex` file is then made available as a
  25. // prebuilt by referencing it from a `prebuilt_apex`. For each such `java_library` that is used by
  26. // modules outside the `.apex` file a `java_import` prebuilt is made available referencing a jar
  27. // that contains the Java classes.
  28. //
  29. // When building a Java module type, e.g. `java_module` or `android_app` against such prebuilts the
  30. // `java_import` provides the classes jar (jar containing `.class` files) against which the
  31. // module's `.java` files are compiled. That classes jar usually contains only stub classes. The
  32. // resulting classes jar is converted into a dex jar (jar containing `.dex` files). Then if
  33. // necessary the dex jar is further processed by `dexpreopt` to produce an optimized form of the
  34. // library specific to the current Android version. This process requires access to implementation
  35. // dex jars for each `java_import`. The `java_import` will obtain the implementation dex jar from
  36. // the `.apex` file in the associated `prebuilt_apex`.
  37. //
  38. // This is intentionally not registered by name as it is not intended to be used from within an
  39. // `Android.bp` file.
  40. // DeapexerProperties specifies the properties supported by the deapexer module.
  41. //
  42. // As these are never intended to be supplied in a .bp file they use a different naming convention
  43. // to make it clear that they are different.
  44. type DeapexerProperties struct {
  45. // List of common modules that may need access to files exported by this module.
  46. //
  47. // A common module in this sense is one that is not arch specific but uses a common variant for
  48. // all architectures, e.g. java.
  49. CommonModules []string
  50. // List of files exported from the .apex file by this module
  51. //
  52. // Each entry is a path from the apex root, e.g. javalib/core-libart.jar.
  53. ExportedFiles []string
  54. }
  55. type SelectedApexProperties struct {
  56. // The path to the apex selected for use by this module.
  57. //
  58. // Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>"
  59. // and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an
  60. // Android.bp file.
  61. Selected_apex *string `android:"path" blueprint:"mutated"`
  62. }
  63. type Deapexer struct {
  64. android.ModuleBase
  65. properties DeapexerProperties
  66. selectedApexProperties SelectedApexProperties
  67. inputApex android.Path
  68. }
  69. // Returns the name of the deapexer module corresponding to an APEX module with the given name.
  70. func deapexerModuleName(apexModuleName string) string {
  71. return apexModuleName + ".deapexer"
  72. }
  73. // Returns the name of the APEX module corresponding to an deapexer module with
  74. // the given name. This reverses deapexerModuleName.
  75. func apexModuleName(deapexerModuleName string) string {
  76. return strings.TrimSuffix(deapexerModuleName, ".deapexer")
  77. }
  78. func privateDeapexerFactory() android.Module {
  79. module := &Deapexer{}
  80. module.AddProperties(&module.properties, &module.selectedApexProperties)
  81. android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
  82. return module
  83. }
  84. func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) {
  85. // Add dependencies from the java modules to which this exports files from the `.apex` file onto
  86. // this module so that they can access the `DeapexerInfo` object that this provides.
  87. for _, lib := range p.properties.CommonModules {
  88. dep := prebuiltApexExportedModuleName(ctx, lib)
  89. ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep)
  90. }
  91. }
  92. func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  93. p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
  94. // Create and remember the directory into which the .apex file's contents will be unpacked.
  95. deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
  96. exports := make(map[string]android.WritablePath)
  97. // Create mappings from apex relative path to the extracted file's path.
  98. exportedPaths := make(android.Paths, 0, len(exports))
  99. for _, path := range p.properties.ExportedFiles {
  100. // Populate the exports that this makes available.
  101. extractedPath := deapexerOutput.Join(ctx, path)
  102. exports[path] = extractedPath
  103. exportedPaths = append(exportedPaths, extractedPath)
  104. }
  105. // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
  106. // deapexer and verifies that all the required files were created. Also, make the mapping from
  107. // apex relative path to extracted file path available for other modules.
  108. if len(exports) > 0 {
  109. // Make the information available for other modules.
  110. di := android.NewDeapexerInfo(apexModuleName(ctx.ModuleName()), exports)
  111. ctx.SetProvider(android.DeapexerProvider, di)
  112. // Create a sorted list of the files that this exports.
  113. exportedPaths = android.SortedUniquePaths(exportedPaths)
  114. // The apex needs to export some files so create a ninja rule to unpack the apex and check that
  115. // the required files are present.
  116. builder := android.NewRuleBuilder(pctx, ctx)
  117. command := builder.Command()
  118. command.
  119. Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
  120. BuiltTool("deapexer").
  121. BuiltTool("debugfs").
  122. BuiltTool("fsck.erofs").
  123. Input(p.inputApex).
  124. Text(deapexerOutput.String())
  125. for _, p := range exportedPaths {
  126. command.Output(p.(android.WritablePath))
  127. }
  128. builder.Build("deapexer", "deapex "+apexModuleName(ctx.ModuleName()))
  129. }
  130. }