<commit_before>{ stdenv, requireFile, perl, unzip, glibc, zlib, gdk_pixbuf, xorg, glib, fontconfig, freetype, cairo, pango, gtk3, gtk2, ffmpeg, libGL, atk, alsaLib, libav_0_8, setJavaClassPath }:

let
  graalvm8-ee = stdenv.mkDerivation rec {
    pname = "graalvm8-ee";
    version = "19.1.1";
    srcs = [
      (requireFile {
         name   = "graalvm-ee-linux-amd64-${version}.tar.gz";
         sha256 = "1ayg4chiw08jbrivmsylh6275sn0ygzc0iaic28fk7hhw5dc6r2f";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
      (requireFile {
         name   = "native-image-installable-svm-svmee-linux-amd64-${version}.jar";
         sha256 = "1mvc4id193gl482fnjcgh4dz29ifqwigqq68ib0i1p3ks1nnj717";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
      (requireFile {
         name   = "python-installable-svm-svmee-linux-amd64-${version}.jar";
         sha256 = "0q7sggqk5myad053dsfw2i1n8v4wspahg1ac7zr3y3srqvdb0xfp";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
      (requireFile {
         name   = "ruby-installable-svm-svmee-linux-amd64-${version}.jar";
         sha256 = "1xax1bg155d74jhv7h0qb7vbc95jy4wm4bvbpvr76ivzn7qaml6j";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
    ];
    nativeBuildInputs = [ unzip perl ];
    unpackPhase = ''
      unpack_jar() {
        jar=$1
        unzip $jar -d $out
        perl -ne 'use File::Path qw(make_path);
                  use File::Basename qw(dirname);
                  if (/^(.+) = (.+)$/) {
                    make_path dirname("$ENV{out}/$1");
                    system "ln -s $2 $ENV{out}/$1";
                  }' $out/META-INF/symlinks
        perl -ne 'if (/^(.+) = ([r-])([w-])([x-])([r-])([w-])([x-])([r-])([w-])([x-])$/) {
                    my $mode = ($2 eq 'r' ? 0400 : 0) + ($3 eq 'w' ? 0200 : 0) + ($4  eq 'x' ? 0100 : 0) +
                               ($5 eq 'r' ? 0040 : 0) + ($6 eq 'w' ? 0020 : 0) + ($7  eq 'x' ? 0010 : 0) +
                               ($8 eq 'r' ? 0004 : 0) + ($9 eq 'w' ? 0002 : 0) + ($10 eq 'x' ? 0001 : 0);
                    chmod $mode, "$ENV{out}/$1";
                  }' $out/META-INF/permissions
        rm -rf $out/META-INF
      }

      mkdir -p $out
      arr=($srcs)
      tar xf ''${arr[0]} -C $out --strip-components=1
      unpack_jar ''${arr[1]}
      unpack_jar ''${arr[2]}
      unpack_jar ''${arr[3]}
    '';

    installPhase = ''
      # BUG workaround http://mail.openjdk.java.net/pipermail/graal-dev/2017-December/005141.html
      substituteInPlace $out/jre/lib/security/java.security \
        --replace file:/dev/random    file:/dev/./urandom \
        --replace NativePRNGBlocking  SHA1PRNG

      # provide libraries needed for static compilation
      for f in ${glibc}/lib/* ${glibc.static}/lib/* ${zlib.static}/lib/*; do
        ln -s $f $out/jre/lib/svm/clibraries/linux-amd64/$(basename $f)
      done
    '';

    dontStrip = true;

    # copy-paste openjdk's preFixup
    preFixup = ''
      # Propagate the setJavaClassPath setup hook from the JRE so that
      # any package that depends on the JRE has $CLASSPATH set up
      # properly.
      mkdir -p $out/nix-support
      printWords ${setJavaClassPath} > $out/nix-support/propagated-build-inputs

      # Set JAVA_HOME automatically.
      mkdir -p $out/nix-support
      cat <<EOF > $out/nix-support/setup-hook
      if [ -z "\$JAVA_HOME" ]; then export JAVA_HOME=$out; fi
      EOF
    '';

    postFixup = ''
      rpath="$out/jre/lib/amd64/jli:$out/jre/lib/amd64/server:$out/jre/lib/amd64:${
        stdenv.lib.strings.makeLibraryPath [ glibc xorg.libXxf86vm xorg.libX11 xorg.libXext xorg.libXtst xorg.libXi xorg.libXrender
                                             glib zlib alsaLib fontconfig freetype pango gtk3 gtk2 cairo gdk_pixbuf atk ffmpeg libGL ]}"

      for f in $(find $out -type f -perm -0100); do
        patchelf --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" "$f" || true
        patchelf --set-rpath   "$rpath"                                    "$f" || true
      done

      for f in $(find $out -type f -perm -0100); do
        if ldd "$f" | fgrep 'not found'; then echo "in file $f"; fi
      done
    '';

    propagatedBuildInputs = [ zlib ]; # $out/bin/native-image needs zlib to build native executables
    
    doInstallCheck = true;
    installCheckPhase = ''
      echo ${stdenv.lib.escapeShellArg ''
               public class HelloWorld {
                 public static void main(String[] args) {
                   System.out.println("Hello World");
                 }
               }
             ''} > HelloWorld.java<commit_msg>graalvm8-ee: various fixes<commit_after>{ stdenv, requireFile, perl, unzip, glibc, zlib, gdk-pixbuf, xorg, glib, fontconfig, freetype, cairo, pango, gtk3, gtk2, ffmpeg, libGL, atk, alsaLib, libav_0_8, setJavaClassPath }:

let
  graalvm8-ee = stdenv.mkDerivation rec {
    pname = "graalvm8-ee";
    version = "19.1.1";
    srcs = [
      (requireFile {
         name   = "graalvm-ee-linux-amd64-${version}.tar.gz";
         sha256 = "1ayg4chiw08jbrivmsylh6275sn0ygzc0iaic28fk7hhw5dc6r2f";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
      (requireFile {
         name   = "native-image-installable-svm-svmee-linux-amd64-${version}.jar";
         sha256 = "1mvc4id193gl482fnjcgh4dz29ifqwigqq68ib0i1p3ks1nnj717";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
      (requireFile {
         name   = "python-installable-svm-svmee-linux-amd64-${version}.jar";
         sha256 = "0q7sggqk5myad053dsfw2i1n8v4wspahg1ac7zr3y3srqvdb0xfp";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
      (requireFile {
         name   = "ruby-installable-svm-svmee-linux-amd64-${version}.jar";
         sha256 = "1xax1bg155d74jhv7h0qb7vbc95jy4wm4bvbpvr76ivzn7qaml6j";
         url    = "https://www.oracle.com/technetwork/graalvm/downloads/index.html";
      })
    ];
    nativeBuildInputs = [ unzip perl ];
    unpackPhase = ''
      unpack_jar() {
        jar=$1
        unzip $jar -d $out
        perl -ne 'use File::Path qw(make_path);
                  use File::Basename qw(dirname);
                  if (/^(.+) = (.+)$/) {
                    make_path dirname("$ENV{out}/$1");
                    system "ln -s $2 $ENV{out}/$1";
                  }' $out/META-INF/symlinks
        perl -ne 'if (/^(.+) = ([r-])([w-])([x-])([r-])([w-])([x-])([r-])([w-])([x-])$/) {
                    my $mode = ($2 eq 'r' ? 0400 : 0) + ($3 eq 'w' ? 0200 : 0) + ($4  eq 'x' ? 0100 : 0) +
                               ($5 eq 'r' ? 0040 : 0) + ($6 eq 'w' ? 0020 : 0) + ($7  eq 'x' ? 0010 : 0) +
                               ($8 eq 'r' ? 0004 : 0) + ($9 eq 'w' ? 0002 : 0) + ($10 eq 'x' ? 0001 : 0);
                    chmod $mode, "$ENV{out}/$1";
                  }' $out/META-INF/permissions
        rm -rf $out/META-INF
      }

      mkdir -p $out
      arr=($srcs)
      tar xf ''${arr[0]} -C $out --strip-components=1
      unpack_jar ''${arr[1]}
      unpack_jar ''${arr[2]}
      unpack_jar ''${arr[3]}
    '';

    installPhase = ''
      # BUG workaround http://mail.openjdk.java.net/pipermail/graal-dev/2017-December/005141.html
      substituteInPlace $out/jre/lib/security/java.security \
        --replace file:/dev/random    file:/dev/./urandom \
        --replace NativePRNGBlocking  SHA1PRNG

      # provide libraries needed for static compilation
      for f in ${glibc}/lib/* ${glibc.static}/lib/* ${zlib.static}/lib/*; do
        ln -s $f $out/jre/lib/svm/clibraries/linux-amd64/$(basename $f)
      done
    '';

    dontStrip = true;

    preFixup = ''
      # Set JAVA_HOME automatically.
      mkdir -p $out/nix-support
      cat <<EOF > $out/nix-support/setup-hook
      if [ -z "\$JAVA_HOME" ]; then export JAVA_HOME=$out; fi
      EOF
    '';

    postFixup = ''
      rpath="$out/jre/lib/amd64/jli:$out/jre/lib/amd64/server:$out/jre/lib/amd64:${
        stdenv.lib.strings.makeLibraryPath [ glibc xorg.libXxf86vm xorg.libX11 xorg.libXext xorg.libXtst xorg.libXi xorg.libXrender
                                             glib zlib alsaLib fontconfig freetype pango gtk3 gtk2 cairo gdk-pixbuf atk ffmpeg libGL ]}"

      for f in $(find $out -type f -perm -0100); do
        patchelf --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" "$f" || true
        patchelf --set-rpath   "$rpath"                                    "$f" || true
      done

      for f in $(find $out -type f -perm -0100); do
        if ldd "$f" | fgrep 'not found'; then echo "in file $f"; fi
      done
    '';

    propagatedBuildInputs = [ setJavaClassPath zlib ]; # $out/bin/native-image needs zlib to build native executables
    
    doInstallCheck = true;
    installCheckPhase = ''
      echo ${stdenv.lib.escapeShellArg ''
               public class HelloWorld {
                 public static void main(String[] args) {
                   System.out.println("Hello World");
                 }
               }
             ''} > HelloWorld.java
{'fraction_non_alphanumeric': 0.15401254596582306, 'fraction_numerical': 0.0427211767250703, 'mean_word_length': 1.9656831302116742, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 8, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3747199', 'n_tokens_mistral': 3629, 'n_tokens_neox': 3369, 'n_words': 641}