From bc1bc1aa3396c87fe6a78d56c1bf7534c71836c4 Mon Sep 17 00:00:00 2001 From: Pavel Shirshov Date: Sat, 26 Oct 2024 21:09:39 +0100 Subject: [PATCH] graalvm-ce: fix native-image standalone usage by wrapping Nix env variables Closes #350909. --- .../community-edition/buildGraalvm.nix | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/pkgs/development/compilers/graalvm/community-edition/buildGraalvm.nix b/pkgs/development/compilers/graalvm/community-edition/buildGraalvm.nix index d360da5dfa6c..f5b18d196ff5 100644 --- a/pkgs/development/compilers/graalvm/community-edition/buildGraalvm.nix +++ b/pkgs/development/compilers/graalvm/community-edition/buildGraalvm.nix @@ -120,27 +120,44 @@ let xorg.libXtst ]; - postInstall = '' - # jni.h expects jni_md.h to be in the header search path. - ln -sf $out/include/linux/*_md.h $out/include/ + postInstall = + let + cLibsAsFlags = (map (l: "--add-flags '-H:CLibraryPath=${l}/lib'") cLibs); + preservedNixVariables = [ + "-ELOCALE_ARCHIVE" + "-ENIX_BINTOOLS" + "-ENIX_BINTOOLS_WRAPPER_TARGET_HOST_${stdenv.cc.suffixSalt}" + "-ENIX_BUILD_CORES" + "-ENIX_BUILD_TOP" + "-ENIX_CC" + "-ENIX_CC_WRAPPER_TARGET_HOST_${stdenv.cc.suffixSalt}" + "-ENIX_CFLAGS_COMPILE" + "-ENIX_HARDENING_ENABLE" + "-ENIX_LDFLAGS" + ]; + preservedNixVariablesAsFlags = (map (f: "--add-flags '${f}'") preservedNixVariables); + in + '' + # jni.h expects jni_md.h to be in the header search path. + ln -sf $out/include/linux/*_md.h $out/include/ - mkdir -p $out/share - # move files in $out like LICENSE.txt - find $out/ -maxdepth 1 -type f -exec mv {} $out/share \; - # symbolic link to $out/lib/svm/LICENSE_NATIVEIMAGE.txt - rm -f $out/LICENSE_NATIVEIMAGE.txt + mkdir -p $out/share + # move files in $out like LICENSE.txt + find $out/ -maxdepth 1 -type f -exec mv {} $out/share \; + # symbolic link to $out/lib/svm/LICENSE_NATIVEIMAGE.txt + rm -f $out/LICENSE_NATIVEIMAGE.txt - # copy-paste openjdk's preFixup - # Set JAVA_HOME automatically. - mkdir -p $out/nix-support - cat > $out/nix-support/setup-hook << EOF - if [ -z "\''${JAVA_HOME-}" ]; then export JAVA_HOME=$out; fi - EOF + # copy-paste openjdk's preFixup + # Set JAVA_HOME automatically. + mkdir -p $out/nix-support + cat > $out/nix-support/setup-hook << EOF + if [ -z "\''${JAVA_HOME-}" ]; then export JAVA_HOME=$out; fi + EOF - wrapProgram $out/bin/native-image \ - --prefix PATH : ${binPath} \ - ${toString (map (l: "--add-flags '-H:CLibraryPath=${l}/lib'") cLibs)} - ''; + wrapProgram $out/bin/native-image \ + --prefix PATH : ${binPath} \ + ${toString (cLibsAsFlags ++ preservedNixVariablesAsFlags)} + ''; preFixup = lib.optionalString (stdenv.hostPlatform.isLinux) '' for bin in $(find "$out/bin" -executable -type f); do @@ -171,10 +188,8 @@ let echo "Testing GraalVM" $out/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler HelloWorld | fgrep 'Hello World' - extraNativeImageArgs="$(export -p | sed -n 's/^declare -x \([^=]\+\)=.*$/ -E\1/p' | tr -d \\n)" - echo "Ahead-Of-Time compilation" - $out/bin/native-image -H:+UnlockExperimentalVMOptions -H:-CheckToolchain -H:+ReportExceptionStackTraces -march=compatibility $extraNativeImageArgs HelloWorld + $out/bin/native-image -H:+UnlockExperimentalVMOptions -H:-CheckToolchain -H:+ReportExceptionStackTraces -march=compatibility HelloWorld ./helloworld | fgrep 'Hello World' ${# -H:+StaticExecutableWithDynamicLibC is only available in Linux