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