Blob Blame Raw
#!/bin/bash

set -e

ROOT="$(cd `dirname "$0"`; pwd)"
cd "$ROOT"

source function.sh
source toolchain.sh


OPTIONS="-O3 -DNDEBUG"
#OPTIONS="-g -O0"

LIBOPTIONS=
#LIBOPTIONS="-DWITH_FULL_UNICODE_FONT"


packages="zlib png freetype ogg vorbis flac mpg123 sdl2 sdl2image sdl2mixer"
newpackages="helianthus demo"

dir_zlib=zlib-1.2.11
dir_png=libpng-1.6.37
dir_freetype=freetype-2.8.1
dir_ogg=libogg-1.3.4
dir_vorbis=libvorbis-1.3.7
dir_flac=flac-1.3.2
dir_mpg123=mpg123-1.26.3
dir_sdl2=SDL2-2.0.12
dir_sdl2image=SDL2_image-2.0.5
dir_sdl2mixer=SDL2_mixer-2.0.4

tar_zlib=$dir_zlib.tar.gz
tar_png=$dir_png.tar.gz
tar_freetype=$dir_freetype.tar.gz
tar_ogg=$dir_ogg.tar.gz
tar_vorbis=$dir_vorbis.tar.gz
tar_flac=$dir_flac.tar.xz
tar_mpg123=$dir_mpg123.tar.bz2
tar_sdl2=$dir_sdl2.tar.gz
tar_sdl2image=$dir_sdl2image.tar.gz
tar_sdl2mixer=$dir_sdl2mixer.tar.gz

url_zlib="http://zlib.net/$tar_zlib"
url_png="https://prdownloads.sourceforge.net/libpng/$tar_png"
url_freetype="https://download.savannah.gnu.org/releases/freetype/$tar_freetype"
url_ogg="https://downloads.xiph.org/releases/ogg/$tar_ogg"
url_vorbis="https://downloads.xiph.org/releases/vorbis/$tar_vorbis"
url_flac="https://downloads.xiph.org/releases/flac/$tar_flac"
url_mpg123="https://sourceforge.net/projects/mpg123/files/mpg123/1.26.3/$tar_mpg123"
url_sdl2="https://www.libsdl.org/release/$tar_sdl2"
url_sdl2image="https://www.libsdl.org/projects/SDL_image/release/$tar_sdl2image"
url_sdl2mixer="https://www.libsdl.org/projects/SDL_mixer/release/$tar_sdl2mixer"

sha_zlib=c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1
sha_png=daeb2620d829575513e35fecc83f0d3791a620b9b93d800b763542ece9390fb4
sha_freetype=876711d064a6a1bd74beb18dd37f219af26100f72daaebd2d86cb493d7cd7ec6
sha_ogg=fe5670640bd49e828d64d2879c31cb4dde9758681bb664f9bdbf159a01b0c76e
sha_vorbis=0e982409a9c3fc82ee06e08205b1355e5c6aa4c36bca58146ef399621b0ce5ab
sha_flac=91cfc3ed61dc40f47f050a109b08610667d73477af6ef36dcad31c31a4a8d53f
sha_mpg123=30c998785a898f2846deefc4d17d6e4683a5a550b7eacf6ea506e30a7a736c6e
sha_sdl2=349268f695c02efbc9b9148a70b85e58cefbbf704abd3e91be654db7f1e2c863
sha_sdl2image=bdd5f6e026682f7d7e1be0b6051b209da2f402a2dd8bd1c4bd9c25ad263108d0
sha_sdl2mixer=b4cf5a382c061cd75081cf246c2aa2f9df8db04bdda8dcdc6b6cca55bede2419


#######################################################################


function build_zlib() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--static
	make -j4
	make install
}


function build_png() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared
	make -j4
	make install
}


function build_freetype() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared \
		--without-bzip2
	make -j4
	make install
	mkdir -p build-shared
	cd build-shared
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--disable-static \
		--enable-shared \
		--without-bzip2
	make -j4
	make install
	cd ..
}


function build_ogg() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared
	make -j4
	make install
}


function build_vorbis() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared
	make -j4
	make install
}


function build_flac() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared \
		--disable-cpplibs
	make -j4
	make install
}


function build_mpg123() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared
	make -j4
	make install
}


function build_sdl2() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared
	make -j4
	make install
}


function build_sdl2image() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared \
		--enable-png \
		--disable-png-shared \
		--disable-pnm \
		--disable-webp \
		--disable-webp-shared
	make -j4
	make install
}


