Created
June 29, 2023 20:11
-
-
Save kosivantsov/4f8b020e6c846bf89625f42754ebea9f to your computer and use it in GitHub Desktop.
Bash script to meld x64 and arm64 JREs into a universal JRE for macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Taken from https://incenp.org/notes/2023/universal-java-app-on-macos.html | |
# with slight modifications to compress the resulting JRE into a tar.gz file (like the original distributed files) | |
die() { | |
echo "${0##*/}: $@" >&2 | |
exit 1 | |
} | |
version=$(ls OpenJDK*-jre_x64_mac_hotspot_*.tar.gz | sed -E "s,OpenJDK.+-jre_x64_mac_hotspot_(.+).tar.gz,\1,") | |
[ -n "$version" ] || die "Cannot identify JRE version" | |
[ -f OpenJDK*-jre_aarch64_mac_hotspot_$version.tar.gz ] || die "Missing corresponding arm64 JRE" | |
name=$(ls OpenJDK*-jre_x64_mac_hotspot_*.tar.gz | sed "s/-jre.*//g") | |
echo "Extracting native JREs..." | |
rm -rf x86_64 arm64 | |
mkdir x86_64 arm64 | |
(cd x86_64 | |
tar xf ../OpenJDK*-jre_x64_mac_hotspot_$version.tar.gz) | |
(cd arm64 | |
tar xf ../OpenJDK*-jre_aarch64_mac_hotspot_$version.tar.gz) | |
echo "Creating universal JRE..." | |
rm -rf universal | |
mkdir universal | |
find arm64 -type f | while read arm_file ; do | |
noarch_file=${arm_file#arm64/} | |
mkdir -p universal/${noarch_file%/*} | |
if file $arm_file | grep "Mach-O.\+arm64" ; then | |
# Create universal binary from both x86_64 and arm64 | |
lipo -create -output universal/$noarch_file x86_64/$noarch_file $arm_file | |
if file $arm_file | grep executable ; then | |
chmod 755 universal/$noarch_file | |
fi | |
else | |
# Not a file with binary code, copy it as it is | |
cp $arm_file universal/$noarch_file | |
fi | |
done | |
echo "Packaging the JRE..." | |
(cd universal/jdk*/Contents | |
rm -rf _CodeSignature) | |
cd universal | |
# Do not create ._ files (macOS extended attributes) inside the tar archive | |
export COPYFILE_DISABLE=1 | |
targz_file="$name-jre_universal_mac_hotspot_$version.tar.gz" | |
tar cf - ./ | gzip -f9 > ../$targz_file | |
cd .. | |
echo "$targz_file created" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment