Problem with gradle building kotlin/native

Hi,

I have problems compiling kotlin/native with gradle, it seems it cannot find three libraries. I get the error

exception: java.io.IOException: Cannot run program "/home/simon/.konan/dependencies/clang-llvm-6.0.1-linux-x86-64/bin/llvm-lto": error=2, No such file or directory"

Which I have deduced is reported because the dynamic linker cannot find the libraries. I have tried patchelf with:

patchelf --set-rpath /home/simon/.konan/ clang-llvm-6.0.1-linux-x86-64/bin/llvm-lto

But that did not do any difference (maybe I am using it wrong?). I would really appreciate some help with this issue.

My shell.nix used:

~/C/W/L/dbugged
$> cat shell.nix
with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "gradle";
  buildInputs = [
    gradle
    kotlin
    zlib
    ncurses
    gcc
    patchelf
  ];
}

And this is the binary it cannot find the libraries for:

~/C/W/L/dbugged
$> ldd /home/simon/.konan/dependencies/clang-llvm-6.0.1-linux-x86-64/bin/llvm-lto
        linux-vdso.so.1 (0x00007ffc707b6000)
        libpthread.so.0 => /nix/store/6m2k8kx8h216jlx9dg3lp4m90bz05yck-glibc-2.30/lib/libpthread.so.0 (0x00007f0a08c8f000)
        libz.so.1 => not found
        librt.so.1 => /nix/store/6m2k8kx8h216jlx9dg3lp4m90bz05yck-glibc-2.30/lib/librt.so.1 (0x00007f0a08c85000)
        libdl.so.2 => /nix/store/6m2k8kx8h216jlx9dg3lp4m90bz05yck-glibc-2.30/lib/libdl.so.2 (0x00007f0a08c80000)
        libtinfo.so.5 => not found
        libm.so.6 => /nix/store/6m2k8kx8h216jlx9dg3lp4m90bz05yck-glibc-2.30/lib/libm.so.6 (0x00007f0a08b40000)
        libstdc++.so.6 => not found
        libgcc_s.so.1 => /nix/store/6m2k8kx8h216jlx9dg3lp4m90bz05yck-glibc-2.30/lib/libgcc_s.so.1 (0x00007f0a08b24000)
        libc.so.6 => /nix/store/6m2k8kx8h216jlx9dg3lp4m90bz05yck-glibc-2.30/lib/libc.so.6 (0x00007f0a08965000)
        /lib64/ld-linux-x86-64.so.2 => /nix/store/6m2k8kx8h216jlx9dg3lp4m90bz05yck-glibc-2.30/lib64/ld-linux-x86-64.so.2 (0x00007f0a08cb2000)

As you can see; libz, litinfo and libstdc++ are not found, even though they very much exists, libz for example:

