From aabb6b55bb64b65d2327cbeb15e9f238bb433cbd Mon Sep 17 00:00:00 2001 From: Kaiz_JP Date: Tue, 8 Oct 2024 01:23:43 +0900 Subject: [PATCH] KT-72027: fix kotlin-gradle-plugin tries to execute uname on Windows on ARM --- .../kotlin/gradle/internal/hostUnameOutput.kt | 10 +++- .../gradle/targets/js/nodejs/Platform.kt | 57 +++++++++++++++---- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/hostUnameOutput.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/hostUnameOutput.kt index e82e33f3aaa2b..d8dd06613419c 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/hostUnameOutput.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/hostUnameOutput.kt @@ -7,12 +7,18 @@ package org.jetbrains.kotlin.gradle.internal import org.gradle.api.provider.Provider import org.gradle.api.provider.ProviderFactory +import org.gradle.internal.os.OperatingSystem internal val ProviderFactory.unameExecResult: Provider get() { val cmd = exec { - it.executable = "uname" - it.args = listOf("-m") + if (OperatingSystem.current().isWindows){ + it.executable = "powershell" + it.args = listOf("-NoProfile", "-Command", "(Get-WmiObject Win32_Processor).Architecture") + } else { + it.executable = "uname" + it.args = listOf("-m") + } } return cmd.standardOutput.asText.map { it.trim() } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/Platform.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/Platform.kt index e3aa22812e532..6bee50e36fb61 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/Platform.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/Platform.kt @@ -13,23 +13,33 @@ internal data class Platform( } } +internal enum class OsType(val osName: String) { + WINDOWS("win"), + MAC("darwin"), + LINUX("linux"), + FREEBSD("linux"), // https://github.com/node-gradle/gradle-node-plugin/issues/178 + SUN("sunos"), +} + internal fun parsePlatform(name: String, arch: String, uname: Provider): Platform { + val osType = parseOsType(name) + val osArch = if (osType == OsType.WINDOWS) parseWindowsArch(arch.toLowerCase(), uname) + else parseOsArch(arch.toLowerCase(), uname) + return Platform( - parseOsName(name.toLowerCase()), - parseOsArch( - arch.toLowerCase(), - uname - ) + osType.osName, + osArch ) } -internal fun parseOsName(name: String): String { +internal fun parseOsType(type: String): OsType { + val name = type.toLowerCase() return when { - name.contains("windows") -> "win" - name.contains("mac") -> "darwin" - name.contains("linux") -> "linux" - name.contains("freebsd") -> "linux" - name.contains("sunos") -> "sunos" + name.contains("windows") -> OsType.WINDOWS + name.contains("mac") -> OsType.MAC + name.contains("linux") -> OsType.LINUX + name.contains("freebsd") -> OsType.FREEBSD + name.contains("sunos") -> OsType.SUN else -> error("Unsupported OS: $name") } } @@ -58,6 +68,31 @@ internal fun parseOsArch(arch: String, uname: Provider): String { } } +internal fun parseWindowsArch(arch: String, uname: Provider): String { + return when { + arch.startsWith("aarch") || arch.startsWith("arm") + -> { + val wmiArch = uname.get() + return when (wmiArch) { + /* + * Parse Win32_Processor.Architectures to real processor type + * + * Table from https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info#members + */ + "12" -> "arm64" + "9" -> "x64" + // "6" -> "IA64" + // "5" -> "arm" // 32-bit + "0" -> "x86" + // "0xffff" -> "Unknown" + else -> error("Unexpected Win32_Processor.Architecture: $arch") + } + } + arch.contains("64") -> "x64" + else -> "x86" + } +} + internal fun computeNpmScriptFile( nodeDirProvider: File, command: String,