stripJavaArchivesHook: rename from canonicalize-jars-hook and use strip-nondeterminism

This commit is contained in:
TomaSajt 2024-03-17 01:52:15 +01:00
parent fc405ec2d8
commit 48285f7169
No known key found for this signature in database
GPG key ID: F011163C050122A1
6 changed files with 21 additions and 59 deletions

View file

@ -1,9 +0,0 @@
{ substituteAll, unzip, zip }:
substituteAll {
name = "canonicalize-jar";
src = ./canonicalize-jar.sh;
unzip = "${unzip}/bin/unzip";
zip = "${zip}/bin/zip";
}

View file

@ -1,29 +0,0 @@
# Canonicalize the manifest & repack with deterministic timestamps.
canonicalizeJar() {
local input='' outer=''
input="$(realpath -sm -- "$1")"
outer="$(pwd)"
# -qq: even quieter
@unzip@ -qq "$input" -d "$input-tmp"
canonicalizeJarManifest "$input-tmp/META-INF/MANIFEST.MF"
# Sets all timestamps to Jan 1 1980, the earliest mtime zips support.
find -- "$input-tmp" -exec touch -t 198001010000.00 {} +
rm "$input"
pushd "$input-tmp" 2>/dev/null
# -q|--quiet, -r|--recurse-paths
# -o|--latest-time: canonicalizes overall archive mtime
# -X|--no-extra: don't store platform-specific extra file attribute fields
@zip@ -qroX "$outer/tmp-out.jar" . 2> /dev/null
popd 2>/dev/null
rm -rf "$input-tmp"
mv "$outer/tmp-out.jar" "$input"
}
# See also the Java specification's JAR requirements:
# https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Notes_on_Manifest_and_Signature_Files
canonicalizeJarManifest() {
local input=''
input="$(realpath -sm -- "$1")"
(head -n 1 "$input" && tail -n +2 "$input" | sort | grep -v '^\s*$') > "$input-tmp"
mv "$input-tmp" "$input"
}

View file

@ -1,16 +0,0 @@
# This setup hook causes the fixup phase to repack all JAR files in a
# canonical & deterministic fashion, e.g. resetting mtimes (like with normal
# store files) and avoiding impure metadata.
fixupOutputHooks+=('if [ -z "$dontCanonicalizeJars" -a -e "$prefix" ]; then canonicalizeJarsIn "$prefix"; fi')
canonicalizeJarsIn() {
local dir="$1"
echo "canonicalizing jars in $dir"
dir="$(realpath -sm -- "$dir")"
while IFS= read -rd '' f; do
canonicalizeJar "$f"
done < <(find -- "$dir" -type f -name '*.jar' -print0)
}
source @canonicalize_jar@

View file

@ -0,0 +1,16 @@
# This setup hook makes the fixup phase to repack all java archives in a
# deterministic fashion. The most important change being done is the resetting
# of the modification times of the archive entries
fixupOutputHooks+=('stripJavaArchivesIn $prefix')
stripJavaArchivesIn() {
local dir="$1"
echo "stripping java archives in $dir"
find $dir -type f -regextype posix-egrep -regex ".*\.(jar|war|hpi|apk)$" -print0 |
while IFS= read -rd '' f; do
echo "stripping java archive $f"
strip-nondeterminism --type jar "$f"
done
}

View file

@ -146,6 +146,7 @@ mapAliases ({
callPackage_i686 = pkgsi686Linux.callPackage;
cadence = throw "cadence has been removed from nixpkgs, as it was archived upstream"; # Added 2023-10-28
cask = emacs.pkgs.cask; # Added 2022-11-12
canonicalize-jars-hook = stripJavaArchivesHook; # Added 2024-03-17
cargo-embed = throw "cargo-embed is now part of the probe-rs package"; # Added 2023-07-03
cargo-espflash = espflash;
cargo-flash = throw "cargo-flash is now part of the probe-rs package"; # Added 2023-07-03

View file

@ -212,11 +212,10 @@ with pkgs;
inherit (darwin.apple_sdk.frameworks) Security;
};
canonicalize-jar = callPackage ../build-support/java/canonicalize-jar.nix { };
canonicalize-jars-hook = makeSetupHook {
name = "canonicalize-jars-hook";
substitutions = { canonicalize_jar = canonicalize-jar; };
} ../build-support/setup-hooks/canonicalize-jars.sh;
stripJavaArchivesHook = makeSetupHook {
name = "strip-java-archives-hook";
propagatedBuildInputs = [ strip-nondeterminism ];
} ../build-support/setup-hooks/strip-java-archives.sh;
ensureNewerSourcesHook = { year }: makeSetupHook {
name = "ensure-newer-sources-hook";