Skip to content

Commit

Permalink
macOS on M1
Browse files Browse the repository at this point in the history
Tested on:
macOS Monterey
Version 12.4
Chip Apple M1
  • Loading branch information
santoszv authored and karlsabo committed Apr 11, 2024
1 parent 03b7319 commit 2f257ac
Show file tree
Hide file tree
Showing 40 changed files with 25,371 additions and 6 deletions.
23 changes: 22 additions & 1 deletion DrOpt/drOpt.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,28 @@ library {

publicHeaders.from(file("dropt/include"))

targetMachines.set(listOf(machines.windows.x86_64, machines.linux.x86_64, machines.macOS.x86_64))
targetMachines.set(listOf(machines.windows.x86_64, machines.linux.x86_64, machines.macOS.x86_64, machines.macOS.architecture("aarch64")))

toolChains.configureEach {
if (this is Clang) {
target("host:x86-64") {
cppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
getcCompiler().withArguments { add("--target=x86_64-apple-darwin") }
objcCompiler.withArguments { add("--target=x86_64-apple-darwin") }
objcppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
linker.withArguments { add("--target=x86_64-apple-darwin") }
assembler.withArguments { add("--target=x86_64-apple-darwin") }
}
target("host:aarch64") {
cppCompiler.withArguments { add("--target=arm64-apple-darwin") }
getcCompiler().withArguments { add("--target=arm64-apple-darwin") }
objcCompiler.withArguments { add("--target=arm64-apple-darwin") }
objcppCompiler.withArguments { add("--target=arm64-apple-darwin") }
linker.withArguments { add("--target=arm64-apple-darwin") }
assembler.withArguments { add("--target=arm64-apple-darwin") }
}
}
}

binaries.configureEach(CppStaticLibrary::class.java) {
val binaryToolChain = toolChain
Expand Down
67 changes: 63 additions & 4 deletions PackrLauncher/packrLauncher.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ tasks.withType(CppCompile::class).configureEach {

dependencies {
implementation(project(":DrOpt"))
testImplementation("org.gradle.cpp-samples:googletest:1.9.0-gr4-SNAPSHOT")
//testImplementation("org.gradle.cpp-samples:googletest:1.9.0-gr4-SNAPSHOT")
}

/**
Expand All @@ -95,11 +95,32 @@ val operatingSystemFamilyByPublicationName: MutableMap<String, OperatingSystemFa
* MacOS x86 is no longer built because it requires and older version of Xcode and Apple makes it too difficult to install on newer versions of Mac
* Windows x86 is no longer built because the Adopt OpenJDK has crash failures.
*/
val targetPlatformsToBuild = listOf(machines.windows.x86_64, machines.linux.x86_64, machines.macOS.x86_64)
val targetPlatformsToBuild = listOf(machines.windows.x86_64, machines.linux.x86_64, machines.macOS.x86_64, machines.macOS.architecture("aarch64"))

application {
targetMachines.set(targetPlatformsToBuild)

toolChains.configureEach {
if (this is Clang) {
target("host:x86-64") {
cppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
getcCompiler().withArguments { add("--target=x86_64-apple-darwin") }
objcCompiler.withArguments { add("--target=x86_64-apple-darwin") }
objcppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
linker.withArguments { add("--target=x86_64-apple-darwin") }
assembler.withArguments { add("--target=x86_64-apple-darwin") }
}
target("host:aarch64") {
cppCompiler.withArguments { add("--target=arm64-apple-darwin") }
getcCompiler().withArguments { add("--target=arm64-apple-darwin") }
objcCompiler.withArguments { add("--target=arm64-apple-darwin") }
objcppCompiler.withArguments { add("--target=arm64-apple-darwin") }
linker.withArguments { add("--target=arm64-apple-darwin") }
assembler.withArguments { add("--target=arm64-apple-darwin") }
}
}
}

binaries.configureEach(CppExecutable::class.java) {
logger.debug("Configuring executable ${this.name}")

Expand Down Expand Up @@ -161,6 +182,7 @@ application {
when (targetMachine.architecture.name) {
MachineArchitecture.X86 -> args("i386")
MachineArchitecture.X86_64 -> args("x86_64")
"aarch64" -> args("arm64")
else -> throw GradleException("Don't know the lipo -arch flag for architecture ${targetMachine.architecture.name}")
}
args(executableFile.get().asFile.absolutePath)
Expand Down Expand Up @@ -247,10 +269,34 @@ application {
unitTest {
targetMachines.set(targetPlatformsToBuild)

toolChains.configureEach {
if (this is Clang) {
target("host:x86-64") {
cppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
getcCompiler().withArguments { add("--target=x86_64-apple-darwin") }
objcCompiler.withArguments { add("--target=x86_64-apple-darwin") }
objcppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
linker.withArguments { add("--target=x86_64-apple-darwin") }
assembler.withArguments { add("--target=x86_64-apple-darwin") }
}
target("host:aarch64") {
cppCompiler.withArguments { add("--target=arm64-apple-darwin") }
getcCompiler().withArguments { add("--target=arm64-apple-darwin") }
objcCompiler.withArguments { add("--target=arm64-apple-darwin") }
objcppCompiler.withArguments { add("--target=arm64-apple-darwin") }
linker.withArguments { add("--target=arm64-apple-darwin") }
assembler.withArguments { add("--target=arm64-apple-darwin") }
}
}
}

binaries.configureEach(CppTestExecutable::class.java) {
val binaryCompileTask = compileTask.get()
val binaryLinkTask = linkTask.get()
when (toolChain) {
is Gcc -> {
binaryCompileTask.compilerArgs.add("-std=c++14")

if (targetMachine.operatingSystemFamily.isLinux) {
binaryLinkTask.linkerArgs.add("-lpthread")
}
Expand All @@ -259,11 +305,16 @@ unitTest {
binaryLinkTask.linkerArgs.add("-fno-pie")
}
is Clang -> {
binaryCompileTask.compilerArgs.add("-std=c++14")

binaryLinkTask.linkerArgs.add("-ldl")
}
is VisualCpp -> {
compileTask.get().macros["UNICODE"] = null
compileTask.get().macros["_UNICODE"] = null
binaryCompileTask.macros["UNICODE"] = null
binaryCompileTask.macros["_UNICODE"] = null

binaryCompileTask.compilerArgs.add("/std:c++14")

binaryLinkTask.linkerArgs.add("/SUBSYSTEM:CONSOLE")
binaryLinkTask.linkerArgs.add("Shell32.lib")
}
Expand All @@ -275,6 +326,8 @@ unitTest {
}

addJvmHeaders(compileTask.get(), this)

addGoogleTest(compileTask.get())
}
}

Expand Down Expand Up @@ -398,3 +451,9 @@ fun addJvmHeaders(binaryCompileTask: CppCompile, cppBinary: CppBinary) {
}
}
}

fun addGoogleTest(binaryCompileTask: CppCompile) {
binaryCompileTask.includes(file("${rootProject.projectDir}/googletest"))
binaryCompileTask.includes(file("${rootProject.projectDir}/googletest/include"))
binaryCompileTask.source(file("${rootProject.projectDir}/googletest/src/gtest-all.cc"))
}
45 changes: 45 additions & 0 deletions TestAppJreDist/testAppJreDist.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,30 @@ data class JvmRemoteArchiveInformation(
"0a5419a45fe3680610ff15afa7d854c9b79579550327d14d616ea8ccd0e89505"
),

// macOs aarch64 Java 11
JvmRemoteArchiveInformation(
uri("https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.15%2B10/OpenJDK11U-jdk_aarch64_mac_hotspot_11.0.15_10.tar.gz").toURL(),
"e84143a6c633a26aeefcb1fd5ad8dfb9e952cfec2a1af5c9d9b69f2390990dac"
),

// macOs aarch64 Java 11 JRE
JvmRemoteArchiveInformation(
uri("https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.15%2B10/OpenJDK11U-jre_aarch64_mac_hotspot_11.0.15_10.tar.gz").toURL(),
"6736f6ef503658f3ae48a54646a8ac6dc8ca286f10887dae22b41f2148882695"
),

// macOs x86-64 Java 17
JvmRemoteArchiveInformation(
uri("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.3%2B7/OpenJDK17U-jdk_x64_mac_hotspot_17.0.3_7.tar.gz").toURL(),
"a5db5927760d2864316354d98ff18d18bec2e72bfac59cd25a416ed67fa84594"
),

// macOs aarch64 Java 17
JvmRemoteArchiveInformation(
uri("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.3%2B7/OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.3_7.tar.gz").toURL(),
"ff42be4d7a348d0d7aee07749e4daec9f427dcc7eb46b343f8131e8f3906c05b"
),

// Windows x86-64 Java 8
JvmRemoteArchiveInformation(
uri("https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u332-b09/OpenJDK8U-jdk_x64_windows_hotspot_8u332b09.zip").toURL(),
Expand Down Expand Up @@ -233,6 +251,19 @@ fun getJdkOsFamily(jdkFilename: String): String {
}
}

fun getJdkOsArch(jdkFilename: String): String {
val jdkFilenameLowercase = jdkFilename.toLowerCase()
return when {
jdkFilenameLowercase.contains("x64") -> {
"x86-64"
}
jdkFilenameLowercase.contains("aarch64") -> {
"aarch64"
}
else -> throw GradleException("Unknown Java Arch for JDK $jdkFilename")
}
}

/*
* List of JDKs to download
* task - Download JDKs from the internet
Expand All @@ -258,6 +289,20 @@ jvmRemoteArchiveInformationList.forEach { (jvmArchiveUrl, jvmArchiveSha256) ->
return@forEach
}

// This code is ugly, but works
if (Os.isFamily(Os.FAMILY_MAC)) {
val jdkOsArch = getJdkOsArch(jvmDownloadFilename)
if (Os.isArch("aarch64")) {
if (jdkOsArch != "aarch64") {
return@forEach
}
} else {
if (jdkOsArch == "aarch64") {
return@forEach
}
}
}

val downloadTask = tasks.register("download${jvmDownloadFilename.substring(0, jvmDownloadFilename.indexOf('.'))}") {
inputs.property(jvmArchiveUrl.toString(), jvmArchiveSha256)

Expand Down
Loading

0 comments on commit 2f257ac

Please sign in to comment.