@@ -0,0 +1,133 @@
1+{ stdenv, requireFile, perl, unzip, glibc, zlib, gdk-pixbuf, xorg, glib, fontconfig, freetype, cairo, pango, gtk3, gtk2, ffmpeg, libGL, atk, alsaLib, libav_0_8, setJavaClassPath }:
2+
3+let
4+ graalvm8-ee = stdenv.mkDerivation rec {
5+ pname = "graalvm8-ee";
6+ version = "19.1.1";
7+ srcs = [
8+ (requireFile {
9+ name = "graalvm-ee-linux-amd64-${version}.tar.gz";
10+ sha256 = "1ayg4chiw08jbrivmsylh6275sn0ygzc0iaic28fk7hhw5dc6r2f";
11+ url = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
12+ })
13+ (requireFile {
14+ name = "native-image-installable-svm-svmee-linux-amd64-${version}.jar";
15+ sha256 = "1mvc4id193gl482fnjcgh4dz29ifqwigqq68ib0i1p3ks1nnj717";
16+ url = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
17+ })
18+ (requireFile {
19+ name = "python-installable-svm-svmee-linux-amd64-${version}.jar";
20+ sha256 = "0q7sggqk5myad053dsfw2i1n8v4wspahg1ac7zr3y3srqvdb0xfp";
21+ url = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
22+ })
23+ (requireFile {
24+ name = "ruby-installable-svm-svmee-linux-amd64-${version}.jar";
25+ sha256 = "1xax1bg155d74jhv7h0qb7vbc95jy4wm4bvbpvr76ivzn7qaml6j";
26+ url = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
27+ })
28+ ];
29+ nativeBuildInputs = [ unzip perl ];
30+ unpackPhase = ''
31+ unpack_jar() {
32+ jar=$1
33+ unzip $jar -d $out
34+ perl -ne 'use File::Path qw(make_path);
35+ use File::Basename qw(dirname);
36+ if (/^(.+) = (.+)$/) {
37+ make_path dirname("$ENV{out}/$1");
38+ system "ln -s $2 $ENV{out}/$1";
39+ }' $out/META-INF/symlinks
40+ perl -ne 'if (/^(.+) = ([r-])([w-])([x-])([r-])([w-])([x-])([r-])([w-])([x-])$/) {
41+ my $mode = ($2 eq 'r' ? 0400 : 0) + ($3 eq 'w' ? 0200 : 0) + ($4 eq 'x' ? 0100 : 0) +
42+ ($5 eq 'r' ? 0040 : 0) + ($6 eq 'w' ? 0020 : 0) + ($7 eq 'x' ? 0010 : 0) +
43+ ($8 eq 'r' ? 0004 : 0) + ($9 eq 'w' ? 0002 : 0) + ($10 eq 'x' ? 0001 : 0);
44+ chmod $mode, "$ENV{out}/$1";
45+ }' $out/META-INF/permissions
46+ rm -rf $out/META-INF
47+ }
48+
49+ mkdir -p $out
50+ arr=($srcs)
51+ tar xf ''${arr[0]} -C $out --strip-components=1
52+ unpack_jar ''${arr[1]}
53+ unpack_jar ''${arr[2]}
54+ unpack_jar ''${arr[3]}
55+ '';
56+
57+ installPhase = ''
58+ # BUG workaround http://mail.openjdk.java.net/pipermail/graal-dev/2017-December/005141.html
59+ substituteInPlace $out/jre/lib/security/java.security \
60+ --replace file:/dev/random file:/dev/./urandom \
61+ --replace NativePRNGBlocking SHA1PRNG
62+
63+ # provide libraries needed for static compilation
64+ for f in ${glibc}/lib/* ${glibc.static}/lib/* ${zlib.static}/lib/*; do
65+ ln -s $f $out/jre/lib/svm/clibraries/linux-amd64/$(basename $f)
66+ done
67+ '';
68+
69+ dontStrip = true;
70+
71+ preFixup = ''
72+ # Set JAVA_HOME automatically.
73+ mkdir -p $out/nix-support
74+ cat <<EOF > $out/nix-support/setup-hook
75+ if [ -z "\$JAVA_HOME" ]; then export JAVA_HOME=$out; fi
76+ EOF
77+ '';
78+
79+ postFixup = ''
80+ rpath="$out/jre/lib/amd64/jli:$out/jre/lib/amd64/server:$out/jre/lib/amd64:${
81+ stdenv.lib.strings.makeLibraryPath [ glibc xorg.libXxf86vm xorg.libX11 xorg.libXext xorg.libXtst xorg.libXi xorg.libXrender
82+ glib zlib alsaLib fontconfig freetype pango gtk3 gtk2 cairo gdk-pixbuf atk ffmpeg libGL ]}"
83+
84+ for f in $(find $out -type f -perm -0100); do
85+ patchelf --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" "$f" || true
86+ patchelf --set-rpath "$rpath" "$f" || true
87+ done
88+
89+ for f in $(find $out -type f -perm -0100); do
90+ if ldd "$f" | fgrep 'not found'; then echo "in file $f"; fi
91+ done
92+ '';
93+
94+ propagatedBuildInputs = [ setJavaClassPath zlib ]; # $out/bin/native-image needs zlib to build native executables
95+
96+ doInstallCheck = true;
97+ installCheckPhase = ''
98+ echo ${stdenv.lib.escapeShellArg ''
99+ public class HelloWorld {
100+ public static void main(String[] args) {
101+ System.out.println("Hello World");
102+ }
103+ }
104+ ''} > HelloWorld.java
105+ $out/bin/javac HelloWorld.java
106+
107+ # run on JVM with Graal Compiler
108+ $out/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler HelloWorld
109+ $out/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler HelloWorld | fgrep 'Hello World'
110+
111+ # Ahead-Of-Time compilation
112+ $out/bin/native-image --no-server HelloWorld
113+ ./helloworld
114+ ./helloworld | fgrep 'Hello World'
115+
116+ # Ahead-Of-Time compilation with --static
117+ $out/bin/native-image --no-server --static HelloWorld
118+ ./helloworld
119+ ./helloworld | fgrep 'Hello World'
120+ '';
121+
122+ passthru.home = graalvm8-ee;
123+
124+ meta = with stdenv.lib; {
125+ homepage = https://www.graalvm.org/;
126+ description = "High-Performance Polyglot VM";
127+ license = licenses.unfree;
128+ maintainers = with maintainers; [ volth hlolli ];
129+ platforms = [ "x86_64-linux" ];
130+ };
131+ };
132+in
133+ graalvm8-ee