graalvm-ce: fix native-image standalone usage by wrapping Nix env variables

Closes #350909.
This commit is contained in:
Pavel Shirshov
2024-10-27 12:22:11 +00:00
committed by Thiago Kenji Okada
parent 5bc90efcc4
commit bc1bc1aa33
@@ -120,27 +120,44 @@ let
xorg.libXtst xorg.libXtst
]; ];
postInstall = '' postInstall =
# jni.h expects jni_md.h to be in the header search path. let
ln -sf $out/include/linux/*_md.h $out/include/ 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 mkdir -p $out/share
# move files in $out like LICENSE.txt # move files in $out like LICENSE.txt
find $out/ -maxdepth 1 -type f -exec mv {} $out/share \; find $out/ -maxdepth 1 -type f -exec mv {} $out/share \;
# symbolic link to $out/lib/svm/LICENSE_NATIVEIMAGE.txt # symbolic link to $out/lib/svm/LICENSE_NATIVEIMAGE.txt
rm -f $out/LICENSE_NATIVEIMAGE.txt rm -f $out/LICENSE_NATIVEIMAGE.txt
# copy-paste openjdk's preFixup # copy-paste openjdk's preFixup
# Set JAVA_HOME automatically. # Set JAVA_HOME automatically.
mkdir -p $out/nix-support mkdir -p $out/nix-support
cat > $out/nix-support/setup-hook << EOF cat > $out/nix-support/setup-hook << EOF
if [ -z "\''${JAVA_HOME-}" ]; then export JAVA_HOME=$out; fi if [ -z "\''${JAVA_HOME-}" ]; then export JAVA_HOME=$out; fi
EOF EOF
wrapProgram $out/bin/native-image \ wrapProgram $out/bin/native-image \
--prefix PATH : ${binPath} \ --prefix PATH : ${binPath} \
${toString (map (l: "--add-flags '-H:CLibraryPath=${l}/lib'") cLibs)} ${toString (cLibsAsFlags ++ preservedNixVariablesAsFlags)}
''; '';
preFixup = lib.optionalString (stdenv.hostPlatform.isLinux) '' preFixup = lib.optionalString (stdenv.hostPlatform.isLinux) ''
for bin in $(find "$out/bin" -executable -type f); do for bin in $(find "$out/bin" -executable -type f); do
@@ -171,10 +188,8 @@ let
echo "Testing GraalVM" echo "Testing GraalVM"
$out/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler HelloWorld | fgrep 'Hello World' $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" 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' ./helloworld | fgrep 'Hello World'
${# -H:+StaticExecutableWithDynamicLibC is only available in Linux ${# -H:+StaticExecutableWithDynamicLibC is only available in Linux