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