ci: Workaround macOS hdiutil out of space errors
[quassel.git] / scripts / build / macosx_makePackage.sh
1 #!/bin/bash
2 # Don't consider packaging a success if any commands fail
3 # See http://redsymbol.net/articles/unofficial-bash-strict-mode/
4 set -euo pipefail
5
6 myname=$0
7 if [ -s "$myname" ] && [ -x "$myname" ]; then
8     # $myname is already a valid file name
9
10     mypath=$myname
11 else
12     case "$myname" in
13     /*) exit 1;;             # absolute path - do not search PATH
14     *)
15         # Search all directories from the PATH variable. Take
16         # care to interpret leading and trailing ":" as meaning
17         # the current directory; the same is true for "::" within
18         # the PATH.
19
20         # Replace leading : with . in PATH, store in p
21         p=${PATH/#:/.:}
22         # Replace trailing : with .
23         p=${p//%:/:.}
24         # Replace :: with :.:
25         p=${p//::/:.:}
26         # Temporary input field separator, see FAQ #1
27         OFS=$IFS IFS=:
28         # Split the path on colons and loop through each of them
29         for dir in $p; do
30                 [ -f "$dir/$myname" ] || continue # no file
31                 [ -x "$dir/$myname" ] || continue # not executable
32                 mypath=$dir/$myname
33                 break           # only return first matching file
34         done
35         # Restore old input field separator
36         IFS=$OFS
37         ;;
38     esac
39 fi
40
41 if [ ! -f "$mypath" ]; then
42     echo >&2 "cannot find full path name: $myname"
43     exit 1
44 fi
45
46 SCRIPTDIR=$(dirname $mypath)
47 QUASSEL_VERSION=$(git describe)
48 BUILDTYPE=$1
49
50 # check the working dir
51 # Default to "." using Bash default-value syntax
52 WORKINGDIR="${2:-.}"
53 WORKINGDIR="${WORKINGDIR}/"
54 PACKAGETMPDIR="${WORKINGDIR}PACKAGE_TMP_DIR_${BUILDTYPE}"
55 QUASSEL_DMG="Quassel${BUILDTYPE}_MacOSX-x86_64_${QUASSEL_VERSION}.dmg"
56
57 # Default to null string
58 if [[ -z ${3:-} ]]; then
59         ADDITIONAL_PLUGINS=""
60 else
61         # Options provided, append to list
62         ADDITIONAL_PLUGINS=",$3"
63 fi
64
65 echo "ADDITIONAL_PLUGINS: ${ADDITIONAL_PLUGINS}"
66
67 mkdir $PACKAGETMPDIR
68 case $BUILDTYPE in
69 "Client")
70         cp -r ${WORKINGDIR}Quassel\ Client.app ${PACKAGETMPDIR}/
71         ${SCRIPTDIR}/macosx_DeployApp.py --plugins=qcocoa,qgenericbearer,qcorewlanbearer,qmacstyle${ADDITIONAL_PLUGINS} "${PACKAGETMPDIR}/Quassel Client.app"
72         ;;
73 "Core")
74         cp ${WORKINGDIR}quasselcore ${PACKAGETMPDIR}/
75         ${SCRIPTDIR}/macosx_DeployApp.py --nobundle --plugins=qsqlite,qsqlpsql${ADDITIONAL_PLUGINS} ${PACKAGETMPDIR}
76         ;;
77 "Mono")
78         cp -r ${WORKINGDIR}Quassel.app ${PACKAGETMPDIR}/
79         ${SCRIPTDIR}/macosx_DeployApp.py --plugins=qsqlite,qsqlpsql,qcocoa,qgenericbearer,qcorewlanbearer,qmacstyle${ADDITIONAL_PLUGINS} "${PACKAGETMPDIR}/Quassel.app"
80         ;;
81 *)
82         echo >&2 "Valid parameters are \"Client\", \"Core\", or \"Mono\"."
83         rmdir ${PACKAGETMPDIR}
84         exit 1
85         ;;
86 esac
87
88 echo "Creating macOS disk image with hdiutil: 'Quassel ${BUILDTYPE} - ${QUASSEL_VERSION}'"
89
90 # hdiutil seems to have a bit of a reputation for failing to create disk images
91 # for various reasons.
92 #
93 # If you've come here to see why on earth your macOS build is failing despite
94 # making changes entirely unrelated to macOS, you have my sympathy.
95 #
96 # There are two main approaches:
97 #
98 # 1.  Let hdiutil calculate a size automatically
99 #
100 # 2.  Separately calculate the size with a margin of error, then specify this
101 #     to hdiutil during disk image creation.
102 #
103 # Both seem to have caused issues, but in recent tests, option #1 seemed more
104 # reliable.
105 #
106 # Option 1:
107
108 hdiutil create -srcfolder ${PACKAGETMPDIR} -format UDBZ -volname "Quassel ${BUILDTYPE} - ${QUASSEL_VERSION}" "${WORKINGDIR}${QUASSEL_DMG}" >/dev/null
109
110 # If hdiutil changes over time and fails often, you can try the other option.
111 #
112 # Option 2:
113 #
114 #PACKAGESIZE_MARGIN="1.1"
115 #PACKAGESIZE=$(echo "$(du -ms ${PACKAGETMPDIR} | cut -f1) * $PACKAGESIZE_MARGIN" | bc)
116 #echo "PACKAGESIZE: $PACKAGESIZE MB"
117 #hdiutil create -srcfolder ${PACKAGETMPDIR} -format UDBZ -size ${PACKAGESIZE}M -volname "Quassel ${BUILDTYPE} - ${QUASSEL_VERSION}" "${WORKINGDIR}${QUASSEL_DMG}" >/dev/null
118
119
120 # Regardless of choice, clean up the packaging temporary directory
121 rm -rf ${PACKAGETMPDIR}