#!/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
+ CC_PKG=mingw-w64-i686-gcc
+ ;;
+ MINGW64)
+ ARCH=x86_64
+ CC_PKG=mingw-w64-x86_64-gcc
+ ;;
+ CLANG32)
+ ARCH=i686
+ CC_PKG=mingw-w64-clang-i686-clang
+ ;;
+ CLANG64)
+ ARCH=x86_64
+ CC_PKG=mingw-w64-clang-x86_64-clang
+ ;;
+ CLANGARM64)
+ ARCH=aarch64
+ CC_PKG=mingw-w64-clang-aarch64-clang
+ ;;
+ *)
+ echo 1>&2 "Unsupported MSYS2 environment: $MSYSTEM. This script supports"
+ echo 1>&2 "MINGW32, MINGW64, CLANG32, CLANG64, and CLANGARM64."
+ echo 1>&2 "See https://www.msys2.org/docs/environments/"
+ exit 1
+ esac
fi
+INCLUDE_DOCS=false
+INSTALL_MSYS2_PACKAGES=false
+SKIP_CONFIGURE=false
+ZIP=false
+
+longopts="help"
+longopts+=",arch:"
+longopts+=",include-docs"
+longopts+=",install-msys2-packages"
+longopts+=",skip-configure"
+longopts+=",zip"
+
+usage()
+{
+ cat << EOF
+Usage: $SCRIPTNAME [OPTION]... [EXTRA_CONFIGURE_ARG]...
+Options:
+ --arch=ARCH Specify the CPU architecture. This is unnecessary
+ when using MSYS2.
+
+ --include-docs Build and install the PDF manual pages.
+
+ --install-msys2-packages Install the MSYS2 packages needed to build wimlib.
+ You can omit this if you have already done this for
+ the same MSYS2 environment.
+
+ --skip-configure Skip running the configure script again. You can
+ use this to save time in incremental builds if you
+ are sure you didn't change any options.
+
+ --zip Zip the output files up into a zip file.
+EOF
+}
-if [ $# -ne 1 ]; then
- echo "Usage: $0 i686|x86_64" 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
+ ;;
+ --include-docs)
+ INCLUDE_DOCS=true
+ ;;
+ --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
+ ;;
+ --skip-configure)
+ SKIP_CONFIGURE=true
+ ;;
+ --zip)
+ ZIP=true
+ ;;
+ --)
+ 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)
+i686|x86_64|aarch64)
;;
*)
- 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 $MSYSTEM packages needed to build wimlib..."
+ pacman -Syu --noconfirm --needed \
+ autoconf \
+ automake \
+ git \
+ libtool \
+ make \
+ "$CC_PKG" \
+ pkgconf
+ echo "Done installing the MSYS2 $MSYSTEM packages needed to build wimlib."
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 || \
- ! grep -q "configure: exit 0" config.log
-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:
+# Configure wimlib.
+if ! [ -e config.log ] || ! $SKIP_CONFIGURE; then
+ echo "Configuring wimlib..."
+ configure_args=("--host=${ARCH}-w64-mingw32")
+ configure_args+=("--disable-static")
+ # -static-libgcc is needed with gcc. It should go in the CFLAGS, but
+ # libtool strips it, so it must go directly in CC instead. See
# 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
+ if "${ARCH}-w64-mingw32-cc" --version | grep -q '(GCC)'; then
+ configure_args+=("CC=${ARCH}-w64-mingw32-cc -static-libgcc")
+ fi
+ configure_args+=("${EXTRA_CONFIGURE_ARGS[@]}")
+ ./configure "${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
-
-cp NEWS README* COPYING* $DESTDIR
-cp $WINDEPDIR/COPYING* $DESTDIR
-(
- cd $DESTDIR
- for fil in NEWS README* COPYING*; do
- sed < $fil > ${fil}.txt -e 's/$/\r/g'
- rm $fil
- done
-)
+echo "Building wimlib..."
+$MAKE
-# Install man pages
+echo "Installing binaries..."
+cp .libs/*.{dll,exe} "$DESTDIR"
+strip "$DESTDIR"/*.{dll,exe}
-mkdir $DESTDIR/doc
+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
+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 $INCLUDE_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 $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