Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gradle: auto-detect native version and patch file-events #277721

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions pkgs/development/tools/build-managers/gradle/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
rec {
gen =

{ version, nativeVersion, hash,
{ version, hash,

# The default JDK/JRE that will be used for derived Gradle packages.
# A current LTS version of a JDK is a good choice.
Expand Down Expand Up @@ -36,6 +36,7 @@ rec {
, testers
, runCommand
, writeText
, autoPatchelfHook

# The JDK/JRE used for running Gradle.
, java ? defaultJava
Expand All @@ -57,8 +58,22 @@ rec {

dontBuild = true;

nativeBuildInputs = [ makeWrapper unzip ];
buildInputs = [ java ];
nativeBuildInputs = [
makeWrapper
unzip
] ++ lib.optionals stdenv.isLinux [
autoPatchelfHook
];

buildInputs = [
java
stdenv.cc.cc
ncurses5
ncurses6
];

# We only need to patchelf some libs embedded in JARs.
dontAutoPatchelf = true;

installPhase = with builtins;
let
Expand Down Expand Up @@ -87,18 +102,22 @@ rec {

fixupPhase = let arch = if stdenv.is64bit then "amd64" else "i386";
in ''
. ${./patching.sh}

nativeVersion="$(extractVersion native-platform $out/lib/gradle/lib/native-platform-*.jar)"
for variant in "" "-ncurses5" "-ncurses6"; do
mkdir "patching$variant"
pushd "patching$variant"
jar xf $out/lib/gradle/lib/native-platform-linux-${arch}$variant-${nativeVersion}.jar
patchelf \
--set-rpath "${stdenv.cc.cc.lib}/lib64:${lib.makeLibraryPath [ stdenv.cc.cc ncurses5 ncurses6 ]}" \
net/rubygrapefruit/platform/linux-${arch}$variant/libnative-platform*.so
jar cf native-platform-linux-${arch}$variant-${nativeVersion}.jar .
mv native-platform-linux-${arch}$variant-${nativeVersion}.jar $out/lib/gradle/lib/
popd
autoPatchelfInJar \
$out/lib/gradle/lib/native-platform-linux-${arch}$variant-''${nativeVersion}.jar \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record - this is "just" patching the linux version (as it was done before).
As others told me, the resulting nix package works on macOS too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, patchelf is a Linux-only thing, AFAIK.

"${stdenv.cc.cc.lib}/lib64:${lib.makeLibraryPath [ stdenv.cc.cc ncurses5 ncurses6 ]}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know too much about elf patching - but this looks reasonable to me🤷

done

# The file-events library _seems_ to follow the native-platform version, but
# we won’t assume that.
fileEventsVersion="$(extractVersion file-events $out/lib/gradle/lib/file-events-*.jar)"
autoPatchelfInJar \
$out/lib/gradle/lib/file-events-linux-${arch}-''${fileEventsVersion}.jar \
"${stdenv.cc.cc.lib}/lib64:${lib.makeLibraryPath [ stdenv.cc.cc ]}"

# The scanner doesn't pick up the runtime dependency in the jar.
# Manually add a reference where it will be found.
mkdir $out/nix-support
Expand Down Expand Up @@ -162,21 +181,18 @@ rec {

gradle_8 = gen {
version = "8.10";
nativeVersion = "0.22-milestone-26";
hash = "sha256-W5xes/n8LJSrrqV9kL14dHyhF927+WyFnTdBGBoSvyo=";
defaultJava = jdk21;
};

gradle_7 = gen {
version = "7.6.4";
nativeVersion = "0.22-milestone-25";
hash = "sha256-vtHaM8yg9VerE2kcd/OLtnOIEZ5HlNET4FEDm4Cvm7E=";
defaultJava = jdk17;
};

gradle_6 = gen {
version = "6.9.4";
nativeVersion = "0.22-milestone-20";
hash = "sha256-PiQCKFON6fGHcqV06ZoLqVnoPW7zUQFDgazZYxeBOJo=";
defaultJava = jdk11;
};
Expand Down
29 changes: 29 additions & 0 deletions pkgs/development/tools/build-managers/gradle/patching.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extractVersion() {
local jar version
local prefix="$1"
shift
local candidates="$@"

jar="$(basename -a $candidates | sort | head -n1)"

version="${jar#$prefix-}"

echo "${version%.jar}"
}

autoPatchelfInJar() {
local file="$1" rpath="$2"
local work

work="$(mktemp -dt patching.XXXXXXXXXX)"
pushd "$work"

jar xf "$file"
rm "$file"

autoPatchelf -- .

jar cf "$file" .

popd
}
15 changes: 1 addition & 14 deletions pkgs/development/tools/build-managers/gradle/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,7 @@ do
read -d "\n" gradle_hash gradle_path < <(nix-prefetch-url --print-path $url)
gradle_hash=$(nix-hash --to-sri --type sha256 "$gradle_hash")

# Prefix and suffix for "native-platform" dependency.
gradle_native_prefix="gradle-$v/lib/native-platform-"
gradle_native_suffix=".jar"
tmp=$(mktemp)
zipinfo -1 "$gradle_path" "$gradle_native_prefix*$gradle_native_suffix" > $tmp
gradle_native=$(cat $tmp | head -n1)
gradle_native=${gradle_native#"$gradle_native_prefix"}
gradle_native=${gradle_native%"$gradle_native_suffix"}

# Supported architectures
#grep -Pho "(linux|osx)-\w+" $tmp | sort | uniq
rm -f $tmp

echo -e "{\\n version = \"$v\";\\n nativeVersion = \"$gradle_native\";\\n sha256 = \"$gradle_hash\";\\n}" > $f
echo -e "{\\n version = \"$v\";\\n sha256 = \"$gradle_hash\";\\n}" > $f

echo "$v DONE"
done