blob: ccb87450637e19c63d682f04f10df8ee68949318 [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() {
25 android.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
Artur Satayevb5df8a02020-02-19 16:39:59 +000026 android.RegisterSingletonType("hiddenapi_index", hiddenAPIIndexSingletonFactory)
Paul Duffin1b033f52019-06-10 14:15:04 +010027 android.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory)
Colin Crossf24a22a2019-01-31 14:12:44 -080028}
29
30type hiddenAPISingletonPathsStruct struct {
Paul Duffinff774a02021-01-29 12:53:15 +000031 // The path to the CSV file that contains the flags that will be encoded into the dex boot jars.
32 //
33 // It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with
34 // a number of additional files that are used to augment the information in the stubFlags with
35 // manually curated data.
36 flags android.OutputPath
37
38 // The path to the CSV index file that contains mappings from Java signature to source location
39 // information for all Java elements annotated with the UnsupportedAppUsage annotation in the
40 // source of all the boot jars.
41 //
42 // It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have
43 // been created by the rest of the build. That includes the index files generated for
44 // <x>-hiddenapi modules.
45 index android.OutputPath
46
47 // The path to the CSV metadata file that contains mappings from Java signature to the value of
48 // properties specified on UnsupportedAppUsage annotations in the source of all the boot jars.
49 //
50 // It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that
51 // have been created by the rest of the build. That includes the metadata files generated for
52 // <x>-hiddenapi modules.
53 metadata android.OutputPath
54
55 // The path to the CSV metadata file that contains mappings from Java signature to flags obtained
56 // from the public, system and test API stubs.
57 //
58 // This is created by the hiddenapi tool which is given dex files for the public, system and test
59 // API stubs (including product specific stubs) along with dex boot jars, so does not include
60 // <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which
61 // members in the dex boot jars match a member in the dex stub jars for that API surface and then
62 // outputs a file containing the signatures of all members in the dex boot jars along with the
63 // flags that indicate which API surface it belongs, if any.
64 //
65 // e.g. a dex member that matches a member in the public dex stubs would have flags
66 // "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex
67 // member that didn't match a member in any of the dex stubs is still output it just has an empty
68 // set of flags.
69 //
70 // The notion of matching is quite complex, it is not restricted to just exact matching but also
71 // follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing
72 // methods are also public. If an interface method is public and a class inherits an
73 // implementation of that method from a super class then that super class method is also public.
74 // That ensures that any method that can be called directly by an App through a public method is
75 // visible to that App.
76 //
77 // Propagating the visibility of members across the inheritance hierarchy at build time will cause
78 // problems when modularizing and unbundling as it that propagation can cross module boundaries.
79 // e.g. Say that a private framework class implements a public interface and inherits an
80 // implementation of one of its methods from a core platform ART class. In that case the ART
81 // implementation method needs to be marked as public which requires the build to have access to
82 // the framework implementation classes at build time. The work to rectify this is being tracked
83 // at http://b/178693149.
84 //
85 // This file (or at least those items marked as being in the public-api) is used by hiddenapi when
86 // creating the metadata and flags for the individual modules in order to perform consistency
87 // checks and filter out bridge methods that are part of the public API. The latter relies on the
88 // propagation of visibility across the inheritance hierarchy.
Artur Satayevb5df8a02020-02-19 16:39:59 +000089 stubFlags android.OutputPath
Colin Crossf24a22a2019-01-31 14:12:44 -080090}
91
92var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
93
94// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
95// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
96// yet been created.
97func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
98 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
99 return hiddenAPISingletonPathsStruct{
Colin Crossf24a22a2019-01-31 14:12:44 -0800100 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +0000101 index: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-index.csv"),
Andrei Onea47841972020-08-10 17:23:52 +0100102 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-unsupported.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +0000103 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
Colin Crossf24a22a2019-01-31 14:12:44 -0800104 }
105 }).(hiddenAPISingletonPathsStruct)
106}
107
Colin Crossf24a22a2019-01-31 14:12:44 -0800108func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -0800109 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -0800110}
111
Colin Crossed023ec2019-02-19 12:38:45 -0800112type hiddenAPISingleton struct {
113 flags, metadata android.Path
114}
Colin Crossf24a22a2019-01-31 14:12:44 -0800115
116// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -0800117func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800118 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
119 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
120 return
121 }
122
123 stubFlagsRule(ctx)
124
Bill Peckhambae47492021-01-08 09:34:44 -0800125 // If there is a prebuilt hiddenapi dir, generate rules to use the
126 // files within. Generally, we build the hiddenapi files from source
127 // during the build, ensuring consistency. It's possible, in a split
128 // build (framework and vendor) scenario, for the vendor build to use
129 // prebuilt hiddenapi files from the framework build. In this scenario,
130 // the framework and vendor builds must use the same source to ensure
131 // consistency.
132
133 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
134 h.flags = prebuiltFlagsRule(ctx)
135 return
136 }
137
Colin Crossf24a22a2019-01-31 14:12:44 -0800138 // 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 +0900139 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -0800140 h.flags = flagsRule(ctx)
141 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800142 } else {
Colin Crossed023ec2019-02-19 12:38:45 -0800143 h.flags = emptyFlagsRule(ctx)
144 }
145}
146
147// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
148// Both paths are used to call dist-for-goals.
149func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
150 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
151 return
152 }
153
154 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
155
156 if h.metadata != nil {
157 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -0800158 }
159}
160
161// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
162// modules.
163func stubFlagsRule(ctx android.SingletonContext) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100164 var publicStubModules []string
165 var systemStubModules []string
166 var testStubModules []string
167 var corePlatformStubModules []string
168
169 if ctx.Config().AlwaysUsePrebuiltSdks() {
170 // Build configuration mandates using prebuilt stub modules
171 publicStubModules = append(publicStubModules, "sdk_public_current_android")
172 systemStubModules = append(systemStubModules, "sdk_system_current_android")
173 testStubModules = append(testStubModules, "sdk_test_current_android")
174 } else {
175 // Use stub modules built from source
176 publicStubModules = append(publicStubModules, "android_stubs_current")
177 systemStubModules = append(systemStubModules, "android_system_stubs_current")
178 testStubModules = append(testStubModules, "android_test_stubs_current")
Paul Duffin719fed42019-02-28 16:15:44 +0000179 }
Anton Hanssona2adc372020-07-03 15:31:32 +0100180 // We do not have prebuilts of the core platform api yet
181 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
Paul Duffin719fed42019-02-28 16:15:44 +0000182
183 // Add the android.test.base to the set of stubs only if the android.test.base module is on
184 // the boot jars list as the runtime will only enforce hiddenapi access against modules on
185 // that list.
Anton Hanssona2adc372020-07-03 15:31:32 +0100186 if inList("android.test.base", ctx.Config().BootJars()) {
187 if ctx.Config().AlwaysUsePrebuiltSdks() {
188 publicStubModules = append(publicStubModules, "sdk_public_current_android.test.base")
189 } else {
190 publicStubModules = append(publicStubModules, "android.test.base.stubs")
191 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800192 }
193
194 // Allow products to define their own stubs for custom product jars that apps can use.
195 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
196 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
197 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800198 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
199 publicStubModules = append(publicStubModules, "jacoco-stubs")
200 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800201
202 publicStubPaths := make(android.Paths, len(publicStubModules))
203 systemStubPaths := make(android.Paths, len(systemStubModules))
204 testStubPaths := make(android.Paths, len(testStubModules))
205 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
206
207 moduleListToPathList := map[*[]string]android.Paths{
208 &publicStubModules: publicStubPaths,
209 &systemStubModules: systemStubPaths,
210 &testStubModules: testStubPaths,
211 &corePlatformStubModules: corePlatformStubPaths,
212 }
213
214 var bootDexJars android.Paths
215
216 ctx.VisitAllModules(func(module android.Module) {
217 // Collect dex jar paths for the modules listed above.
218 if j, ok := module.(Dependency); ok {
219 name := ctx.ModuleName(module)
220 for moduleList, pathList := range moduleListToPathList {
221 if i := android.IndexList(name, *moduleList); i != -1 {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000222 pathList[i] = j.DexJarBuildPath()
Colin Crossf24a22a2019-01-31 14:12:44 -0800223 }
224 }
225 }
226
227 // Collect dex jar paths for modules that had hiddenapi encode called on them.
228 if h, ok := module.(hiddenAPIIntf); ok {
229 if jar := h.bootDexJar(); jar != nil {
Jiyong Park4ed468c2019-12-19 02:11:10 +0000230 // For a java lib included in an APEX, only take the one built for
231 // the platform variant, and skip the variants for APEXes.
232 // Otherwise, the hiddenapi tool will complain about duplicated classes
Colin Cross56a83212020-09-15 18:30:11 -0700233 apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
234 if !apexInfo.IsForPlatform() {
235 return
Jiyong Park7f7766d2019-07-25 22:02:35 +0900236 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700237
Colin Crossf24a22a2019-01-31 14:12:44 -0800238 bootDexJars = append(bootDexJars, jar)
239 }
240 }
241 })
242
243 var missingDeps []string
244 // Ensure all modules were converted to paths
245 for moduleList, pathList := range moduleListToPathList {
246 for i := range pathList {
247 if pathList[i] == nil {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000248 moduleName := (*moduleList)[i]
249 pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800250 if ctx.Config().AllowMissingDependencies() {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000251 missingDeps = append(missingDeps, moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800252 } else {
253 ctx.Errorf("failed to find dex jar path for module %q",
Paul Duffin7f48eef2020-12-03 11:15:58 +0000254 moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800255 }
256 }
257 }
258 }
259
260 // Singleton rule which applies hiddenapi on all boot class path dex files.
Colin Crossf1a035e2020-11-16 17:32:30 -0800261 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800262
263 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
264 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
265
266 rule.MissingDeps(missingDeps)
267
268 rule.Command().
Martin Stjernholm7260d062019-12-09 21:47:14 +0000269 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800270 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800271 FlagForEachInput("--boot-dex=", bootDexJars).
272 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000273 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
274 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800275 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
276 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800277
278 commitChangeForRestat(rule, tempPath, outputPath)
279
Colin Crossf1a035e2020-11-16 17:32:30 -0800280 rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags")
Colin Crossf24a22a2019-01-31 14:12:44 -0800281}
282
Bill Peckhambae47492021-01-08 09:34:44 -0800283func prebuiltFlagsRule(ctx android.SingletonContext) android.Path {
284 outputPath := hiddenAPISingletonPaths(ctx).flags
285 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv")
286
287 ctx.Build(pctx, android.BuildParams{
288 Rule: android.Cp,
289 Output: outputPath,
290 Input: inputPath,
291 })
292
293 return outputPath
294}
295
Colin Crossf24a22a2019-01-31 14:12:44 -0800296// 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 +0000297// the unsupported API.
Colin Crossed023ec2019-02-19 12:38:45 -0800298func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800299 var flagsCSV android.Paths
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100300 var combinedRemovedApis android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -0800301
302 ctx.VisitAllModules(func(module android.Module) {
303 if h, ok := module.(hiddenAPIIntf); ok {
304 if csv := h.flagsCSV(); csv != nil {
305 flagsCSV = append(flagsCSV, csv)
306 }
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100307 } else if g, ok := module.(*genrule.Module); ok {
308 if ctx.ModuleName(module) == "combined-removed-dex" {
309 if len(g.GeneratedSourceFiles()) != 1 || combinedRemovedApis != nil {
310 ctx.Errorf("Expected 1 combined-removed-dex module that generates 1 output file.")
311 }
312 combinedRemovedApis = g.GeneratedSourceFiles()[0]
Artur Satayevc7fb5c92020-03-25 16:48:49 +0000313 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800314 }
315 })
316
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100317 if combinedRemovedApis == nil {
318 ctx.Errorf("Failed to find combined-removed-dex.")
319 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800320
Colin Crossf1a035e2020-11-16 17:32:30 -0800321 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800322
323 outputPath := hiddenAPISingletonPaths(ctx).flags
324 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
325
326 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
327
328 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800329 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
330 FlagWithInput("--csv ", stubFlags).
331 Inputs(flagsCSV).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000332 FlagWithInput("--unsupported ",
Andrei Oneaca790812020-08-04 15:34:35 +0100333 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-unsupported.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100334 FlagWithInput("--unsupported ", combinedRemovedApis).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed").
Mathew Inwoodc1be2f82021-01-13 15:49:17 +0000335 FlagWithInput("--max-target-r ",
336 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-r-loprio.txt")).FlagWithArg("--tag ", "lo-prio").
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000337 FlagWithInput("--max-target-q ",
Andrei Oneaca790812020-08-04 15:34:35 +0100338 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-q.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000339 FlagWithInput("--max-target-p ",
Andrei Oneaca790812020-08-04 15:34:35 +0100340 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-p.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100341 FlagWithInput("--max-target-o ", android.PathForSource(
Mathew Inwood1ef4ba92020-11-10 14:49:43 +0000342 ctx, "frameworks/base/config/hiddenapi-max-target-o.txt")).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio").
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000343 FlagWithInput("--blocked ",
Andrei Oneaca790812020-08-04 15:34:35 +0100344 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blocked.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100345 FlagWithInput("--unsupported ", android.PathForSource(
346 ctx, "frameworks/base/config/hiddenapi-unsupported-packages.txt")).Flag("--packages ").
Colin Cross69f59a32019-02-15 10:39:37 -0800347 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800348
349 commitChangeForRestat(rule, tempPath, outputPath)
350
Colin Crossf1a035e2020-11-16 17:32:30 -0800351 rule.Build("hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800352
353 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800354}
355
356// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
357// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800358func emptyFlagsRule(ctx android.SingletonContext) android.Path {
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).flags
362
Colin Cross69f59a32019-02-15 10:39:37 -0800363 rule.Command().Text("rm").Flag("-f").Output(outputPath)
364 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800365
Colin Crossf1a035e2020-11-16 17:32:30 -0800366 rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800367
368 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800369}
370
Andrei Onea47841972020-08-10 17:23:52 +0100371// 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 -0800372// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800373func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800374 var metadataCSV android.Paths
375
376 ctx.VisitAllModules(func(module android.Module) {
377 if h, ok := module.(hiddenAPIIntf); ok {
378 if csv := h.metadataCSV(); csv != nil {
379 metadataCSV = append(metadataCSV, csv)
380 }
381 }
382 })
383
Colin Crossf1a035e2020-11-16 17:32:30 -0800384 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800385
386 outputPath := hiddenAPISingletonPaths(ctx).metadata
387
388 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800389 BuiltTool("merge_csv").
Artur Satayev79fac052020-01-20 19:11:33 +0000390 FlagWithOutput("--output=", outputPath).
391 Inputs(metadataCSV)
Colin Crossf24a22a2019-01-31 14:12:44 -0800392
Colin Crossf1a035e2020-11-16 17:32:30 -0800393 rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800394
395 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800396}
397
398// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
399// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
400// the rule.
401func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
402 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800403 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800404 rule.Command().
405 Text("(").
406 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800407 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800408 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800409 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800410 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800411 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800412 Text("fi").
413 Text(")")
414}
Paul Duffin1b033f52019-06-10 14:15:04 +0100415
416type hiddenAPIFlagsProperties struct {
417 // name of the file into which the flags will be copied.
418 Filename *string
419}
420
421type hiddenAPIFlags struct {
422 android.ModuleBase
423
424 properties hiddenAPIFlagsProperties
425
426 outputFilePath android.OutputPath
427}
428
429func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
430 filename := String(h.properties.Filename)
431
432 inputPath := hiddenAPISingletonPaths(ctx).flags
433 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
434
435 // This ensures that outputFilePath has the correct name for others to
436 // use, as the source file may have a different name.
437 ctx.Build(pctx, android.BuildParams{
438 Rule: android.Cp,
439 Output: h.outputFilePath,
440 Input: inputPath,
441 })
442}
443
444func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
445 switch tag {
446 case "":
447 return android.Paths{h.outputFilePath}, nil
448 default:
449 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
450 }
451}
452
453// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
454func hiddenAPIFlagsFactory() android.Module {
455 module := &hiddenAPIFlags{}
456 module.AddProperties(&module.properties)
457 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
458 return module
459}
Artur Satayevb5df8a02020-02-19 16:39:59 +0000460
461func hiddenAPIIndexSingletonFactory() android.Singleton {
462 return &hiddenAPIIndexSingleton{}
463}
464
465type hiddenAPIIndexSingleton struct {
466 index android.Path
467}
468
469func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) {
470 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
471 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
472 return
473 }
474
Bill Peckhambae47492021-01-08 09:34:44 -0800475 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
476 outputPath := hiddenAPISingletonPaths(ctx).index
477 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv")
478
479 ctx.Build(pctx, android.BuildParams{
480 Rule: android.Cp,
481 Output: outputPath,
482 Input: inputPath,
483 })
484
485 h.index = outputPath
486 return
487 }
488
Artur Satayevb5df8a02020-02-19 16:39:59 +0000489 indexes := android.Paths{}
490 ctx.VisitAllModules(func(module android.Module) {
491 if h, ok := module.(hiddenAPIIntf); ok {
492 if h.indexCSV() != nil {
493 indexes = append(indexes, h.indexCSV())
494 }
495 }
496 })
497
Colin Crossf1a035e2020-11-16 17:32:30 -0800498 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000499 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800500 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000501 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
502 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
503 Inputs(indexes)
Colin Crossf1a035e2020-11-16 17:32:30 -0800504 rule.Build("singleton-merged-hiddenapi-index", "Singleton merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000505
506 h.index = hiddenAPISingletonPaths(ctx).index
507}
508
509func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) {
510 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
511 return
512 }
513
514 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String())
515}