• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
18	"github.com/google/blueprint"
19
20	"android/soong/android"
21)
22
23var (
24	hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
25		Command:     "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
26		CommandDeps: []string{"${config.Class2NonSdkList}"},
27	}, "outFlag", "stubAPIFlags")
28
29	hiddenAPIGenerateIndexRule = pctx.AndroidStaticRule("hiddenAPIGenerateIndex", blueprint.RuleParams{
30		Command:     "${config.MergeCsvCommand} --zip_input --key_field signature --output=$out $in",
31		CommandDeps: []string{"${config.MergeCsvCommand}"},
32	})
33)
34
35type hiddenAPI struct {
36	// True if the module containing this structure contributes to the hiddenapi information or has
37	// that information encoded within it.
38	active bool
39
40	// The path to the dex jar that is in the boot class path. If this is unset then the associated
41	// module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
42	// annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
43	// themselves.
44	//
45	// This must be the path to the unencoded dex jar as the encoded dex jar indirectly depends on
46	// this file so using the encoded dex jar here would result in a cycle in the ninja rules.
47	bootDexJarPath OptionalDexJarPath
48
49	// The paths to the classes jars that contain classes and class members annotated with
50	// the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API
51	// processing.
52	classesJarPaths android.Paths
53
54	// The compressed state of the dex file being encoded. This is used to ensure that the encoded
55	// dex file has the same state.
56	uncompressDexState *bool
57}
58
59func (h *hiddenAPI) bootDexJar() OptionalDexJarPath {
60	return h.bootDexJarPath
61}
62
63func (h *hiddenAPI) classesJars() android.Paths {
64	return h.classesJarPaths
65}
66
67func (h *hiddenAPI) uncompressDex() *bool {
68	return h.uncompressDexState
69}
70
71// hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement.
72type hiddenAPIModule interface {
73	android.Module
74	hiddenAPIIntf
75
76	MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel
77}
78
79type hiddenAPIIntf interface {
80	bootDexJar() OptionalDexJarPath
81	classesJars() android.Paths
82	uncompressDex() *bool
83}
84
85var _ hiddenAPIIntf = (*hiddenAPI)(nil)
86
87// Initialize the hiddenapi structure
88//
89// uncompressedDexState should be nil when the module is a prebuilt and so does not require hidden
90// API encoding.
91func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar OptionalDexJarPath, classesJar android.Path, uncompressedDexState *bool) {
92
93	// Save the classes jars even if this is not active as they may be used by modular hidden API
94	// processing.
95	classesJars := android.Paths{classesJar}
96	ctx.VisitDirectDepsWithTag(hiddenApiAnnotationsTag, func(dep android.Module) {
97		javaInfo := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
98		classesJars = append(classesJars, javaInfo.ImplementationJars...)
99	})
100	h.classesJarPaths = classesJars
101
102	// Save the unencoded dex jar so it can be used when generating the
103	// hiddenAPISingletonPathsStruct.stubFlags file.
104	h.bootDexJarPath = dexJar
105
106	h.uncompressDexState = uncompressedDexState
107
108	// If hiddenapi processing is disabled treat this as inactive.
109	if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
110		return
111	}
112
113	// The context module must implement hiddenAPIModule.
114	module := ctx.Module().(hiddenAPIModule)
115
116	// If the frameworks/base directories does not exist and no prebuilt hidden API flag files have
117	// been configured then it is not possible to do hidden API encoding.
118	if !ctx.Config().FrameworksBaseDirExists(ctx) && ctx.Config().PrebuiltHiddenApiDir(ctx) == "" {
119		return
120	}
121
122	// It is important that hiddenapi information is only gathered for/from modules that are actually
123	// on the boot jars list because the runtime only enforces access to the hidden API for the
124	// bootclassloader. If information is gathered for modules not on the list then that will cause
125	// failures in the CtsHiddenApiBlocklist... tests.
126	h.active = isModuleInBootClassPath(ctx, module)
127}
128
129func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool {
130	// Get the configured platform and apex boot jars.
131	nonApexBootJars := ctx.Config().NonApexBootJars()
132	apexBootJars := ctx.Config().ApexBootJars()
133	active := isModuleInConfiguredList(ctx, module, nonApexBootJars) ||
134		isModuleInConfiguredList(ctx, module, apexBootJars)
135	return active
136}
137
138// hiddenAPIEncodeDex is called by any module that needs to encode dex files.
139//
140// It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
141// jar list. In that case it simply returns the supplied dex jar path.
142//
143// Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi
144// flags and returns this instead of the supplied dex jar.
145func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath) android.OutputPath {
146
147	if !h.active {
148		return dexJar
149	}
150
151	// A nil uncompressDexState prevents the dex file from being encoded.
152	if h.uncompressDexState == nil {
153		ctx.ModuleErrorf("cannot encode dex file %s when uncompressDexState is nil", dexJar)
154	}
155	uncompressDex := *h.uncompressDexState
156
157	// Create a copy of the dex jar which has been encoded with hiddenapi flags.
158	flagsCSV := hiddenAPISingletonPaths(ctx).flags
159	outputDir := android.PathForModuleOut(ctx, "hiddenapi").OutputPath
160	encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, android.NoneApiLevel, outputDir)
161
162	// Use the encoded dex jar from here onwards.
163	return encodedDex
164}
165
166// buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file
167// from the classes jars and stub-flags.csv files.
168//
169// The annotation-flags.csv file contains mappings from Java signature to various flags derived from
170// annotations in the source, e.g. whether it is public or the sdk version above which it can no
171// longer be used.
172//
173// It is created by the Class2NonSdkList tool which processes the .class files in the class
174// implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
175// tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
176// consistency checks on the information in the annotations and to filter out bridge methods
177// that are already part of the public API.
178func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) {
179	ctx.Build(pctx, android.BuildParams{
180		Rule:        hiddenAPIGenerateCSVRule,
181		Description: desc,
182		Inputs:      classesJars,
183		Output:      outputPath,
184		Implicit:    stubFlagsCSV,
185		Args: map[string]string{
186			"outFlag":      "--write-flags-csv",
187			"stubAPIFlags": stubFlagsCSV.String(),
188		},
189	})
190}
191
192// buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from
193// the classes jars and stub-flags.csv files.
194//
195// The metadata.csv file contains mappings from Java signature to the value of properties specified
196// on UnsupportedAppUsage annotations in the source.
197//
198// Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way.
199// Although the two files could potentially be created in a single invocation of the
200// Class2NonSdkList at the moment they are created using their own invocation, with the behavior
201// being determined by the property that is used.
202func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) {
203	ctx.Build(pctx, android.BuildParams{
204		Rule:        hiddenAPIGenerateCSVRule,
205		Description: desc,
206		Inputs:      classesJars,
207		Output:      metadataCSV,
208		Implicit:    stubFlagsCSV,
209		Args: map[string]string{
210			"outFlag":      "--write-metadata-csv",
211			"stubAPIFlags": stubFlagsCSV.String(),
212		},
213	})
214}
215
216// buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes
217// jars.
218//
219// The index.csv file contains mappings from Java signature to source location information.
220//
221// It is created by the merge_csv tool which processes the class implementation jar, extracting
222// all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
223// created by the unsupported app usage annotation processor during compilation of the class
224// implementation jar.
225func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) {
226	ctx.Build(pctx, android.BuildParams{
227		Rule:        hiddenAPIGenerateIndexRule,
228		Description: desc,
229		Inputs:      classesJars,
230		Output:      indexCSV,
231	})
232}
233
234var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
235	Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
236		unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
237		for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
238		  echo "--input-dex=$${INPUT_DEX}";
239		  echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
240		done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
241		${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
242		${config.MergeZipsCmd} -j -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
243	CommandDeps: []string{
244		"${config.HiddenAPI}",
245		"${config.SoongZipCmd}",
246		"${config.MergeZipsCmd}",
247	},
248}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
249
250// hiddenAPIEncodeDex generates the build rule that will encode the supplied dex jar and place the
251// encoded dex jar in a file of the same name in the output directory.
252//
253// The encode dex rule requires unzipping, encoding and rezipping the classes.dex files along with
254// all the resources from the input jar. It also ensures that if it was uncompressed in the input
255// it stays uncompressed in the output.
256func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, minSdkVersion android.ApiLevel, outputDir android.OutputPath) android.OutputPath {
257
258	// The output file has the same name as the input file and is in the output directory.
259	output := outputDir.Join(ctx, dexInput.Base())
260
261	// Create a jar specific temporary directory in which to do the work just in case this is called
262	// with the same output directory for multiple modules.
263	tmpDir := outputDir.Join(ctx, dexInput.Base()+"-tmp")
264
265	// If the input is uncompressed then generate the output of the encode rule to an intermediate
266	// file as the final output will need further processing after encoding.
267	soongZipFlags := ""
268	encodeRuleOutput := output
269	if uncompressDex {
270		soongZipFlags = "-L 0"
271		encodeRuleOutput = outputDir.Join(ctx, "unaligned", dexInput.Base())
272	}
273
274	// b/149353192: when a module is instrumented, jacoco adds synthetic members
275	// $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
276	// don't complain when we don't find hidden API flags for the synthetic members.
277	hiddenapiFlags := ""
278	if j, ok := ctx.Module().(interface {
279		shouldInstrument(android.BaseModuleContext) bool
280	}); ok && j.shouldInstrument(ctx) {
281		hiddenapiFlags = "--no-force-assign-all"
282	}
283
284	// If the library is targeted for Q and/or R then make sure that they do not
285	// have any S+ flags encoded as that will break the runtime.
286	minApiLevel := minSdkVersion
287	if !minApiLevel.IsNone() {
288		if minApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(ctx, "R")) {
289			hiddenapiFlags = hiddenapiFlags + " --max-hiddenapi-level=max-target-r"
290		}
291	}
292
293	ctx.Build(pctx, android.BuildParams{
294		Rule:        hiddenAPIEncodeDexRule,
295		Description: "hiddenapi encode dex",
296		Input:       dexInput,
297		Output:      encodeRuleOutput,
298		Implicit:    flagsCSV,
299		Args: map[string]string{
300			"flagsCsv":       flagsCSV.String(),
301			"tmpDir":         tmpDir.String(),
302			"soongZipFlags":  soongZipFlags,
303			"hiddenapiFlags": hiddenapiFlags,
304		},
305	})
306
307	if uncompressDex {
308		TransformZipAlign(ctx, output, encodeRuleOutput)
309	}
310
311	return output
312}
313
314type hiddenApiAnnotationsDependencyTag struct {
315	blueprint.BaseDependencyTag
316	android.LicenseAnnotationSharedDependencyTag
317}
318
319// Tag used to mark dependencies on java_library instances that contains Java source files whose
320// sole purpose is to provide additional hiddenapi annotations.
321var hiddenApiAnnotationsTag hiddenApiAnnotationsDependencyTag
322
323// Mark this tag so dependencies that use it are excluded from APEX contents.
324func (t hiddenApiAnnotationsDependencyTag) ExcludeFromApexContents() {}
325
326var _ android.ExcludeFromApexContentsTag = hiddenApiAnnotationsTag
327