gradle: auto-detect nativeVersion

Instead of having to know the version of native-platform at the Nix
level, we use the file `lib/native-platform-<version>.jar` to figure
out the version of the library JAR to patch.

+54 -29
+24 -15
pkgs/development/tools/build-managers/gradle/default.nix
··· 3 3 rec { 4 4 gen = 5 5 6 - { version, nativeVersion, hash, 6 + { version, hash, 7 7 8 8 # The default JDK/JRE that will be used for derived Gradle packages. 9 9 # A current LTS version of a JDK is a good choice. ··· 36 36 , testers 37 37 , runCommand 38 38 , writeText 39 + , autoPatchelfHook 39 40 40 41 # The JDK/JRE used for running Gradle. 41 42 , java ? defaultJava ··· 57 58 58 59 dontBuild = true; 59 60 60 - nativeBuildInputs = [ makeWrapper unzip ]; 61 - buildInputs = [ java ]; 61 + nativeBuildInputs = [ 62 + makeWrapper 63 + unzip 64 + ] ++ lib.optionals stdenv.isLinux [ 65 + autoPatchelfHook 66 + ]; 67 + 68 + buildInputs = [ 69 + java 70 + stdenv.cc.cc 71 + ncurses5 72 + ncurses6 73 + ]; 74 + 75 + # We only need to patchelf some libs embedded in JARs. 76 + dontAutoPatchelf = true; 62 77 63 78 installPhase = with builtins; 64 79 let ··· 87 102 88 103 fixupPhase = let arch = if stdenv.is64bit then "amd64" else "i386"; 89 104 in '' 105 + . ${./patching.sh} 106 + 107 + nativeVersion="$(extractVersion native-platform $out/lib/gradle/lib/native-platform-*.jar)" 90 108 for variant in "" "-ncurses5" "-ncurses6"; do 91 - mkdir "patching$variant" 92 - pushd "patching$variant" 93 - jar xf $out/lib/gradle/lib/native-platform-linux-${arch}$variant-${nativeVersion}.jar 94 - patchelf \ 95 - --set-rpath "${stdenv.cc.cc.lib}/lib64:${lib.makeLibraryPath [ stdenv.cc.cc ncurses5 ncurses6 ]}" \ 96 - net/rubygrapefruit/platform/linux-${arch}$variant/libnative-platform*.so 97 - jar cf native-platform-linux-${arch}$variant-${nativeVersion}.jar . 98 - mv native-platform-linux-${arch}$variant-${nativeVersion}.jar $out/lib/gradle/lib/ 99 - popd 109 + autoPatchelfInJar \ 110 + $out/lib/gradle/lib/native-platform-linux-${arch}$variant-''${nativeVersion}.jar \ 111 + "${stdenv.cc.cc.lib}/lib64:${lib.makeLibraryPath [ stdenv.cc.cc ncurses5 ncurses6 ]}" 100 112 done 101 113 102 114 # The scanner doesn't pick up the runtime dependency in the jar. ··· 162 174 163 175 gradle_8 = gen { 164 176 version = "8.10"; 165 - nativeVersion = "0.22-milestone-26"; 166 177 hash = "sha256-W5xes/n8LJSrrqV9kL14dHyhF927+WyFnTdBGBoSvyo="; 167 178 defaultJava = jdk21; 168 179 }; 169 180 170 181 gradle_7 = gen { 171 182 version = "7.6.4"; 172 - nativeVersion = "0.22-milestone-25"; 173 183 hash = "sha256-vtHaM8yg9VerE2kcd/OLtnOIEZ5HlNET4FEDm4Cvm7E="; 174 184 defaultJava = jdk17; 175 185 }; 176 186 177 187 gradle_6 = gen { 178 188 version = "6.9.4"; 179 - nativeVersion = "0.22-milestone-20"; 180 189 hash = "sha256-PiQCKFON6fGHcqV06ZoLqVnoPW7zUQFDgazZYxeBOJo="; 181 190 defaultJava = jdk11; 182 191 };
+29
pkgs/development/tools/build-managers/gradle/patching.sh
··· 1 + extractVersion() { 2 + local jar version 3 + local prefix="$1" 4 + shift 5 + local candidates="$@" 6 + 7 + jar="$(basename -a $candidates | sort | head -n1)" 8 + 9 + version="${jar#$prefix-}" 10 + 11 + echo "${version%.jar}" 12 + } 13 + 14 + autoPatchelfInJar() { 15 + local file="$1" rpath="$2" 16 + local work 17 + 18 + work="$(mktemp -dt patching.XXXXXXXXXX)" 19 + pushd "$work" 20 + 21 + jar xf "$file" 22 + rm "$file" 23 + 24 + autoPatchelf -- . 25 + 26 + jar cf "$file" . 27 + 28 + popd 29 + }
+1 -14
pkgs/development/tools/build-managers/gradle/update.sh
··· 49 49 read -d "\n" gradle_hash gradle_path < <(nix-prefetch-url --print-path $url) 50 50 gradle_hash=$(nix-hash --to-sri --type sha256 "$gradle_hash") 51 51 52 - # Prefix and suffix for "native-platform" dependency. 53 - gradle_native_prefix="gradle-$v/lib/native-platform-" 54 - gradle_native_suffix=".jar" 55 - tmp=$(mktemp) 56 - zipinfo -1 "$gradle_path" "$gradle_native_prefix*$gradle_native_suffix" > $tmp 57 - gradle_native=$(cat $tmp | head -n1) 58 - gradle_native=${gradle_native#"$gradle_native_prefix"} 59 - gradle_native=${gradle_native%"$gradle_native_suffix"} 60 - 61 - # Supported architectures 62 - #grep -Pho "(linux|osx)-\w+" $tmp | sort | uniq 63 - rm -f $tmp 64 - 65 - echo -e "{\\n version = \"$v\";\\n nativeVersion = \"$gradle_native\";\\n sha256 = \"$gradle_hash\";\\n}" > $f 52 + echo -e "{\\n version = \"$v\";\\n sha256 = \"$gradle_hash\";\\n}" > $f 66 53 67 54 echo "$v DONE" 68 55 done