visualboyadvance-m/tools/macOS/builder

259 lines
9.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
LANG=C LC_COLLATE=C LC_CTYPE=C LC_MESSAGES=C LC_MONETARY=C LC_NUMERIC=C LC_TIME=C LC_ALL=C
export LANG LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME LC_ALL
# bash 3 does not work for this code
if [ -z "$IN_DASH" ]; then
if command -v dash >/dev/null; then
export IN_DASH=1
exec dash "$0" "$@"
else
echo >&2 "Please install dash from Nix/Homebrew to run this script."
exit 1
fi
fi
target_bits=64
target_build_arch=-m64
if [ "$(uname -m)" = arm64 ]; then
export APPLE_SILICON=1
target_cpu=ARM64
else
target_cpu=x86_64
fi
case "$1" in
-[Ii][Nn][Tt][Ee][Ll])
intel_target=1
shift
;;
-[Ii][Nn][Tt][Ee][Ll]64)
intel_target=1
shift
;;
-[Xx]86_64)
intel_target=1
shift
;;
-64)
shift
;;
-[Aa][Rr][Mm]64)
shift
;;
-32)
target_build_arch=-m32
target_cpu=i386
shift
;;
esac
if [ -n "$APPLE_SILICON" ]; then
if [ -n "$intel_target" ]; then
target_build_arch='-target x86_64-apple-macos10.15 -march=core2 -mtune=skylake'
target_cpu=x86_64
export MACOSX_DEPLOYMENT_TARGET=10.15 # Catalina
else
export MACOSX_DEPLOYMENT_TARGET=11.0 # Big Sur
fi
elif [ "$target_cpu" = x86_64 ]; then
target_build_arch='-m64 -march=core2 -mtune=skylake'
fi
# Need to use Xcode 9 for 32 bit builds on Mojave and newer.
# Place it in /Applications/Xcode9.app .
if [ "$target_cpu" = i386 ] && [ -d /Applications/Xcode9.app ]; then
PREV_XCODE=$(xcode-select -p)
printf "\nSetting Xcode9 as the default Xcode for 32 bit build...\n\n"
sudo xcode-select -s /Applications/Xcode9.app/Contents/Developer
fi
if command -v brew >/dev/null; then
export BREW_PREFIX=$(brew --prefix)
elif [ -f /usr/local/bin/brew ]; then
export BREW_PREFIX=$(/usr/local/bin/brew --prefix)
elif [ -f /opt/homebrew/bin/brew ]; then
export BREW_PREFIX=$(/opt/homebrew/bin/brew --prefix)
fi
export BUILD_ROOT="${BUILD_ROOT:-$HOME/vbam-build-mac-${target_cpu}}$BUILD_ROOT_SUFFIX"
ver_file=$(mktemp)
sw_vers -productVersion | sed 's/\./ /g' > "$ver_file"
read -r macos_major macos_minor macos_patch < "$ver_file"
rm -f "$ver_file"
# Find the highest version clang and llvm in Nix or Homebrew.
best_llvm=$(
(
for nix_clang in $(find /nix/store -maxdepth 1 -type d -name '*-clang-[0-9]*[0-9]'); do
llvm_ver=$(echo "$nix_clang" | sed -E 's/.*-([0-9][0-9.]*[0-9])$/\1/')
nix_llvm=$(find /nix/store -maxdepth 1 -type d -name '*-llvm-'"$llvm_ver")
if [ -x "$nix_clang/bin/clang++" ]; then
echo "$nix_clang:$nix_llvm $($nix_clang/bin/clang++ --version | head -1 | awk '{ print $NF }')"
fi
done
for brew_llvm in $(find "$BREW_PREFIX"/Cellar -maxdepth 1 -type l -name 'llvm*'); do
if [ -x "$brew_llvm/bin/clang++" ]; then
echo "$brew_llvm $($brew_llvm/bin/clang++ --version | head -1 | awk '{ print $NF }')"
fi
done
) | sort -k2,2 -V -r | head -1 | awk '{ print $1 }'
)
BUILD_ENV=$(cat <<EOF
export MACOSX_DEPLOYMENT_TARGET=$MACOSX_DEPLOYMENT_TARGET
export COMMAND_MODE=unix2003
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:"$BREW_PREFIX"/bin
[ -n "$best_llvm" ] && export PATH="$best_llvm/bin:\$PATH"
export CC=clang
export CXX=clang++
export CPPFLAGS=-DICONV_CONST=
export CFLAGS="$target_build_arch -framework Carbon -framework Foundation -framework CoreServices -framework SystemConfiguration -Wno-unused-command-line-argument -DICONV_CONST= -Wl,-no_compact_unwind"
export CXXFLAGS="$target_build_arch -framework Carbon -framework Foundation -framework CoreServices -framework SystemConfiguration -Wno-unused-command-line-argument -DICONV_CONST= -Wl,-no_compact_unwind"
export OBJCXXFLAGS="$target_build_arch -framework Carbon -framework Foundation -framework CoreServices -framework SystemConfiguration -Wno-unused-command-line-argument -DICONV_CONST= -Wl,-no_compact_unwind"
export LDFLAGS="$target_build_arch -framework Carbon -framework Foundation -framework CoreServices -framework SystemConfiguration -Wno-unused-command-line-argument -Wl,-no_compact_unwind"
export UUID_CFLAGS="-I\$BUILD_ROOT/root/stow/libuuid/include"
export UUID_LIBS="-L\$BUILD_ROOT/root/stow/libuuid/lib -luuid"
EOF
)
export BUILD_ENV
export TAR=tar
if [ "$target_cpu" = i386 ]; then
export CONFIGURE_REQUIRED_ARGS='--host=i386-apple-darwin --build=x86_64-apple-darwin'
fi
. "$(dirname "$0")/../builder/core.sh"
# Remove -lm, macOS does not use it and it can cause build failures.
BUILD_ENV=$(printf '%s' "$BUILD_ENV" | sed 's/ -lm / /g')
export BUILD_ENV
for dist in flex libsecret c2man graphviz zip; do
table_line_remove DISTS "$dist"
done
# issues with perl modules linked to our libs and brew perl
table_line_remove DISTS shared-mime-info
table_line_replace DIST_PREFIX libuuid /usr/stow/libuuid
table_line_append DIST_PRE_BUILD zip " \
sed -i.bak 's/-DZMEM//g; s/-DNO_DIR//g;' unix/configure \
"
if [ "$target_cpu" = i386 ]; then
table_line_replace DIST_CONFIGURE_OVERRIDES openssl './Configure darwin-i386-cc no-shared --prefix=/usr --openssldir=/etc/ssl'
elif [ "$target_cpu" = ARM64 ]; then
table_line_replace DIST_CONFIGURE_OVERRIDES openssl "./Configure darwin64-arm64-cc no-shared --prefix=/usr --openssldir=/etc/ssl"
else
table_line_replace DIST_CONFIGURE_OVERRIDES openssl "./Configure darwin64-${target_cpu}-cc no-shared --prefix=/usr --openssldir=/etc/ssl"
fi
stdint_h=
if [ -f "$(xcode-select -p)/SDKs/MacOSX.sdk/usr/include/stdint.h" ]; then
stdint_h="\"$(xcode-select -p)\"/SDKs/MacOSX.sdk/usr/include/stdint.h"
elif [ -f "$(xcode-select -p)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdint.h" ]; then
stdint_h="\"$(xcode-select -p)\"/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdint.h"
else
die "Can't find macOS SDK 'stdint.h'"
fi
table_line_append DIST_EXTRA_CFLAGS libogg "-include $stdint_h"
table_line_append DIST_EXTRA_CFLAGS libvorbis "-include $stdint_h"
table_line_append DIST_EXTRA_CFLAGS libtheora "-include $stdint_h"
table_line_append DIST_EXTRA_CFLAGS ffmpeg "-include $stdint_h"
table_line_append DIST_MAKE_ARGS unzip "LD=clang"
# -Wl,-no_compact_unwind must be passed in LDFLAGS to openssl
table_line_append DIST_MAKE_ARGS openssl "LDFLAGS=\"\$LDFLAGS\""
# these are only applicable for cross-compiling to 32 bits
if [ "$target_cpu" = i386 ]; then
# some dists will not cross compile without a CONFIG_SITE
table_line_append DIST_ARGS pkgconfig '--host= --build='
table_line_append DIST_ARGS docbook2x '--host= --build='
# python does not support cross-compiling to 32bits
table_line_append DIST_ARGS python2 '--host= --build='
table_line_append DIST_ARGS python3 '--host= --build='
table_line_append DIST_ARGS sfml '-DCMAKE_OSX_ARCHITECTURES=i386'
table_line_append DIST_PRE_BUILD sfml " \
sed -i.bak '/FATAL_ERROR \"Only 64-bit architecture is supported/d' CMakeLists.txt; \
"
table_line_append DIST_ARGS libicu '--host= --build='
fi
table_line_remove DISTS ninja
table_line_replace DIST_CONFIGURE_TYPES libmodplug autoreconf
table_line_append DIST_PRE_BUILD libmodplug " \
sed -i.bak '/-mmacosx-version-min=/d' configure.ac; \
sed -i.bak 's/-lstdc++/-lc++/g' libmodplug.pc.in; \
"
if [ -z "$APPLE_SILICON" ]; then
table_line_replace DISTS glib 'https://download.gnome.org/sources/glib/2.78/glib-2.78.3.tar.xz lib/libglib-2.0.a'
fi
table_line_append DIST_PRE_BUILD libzmq "sed -i.bak 's/-lstdc++/-lc++/g' src/libzmq.pc.in"
table_line_append DIST_PRE_BUILD ffmpeg "sed -i.bak 's/-lstdc++/-lc++/g' configure"
# For a 10.7 target:
#table_line_replace DISTS wxwidgets "https://github.com/wxWidgets/wxWidgets/releases/download/v3.0.5.1/wxWidgets-3.0.5.1.tar.bz2 lib/libwx_baseu-3.0*.a"
# For 10.7 add --disable-stl
table_line_append DIST_ARGS wxwidgets "--with-macosx-version-min=\$MACOSX_DEPLOYMENT_TARGET"
table_line_append DIST_ARGS libmodplug "CC=clang++ CXX=clang++"
table_line_append DIST_PRE_BUILD libvpx " \
sed -E -i.bak 's/(-mmacosx-version-min=)[[:digit:].]+/\1'\$MACOSX_DEPLOYMENT_TARGET'/g' build/make/configure.sh; \
"
table_line_append DIST_CONFIGURE_OVERRIDES ffmpeg "--disable-videotoolbox --extra-ldflags='-framework CoreText'"
#table_line_append DIST_PATCHES ffmpeg "-p0 https://gist.githubusercontent.com/rkitover/db75d083b74617b186eec11965c1da74/raw/20da2f8c6d8c02ce284096c5e805ae671351cc6e/ffmpeg-macos10-7.patch"
table_line_remove DISTS python2
table_line_append DIST_PRE_BUILD sfml " \
sed -E -i.bak '/OSX_DEPLOYMENT_TARGET/d' CMakeLists.txt; \
"
if [ -n "$APPLE_SILICON" ] && [ "$target_cpu" != ARM64 ]; then
table_line_remove DISTS m4
table_line_remove DISTS XML-Parser
table_line_remove DISTS libffi
table_line_remove DISTS libgcrypt
table_line_append DIST_CONFIGURE_OVERRIDES ffmpeg '--disable-asm'
fi
builder "$@"
if [ -n "$PREV_XCODE" ]; then
printf "\nRe-setting '$PREV_XCODE' as the default Xcode...\n\n"
sudo xcode-select -s "$PREV_XCODE"
fi