xref: /aosp_15_r20/build/soong/java/sdk.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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		// Build flag that controls whether Java 21 is used as the
70		// default target version, or Java 17.
71		return JAVA_VERSION_21
72	} else {
73		return JAVA_VERSION_17
74	}
75}
76
77// systemModuleKind returns the kind of system modules to use for the supplied combination of sdk
78// kind and API level.
79func systemModuleKind(sdkKind android.SdkKind, apiLevel android.ApiLevel) android.SdkKind {
80	systemModuleKind := sdkKind
81	if apiLevel.LessThanOrEqualTo(android.LastWithoutModuleLibCoreSystemModules) {
82		// API levels less than or equal to 31 did not provide a core-for-system-modules.jar
83		// specifically for the module-lib API. So, always use the public system modules for them.
84		systemModuleKind = android.SdkPublic
85	} else if systemModuleKind == android.SdkCore {
86		// Core is by definition what is included in the system module for the public API so should
87		// just use its system modules.
88		systemModuleKind = android.SdkPublic
89	} else if systemModuleKind == android.SdkSystem || systemModuleKind == android.SdkTest ||
90		systemModuleKind == android.SdkTestFrameworksCore {
91		// The core system and test APIs are currently the same as the public API so they should use
92		// its system modules.
93		systemModuleKind = android.SdkPublic
94	} else if systemModuleKind == android.SdkSystemServer {
95		// The core system server API is the same as the core module-lib API.
96		systemModuleKind = android.SdkModule
97	}
98
99	return systemModuleKind
100}
101
102func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext android.SdkContext) sdkDep {
103	sdkVersion := sdkContext.SdkVersion(ctx)
104	if !sdkVersion.Valid() {
105		ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.Raw)
106		return sdkDep{}
107	}
108
109	if ctx.DeviceSpecific() || ctx.SocSpecific() {
110		sdkVersion = sdkVersion.ForVendorPartition(ctx)
111	}
112
113	if !sdkVersion.ValidateSystemSdk(ctx) {
114		return sdkDep{}
115	}
116
117	if sdkVersion.UsePrebuilt(ctx) {
118		dir := filepath.Join("prebuilts", "sdk", sdkVersion.ApiLevel.String(), sdkVersion.Kind.String())
119		jar := filepath.Join(dir, "android.jar")
120		// There's no aidl for other SDKs yet.
121		// TODO(77525052): Add aidl files for other SDKs too.
122		publicDir := filepath.Join("prebuilts", "sdk", sdkVersion.ApiLevel.String(), "public")
123		aidl := filepath.Join(publicDir, "framework.aidl")
124		jarPath := android.ExistentPathForSource(ctx, jar)
125		aidlPath := android.ExistentPathForSource(ctx, aidl)
126		lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
127
128		if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
129			return sdkDep{
130				invalidVersion: true,
131				bootclasspath:  []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.Kind, sdkVersion.ApiLevel.String())},
132			}
133		}
134
135		if !jarPath.Valid() {
136			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.Raw, jar)
137			return sdkDep{}
138		}
139
140		if !aidlPath.Valid() {
141			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.Raw, aidl)
142			return sdkDep{}
143		}
144
145		var systemModules string
146		if defaultJavaLanguageVersion(ctx, sdkVersion).usesJavaModules() {
147			systemModuleKind := systemModuleKind(sdkVersion.Kind, sdkVersion.ApiLevel)
148			systemModules = fmt.Sprintf("sdk_%s_%s_system_modules", systemModuleKind, sdkVersion.ApiLevel)
149		}
150
151		return sdkDep{
152			useFiles:      true,
153			jars:          android.Paths{jarPath.Path(), lambdaStubsPath},
154			aidl:          android.OptionalPathForPath(aidlPath.Path()),
155			systemModules: systemModules,
156		}
157	}
158
159	toModule := func(module string, aidl android.Path) sdkDep {
160		// Select the kind of system modules needed for the sdk version.
161		systemModulesKind := systemModuleKind(sdkVersion.Kind, android.FutureApiLevel)
162		systemModules := fmt.Sprintf("core-%s-stubs-system-modules", systemModulesKind)
163		return sdkDep{
164			useModule:          true,
165			bootclasspath:      []string{module, config.DefaultLambdaStubsLibrary},
166			systemModules:      systemModules,
167			java9Classpath:     []string{module},
168			frameworkResModule: "framework-res",
169			aidl:               android.OptionalPathForPath(aidl),
170		}
171	}
172
173	switch sdkVersion.Kind {
174	case android.SdkPrivate:
175		return sdkDep{
176			useModule:          true,
177			systemModules:      corePlatformSystemModules(ctx),
178			bootclasspath:      corePlatformBootclasspathLibraries(ctx),
179			classpath:          config.FrameworkLibraries,
180			frameworkResModule: "framework-res",
181		}
182	case android.SdkNone:
183		systemModules := sdkContext.SystemModules()
184		if systemModules == "" {
185			ctx.PropertyErrorf("sdk_version",
186				`system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
187		} else if systemModules == "none" {
188			return sdkDep{
189				noStandardLibs: true,
190			}
191		}
192
193		return sdkDep{
194			useModule:      true,
195			noStandardLibs: true,
196			systemModules:  systemModules,
197			bootclasspath:  []string{systemModules},
198		}
199	case android.SdkCorePlatform:
200		return sdkDep{
201			useModule:        true,
202			systemModules:    corePlatformSystemModules(ctx),
203			bootclasspath:    corePlatformBootclasspathLibraries(ctx),
204			noFrameworksLibs: true,
205		}
206	case android.SdkPublic, android.SdkSystem, android.SdkTest, android.SdkTestFrameworksCore:
207		return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), sdkFrameworkAidlPath(ctx))
208	case android.SdkCore:
209		return sdkDep{
210			useModule:        true,
211			bootclasspath:    []string{android.SdkCore.DefaultJavaLibraryName(), config.DefaultLambdaStubsLibrary},
212			systemModules:    "core-public-stubs-system-modules",
213			noFrameworksLibs: true,
214		}
215	case android.SdkModule:
216		// TODO(146757305): provide .apk and .aidl that have more APIs for modules
217		return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), nonUpdatableFrameworkAidlPath(ctx))
218	case android.SdkSystemServer:
219		// TODO(146757305): provide .apk and .aidl that have more APIs for modules
220		return toModule(sdkVersion.Kind.DefaultJavaLibraryName(), sdkFrameworkAidlPath(ctx))
221	default:
222		panic(fmt.Errorf("invalid sdk %q", sdkVersion.Raw))
223	}
224}
225
226func sdkSingletonFactory() android.Singleton {
227	return sdkSingleton{}
228}
229
230type sdkSingleton struct{}
231
232func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
233	if ctx.Config().AlwaysUsePrebuiltSdks() {
234		return
235	}
236
237	createSdkFrameworkAidl(ctx)
238	createNonUpdatableFrameworkAidl(ctx)
239	createAPIFingerprint(ctx)
240}
241
242// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
243func createSdkFrameworkAidl(ctx android.SingletonContext) {
244	stubsModules := []string{
245		android.SdkPublic.DefaultJavaLibraryName(),
246		android.SdkTest.DefaultJavaLibraryName(),
247		android.SdkSystem.DefaultJavaLibraryName(),
248	}
249
250	combinedAidl := sdkFrameworkAidlPath(ctx)
251	tempPath := tempPathForRestat(ctx, combinedAidl)
252
253	rule := createFrameworkAidl(stubsModules, tempPath, ctx)
254
255	commitChangeForRestat(rule, tempPath, combinedAidl)
256
257	rule.Build("framework_aidl", "generate framework.aidl")
258}
259
260// Creates a version of framework.aidl for the non-updatable part of the platform.
261func createNonUpdatableFrameworkAidl(ctx android.SingletonContext) {
262	stubsModules := []string{android.SdkModule.DefaultJavaLibraryName()}
263
264	combinedAidl := nonUpdatableFrameworkAidlPath(ctx)
265	tempPath := tempPathForRestat(ctx, combinedAidl)
266
267	rule := createFrameworkAidl(stubsModules, tempPath, ctx)
268
269	commitChangeForRestat(rule, tempPath, combinedAidl)
270
271	rule.Build("framework_non_updatable_aidl", "generate framework_non_updatable.aidl")
272}
273
274func createFrameworkAidl(stubsModules []string, path android.WritablePath, ctx android.SingletonContext) *android.RuleBuilder {
275	stubsJars := make([]android.Paths, len(stubsModules))
276
277	ctx.VisitAllModules(func(module android.Module) {
278		// Collect dex jar paths for the modules listed above.
279		if j, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
280			name := ctx.ModuleName(module)
281			if i := android.IndexList(name, stubsModules); i != -1 {
282				stubsJars[i] = j.HeaderJars
283			}
284		}
285	})
286
287	var missingDeps []string
288
289	for i := range stubsJars {
290		if stubsJars[i] == nil {
291			if ctx.Config().AllowMissingDependencies() {
292				missingDeps = append(missingDeps, stubsModules[i])
293			} else {
294				ctx.Errorf("failed to find dex jar path for module %q", stubsModules[i])
295			}
296		}
297	}
298
299	rule := android.NewRuleBuilder(pctx, ctx)
300	rule.MissingDeps(missingDeps)
301
302	var aidls android.Paths
303	for _, jars := range stubsJars {
304		for _, jar := range jars {
305			aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
306
307			rule.Command().
308				Text("rm -f").Output(aidl)
309
310			rule.Command().
311				BuiltTool("sdkparcelables").
312				Input(jar).
313				Output(aidl).
314				Flag("--guarantee_stable")
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