[Gradle] Read more system properties through Gradle providers

Related to #KT-43605
This commit is contained in:
Alexander Likhachev
2021-04-01 20:51:54 +03:00
parent ccfc16c84a
commit 16dc0a7d29
4 changed files with 18 additions and 9 deletions
@@ -36,7 +36,12 @@ enum class CompilerSystemProperties(val property: String) {
KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY("kotlin.environment.keepalive"), KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY("kotlin.environment.keepalive"),
COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS("kotlin.daemon.custom.run.files.path.for.tests"), COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS("kotlin.daemon.custom.run.files.path.for.tests"),
KOTLIN_COLORS_ENABLED_PROPERTY("kotlin.colors.enabled"), KOTLIN_COLORS_ENABLED_PROPERTY("kotlin.colors.enabled"),
OS_NAME("os.name") OS_NAME("os.name"),
TMP_DIR("java.io.tmpdir"),
USER_HOME("user.home"),
JAVA_VERSION("java.specification.version"),
JAVA_HOME("java.home"),
JAVA_CLASS_PATH("java.class.path"),
; ;
var value var value
@@ -45,6 +50,9 @@ enum class CompilerSystemProperties(val property: String) {
(systemPropertySetter ?: System::setProperty)(property, value!!) (systemPropertySetter ?: System::setProperty)(property, value!!)
} }
val safeValue
get() = value ?: error("No value for $property system property")
fun clear(): String? = (systemPropertyCleaner ?: System::clearProperty)(property) fun clear(): String? = (systemPropertyCleaner ?: System::clearProperty)(property)
companion object { companion object {
@@ -360,7 +360,7 @@ class KotlinCompilerClient : KotlinCompilerDaemonClient {
} }
private fun detectCompilerClasspath(): List<String>? = private fun detectCompilerClasspath(): List<String>? =
System.getProperty("java.class.path") CompilerSystemProperties.JAVA_CLASS_PATH.value
?.split(File.pathSeparator) ?.split(File.pathSeparator)
?.map { File(it).parentFile } ?.map { File(it).parentFile }
?.distinct() ?.distinct()
@@ -454,7 +454,7 @@ class KotlinCompilerClient : KotlinCompilerDaemonClient {
daemonOptions: DaemonOptions, daemonOptions: DaemonOptions,
reportingTargets: DaemonReportingTargets reportingTargets: DaemonReportingTargets
): Boolean { ): Boolean {
val javaExecutable = File(File(System.getProperty("java.home"), "bin"), "java") val javaExecutable = File(File(CompilerSystemProperties.JAVA_HOME.safeValue, "bin"), "java")
val serverHostname = CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.value ?: error("${CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.property} is not set!") val serverHostname = CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.value ?: error("${CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.property} is not set!")
val platformSpecificOptions = listOf( val platformSpecificOptions = listOf(
// hide daemon window // hide daemon window
@@ -299,7 +299,7 @@ object KotlinCompilerClient {
} }
fun detectCompilerClasspath(): List<String>? = fun detectCompilerClasspath(): List<String>? =
System.getProperty("java.class.path") CompilerSystemProperties.JAVA_CLASS_PATH.value
?.split(File.pathSeparator) ?.split(File.pathSeparator)
?.map { File(it).parentFile } ?.map { File(it).parentFile }
?.distinct() ?.distinct()
@@ -368,13 +368,13 @@ object KotlinCompilerClient {
private fun startDaemon(compilerId: CompilerId, daemonJVMOptions: DaemonJVMOptions, daemonOptions: DaemonOptions, reportingTargets: DaemonReportingTargets): Boolean { private fun startDaemon(compilerId: CompilerId, daemonJVMOptions: DaemonJVMOptions, daemonOptions: DaemonOptions, reportingTargets: DaemonReportingTargets): Boolean {
val javaExecutable = File(File(System.getProperty("java.home"), "bin"), "java") val javaExecutable = File(File(CompilerSystemProperties.JAVA_HOME.safeValue, "bin"), "java")
val serverHostname = CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.value ?: error("${CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.property} is not set!") val serverHostname = CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.value ?: error("${CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.property} is not set!")
val platformSpecificOptions = listOf( val platformSpecificOptions = listOf(
// hide daemon window // hide daemon window
"-Djava.awt.headless=true", "-Djava.awt.headless=true",
"-D$${CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.property}=$serverHostname") "-D$${CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.property}=$serverHostname")
val javaVersion = System.getProperty("java.specification.version")?.toIntOrNull() val javaVersion = CompilerSystemProperties.JAVA_VERSION.value?.toIntOrNull()
val javaIllegalAccessWorkaround = val javaIllegalAccessWorkaround =
if (javaVersion != null && javaVersion >= 16) if (javaVersion != null && javaVersion >= 16)
listOf("--illegal-access=permit") listOf("--illegal-access=permit")
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.daemon.common package org.jetbrains.kotlin.daemon.common
import org.jetbrains.kotlin.cli.common.CompilerSystemProperties
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
import java.io.File import java.io.File
@@ -26,7 +27,7 @@ enum class OSKind {
Unknown; Unknown;
companion object { companion object {
val current: OSKind = System.getProperty("os.name").toLowerCaseAsciiOnly().let { val current: OSKind = CompilerSystemProperties.OS_NAME.safeValue.toLowerCaseAsciiOnly().let {
when { when {
// partly taken from http://www.code4copy.com/java/post/detecting-os-type-in-java // partly taken from http://www.code4copy.com/java/post/detecting-os-type-in-java
it.startsWith("windows") -> Windows it.startsWith("windows") -> Windows
@@ -57,8 +58,8 @@ private fun String?.orDefault(v: String): String =
object FileSystem { object FileSystem {
val userHomePath: String get() = System.getProperty("user.home") val userHomePath: String get() = CompilerSystemProperties.USER_HOME.safeValue
val tempPath: String get() = System.getProperty("java.io.tmpdir") val tempPath: String get() = CompilerSystemProperties.TMP_DIR.safeValue
val logFilesPath: String get() = tempPath val logFilesPath: String get() = tempPath