Skip to content

Commit

Permalink
KT-72027: fix kotlin-gradle-plugin tries to execute uname on Windows …
Browse files Browse the repository at this point in the history
…on ARM
  • Loading branch information
Kai-Z-JP authored and qodana-bot committed Dec 9, 2024
1 parent a416d77 commit aabb6b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>): 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")
}
}
Expand Down Expand Up @@ -58,6 +68,31 @@ internal fun parseOsArch(arch: String, uname: Provider<String>): String {
}
}

internal fun parseWindowsArch(arch: String, uname: Provider<String>): 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,
Expand Down

0 comments on commit aabb6b5

Please sign in to comment.