~/C/W/L/dbugged
$> sudo find / -iname "libz.so.1"
[sudo] password for simon: 
/home/simon/.local/share/Steam/steamapps/common/SteamLinuxRuntime/pressure-vessel/lib/x86_64-linux-gnu/libz.so.1
/home/simon/.local/share/Steam/steamapps/common/SteamLinuxRuntime/pressure-vessel/lib/i386-linux-gnu/libz.so.1
/home/simon/.konan/dependencies/target-gcc-toolchain-3-linux-x86-64/x86_64-unknown-linux-gnu/sysroot/usr/lib/libz.so.1
/nix/store/rz1v42v5l0vb1syr8z3m9m3ckzl2bv6g-steam-fhs/usr/lib64/libz.so.1
/nix/store/rz1v42v5l0vb1syr8z3m9m3ckzl2bv6g-steam-fhs/usr/lib64/steam-runtime/lib/x86_64-linux-gnu/libz.so.1
/nix/store/rz1v42v5l0vb1syr8z3m9m3ckzl2bv6g-steam-fhs/usr/lib32/libz.so.1
/nix/store/rz1v42v5l0vb1syr8z3m9m3ckzl2bv6g-steam-fhs/usr/lib32/steam-runtime/lib/i386-linux-gnu/libz.so.1
/nix/store/z5pb00r4w53dxhi3sqrz6hd6xay66xyb-zlib-1.2.11/lib/libz.so.1
/nix/store/xh4fzp73zjz9cj3wpm4lk8iqrznk7cvf-steam-fhs/usr/lib64/steam-runtime/lib/x86_64-linux-gnu/libz.so.1
/nix/store/xh4fzp73zjz9cj3wpm4lk8iqrznk7cvf-steam-fhs/usr/lib32/steam-runtime/lib/i386-linux-gnu/libz.so.1
/nix/store/m718gk3pajbr4p7hxxvf014mljsjgw6i-steam-runtime-wrapped/lib/steam-runtime/lib/x86_64-linux-gnu/libz.so.1
/nix/store/kf2sh7c2fmizvycqq02aad1rfp6xswgv-zlib-1.2.11/lib/libz.so.1
/nix/store/15mdd6z0v69v9l3m81pbi3pk8k369knc-steam-runtime-2016-08-13/lib/x86_64-linux-gnu/libz.so.1
/nix/store/iiymx8j7nlar3gc23lfkcscvr61fng8s-zlib-1.2.11/lib/libz.so.1
/nix/store/xvxsbvbi7ckccz4pz2j6np7czadgjy2x-zlib-1.2.11/lib/libz.so.1
/nix/store/qvgf4g9c5h3wzq4xjy566vc0pqgc9fjq-zlib-1.2.11/lib/libz.so.1
/nix/store/q6jyp9ixxrkq2mimhx16arfklwxm5kb2-steam-runtime-wrapped/lib/steam-runtime/lib/i386-linux-gnu/libz.so.1
/nix/store/8c30dl4mmgm4rip1l08fvad9gca5sk2j-zlib-1.2.11/lib/libz.so.1
/nix/store/b67fnkx3njvmknkrzki5260jbxbjcmmb-steam-runtime-wrapped/lib/steam-runtime/lib/x86_64-linux-gnu/libz.so.1
/nix/store/qqwpnxb9v0q254xh43pykpb8lsnq5yxi-steam-runtime-2016-08-13/lib/i386-linux-gnu/libz.so.1
/nix/store/5b3rwgl10k4cm2fjs8kyrc7033x19m5m-zlib-1.2.11/lib/libz.so.1
/nix/store/jjz971jv9iq3mk2lwdvhlibvwxr50jm9-steam-runtime-2016-08-13/lib/x86_64-linux-gnu/libz.so.1
/nix/store/n5pwyn9zwdmx94hibh80rb43kd4dwjv1-steam-usr-target/lib/libz.so.1
/nix/store/4kdifbq3j7bl540242f0zylyjk64n6lj-steam-usr-multi/lib/libz.so.1
/nix/store/70684wvnaaij9j12q4sypa28fz8cpvs4-steam-runtime-2016-08-13/lib/i386-linux-gnu/libz.so.1
/nix/store/sz7v4j7wq28sb0qrk181lvm5lsvs0a87-system-path/lib/libz.so.1
/nix/store/ipdqy17mjyy797rjvw3390nvkx5lkc68-steam-runtime-wrapped/lib/steam-runtime/lib/i386-linux-gnu/libz.so.1
/nix/store/84jxhr8l3plkd6z2x4v941za9kvmv88g-zlib-1.2.11/lib/libz.so.1

It even exists in the ~/.konan path that gradle has pulled all dependencies into. Even though it does not seem to search that path at all since the other dependencies are taken from /nix/store/?

