blob: 25d39f34637b59ca402170b1da8595766205f443 [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 (
Paul Duffin1b033f52019-06-10 14:15:04 +010018 "fmt"
19
Colin Crossf24a22a2019-01-31 14:12:44 -080020 "android/soong/android"
Anton Hanssonb3cbd612020-10-06 12:04:34 +010021 "android/soong/genrule"
Colin Crossf24a22a2019-01-31 14:12:44 -080022)
23
24func init() {
Paul Duffin01289a22021-02-04 17:49:33 +000025 RegisterHiddenApiSingletonComponents(android.InitRegistrationContext)
26}
27
28func RegisterHiddenApiSingletonComponents(ctx android.RegistrationContext) {
29 ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
30 ctx.RegisterSingletonType("hiddenapi_index", hiddenAPIIndexSingletonFactory)
31 ctx.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory)
Colin Crossf24a22a2019-01-31 14:12:44 -080032}
33
34type hiddenAPISingletonPathsStruct struct {
Paul Duffinff774a02021-01-29 12:53:15 +000035 // The path to the CSV file that contains the flags that will be encoded into the dex boot jars.
36 //
37 // It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with
38 // a number of additional files that are used to augment the information in the stubFlags with
39 // manually curated data.
40 flags android.OutputPath
41
42 // The path to the CSV index file that contains mappings from Java signature to source location
43 // information for all Java elements annotated with the UnsupportedAppUsage annotation in the
44 // source of all the boot jars.
45 //
46 // It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have
47 // been created by the rest of the build. That includes the index files generated for
48 // <x>-hiddenapi modules.
49 index android.OutputPath
50
51 // The path to the CSV metadata file that contains mappings from Java signature to the value of
52 // properties specified on UnsupportedAppUsage annotations in the source of all the boot jars.
53 //
54 // It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that
55 // have been created by the rest of the build. That includes the metadata files generated for
56 // <x>-hiddenapi modules.
57 metadata android.OutputPath
58
59 // The path to the CSV metadata file that contains mappings from Java signature to flags obtained
60 // from the public, system and test API stubs.
61 //
62 // This is created by the hiddenapi tool which is given dex files for the public, system and test
63 // API stubs (including product specific stubs) along with dex boot jars, so does not include
64 // <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which
65 // members in the dex boot jars match a member in the dex stub jars for that API surface and then
66 // outputs a file containing the signatures of all members in the dex boot jars along with the
67 // flags that indicate which API surface it belongs, if any.
68 //
69 // e.g. a dex member that matches a member in the public dex stubs would have flags
70 // "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex
71 // member that didn't match a member in any of the dex stubs is still output it just has an empty
72 // set of flags.
73 //
74 // The notion of matching is quite complex, it is not restricted to just exact matching but also
75 // follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing
76 // methods are also public. If an interface method is public and a class inherits an
77 // implementation of that method from a super class then that super class method is also public.
78 // That ensures that any method that can be called directly by an App through a public method is
79 // visible to that App.
80 //
81 // Propagating the visibility of members across the inheritance hierarchy at build time will cause
82 // problems when modularizing and unbundling as it that propagation can cross module boundaries.
83 // e.g. Say that a private framework class implements a public interface and inherits an
84 // implementation of one of its methods from a core platform ART class. In that case the ART
85 // implementation method needs to be marked as public which requires the build to have access to
86 // the framework implementation classes at build time. The work to rectify this is being tracked
87 // at http://b/178693149.
88 //
89 // This file (or at least those items marked as being in the public-api) is used by hiddenapi when
90 // creating the metadata and flags for the individual modules in order to perform consistency
91 // checks and filter out bridge methods that are part of the public API. The latter relies on the
92 // propagation of visibility across the inheritance hierarchy.
Artur Satayevb5df8a02020-02-19 16:39:59 +000093 stubFlags android.OutputPath
Colin Crossf24a22a2019-01-31 14:12:44 -080094}
95
96var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
97
98// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
99// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
100// yet been created.
101func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
102 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
103 return hiddenAPISingletonPathsStruct{
Colin Crossf24a22a2019-01-31 14:12:44 -0800104 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +0000105 index: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-index.csv"),
Andrei Onea47841972020-08-10 17:23:52 +0100106 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-unsupported.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +0000107 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
Colin Crossf24a22a2019-01-31 14:12:44 -0800108 }
109 }).(hiddenAPISingletonPathsStruct)
110}
111
Colin Crossf24a22a2019-01-31 14:12:44 -0800112func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -0800113 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -0800114}
115
Colin Crossed023ec2019-02-19 12:38:45 -0800116type hiddenAPISingleton struct {
117 flags, metadata android.Path
118}
Colin Crossf24a22a2019-01-31 14:12:44 -0800119
120// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -0800121func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800122 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
123 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
124 return
125 }
126
127 stubFlagsRule(ctx)
128
Bill Peckhambae47492021-01-08 09:34:44 -0800129 // If there is a prebuilt hiddenapi dir, generate rules to use the
130 // files within. Generally, we build the hiddenapi files from source
131 // during the build, ensuring consistency. It's possible, in a split
132 // build (framework and vendor) scenario, for the vendor build to use
133 // prebuilt hiddenapi files from the framework build. In this scenario,
134 // the framework and vendor builds must use the same source to ensure
135 // consistency.
136
137 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
138 h.flags = prebuiltFlagsRule(ctx)
139 return
140 }
141
Colin Crossf24a22a2019-01-31 14:12:44 -0800142 // 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 +0900143 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -0800144 h.flags = flagsRule(ctx)
145 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800146 } else {
Colin Crossed023ec2019-02-19 12:38:45 -0800147 h.flags = emptyFlagsRule(ctx)
148 }
149}
150
151// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
152// Both paths are used to call dist-for-goals.
153func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
154 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
155 return
156 }
157
158 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
159
160 if h.metadata != nil {
161 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -0800162 }
163}
164
165// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
166// modules.
167func stubFlagsRule(ctx android.SingletonContext) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100168 var publicStubModules []string
169 var systemStubModules []string
170 var testStubModules []string
171 var corePlatformStubModules []string
172
173 if ctx.Config().AlwaysUsePrebuiltSdks() {
174 // Build configuration mandates using prebuilt stub modules
175 publicStubModules = append(publicStubModules, "sdk_public_current_android")
176 systemStubModules = append(systemStubModules, "sdk_system_current_android")
177 testStubModules = append(testStubModules, "sdk_test_current_android")
178 } else {
179 // Use stub modules built from source
180 publicStubModules = append(publicStubModules, "android_stubs_current")
181 systemStubModules = append(systemStubModules, "android_system_stubs_current")
182 testStubModules = append(testStubModules, "android_test_stubs_current")
Paul Duffin719fed42019-02-28 16:15:44 +0000183 }
Anton Hanssona2adc372020-07-03 15:31:32 +0100184 // We do not have prebuilts of the core platform api yet
185 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
Paul Duffin719fed42019-02-28 16:15:44 +0000186
187 // Add the android.test.base to the set of stubs only if the android.test.base module is on
188 // the boot jars list as the runtime will only enforce hiddenapi access against modules on
189 // that list.
Anton Hanssona2adc372020-07-03 15:31:32 +0100190 if inList("android.test.base", ctx.Config().BootJars()) {
191 if ctx.Config().AlwaysUsePrebuiltSdks() {
192 publicStubModules = append(publicStubModules, "sdk_public_current_android.test.base")
193 } else {
194 publicStubModules = append(publicStubModules, "android.test.base.stubs")
195 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800196 }
197
198 // Allow products to define their own stubs for custom product jars that apps can use.
199 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
200 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
201 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800202 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
203 publicStubModules = append(publicStubModules, "jacoco-stubs")
204 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800205
206 publicStubPaths := make(android.Paths, len(publicStubModules))
207 systemStubPaths := make(android.Paths, len(systemStubModules))
208 testStubPaths := make(android.Paths, len(testStubModules))
209 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
210
211 moduleListToPathList := map[*[]string]android.Paths{
212 &publicStubModules: publicStubPaths,
213 &systemStubModules: systemStubPaths,
214 &testStubModules: testStubPaths,
215 &corePlatformStubModules: corePlatformStubPaths,
216 }
217
218 var bootDexJars android.Paths
219
220 ctx.VisitAllModules(func(module android.Module) {
221 // Collect dex jar paths for the modules listed above.
Colin Crossdcf71b22021-02-01 13:59:03 -0800222 if j, ok := module.(UsesLibraryDependency); ok {
Colin Crossf24a22a2019-01-31 14:12:44 -0800223 name := ctx.ModuleName(module)
224 for moduleList, pathList := range moduleListToPathList {
225 if i := android.IndexList(name, *moduleList); i != -1 {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000226 pathList[i] = j.DexJarBuildPath()
Colin Crossf24a22a2019-01-31 14:12:44 -0800227 }
228 }
229 }
230
231 // Collect dex jar paths for modules that had hiddenapi encode called on them.
232 if h, ok := module.(hiddenAPIIntf); ok {
233 if jar := h.bootDexJar(); jar != nil {
234 bootDexJars = append(bootDexJars, jar)
235 }
236 }
237 })
238
239 var missingDeps []string
240 // Ensure all modules were converted to paths
241 for moduleList, pathList := range moduleListToPathList {
242 for i := range pathList {
243 if pathList[i] == nil {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000244 moduleName := (*moduleList)[i]
245 pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800246 if ctx.Config().AllowMissingDependencies() {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000247 missingDeps = append(missingDeps, moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800248 } else {
249 ctx.Errorf("failed to find dex jar path for module %q",
Paul Duffin7f48eef2020-12-03 11:15:58 +0000250 moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800251 }
252 }
253 }
254 }
255
256 // Singleton rule which applies hiddenapi on all boot class path dex files.
Colin Crossf1a035e2020-11-16 17:32:30 -0800257 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800258
259 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
260 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
261
262 rule.MissingDeps(missingDeps)
263
264 rule.Command().
Martin Stjernholm7260d062019-12-09 21:47:14 +0000265 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800266 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800267 FlagForEachInput("--boot-dex=", bootDexJars).
268 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000269 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
270 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800271 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
272 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800273
274 commitChangeForRestat(rule, tempPath, outputPath)
275
Colin Crossf1a035e2020-11-16 17:32:30 -0800276 rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags")
Colin Crossf24a22a2019-01-31 14:12:44 -0800277}
278
Paul Duffindd63d6d2021-02-03 18:34:00 +0000279// Checks to see whether the supplied module variant is in the list of boot jars.
280//
281// This is similar to logic in getBootImageJar() so any changes needed here are likely to be needed
282// there too.
283//
284// TODO(b/179354495): Avoid having to perform this type of check or if necessary dedup it.
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000285func isModuleInConfiguredList(ctx android.BaseModuleContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool {
286 name := ctx.OtherModuleName(module)
Paul Duffindd63d6d2021-02-03 18:34:00 +0000287
288 // Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed.
289 name = android.RemoveOptionalPrebuiltPrefix(name)
290
291 // Ignore any module that is not listed in the boot image configuration.
292 index := configuredBootJars.IndexOfJar(name)
293 if index == -1 {
294 return false
295 }
296
297 // It is an error if the module is not an ApexModule.
298 if _, ok := module.(android.ApexModule); !ok {
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000299 ctx.ModuleErrorf("is configured in boot jars but does not support being added to an apex")
Paul Duffindd63d6d2021-02-03 18:34:00 +0000300 return false
301 }
302
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000303 apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
Paul Duffindd63d6d2021-02-03 18:34:00 +0000304
305 // Now match the apex part of the boot image configuration.
306 requiredApex := configuredBootJars.Apex(index)
307 if requiredApex == "platform" {
308 if len(apexInfo.InApexes) != 0 {
309 // A platform variant is required but this is for an apex so ignore it.
310 return false
311 }
312 } else if !apexInfo.InApexByBaseName(requiredApex) {
313 // An apex variant for a specific apex is required but this is the wrong apex.
314 return false
315 }
316
317 return true
318}
319
Bill Peckhambae47492021-01-08 09:34:44 -0800320func prebuiltFlagsRule(ctx android.SingletonContext) android.Path {
321 outputPath := hiddenAPISingletonPaths(ctx).flags
322 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv")
323
324 ctx.Build(pctx, android.BuildParams{
325 Rule: android.Cp,
326 Output: outputPath,
327 Input: inputPath,
328 })
329
330 return outputPath
331}
332
Colin Crossf24a22a2019-01-31 14:12:44 -0800333// flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000334// the unsupported API.
Colin Crossed023ec2019-02-19 12:38:45 -0800335func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800336 var flagsCSV android.Paths
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100337 var combinedRemovedApis android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -0800338
339 ctx.VisitAllModules(func(module android.Module) {
340 if h, ok := module.(hiddenAPIIntf); ok {
341 if csv := h.flagsCSV(); csv != nil {
342 flagsCSV = append(flagsCSV, csv)
343 }
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100344 } else if g, ok := module.(*genrule.Module); ok {
345 if ctx.ModuleName(module) == "combined-removed-dex" {
346 if len(g.GeneratedSourceFiles()) != 1 || combinedRemovedApis != nil {
347 ctx.Errorf("Expected 1 combined-removed-dex module that generates 1 output file.")
348 }
349 combinedRemovedApis = g.GeneratedSourceFiles()[0]
Artur Satayevc7fb5c92020-03-25 16:48:49 +0000350 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800351 }
352 })
353
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100354 if combinedRemovedApis == nil {
355 ctx.Errorf("Failed to find combined-removed-dex.")
356 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800357
Colin Crossf1a035e2020-11-16 17:32:30 -0800358 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800359
360 outputPath := hiddenAPISingletonPaths(ctx).flags
361 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
362
363 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
364
365 rule.Command().
Paul Duffinfdada682021-02-08 18:08:09 +0000366 BuiltTool("generate_hiddenapi_lists").
Colin Cross69f59a32019-02-15 10:39:37 -0800367 FlagWithInput("--csv ", stubFlags).
368 Inputs(flagsCSV).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000369 FlagWithInput("--unsupported ",
Andrei Oneaca790812020-08-04 15:34:35 +0100370 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-unsupported.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100371 FlagWithInput("--unsupported ", combinedRemovedApis).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed").
Mathew Inwoodc1be2f82021-01-13 15:49:17 +0000372 FlagWithInput("--max-target-r ",
373 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-r-loprio.txt")).FlagWithArg("--tag ", "lo-prio").
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000374 FlagWithInput("--max-target-q ",
Andrei Oneaca790812020-08-04 15:34:35 +0100375 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-q.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000376 FlagWithInput("--max-target-p ",
Andrei Oneaca790812020-08-04 15:34:35 +0100377 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-p.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100378 FlagWithInput("--max-target-o ", android.PathForSource(
Mathew Inwood1ef4ba92020-11-10 14:49:43 +0000379 ctx, "frameworks/base/config/hiddenapi-max-target-o.txt")).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio").
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000380 FlagWithInput("--blocked ",
Andrei Oneaca790812020-08-04 15:34:35 +0100381 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blocked.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100382 FlagWithInput("--unsupported ", android.PathForSource(
383 ctx, "frameworks/base/config/hiddenapi-unsupported-packages.txt")).Flag("--packages ").
Colin Cross69f59a32019-02-15 10:39:37 -0800384 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800385
386 commitChangeForRestat(rule, tempPath, outputPath)
387
Colin Crossf1a035e2020-11-16 17:32:30 -0800388 rule.Build("hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800389
390 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800391}
392
393// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
394// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800395func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf1a035e2020-11-16 17:32:30 -0800396 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800397
398 outputPath := hiddenAPISingletonPaths(ctx).flags
399
Colin Cross69f59a32019-02-15 10:39:37 -0800400 rule.Command().Text("rm").Flag("-f").Output(outputPath)
401 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800402
Colin Crossf1a035e2020-11-16 17:32:30 -0800403 rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800404
405 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800406}
407
Andrei Onea47841972020-08-10 17:23:52 +0100408// 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 -0800409// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800410func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800411 var metadataCSV android.Paths
412
413 ctx.VisitAllModules(func(module android.Module) {
414 if h, ok := module.(hiddenAPIIntf); ok {
415 if csv := h.metadataCSV(); csv != nil {
416 metadataCSV = append(metadataCSV, csv)
417 }
418 }
419 })
420
Colin Crossf1a035e2020-11-16 17:32:30 -0800421 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800422
423 outputPath := hiddenAPISingletonPaths(ctx).metadata
424
425 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800426 BuiltTool("merge_csv").
Artur Satayev79fac052020-01-20 19:11:33 +0000427 FlagWithOutput("--output=", outputPath).
428 Inputs(metadataCSV)
Colin Crossf24a22a2019-01-31 14:12:44 -0800429
Colin Crossf1a035e2020-11-16 17:32:30 -0800430 rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800431
432 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800433}
434
435// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
436// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
437// the rule.
438func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
439 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800440 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800441 rule.Command().
442 Text("(").
443 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800444 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800445 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800446 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800447 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800448 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800449 Text("fi").
450 Text(")")
451}
Paul Duffin1b033f52019-06-10 14:15:04 +0100452
453type hiddenAPIFlagsProperties struct {
454 // name of the file into which the flags will be copied.
455 Filename *string
456}
457
458type hiddenAPIFlags struct {
459 android.ModuleBase
460
461 properties hiddenAPIFlagsProperties
462
463 outputFilePath android.OutputPath
464}
465
466func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
467 filename := String(h.properties.Filename)
468
469 inputPath := hiddenAPISingletonPaths(ctx).flags
470 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
471
472 // This ensures that outputFilePath has the correct name for others to
473 // use, as the source file may have a different name.
474 ctx.Build(pctx, android.BuildParams{
475 Rule: android.Cp,
476 Output: h.outputFilePath,
477 Input: inputPath,
478 })
479}
480
481func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
482 switch tag {
483 case "":
484 return android.Paths{h.outputFilePath}, nil
485 default:
486 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
487 }
488}
489
490// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
491func hiddenAPIFlagsFactory() android.Module {
492 module := &hiddenAPIFlags{}
493 module.AddProperties(&module.properties)
494 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
495 return module
496}
Artur Satayevb5df8a02020-02-19 16:39:59 +0000497
498func hiddenAPIIndexSingletonFactory() android.Singleton {
499 return &hiddenAPIIndexSingleton{}
500}
501
502type hiddenAPIIndexSingleton struct {
503 index android.Path
504}
505
506func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) {
507 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
508 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
509 return
510 }
511
Bill Peckhambae47492021-01-08 09:34:44 -0800512 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
513 outputPath := hiddenAPISingletonPaths(ctx).index
514 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv")
515
516 ctx.Build(pctx, android.BuildParams{
517 Rule: android.Cp,
518 Output: outputPath,
519 Input: inputPath,
520 })
521
522 h.index = outputPath
523 return
524 }
525
Artur Satayevb5df8a02020-02-19 16:39:59 +0000526 indexes := android.Paths{}
527 ctx.VisitAllModules(func(module android.Module) {
528 if h, ok := module.(hiddenAPIIntf); ok {
529 if h.indexCSV() != nil {
530 indexes = append(indexes, h.indexCSV())
531 }
532 }
533 })
534
Colin Crossf1a035e2020-11-16 17:32:30 -0800535 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000536 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800537 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000538 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
539 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
540 Inputs(indexes)
Colin Crossf1a035e2020-11-16 17:32:30 -0800541 rule.Build("singleton-merged-hiddenapi-index", "Singleton merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000542
543 h.index = hiddenAPISingletonPaths(ctx).index
544}
545
546func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) {
547 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
548 return
549 }
550
551 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String())
552}