# Makefile - Don Yang (uguu.org)
#
# 05/06/12

ifeq ($(DEBUG),1)
debug_opt = -D_FORTIFY_SOURCE=1
else
debug_opt = -DNDEBUG
endif
ifeq ($(OSTYPE),cygwin)
eext = .exe
cc = i686-w64-mingw32-g++
c99 = i686-w64-mingw32-gcc
strip = i686-w64-mingw32-strip
cflags = -O3 -Wall -ansi -pedantic -finline-functions -include binmode_main.h $(debug_opt)
else
eext =
cc = g++
c99 = gcc
strip = strip
cflags = -O3 -Wall -ansi -pedantic -finline-functions $(debug_opt)
endif
target = hazuki$(eext)

all: $(target)

$(target): hazuki.cc
	$(cc) $(cflags) -Werror -static $< -o $@
	$(strip) $@

manual.html: manual.texi
	texi2html $< > $@

bf$(eext): bf.c
	$(c99) $(cflags) -Werror $< -o $@

blockio$(eext): blockio.c
	$(c99) $(cflags) -Werror $< -o $@

cipher$(eext): cipher.c
	$(c99) $(cflags) -Werror $< -o $@

date$(eext): date.c
	$(c99) $(cflags) -Werror $< -o $@

rand$(eext): rand.c
	$(c99) $(cflags) -Werror $< -o $@

sbox$(eext): sbox.c
	$(c99) $(cflags) $< -o $@

sbox_params$(eext): sbox_params2.c
	$(c99) $(cflags) -Werror $< -o $@

verify_sbox_params$(eext): verify_sbox_params2.c
	$(c99) $(cflags) -Werror $< -o $@


test: test_passed.hazuki test_passed.moon_phase test_passed.bf test_passed.blockio test_passed.cipher

test_passed.hazuki: hazuki_test.sh hazuki$(eext)
	./$< && touch $@

test_passed.moon_phase: moon_phase_test.pl hazuki$(eext)
	perl $< && touch $@

test_passed.bf: bf$(eext)
	./$< && touch $@

test_passed.blockio: blockio_test.sh blockio$(eext)
	./$< && touch $@

test_passed.cipher: cipher_test.sh cipher$(eext)
	./$< && touch $@

clean:
	-rm -f $(target) bf$(eext) blockio$(eext) cipher$(eext) date$(eext) rand$(eext) sbox$(eext) sbox_params$(eext) verify_sbox_params$(eext)
	-rm -f manual.html
	-rm -f test_passed.*
	-rm -rf test_output

backup:
	-rm -f backup.tar.xz
	tar cf backup.tar *.c *.cc *.pl *.sh *.txt *.texi *.log Makefile
	xz -9 backup.tar
