#!/bin/bash
#
# You can use this to build wxWidgets.  Just run it from within the root of the
# wxWidgets source tree like so:
#
#  sudo <path to this script>/build_wxwidgets
#

# The minimum OS X version supported by Audacity is 10.7.
minver=10.7

# If you want to use a specific SDK, specify the full path here
sdkdir=""

# Build a specific configuration
function bld
{
   flavour="${1}"
   shift

   # Create and change to our build directory
   rm -rf "bld_${flavour}_${arch}"
   mkdir -p "bld_${flavour}_${arch}"
   pushd "bld_${flavour}_${arch}"

   # Force Audacity specific options
   export CXX="g++ -std=c++1z -stdlib=libc++"
   export LD="g++ -std=c++1z -stdlib=libc++"

   # NOTES: liblzma isn't available on MacOS 10.8 or older and Audacity doesn't
   #        need it.  So, build wxWidgets without the support to allow Audacity
   #        to run on MacOS 10.7 or newer.
   ../configure --prefix="/usr/local/${arch}" \
                --enable-macosx-arch="${arch}" \
                --enable-shared=yes \
                --enable-unicode=yes \
                --enable-universal_binary=no \
                --enable-webkit=no \
                --with-expat=builtin \
                --with-flavour="${flavour}" \
                --with-libjpeg=builtin \
                --with-libpng=builtin \
                --with-libtiff=builtin \
                --with-macosx-sdk="${sdkdir}" \
                --with-macosx-version-min="${minver}" \
                --with-regex=builtin \
                 --with-zlib=builtin \
                --without-liblzma \
                ${@}
   make -j $(sysctl -n hw.ncpu) install
   popd
}

# Only build 32-bit version if running on 10.14 or older and
# using the 10.13 SDK or older.
arches="x86_64"
osver="$(sw_vers -productVersion)"
sdkver="$(xcrun --show-sdk-version)"
if [ "${osver}" \< "10.15" -a "${sdkver}" \< "10.14" ]
then
   arches="${arches} i386"
fi

for arch in ${arches}
do
   bld "debug" --enable-debug=yes
   bld "release" --enable-debug=no
done