Skip to content

Instantly share code, notes, and snippets.

@kilbith
Created January 22, 2023 19:31
Show Gist options
  • Save kilbith/2d218c27d0e1e4a1fea865a305d6cac2 to your computer and use it in GitHub Desktop.
Save kilbith/2d218c27d0e1e4a1fea865a305d6cac2 to your computer and use it in GitHub Desktop.
Improved MinGW Build Script
#!/bin/bash
set -e
clear
GITURL=https://github.com/minetest
CORE_GIT=$GITURL/minetest
CORE_BRANCH=master
CORE_NAME=minetest
IRRLICHT_GIT=$GITURL/irrlicht
IRRLICHT_BRANCH=master
IRRLICHT_NAME=irrlicht
compiler=x86_64-w64-mingw32-gcc-posix
command -v $compiler >/dev/null
if [ $? -ne 0 ]
then
echo "$compiler compiler must be installed"
exit 1
fi
toolchain_file=$topdir/toolchain.cmake
topdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ $# -ne 1 ]; then
echo "Usage: $0 <build directory>"
exit 1
fi
# Create directories
workdir=$1
mkdir -p $workdir
workdir="$( cd "$workdir" && pwd )"
sourcedir=$workdir/src
mkdir -p $sourcedir
builddir=$workdir/build
mkdir -p $builddir
tmpdir=$workdir/tmp
mkdir -p $tmpdir
# LibJpeg (Irrlicht dependency)
JPEG_SRC="$sourcedir/jpeg-9"
JPEG_INC="$JPEG_SRC"
JPEG_LIB="$JPEG_SRC/.libs/libjpeg.dll.a"
if [ ! -f "$JPEG_LIB" ]; then
cd "$sourcedir"
wget http://www.ijg.org/files/jpegsrc.v9.tar.gz -O "$tmpdir/temp.tar.gz"
tar xf $tmpdir/temp.tar.gz
rm $tmpdir/temp.tar.gz
cd $JPEG_SRC
CC=$compiler ./configure --prefix=/usr/x86_64-w64-mingw32 --host=x86_64-w64-mingw32
make -j$(nproc)
if [ $? -ne 0 ]; then
echo "Faild to build libjpeg"
exit 1
fi
fi
# LibPng (Irrlicht dependency)
PNG_SRC="$sourcedir/libpng-1.6.38"
PNG_INC="$PNG_SRC"
PNG_LIB="$PNG_SRC/.libs/libpng16.dll.a"
if [ ! -f "$PNG_LIB" ]; then
cd "$sourcedir"
wget https://sourceforge.net/projects/libpng/files/libpng16/1.6.38/libpng-1.6.38.tar.gz -O "$tmpdir/temp.tar.gz"
tar xf "$tmpdir/temp.tar.gz"
rm "$tmpdir/temp.tar.gz"
cd "$PNG_SRC"
CC=$compiler ./configure --prefix=/usr/x86_64-w64-mingw32 --host=x86_64-w64-mingw32
make -j$(nproc)
if [ $? -ne 0 ]; then
echo "Faild to build libpng"
exit 1
fi
fi
# Build Irrlicht
cd $sourcedir
[ -d $IRRLICHT_NAME ] && { pushd $IRRLICHT_NAME; git config pull.rebase true; git pull; popd; } ||
git clone -q --depth 1 -b $IRRLICHT_BRANCH $IRRLICHT_GIT $IRRLICHT_NAME
cmake -S $sourcedir/$IRRLICHT_NAME -B $builddir/$IRRLICHT_NAME \
-DCMAKE_TOOLCHAIN_FILE=$topdir/$toolchain_file \
-DBUILD_SHARED_LIBS=ON \
-DJPEG_INCLUDE_DIR=$JPEG_INC \
-DJPEG_LIBRARY=$JPEG_LIB \
-DPNG_PNG_INCLUDE_DIR=$PNG_INC \
-DPNG_LIBRARY=$PNG_LIB \
cmake --build $builddir/$IRRLICHT_NAME -j$(nproc)
libdir=$workdir/libs
mkdir -p $libdir
# Get some more libs
ogg_version=1.3.2
vorbis_version=1.3.5
curl_version=7.65.3
gettext_version=0.20.1
freetype_version=2.11.1
sqlite3_version=3.27.2
zstd_version=1.5.2
luajit_version=2.1.0-beta3
zlib_version=1.2.11
download () {
local url="$1"
local foldername="$2"
[ -d "$foldername" ] && return 0
echo "Downloading $1"
wget "$url" -O "$tmpdir/temp.zip"
unzip -q -o "$tmpdir/temp.zip" -d "$foldername"
rm "$tmpdir/temp.zip"
}
download "http://minetest.kitsunemimi.pw/zlib-$zlib_version-win64.zip" "$libdir/zlib"
download "http://minetest.kitsunemimi.pw/zstd-$zstd_version-win64.zip" "$libdir/zstd"
download "http://minetest.kitsunemimi.pw/libogg-$ogg_version-win64.zip" "$libdir/libogg"
download "http://minetest.kitsunemimi.pw/libvorbis-$vorbis_version-win64.zip" "$libdir/libvorbis"
download "http://minetest.kitsunemimi.pw/curl-$curl_version-win64.zip" "$libdir/curl"
download "http://minetest.kitsunemimi.pw/gettext-$gettext_version-win64.zip" "$libdir/gettext"
download "http://minetest.kitsunemimi.pw/freetype2-$freetype_version-win64.zip" "$libdir/freetype"
download "http://minetest.kitsunemimi.pw/sqlite3-$sqlite3_version-win64.zip" "$libdir/sqlite3"
download "http://minetest.kitsunemimi.pw/openal_stripped64.zip" "$libdir/openal"
download "http://minetest.kitsunemimi.pw/luajit-$luajit_version-win64.zip" "$libdir/luajit"
# Build core
cd $sourcedir
[ -d $CORE_NAME ] && { pushd $CORE_NAME; git config pull.rebase true; git pull; popd; } ||
git clone -q --depth 1 -b $CORE_BRANCH $CORE_GIT $CORE_NAME
rm -f $sourcedir/$CORE_NAME/lib/irrlichtmt
ln -s $sourcedir/$IRRLICHT_NAME $sourcedir/$CORE_NAME/lib/irrlichtmt
vorbis_dlls=$(echo $libdir/libvorbis/bin/libvorbis{,file}-*.dll | tr ' ' ';')
gettext_dlls=$(echo $libdir/gettext/bin/lib{intl,iconv}-*.dll | tr ' ' ';')
cmake -S $sourcedir/$CORE_NAME -B $builddir/$CORE_NAME \
-DCMAKE_TOOLCHAIN_FILE=$topdir/$toolchain_file \
-DCMAKE_INSTALL_PREFIX=/tmp \
-DVERSION_EXTRA=$git_hash \
-DBUILD_CLIENT=1 -DBUILD_SERVER=0 \
-DRUN_IN_PLACE=1 \
\
-DENABLE_SOUND=1 \
-DENABLE_CURL=1 \
-DENABLE_GETTEXT=1 \
-DENABLE_LEVELDB=0 \
\
-DCMAKE_PREFIX_PATH=$sourcedir/$IRRLICHT_NAME \
-DIRRLICHT_DLL="$builddir/$IRRLICHT_NAME/lib/Win32-gcc/libIrrlichtMt.dll.a" \
\
-DZLIB_INCLUDE_DIR=$libdir/zlib/include \
-DZLIB_LIBRARY=$libdir/zlib/lib/libz.dll.a \
-DZLIB_DLL=$libdir/zlib/bin/zlib1.dll \
\
-DJPEG_INCLUDE_DIR=$JPEG_INC \
-DJPEG_LIBRARY=$JPEG_LIB \
-DPNG_PNG_INCLUDE_DIR=$PNG_INC \
-DPNG_LIBRARY=$PNG_LIB \
\
-DZSTD_INCLUDE_DIR=$libdir/zstd/include \
-DZSTD_LIBRARY=$libdir/zstd/lib/libzstd.dll.a \
-DZSTD_DLL=$libdir/zstd/bin/libzstd.dll \
\
-DLUA_INCLUDE_DIR=$libdir/luajit/include \
-DLUA_LIBRARY=$libdir/luajit/libluajit.a \
\
-DOGG_INCLUDE_DIR=$libdir/libogg/include \
-DOGG_LIBRARY=$libdir/libogg/lib/libogg.dll.a \
-DOGG_DLL=$libdir/libogg/bin/libogg-0.dll \
\
-DVORBIS_INCLUDE_DIR=$libdir/libvorbis/include \
-DVORBIS_LIBRARY=$libdir/libvorbis/lib/libvorbis.dll.a \
-DVORBIS_DLL="$vorbis_dlls" \
-DVORBISFILE_LIBRARY=$libdir/libvorbis/lib/libvorbisfile.dll.a \
\
-DOPENAL_INCLUDE_DIR=$libdir/openal/openal_stripped/include/AL \
-DOPENAL_LIBRARY=$libdir/openal/openal_stripped/lib/libOpenAL32.dll.a \
-DOPENAL_DLL=$libdir/openal/openal_stripped/bin/OpenAL32.dll \
\
-DCURL_DLL=$libdir/curl/bin/libcurl-4.dll \
-DCURL_INCLUDE_DIR=$libdir/curl/include \
-DCURL_LIBRARY=$libdir/curl/lib/libcurl.dll.a \
\
-DGETTEXT_MSGFMT=`command -v msgfmt` \
-DGETTEXT_DLL="$gettext_dlls" \
-DGETTEXT_INCLUDE_DIR=$libdir/gettext/include \
-DGETTEXT_LIBRARY=$libdir/gettext/lib/libintl.dll.a \
\
-DFREETYPE_INCLUDE_DIR_freetype2=$libdir/freetype/include/freetype2 \
-DFREETYPE_INCLUDE_DIR_ft2build=$libdir/freetype/include/freetype2 \
-DFREETYPE_LIBRARY=$libdir/freetype/lib/libfreetype.dll.a \
-DFREETYPE_DLL=$libdir/freetype/bin/libfreetype-6.dll \
\
-DSQLITE3_INCLUDE_DIR=$libdir/sqlite3/include \
-DSQLITE3_LIBRARY=$libdir/sqlite3/lib/libsqlite3.dll.a \
-DSQLITE3_DLL=$libdir/sqlite3/bin/libsqlite3-0.dll
cmake --build $builddir/$CORE_NAME -j$(nproc)
# Packaging
getgit()
{
# 1 - Git repo url
# 2 - Git branch
# 3 - Destination path
echo "Cloning $1"
rm -rf $3
git clone -q -j 5 --depth 1 --recurse-submodules $1 -b $2 $3
rm -rf $3/.git
}
packagedir=$workdir/package
rm -rf $packagedir
# Directory skeletton
mkdir -p $packagedir/$CORE_NAME
mkdir -p $packagedir/$CORE_NAME/client
mkdir -p $packagedir/$CORE_NAME/bin
mkdir -p $packagedir/$CORE_NAME/games
mkdir -p $packagedir/$CORE_NAME/mods
# Core files
cp -r $sourcedir/$CORE_NAME/builtin $packagedir/$CORE_NAME
cp -r $sourcedir/$CORE_NAME/client/shaders $packagedir/$CORE_NAME/client
cp -r $sourcedir/$CORE_NAME/fonts $packagedir/$CORE_NAME
cp -r $builddir/$CORE_NAME/locale $packagedir/$CORE_NAME
cp -r $sourcedir/$CORE_NAME/textures $packagedir/$CORE_NAME
cp \
$builddir/$CORE_NAME/src/minetest.exe \
$builddir/$IRRLICHT_NAME/bin/Win32-gcc/IrrlichtMt.dll \
$libdir/curl/bin/libcurl-4.dll \
$libdir/freetype/bin/libfreetype-6.dll \
$libdir/gettext/bin/libiconv-2.dll \
$libdir/gettext/bin/libintl-8.dll \
$libdir/libogg/bin/libogg-0.dll \
$libdir/libvorbis/bin/libvorbisfile-3.dll \
$libdir/libvorbis/bin/libvorbis-0.dll \
$libdir/openal/openal_stripped/bin/OpenAL32.dll \
$libdir/sqlite3/bin/libsqlite3-0.dll \
$libdir/zlib/bin/zlib1.dll \
$libdir/zstd/bin/libzstd.dll \
$sourcedir/jpeg-9/.libs/libjpeg-9.dll \
$sourcedir/libpng-1.6.38/.libs/libpng16-16.dll \
$topdir/dlls/* \
$packagedir/$CORE_NAME/bin
# Games
getgit $GITURL/minetest_game master $packagedir/$CORE_NAME/games/minetest_game
# Zip creation
ZIPNAME="$CORE_NAME""_$(date +"%Y-%m-%d").zip"
cd $packagedir && zip -qr $ZIPNAME $CORE_NAME
# We are done!
echo
echo "$packagedir/$ZIPNAME created!"
echo
# name of the target operating system
SET(CMAKE_SYSTEM_NAME Windows)
# which compilers to use for C and C++
# *-posix is Ubuntu's naming for the MinGW variant that comes with support
# for pthreads / std::thread (required by MT)
SET(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc-posix)
SET(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++-posix)
SET(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32)
# adjust the default behavior of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment