blob: 641e19f601b214c042fd26e60afc1bf480e14050 [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"
21)
22
23func init() {
Paul Duffin01289a22021-02-04 17:49:33 +000024 RegisterHiddenApiSingletonComponents(android.InitRegistrationContext)
25}
26
27func RegisterHiddenApiSingletonComponents(ctx android.RegistrationContext) {
28 ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
29 ctx.RegisterSingletonType("hiddenapi_index", hiddenAPIIndexSingletonFactory)
30 ctx.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory)
Colin Crossf24a22a2019-01-31 14:12:44 -080031}
32
Paul Duffin175947f2021-03-12 21:44:02 +000033var PrepareForTestWithHiddenApiBuildComponents = android.FixtureRegisterWithContext(RegisterHiddenApiSingletonComponents)
34
Colin Crossf24a22a2019-01-31 14:12:44 -080035type hiddenAPISingletonPathsStruct struct {
Paul Duffinff774a02021-01-29 12:53:15 +000036 // The path to the CSV file that contains the flags that will be encoded into the dex boot jars.
37 //
38 // It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with
39 // a number of additional files that are used to augment the information in the stubFlags with
40 // manually curated data.
41 flags android.OutputPath
42
43 // The path to the CSV index file that contains mappings from Java signature to source location
44 // information for all Java elements annotated with the UnsupportedAppUsage annotation in the
45 // source of all the boot jars.
46 //
47 // It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have
48 // been created by the rest of the build. That includes the index files generated for
49 // <x>-hiddenapi modules.
50 index android.OutputPath
51
52 // The path to the CSV metadata file that contains mappings from Java signature to the value of
53 // properties specified on UnsupportedAppUsage annotations in the source of all the boot jars.
54 //
55 // It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that
56 // have been created by the rest of the build. That includes the metadata files generated for
57 // <x>-hiddenapi modules.
58 metadata android.OutputPath
59
60 // The path to the CSV metadata file that contains mappings from Java signature to flags obtained
61 // from the public, system and test API stubs.
62 //
63 // This is created by the hiddenapi tool which is given dex files for the public, system and test
64 // API stubs (including product specific stubs) along with dex boot jars, so does not include
65 // <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which
66 // members in the dex boot jars match a member in the dex stub jars for that API surface and then
67 // outputs a file containing the signatures of all members in the dex boot jars along with the
68 // flags that indicate which API surface it belongs, if any.
69 //
70 // e.g. a dex member that matches a member in the public dex stubs would have flags
71 // "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex
72 // member that didn't match a member in any of the dex stubs is still output it just has an empty
73 // set of flags.
74 //
75 // The notion of matching is quite complex, it is not restricted to just exact matching but also
76 // follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing
77 // methods are also public. If an interface method is public and a class inherits an
78 // implementation of that method from a super class then that super class method is also public.
79 // That ensures that any method that can be called directly by an App through a public method is
80 // visible to that App.
81 //
82 // Propagating the visibility of members across the inheritance hierarchy at build time will cause
83 // problems when modularizing and unbundling as it that propagation can cross module boundaries.
84 // e.g. Say that a private framework class implements a public interface and inherits an
85 // implementation of one of its methods from a core platform ART class. In that case the ART
86 // implementation method needs to be marked as public which requires the build to have access to
87 // the framework implementation classes at build time. The work to rectify this is being tracked
88 // at http://b/178693149.
89 //
90 // This file (or at least those items marked as being in the public-api) is used by hiddenapi when
91 // creating the metadata and flags for the individual modules in order to perform consistency
92 // checks and filter out bridge methods that are part of the public API. The latter relies on the
93 // propagation of visibility across the inheritance hierarchy.
Artur Satayevb5df8a02020-02-19 16:39:59 +000094 stubFlags android.OutputPath
Colin Crossf24a22a2019-01-31 14:12:44 -080095}
96
97var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey")
98
99// hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be
100// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
101// yet been created.
102func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct {
103 return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} {
104 return hiddenAPISingletonPathsStruct{
Colin Crossf24a22a2019-01-31 14:12:44 -0800105 flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +0000106 index: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-index.csv"),
Andrei Onea47841972020-08-10 17:23:52 +0100107 metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-unsupported.csv"),
Artur Satayevb5df8a02020-02-19 16:39:59 +0000108 stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"),
Colin Crossf24a22a2019-01-31 14:12:44 -0800109 }
110 }).(hiddenAPISingletonPathsStruct)
111}
112
Colin Crossf24a22a2019-01-31 14:12:44 -0800113func hiddenAPISingletonFactory() android.Singleton {
Colin Crossed023ec2019-02-19 12:38:45 -0800114 return &hiddenAPISingleton{}
Colin Crossf24a22a2019-01-31 14:12:44 -0800115}
116
Colin Crossed023ec2019-02-19 12:38:45 -0800117type hiddenAPISingleton struct {
118 flags, metadata android.Path
119}
Colin Crossf24a22a2019-01-31 14:12:44 -0800120
121// hiddenAPI singleton rules
Colin Crossed023ec2019-02-19 12:38:45 -0800122func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Crossf24a22a2019-01-31 14:12:44 -0800123 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
124 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
125 return
126 }
127
128 stubFlagsRule(ctx)
129
Bill Peckhambae47492021-01-08 09:34:44 -0800130 // If there is a prebuilt hiddenapi dir, generate rules to use the
131 // files within. Generally, we build the hiddenapi files from source
132 // during the build, ensuring consistency. It's possible, in a split
133 // build (framework and vendor) scenario, for the vendor build to use
134 // prebuilt hiddenapi files from the framework build. In this scenario,
135 // the framework and vendor builds must use the same source to ensure
136 // consistency.
137
138 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
139 h.flags = prebuiltFlagsRule(ctx)
140 return
141 }
142
Colin Crossf24a22a2019-01-31 14:12:44 -0800143 // 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 +0900144 if ctx.Config().FrameworksBaseDirExists(ctx) {
Colin Crossed023ec2019-02-19 12:38:45 -0800145 h.flags = flagsRule(ctx)
146 h.metadata = metadataRule(ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800147 } else {
Colin Crossed023ec2019-02-19 12:38:45 -0800148 h.flags = emptyFlagsRule(ctx)
149 }
150}
151
152// Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
153// Both paths are used to call dist-for-goals.
154func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) {
155 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
156 return
157 }
158
159 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String())
160
161 if h.metadata != nil {
162 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String())
Colin Crossf24a22a2019-01-31 14:12:44 -0800163 }
164}
165
166// stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image
167// modules.
168func stubFlagsRule(ctx android.SingletonContext) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100169 var publicStubModules []string
170 var systemStubModules []string
171 var testStubModules []string
172 var corePlatformStubModules []string
173
174 if ctx.Config().AlwaysUsePrebuiltSdks() {
175 // Build configuration mandates using prebuilt stub modules
176 publicStubModules = append(publicStubModules, "sdk_public_current_android")
177 systemStubModules = append(systemStubModules, "sdk_system_current_android")
178 testStubModules = append(testStubModules, "sdk_test_current_android")
179 } else {
180 // Use stub modules built from source
181 publicStubModules = append(publicStubModules, "android_stubs_current")
182 systemStubModules = append(systemStubModules, "android_system_stubs_current")
183 testStubModules = append(testStubModules, "android_test_stubs_current")
Paul Duffin719fed42019-02-28 16:15:44 +0000184 }
Anton Hanssona2adc372020-07-03 15:31:32 +0100185 // We do not have prebuilts of the core platform api yet
186 corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs")
Paul Duffin719fed42019-02-28 16:15:44 +0000187
Colin Crossf24a22a2019-01-31 14:12:44 -0800188 // Allow products to define their own stubs for custom product jars that apps can use.
189 publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
190 systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
191 testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
Allen Hairde816cf2019-02-25 16:37:42 -0800192 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
193 publicStubModules = append(publicStubModules, "jacoco-stubs")
194 }
Colin Crossf24a22a2019-01-31 14:12:44 -0800195
196 publicStubPaths := make(android.Paths, len(publicStubModules))
197 systemStubPaths := make(android.Paths, len(systemStubModules))
198 testStubPaths := make(android.Paths, len(testStubModules))
199 corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules))
200
201 moduleListToPathList := map[*[]string]android.Paths{
202 &publicStubModules: publicStubPaths,
203 &systemStubModules: systemStubPaths,
204 &testStubModules: testStubPaths,
205 &corePlatformStubModules: corePlatformStubPaths,
206 }
207
208 var bootDexJars android.Paths
209
210 ctx.VisitAllModules(func(module android.Module) {
211 // Collect dex jar paths for the modules listed above.
Colin Crossdcf71b22021-02-01 13:59:03 -0800212 if j, ok := module.(UsesLibraryDependency); ok {
Colin Crossf24a22a2019-01-31 14:12:44 -0800213 name := ctx.ModuleName(module)
214 for moduleList, pathList := range moduleListToPathList {
215 if i := android.IndexList(name, *moduleList); i != -1 {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000216 pathList[i] = j.DexJarBuildPath()
Colin Crossf24a22a2019-01-31 14:12:44 -0800217 }
218 }
219 }
220
221 // Collect dex jar paths for modules that had hiddenapi encode called on them.
222 if h, ok := module.(hiddenAPIIntf); ok {
223 if jar := h.bootDexJar(); jar != nil {
224 bootDexJars = append(bootDexJars, jar)
225 }
226 }
227 })
228
229 var missingDeps []string
230 // Ensure all modules were converted to paths
231 for moduleList, pathList := range moduleListToPathList {
232 for i := range pathList {
233 if pathList[i] == nil {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000234 moduleName := (*moduleList)[i]
235 pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800236 if ctx.Config().AllowMissingDependencies() {
Paul Duffin7f48eef2020-12-03 11:15:58 +0000237 missingDeps = append(missingDeps, moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800238 } else {
239 ctx.Errorf("failed to find dex jar path for module %q",
Paul Duffin7f48eef2020-12-03 11:15:58 +0000240 moduleName)
Colin Crossf24a22a2019-01-31 14:12:44 -0800241 }
242 }
243 }
244 }
245
246 // Singleton rule which applies hiddenapi on all boot class path dex files.
Colin Crossf1a035e2020-11-16 17:32:30 -0800247 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800248
249 outputPath := hiddenAPISingletonPaths(ctx).stubFlags
250 tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp")
251
252 rule.MissingDeps(missingDeps)
253
254 rule.Command().
Martin Stjernholm7260d062019-12-09 21:47:14 +0000255 Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")).
Colin Crossf24a22a2019-01-31 14:12:44 -0800256 Text("list").
Colin Cross69f59a32019-02-15 10:39:37 -0800257 FlagForEachInput("--boot-dex=", bootDexJars).
258 FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
Andrei Oneae04da072019-03-01 17:44:13 +0000259 FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":").
260 FlagWithInputList("--test-stub-classpath=", testStubPaths, ":").
Colin Cross69f59a32019-02-15 10:39:37 -0800261 FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
262 FlagWithOutput("--out-api-flags=", tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800263
264 commitChangeForRestat(rule, tempPath, outputPath)
265
Colin Crossf1a035e2020-11-16 17:32:30 -0800266 rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags")
Colin Crossf24a22a2019-01-31 14:12:44 -0800267}
268
Paul Duffindd63d6d2021-02-03 18:34:00 +0000269// Checks to see whether the supplied module variant is in the list of boot jars.
270//
271// This is similar to logic in getBootImageJar() so any changes needed here are likely to be needed
272// there too.
273//
274// TODO(b/179354495): Avoid having to perform this type of check or if necessary dedup it.
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000275func isModuleInConfiguredList(ctx android.BaseModuleContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool {
276 name := ctx.OtherModuleName(module)
Paul Duffindd63d6d2021-02-03 18:34:00 +0000277
278 // Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed.
279 name = android.RemoveOptionalPrebuiltPrefix(name)
280
281 // Ignore any module that is not listed in the boot image configuration.
282 index := configuredBootJars.IndexOfJar(name)
283 if index == -1 {
284 return false
285 }
286
287 // It is an error if the module is not an ApexModule.
288 if _, ok := module.(android.ApexModule); !ok {
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000289 ctx.ModuleErrorf("is configured in boot jars but does not support being added to an apex")
Paul Duffindd63d6d2021-02-03 18:34:00 +0000290 return false
291 }
292
Paul Duffin82b3fcf2021-02-12 15:42:46 +0000293 apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
Paul Duffindd63d6d2021-02-03 18:34:00 +0000294
295 // Now match the apex part of the boot image configuration.
296 requiredApex := configuredBootJars.Apex(index)
297 if requiredApex == "platform" {
298 if len(apexInfo.InApexes) != 0 {
299 // A platform variant is required but this is for an apex so ignore it.
300 return false
301 }
302 } else if !apexInfo.InApexByBaseName(requiredApex) {
303 // An apex variant for a specific apex is required but this is the wrong apex.
304 return false
305 }
306
307 return true
308}
309
Bill Peckhambae47492021-01-08 09:34:44 -0800310func prebuiltFlagsRule(ctx android.SingletonContext) android.Path {
311 outputPath := hiddenAPISingletonPaths(ctx).flags
312 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv")
313
314 ctx.Build(pctx, android.BuildParams{
315 Rule: android.Cp,
316 Output: outputPath,
317 Input: inputPath,
318 })
319
320 return outputPath
321}
322
Paul Duffin702210b2021-04-08 20:12:41 +0100323// flagsRule is a placeholder that simply returns the location of the file, the generation of the
324// ninja rules is done in generateHiddenAPIBuildActions.
Colin Crossed023ec2019-02-19 12:38:45 -0800325func flagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800326 outputPath := hiddenAPISingletonPaths(ctx).flags
Paul Duffinc6bb7cf2021-04-08 17:49:27 +0100327 return outputPath
328}
329
Colin Crossf24a22a2019-01-31 14:12:44 -0800330// emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that
331// have a partial manifest without frameworks/base but still need to build a boot image.
Colin Crossed023ec2019-02-19 12:38:45 -0800332func emptyFlagsRule(ctx android.SingletonContext) android.Path {
Colin Crossf1a035e2020-11-16 17:32:30 -0800333 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossf24a22a2019-01-31 14:12:44 -0800334
335 outputPath := hiddenAPISingletonPaths(ctx).flags
336
Colin Cross69f59a32019-02-15 10:39:37 -0800337 rule.Command().Text("rm").Flag("-f").Output(outputPath)
338 rule.Command().Text("touch").Output(outputPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800339
Colin Crossf1a035e2020-11-16 17:32:30 -0800340 rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
Colin Crossed023ec2019-02-19 12:38:45 -0800341
342 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800343}
344
Andrei Onea47841972020-08-10 17:23:52 +0100345// 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 -0800346// modules.
Colin Crossed023ec2019-02-19 12:38:45 -0800347func metadataRule(ctx android.SingletonContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800348 var metadataCSV android.Paths
349
350 ctx.VisitAllModules(func(module android.Module) {
351 if h, ok := module.(hiddenAPIIntf); ok {
352 if csv := h.metadataCSV(); csv != nil {
353 metadataCSV = append(metadataCSV, csv)
354 }
355 }
356 })
357
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).metadata
361
362 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800363 BuiltTool("merge_csv").
Paul Duffin2c36f242021-02-16 16:57:06 +0000364 Flag("--key_field signature").
Artur Satayev79fac052020-01-20 19:11:33 +0000365 FlagWithOutput("--output=", outputPath).
366 Inputs(metadataCSV)
Colin Crossf24a22a2019-01-31 14:12:44 -0800367
Colin Crossf1a035e2020-11-16 17:32:30 -0800368 rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
Colin Crossed023ec2019-02-19 12:38:45 -0800369
370 return outputPath
Colin Crossf24a22a2019-01-31 14:12:44 -0800371}
372
373// commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It
374// also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of
375// the rule.
376func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
377 rule.Restat()
Colin Cross69f59a32019-02-15 10:39:37 -0800378 rule.Temporary(tempPath)
Colin Crossf24a22a2019-01-31 14:12:44 -0800379 rule.Command().
380 Text("(").
381 Text("if").
Colin Cross69f59a32019-02-15 10:39:37 -0800382 Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800383 Text("then").
Colin Cross69f59a32019-02-15 10:39:37 -0800384 Text("rm").Input(tempPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800385 Text("else").
Colin Cross69f59a32019-02-15 10:39:37 -0800386 Text("mv").Input(tempPath).Output(outputPath).Text(";").
Colin Crossf24a22a2019-01-31 14:12:44 -0800387 Text("fi").
388 Text(")")
389}
Paul Duffin1b033f52019-06-10 14:15:04 +0100390
391type hiddenAPIFlagsProperties struct {
392 // name of the file into which the flags will be copied.
393 Filename *string
394}
395
396type hiddenAPIFlags struct {
397 android.ModuleBase
398
399 properties hiddenAPIFlagsProperties
400
401 outputFilePath android.OutputPath
402}
403
404func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) {
405 filename := String(h.properties.Filename)
406
407 inputPath := hiddenAPISingletonPaths(ctx).flags
408 h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
409
410 // This ensures that outputFilePath has the correct name for others to
411 // use, as the source file may have a different name.
412 ctx.Build(pctx, android.BuildParams{
413 Rule: android.Cp,
414 Output: h.outputFilePath,
415 Input: inputPath,
416 })
417}
418
419func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) {
420 switch tag {
421 case "":
422 return android.Paths{h.outputFilePath}, nil
423 default:
424 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
425 }
426}
427
428// hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build.
429func hiddenAPIFlagsFactory() android.Module {
430 module := &hiddenAPIFlags{}
431 module.AddProperties(&module.properties)
432 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
433 return module
434}
Artur Satayevb5df8a02020-02-19 16:39:59 +0000435
436func hiddenAPIIndexSingletonFactory() android.Singleton {
437 return &hiddenAPIIndexSingleton{}
438}
439
440type hiddenAPIIndexSingleton struct {
441 index android.Path
442}
443
444func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) {
445 // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true
446 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
447 return
448 }
449
Bill Peckhambae47492021-01-08 09:34:44 -0800450 if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" {
451 outputPath := hiddenAPISingletonPaths(ctx).index
452 inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv")
453
454 ctx.Build(pctx, android.BuildParams{
455 Rule: android.Cp,
456 Output: outputPath,
457 Input: inputPath,
458 })
459
460 h.index = outputPath
461 return
462 }
463
Artur Satayevb5df8a02020-02-19 16:39:59 +0000464 indexes := android.Paths{}
465 ctx.VisitAllModules(func(module android.Module) {
466 if h, ok := module.(hiddenAPIIntf); ok {
467 if h.indexCSV() != nil {
468 indexes = append(indexes, h.indexCSV())
469 }
470 }
471 })
472
Colin Crossf1a035e2020-11-16 17:32:30 -0800473 rule := android.NewRuleBuilder(pctx, ctx)
Artur Satayevb5df8a02020-02-19 16:39:59 +0000474 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800475 BuiltTool("merge_csv").
Paul Duffin2c36f242021-02-16 16:57:06 +0000476 Flag("--key_field signature").
Artur Satayevb5df8a02020-02-19 16:39:59 +0000477 FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
478 FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
479 Inputs(indexes)
Colin Crossf1a035e2020-11-16 17:32:30 -0800480 rule.Build("singleton-merged-hiddenapi-index", "Singleton merged Hidden API index")
Artur Satayevb5df8a02020-02-19 16:39:59 +0000481
482 h.index = hiddenAPISingletonPaths(ctx).index
483}
484
485func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) {
486 if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
487 return
488 }
489
490 ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String())
491}