Skip to content

Instantly share code, notes, and snippets.

@scratchboom
Last active June 1, 2023 16:49
Show Gist options
  • Save scratchboom/250213170dcd20fc05700e87350d2deb to your computer and use it in GitHub Desktop.
Save scratchboom/250213170dcd20fc05700e87350d2deb to your computer and use it in GitHub Desktop.
import org.gradle.internal.os.OperatingSystem
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
// classpath("org.jetbrains.kotlin:kotlin-serialization:1.6.10")
classpath("com.android.tools.build:gradle:7.2.2")
classpath("com.google.gms:google-services:4.3.14")
}
}
plugins {
kotlin("multiplatform")
// id("com.android.library")
}
repositories {
google()
mavenCentral()
}
val BUILDING_FOR_LINUX = true
val BUILDING_FOR_WINDOWS = false
val BUILDING_FOR_MACOS = false
val BUILDING_FOR_IOS = false
kotlin {
jvm()
js {
browser {
runTask {
outputFileName = "KMPTemplate.js"
}
}
}
val targetNative = when (OperatingSystem.current()) {
OperatingSystem.MAC_OS -> macosX64("native")
OperatingSystem.LINUX -> linuxX64("native")
else -> throw Exception("Unsupported Os")
}
val targetLinuxX64 = if (BUILDING_FOR_LINUX) linuxX64 {
binaries {
executable { }
}
} else null
val targetMacosX64 = if(BUILDING_FOR_MACOS)macosX64 {
binaries {
executable { }
}
}else null
if(BUILDING_FOR_MACOS) {
macosArm64()
}
val targetMingwX64 = if (BUILDING_FOR_WINDOWS) mingwX64 {
binaries {
executable {
}
}
} else null
val targetsGlew = listOf(targetMacosX64, targetLinuxX64, targetMingwX64).filterNotNull()
targetsGlew.forEach { target ->
if (!BUILDING_FOR_IOS) {
target.compilations["main"].cinterops {
val glew by creating {
defFile(project.file("src/glewMain/glew.def"))
}
}
}
}
val targetsGlfw = listOf(targetMacosX64, targetLinuxX64).filterNotNull()
targetsGlfw.forEach { target ->
//this check seems to be redundant
if (!BUILDING_FOR_IOS) {
target.compilations["main"].cinterops {
val glfw by creating {
defFile(project.file("src/glfwMain/glfw.def"))
}
}
}
}
val targetsStb = listOf(targetMacosX64, targetLinuxX64, targetMingwX64).filterNotNull()
targetsStb.forEach { target ->
if (!BUILDING_FOR_IOS) {
target.compilations["main"].cinterops {
val stb by creating {
defFile(project.file("src/stbMain/stb.def"))
}
}
}
}
val targetsMiniaudio = listOf(targetMacosX64, targetLinuxX64, targetMingwX64).filterNotNull()
targetsMiniaudio.forEach { target ->
if (!BUILDING_FOR_IOS) {
target.compilations["main"].cinterops {
val miniaudio by creating {
defFile(project.file("src/miniaudioMain/miniaudio.def"))
}
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting {
dependencies {
val lwjglVersion = "3.2.3"
val lwjglNatives = when (OperatingSystem.current()) {
OperatingSystem.LINUX -> System.getProperty("os.arch").let {
if (it.startsWith("arm") || it.startsWith("aarch64"))
"natives-linux-${if (it.contains("64") || it.startsWith("armv8")) "arm64" else "arm32"}"
else
"natives-linux"
}
OperatingSystem.MAC_OS -> if (System.getProperty("os.arch")
.startsWith("aarch64")
) "natives-macos-arm64" else "natives-macos"
OperatingSystem.WINDOWS -> System.getProperty("os.arch").let {
if (it.contains("64"))
"natives-windows${if (it.startsWith("aarch64")) "-arm64" else ""}"
else
"natives-windows-x86"
}
else -> throw Error("Unrecognized or unsupported Operating system. Please set \"lwjglNatives\" manually")
}
implementation("org.lwjgl:lwjgl-bom:$lwjglVersion")
implementation("org.lwjgl:lwjgl:$lwjglVersion")
implementation("org.lwjgl:lwjgl-glfw:$lwjglVersion")
implementation("org.lwjgl:lwjgl-opengl:$lwjglVersion")
runtimeOnly("org.lwjgl:lwjgl:$lwjglVersion:$lwjglNatives")
runtimeOnly("org.lwjgl:lwjgl-glfw:$lwjglVersion:$lwjglNatives")
runtimeOnly("org.lwjgl:lwjgl-opengl:$lwjglVersion:$lwjglNatives")
}
}
val jvmTest by getting
val jsMain by getting
// val androidMain by getting
val linuxMain by creating
val windowsMain by creating
val nativeMain by getting
val glfwMain by creating
val glewMain by creating
val stbMain by creating
val miniaudioMain by creating {
dependsOn(commonMain)
}
val glfwEntryPoint by creating
jvmMain.dependsOn(commonMain)
jsMain.dependsOn(commonMain)
// androidMain.dependsOn(commonMain)
nativeMain.dependsOn(commonMain)
nativeMain.dependsOn(glfwMain)
glewMain.dependsOn(commonMain)
glewMain.dependsOn(nativeMain)
glewMain.dependsOn(stbMain)
glfwMain.dependsOn(commonMain)
glfwEntryPoint.dependsOn(glfwMain)
glfwEntryPoint.dependsOn(glewMain)
glfwEntryPoint.dependsOn(miniaudioMain)
if (BUILDING_FOR_LINUX) {
val linuxX64Main by getting
linuxMain.dependsOn(miniaudioMain)
linuxMain.dependsOn(glewMain)
linuxMain.dependsOn(glfwEntryPoint)
linuxMain.dependsOn(nativeMain)
linuxX64Main.dependsOn(linuxMain)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment