• 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	"fmt"
19	"path/filepath"
20
21	"android/soong/android"
22	"android/soong/java/config"
23
24	"github.com/google/blueprint/pathtools"
25)
26
27func init() {
28	android.RegisterParallelSingletonType("sdk", sdkSingletonFactory)
29	android.RegisterMakeVarsProvider(pctx, sdkMakeVars)
30}
31
32var sdkFrameworkAidlPathKey = android.NewOnceKey("sdkFrameworkAidlPathKey")
33var nonUpdatableFrameworkAidlPathKey = android.NewOnceKey("nonUpdatableFrameworkAidlPathKey")
34
35func UseApiFingerprint(ctx android.BaseModuleContext) (useApiFingerprint bool, fingerprintSdkVersion string, fingerprintDeps android.OutputPath) {
36	if ctx.Config().UnbundledBuild() && !ctx.Config().AlwaysUsePrebuiltSdks() {
37		apiFingerprintTrue := ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT")
38		dessertShaIsSet := ctx.Config().Getenv("UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA") != ""
39
40		// Error when both UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT and UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA are set
41		if apiFingerprintTrue && dessertShaIsSet {
42			ctx.ModuleErrorf("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT=true cannot be set alongside with UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA")
43		}
44
45		useApiFingerprint = apiFingerprintTrue || dessertShaIsSet
46		if apiFingerprintTrue {
47			fingerprintSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", android.ApiFingerprintPath(ctx).String())
48			fingerprintDeps = android.ApiFingerprintPath(ctx)
49		}
50		if dessertShaIsSet {
51			fingerprintSdkVersion = ctx.Config().Getenv("UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA")
52		}
53	}
54	return useApiFingerprint, fingerprintSdkVersion, fingerprintDeps
55}
56
57func defaultJavaLanguageVersion(ctx android.EarlyModuleContext, s android.SdkSpec) javaVersion {
58	sdk, err := s.EffectiveVersion(ctx)
59	if err != nil {
60		ctx.PropertyErrorf("sdk_version", "%s", err)
61	}
62	if sdk.FinalOrFutureInt() <= 29 {
63		return JAVA_VERSION_8
64	} else if sdk.FinalOrFutureInt() <= 31 {
65		return JAVA_VERSION_9
66	} else if sdk.FinalOrFutureInt() <= 33 {
67		return JAVA_VERSION_11
68	} else if ctx.Config().TargetsJava21() {
69		// Temporary experimental flag to be able to try and build with
70		// java version 21 options.  The flag, if used, just sets Java
71		// 21 as the default version, leaving any components that
72		// target an older version intact.
73		return JAVA_VERSION_21
74	} else {
75		return JAVA_VERSION_17
76	}
77}
78
79// systemModuleKind returns the kind of system modules to use for the supplied combination of sdk
80// kind and API level.
81func systemModuleKind(sdkKind android.SdkKind, apiLevel android.ApiLevel) android.SdkKind {
82	systemModuleKind := sdkKind
83	if apiLevel.LessThanOrEqualTo(android.LastWithoutModuleLibCoreSystemModules) {
84		// API levels less than or equal to 31 did not provide a core-for-system-modules.jar
85		// specifically for the module-lib API. So, always use the public system modules for them.
86		systemModuleKind = android.SdkPublic
87	} else if systemModuleKind == android.SdkCore {
88		// Core is by definition what is included in the system module for the public API so should
89		// just use its system modules.
90		systemModuleKind = android.SdkPublic
91	} else if systemModuleKind == android.SdkSystem || systemModuleKind == android.SdkTest ||
92		systemModuleKind == android.SdkTestFrameworksCore {
93		// The core system and test APIs are currently the same as the public API so they should use
94		// its system modules.
95		systemModuleKind = android.SdkPublic
96	} else if systemModuleKind == android.SdkSystemServer {
97		// The core system server API is the same as the core module-lib API.
98		systemModuleKind = android.SdkModule
99	}
100
101	return systemModuleKind
102}
103
104func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext android.SdkContext) sdkDep {
105	sdkVersion := sdkContext.SdkVersion(ctx)
106	if !sdkVersion.Valid() {
107		ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.Raw)
108		return sdkDep{}
109	}
110
111	if ctx.DeviceSpecific() || ctx.SocSpecific() {
112		sdkVersion = sdkVersion.ForVendorPartition(ctx)
113	}
114
115	if !sdkVersion.ValidateSystemSdk(ctx) {
116		return sdkDep{}
117	}
118
119	if sdkVersion.UsePrebuilt(ctx) {
120		dir := filepath.Join("prebuilts", "sdk", sdkVersion.ApiLevel.String(), sdkVersion.Kind.String())
121		jar := filepath.Join(dir, "android.jar")
122		// There's no aidl for other SDKs yet.
123		// TODO(77525052): Add aidl files for other SDKs too.
124		publicDir := filepath.Join("prebuilts", "sdk", sdkVersion.ApiLevel.String(), "public")
125		aidl := filepath.Join(publicDir, "framework.aidl")
126		jarPath := android.ExistentPathForSource(ctx, jar)
127		aidlPath := android.ExistentPathForSource(ctx, aidl)
128		lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
129
130		if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
131			return sdkDep{
132				invalidVersion: true,
133				bootclasspath:  []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.Kind, sdkVersion.ApiLevel.String())},
134			}
135		}
136
137		if !jarPath.Valid() {
138			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.Raw, jar)
139			return sdkDep{}
140		}
141
142		if !aidlPath.Valid() {
143			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.Raw, aidl)
144			return sdkDep{}
145		}
146
147		var systemModules string
148		if defaultJavaLanguageVersion(ctx, sdkVersion).usesJavaModules() {
149			systemModuleKind := systemModuleKind(sdkVersion.Kind, sdkVersion.ApiLevel)
150			systemModules = fmt.Sprintf("sdk_%s_%s_system_modules", systemModuleKind, sdkVersion.ApiLevel)
151		}
152
153		return sdkDep{
154			useFiles:      true,
155			jars:          android.Paths{jarPath.Path(), lambdaStubsPath},
156			aidl:          android.OptionalPathForPath(aidlPath.Path()),
157			systemModules: systemModules,
158		}
159	}
160
161	toModule := func(module string, aidl android.Path) sdkDep {
162		// Select the kind of system modules needed for the sdk version.
163		systemModulesKind := systemModuleKind(sdkVersion.Kind, android.FutureApiLevel)
164		systemModules := fmt.Sprintf("core-%s-stubs-system-modules", systemModulesKind)
165		return sdkDep{
166			useModule:          true,
167			bootclasspath:      []string{module, config.DefaultLambdaStubsLibrary},
168			systemModules:      systemModules,
169			java9Classpath:     []string{module},
170			frameworkResModule: "framework-res",
171			aidl:               android.OptionalPathForPath(aidl),
172		}
173	}
174
175	switch sdkVersion.Kind {
176	case android.SdkPrivate:
177		return sdkDep{
178			useModule:          true,
179			systemModules:      corePlatformSystemModules(ctx),
180			bootclasspath:      corePlatformBootclasspathLibraries(ctx),
181			classpath:          config.FrameworkLibraries,
182			frameworkResModule: "framework-res",
183		}
184	case android.SdkNone:
185		systemModules := sdkContext.SystemModules()
186		if systemModules == "" {
187			ctx.PropertyErrorf("sdk_version",
188				`system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
189		} else if systemModules == "none" {
190			return sdkDep{
191				noStandardLibs: true,
192			}
193		}
194
195		return sdkDep{
196			useModule:      true,
197			noStandardLibs: true,
198			systemModules:  systemModules,
199			bootclasspath:  []string{systemModules},
200		}
201	case android.SdkCorePlatform:
202		return sdkDep{
203			useModule:        true,
204			systemModules:    corePlatformSystemModules(ctx),
205			bootclasspath:    corePlatformBootclasspathLibraries(ctx),
206			noFrameworksLibs: true,
207		}
208	case android.SdkPublic, android.SdkSystem, android.SdkTest, android.SdkTestFrameworksCore:
209		return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), sdkFrameworkAidlPath(ctx))
210	case android.SdkCore:
211		return sdkDep{
212			useModule:        true,
213			bootclasspath:    []string{android.SdkCore.DefaultJavaLibraryName(), config.DefaultLambdaStubsLibrary},
214			systemModules:    "core-public-stubs-system-modules",
215			noFrameworksLibs: true,
216		}
217	case android.SdkModule:
218		// TODO(146757305): provide .apk and .aidl that have more APIs for modules
219		return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), nonUpdatableFrameworkAidlPath(ctx))
220	case android.SdkSystemServer:
221		// TODO(146757305): provide .apk and .aidl that have more APIs for modules
222		return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), sdkFrameworkAidlPath(ctx))
223	default:
224		panic(fmt.Errorf("invalid sdk %q", sdkVersion.Raw))
225	}
226}
227
228func sdkSingletonFactory() android.Singleton {
229	return sdkSingleton{}
230}
231
232type sdkSingleton struct{}
233
234func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
235	if ctx.Config().AlwaysUsePrebuiltSdks() {
236		return
237	}
238
239	createSdkFrameworkAidl(ctx)
240	createNonUpdatableFrameworkAidl(ctx)
241	createAPIFingerprint(ctx)
242}
243
244// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
245func createSdkFrameworkAidl(ctx android.SingletonContext) {
246	stubsModules := []string{
247		android.SdkPublic.DefaultJavaLibraryName(),
248		android.SdkTest.DefaultJavaLibraryName(),
249		android.SdkSystem.DefaultJavaLibraryName(),
250	}
251
252	combinedAidl := sdkFrameworkAidlPath(ctx)
253	tempPath := tempPathForRestat(ctx, combinedAidl)
254
255	rule := createFrameworkAidl(stubsModules, tempPath, ctx)
256
257	commitChangeForRestat(rule, tempPath, combinedAidl)
258
259	rule.Build("framework_aidl", "generate framework.aidl")
260}
261
262// Creates a version of framework.aidl for the non-updatable part of the platform.
263func createNonUpdatableFrameworkAidl(ctx android.SingletonContext) {
264	stubsModules := []string{android.SdkModule.DefaultJavaLibraryName()}
265
266	combinedAidl := nonUpdatableFrameworkAidlPath(ctx)
267	tempPath := tempPathForRestat(ctx, combinedAidl)
268
269	rule := createFrameworkAidl(stubsModules, tempPath, ctx)
270
271	commitChangeForRestat(rule, tempPath, combinedAidl)
272
273	rule.Build("framework_non_updatable_aidl", "generate framework_non_updatable.aidl")
274}
275
276func createFrameworkAidl(stubsModules []string, path android.WritablePath, ctx android.SingletonContext) *android.RuleBuilder {
277	stubsJars := make([]android.Paths, len(stubsModules))
278
279	ctx.VisitAllModules(func(module android.Module) {
280		// Collect dex jar paths for the modules listed above.
281		if j, ok := android.SingletonModuleProvider(ctx, module, JavaInfoProvider); ok {
282			name := ctx.ModuleName(module)
283			if i := android.IndexList(name, stubsModules); i != -1 {
284				stubsJars[i] = j.HeaderJars
285			}
286		}
287	})
288
289	var missingDeps []string
290
291	for i := range stubsJars {
292		if stubsJars[i] == nil {
293			if ctx.Config().AllowMissingDependencies() {
294				missingDeps = append(missingDeps, stubsModules[i])
295			} else {
296				ctx.Errorf("failed to find dex jar path for module %q", stubsModules[i])
297			}
298		}
299	}
300
301	rule := android.NewRuleBuilder(pctx, ctx)
302	rule.MissingDeps(missingDeps)
303
304	var aidls android.Paths
305	for _, jars := range stubsJars {
306		for _, jar := range jars {
307			aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
308
309			rule.Command().
310				Text("rm -f").Output(aidl)
311			rule.Command().
312				BuiltTool("sdkparcelables").
313				Input(jar).
314				Output(aidl)
315
316			aidls = append(aidls, aidl)
317		}
318	}
319
320	rule.Command().
321		Text("rm -f").Output(path)
322	rule.Command().
323		Text("cat").
324		Inputs(aidls).
325		Text("| sort -u >").
326		Output(path)
327
328	return rule
329}
330
331func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
332	return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
333		return android.PathForOutput(ctx, "framework.aidl")
334	}).(android.OutputPath)
335}
336
337func nonUpdatableFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
338	return ctx.Config().Once(nonUpdatableFrameworkAidlPathKey, func() interface{} {
339		return android.PathForOutput(ctx, "framework_non_updatable.aidl")
340	}).(android.OutputPath)
341}
342
343// Create api_fingerprint.txt
344func createAPIFingerprint(ctx android.SingletonContext) {
345	out := android.ApiFingerprintPath(ctx)
346
347	rule := android.NewRuleBuilder(pctx, ctx)
348
349	rule.Command().
350		Text("rm -f").Output(out)
351	cmd := rule.Command()
352
353	if ctx.Config().PlatformSdkCodename() == "REL" {
354		cmd.Text("echo REL >").Output(out)
355	} else if ctx.Config().FrameworksBaseDirExists(ctx) && !ctx.Config().AlwaysUsePrebuiltSdks() {
356		cmd.Text("cat")
357		apiTxtFileModules := []string{
358			"api_fingerprint",
359		}
360		count := 0
361		ctx.VisitAllModules(func(module android.Module) {
362			name := ctx.ModuleName(module)
363			if android.InList(name, apiTxtFileModules) {
364				cmd.Inputs(android.OutputFilesForModule(ctx, module, ""))
365				count++
366			}
367		})
368		if count != len(apiTxtFileModules) {
369			ctx.Errorf("Could not find expected API module %v, found %d\n", apiTxtFileModules, count)
370			return
371		}
372		cmd.Text(">").
373			Output(out)
374	} else {
375		// Unbundled build
376		// TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
377		cmd.Text("echo").
378			Flag(ctx.Config().PlatformPreviewSdkVersion()).
379			Text(">").
380			Output(out)
381	}
382
383	rule.Build("api_fingerprint", "generate api_fingerprint.txt")
384}
385
386func sdkMakeVars(ctx android.MakeVarsContext) {
387	if ctx.Config().AlwaysUsePrebuiltSdks() {
388		return
389	}
390
391	ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
392	ctx.Strict("API_FINGERPRINT", android.ApiFingerprintPath(ctx).String())
393}
394