hiddenapi_singleton.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2019 Google Inc. All rights reserved.
  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 java
  15. import (
  16. "strings"
  17. "android/soong/android"
  18. )
  19. func init() {
  20. RegisterHiddenApiSingletonComponents(android.InitRegistrationContext)
  21. }
  22. func RegisterHiddenApiSingletonComponents(ctx android.RegistrationContext) {
  23. ctx.RegisterParallelSingletonType("hiddenapi", hiddenAPISingletonFactory)
  24. }
  25. var PrepareForTestWithHiddenApiBuildComponents = android.FixtureRegisterWithContext(RegisterHiddenApiSingletonComponents)
  26. type hiddenAPISingletonPathsStruct struct {
  27. // The path to the CSV file that contains the flags that will be encoded into the dex boot jars.
  28. //
  29. // It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with
  30. // a number of additional files that are used to augment the information in the stubFlags with
  31. // manually curated data.
  32. flags android.OutputPath
  33. // The path to the CSV index file that contains mappings from Java signature to source location
  34. // information for all Java elements annotated with the UnsupportedAppUsage annotation in the
  35. // source of all the boot jars.
  36. //
  37. // It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have
  38. // been created by the rest of the build. That includes the index files generated for
  39. // <x>-hiddenapi modules.
  40. index android.OutputPath
  41. // The path to the CSV metadata file that contains mappings from Java signature to the value of
  42. // properties specified on UnsupportedAppUsage annotations in the source of all the boot jars.
  43. //
  44. // It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that
  45. // have been created by the rest of the build. That includes the metadata files generated for
  46. // <x>-hiddenapi modules.
  47. metadata android.OutputPath
  48. // The path to the CSV metadata file that contains mappings from Java signature to flags obtained
  49. // from the public, system and test API stubs.
  50. //
  51. // This is created by the hiddenapi tool which is given dex files for the public, system and test
  52. // API stubs (including product specific stubs) along with dex boot jars, so does not include
  53. // <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which
  54. // members in the dex boot jars match a member in the dex stub jars for that API surface and then
  55. // outputs a file containing the signatures of all members in the dex boot jars along with the
  56. // flags that indicate which API surface it belongs, if any.
  57. //
  58. // e.g. a dex member that matches a member in the public dex stubs would have flags
  59. // "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex
  60. // member that didn't match a member in any of the dex stubs is still output it just has an empty
  61. // set of flags.
  62. //
  63. // The notion of matching is quite complex, it is not restricted to just exact matching but also
  64. // follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing
  65. // methods are also public. If an interface method is public and a class inherits an
  66. // implementation of that method from a super class then that super class method is also public.
  67. // That ensures that any method that can be called directly by an App through a public method is
  68. // visible to that App.
  69. //
  70. // Propagating the visibility of members across the inheritance hierarchy at build time will cause
  71. // problems when modularizing and unbundling as it that propagation can cross module boundaries.
  72. // e.g. Say that a private framework class implements a public interface and inherits an
  73. // implementation of one of its methods from a core platform ART class. In that case the ART
  74. // implementation method needs to be marked as public which requires the build to have access to
  75. // the framework implementation classes at build time. The work to rectify this is being tracked
  76. // at http://b/178693149.
  77. //
  78. // This file (or at least those items marked as being in the public-api) is used by hiddenapi when
  79. // creating the metadata and flags for the individual modules in order to perform consistency
  80. // checks and filter out bridge methods that are part of the public API. The latter relies on the
  81. // propagation of visibility across the inheritance hierarchy.
  82. stubFlags android.OutputPath
  83. }
  84. var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
  85. // hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
  86. // from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
  87. // yet been created.
  88. func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
  89. return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
  90. // Make the paths relative to the out/soong/hiddenapi directory instead of to the out/soong/
  91. // directory. This ensures that if they are used as java_resources they do not end up in a
  92. // hiddenapi directory in the resulting APK.
  93. hiddenapiDir := android.PathForOutput(ctx, "hiddenapi")
  94. return hiddenAPISingletonPathsStruct{
  95. flags: hiddenapiDir.Join(ctx, "hiddenapi-flags.csv"),
  96. index: hiddenapiDir.Join(ctx, "hiddenapi-index.csv"),
  97. metadata: hiddenapiDir.Join(ctx, "hiddenapi-unsupported.csv"),
  98. stubFlags: hiddenapiDir.Join(ctx, "hiddenapi-stub-flags.txt"),
  99. }
  100. }).(hiddenAPISingletonPathsStruct)
  101. }
  102. func hiddenAPISingletonFactory() android.Singleton {
  103. return &hiddenAPISingleton{}
  104. }
  105. type hiddenAPISingleton struct {
  106. }
  107. // hiddenAPI singleton rules
  108. func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
  109. // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
  110. if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
  111. return
  112. }
  113. // If there is a prebuilt hiddenapi dir, generate rules to use the
  114. // files within. Generally, we build the hiddenapi files from source
  115. // during the build, ensuring consistency. It's possible, in a split
  116. // build (framework and vendor) scenario, for the vendor build to use
  117. // prebuilt hiddenapi files from the framework build. In this scenario,
  118. // the framework and vendor builds must use the same source to ensure
  119. // consistency.
  120. if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
  121. prebuiltFlagsRule(ctx)
  122. prebuiltIndexRule(ctx)
  123. return
  124. }
  125. }
  126. // Checks to see whether the supplied module variant is in the list of boot jars.
  127. //
  128. // TODO(b/179354495): Avoid having to perform this type of check.
  129. func isModuleInConfiguredList(ctx android.BaseModuleContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool {
  130. name := ctx.OtherModuleName(module)
  131. // Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed.
  132. name = android.RemoveOptionalPrebuiltPrefix(name)
  133. // Ignore any module that is not listed in the boot image configuration.
  134. index := configuredBootJars.IndexOfJar(name)
  135. if index == -1 {
  136. return false
  137. }
  138. // It is an error if the module is not an ApexModule.
  139. if _, ok := module.(android.ApexModule); !ok {
  140. ctx.ModuleErrorf("%s is configured in boot jars but does not support being added to an apex", ctx.OtherModuleName(module))
  141. return false
  142. }
  143. apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
  144. // Now match the apex part of the boot image configuration.
  145. requiredApex := configuredBootJars.Apex(index)
  146. if requiredApex == "platform" || requiredApex == "system_ext" {
  147. if len(apexInfo.InApexVariants) != 0 {
  148. // A platform variant is required but this is for an apex so ignore it.
  149. return false
  150. }
  151. } else if !apexInfo.InApexVariant(requiredApex) {
  152. // An apex variant for a specific apex is required but this is the wrong apex.
  153. return false
  154. }
  155. return true
  156. }
  157. func prebuiltFlagsRule(ctx android.SingletonContext) {
  158. outputPath := hiddenAPISingletonPaths(ctx).flags
  159. inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv")
  160. ctx.Build(pctx, android.BuildParams{
  161. Rule: android.Cp,
  162. Output: outputPath,
  163. Input: inputPath,
  164. })
  165. }
  166. func prebuiltIndexRule(ctx android.SingletonContext) {
  167. outputPath := hiddenAPISingletonPaths(ctx).index
  168. inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv")
  169. ctx.Build(pctx, android.BuildParams{
  170. Rule: android.Cp,
  171. Output: outputPath,
  172. Input: inputPath,
  173. })
  174. }
  175. // tempPathForRestat creates a path of the same type as the supplied type but with a name of
  176. // <path>.tmp.
  177. //
  178. // e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return
  179. // an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.tmp
  180. func tempPathForRestat(ctx android.PathContext, path android.WritablePath) android.WritablePath {
  181. extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".")
  182. return path.ReplaceExtension(ctx, extWithoutLeadingDot+".tmp")
  183. }
  184. // commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
  185. // also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
  186. // the rule.
  187. func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
  188. rule.Restat()
  189. rule.Temporary(tempPath)
  190. rule.Command().
  191. Text("(").
  192. Text("if").
  193. Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
  194. Text("then").
  195. Text("rm").Input(tempPath).Text(";").
  196. Text("else").
  197. Text("mv").Input(tempPath).Output(outputPath).Text(";").
  198. Text("fi").
  199. Text(")")
  200. }