function build_sdl2mixer() {
	"$srcdir"/configure \
		--prefix="$prefix" \
		--host="$host" \
		--enable-static \
		--enable-shared \
		--enable-music-wave \
		--enable-music-ogg \
		--disable-music-ogg-shared \
		--enable-music-flac \
		--disable-music-flac-shared \
		--enable-music-mp3-mpg123 \
		--disable-music-mp3-mpg123-shared
	make -j4
	make install
}


function build_helianthus() {
	mkdir -p src
	cp -r ../../../src/data src/

	local flags=
	if [ ! -z "$LINUX" ]; then
		flags="-fPIC"
	fi

	local file=
	local srcfiles=()
	local compileflags=(-Wall $OPTIONS $LIBOPTIONS $flags -I"$prefix/include/SDL2" -I"$prefix/include/freetype2")
	for file in ../../../src/*.c ../../../src/*.S; do
		local filename=$(basename "$file")
		srcfiles+=("$file")
		echocall "$CC" "$file" ${compileflags[@]} -c -o "$filename.o"
	done

	local libs="SDL2 SDL2_image SDL2_mixer FLAC mpg123 vorbisfile vorbis ogg freetype png16 z"
	local lib=
	local file=
	local libfiles=()
	rm -f libhelianthus.a
	for lib in $libs; do
		mkdir -p $lib
		cd $lib
		echocall "$AR" -x "$prefix/lib/lib$lib.a"
		for file in *.o; do
			cp $file ../thirdparty_${lib}_$file
		done
		libfiles+=("$prefix/lib/lib$lib.a")
		cd ..
	done
	echocall "$AR" rcs libhelianthus.a *.o

	if [ -z "$LINUX" ]; then
		echocall "$CC" -shared "${srcfiles[@]}" ${compileflags[@]} "${libfiles[@]}" \
			-lmingw32 \
			-lm -static-libgcc \
			-lopengl32 -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lshlwapi \
			-lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid \
			-Wl,--out-implib,libhelianthus.dll.a \
			-o libhelianthus.dll
	else
		echocall "$CC" -shared "${srcfiles[@]}" ${compileflags[@]} "${libfiles[@]}" \
			-lm -static-libgcc -lGL -ldl -pthread \
			-o libhelianthus.so
	fi

	mkdir -p ../../release/include/helianthus/helianthus
	mkdir -p ../../release/lib-$bits
	cp libhelianthus.a ../../release/lib-$bits/
	if [ -z "$LINUX" ]; then
		cp libhelianthus.dll ../../release/lib-$bits/
		cp libhelianthus.dll.a ../../release/lib-$bits/
	else
		cp libhelianthus.so ../../release/lib-$bits/
	fi
	cp -r ../../../src/*.h ../../release/include/helianthus/helianthus/
	rm -f ../../release/include/helianthus/helianthus/*.inc.h
	rm -f ../../release/include/helianthus/helianthus/private.h
	mv ../../release/include/helianthus/helianthus/helianthus.h ../../release/include/helianthus/
}


function build_demo() {
	if [ -z "$LINUX" ]; then
		echocall $CC \
			../../../demo/src/*.c \
			-Wall $OPTIONS \
			-I"$ROOT/release/include/helianthus" \
			"$ROOT/release/lib-$bits/libhelianthus.a" \
			-lmingw32 -mconsole \
			-lm -static-libgcc \
			-lopengl32 -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lshlwapi \
			-lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid \
			-o demo.exe
	else
		echocall $CC \
			../../../demo/src/*.c \
			-Wall $OPTIONS \
			-I"$ROOT/release/include/helianthus" \
			"$ROOT/release/lib-$bits/libhelianthus.a" \
			-lm -static-libgcc -lGL -ldl -pthread \
			-o demo
	fi
	mkdir -p ../../release/demo
	if [ -z "$LINUX" ]; then
		cp demo.exe ../../release/demo/demo-$bits.exe
	else
		cp demo ../../release/demo/demo-$bits
	fi
	cp -r ../../../demo/data ../../release/demo/
	mkdir -p ../../release/doc/
	cp -r ../../../doc/ru ../../release/doc/
}


#######################################################################


download
unpack
build win32 i686-w64-mingw32
build win64 x86_64-w64-mingw32
#LINUX=1 ADDCFLAGS="-m32 -fPIC" build linux32 ""
LINUX=1 ADDCFLAGS=-fPIC build linux64 ""

print_section done