]> wimlib.net Git - wimlib/blob - tools/make-windows-release
Improve the make-windows-release script
[wimlib] / tools / make-windows-release
1 #!/bin/bash
2 #
3 # This script prepares a Windows binary distribution of wimlib.
4
5 set -e -u
6
7 SCRIPTNAME="$0"
8 TOPDIR=$(dirname "$(dirname "$(realpath "$0")")")
9 cd "$TOPDIR" # Top-level directory of the git repo
10
11 MSYSTEM=${MSYSTEM:-}
12 if [ -z "$MSYSTEM" ]; then
13         ARCH=x86_64
14 else
15         case "$MSYSTEM" in
16         MINGW32)
17                 ARCH=i686
18                 ;;
19         MINGW64)
20                 ARCH=x86_64
21                 ;;
22         *)
23                 echo 1>&2 "Unsupported MSYS2 environment: $MSYSTEM"
24                 echo 1>&2 "In MSYS2, you must use the MINGW32 or MINGW64 environment."
25                 echo 1>&2 "See https://www.msys2.org/docs/environments/"
26                 exit 1
27         esac
28 fi
29 INSTALL_MSYS2_PACKAGES=false
30 BUILD_DOCS=true
31 BUILD_ZIP=true
32
33 longopts="help"
34 longopts+=",arch:"
35 longopts+=",install-msys2-packages"
36 longopts+=",no-docs"
37 longopts+=",no-zip"
38
39 usage()
40 {
41         cat << EOF
42 Usage: $SCRIPTNAME [OPTION]... [EXTRA_CONFIGURE_ARG]...
43 Options:
44   --arch=ARCH                Specify the architecture.  The default is taken
45                                  from MSYSTEM if available, otherwise is x86_64.
46   --install-msys2-packages   Install prerequisite MSYS2 packages
47   --no-docs                  Don't build and install PDF manual pages
48   --no-zip                   Don't create the final zip file
49 EOF
50 }
51
52 if ! options=$(getopt -o "" -l "$longopts" -- "$@"); then
53         usage 1>&2
54         exit 1
55 fi
56 eval set -- "$options"
57 while true; do
58         case "$1" in
59         --help)
60                 usage
61                 exit 0
62                 ;;
63         --arch)
64                 ARCH=$2
65                 shift
66                 ;;
67         --install-msys2-packages)
68                 if [ -z "$MSYSTEM" ]; then
69                         echo 1>&2 "--install-msys2-packages is not allowed outside MSYS2."
70                         exit 1
71                 fi
72                 INSTALL_MSYS2_PACKAGES=true
73                 ;;
74         --no-docs)
75                 BUILD_DOCS=false
76                 ;;
77         --no-zip)
78                 BUILD_ZIP=false
79                 ;;
80         --)
81                 shift
82                 break
83                 ;;
84         *)
85                 echo 1>&2 "Invalid option '$1'"
86                 usage 1>&2
87                 exit 1
88                 ;;
89         esac
90         shift
91 done
92
93 EXTRA_CONFIGURE_ARGS=("$@")
94
95 case "$ARCH" in
96 i686|x86_64)
97         ;;
98 *)
99         echo 1>&2 "Unknown ARCH: $ARCH.  Please specify a supported architecture with --arch"
100         exit 1
101         ;;
102 esac
103
104 VERSION=$(tools/get-version-number)
105 DESTDIR=wimlib-${VERSION}-windows-${ARCH}-bin
106 ZIPFILE=$DESTDIR.zip
107 MAKE="make -j$(getconf _NPROCESSORS_ONLN)"
108
109 rm -rf "$DESTDIR" "$ZIPFILE"
110 mkdir "$DESTDIR"
111
112 # Install the required MSYS2 packages if requested.
113 if $INSTALL_MSYS2_PACKAGES; then
114         echo "Installing the MSYS2 packages required to build wimlib ($MSYSTEM)..."
115         pacman -Syu --noconfirm --needed \
116                 autoconf \
117                 automake \
118                 git \
119                 libtool \
120                 make \
121                 "mingw-w64-${ARCH}-gcc" \
122                 pkgconf
123         echo "Done installing the MSYS2 packages required to build wimlib ($MSYSTEM)."
124 fi
125
126 # Bootstrap the repository if not already done.
127 if [ ! -e configure ]; then
128         echo "Bootstrapping the wimlib repository..."
129         ./bootstrap
130 fi
131
132 # Configure wimlib for the given $ARCH if not already done.
133 if ! [ -e config.log ] ||
134         ! grep -q "./configure --host=${ARCH}-w64-mingw32" config.log || \
135         ! grep -q "configure: exit 0" config.log || \
136         [ ${#EXTRA_CONFIGURE_ARGS[@]} -gt 0 ]
137 then
138         # Note: putting -static-libgcc in CC is a workaround for libtool
139         # stripping it:
140         # http://www.gnu.org/software/libtool/manual/libtool.html#Stripped-link-flags
141         echo "Configuring wimlib..."
142         ./configure --host="${ARCH}-w64-mingw32" --disable-static       \
143                 CC="${ARCH}-w64-mingw32-gcc -static-libgcc"     \
144                 "${EXTRA_CONFIGURE_ARGS[@]}"
145         $MAKE clean
146 fi
147
148 echo "Building wimlib..."
149 $MAKE
150
151 echo "Installing binaries..."
152 cp .libs/*.{dll,exe} "$DESTDIR"
153 strip "$DESTDIR"/*.{dll,exe}
154
155 echo "Installing NEWS, README, and licenses..."
156 cp NEWS README* COPYING* "$DESTDIR"
157 sed -n '/^#/q; s/^[\/\* ]*//; p' src/divsufsort.c > "$DESTDIR"/COPYING.libdivsufsort-lite
158 if ! grep -q 'Copyright' "$DESTDIR"/COPYING.libdivsufsort-lite; then
159         echo 1>&2 "ERROR: failed to extract libdivsufsort-lite license text"
160         exit 1
161 fi
162 cd "$DESTDIR"
163 for fil in NEWS README* COPYING*; do
164         sed < "$fil" > "${fil}".txt -e 's/$/\r/g'
165         rm "$fil"
166 done
167 cd ..
168
169 function gen_pdf_from_man_page()
170 {
171         local manbase=$1
172         local pdf=${DESTDIR}/doc/${manbase}.pdf
173
174         echo "Generating $pdf"
175         MANPATH="./doc" man -t "$manbase" | ps2pdf - "$pdf"
176 }
177 if $BUILD_DOCS; then
178         echo "Installing manual pages..."
179         mkdir "$DESTDIR"/doc
180         for fil in ./doc/man1/wim*.1; do
181                 manbase=$(basename "$fil")
182                 cmd=${manbase%.1}
183                 case "$cmd" in
184                 wimlib-imagex|wimmount|wimmountrw|wimunmount)
185                         continue
186                         ;;
187                 esac
188
189                 gen_pdf_from_man_page "$cmd"
190         done
191         gen_pdf_from_man_page wimlib-imagex
192 fi
193
194 echo "Installing wimlib-imagex command shortcut files..."
195 for fil in ./doc/man1/wim*.1; do
196         manbase=$(basename "$fil")
197         cmd=${manbase%.1}
198         sed 's/$/\r/g' > "${DESTDIR}/${cmd}.cmd" <<- EOF
199                 @echo off
200                 "%~dp0\\wimlib-imagex" ${cmd#wim} %*
201         EOF
202 done
203 chmod +x "${DESTDIR}/${cmd}.cmd"
204
205 echo "Installing development files..."
206 mkdir "$DESTDIR"/devel
207 cp .libs/libwim.dll.a "$DESTDIR"/devel/libwim.lib
208 cp include/wimlib.h "$DESTDIR"/devel/
209
210 if $BUILD_ZIP; then
211         echo "Creating zip file..."
212         cd "$DESTDIR"
213         7z -mx9 a ../"$ZIPFILE" . > /dev/null
214         cd ..
215         echo "Success!  Output is in $ZIPFILE"
216 else
217         echo "Success!  Output is in $DESTDIR"
218 fi