blob: 6c91a91406e49571af2fa3b64354a0306f71a3f4 [file] [log] [blame]
Colin Crossf24a22a2019-01-31 14:12:44 -08001// 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
15package java
16
17import (
18 "android/soong/android"
19)
20
21func init() {
Paul Duffin01289a22021-02-04 17:49:33 +000022 RegisterHiddenApiSingletonComponents(android.InitRegistrationContext)
23}
24
25func RegisterHiddenApiSingletonComponents(ctx android.RegistrationContext) {
26 ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
27 ctx.RegisterSingletonType("hiddenapi_index", hiddenAPIIndexSingletonFactory)
Colin Crossf24a22a2019-01-31 14:12:44 -080028}
29
Paul Duffin175947f2021-03-12 21:44:02 +000030var PrepareForTestWithHiddenApiBuildComponents = android.FixtureRegisterWithContext(RegisterHiddenApiSingletonComponents)
31
Colin Crossf24a22a2019-01-31 14:12:44 -080032type hiddenAPISingletonPathsStruct struct {
Paul Duffinff774a02021-01-29 12:53:15 +000033 // The path to the CSV file that contains the flags that will be encoded into the dex boot jars.
34 //
35 // It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with
36 // a number of additional files that are used to augment the information in the stubFlags with
37 // manually curated data.
38 flags android.OutputPath
39
40 // The path to the CSV index file that contains mappings from Java signature to source location
41 // information for all Java elements annotated with the UnsupportedAppUsage annotation in the
42 // source of all the boot jars.
43 //
44 // It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have
45 // been created by the rest of the build. That includes the index files generated for
46 // <x>-hiddenapi modules.
47 index android.OutputPath
48
49 // The path to the CSV metadata file that contains mappings from Java signature to the value of
50 // properties specified on UnsupportedAppUsage annotations in the source of all the boot jars.
51 //
52 // It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that
53 // have been created by the rest of the build. That includes the metadata files generated for
54 // <x>-hiddenapi modules.
55 metadata android.OutputPath
56
57 // The path to the CSV metadata file that contains mappings from Java signature to flags obtained
58 // from the public, system and test API stubs.
59 //
60 // This is created by the hiddenapi tool which is given dex files for the public, system and test
61 // API stubs (including product specific stubs) along with dex boot jars, so does not include
62 // <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which
63 // members in the dex boot jars match a member in the dex stub jars for that API surface and then
64 // outputs a file containing the signatures of all members in the dex boot jars along with the
65 // flags that indicate which API surface it belongs, if any.
66 //
67 // e.g. a dex member that matches a member in the public dex stubs would have flags
68 // "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex
69 // member that didn't match a member in any of the dex stubs is still output it just has an empty
70 // set of flags.
71 //
72 // The notion of matching is quite complex, it is not restricted to just exact matching but also
73 // follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing
74 // methods are also public. If an interface method is public and a class inherits an
75 // implementation of that method from a super class then that super class method is also public.
76 // That ensures that any method that can be called directly by an App through a public method is
77 // visible to that App.
78 //
79 // Propagating the visibility of members across the inheritance hierarchy at build time will cause
80 // problems when modularizing and unbundling as it that propagation can cross module boundaries.
81 // e.g. Say that a private framework class implements a public interface and inherits an
82 // implementation of one of its methods from a core platform ART class. In that case the ART
83 // implementation method needs to be marked as public which requires the build to have access to
84 // the framework implementation classes at build time. The work to rectify this is being tracked
85 // at http://b/178693149.
86 //
87 // This file (or at least those items marked as being in the public-api) is used by hiddenapi when
88 // creating the metadata and flags for the individual modules in order to perform consistency
89 // checks and filter out bridge methods that are part of the public API. The latter relies on the
90 // propagation of visibility across the inheritance hierarchy.
Artur Satayevb5df8a02020-02-19 16:39:59 +000091 stubFlags android.OutputPath
Colin Crossf24a22a2019-01-31 14:12:44 -080092}
93
94var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
95
96// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
97// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
98// yet been created.
99func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
100 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
Paul Duffin6a766452021-04-12 14:15:22 +0100101 // Make the paths relative to the out/soong/hiddenapi directory instead of to the out/soong/
102 // directory. This ensures that if they are used as java_resources they do not end up in a
103 // hiddenapi directory in the resulting APK.
104 hiddenapiDir := android.PathForOutput(ctx, "hiddenapi")
Colin Crossf24a22a2019-01-31 14:12:44 -0800105 return hiddenAPISingletonPathsStruct{
Paul Duffin6a766452021-04-12 14:15:22 +0100106 flags: hiddenapiDir.Join(ctx, "hiddenapi-flags.csv"),
107 index: hiddenapiDir.Join(ctx, "hiddenapi-index.csv"),
108 metadata: hiddenapiDir.Join(ctx, "hiddenapi-unsupported.csv"),
109 stubFlags: hiddenapiDir.Join(ctx, "hiddenapi-stub-flags.txt"),
Colin Crossf24a22a2019-01-31 14:12:44 -0800110 }
111 }).(hiddenAPISingletonPathsStruct)
112}
113
Colin Crossf24a22a2019-01-31 14:12:44 -0800114func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -0800115 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -0800116}
117
Colin Crossed023ec2019-02-19 12:38:45 -0800118type hiddenAPISingleton struct {
119 flags, metadata android.Path
120}
Colin Crossf24a22a2019-01-31 14:12:44 -0800121
122// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -0800123func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800124 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
125 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
126 return
127 }
128
129 stubFlagsRule(ctx)
130
Bill Peckhambae47492021-01-08 09:34:44 -0800131 // If there is a prebuilt hiddenapi dir, generate rules to use the
132 // files within. Generally, we build the hiddenapi files from source
133 // during the build, ensuring consistency. It's possible, in a split
134 // build (framework and vendor) scenario, for the vendor build to use
135 // prebuilt hiddenapi files from the framework build. In this scenario,
136 // the framework and vendor builds must use the same source to ensure
137 // consistency.
138
139 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
140 h.flags = prebuiltFlagsRule(ctx)
141 return
142 }
143
Colin Crossf24a22a2019-01-31 14:12:44 -0800144 // These rules depend on files located in frameworks/base, skip them if running in a tree that doesn't have them.
Jiyong Park09cb6292019-07-15 15:29:23 +0900145 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -0800146 h.flags = flagsRule(ctx)
147 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800148 } else {
Colin Crossed023ec2019-02-19 12:38:45 -0800149 h.flags = emptyFlagsRule(ctx)
150 }
151}
152
153// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
154// Both paths are used to call dist-for-goals.
155func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
156 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
157 return
158 }
159
160 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
161
162 if h.metadata != nil {
163 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -0800164 }
165}
166
167// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
168// modules.
169func stubFlagsRule(ctx android.SingletonContext) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100170 var publicStubModules []string
171 var systemStubModules []string
172 var testStubModules []string
173 var corePlatformStubModules []string
174
175 if ctx.Config().AlwaysUsePrebuiltSdks() {
176 // Build configuration mandates using prebuilt stub modules
177 publicStubModules = append(publicStubModules, "sdk_public_current_android")
178 systemStubModules = append(systemStubModules, "sdk_system_current_android")
179 testStubModules = append(testStubModules, "sdk_test_current_android")
180 } else {
181 // Use stub modules built from source
182 publicStubModules = append(publicStubModules, "android_stubs_current")
183 systemStubModules = append(systemStubModules, "android_system_stubs_current")
184 testStubModules = append(testStubModules, "android_test_stubs_current")
Paul Duffin719fed42019-02-28 16:15:44 +0000185 }
Anton Hanssona2adc372020-07-03 15:31:32 +0100186 // We do not have prebuilts of the core platform api yet
187 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
Paul Duffin719fed42019-02-28 16:15:44 +0000188
Colin Crossf24a22a2019-01-31 14:12:44 -0800189 // Allow products to define their own stubs for custom product jars that apps can use.
190 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
191 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
192 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800193 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
194 publicStubModules = append(publicStubModules, "jacoco-stubs")
195 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800196
197 publicStubPaths := make(android.Paths, len(publicStubModules))
198 systemStubPaths := make(android.Paths, len(systemStubModules))
199 testStubPaths := make(android.Paths, len(testStubModules))
200 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
201
202 moduleListToPathList := map[*[]string]android.Paths{
203 &publicStubModules: publicStubPaths,
204 &systemStubModules: systemStubPaths,
205 &testStubModules: testStubPaths,
206 &corePlatformStubModules: corePlatformStubPaths,
207 }
208
209 var bootDexJars android.Paths
210
211 ctx.VisitAllModules(func(module android.Module) {
212 // Collect dex jar paths for the modules listed above.
Colin Crossdcf71b22021-02-01 13:59:03 -0800213 if j, ok := module.(UsesLibraryDependency); ok {
Colin Crossf24a22a2019-01-31 14:12:44 -0800214 name := ctx.ModuleName(module)
215 for moduleList, pathList := range moduleListToPathList {
216 if i := android.IndexList(name, *moduleList); i != -1 {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000217 pathList[i] = j.DexJarBuildPath()
Colin Crossf24a22a2019-01-31 14:12:44 -0800218 }
219 }
220 }
221
222 // Collect dex jar paths for modules that had hiddenapi encode called on them.
223 if h, ok := module.(hiddenAPIIntf); ok {
224 if jar := h.bootDexJar(); jar != nil {
225 bootDexJars = append(bootDexJars, jar)
226 }
227 }
228 })
229
230 var missingDeps []string
231 // Ensure all modules were converted to paths
232 for moduleList, pathList := range moduleListToPathList {
233 for i := range pathList {
234 if pathList[i] == nil {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000235 moduleName := (*moduleList)[i]
236 pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800237 if ctx.Config().AllowMissingDependencies() {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000238 missingDeps = append(missingDeps, moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800239 } else {
240 ctx.Errorf("failed to find dex jar path for module %q",
Paul Duffin7f48eef2020-12-03 11:15:58 +0000241 moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800242 }
243 }
244 }
245 }
246
247 // Singleton rule which applies hiddenapi on all boot class path dex files.
Colin Crossf1a035e2020-11-16 17:32:30 -0800248 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800249
250 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
251 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
252
253 rule.MissingDeps(missingDeps)
254
255 rule.Command().
Martin Stjernholm7260d062019-12-09 21:47:14 +0000256 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800257 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800258 FlagForEachInput("--boot-dex=", bootDexJars).
259 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000260 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
261 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800262 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
263 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800264
265 commitChangeForRestat(rule, tempPath, outputPath)
266
Colin Crossf1a035e2020-11-16 17:32:30 -0800267 rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags")
Colin Crossf24a22a2019-01-31 14:12:44 -0800268}
269
Paul Duffindd63d6d2021-02-03 18:34:00 +0000270// Checks to see whether the supplied module variant is in the list of boot jars.
271//
272// This is similar to logic in getBootImageJar() so any changes needed here are likely to be needed
273// there too.
274//
275// TODO(b/179354495): Avoid having to perform this type of check or if necessary dedup it.
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000276func isModuleInConfiguredList(ctx android.BaseModuleContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool {
277 name := ctx.OtherModuleName(module)
Paul Duffindd63d6d2021-02-03 18:34:00 +0000278
279 // Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed.
280 name = android.RemoveOptionalPrebuiltPrefix(name)
281
282 // Ignore any module that is not listed in the boot image configuration.
283 index := configuredBootJars.IndexOfJar(name)
284 if index == -1 {
285 return false
286 }
287
288 // It is an error if the module is not an ApexModule.
289 if _, ok := module.(android.ApexModule); !ok {
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000290 ctx.ModuleErrorf("is configured in boot jars but does not support being added to an apex")
Paul Duffindd63d6d2021-02-03 18:34:00 +0000291 return false
292 }
293
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000294 apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
Paul Duffindd63d6d2021-02-03 18:34:00 +0000295
296 // Now match the apex part of the boot image configuration.
297 requiredApex := configuredBootJars.Apex(index)
298 if requiredApex == "platform" {
299 if len(apexInfo.InApexes) != 0 {
300 // A platform variant is required but this is for an apex so ignore it.
301 return false
302 }
303 } else if !apexInfo.InApexByBaseName(requiredApex) {
304 // An apex variant for a specific apex is required but this is the wrong apex.
305 return false
306 }
307
308 return true
309}
310
Bill Peckhambae47492021-01-08 09:34:44 -0800311func prebuiltFlagsRule(ctx android.SingletonContext) android.Path {
312 outputPath := hiddenAPISingletonPaths(ctx).flags
313 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv")
314
315 ctx.Build(pctx, android.BuildParams{
316 Rule: android.Cp,
317 Output: outputPath,
318 Input: inputPath,
319 })
320
321 return outputPath
322}
323
Paul Duffin702210b2021-04-08 20:12:41 +0100324// flagsRule is a placeholder that simply returns the location of the file, the generation of the
325// ninja rules is done in generateHiddenAPIBuildActions.
Colin Crossed023ec2019-02-19 12:38:45 -0800326func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800327 outputPath := hiddenAPISingletonPaths(ctx).flags
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100328 return outputPath
329}
330
Colin Crossf24a22a2019-01-31 14:12:44 -0800331// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
332// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800333func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf1a035e2020-11-16 17:32:30 -0800334 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800335
336 outputPath := hiddenAPISingletonPaths(ctx).flags
337
Colin Cross69f59a32019-02-15 10:39:37 -0800338 rule.Command().Text("rm").Flag("-f").Output(outputPath)
339 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800340
Colin Crossf1a035e2020-11-16 17:32:30 -0800341 rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800342
343 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800344}
345
Andrei Onea47841972020-08-10 17:23:52 +0100346// metadataRule creates a rule to build hiddenapi-unsupported.csv out of the metadata.csv files generated for boot image
Colin Crossf24a22a2019-01-31 14:12:44 -0800347// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800348func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800349 var metadataCSV android.Paths
350
351 ctx.VisitAllModules(func(module android.Module) {
352 if h, ok := module.(hiddenAPIIntf); ok {
353 if csv := h.metadataCSV(); csv != nil {
354 metadataCSV = append(metadataCSV, csv)
355 }
356 }
357 })
358
Colin Crossf1a035e2020-11-16 17:32:30 -0800359 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800360
361 outputPath := hiddenAPISingletonPaths(ctx).metadata
362
363 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800364 BuiltTool("merge_csv").
Paul Duffin2c36f242021-02-16 16:57:06 +0000365 Flag("--key_field signature").
Artur Satayev79fac052020-01-20 19:11:33 +0000366 FlagWithOutput("--output=", outputPath).
367 Inputs(metadataCSV)
Colin Crossf24a22a2019-01-31 14:12:44 -0800368
Colin Crossf1a035e2020-11-16 17:32:30 -0800369 rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800370
371 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800372}
373
374// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
375// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
376// the rule.
377func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
378 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800379 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800380 rule.Command().
381 Text("(").
382 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800383 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800384 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800385 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800386 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800387 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800388 Text("fi").
389 Text(")")
390}
Paul Duffin1b033f52019-06-10 14:15:04 +0100391
Artur Satayevb5df8a02020-02-19 16:39:59 +0000392func hiddenAPIIndexSingletonFactory() android.Singleton {
393 return &hiddenAPIIndexSingleton{}
394}
395
396type hiddenAPIIndexSingleton struct {
397 index android.Path
398}
399
400func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) {
401 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
402 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
403 return
404 }
405
Bill Peckhambae47492021-01-08 09:34:44 -0800406 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
407 outputPath := hiddenAPISingletonPaths(ctx).index
408 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv")
409
410 ctx.Build(pctx, android.BuildParams{
411 Rule: android.Cp,
412 Output: outputPath,
413 Input: inputPath,
414 })
415
416 h.index = outputPath
417 return
418 }
Artur Satayevb5df8a02020-02-19 16:39:59 +0000419}