Custom Clang version with Kotlin/Native

Hi, I’m trying to link a Rust MinGW static library to a Kotlin/Native MinGW library on macOS. I’m using Rust 1.75 with Kotlin 1.9.21.
I encounter a linker error like the following:

e: /Users/paxbun/.konan/dependencies/apple-llvm-20200714-macos-aarch64-essentials/bin/clang++ invocation reported errors

The /Users/paxbun/.konan/dependencies/apple-llvm-20200714-macos-aarch64-essentials/bin/clang++ command returned non-zero exit code: 1.
output:
lld-link: warning: ignoring unknown argument: -exclude-symbols:___chkstk_ms
lld-link: error: -exclude-symbols:___chkstk_ms is not allowed in .drectve
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

FAILURE: Build failed with an exception.

and it seems that the flag was added in Clang 16 and then ported to Clang 15.

I want to use K/N with Clang 16, which is installed on my machine. Is there any way to use a user-specified version of Clang when using K/N?

1 Like

I found this docs in GH: kotlin/kotlin-native/HACKING.md at 7a13023b1463b651904440e3ffbedf1bfa379c9b · JetBrains/kotlin · GitHub

I’ll leave the results here after some experiments.

mingwX64().binaries.executable {
    compilerOptions {
        freeCompilerArgs.add("-Xoverride-konan-properties=linker.macos_arm64-mingw_x64=/opt/homebrew/opt/llvm/bin/ld.lld")
    }
}

Overriding the Konan linker.<host>-<target> option did work. Issue resolved! :smile:

BTW, building my own version of LLVM did not work, as Gradle crashes while running linkDebugExecutableMingwX64. It seems linkDebugExecutableMingwX64 runs the compiler within the Gradle process (so it calls a shared library, I assume), and the crash occurs inside the shared library, which is why Gradle just terminates without any Java exception. Maybe using a different LLVM version entirely is incompatible with the Kotlin Gradle plugin.