Repository: lavalink-devs/Lavalink
Pull request #1003: Add support for Kotlin/Native in protocol
Author: login: DRSchlaubi
Labels: name: enhancement
Created: 2024-01-26T15:59:01Z | Merged: 2025-04-03T13:59:07Z

In preparation of Kord & Lavalink.kt adding support for Kotlin/Native this PR updates the protocol module accordingly

Lavalink.kt & Kord are currently waiting for ktor 3.0 (or at least a beta) to use this, so priority on this isn't high as of rn

**Breaking change** This PR un-ignores the `gradle.properties` file, if you use this file for any private data, please use `~/.gradle/gradle.properties` instead


Diff:
diff --git a/.gitignore b/.gitignore
index 17558ae6d..6d933a8f6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,7 +17,6 @@ build/*
 /plugin-api/out/
 /protocol/build/
 /protocol/out/
-gradle.properties
 application.yml
 LavalinkServer/plugins
 .cache/
diff --git a/build.gradle.kts b/build.gradle.kts
index 9ecfe5d8a..5e54b68aa 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -11,9 +11,9 @@ plugins {
     id("org.springframework.boot") version "3.1.0" apply false
     id("org.sonarqube") version "4.2.0.3129"
     id("com.adarshr.test-logger") version "3.2.0"
-    id("org.jetbrains.kotlin.jvm") version "2.0.0"
-    id("org.jetbrains.kotlin.plugin.allopen") version "2.0.0"
-    id("org.jetbrains.kotlin.plugin.serialization") version "2.0.0" apply false
+    id("org.jetbrains.kotlin.jvm") version "2.1.20"
+    id("org.jetbrains.kotlin.plugin.allopen") version "2.1.20"
+    id("org.jetbrains.kotlin.plugin.serialization") version "2.1.20" apply false
     alias(libs.plugins.maven.publish.base) apply false
 }
 
@@ -27,7 +27,6 @@ allprojects {
         maven("https://m2.dv8tion.net/releases")
         maven("https://maven.lavalink.dev/releases")
         maven("https://maven.lavalink.dev/snapshots")
-        jcenter()
         maven("https://jitpack.io") // build projects directly from GitHub
     }
 }
diff --git a/gradle.properties b/gradle.properties
new file mode 100644
index 000000000..d09bcdc44
--- /dev/null
+++ b/gradle.properties
@@ -0,0 +1,2 @@
+kotlin.native.ignoreDisabledTargets=true
+kotlin.native.enableKlibsCrossCompilation=true
diff --git a/protocol/build.gradle.kts b/protocol/build.gradle.kts
index 74e2758ef..b2e17befb 100644
--- a/protocol/build.gradle.kts
+++ b/protocol/build.gradle.kts
@@ -1,7 +1,7 @@
 import com.vanniktech.maven.publish.JavadocJar
 import com.vanniktech.maven.publish.KotlinMultiplatform
-import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
-import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
+import org.jetbrains.kotlin.gradle.dsl.JvmTarget
+import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsPlugin.Companion.kotlinNodeJsEnvSpec
 import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
 
 plugins {
@@ -15,11 +15,11 @@ val archivesBaseName = "protocol"
 group = "dev.arbjerg.lavalink"
 
 kotlin {
+    applyDefaultHierarchyTemplate()
+
     jvm {
-        compilations.all {
-            kotlinOptions {
-                jvmTarget = "17"
-            }
+        compilerOptions {
+            jvmTarget = JvmTarget.JVM_17
         }
     }
 
@@ -38,6 +38,30 @@ kotlin {
         }
     }
 
+    linuxX64()
+    linuxArm64()
+
+    mingwX64()
+
+    macosArm64()
+    macosX64()
+
+    iosArm64()
+    iosSimulatorArm64()
+    iosSimulatorArm64()
+
+    watchosArm64()
+    watchosSimulatorArm64()
+    watchosX64()
+
+    tvosArm64()
+    tvosSimulatorArm64()
+    tvosX64()
+
+    compilerOptions {
+        freeCompilerArgs.add("-Xconsistent-data-class-copy-visibility")
+    }
+
     sourceSets {
         all {
             languageSettings.optIn("kotlinx.serialization.ExperimentalSerializationApi")
@@ -87,7 +111,7 @@ tasks {
 
 // Use system Node.Js on NixOS
 if (System.getenv("NIX_PROFILES") != null) {
-    rootProject.plugins.withType<NodeJsRootPlugin> {
-        rootProject.the<NodeJsRootExtension>().download = false
+    kotlinNodeJsEnvSpec.apply {
+        download = false
     }
 }
diff --git a/settings.gradle.kts b/settings.gradle.kts
index ded369bb6..10b7e9f53 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -65,7 +65,7 @@ fun VersionCatalogBuilder.metrics() {
 }
 
 fun VersionCatalogBuilder.common() {
-    version("kotlin", "2.0.0")
+    version("kotlin", "2.1.20")
 
     library("kotlin-reflect", "org.jetbrains.kotlin", "kotlin-reflect").versionRef("kotlin")
     library("kotlin-stdlib-jdk8", "org.jetbrains.kotlin", "kotlin-stdlib-jdk8").versionRef("kotlin")


Reviews:
user: login: topi314
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-04-02T07:59:23Z
user: login: DRSchlaubi
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-02T10:45:31Z
user: login: DRSchlaubi
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-02T10:47:14Z
user: login: topi314
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-02T10:55:42Z
user: login: topi314
state: APPROVED
author_association: MEMBER
submitted_at: 2025-04-03T13:58:58Z

Review comments:
diff_hunk: @@ -1,6 +1,6 @@
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
path: gradle/wrapper/gradle-wrapper.properties
user: login: topi314
body: is this gradle downgrade needed?
created_at: 2025-04-02T07:58:00Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,2 @@
+kotlin.native.enableKlibsCrossCompilation=true
path: gradle.properties
user: login: topi314
body: can we put this somewhere else?
I'd rather not have the `gradle.properties` committed
created_at: 2025-04-02T07:58:42Z
author_association: MEMBER
diff_hunk: @@ -1,6 +1,6 @@
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
path: gradle/wrapper/gradle-wrapper.properties
user: login: DRSchlaubi
body: I assume this happens when merging in master as master has updated the version since
created_at: 2025-04-02T10:45:31Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,2 @@
+kotlin.native.enableKlibsCrossCompilation=true
path: gradle.properties
user: login: DRSchlaubi
body: The gradle.properties is supposed to be committed, ignoring it was wrong in the first place
created_at: 2025-04-02T10:47:14Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,2 @@
+kotlin.native.enableKlibsCrossCompilation=true
path: gradle.properties
user: login: topi314
body: Apparently user specific configuration should go into `~/.gradle/gradle.properties`.
Can you add that into the pr description?
created_at: 2025-04-02T10:55:42Z
author_association: MEMBER

PR comments:
user: login: DRSchlaubi
created_at: 2025-04-02T00:31:41Z
body: Ktor 3.1 released the changed required, would appriciate if we could release this at some point in the near future
author_association: CONTRIBUTOR
user: login: topi314
created_at: 2025-04-02T00:36:29Z
body: So, does this still require a mac runner?
author_association: MEMBER
user: login: DRSchlaubi
created_at: 2025-04-02T00:39:31Z
body: > So, does this still require a mac runner?

Not as of Kotlin 2.1.0: https://kotlinlang.org/docs/whatsnew21.html#how-to-enable-publishing-libraries-from-any-host
author_association: CONTRIBUTOR
user: login: DRSchlaubi
created_at: 2025-04-02T00:43:21Z
body: Also macos runners are free for public projects 
author_association: CONTRIBUTOR
user: login: topi314
created_at: 2025-04-02T20:04:26Z
body: this needs to be rebased, there are a lot of unrelated changes in here
author_association: MEMBER