The build.gradle.kts (taken from the example at https://kotlinlang.org/docs/tutorials/native/using-gradle.html):

~/C/W/L/dbugged
$> cat build.gradle.kts 
plugins {
    kotlin("multiplatform") version "1.3.21"
}

repositories {
    mavenCentral()
}

kotlin {
  linuxX64("native") {
    binaries {
      executable()
    }
  }
}

tasks.withType<Wrapper> {
  gradleVersion = "5.6.4"
  distributionType = Wrapper.DistributionType.ALL
}

And the build output:

[nix-shell:~/Code/Work/Linetic/dbugged]$ gradle nativeBinaries

> Configure project :
Kotlin Multiplatform Projects are an experimental feature.

> Task :compileKotlinNative UP-TO-DATE

> Task :linkDebugExecutableNative
error: compilation failed: Cannot run program "/home/simon/.konan/dependencies/clang-llvm-6.0.1-linux-x86-64/bin/llvm-lto": error=2, No such file or directory

 * Source files: dbugged.kt
 * Compiler version info: Konan: 1.1.2 / Kotlin: 1.3.21
 * Output kind: PROGRAM

exception: java.io.IOException: Cannot run program "/home/simon/.konan/dependencies/clang-llvm-6.0.1-linux-x86-64/bin/llvm-lto": error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at org.jetbrains.kotlin.konan.exec.Command.runProcess(ExecuteCommand.kt:57)
        at org.jetbrains.kotlin.konan.exec.Command.execute(ExecuteCommand.kt:65)
        at org.jetbrains.kotlin.backend.konan.LinkStage.runTool(LinkStage.kt:44)
        at org.jetbrains.kotlin.backend.konan.LinkStage.runTool(LinkStage.kt:40)
        at org.jetbrains.kotlin.backend.konan.LinkStage.llvmLto(LinkStage.kt:60)
        at org.jetbrains.kotlin.backend.konan.LinkStage.access$llvmLto(LinkStage.kt:18)
        at org.jetbrains.kotlin.backend.konan.LinkStage$linkStage$1.invoke(LinkStage.kt:221)
        at org.jetbrains.kotlin.backend.konan.LinkStage$linkStage$1.invoke(LinkStage.kt:18)
        at org.jetbrains.kotlin.backend.konan.PhaseManager$phase$$inlined$with$lambda$1.invoke(KonanPhases.kt:137)
        at org.jetbrains.kotlin.backend.konan.PhaseManager$phase$$inlined$with$lambda$1.invoke(KonanPhases.kt:111)
        at org.jetbrains.kotlin.konan.util.UtilKt.profileIf(Util.kt:37)
        at org.jetbrains.kotlin.backend.konan.PhaseManager.phase(KonanPhases.kt:136)
        at org.jetbrains.kotlin.backend.konan.LinkStage.linkStage(LinkStage.kt:213)
        at org.jetbrains.kotlin.backend.konan.KonanDriverKt$runTopLevelPhases$7.invoke(KonanDriver.kt:107)
        at org.jetbrains.kotlin.backend.konan.KonanDriverKt$runTopLevelPhases$7.invoke(KonanDriver.kt)
        at org.jetbrains.kotlin.backend.konan.PhaseManager$phase$$inlined$with$lambda$1.invoke(KonanPhases.kt:137)
        at org.jetbrains.kotlin.backend.konan.PhaseManager$phase$$inlined$with$lambda$1.invoke(KonanPhases.kt:111)
        at org.jetbrains.kotlin.konan.util.UtilKt.profileIf(Util.kt:37)
        at org.jetbrains.kotlin.backend.konan.PhaseManager.phase(KonanPhases.kt:136)
        at org.jetbrains.kotlin.backend.konan.KonanDriverKt.runTopLevelPhases(KonanDriver.kt:106)
        at org.jetbrains.kotlin.cli.bc.K2Native.doExecute(K2Native.kt:79)
        at org.jetbrains.kotlin.cli.bc.K2Native.doExecute(K2Native.kt:35)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:96)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:52)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:93)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:71)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:39)
        at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMainNoExit(CLITool.kt:204)
        at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMain(CLITool.kt:196)
        at org.jetbrains.kotlin.cli.bc.K2Native$Companion$main$1.invoke(K2Native.kt:217)
        at org.jetbrains.kotlin.cli.bc.K2Native$Companion$main$1.invoke(K2Native.kt:208)
        at org.jetbrains.kotlin.konan.util.UtilKt.profileIf(Util.kt:37)
        at org.jetbrains.kotlin.konan.util.UtilKt.profile(Util.kt:31)
        at org.jetbrains.kotlin.cli.bc.K2Native$Companion.main(K2Native.kt:210)
        at org.jetbrains.kotlin.cli.bc.K2NativeKt.main(K2Native.kt:279)
        at org.jetbrains.kotlin.cli.utilities.MainKt.main(main.kt:16)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
        at java.lang.ProcessImpl.start(ProcessImpl.java:134)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
        ... 36 more


> Task :linkDebugExecutableNative FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':linkDebugExecutableNative'.
> Process 'command '/nix/store/9iwwc03g2gcbixm2rfmd52lf0890jj7l-openjdk-8u222-ga/lib/openjdk/bin/java'' finished with non-zero exit value 2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s
2 actionable tasks: 1 executed, 1 up-to-date

My nixos version is:

~/C/W/L/dbugged
$> nixos-version
20.03.1896.025deb80b24 (Markhor)

And my configuration can be found here: https://bitbucket.org/simonkampe/nixos-config/src/master/

I’ll answer my own question for when some other person has the same issue, this shell.nix works:

{ pkgs ? import<nixpkgs> {} }:
let fhs = pkgs.buildFHSUserEnv {
  name = "gradle-env";
  targetPkgs = pkgs : (with pkgs;
  [
    gradle
    kotlin
    jdk
    zlib
    ncurses
  ]);
};
in pkgs.stdenv.mkDerivation {
  name = "gradle-env-shell";
  nativeBuildInputs = [ fhs ];
  shellHook="exec gradle-env";
}

Notice the shellHook, that is required to launch the chroot…