blob: 99485b4ede2e63e2e303d065c85872782d34fec5 [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 Duffin7487a7a2021-05-19 09:36:09 +010018 "fmt"
Paul Duffindfa10832021-05-13 17:31:51 +010019 "strings"
20
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010021 "android/soong/android"
Paul Duffin9b381ef2021-04-08 23:01:37 +010022 "github.com/google/blueprint"
Paul Duffinc6bb7cf2021-04-08 17:49:27 +010023)
24
25// Contains support for processing hiddenAPI in a modular fashion.
26
Paul Duffin74431d52021-04-21 14:10:42 +010027type hiddenAPIStubsDependencyTag struct {
28 blueprint.BaseDependencyTag
29 sdkKind android.SdkKind
30}
31
32func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() {
33}
34
35func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool {
36 return false
37}
38
Paul Duffin976b0e52021-04-27 23:20:26 +010039func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType {
40 // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs
41 // property, otherwise treat if it was specified in the java_header_libs property.
42 if javaSdkLibrarySdkMemberType.IsInstance(child) {
43 return javaSdkLibrarySdkMemberType
44 }
45
46 return javaHeaderLibsSdkMemberType
47}
48
49func (b hiddenAPIStubsDependencyTag) ExportMember() bool {
50 // Export the module added via this dependency tag from the sdk.
51 return true
52}
53
Paul Duffin74431d52021-04-21 14:10:42 +010054// Avoid having to make stubs content explicitly visible to dependent modules.
55//
56// This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules
57// with proper dependencies.
58// TODO(b/177892522): Remove this and add needed visibility.
59func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() {
60}
61
62var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{}
63var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{}
64var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{}
Paul Duffin976b0e52021-04-27 23:20:26 +010065var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{}
Paul Duffin74431d52021-04-21 14:10:42 +010066
Paul Duffin3e7fcc32021-04-15 13:31:38 +010067// hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden
68// API processing.
69var hiddenAPIRelevantSdkKinds = []android.SdkKind{
70 android.SdkPublic,
71 android.SdkSystem,
72 android.SdkTest,
73 android.SdkCorePlatform,
74}
75
Paul Duffin74431d52021-04-21 14:10:42 +010076// hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs
77// needed to produce the hidden API monolithic stub flags file.
78func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[android.SdkKind][]string {
79 var publicStubModules []string
80 var systemStubModules []string
81 var testStubModules []string
82 var corePlatformStubModules []string
83
84 if config.AlwaysUsePrebuiltSdks() {
85 // Build configuration mandates using prebuilt stub modules
86 publicStubModules = append(publicStubModules, "sdk_public_current_android")
87 systemStubModules = append(systemStubModules, "sdk_system_current_android")
88 testStubModules = append(testStubModules, "sdk_test_current_android")
89 } else {
90 // Use stub modules built from source
91 publicStubModules = append(publicStubModules, "android_stubs_current")
92 systemStubModules = append(systemStubModules, "android_system_stubs_current")
93 testStubModules = append(testStubModules, "android_test_stubs_current")
94 }
95 // We do not have prebuilts of the core platform api yet
96 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
97
98 // Allow products to define their own stubs for custom product jars that apps can use.
99 publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...)
100 systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...)
101 testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...)
102 if config.IsEnvTrue("EMMA_INSTRUMENT") {
Paul Duffin098c8782021-05-14 10:45:25 +0100103 // Add jacoco-stubs to public, system and test. It doesn't make any real difference as public
104 // allows everyone access but it is needed to ensure consistent flags between the
105 // bootclasspath fragment generated flags and the platform_bootclasspath generated flags.
Paul Duffin74431d52021-04-21 14:10:42 +0100106 publicStubModules = append(publicStubModules, "jacoco-stubs")
Paul Duffin098c8782021-05-14 10:45:25 +0100107 systemStubModules = append(systemStubModules, "jacoco-stubs")
108 testStubModules = append(testStubModules, "jacoco-stubs")
Paul Duffin74431d52021-04-21 14:10:42 +0100109 }
110
111 m := map[android.SdkKind][]string{}
112 m[android.SdkPublic] = publicStubModules
113 m[android.SdkSystem] = systemStubModules
114 m[android.SdkTest] = testStubModules
115 m[android.SdkCorePlatform] = corePlatformStubModules
116 return m
117}
118
119// hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in
120// sdkKindToStubLibModules. It adds them in a well known order and uses an SdkKind specific tag to
121// identify the source of the dependency.
122func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, sdkKindToStubLibModules map[android.SdkKind][]string) {
123 module := ctx.Module()
124 for _, sdkKind := range hiddenAPIRelevantSdkKinds {
125 modules := sdkKindToStubLibModules[sdkKind]
126 ctx.AddDependency(module, hiddenAPIStubsDependencyTag{sdkKind: sdkKind}, modules...)
127 }
128}
129
130// hiddenAPIGatherStubLibDexJarPaths gathers the paths to the dex jars from the dependencies added
131// in hiddenAPIAddStubLibDependencies.
Paul Duffin34827d42021-05-13 21:25:05 +0100132func hiddenAPIGatherStubLibDexJarPaths(ctx android.ModuleContext, contents []android.Module) map[android.SdkKind]android.Paths {
Paul Duffin74431d52021-04-21 14:10:42 +0100133 m := map[android.SdkKind]android.Paths{}
Paul Duffin34827d42021-05-13 21:25:05 +0100134
135 // If the contents includes any java_sdk_library modules then add them to the stubs.
136 for _, module := range contents {
137 if _, ok := module.(SdkLibraryDependency); ok {
138 for _, kind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkTest} {
139 dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind)
140 if dexJar != nil {
141 m[kind] = append(m[kind], dexJar)
142 }
143 }
144 }
145 }
146
Paul Duffin74431d52021-04-21 14:10:42 +0100147 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
148 tag := ctx.OtherModuleDependencyTag(module)
149 if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok {
150 kind := hiddenAPIStubsTag.sdkKind
Paul Duffin10931582021-04-25 10:13:54 +0100151 dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, kind)
Paul Duffin74431d52021-04-21 14:10:42 +0100152 if dexJar != nil {
153 m[kind] = append(m[kind], dexJar)
154 }
155 }
156 })
Paul Duffin34827d42021-05-13 21:25:05 +0100157
158 // Normalize the paths, i.e. remove duplicates and sort.
159 for k, v := range m {
160 m[k] = android.SortedUniquePaths(v)
161 }
162
Paul Duffin74431d52021-04-21 14:10:42 +0100163 return m
164}
165
166// hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if
167// available, or reports an error.
Paul Duffin10931582021-04-25 10:13:54 +0100168func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path {
169 var dexJar android.Path
170 if sdkLibrary, ok := module.(SdkLibraryDependency); ok {
171 dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind)
172 } else if j, ok := module.(UsesLibraryDependency); ok {
173 dexJar = j.DexJarBuildPath()
Paul Duffin74431d52021-04-21 14:10:42 +0100174 } else {
175 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 +0100176 return nil
Paul Duffin74431d52021-04-21 14:10:42 +0100177 }
Paul Duffin10931582021-04-25 10:13:54 +0100178
179 if dexJar == nil {
180 ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module)
181 }
182 return dexJar
Paul Duffin74431d52021-04-21 14:10:42 +0100183}
184
185var sdkKindToHiddenapiListOption = map[android.SdkKind]string{
186 android.SdkPublic: "public-stub-classpath",
187 android.SdkSystem: "system-stub-classpath",
188 android.SdkTest: "test-stub-classpath",
189 android.SdkCorePlatform: "core-platform-stub-classpath",
190}
191
192// ruleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file.
193//
194// The rule is initialized but not built so that the caller can modify it and select an appropriate
195// name.
Paul Duffin2fef1362021-04-15 13:32:00 +0100196func 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 +0100197 // Singleton rule which applies hiddenapi on all boot class path dex files.
198 rule := android.NewRuleBuilder(pctx, ctx)
199
200 tempPath := tempPathForRestat(ctx, outputPath)
201
202 command := rule.Command().
203 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
204 Text("list").
205 FlagForEachInput("--boot-dex=", bootDexJars)
206
207 // Iterate over the sdk kinds in a fixed order.
208 for _, sdkKind := range hiddenAPIRelevantSdkKinds {
209 paths := sdkKindToPathList[sdkKind]
210 if len(paths) > 0 {
211 option := sdkKindToHiddenapiListOption[sdkKind]
212 command.FlagWithInputList("--"+option+"=", paths, ":")
213 }
214 }
215
216 // Add the output path.
217 command.FlagWithOutput("--out-api-flags=", tempPath)
218
219 commitChangeForRestat(rule, tempPath, outputPath)
220 return rule
221}
222
Paul Duffin46169772021-04-14 15:01:56 +0100223// HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the
224// information obtained from annotations within the source code in order to create the complete set
225// of flags that should be applied to the dex implementation jars on the bootclasspath.
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100226//
227// Each property contains a list of paths. With the exception of the Unsupported_packages the paths
228// of each property reference a plain text file that contains a java signature per line. The flags
229// for each of those signatures will be updated in a property specific way.
230//
231// The Unsupported_packages property contains a list of paths, each of which is a plain text file
232// with one Java package per line. All members of all classes within that package (but not nested
233// packages) will be updated in a property specific way.
Paul Duffin46169772021-04-14 15:01:56 +0100234type HiddenAPIFlagFileProperties struct {
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100235 // Marks each signature in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100236 Unsupported []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100237
238 // Marks each signature in the referenced files as being unsupported because it has been removed.
239 // Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100240 Removed []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100241
242 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R
243 // and low priority.
Paul Duffin702210b2021-04-08 20:12:41 +0100244 Max_target_r_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100245
246 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q.
Paul Duffin702210b2021-04-08 20:12:41 +0100247 Max_target_q []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100248
249 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P.
Paul Duffin702210b2021-04-08 20:12:41 +0100250 Max_target_p []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100251
252 // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O
253 // and low priority. Any conflicts with other flags are ignored.
Paul Duffin702210b2021-04-08 20:12:41 +0100254 Max_target_o_low_priority []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100255
256 // Marks each signature in the referenced files as being blocked.
Paul Duffin702210b2021-04-08 20:12:41 +0100257 Blocked []string `android:"path"`
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100258
259 // Marks each signature in every package in the referenced files as being unsupported.
Paul Duffin702210b2021-04-08 20:12:41 +0100260 Unsupported_packages []string `android:"path"`
261}
262
Paul Duffin46169772021-04-14 15:01:56 +0100263func (p *HiddenAPIFlagFileProperties) hiddenAPIFlagFileInfo(ctx android.ModuleContext) hiddenAPIFlagFileInfo {
Paul Duffin438eb572021-05-21 16:58:23 +0100264 info := hiddenAPIFlagFileInfo{FlagFilesByCategory: FlagFilesByCategory{}}
Paul Duffine3dc6602021-04-14 09:50:43 +0100265 for _, category := range hiddenAPIFlagFileCategories {
Paul Duffincc17bfe2021-04-19 13:21:20 +0100266 paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p))
Paul Duffin438eb572021-05-21 16:58:23 +0100267 info.FlagFilesByCategory[category] = paths
Paul Duffin702210b2021-04-08 20:12:41 +0100268 }
Paul Duffine3dc6602021-04-14 09:50:43 +0100269 return info
270}
271
272type hiddenAPIFlagFileCategory struct {
273 // propertyName is the name of the property for this category.
274 propertyName string
275
Paul Duffincc17bfe2021-04-19 13:21:20 +0100276 // propertyValueReader retrieves the value of the property for this category from the set of
Paul Duffine3dc6602021-04-14 09:50:43 +0100277 // properties.
Paul Duffincc17bfe2021-04-19 13:21:20 +0100278 propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string
Paul Duffine3dc6602021-04-14 09:50:43 +0100279
280 // commandMutator adds the appropriate command line options for this category to the supplied
281 // command
282 commandMutator func(command *android.RuleBuilderCommand, path android.Path)
283}
284
285var hiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{
Paul Duffin46169772021-04-14 15:01:56 +0100286 // See HiddenAPIFlagFileProperties.Unsupported
Paul Duffine3dc6602021-04-14 09:50:43 +0100287 {
288 propertyName: "unsupported",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100289 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100290 return properties.Unsupported
291 },
292 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
293 command.FlagWithInput("--unsupported ", path)
294 },
295 },
Paul Duffin46169772021-04-14 15:01:56 +0100296 // See HiddenAPIFlagFileProperties.Removed
Paul Duffine3dc6602021-04-14 09:50:43 +0100297 {
298 propertyName: "removed",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100299 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100300 return properties.Removed
301 },
302 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
303 command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed")
304 },
305 },
Paul Duffin46169772021-04-14 15:01:56 +0100306 // See HiddenAPIFlagFileProperties.Max_target_r_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100307 {
308 propertyName: "max_target_r_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100309 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100310 return properties.Max_target_r_low_priority
311 },
312 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
313 command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio")
314 },
315 },
Paul Duffin46169772021-04-14 15:01:56 +0100316 // See HiddenAPIFlagFileProperties.Max_target_q
Paul Duffine3dc6602021-04-14 09:50:43 +0100317 {
318 propertyName: "max_target_q",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100319 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100320 return properties.Max_target_q
321 },
322 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
323 command.FlagWithInput("--max-target-q ", path)
324 },
325 },
Paul Duffin46169772021-04-14 15:01:56 +0100326 // See HiddenAPIFlagFileProperties.Max_target_p
Paul Duffine3dc6602021-04-14 09:50:43 +0100327 {
328 propertyName: "max_target_p",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100329 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100330 return properties.Max_target_p
331 },
332 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
333 command.FlagWithInput("--max-target-p ", path)
334 },
335 },
Paul Duffin46169772021-04-14 15:01:56 +0100336 // See HiddenAPIFlagFileProperties.Max_target_o_low_priority
Paul Duffine3dc6602021-04-14 09:50:43 +0100337 {
338 propertyName: "max_target_o_low_priority",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100339 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100340 return properties.Max_target_o_low_priority
341 },
342 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
343 command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio")
344 },
345 },
Paul Duffin46169772021-04-14 15:01:56 +0100346 // See HiddenAPIFlagFileProperties.Blocked
Paul Duffine3dc6602021-04-14 09:50:43 +0100347 {
348 propertyName: "blocked",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100349 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100350 return properties.Blocked
351 },
352 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
353 command.FlagWithInput("--blocked ", path)
354 },
355 },
Paul Duffin46169772021-04-14 15:01:56 +0100356 // See HiddenAPIFlagFileProperties.Unsupported_packages
Paul Duffine3dc6602021-04-14 09:50:43 +0100357 {
358 propertyName: "unsupported_packages",
Paul Duffincc17bfe2021-04-19 13:21:20 +0100359 propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string {
Paul Duffine3dc6602021-04-14 09:50:43 +0100360 return properties.Unsupported_packages
361 },
362 commandMutator: func(command *android.RuleBuilderCommand, path android.Path) {
363 command.FlagWithInput("--unsupported ", path).Flag("--packages ")
364 },
365 },
Paul Duffin702210b2021-04-08 20:12:41 +0100366}
367
Paul Duffin438eb572021-05-21 16:58:23 +0100368// FlagFilesByCategory maps a hiddenAPIFlagFileCategory to the paths to the files in that category.
369type FlagFilesByCategory map[*hiddenAPIFlagFileCategory]android.Paths
370
371// append appends the supplied flags files to the corresponding category in this map.
372func (s FlagFilesByCategory) append(other FlagFilesByCategory) {
373 for _, category := range hiddenAPIFlagFileCategories {
374 s[category] = append(s[category], other[category]...)
375 }
376}
377
378// dedup removes duplicates in the flag files, while maintaining the order in which they were
379// appended.
380func (s FlagFilesByCategory) dedup() {
381 for category, paths := range s {
382 s[category] = android.FirstUniquePaths(paths)
383 }
384}
385
Paul Duffin2fef1362021-04-15 13:32:00 +0100386// hiddenAPIFlagFileInfo contains paths resolved from HiddenAPIFlagFileProperties and also generated
387// by hidden API processing.
388//
389// This is used both for an individual bootclasspath_fragment to provide it to other modules and
390// for a module to collate the files from the fragments it depends upon. That is why the fields are
391// all Paths even though they are initialized with a single path.
Paul Duffin46169772021-04-14 15:01:56 +0100392type hiddenAPIFlagFileInfo struct {
Paul Duffin438eb572021-05-21 16:58:23 +0100393 // FlagFilesByCategory maps from the flag file category to the paths containing information for
394 // that category.
395 FlagFilesByCategory FlagFilesByCategory
Paul Duffin2fef1362021-04-15 13:32:00 +0100396
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100397 // The output from the hidden API processing needs to be made available to other modules.
398 HiddenAPIFlagOutput
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100399}
Paul Duffin702210b2021-04-08 20:12:41 +0100400
Paul Duffin9b381ef2021-04-08 23:01:37 +0100401var hiddenAPIFlagFileInfoProvider = blueprint.NewProvider(hiddenAPIFlagFileInfo{})
402
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100403// HiddenAPIFlagOutput contains paths to output files from the hidden API flag generation for a
404// bootclasspath_fragment module.
405type HiddenAPIFlagOutput struct {
406 // The path to the generated stub-flags.csv file.
407 StubFlagsPath android.Path
408
409 // The path to the generated annotation-flags.csv file.
410 AnnotationFlagsPath android.Path
411
412 // The path to the generated metadata.csv file.
413 MetadataPath android.Path
414
415 // The path to the generated index.csv file.
416 IndexPath android.Path
417
418 // The path to the generated all-flags.csv file.
419 AllFlagsPath android.Path
420}
421
Paul Duffindfa10832021-05-13 17:31:51 +0100422// pathForValidation creates a path of the same type as the supplied type but with a name of
423// <path>.valid.
424//
425// e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return
426// an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.valid
427func pathForValidation(ctx android.PathContext, path android.WritablePath) android.WritablePath {
428 extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".")
429 return path.ReplaceExtension(ctx, extWithoutLeadingDot+".valid")
430}
431
Paul Duffin2fef1362021-04-15 13:32:00 +0100432// buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from
433// the flags from all the modules, the stub flags, augmented with some additional configuration
434// files.
Paul Duffin702210b2021-04-08 20:12:41 +0100435//
436// baseFlagsPath is the path to the flags file containing all the information from the stubs plus
437// an entry for every single member in the dex implementation jars of the individual modules. Every
438// signature in any of the other files MUST be included in this file.
439//
Paul Duffin537ea3d2021-05-14 10:38:00 +0100440// annotationFlags is the path to the annotation flags file generated from annotation information
441// in each module.
Paul Duffin702210b2021-04-08 20:12:41 +0100442//
Paul Duffin537ea3d2021-05-14 10:38:00 +0100443// flagFileInfo is a struct containing paths to files that augment the information provided by
444// the annotationFlags.
Paul Duffin438eb572021-05-21 16:58:23 +0100445func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, baseFlagsPath android.Path, annotationFlags android.Path, flagFilesByCategory FlagFilesByCategory, allFlagsPaths android.Paths) {
Paul Duffindfa10832021-05-13 17:31:51 +0100446
447 // The file which is used to record that the flags file is valid.
448 var validFile android.WritablePath
449
450 // If there are flag files that have been generated by fragments on which this depends then use
451 // them to validate the flag file generated by the rules created by this method.
Paul Duffin438eb572021-05-21 16:58:23 +0100452 if len(allFlagsPaths) > 0 {
Paul Duffindfa10832021-05-13 17:31:51 +0100453 // The flags file generated by the rule created by this method needs to be validated to ensure
454 // that it is consistent with the flag files generated by the individual fragments.
455
456 validFile = pathForValidation(ctx, outputPath)
457
458 // Create a rule to validate the output from the following rule.
459 rule := android.NewRuleBuilder(pctx, ctx)
460 rule.Command().
461 BuiltTool("verify_overlaps").
462 Input(outputPath).
463 Inputs(allFlagsPaths).
464 // If validation passes then update the file that records that.
465 Text("&& touch").Output(validFile)
466 rule.Build(name+"Validation", desc+" validation")
467 }
468
469 // Create the rule that will generate the flag files.
Paul Duffind3c15132021-04-21 22:12:35 +0100470 tempPath := tempPathForRestat(ctx, outputPath)
Paul Duffin702210b2021-04-08 20:12:41 +0100471 rule := android.NewRuleBuilder(pctx, ctx)
472 command := rule.Command().
473 BuiltTool("generate_hiddenapi_lists").
474 FlagWithInput("--csv ", baseFlagsPath).
Paul Duffin537ea3d2021-05-14 10:38:00 +0100475 Input(annotationFlags).
Paul Duffin702210b2021-04-08 20:12:41 +0100476 FlagWithOutput("--output ", tempPath)
477
Paul Duffine3dc6602021-04-14 09:50:43 +0100478 // Add the options for the different categories of flag files.
479 for _, category := range hiddenAPIFlagFileCategories {
Paul Duffin438eb572021-05-21 16:58:23 +0100480 paths := flagFilesByCategory[category]
Paul Duffine3dc6602021-04-14 09:50:43 +0100481 for _, path := range paths {
482 category.commandMutator(command, path)
483 }
Paul Duffin702210b2021-04-08 20:12:41 +0100484 }
485
486 commitChangeForRestat(rule, tempPath, outputPath)
487
Paul Duffindfa10832021-05-13 17:31:51 +0100488 if validFile != nil {
489 // Add the file that indicates that the file generated by this is valid.
490 //
491 // This will cause the validation rule above to be run any time that the output of this rule
492 // changes but the validation will run in parallel with other rules that depend on this file.
493 command.Validation(validFile)
494 }
495
Paul Duffin2fef1362021-04-15 13:32:00 +0100496 rule.Build(name, desc)
497}
498
499// hiddenAPIGenerateAllFlagsForBootclasspathFragment will generate all the flags for a fragment
500// of the bootclasspath.
501//
502// It takes:
503// * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind.
504// * The list of modules that are the contents of the fragment.
505// * The additional manually curated flag files to use.
506//
507// It generates:
508// * stub-flags.csv
509// * annotation-flags.csv
510// * metadata.csv
511// * index.csv
512// * all-flags.csv
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100513func hiddenAPIGenerateAllFlagsForBootclasspathFragment(ctx android.ModuleContext, contents []hiddenAPIModule, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) *HiddenAPIFlagOutput {
Paul Duffin2fef1362021-04-15 13:32:00 +0100514 hiddenApiSubDir := "modular-hiddenapi"
515
Paul Duffin2fef1362021-04-15 13:32:00 +0100516 // Generate the stub-flags.csv.
Paul Duffin537ea3d2021-05-14 10:38:00 +0100517 bootDexJars := extractBootDexJarsFromHiddenAPIModules(ctx, contents)
Paul Duffin2fef1362021-04-15 13:32:00 +0100518 stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv")
519 rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, stubFlagsCSV, bootDexJars, stubJarsByKind)
520 rule.Build("modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags")
521
Paul Duffin537ea3d2021-05-14 10:38:00 +0100522 // Extract the classes jars from the contents.
523 classesJars := extractClassJarsFromHiddenAPIModules(ctx, contents)
524
Paul Duffin2fef1362021-04-15 13:32:00 +0100525 // Generate the set of flags from the annotations in the source code.
526 annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100527 buildRuleToGenerateAnnotationFlags(ctx, "modular hiddenapi annotation flags", classesJars, stubFlagsCSV, annotationFlagsCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100528
529 // Generate the metadata from the annotations in the source code.
530 metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100531 buildRuleToGenerateMetadata(ctx, "modular hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100532
Paul Duffin537ea3d2021-05-14 10:38:00 +0100533 // Generate the index file from the CSV files in the classes jars.
Paul Duffin2fef1362021-04-15 13:32:00 +0100534 indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv")
Paul Duffin850e61f2021-05-14 09:58:48 +0100535 buildRuleToGenerateIndex(ctx, "modular hiddenapi index", classesJars, indexCSV)
Paul Duffin2fef1362021-04-15 13:32:00 +0100536
537 // Removed APIs need to be marked and in order to do that the flagFileInfo needs to specify files
538 // containing dex signatures of all the removed APIs. In the monolithic files that is done by
539 // manually combining all the removed.txt files for each API and then converting them to dex
540 // signatures, see the combined-removed-dex module. That will all be done automatically in future.
541 // For now removed APIs are ignored.
542 // TODO(b/179354495): handle removed apis automatically.
543
544 // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex
545 // files.
546 outputPath := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv")
Paul Duffin438eb572021-05-21 16:58:23 +0100547 buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", outputPath, stubFlagsCSV, annotationFlagsCSV, flagFileInfo.FlagFilesByCategory, nil)
Paul Duffin2fef1362021-04-15 13:32:00 +0100548
549 // Store the paths in the info for use by other modules and sdk snapshot generation.
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100550 output := HiddenAPIFlagOutput{
551 StubFlagsPath: stubFlagsCSV,
552 AnnotationFlagsPath: annotationFlagsCSV,
553 MetadataPath: metadataCSV,
554 IndexPath: indexCSV,
555 AllFlagsPath: outputPath,
556 }
557 return &output
Paul Duffin702210b2021-04-08 20:12:41 +0100558}
Paul Duffin537ea3d2021-05-14 10:38:00 +0100559
560// gatherHiddenAPIModuleFromContents gathers the hiddenAPIModule from the supplied contents.
561func gatherHiddenAPIModuleFromContents(ctx android.ModuleContext, contents []android.Module) []hiddenAPIModule {
562 hiddenAPIModules := []hiddenAPIModule{}
563 for _, module := range contents {
564 if hiddenAPI, ok := module.(hiddenAPIModule); ok {
565 hiddenAPIModules = append(hiddenAPIModules, hiddenAPI)
566 } else if _, ok := module.(*DexImport); ok {
567 // Ignore this for the purposes of hidden API processing
568 } else {
569 ctx.ModuleErrorf("module %s does not implement hiddenAPIModule", module)
570 }
571 }
572 return hiddenAPIModules
573}
574
575// extractBootDexJarsFromHiddenAPIModules extracts the boot dex jars from the supplied modules.
576func extractBootDexJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths {
577 bootDexJars := android.Paths{}
578 for _, module := range contents {
579 bootDexJar := module.bootDexJar()
580 if bootDexJar == nil {
Paul Duffin7487a7a2021-05-19 09:36:09 +0100581 if ctx.Config().AlwaysUsePrebuiltSdks() {
582 // TODO(b/179354495): Remove this work around when it is unnecessary.
583 // Prebuilt modules like framework-wifi do not yet provide dex implementation jars. So,
584 // create a fake one that will cause a build error only if it is used.
585 fake := android.PathForModuleOut(ctx, "fake/boot-dex/%s.jar", module.Name())
586
587 // Create an error rule that pretends to create the output file but will actually fail if it
588 // is run.
589 ctx.Build(pctx, android.BuildParams{
590 Rule: android.ErrorRule,
591 Output: fake,
592 Args: map[string]string{
593 "error": fmt.Sprintf("missing dependencies: boot dex jar for %s", module),
594 },
595 })
596 bootDexJars = append(bootDexJars, fake)
597 } else {
598 ctx.ModuleErrorf("module %s does not provide a dex jar", module)
599 }
Paul Duffin537ea3d2021-05-14 10:38:00 +0100600 } else {
601 bootDexJars = append(bootDexJars, bootDexJar)
602 }
603 }
604 return bootDexJars
605}
606
607// extractClassJarsFromHiddenAPIModules extracts the class jars from the supplied modules.
608func extractClassJarsFromHiddenAPIModules(ctx android.ModuleContext, contents []hiddenAPIModule) android.Paths {
609 classesJars := android.Paths{}
610 for _, module := range contents {
611 classesJars = append(classesJars, module.classesJars()...)
612 }
613 return classesJars
614}