#build-m3 file for fishtrax client

execute("ln -sf parser.yy.c src/parser.yy.cpp");
execute("ln -sf parser.tab.c src/parser.tab.cpp");

action "default"
{
	warning "Use the 'release' target to strip debugging and build without deps.";
	DEBUG = "true";
	build: ["stage.exe"];
}

action "release"
{
	warning "Relesae will build without deps, and will strip debugging.";
	DEBUG = "false";
	build: ["stage.exe"];
}

action "clean"
{
	clean: targets();
}

ZIPFILE = "stage-$(git describe).zip";
ZIPINPUT = [
	"stage.exe",
	"bloodfields.stage"
];

action "package"
{
	build: [ZIPFILE];
}

CXXFLAGS += "-ggdb";

target ZIPFILE
{
	profile "build"
	{
		execute("zip -9 ${ZIPFILE} ${ZIPINPUT}");
	}
}

target "stage.exe"
{
	input [
		files("src/*.cpp")
//		files("src/*.c")
		];
	rule "winexe";

	CXXFLAGS += "-O2 -frtti -fexceptions -Wall -Ilibbu++/support/windows -Isrc";

	LDFLAGS += "-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -lmingw32 -Llibbu++ -lbu++win -Llibbu++/support/windows -lbz2";

	CXXFLAGS += "-Ilibbu++";
}

rule "winlib"
{
	input "*.win_o";
	profile "build"
	{
		execute("wine C:/MinGW/bin/ar.exe cr ${OUTPUT} ${INPUT}");
	}
}

rule "winexe"
{
	input "*.win_o";
	profile "build"
	{
		if DEBUG != "true" then
		{
			LDFLAGS += "-Wl,-s";
		}
		execute("wine C:/MinGW/bin/mingw32-g++.exe -o ${OUTPUT} ${INPUT} ${LDFLAGS}");
	}
}

function cppToWinObj()
{
	if OBJ_DIR == null then
	{
		DIR = INPUT.dirName();
	}
	else
	{
		DIR = OBJ_DIR;
	}

	return DIR + "/" + INPUT.fileName().replace(".cpp", ".win_o");
}

function cToWinObj()
{
	if OBJ_DIR == null then
	{
		DIR = INPUT.dirName();
	}
	else
	{
		DIR = OBJ_DIR;
	}

	return DIR + "/" + INPUT.fileName().replace(".c", ".win_o");
}

function winDeps( CMD )
{
	if DEBUG == "true" then
	{
		return getMakeDeps( CMD );
	}
	return [];
}

rule "wincpp"
{
	input "*.cpp";
	output INPUT.cppToWinObj();
	requires winDeps("wine C:/MinGW/bin/g++.exe ${CXXFLAGS} -M ${INPUT}");
	profile "build"
	{
		//condition always;
		execute("wine C:/MinGW/bin/mingw32-g++.exe ${CXXFLAGS} -c -o ${OUTPUT} ${INPUT}", "g++");
	}
}

rule "winc"
{
	input "*.c";
	output INPUT.cToWinObj();
	requires winDeps("wine C:/MinGW/bin/g++.exe ${CXXFLAGS} -M ${INPUT}");
	profile "build"
	{
		//condition always;
		execute("wine C:/MinGW/bin/mingw32-g++.exe ${CXXFLAGS} -c -o ${OUTPUT} ${INPUT}", "g++");
	}
}