blob: 2dceb6535f278c9dda99bdd18b235571064f6378 [file] [log] [blame]
Paul Duffinc6bb7cf2021-04-08 17:49:27 +01001// 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
15package java
16
17import (
Paul Duffindfa10832021-05-13 17:31:51 +010018 "strings"
19
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010020 "android/soong/android"
Paul Duffin9b381ef2021-04-08 23:01:37 +010021 "github.com/google/blueprint"
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010022)
23
24// Contains support for processing hiddenAPI in a modular fashion.
25
Paul Duffin74431d52021-04-21 14:10:42 +010026type hiddenAPIStubsDependencyTag struct {
27 blueprint.BaseDependencyTag
28 sdkKind android.SdkKind
29}
30
31func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() {
32}
33
34func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool {
35 return false
36}
37
Paul Duffin976b0e52021-04-27 23:20:26 +010038func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType {
39 // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs
40 // property, otherwise treat if it was specified in the java_header_libs property.
41 if javaSdkLibrarySdkMemberType.IsInstance(child) {
42 return javaSdkLibrarySdkMemberType
43 }
44
45 return javaHeaderLibsSdkMemberType
46}
47
48func (b hiddenAPIStubsDependencyTag) ExportMember() bool {
49 // Export the module added via this dependency tag from the sdk.
50 return true
51}
52
Paul Duffin74431d52021-04-21 14:10:42 +010053// Avoid having to make stubs content explicitly visible to dependent modules.
54//
55// This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules
56// with proper dependencies.
57// TODO(b/177892522): Remove this and add needed visibility.
58func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() {
59}
60
61var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{}
62var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{}
63var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{}
Paul Duffin976b0e52021-04-27 23:20:26 +010064var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{}
Paul Duffin74431d52021-04-21 14:10:42 +010065
Paul Duffin3e7fcc32021-04-15 13:31:38 +010066// hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden
67// API processing.
68var hiddenAPIRelevantSdkKinds = []android.SdkKind{
69 android.SdkPublic,
70 android.SdkSystem,
71 android.SdkTest,
72 android.SdkCorePlatform,
73}
74
Paul Duffin74431d52021-04-21 14:10:42 +010075// hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs
76// needed to produce the hidden API monolithic stub flags file.
77func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[android.SdkKind][]string {
78 var publicStubModules []string
79 var systemStubModules []string
80 var testStubModules []string
81 var corePlatformStubModules []string
82
83 if config.AlwaysUsePrebuiltSdks() {
84 // Build configuration mandates using prebuilt stub modules
85 publicStubModules = append(publicStubModules, "sdk_public_current_android")
86 systemStubModules = append(systemStubModules, "sdk_system_current_android")
87 testStubModules = append(testStubModules, "sdk_test_current_android")
88 } else {
89 // Use stub modules built from source
90 publicStubModules = append(publicStubModules, "android_stubs_current")
91 systemStubModules = append(systemStubModules, "android_system_stubs_current")
92 testStubModules = append(testStubModules, "android_test_stubs_current")
93 }
94 // We do not have prebuilts of the core platform api yet
95 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
96
97 // Allow products to define their own stubs for custom product jars that apps can use.
98 publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...)
99 systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...)
100 testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...)
101 if config.IsEnvTrue("EMMA_INSTRUMENT") {
Paul Duffin098c8782021-05-14 10:45:25 +0100102 // Add jacoco-stubs to public, system and test. It doesn't make any real difference as public
103 // allows everyone access but it is needed to ensure consistent flags between the
104 // bootclasspath fragment generated flags and the platform_bootclasspath generated flags.
Paul Duffin74431d52021-04-21 14:10:42 +0100105 publicStubModules = append(publicStubModules, "jacoco-stubs")
Paul Duffin098c8782021-05-14 10:45:25 +0100106 systemStubModules = append(systemStubModules, "jacoco-stubs")
107 testStubModules = append(testStubModules, "jacoco-stubs")
Paul Duffin74431d52021-04-21 14:10:42 +0100108 }
109
110 m := map[android.SdkKind][]string{}
111 m[android.SdkPublic] = publicStubModules
112 m[android.SdkSystem] = systemStubModules
113 m[android.SdkTest] = testStubModules
114 m[android.SdkCorePlatform] = corePlatformStubModules
115 return m
116}
117
118// hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in
119// sdkKindToStubLibModules. It adds them in a well known order and uses an SdkKind specific tag to
120// identify the source of the dependency.
121func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, sdkKindToStubLibModules map[android.SdkKind][]string) {
122 module := ctx.Module()
123 for _, sdkKind := range hiddenAPIRelevantSdkKinds {
124 modules := sdkKindToStubLibModules[sdkKind]
125 ctx.AddDependency(module, hiddenAPIStubsDependencyTag{sdkKind: sdkKind}, modules...)
126 }
127}
128
129// hiddenAPIGatherStubLibDexJarPaths gathers the paths to the dex jars from the dependencies added
130// in hiddenAPIAddStubLibDependencies.
Paul Duffin34827d42021-05-13 21:25:05 +0100131func hiddenAPIGatherStubLibDexJarPaths(ctx android.ModuleContext, contents []android.Module) map[android.SdkKind]android.Paths {
Paul Duffin74431d52021-04-21 14:10:42 +0100132 m := map[android.SdkKind]android.Paths{}
Paul Duffin34827d42021-05-13 21:25:05 +0100133
134 // If the contents includes any java_sdk_library modules then add them to the stubs.
135 for _, module := range contents {
136 if _, ok := module.(SdkLibraryDependency); ok {
137 for _, kind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkTest} {
138 dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind)
139 if dexJar != nil {
140 m[kind] = append(m[kind], dexJar)
141 }
142 }
143 }
144 }
145
Paul Duffin74431d52021-04-21 14:10:42 +0100146 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
147 tag := ctx.OtherModuleDependencyTag(module)
148 if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok {
149 kind := hiddenAPIStubsTag.sdkKind
Paul Duffin10931582021-04-25 10:13:54 +0100150 dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind)
Paul Duffin74431d52021-04-21 14:10:42 +0100151 if dexJar != nil {
152 m[kind] = append(m[kind], dexJar)
153 }
154 }
155 })
Paul Duffin34827d42021-05-13 21:25:05 +0100156
157 // Normalize the paths, i.e. remove duplicates and sort.
158 for k, v := range m {
159 m[k] = android.SortedUniquePaths(v)
160 }
161
Paul Duffin74431d52021-04-21 14:10:42 +0100162 return m
163}
164
165// hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if
166// available, or reports an error.
Paul Duffin10931582021-04-25 10:13:54 +0100167func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path {
168 var dexJar android.Path
169 if sdkLibrary, ok := module.(SdkLibraryDependency); ok {
170 dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind)
171 } else if j, ok := module.(UsesLibraryDependency); ok {
172 dexJar = j.DexJarBuildPath()
Paul Duffin74431d52021-04-21 14:10:42 +0100173 } else {
174 ctx.ModuleErrorf("dependency %s of module type %s does not support providing a dex jar", module, ctx.OtherModuleType(module))
Paul Duffin10931582021-04-25 10:13:54 +0100175 return nil
Paul Duffin74431d52021-04-21 14:10:42 +0100176 }
Paul Duffin10931582021-04-25 10:13:54 +0100177
178 if dexJar == nil {
179 ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module)
180 }
181 return dexJar
Paul Duffin74431d52021-04-21 14:10:42 +0100182}
183
184var sdkKindToHiddenapiListOption = map[android.SdkKind]string{
185 android.SdkPublic: "public-stub-classpath",
186 android.SdkSystem: "system-stub-classpath",
187 android.SdkTest: "test-stub-classpath",
188 android.SdkCorePlatform: "core-platform-stub-classpath",
189}
190
191// ruleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file.
192//
193// The rule is initialized but not built so that the caller can modify it and select an appropriate
194// name.
Paul Duffin2fef1362021-04-15 13:32:00 +0100195func ruleToGenerateHiddenAPIStubFlagsFile(ctx android.BuilderContext, outputPath android.WritablePath, bootDexJars android.Paths, sdkKindToPathList map[android.SdkKind]android.Paths) *android.RuleBuilder {
Paul Duffin74431d52021-04-21 14:10:42 +0100196 // Singleton rule which applies hiddenapi on all boot class path dex files.
197 rule := android.NewRuleBuilder(pctx, ctx)
198
199 tempPath := tempPathForRestat(ctx, outputPath)
200
201 command := rule.Command().
202 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
203 Text("list").
204 FlagForEachInput("--boot-dex=", bootDexJars)
205
206 // Iterate over the sdk kinds in a fixed order.
207 for _, sdkKind := range hiddenAPIRelevantSdkKinds {
208 paths := sdkKindToPathList[sdkKind]
209 if len(paths) > 0 {
210 option := sdkKindToHiddenapiListOption[sdkKind]
211 command.FlagWithInputList("--"+option+"=", paths, ":")
212 }
213 }
214
215 // Add the output path.
216 command.FlagWithOutput("--out-api-flags=", tempPath)
217
218 commitChangeForRestat(rule, tempPath, outputPath)
219 return rule
220}
221
Paul Duffin46169772021-04-14 15:01:56 +0100222// HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the
223// information obtained from annotations within the source code in order to create the complete set
224// of flags that should be applied to the dex implementation jars on the bootclasspath.
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100225//
226// Each property contains a list of paths. With the exception of the Unsupported_packages the paths
227// of each property reference a plain text file that contains a java signature per line. The flags
228// for each of those signatures will be updated in a property specific way.
229//
230// The Unsupported_packages property contains a list of paths, each of which is a plain text file
231// with one Java package per line. All members of all classes within that package (but not nested
232// packages) will be updated in a property specific way.
Paul Duffin46169772021-04-14 15:01:56 +0100233type HiddenAPIFlagFileProperties struct {
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100234 // Marks each signature in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100235 Unsupported []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100236
237 // Marks each signature in the referenced files as being unsupported because it has been removed.
238 // Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100239 Removed []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100240
241 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R
242 // and low priority.
Paul Duffin702210b2021-04-08 20:12:41 +0100243 Max_target_r_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100244
245 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q.
Paul Duffin702210b2021-04-08 20:12:41 +0100246 Max_target_q []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100247
248 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P.
Paul Duffin702210b2021-04-08 20:12:41 +0100249 Max_target_p []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100250
251 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O
252 // and low priority. Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100253 Max_target_o_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100254
255 // Marks each signature in the referenced files as being blocked.
Paul Duffin702210b2021-04-08 20:12:41 +0100256 Blocked []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100257
258 // Marks each signature in every package in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100259 Unsupported_packages []string `android:"path"`
260}
261
Paul Duffin46169772021-04-14 15:01:56 +0100262func (p *HiddenAPIFlagFileProperties) hiddenAPIFlagFileInfo(ctx android.ModuleContext) hiddenAPIFlagFileInfo {
263 info := hiddenAPIFlagFileInfo{categoryToPaths: map[*hiddenAPIFlagFileCategory]android.Paths{}}
Paul Duffine3dc6602021-04-14 09:50:43 +0100264 for _, category := range hiddenAPIFlagFileCategories {
Paul Duffincc17bfe2021-04-19 13:21:20 +0100265 paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p))
Paul Duffine3dc6602021-04-14 09:50:43 +0100266 info.categoryToPaths[category] = paths
Paul Duffin702210b2021-04-08 20:12:41 +0100267 }
Paul Duffine3dc6602021-04-14 09:50:43 +0100268 return info
269}
270
271type hiddenAPIFlagFileCategory struct {
272 // propertyName is the name of the property for this category.
273 propertyName string
274
Paul Duffincc17bfe2021-04-19 13:21:20 +0100275 // propertyValueReader retrieves the value of the property for this category from the set of
Paul Duffine3dc6602021-04-14 09:50:43 +0100276 // properties.
Paul Duffincc17bfe2021-04-19 13:21:20 +0100277 propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string
Paul Duffine3dc6602021-04-14 09:50:43 +0100278
279 // commandMutator adds the appropriate command line options for this category to the supplied
280 // command
281 commandMutator func(command *android.RuleBuilderCommand, path android.Path)
282}
283
284var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{
Paul Duffin46169772021-04-14 15:01:56 +0100285 // See HiddenAPIFlagFileProperties.Unsupported
Paul Duffine3dc6602021-04-14 09:50:43 +0100286 {
287 propertyName: "unsupported",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100288 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100289 return properties.Unsupported
290 },
291 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
292 command.FlagWithInput("--unsupported ", path)
293 },
294 },
Paul Duffin46169772021-04-14 15:01:56 +0100295 // See HiddenAPIFlagFileProperties.Removed
Paul Duffine3dc6602021-04-14 09:50:43 +0100296 {
297 propertyName: "removed",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100298 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100299 return properties.Removed
300 },
301 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
302 command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed")
303 },
304 },
Paul Duffin46169772021-04-14 15:01:56 +0100305 // See HiddenAPIFlagFileProperties.Max_target_r_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100306 {
307 propertyName: "max_target_r_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100308 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100309 return properties.Max_target_r_low_priority
310 },
311 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
312 command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio")
313 },
314 },
Paul Duffin46169772021-04-14 15:01:56 +0100315 // See HiddenAPIFlagFileProperties.Max_target_q
Paul Duffine3dc6602021-04-14 09:50:43 +0100316 {
317 propertyName: "max_target_q",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100318 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100319 return properties.Max_target_q
320 },
321 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
322 command.FlagWithInput("--max-target-q ", path)
323 },
324 },
Paul Duffin46169772021-04-14 15:01:56 +0100325 // See HiddenAPIFlagFileProperties.Max_target_p
Paul Duffine3dc6602021-04-14 09:50:43 +0100326 {
327 propertyName: "max_target_p",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100328 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100329 return properties.Max_target_p
330 },
331 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
332 command.FlagWithInput("--max-target-p ", path)
333 },
334 },
Paul Duffin46169772021-04-14 15:01:56 +0100335 // See HiddenAPIFlagFileProperties.Max_target_o_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100336 {
337 propertyName: "max_target_o_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100338 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100339 return properties.Max_target_o_low_priority
340 },
341 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
342 command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio")
343 },
344 },
Paul Duffin46169772021-04-14 15:01:56 +0100345 // See HiddenAPIFlagFileProperties.Blocked
Paul Duffine3dc6602021-04-14 09:50:43 +0100346 {
347 propertyName: "blocked",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100348 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100349 return properties.Blocked
350 },
351 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
352 command.FlagWithInput("--blocked ", path)
353 },
354 },
Paul Duffin46169772021-04-14 15:01:56 +0100355 // See HiddenAPIFlagFileProperties.Unsupported_packages
Paul Duffine3dc6602021-04-14 09:50:43 +0100356 {
357 propertyName: "unsupported_packages",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100358 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100359 return properties.Unsupported_packages
360 },
361 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
362 command.FlagWithInput("--unsupported ", path).Flag("--packages ")
363 },
364 },
Paul Duffin702210b2021-04-08 20:12:41 +0100365}
366
Paul Duffin2fef1362021-04-15 13:32:00 +0100367// hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties and also generated
368// by hidden API processing.
369//
370// This is used both for an individual bootclasspath_fragment to provide it to other modules and
371// for a module to collate the files from the fragments it depends upon. That is why the fields are
372// all Paths even though they are initialized with a single path.
Paul Duffin46169772021-04-14 15:01:56 +0100373type hiddenAPIFlagFileInfo struct {
Paul Duffine3dc6602021-04-14 09:50:43 +0100374 // categoryToPaths maps from the flag file category to the paths containing information for that
375 // category.
376 categoryToPaths map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffin2fef1362021-04-15 13:32:00 +0100377
378 // The paths to the generated stub-flags.csv files.
379 StubFlagsPaths android.Paths
380
381 // The paths to the generated annotation-flags.csv files.
382 AnnotationFlagsPaths android.Paths
383
384 // The paths to the generated metadata.csv files.
385 MetadataPaths android.Paths
386
387 // The paths to the generated index.csv files.
388 IndexPaths android.Paths
389
390 // The paths to the generated all-flags.csv files.
391 AllFlagsPaths android.Paths
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100392}
Paul Duffin702210b2021-04-08 20:12:41 +0100393
Paul Duffin9b381ef2021-04-08 23:01:37 +0100394func (i *hiddenAPIFlagFileInfo) append(other hiddenAPIFlagFileInfo) {
395 for _, category := range hiddenAPIFlagFileCategories {
396 i.categoryToPaths[category] = append(i.categoryToPaths[category], other.categoryToPaths[category]...)
397 }
Paul Duffin2fef1362021-04-15 13:32:00 +0100398 i.StubFlagsPaths = append(i.StubFlagsPaths, other.StubFlagsPaths...)
399 i.AnnotationFlagsPaths = append(i.AnnotationFlagsPaths, other.AnnotationFlagsPaths...)
400 i.MetadataPaths = append(i.MetadataPaths, other.MetadataPaths...)
401 i.IndexPaths = append(i.IndexPaths, other.IndexPaths...)
402 i.AllFlagsPaths = append(i.AllFlagsPaths, other.AllFlagsPaths...)
Paul Duffin9b381ef2021-04-08 23:01:37 +0100403}
404
405var hiddenAPIFlagFileInfoProvider = blueprint.NewProvider(hiddenAPIFlagFileInfo{})
406
Paul Duffindfa10832021-05-13 17:31:51 +0100407// pathForValidation creates a path of the same type as the supplied type but with a name of
408// <path>.valid.
409//
410// e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return
411// an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.valid
412func pathForValidation(ctx android.PathContext, path android.WritablePath) android.WritablePath {
413 extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".")
414 return path.ReplaceExtension(ctx, extWithoutLeadingDot+".valid")
415}
416
Paul Duffin2fef1362021-04-15 13:32:00 +0100417// buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from
418// the flags from all the modules, the stub flags, augmented with some additional configuration
419// files.
Paul Duffin702210b2021-04-08 20:12:41 +0100420//
421// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
422// an entry for every single member in the dex implementation jars of the individual modules. Every
423// signature in any of the other files MUST be included in this file.
424//
Paul Duffin537ea3d2021-05-14 10:38:00 +0100425// annotationFlags is the path to the annotation flags file generated from annotation information
426// in each module.
Paul Duffin702210b2021-04-08 20:12:41 +0100427//
Paul Duffin537ea3d2021-05-14 10:38:00 +0100428// flagFileInfo is a struct containing paths to files that augment the information provided by
429// the annotationFlags.
430func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, baseFlagsPath android.Path, annotationFlags android.Path, flagFileInfo *hiddenAPIFlagFileInfo) {
Paul Duffindfa10832021-05-13 17:31:51 +0100431
432 // The file which is used to record that the flags file is valid.
433 var validFile android.WritablePath
434
435 // If there are flag files that have been generated by fragments on which this depends then use
436 // them to validate the flag file generated by the rules created by this method.
437 if allFlagsPaths := flagFileInfo.AllFlagsPaths; len(allFlagsPaths) > 0 {
438 // The flags file generated by the rule created by this method needs to be validated to ensure
439 // that it is consistent with the flag files generated by the individual fragments.
440
441 validFile = pathForValidation(ctx, outputPath)
442
443 // Create a rule to validate the output from the following rule.
444 rule := android.NewRuleBuilder(pctx, ctx)
445 rule.Command().
446 BuiltTool("verify_overlaps").
447 Input(outputPath).
448 Inputs(allFlagsPaths).
449 // If validation passes then update the file that records that.
450 Text("&& touch").Output(validFile)
451 rule.Build(name+"Validation", desc+" validation")
452 }
453
454 // Create the rule that will generate the flag files.
Paul Duffind3c15132021-04-21 22:12:35 +0100455 tempPath := tempPathForRestat(ctx, outputPath)
Paul Duffin702210b2021-04-08 20:12:41 +0100456 rule := android.NewRuleBuilder(pctx, ctx)
457 command := rule.Command().
458 BuiltTool("generate_hiddenapi_lists").
459 FlagWithInput("--csv ", baseFlagsPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100460 Input(annotationFlags).
Paul Duffin702210b2021-04-08 20:12:41 +0100461 FlagWithOutput("--output ", tempPath)
462
Paul Duffine3dc6602021-04-14 09:50:43 +0100463 // Add the options for the different categories of flag files.
464 for _, category := range hiddenAPIFlagFileCategories {
Paul Duffin2fef1362021-04-15 13:32:00 +0100465 paths := flagFileInfo.categoryToPaths[category]
Paul Duffine3dc6602021-04-14 09:50:43 +0100466 for _, path := range paths {
467 category.commandMutator(command, path)
468 }
Paul Duffin702210b2021-04-08 20:12:41 +0100469 }
470
471 commitChangeForRestat(rule, tempPath, outputPath)
472
Paul Duffindfa10832021-05-13 17:31:51 +0100473 if validFile != nil {
474 // Add the file that indicates that the file generated by this is valid.
475 //
476 // This will cause the validation rule above to be run any time that the output of this rule
477 // changes but the validation will run in parallel with other rules that depend on this file.
478 command.Validation(validFile)
479 }
480
Paul Duffin2fef1362021-04-15 13:32:00 +0100481 rule.Build(name, desc)
482}
483
484// hiddenAPIGenerateAllFlagsForBootclasspathFragment will generate all the flags for a fragment
485// of the bootclasspath.
486//
487// It takes:
488// * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind.
489// * The list of modules that are the contents of the fragment.
490// * The additional manually curated flag files to use.
491//
492// It generates:
493// * stub-flags.csv
494// * annotation-flags.csv
495// * metadata.csv
496// * index.csv
497// * all-flags.csv
Paul Duffin537ea3d2021-05-14 10:38:00 +0100498func hiddenAPIGenerateAllFlagsForBootclasspathFragment(ctx android.ModuleContext, contents []hiddenAPIModule, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) {
Paul Duffin2fef1362021-04-15 13:32:00 +0100499 hiddenApiSubDir := "modular-hiddenapi"
500
Paul Duffin2fef1362021-04-15 13:32:00 +0100501 // Generate the stub-flags.csv.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100502 bootDexJars := extractBootDexJarsFromHiddenAPIModules(ctx, contents)
Paul Duffin2fef1362021-04-15 13:32:00 +0100503 stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv")
504 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, stubFlagsCSV, bootDexJars, stubJarsByKind)
505 rule.Build("modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags")
506
Paul Duffin537ea3d2021-05-14 10:38:00 +0100507 // Extract the classes jars from the contents.
508 classesJars := extractClassJarsFromHiddenAPIModules(ctx, contents)
509
Paul Duffin2fef1362021-04-15 13:32:00 +0100510 // Generate the set of flags from the annotations in the source code.
511 annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100512 buildRuleToGenerateAnnotationFlags(ctx, "modular hiddenapi annotation flags", classesJars, stubFlagsCSV, annotationFlagsCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100513
514 // Generate the metadata from the annotations in the source code.
515 metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100516 buildRuleToGenerateMetadata(ctx, "modular hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100517
Paul Duffin537ea3d2021-05-14 10:38:00 +0100518 // Generate the index file from the CSV files in the classes jars.
Paul Duffin2fef1362021-04-15 13:32:00 +0100519 indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100520 buildRuleToGenerateIndex(ctx, "modular hiddenapi index", classesJars, indexCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100521
522 // Removed APIs need to be marked and in order to do that the flagFileInfo needs to specify files
523 // containing dex signatures of all the removed APIs. In the monolithic files that is done by
524 // manually combining all the removed.txt files for each API and then converting them to dex
525 // signatures, see the combined-removed-dex module. That will all be done automatically in future.
526 // For now removed APIs are ignored.
527 // TODO(b/179354495): handle removed apis automatically.
528
529 // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex
530 // files.
531 outputPath := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv")
Paul Duffin537ea3d2021-05-14 10:38:00 +0100532 buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", outputPath, stubFlagsCSV, annotationFlagsCSV, flagFileInfo)
Paul Duffin2fef1362021-04-15 13:32:00 +0100533
534 // Store the paths in the info for use by other modules and sdk snapshot generation.
535 flagFileInfo.StubFlagsPaths = android.Paths{stubFlagsCSV}
536 flagFileInfo.AnnotationFlagsPaths = android.Paths{annotationFlagsCSV}
537 flagFileInfo.MetadataPaths = android.Paths{metadataCSV}
538 flagFileInfo.IndexPaths = android.Paths{indexCSV}
539 flagFileInfo.AllFlagsPaths = android.Paths{outputPath}
Paul Duffin702210b2021-04-08 20:12:41 +0100540}
Paul Duffin537ea3d2021-05-14 10:38:00 +0100541
542// gatherHiddenAPIModuleFromContents gathers the hiddenAPIModule from the supplied contents.
543func gatherHiddenAPIModuleFromContents(ctx android.ModuleContext, contents []android.Module) []hiddenAPIModule {
544 hiddenAPIModules := []hiddenAPIModule{}
545 for _, module := range contents {
546 if hiddenAPI, ok := module.(hiddenAPIModule); ok {
547 hiddenAPIModules = append(hiddenAPIModules, hiddenAPI)
548 } else if _, ok := module.(*DexImport); ok {
549 // Ignore this for the purposes of hidden API processing
550 } else {
551 ctx.ModuleErrorf("module %s does not implement hiddenAPIModule", module)
552 }
553 }
554 return hiddenAPIModules
555}
556
557// extractBootDexJarsFromHiddenAPIModules extracts the boot dex jars from the supplied modules.
558func extractBootDexJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths {
559 bootDexJars := android.Paths{}
560 for _, module := range contents {
561 bootDexJar := module.bootDexJar()
562 if bootDexJar == nil {
563 ctx.ModuleErrorf("module %s does not provide a dex jar", module)
564 } else {
565 bootDexJars = append(bootDexJars, bootDexJar)
566 }
567 }
568 return bootDexJars
569}
570
571// extractClassJarsFromHiddenAPIModules extracts the class jars from the supplied modules.
572func extractClassJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths {
573 classesJars := android.Paths{}
574 for _, module := range contents {
575 classesJars = append(classesJars, module.classesJars()...)
576 }
577 return classesJars
578}