Ticket #21533: 21533.sh

File 21533.sh, 4.8 KB (added by taylor.smock, 2 years ago)

Universal app generation (note: currently requires a JOSM install at /Applications -- this is also the only (current) part that requires a mac)

Line 
1set -x
2# Install file, curl, jq and LLVM v15 in whatever distribution you are using, e.g. `apt install file curl llvm-15 jq`
3# API documentation: https://api.azul.com/metadata/v1/docs/swagger
4JAVA_VERSION=21;
5JAVA_TYPE="jdk"
6BASE_URL="https://api.azul.com/metadata/v1/zulu/packages/?java_version=${JAVA_VERSION}&os=macos&archive_type=tar.gz&java_package_type=${JAVA_TYPE}&javafx_bundled=true&crac_supported=false&crs_supported=false&support_term=lts&latest=true&release_status=ga&availability_types=CA&certifications=tck&page=1&page_size=100&arch="
7X86=$(curl ${BASE_URL}amd64)
8AARCH64=$(curl ${BASE_URL}aarch64)
9WORKDIR=workdir
10AARCH64_NAME=$(echo "${AARCH64}" | jq -r '.[].name')
11X86_NAME=$(echo "${X86}" | jq -r '.[].name')
12
13function merge() {
14 if [ "$(command -v lipo)" ]; then
15 lipo -create -output "${1}" "${2}" "${3}"
16 elif [ "$(command -v llvm-lipo-15)" ]; then
17 llvm-lipo-15 -create -output "${1}" "${2}" "${3}"
18 fi
19}
20
21function get_jres() {
22 local localPwd=$(pwd)
23 if [ ! -d "${WORKDIR}" ]; then mkdir "${WORKDIR}"; fi
24 cd "${WORKDIR}" || exit 2
25 curl --location "$(echo "${AARCH64}" | jq -r '.[].download_url')" --output "${AARCH64_NAME}"
26 curl --location "$(echo "${X86}" | jq -r '.[].download_url')" --output "${X86_NAME}"
27 echo "${AARCH64}" > ".version"
28 cd "${localPwd}" || exit 2
29}
30
31function copy() {
32 # Trim the root path
33 FILE="${1#*/}"
34 if [ ! -e "target/${FILE}" ]; then
35 # Only make directories if we aren't looking at the root files
36 if [[ "${FILE}" == *"/"* ]]; then mkdir -p "target/${FILE%/*}"; fi
37 if file "${1}" | grep -q 'Mach-O' ; then
38 merge "target/${FILE}" "x64/${FILE}" "aarch64/${FILE}"
39 if file "${1}" | grep -q 'executable'; then
40 chmod 755 "target/${FILE}"
41 fi
42 else
43 cp -a "${1}" "target/${FILE}"
44 fi
45 fi
46}
47
48function directory_iterate() {
49 while IFS= read -r -d '' file
50 do
51 copy "${file}" &
52 done < <(find "${1}" -type f,l -print0)
53 wait
54}
55
56function create_universal_target() {
57 local localPwd="$(pwd)"
58 cd "${WORKDIR}" || exit 2
59 mkdir aarch64 && tar --strip-components 1 --directory=aarch64 -xf "${AARCH64_NAME}"
60 mkdir x64 && tar --strip-components 1 --directory=x64 -xf "${X86_NAME}"
61 #if [ -d target ]; then rm -r target; fi
62 mkdir target
63 # Do both just in case there are non-shared files
64 directory_iterate aarch64
65 directory_iterate x64
66 # Remove extracted files to save disk space
67 #rm -rf x64
68 #rm -rf aarch64
69 cd "${localPwd}" || exit 2
70}
71
72function package() {
73 local localPwd="$(pwd)"
74 # Remove generic JDK information
75 cd "${1}/"*"${JAVA_TYPE}"*"/Contents" || exit 2
76 rm -r "Info.plist"
77 rm -r "MacOS"
78 rm -r "_CodeSignature"
79 # The jre directory is what we point to as "JAVA_HOME" in the script; this is the bundled jdk.
80 mv "Home" "jre"
81 cd ${localPwd} || exit 2
82 jar --create --file jre.os-x-"$(jq -r '.[].java_version | join(".")' < "${WORKDIR}/.version").jar" -C "${WORKDIR}/target/"*"${JAVA_TYPE}"*"/Contents" .
83}
84
85function generate_josm() {
86 mkdir -p "${WORKDIR}/JOSM.app/Contents/MacOS"
87 mkdir -p "${WORKDIR}/JOSM.app/Contents/jars"
88 cp -R "${WORKDIR}/target/"*"${JAVA_TYPE}"*"/Contents" "${WORKDIR}/JOSM.app/"
89 cat << EOF > "${WORKDIR}/JOSM.app/Contents/MacOS/JOSM"
90#!/bin/bash
91cd "\$(dirname \$0)/.."
92JAVA_OPTS="--add-modules java.scripting,java.sql,javafx.controls,javafx.media,javafx.swing,javafx.web \
93 --add-exports=java.base/sun.security.action=ALL-UNNAMED \
94 --add-exports=java.desktop/com.sun.imageio.plugins.jpeg=ALL-UNNAMED \
95 --add-exports=java.desktop/com.sun.imageio.spi=ALL-UNNAMED"
96# shellcheck disable=SC2086
97while true; do
98 arch -\$(uname -m) -e JAVA_HOME="./jre/" ./jre/bin/java \${JAVA_OPTS} -jar jars/josm-tested.jar "\${@}"
99 if [ "z\$?" != "z9" ]; then
100 break;
101 fi
102 echo ">> restarting josm..."
103done
104EOF
105 chmod +x "${WORKDIR}/JOSM.app/Contents/MacOS/JOSM"
106 curl -L https://josm.openstreetmap.de/josm-tested.jar --output "${WORKDIR}/JOSM.app/Contents/jars/josm-tested.jar"
107 # TODO generate Info.plist (this is the only bit that is specific to Mac)
108 # Note: if we keep a static copy of it, note how it was generated here!
109 cp /Applications/JOSM.app/Contents/Info.plist "${WORKDIR}/JOSM.app/Contents"
110}
111
112if [ "$(echo "${AARCH64}" | jq '.[].distro_version')" == "$(echo "${X86}" | jq '.[].distro_version')" ]; then
113 if [ -d "${WORKDIR}" ] && [ -f "${WORKDIR}/.version" ]; then
114 if [ "$(echo "${AARCH64}" | jq '.[].distro_version')" != "$(jq '.[].distro_version' < "${WORKDIR}/.version")" ]; then
115 rm -rf "${WORKDIR}"
116 get_jres
117 fi
118 else
119 get_jres
120 fi
121 if [ ! -d "${WORKDIR}/target" ]; then create_universal_target; fi
122 if [ ! -f "jre.os-x-$(jq -r '.[].java_version | join(".")' < "${WORKDIR}/.version").jar" ]; then package "${WORKDIR}/target"; fi
123 # Everything up to this point is usable by other people
124 generate_josm
125else
126 echo "Mismatched versions"
127 echo "${X86}" | jq
128 echo "${AARCH64}" | jq
129 exit 1
130fi