Skip to content

Instantly share code, notes, and snippets.

@Quintus
Last active December 30, 2015 14:39
Show Gist options
  • Save Quintus/7843534 to your computer and use it in GitHub Desktop.
Save Quintus/7843534 to your computer and use it in GitHub Desktop.
Fully automated build script to crosscompile SMC from Linux to Windows.
#!/bin/bash
########################################
# Main variables
MXE_URL="git://github.com/Quintus/mxe.git"
SMC_URL="git://github.com/Quintus/SMC.git"
MXE_BRANCH="smc-building"
SMC_BRANCH="mruby"
########################################
# User stuff
#
# You can set the following variables via environment;
# they will not be filled in by asking the user then.
echo "The MXE and SMC target directories you are now"
echo "being asked for do not need to exist; they will"
echo "be created for you."
echo
# $MXE_DIR: The directory where MXE will be downloaded to.
if [ -z "$MXE_DIR" ]; then
echo -n "MXE target directory: "
read -e MXE_DIR
fi
# $SMC_DIR: The directory where SMC will be downloaded to.
if [ -z "$SMC_DIR" ]; then
echo -n "SMC target directory: "
read -e SMC_DIR
fi
echo "=== Summary ==="
echo "MXE target directory: $MXE_DIR"
echo "SMC target directory: $SMC_DIR"
echo
if [ -z "$DONTASK" ]; then
echo "Is this correct?"
select result in "yes" "no"; do
case $result in
yes)
break;;
*)
echo "Aborted by user." >&2
exit 1;;
esac
done
fi
########################################
# Check environment
echo -n "Checking for required programs... "
hash g++ 2>/dev/null || { echo "g++ is not installed" >&2; exit 1; }
hash make 2>/dev/null || { echo "make is not installed" >&2; exit 1; }
hash cmake 2>/dev/null || { echo "cmake is not installed" >&2; exit 1; }
hash git 2>/dev/null || { echo "git is not installed" >&2; exit 1; }
hash mktemp 2>/dev/null || { echo "mktemp is not installed" >&2; exit 1; }
hash pkg-config 2>/dev/null || { echo "pkg-config is not installed" >&2; exit 1; }
hash which 2>/dev/null || { echo "which is not installed" >&2; exit 1; }
hash ruby 2>/dev/null || { echo "ruby is not installed" >&2; exit 1; }
hash rake 2>/dev/null || { echo "rake is not installed" >&2; exit 1; }
hash yacc 2>/dev/null || { echo "yacc is not installed" >&2; exit 1; }
hash gperf 2>/dev/null || { echo "gperf is not installed" >&2; exit 1; }
hash zip 2>/dev/null || { echo "zip is not installed" >&2; exit 1; }
echo "ok"
# Library checking is done by CMake later so we won’t duplicate
# that here.
########################################
# Create our directories
if ! [ -d "$MXE_DIR" ]; then
dir=$(dirname "$MXE_DIR")
mkdir -p "$dir"
echo "Created directory $dir"
fi
if ! [ -d "$SMC_DIR" ]; then
dir=$(dirname "$SMC_DIR")
mkdir -p "$dir"
echo "Created directory $dir"
fi
tmpdir=$(mktemp -d)
cd $tmpdir
########################################
# Download
echo "Cloning sources."
git clone $MXE_URL "$MXE_DIR"
git clone $SMC_URL "$SMC_DIR"
########################################
# Building
# Determine number of processor cores to check how hard
# we can parallize make.
corenum=$(grep 'processor.*:' /proc/cpuinfo | wc -l)
echo "Going to build with up to $corenum tasks in parallel."
sleep 1
echo "--- Building MXE ---"
cd "$MXE_DIR"
# Build the parts of MXE we need
git checkout $MXE_BRANCH
sed -i -e "s/^JOBS.*:=.*$/JOBS := $corenum/" Makefile
make -j$corenum boost pcre cegui libpng freeimage sdl sdl_image sdl_mixer sdl_ttf
if [ $? -ne 0 ]; then
echo "Failed to build MXE." >&2
exit 2
fi
# Work around a bug in CMake that only finds the host pkg-config instead of
# MXE’s (the slash stuff below substitutioes / with \/ for sed)
pkgconfig=$(which pkg-config)
pkgconfig=${pkgconfig//\//\\\/}
cd usr/bin
ln -s i686-pc-mingw32-pkg-config pkg-config
sed -i -e "s/exec pkg-config/exec $pkgconfig/" pkg-config
if [ $? -ne 0 ]; then
echo "Failed to edit MXE's pkg-config." >&2
exit 2
fi
echo "Finished building MXE."
sleep 3
echo "--- Building SMC ---"
# Prepare build tree
cd "$SMC_DIR"
git checkout $SMC_BRANCH
git submodule init
git submodule update
# Make the toolchain file point to our MXE installation
mxe="${MXE_DIR}/usr/i686-pc-mingw32"
mxe=${mxe//\//\\\/}
echo $SMC_DIR
echo $PWD
sed -i -e "s/^set(CMAKE_FIND_ROOT_PATH \".*\")/set(CMAKE_FIND_ROOT_PATH \"$mxe\")/" smc/cmake/toolchains/linux2mingw32.cmake
if [ $? -ne 0 ]; then
echo "Failed to edit SMC's linux2mingw32 toolchain file." >&2
exit 3
fi
# Prepare the environment
export PATH="$MXE_DIR/usr/bin:$PATH"
# Compile!
mkdir smc/crossbuild
cd smc/crossbuild
cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/linux2mingw32.cmake -DCMAKE_INSTALL_PREFIX=$PWD/SMC -DCMAKE_BUILD_TYPE=Debug ..
make -j$corenum
if [ $? -ne 0 ]; then
echo "Failed to build SMC." >&2
exit 3
fi
make install
if [ $? -ne 0 ]; then
echo "Failed to install SMC. But it build correctly!" >&2
exit 4
fi
echo "Completed SMC installation in '$PWD/SMC'"
########################################
# Zip the compilation result up
echo "Zipping the installation up"
smcfilename=SMC-$(git rev-parse --short HEAD).zip
zip -r $smcfilename SMC
if [ $? -ne 0 ]; then
echo "Failed to create a ZIP file from SMC." >&2
echo "You have a complete SMC installation in $PWD/SMC nevertheless." >&2
exit 5
fi
echo
echo "============== DONE =============="
echo "You have a ready-to-distribute SMC installation"
echo "in $PWD/$smcfilename"
echo "and can copy that ZIP file whereever you want."
# Cleanup
cd $tmpdir
cd ..
rm -rf $tmpdir
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment