Skip to content

Instantly share code, notes, and snippets.

@jprinet
Last active August 4, 2022 11:00
Show Gist options
  • Save jprinet/5c4581a7a0070a0df975b877989e8ab1 to your computer and use it in GitHub Desktop.
Save jprinet/5c4581a7a0070a0df975b877989e8ab1 to your computer and use it in GitHub Desktop.
CC compatible version of gradle-processor-arch.gradle
import java.nio.charset.Charset
import java.util.concurrent.TimeUnit
/**
* This Gradle script captures the processor architecture
* and adds these as a custom value.
* This should be applied to the root project:
* <code> apply from: file('gradle-processor-arch.gradle') </code>
*/
def buildScanApi = project.extensions.findByName('buildScan')
if (!buildScanApi) {
return
}
buildScan.background { api ->
Capture.captureProcessorArch(api)
}
class Capture {
static void captureProcessorArch(def api) {
def osName = System.getProperty("os.name")
api.value("os.name", osName)
def osArch = System.getProperty("os.arch")
api.value("os.arch", osArch)
if (isDarwin(osName)) {
if (isTranslatedByRosetta()) {
api.tag("M1-translated")
} else if (isM1()) {
api.tag("M1")
}
}
}
static boolean isDarwin(String osName) {
return osName.contains("OS X") || osName.startsWith("Darwin")
}
static boolean isM1() {
return execAndGetStdout("uname", "-p") == "arm"
}
// On Apple silicon, a universal binary may run either natively or as a translated binary
// https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment#Determine-Whether-Your-App-Is-Running-as-a-Translated-Binary
static boolean isTranslatedByRosetta() {
return execAndGetStdout("sysctl", "sysctl.proc_translated") == "sysctl.proc_translated: 1"
}
static String execAndGetStdout(String... args) {
Process process = args.toList().execute()
try {
def standardText = process.inputStream.withStream { s -> s.getText(Charset.defaultCharset().name()) }
def ignore = process.errorStream.withStream { s -> s.getText(Charset.defaultCharset().name()) }
def finished = process.waitFor(10, TimeUnit.SECONDS)
finished && process.exitValue() == 0 ? trimAtEnd(standardText) : null
} finally {
process.destroyForcibly()
}
}
static String trimAtEnd(String str) {
('x' + str).trim().substring(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment