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