#!/bin/bash
#
-# This script prepares a Windows binary distribution of wimlib on Linux using
-# MinGW-w64. The desired architecture must be passed as the first argument.
-
-set -e
-
-if [ ! -e src/wim.c ]; then
- echo "This script must be run from the toplevel directory" 1>&2
- exit 1
+# This script prepares a Windows binary distribution of wimlib.
+
+set -e -u
+
+SCRIPTNAME="$0"
+TOPDIR=$(dirname "$(dirname "$(realpath "$0")")")
+cd "$TOPDIR" # Top-level directory of the git repo
+
+MSYSTEM=${MSYSTEM:-}
+if [ -z "$MSYSTEM" ]; then
+ ARCH=x86_64
+else
+ case "$MSYSTEM" in
+ MINGW32)
+ ARCH=i686
+ ;;
+ MINGW64)
+ ARCH=x86_64
+ ;;
+ *)
+ echo 1>&2 "Unsupported MSYS2 environment: $MSYSTEM"
+ echo 1>&2 "In MSYS2, you must use the MINGW32 or MINGW64 environment."
+ echo 1>&2 "See https://www.msys2.org/docs/environments/"
+ exit 1
+ esac
fi
+INSTALL_MSYS2_PACKAGES=false
+BUILD_DOCS=true
+BUILD_ZIP=true
+
+longopts="help"
+longopts+=",arch:"
+longopts+=",install-msys2-packages"
+longopts+=",no-docs"
+longopts+=",no-zip"
+
+usage()
+{
+ cat << EOF
+Usage: $SCRIPTNAME [OPTION]... [EXTRA_CONFIGURE_ARG]...
+Options:
+ --arch=ARCH Specify the architecture. The default is taken
+ from MSYSTEM if available, otherwise is x86_64.
+ --install-msys2-packages Install prerequisite MSYS2 packages
+ --no-docs Don't build and install PDF manual pages
+ --no-zip Don't create the final zip file
+EOF
+}
-if [ $# -lt 1 ]; then
- echo "Usage: $0 i686|x86_64 [EXTRA_CONFIGURE_ARG]..." 1>&2
+if ! options=$(getopt -o "" -l "$longopts" -- "$@"); then
+ usage 1>&2
exit 1
fi
+eval set -- "$options"
+while true; do
+ case "$1" in
+ --help)
+ usage
+ exit 0
+ ;;
+ --arch)
+ ARCH=$2
+ shift
+ ;;
+ --install-msys2-packages)
+ if [ -z "$MSYSTEM" ]; then
+ echo 1>&2 "--install-msys2-packages is not allowed outside MSYS2."
+ exit 1
+ fi
+ INSTALL_MSYS2_PACKAGES=true
+ ;;
+ --no-docs)
+ BUILD_DOCS=false
+ ;;
+ --no-zip)
+ BUILD_ZIP=false
+ ;;
+ --)
+ shift
+ break
+ ;;
+ *)
+ echo 1>&2 "Invalid option '$1'"
+ usage 1>&2
+ exit 1
+ ;;
+ esac
+ shift
+done
-ARCH="$1"
-shift
+EXTRA_CONFIGURE_ARGS=("$@")
case "$ARCH" in
i686|x86_64)
;;
*)
- echo "ERROR: ARCH must be i686 or x86_64" 1>&2
+ echo 1>&2 "Unknown ARCH: $ARCH. Please specify a supported architecture with --arch"
exit 1
;;
esac
-VERSION=$(grep 'AC_INIT' configure.ac | \
- grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+[^]]*')
+VERSION=$(tools/get-version-number)
DESTDIR=wimlib-${VERSION}-windows-${ARCH}-bin
-ZIPFILE=wimlib-${VERSION}-windows-${ARCH}-bin.zip
-MAKE="make -j $(grep -c processor /proc/cpuinfo)"
-WINDEPDIR=./tools/windeps
-SYSROOT=$WINDEPDIR/sysroot_${ARCH}
-
-# Prepare third party libraries
-
-if [ ! -e $SYSROOT ]; then
- $MAKE -C $WINDEPDIR sysroot_${ARCH}
+ZIPFILE=$DESTDIR.zip
+MAKE="make -j$(getconf _NPROCESSORS_ONLN)"
+
+rm -rf "$DESTDIR" "$ZIPFILE"
+mkdir "$DESTDIR"
+
+# Install the required MSYS2 packages if requested.
+if $INSTALL_MSYS2_PACKAGES; then
+ echo "Installing the MSYS2 packages required to build wimlib ($MSYSTEM)..."
+ pacman -Syu --noconfirm --needed \
+ autoconf \
+ automake \
+ git \
+ libtool \
+ make \
+ "mingw-w64-${ARCH}-gcc" \
+ pkgconf
+ echo "Done installing the MSYS2 packages required to build wimlib ($MSYSTEM)."
fi
-# Compile wimlib
+# Bootstrap the repository if not already done.
+if [ ! -e configure ]; then
+ echo "Bootstrapping the wimlib repository..."
+ ./bootstrap
+fi
-if ! grep -q "./configure --host=${ARCH}-w64-mingw32" config.log || \
+# Configure wimlib for the given $ARCH if not already done.
+if ! [ -e config.log ] ||
+ ! grep -q "./configure --host=${ARCH}-w64-mingw32" config.log || \
! grep -q "configure: exit 0" config.log || \
- [ $# -gt 0 ]
+ [ ${#EXTRA_CONFIGURE_ARGS[@]} -gt 0 ]
then
- extra_args=
- if [ $ARCH = x86_64 ]; then
- extra_args="--enable-ssse3-sha1"
- fi
# Note: putting -static-libgcc in CC is a workaround for libtool
# stripping it:
# http://www.gnu.org/software/libtool/manual/libtool.html#Stripped-link-flags
- ./configure --host=${ARCH}-w64-mingw32 --disable-static \
- CC="${ARCH}-w64-mingw32-gcc -static-libgcc" \
- CPPFLAGS="-I$SYSROOT/include" \
- LDFLAGS="-L$SYSROOT/lib" \
- PKG_CONFIG_PATH="$SYSROOT/lib/pkgconfig" \
- --without-libcrypto \
- $extra_args "$@"
+ echo "Configuring wimlib..."
+ ./configure --host="${ARCH}-w64-mingw32" --disable-static \
+ CC="${ARCH}-w64-mingw32-gcc -static-libgcc" \
+ "${EXTRA_CONFIGURE_ARGS[@]}"
$MAKE clean
fi
-$MAKE
-
-# Create empty destination directory
-
-rm -rf $DESTDIR
-mkdir $DESTDIR
-# Install binaries
-
-cp .libs/*.{dll,exe} $DESTDIR
-${ARCH}-w64-mingw32-strip $DESTDIR/*.{dll,exe}
-
-# Install text files
+echo "Building wimlib..."
+$MAKE
-cp NEWS README* COPYING* $DESTDIR
-cp $WINDEPDIR/COPYING* $DESTDIR
+echo "Installing binaries..."
+cp .libs/*.{dll,exe} "$DESTDIR"
+strip "$DESTDIR"/*.{dll,exe}
-sed -n '/^#/q; s/^[\/\* ]*//; p' src/divsufsort.c > $DESTDIR/COPYING.libdivsufsort-lite
-if ! grep -q 'Copyright' $DESTDIR/COPYING.libdivsufsort-lite; then
- echo "ERROR: failed to extract libdivsufsort-lite license text" 1>&2
+echo "Installing NEWS, README, and licenses..."
+cp NEWS README* COPYING* "$DESTDIR"
+sed -n '/^#/q; s/^[\/\* ]*//; p' src/divsufsort.c > "$DESTDIR"/COPYING.libdivsufsort-lite
+if ! grep -q 'Copyright' "$DESTDIR"/COPYING.libdivsufsort-lite; then
+ echo 1>&2 "ERROR: failed to extract libdivsufsort-lite license text"
exit 1
fi
-(
- cd $DESTDIR
- for fil in NEWS README* COPYING*; do
- sed < $fil > ${fil}.txt -e 's/$/\r/g'
- rm $fil
- done
-)
-
-
-# Install man pages
-
-mkdir $DESTDIR/doc
+cd "$DESTDIR"
+for fil in NEWS README* COPYING*; do
+ sed < "$fil" > "${fil}".txt -e 's/$/\r/g'
+ rm "$fil"
+done
+cd ..
+
+IMAGEX_CMDS=()
+for cmd in ./doc/man1/wim*.1; do
+ cmd=${cmd##*/}
+ cmd=${cmd%.1}
+ case "$cmd" in
+ wimlib-imagex|wimmount|wimmountrw|wimunmount)
+ ;;
+ *)
+ IMAGEX_CMDS+=("$cmd")
+ ;;
+ esac
+done
-function gen_pdf_from_man_page() {
- local manbase=$1
- local pdf=${DESTDIR}/doc/${manbase}.pdf
+function gen_pdf_from_man_page()
+{
+ local cmd=$1
+ local pdf=${DESTDIR}/doc/${cmd}.pdf
echo "Generating $pdf"
-
- MANPATH="./doc" man -t $manbase | ps2pdf - $pdf
+ MANPATH="./doc" man -t "$cmd" | ps2pdf - "$pdf"
}
-for fil in ./doc/man1/wimlib-imagex-*.1; do
- manbase=`basename $fil`
- manbase=${manbase%%.1}
- cmd=$(echo $manbase | sed s/wimlib-imagex-//)
- if [ $cmd == mount -o $cmd == mountrw -o $cmd == unmount ]; then
- continue
- fi
-
- gen_pdf_from_man_page $manbase
+if $BUILD_DOCS; then
+ echo "Installing manual pages..."
+ mkdir "$DESTDIR"/doc
+ for cmd in "${IMAGEX_CMDS[@]}"; do
+ gen_pdf_from_man_page "$cmd"
+ done
+ gen_pdf_from_man_page wimlib-imagex
+fi
- sed 's/$/\r/g' > ${DESTDIR}/wim${cmd}.cmd <<- EOF
+echo "Installing wim*.cmd files..."
+for cmd in "${IMAGEX_CMDS[@]}"; do
+ sed 's/$/\r/g' > "${DESTDIR}/${cmd}.cmd" <<- EOF
@echo off
- "%~dp0\\wimlib-imagex" $cmd %*
+ "%~dp0\\wimlib-imagex" ${cmd#wim} %*
EOF
- chmod +x ${DESTDIR}/wim${cmd}.cmd
+ chmod +x "${DESTDIR}/${cmd}.cmd"
done
-gen_pdf_from_man_page wimlib-imagex
-
-# Generate ZIP file
-
-rm -f $ZIPFILE
-(
- dir=$PWD
- cd $DESTDIR
- 7z -mx9 a "$dir/$ZIPFILE" .
-)
+echo "Installing development files..."
+mkdir "$DESTDIR"/devel
+cp .libs/libwim.dll.a "$DESTDIR"/devel/libwim.lib
+cp include/wimlib.h "$DESTDIR"/devel/
+
+if $BUILD_ZIP; then
+ echo "Creating zip file..."
+ cd "$DESTDIR"
+ 7z -mx9 a ../"$ZIPFILE" . > /dev/null
+ cd ..
+ echo "Success! Output is in $ZIPFILE"
+else
+ echo "Success! Output is in $DESTDIR"
+fi