blob: 32d1e3faa72c82aef76af6e0eb9a0302c6c9eb1e [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
Paul Duffindd63d6d2021-02-03 18:34:00 +0000216 // Get the configured non-updatable and updatable boot jars.
217 nonUpdatableBootJars := ctx.Config().NonUpdatableBootJars()
218 updatableBootJars := ctx.Config().UpdatableBootJars()
219
Colin Crossf24a22a2019-01-31 14:12:44 -0800220 ctx.VisitAllModules(func(module android.Module) {
221 // Collect dex jar paths for the modules listed above.
222 if j, ok := module.(Dependency); ok {
223 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 {
Paul Duffindd63d6d2021-02-03 18:34:00 +0000234 if !isModuleInConfiguredList(ctx, module, nonUpdatableBootJars) &&
235 !isModuleInConfiguredList(ctx, module, updatableBootJars) {
Colin Cross56a83212020-09-15 18:30:11 -0700236 return
Jiyong Park7f7766d2019-07-25 22:02:35 +0900237 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700238
Colin Crossf24a22a2019-01-31 14:12:44 -0800239 bootDexJars = append(bootDexJars, jar)
240 }
241 }
242 })
243
244 var missingDeps []string
245 // Ensure all modules were converted to paths
246 for moduleList, pathList := range moduleListToPathList {
247 for i := range pathList {
248 if pathList[i] == nil {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000249 moduleName := (*moduleList)[i]
250 pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800251 if ctx.Config().AllowMissingDependencies() {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000252 missingDeps = append(missingDeps, moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800253 } else {
254 ctx.Errorf("failed to find dex jar path for module %q",
Paul Duffin7f48eef2020-12-03 11:15:58 +0000255 moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800256 }
257 }
258 }
259 }
260
261 // Singleton rule which applies hiddenapi on all boot class path dex files.
Colin Crossf1a035e2020-11-16 17:32:30 -0800262 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800263
264 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
265 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
266
267 rule.MissingDeps(missingDeps)
268
269 rule.Command().
Martin Stjernholm7260d062019-12-09 21:47:14 +0000270 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800271 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800272 FlagForEachInput("--boot-dex=", bootDexJars).
273 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000274 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
275 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800276 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
277 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800278
279 commitChangeForRestat(rule, tempPath, outputPath)
280
Colin Crossf1a035e2020-11-16 17:32:30 -0800281 rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags")
Colin Crossf24a22a2019-01-31 14:12:44 -0800282}
283
Paul Duffindd63d6d2021-02-03 18:34:00 +0000284// Checks to see whether the supplied module variant is in the list of boot jars.
285//
286// This is similar to logic in getBootImageJar() so any changes needed here are likely to be needed
287// there too.
288//
289// TODO(b/179354495): Avoid having to perform this type of check or if necessary dedup it.
290func isModuleInConfiguredList(ctx android.SingletonContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool {
291 name := ctx.ModuleName(module)
292
293 // Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed.
294 name = android.RemoveOptionalPrebuiltPrefix(name)
295
296 // Ignore any module that is not listed in the boot image configuration.
297 index := configuredBootJars.IndexOfJar(name)
298 if index == -1 {
299 return false
300 }
301
302 // It is an error if the module is not an ApexModule.
303 if _, ok := module.(android.ApexModule); !ok {
304 ctx.Errorf("module %q configured in boot jars does not support being added to an apex", module)
305 return false
306 }
307
308 apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
309
310 // Now match the apex part of the boot image configuration.
311 requiredApex := configuredBootJars.Apex(index)
312 if requiredApex == "platform" {
313 if len(apexInfo.InApexes) != 0 {
314 // A platform variant is required but this is for an apex so ignore it.
315 return false
316 }
317 } else if !apexInfo.InApexByBaseName(requiredApex) {
318 // An apex variant for a specific apex is required but this is the wrong apex.
319 return false
320 }
321
322 return true
323}
324
Bill Peckhambae47492021-01-08 09:34:44 -0800325func prebuiltFlagsRule(ctx android.SingletonContext) android.Path {
326 outputPath := hiddenAPISingletonPaths(ctx).flags
327 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv")
328
329 ctx.Build(pctx, android.BuildParams{
330 Rule: android.Cp,
331 Output: outputPath,
332 Input: inputPath,
333 })
334
335 return outputPath
336}
337
Colin Crossf24a22a2019-01-31 14:12:44 -0800338// 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 +0000339// the unsupported API.
Colin Crossed023ec2019-02-19 12:38:45 -0800340func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800341 var flagsCSV android.Paths
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100342 var combinedRemovedApis android.Path
Colin Crossf24a22a2019-01-31 14:12:44 -0800343
344 ctx.VisitAllModules(func(module android.Module) {
345 if h, ok := module.(hiddenAPIIntf); ok {
346 if csv := h.flagsCSV(); csv != nil {
347 flagsCSV = append(flagsCSV, csv)
348 }
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100349 } else if g, ok := module.(*genrule.Module); ok {
350 if ctx.ModuleName(module) == "combined-removed-dex" {
351 if len(g.GeneratedSourceFiles()) != 1 || combinedRemovedApis != nil {
352 ctx.Errorf("Expected 1 combined-removed-dex module that generates 1 output file.")
353 }
354 combinedRemovedApis = g.GeneratedSourceFiles()[0]
Artur Satayevc7fb5c92020-03-25 16:48:49 +0000355 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800356 }
357 })
358
Anton Hanssonb3cbd612020-10-06 12:04:34 +0100359 if combinedRemovedApis == nil {
360 ctx.Errorf("Failed to find combined-removed-dex.")
361 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800362
Colin Crossf1a035e2020-11-16 17:32:30 -0800363 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800364
365 outputPath := hiddenAPISingletonPaths(ctx).flags
366 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
367
368 stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
369
370 rule.Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800371 Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
372 FlagWithInput("--csv ", stubFlags).
373 Inputs(flagsCSV).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000374 FlagWithInput("--unsupported ",
Andrei Oneaca790812020-08-04 15:34:35 +0100375 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-unsupported.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100376 FlagWithInput("--unsupported ", combinedRemovedApis).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed").
Mathew Inwoodc1be2f82021-01-13 15:49:17 +0000377 FlagWithInput("--max-target-r ",
378 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-r-loprio.txt")).FlagWithArg("--tag ", "lo-prio").
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000379 FlagWithInput("--max-target-q ",
Andrei Oneaca790812020-08-04 15:34:35 +0100380 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-q.txt")).
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000381 FlagWithInput("--max-target-p ",
Andrei Oneaca790812020-08-04 15:34:35 +0100382 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-p.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100383 FlagWithInput("--max-target-o ", android.PathForSource(
Mathew Inwood1ef4ba92020-11-10 14:49:43 +0000384 ctx, "frameworks/base/config/hiddenapi-max-target-o.txt")).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio").
Aleksei Kalinovf0f5cdc2020-07-28 13:44:24 +0000385 FlagWithInput("--blocked ",
Andrei Oneaca790812020-08-04 15:34:35 +0100386 android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blocked.txt")).
Mathew Inwooda44e8c52020-10-20 15:23:04 +0100387 FlagWithInput("--unsupported ", android.PathForSource(
388 ctx, "frameworks/base/config/hiddenapi-unsupported-packages.txt")).Flag("--packages ").
Colin Cross69f59a32019-02-15 10:39:37 -0800389 FlagWithOutput("--output ", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800390
391 commitChangeForRestat(rule, tempPath, outputPath)
392
Colin Crossf1a035e2020-11-16 17:32:30 -0800393 rule.Build("hiddenAPIFlagsFile", "hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800394
395 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800396}
397
398// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
399// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800400func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf1a035e2020-11-16 17:32:30 -0800401 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800402
403 outputPath := hiddenAPISingletonPaths(ctx).flags
404
Colin Cross69f59a32019-02-15 10:39:37 -0800405 rule.Command().Text("rm").Flag("-f").Output(outputPath)
406 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800407
Colin Crossf1a035e2020-11-16 17:32:30 -0800408 rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800409
410 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800411}
412
Andrei Onea47841972020-08-10 17:23:52 +0100413// 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 -0800414// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800415func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800416 var metadataCSV android.Paths
417
418 ctx.VisitAllModules(func(module android.Module) {
419 if h, ok := module.(hiddenAPIIntf); ok {
420 if csv := h.metadataCSV(); csv != nil {
421 metadataCSV = append(metadataCSV, csv)
422 }
423 }
424 })
425
Colin Crossf1a035e2020-11-16 17:32:30 -0800426 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800427
428 outputPath := hiddenAPISingletonPaths(ctx).metadata
429
430 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800431 BuiltTool("merge_csv").
Artur Satayev79fac052020-01-20 19:11:33 +0000432 FlagWithOutput("--output=", outputPath).
433 Inputs(metadataCSV)
Colin Crossf24a22a2019-01-31 14:12:44 -0800434
Colin Crossf1a035e2020-11-16 17:32:30 -0800435 rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800436
437 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800438}
439
440// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
441// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
442// the rule.
443func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
444 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800445 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800446 rule.Command().
447 Text("(").
448 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800449 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800450 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800451 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800452 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800453 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800454 Text("fi").
455 Text(")")
456}
Paul Duffin1b033f52019-06-10 14:15:04 +0100457
458type hiddenAPIFlagsProperties struct {
459 // name of the file into which the flags will be copied.
460 Filename *string
461}
462
463type hiddenAPIFlags struct {
464 android.ModuleBase
465
466 properties hiddenAPIFlagsProperties
467
468 outputFilePath android.OutputPath
469}
470
471func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
472 filename := String(h.properties.Filename)
473
474 inputPath := hiddenAPISingletonPaths(ctx).flags
475 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
476
477 // This ensures that outputFilePath has the correct name for others to
478 // use, as the source file may have a different name.
479 ctx.Build(pctx, android.BuildParams{
480 Rule: android.Cp,
481 Output: h.outputFilePath,
482 Input: inputPath,
483 })
484}
485
486func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
487 switch tag {
488 case "":
489 return android.Paths{h.outputFilePath}, nil
490 default:
491 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
492 }
493}
494
495// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
496func hiddenAPIFlagsFactory() android.Module {
497 module := &hiddenAPIFlags{}
498 module.AddProperties(&module.properties)
499 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
500 return module
501}
Artur Satayevb5df8a02020-02-19 16:39:59 +0000502
503func hiddenAPIIndexSingletonFactory() android.Singleton {
504 return &hiddenAPIIndexSingleton{}
505}
506
507type hiddenAPIIndexSingleton struct {
508 index android.Path
509}
510
511func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) {
512 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
513 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
514 return
515 }
516
Bill Peckhambae47492021-01-08 09:34:44 -0800517 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
518 outputPath := hiddenAPISingletonPaths(ctx).index
519 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv")
520
521 ctx.Build(pctx, android.BuildParams{
522 Rule: android.Cp,
523 Output: outputPath,
524 Input: inputPath,
525 })
526
527 h.index = outputPath
528 return
529 }
530
Artur Satayevb5df8a02020-02-19 16:39:59 +0000531 indexes := android.Paths{}
532 ctx.VisitAllModules(func(module android.Module) {
533 if h, ok := module.(hiddenAPIIntf); ok {
534 if h.indexCSV() != nil {
535 indexes = append(indexes, h.indexCSV())
536 }
537 }
538 })
539
Colin Crossf1a035e2020-11-16 17:32:30 -0800540 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000541 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800542 BuiltTool("merge_csv").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000543 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
544 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
545 Inputs(indexes)
Colin Crossf1a035e2020-11-16 17:32:30 -0800546 rule.Build("singleton-merged-hiddenapi-index", "Singleton merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000547
548 h.index = hiddenAPISingletonPaths(ctx).index
549}
550
551func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) {
552 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
553 return
554 }
555
556 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String())
557}