#!/usr/bin/make -f

# With gcc-4.6 4.6.1-15 on Debian unstable i386-kfreebsd something in -O2
# that can't be controlled by a flag causes problems with the bsnes GUI.
# The bsnes GUI doesn't show up at all, snespurify-gtk crashes all the time.
# Using -O1 and enabling all additional flags from -O3 works fine.
CFLAGS += -O1 -fthread-jumps -falign-functions  -falign-jumps -falign-loops \
-falign-labels -fcaller-saves -fcrossjumping -fcse-follow-jumps \
-fcse-skip-blocks -fdelete-null-pointer-checks -fdevirtualize \
-fexpensive-optimizations -fgcse -fgcse-lm -finline-small-functions \
-findirect-inlining -fipa-sra -foptimize-sibling-calls -fpartial-inlining \
-fpeephole2 -fregmove -freorder-blocks -freorder-functions \
-frerun-cse-after-loop -fsched-interblock -fsched-spec -fschedule-insns \
-fschedule-insns2 -fstrict-aliasing -fstrict-overflow -ftree-switch-conversion \
-ftree-pre -ftree-vrp -finline-functions -funswitch-loops -fpredictive-commoning \
-fgcse-after-reload -fipa-cp-clone -fomit-frame-pointer

%:
	dh $@ --parallel

# We have to clean manually because the Makefile does not support distclean,
# and does not remove compiled bsnes binary
# Don't use make clean for snesfilter because it creates an error if called
# without files to delete.
override_dh_auto_clean:
	$(MAKE) --directory=bsnes clean
	rm -rf bsnes/obj bsnes/out
	rm -rf snesfilter/out
	$(MAKE) --directory=purify clean
	rm -f purify/purify

override_dh_auto_build:
	mkdir -p bsnes/obj bsnes/out snesfilter/out
	dh_auto_build -Dbsnes -- compiler=gcc phoenix=gtk profile=compatibility name=bsnes-compatibility
# Allow for building bsnes only once when testing stuff with the build option testbuild.
ifeq (,$(findstring testbuild,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -Cbsnes clean
	dh_auto_build -Dbsnes -- compiler=gcc phoenix=gtk profile=accuracy name=bsnes-accuracy
	$(MAKE) -Cbsnes clean
	dh_auto_build -Dbsnes -- compiler=gcc phoenix=gtk profile=performance name=bsnes-performance
	$(MAKE) -Cbsnes clean
	dh_auto_build -Dbsnes -- compiler=gcc phoenix=gtk target=debugger name=laevateinn-bin
else
	cp bsnes/out/bsnes-compatibility bsnes/out/bsnes-accuracy
	cp bsnes/out/bsnes-compatibility bsnes/out/bsnes-performance
	cp bsnes/out/bsnes-compatibility bsnes/out/laevateinn-bin
endif
	dh_auto_build -Dsnesfilter -- compiler=gcc
	dh_auto_build -Dpurify -- compiler=gcc
	chmod 644 snesshader/*.OpenGL.shader

override_dh_auto_install:
	install -D -m 644 bsnes/data/bsnes.png debian/bsnes/usr/share/pixmaps/bsnes.png
	install -D -m 644 bsnes/data/bsnes.desktop debian/bsnes/usr/share/applications/bsnes.desktop
	install -D -m 644 bsnes/data/cheats.xml debian/bsnes/usr/share/games/bsnes/cheats.xml
	install -D -m 755 purify/purify debian/bsnes/usr/games/bsnes-purify
	cp -R bsnes/profile/* debian/bsnes/usr/share/games/bsnes
	chmod 644 debian/bsnes/usr/share/games/bsnes/*/*
	mkdir -p debian/laevateinn/usr/share/games/laevateinn
	cp -R bsnes/profile/* debian/laevateinn/usr/share/games/laevateinn
	chmod 644 debian/laevateinn/usr/share/games/laevateinn/*/*

override_dh_fixperms:
	chmod 644 debian/bsnes/usr/lib/games/bsnes/*/*
	dh_fixperms

override_dh_strip:
	strip --strip-unneeded debian/bsnes/usr/lib/games/bsnes/*/*
	dh_strip
