1// Copyright 2017 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 config
16
17import (
18	"path/filepath"
19	"runtime"
20	"strings"
21
22	_ "github.com/google/blueprint/bootstrap"
23
24	"android/soong/android"
25	"android/soong/remoteexec"
26)
27
28var (
29	pctx = android.NewPackageContext("android/soong/java/config")
30
31	LegacyCorePlatformBootclasspathLibraries = []string{"legacy.core.platform.api.stubs", "core-lambda-stubs"}
32	LegacyCorePlatformSystemModules          = "legacy-core-platform-api-stubs-system-modules"
33	StableCorePlatformBootclasspathLibraries = []string{"stable.core.platform.api.stubs", "core-lambda-stubs"}
34	StableCorePlatformSystemModules          = "stable-core-platform-api-stubs-system-modules"
35	FrameworkLibraries                       = []string{"ext", "framework"}
36	DefaultLambdaStubsLibrary                = "core-lambda-stubs"
37	SdkLambdaStubsPath                       = "prebuilts/sdk/tools/core-lambda-stubs.jar"
38
39	DefaultMakeJacocoExcludeFilter = []string{"org.junit.*", "org.jacoco.*", "org.mockito.*"}
40	DefaultJacocoExcludeFilter     = []string{"org.junit.**", "org.jacoco.**", "org.mockito.**"}
41
42	InstrumentFrameworkModules = []string{
43		"framework",
44		"telephony-common",
45		"services",
46		"android.car",
47		"android.car7",
48		"conscrypt",
49		"core-icu4j",
50		"core-oj",
51		"core-libart",
52		// TODO: Could this be all updatable bootclasspath jars?
53		"updatable-media",
54		"framework-sdkextensions",
55		"android.net.ipsec.ike",
56	}
57)
58
59const (
60	JavaVmFlags  = `-XX:OnError="cat hs_err_pid%p.log" -XX:CICompilerCount=6 -XX:+UseDynamicNumberOfGCThreads`
61	JavacVmFlags = `-J-XX:OnError="cat hs_err_pid%p.log" -J-XX:CICompilerCount=6 -J-XX:+UseDynamicNumberOfGCThreads`
62)
63
64func init() {
65	pctx.Import("github.com/google/blueprint/bootstrap")
66
67	pctx.StaticVariable("JavacHeapSize", "2048M")
68	pctx.StaticVariable("JavacHeapFlags", "-J-Xmx${JavacHeapSize}")
69	pctx.StaticVariable("DexFlags", "-JXX:OnError='cat hs_err_pid%p.log' -JXX:CICompilerCount=6 -JXX:+UseDynamicNumberOfGCThreads")
70
71	pctx.StaticVariable("CommonJdkFlags", strings.Join([]string{
72		`-Xmaxerrs 9999999`,
73		`-encoding UTF-8`,
74		`-sourcepath ""`,
75		`-g`,
76		// Turbine leaves out bridges which can cause javac to unnecessarily insert them into
77		// subclasses (b/65645120).  Setting this flag causes our custom javac to assume that
78		// the missing bridges will exist at runtime and not recreate them in subclasses.
79		// If a different javac is used the flag will be ignored and extra bridges will be inserted.
80		// The flag is implemented by https://android-review.googlesource.com/c/486427
81		`-XDskipDuplicateBridges=true`,
82
83		// b/65004097: prevent using java.lang.invoke.StringConcatFactory when using -target 1.9
84		`-XDstringConcat=inline`,
85	}, " "))
86
87	pctx.StaticVariable("JavaVmFlags", JavaVmFlags)
88	pctx.StaticVariable("JavacVmFlags", JavacVmFlags)
89
90	pctx.VariableConfigMethod("hostPrebuiltTag", android.Config.PrebuiltOS)
91
92	pctx.VariableFunc("JavaHome", func(ctx android.PackageVarContext) string {
93		// This is set up and guaranteed by soong_ui
94		return ctx.Config().Getenv("ANDROID_JAVA_HOME")
95	})
96	pctx.VariableFunc("JlinkVersion", func(ctx android.PackageVarContext) string {
97		if override := ctx.Config().Getenv("OVERRIDE_JLINK_VERSION_NUMBER"); override != "" {
98			return override
99		}
100		return "11"
101	})
102
103	pctx.SourcePathVariable("JavaToolchain", "${JavaHome}/bin")
104	pctx.SourcePathVariableWithEnvOverride("JavacCmd",
105		"${JavaToolchain}/javac", "ALTERNATE_JAVAC")
106	pctx.SourcePathVariable("JavaCmd", "${JavaToolchain}/java")
107	pctx.SourcePathVariable("JarCmd", "${JavaToolchain}/jar")
108	pctx.SourcePathVariable("JavadocCmd", "${JavaToolchain}/javadoc")
109	pctx.SourcePathVariable("JlinkCmd", "${JavaToolchain}/jlink")
110	pctx.SourcePathVariable("JmodCmd", "${JavaToolchain}/jmod")
111	pctx.SourcePathVariable("JrtFsJar", "${JavaHome}/lib/jrt-fs.jar")
112	pctx.SourcePathVariable("JavaKytheExtractorJar", "prebuilts/build-tools/common/framework/javac_extractor.jar")
113	pctx.SourcePathVariable("Ziptime", "prebuilts/build-tools/${hostPrebuiltTag}/bin/ziptime")
114
115	pctx.HostBinToolVariable("GenKotlinBuildFileCmd", "gen-kotlin-build-file.py")
116
117	pctx.SourcePathVariable("JarArgsCmd", "build/soong/scripts/jar-args.sh")
118	pctx.SourcePathVariable("PackageCheckCmd", "build/soong/scripts/package-check.sh")
119	pctx.HostBinToolVariable("ExtractJarPackagesCmd", "extract_jar_packages")
120	pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
121	pctx.HostBinToolVariable("MergeZipsCmd", "merge_zips")
122	pctx.HostBinToolVariable("Zip2ZipCmd", "zip2zip")
123	pctx.HostBinToolVariable("ZipSyncCmd", "zipsync")
124	pctx.HostBinToolVariable("ApiCheckCmd", "apicheck")
125	pctx.HostBinToolVariable("D8Cmd", "d8")
126	pctx.HostBinToolVariable("R8Cmd", "r8-compat-proguard")
127	pctx.HostBinToolVariable("HiddenAPICmd", "hiddenapi")
128	pctx.HostBinToolVariable("ExtractApksCmd", "extract_apks")
129	pctx.VariableFunc("TurbineJar", func(ctx android.PackageVarContext) string {
130		turbine := "turbine.jar"
131		if ctx.Config().UnbundledBuild() {
132			return "prebuilts/build-tools/common/framework/" + turbine
133		} else {
134			return ctx.Config().HostJavaToolPath(ctx, turbine).String()
135		}
136	})
137
138	pctx.HostJavaToolVariable("JarjarCmd", "jarjar.jar")
139	pctx.HostJavaToolVariable("JsilverJar", "jsilver.jar")
140	pctx.HostJavaToolVariable("DoclavaJar", "doclava.jar")
141	pctx.HostJavaToolVariable("MetalavaJar", "metalava.jar")
142	pctx.HostJavaToolVariable("DokkaJar", "dokka.jar")
143	pctx.HostJavaToolVariable("JetifierJar", "jetifier.jar")
144	pctx.HostJavaToolVariable("R8Jar", "r8-compat-proguard.jar")
145	pctx.HostJavaToolVariable("D8Jar", "d8.jar")
146
147	pctx.HostBinToolVariable("SoongJavacWrapper", "soong_javac_wrapper")
148	pctx.HostBinToolVariable("DexpreoptGen", "dexpreopt_gen")
149
150	pctx.VariableFunc("REJavaPool", remoteexec.EnvOverrideFunc("RBE_JAVA_POOL", "java16"))
151	pctx.VariableFunc("REJavacExecStrategy", remoteexec.EnvOverrideFunc("RBE_JAVAC_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
152	pctx.VariableFunc("RED8ExecStrategy", remoteexec.EnvOverrideFunc("RBE_D8_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
153	pctx.VariableFunc("RER8ExecStrategy", remoteexec.EnvOverrideFunc("RBE_R8_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
154	pctx.VariableFunc("RETurbineExecStrategy", remoteexec.EnvOverrideFunc("RBE_TURBINE_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
155	pctx.VariableFunc("RESignApkExecStrategy", remoteexec.EnvOverrideFunc("RBE_SIGNAPK_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
156	pctx.VariableFunc("REJarExecStrategy", remoteexec.EnvOverrideFunc("RBE_JAR_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
157	pctx.VariableFunc("REZipExecStrategy", remoteexec.EnvOverrideFunc("RBE_ZIP_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
158
159	pctx.HostJavaToolVariable("JacocoCLIJar", "jacoco-cli.jar")
160
161	pctx.HostBinToolVariable("ManifestCheckCmd", "manifest_check")
162	pctx.HostBinToolVariable("ManifestFixerCmd", "manifest_fixer")
163
164	pctx.HostBinToolVariable("ManifestMergerCmd", "manifest-merger")
165
166	pctx.HostBinToolVariable("Class2Greylist", "class2greylist")
167	pctx.HostBinToolVariable("HiddenAPI", "hiddenapi")
168
169	hostBinToolVariableWithSdkToolsPrebuilt("Aapt2Cmd", "aapt2")
170	hostBinToolVariableWithBuildToolsPrebuilt("AidlCmd", "aidl")
171	hostBinToolVariableWithBuildToolsPrebuilt("ZipAlign", "zipalign")
172
173	hostJavaToolVariableWithSdkToolsPrebuilt("SignapkCmd", "signapk")
174	// TODO(ccross): this should come from the signapk dependencies, but we don't have any way
175	// to express host JNI dependencies yet.
176	hostJNIToolVariableWithSdkToolsPrebuilt("SignapkJniLibrary", "libconscrypt_openjdk_jni")
177}
178
179func hostBinToolVariableWithSdkToolsPrebuilt(name, tool string) {
180	pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
181		if ctx.Config().UnbundledBuild() || ctx.Config().IsPdkBuild() {
182			return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "bin", tool)
183		} else {
184			return ctx.Config().HostToolPath(ctx, tool).String()
185		}
186	})
187}
188
189func hostJavaToolVariableWithSdkToolsPrebuilt(name, tool string) {
190	pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
191		if ctx.Config().UnbundledBuild() || ctx.Config().IsPdkBuild() {
192			return filepath.Join("prebuilts/sdk/tools/lib", tool+".jar")
193		} else {
194			return ctx.Config().HostJavaToolPath(ctx, tool+".jar").String()
195		}
196	})
197}
198
199func hostJNIToolVariableWithSdkToolsPrebuilt(name, tool string) {
200	pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
201		if ctx.Config().UnbundledBuild() || ctx.Config().IsPdkBuild() {
202			ext := ".so"
203			if runtime.GOOS == "darwin" {
204				ext = ".dylib"
205			}
206			return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "lib64", tool+ext)
207		} else {
208			return ctx.Config().HostJNIToolPath(ctx, tool).String()
209		}
210	})
211}
212
213func hostBinToolVariableWithBuildToolsPrebuilt(name, tool string) {
214	pctx.VariableFunc(name, func(ctx android.PackageVarContext) string {
215		if ctx.Config().UnbundledBuild() || ctx.Config().IsPdkBuild() {
216			return filepath.Join("prebuilts/build-tools", ctx.Config().PrebuiltOS(), "bin", tool)
217		} else {
218			return ctx.Config().HostToolPath(ctx, tool).String()
219		}
220	})
221}
222
223// JavaCmd returns a SourcePath object with the path to the java command.
224func JavaCmd(ctx android.PathContext) android.SourcePath {
225	return javaTool(ctx, "java")
226}
227
228// JavadocCmd returns a SourcePath object with the path to the java command.
229func JavadocCmd(ctx android.PathContext) android.SourcePath {
230	return javaTool(ctx, "javadoc")
231}
232
233func javaTool(ctx android.PathContext, tool string) android.SourcePath {
234	type javaToolKey string
235
236	key := android.NewCustomOnceKey(javaToolKey(tool))
237
238	return ctx.Config().OnceSourcePath(key, func() android.SourcePath {
239		return javaToolchain(ctx).Join(ctx, tool)
240	})
241
242}
243
244var javaToolchainKey = android.NewOnceKey("javaToolchain")
245
246func javaToolchain(ctx android.PathContext) android.SourcePath {
247	return ctx.Config().OnceSourcePath(javaToolchainKey, func() android.SourcePath {
248		return javaHome(ctx).Join(ctx, "bin")
249	})
250}
251
252var javaHomeKey = android.NewOnceKey("javaHome")
253
254func javaHome(ctx android.PathContext) android.SourcePath {
255	return ctx.Config().OnceSourcePath(javaHomeKey, func() android.SourcePath {
256		// This is set up and guaranteed by soong_ui
257		return android.PathForSource(ctx, ctx.Config().Getenv("ANDROID_JAVA_HOME"))
258	})
259}
260