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