From 4144d06730dac03d35b6b41c38ee2e5f3b93e993 Mon Sep 17 00:00:00 2001 From: Eva Tatarka Date: Tue, 21 Nov 2023 10:02:35 -0800 Subject: [PATCH] fix macos architecture check use /usr/bin/arch to detect what architecture we are actually running on instead of what arch the jre was built for. This fixes the case of using an intel version of the jvm on arm. Fixes #1031 --- .../app/cash/paparazzi/internal/Renderer.kt | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/paparazzi/src/main/java/app/cash/paparazzi/internal/Renderer.kt b/paparazzi/src/main/java/app/cash/paparazzi/internal/Renderer.kt index 554c0a9e8..be0af2f05 100644 --- a/paparazzi/src/main/java/app/cash/paparazzi/internal/Renderer.kt +++ b/paparazzi/src/main/java/app/cash/paparazzi/internal/Renderer.kt @@ -37,6 +37,7 @@ import java.io.File import java.nio.file.Paths import java.util.Locale import kotlin.io.path.name +import kotlin.jvm.optionals.getOrNull /** View rendering. */ internal class Renderer( @@ -188,12 +189,28 @@ internal class Renderer( } private fun getNativeLibDir(): String { - val osName = System.getProperty("os.name").toLowerCase(Locale.US) + val osName = System.getProperty("os.name").lowercase(Locale.US) val osLabel = when { osName.startsWith("windows") -> "win" osName.startsWith("mac") -> { - val osArch = System.getProperty("os.arch").lowercase(Locale.US) - if (osArch.startsWith("x86")) "mac" else "mac-arm" + // System.getProperty("os.arch") returns the os of the jre, not necessarily of the platform we are running on, + // try /usr/bin/arch to get the actual architecture + val osArch = ProcessBuilder("/usr/bin/arch") + .start() + .inputStream + .bufferedReader() + .lines() + .findFirst() + .getOrNull() + ?.lowercase(Locale.US) + + if (osArch != null) { + if (osArch.startsWith("i386")) "mac" else "mac-arm" + } else { + // fallback to jre arch and cross fingers it's correct + val jreArch = System.getProperty("os.arch").lowercase(Locale.US) + if (jreArch.startsWith("x86")) "mac" else "mac-arm" + } } else -> "linux" }