#
# makefile for http://floor13.sakura.ne.jp/book06/*.zip
#
# usage:
#	$ make fullclean prepare all exec
#
# Written by Taiji Yamada <taiji@aihara.co.jp> at 2021/10/27
#
SRCS=\
./chapter01/ex1_1/ex1_1.c	\
./chapter01/ex1_2/ex1_2.c	\
./chapter02/ex2_1/ex2_1.c	\
./chapter02/ex2_2/ex2_2.c	\
./chapter03/ex3_1/ex3_1.c	\
./chapter03/ex3_2/ex3_2.c	\
./chapter03/ex3_3/ex3_3.c	\
./chapter03/ex3_4/ex3_4.c	\
./chapter03/ex3_5/ex3_5.c	\
./chapter04/ex4_1/ex4_1.c	\
./chapter04/ex4_2/ex4_2.c	\
./chapter04/ex4_3/ex4_3.c	\
./chapter04/ex4_4/ex4_4.c	\
./chapter05/ex5_1/ex5_1.c	\
./chapter05/ex5_2/ex5_2.c	\
./chapter05/ex5_3/ex5_3.c	\
./chapter05/ex5_4/ex5_4.c	\
./chapter05/ex5_5/ex5_5.c	\
./chapter06/ex6_1/ex6_1.c	\
./chapter06/ex6_2/ex6_2.c	\
./chapter06/ex6_3/ex6_3.c	\
./chapter06/ex6_4/ex6_4.c	\
./chapter06/ex6_5/ex6_5.c	\
./chapter08/ex8_1/ex8_1.c	\
./chapter08/ex8_10/ex8_10.c	\
./chapter08/ex8_11/ex8_11.c	\
./chapter08/ex8_12/ex8_12.c	\
./chapter08/ex8_2/ex8_2.c	\
./chapter08/ex8_3/ex8_3.c	\
./chapter08/ex8_4/ex8_4.c	\
./chapter08/ex8_5/ex8_5.c	\
./chapter08/ex8_6/ex8_6.c	\
./chapter08/ex8_7/ex8_7.c	\
./chapter08/ex8_8/ex8_8.c	\
./chapter08/ex8_9/ex8_9.c	\
./chapter09/ex9_1/ex9_1.c	\
./chapter09/ex9_10/ex9_10.c	\
./chapter09/ex9_11/ex9_11.c	\
./chapter09/ex9_2/ex9_2.c	\
./chapter09/ex9_3/ex9_3.c	\
./chapter09/ex9_4/ex9_4.c	\
./chapter09/ex9_5/ex9_5.c	\
./chapter09/ex9_6/ex9_6.c	\
./chapter09/ex9_7/ex9_7.c	\
./chapter09/ex9_8/ex9_8.c	\
./chapter09/ex9_9/ex9_9.c	\
./chapter10/ex10_1/ex10_1.c	\
./chapter10/ex10_2/ex10_2.c	\
./chapter10/ex10_3/ex10_3.c	\
./chapter10/ex10_4/ex10_4.c	\
./chapter10/ex10_5/ex10_5.c	\
./chapter10/ex10_6/ex10_6.c	\
./chapter11/ex11_1/ex11_1.c	\
./chapter11/ex11_2/ex11_2.c	\
./chapter11/ex11_3/ex11_3.c	\
./chapter11/ex11_4/ex11_4.c	\
./chapter11/ex11_5/ex11_5.c	\
./chapter11/ex11_6/ex11_6.c	\
./chapter11/ex11_7/ex11_7.c	\
./chapter11/ex11_8/ex11_8.c	\
./chapter11/ex11_9/ex11_9.c	\
./ex7_1/ex7_1.c	\
./ex7_2/ex7_2.c	\
./ex7_3/ex7_3.c	\
./ex7_4/ex7_4.c	\

SRCS_windows=\
./chapter12/ex12_1/ex12_1.c	\
./chapter12/ex12_2/ex12_2.c	\
./chapter12/ex12_3/ex12_3.c	\
./chapter12/ex12_4/ex12_4.c	\

SRCS_hidden=\
./chapter04/ex4_1/ex4_1.c	\

EXES=$(SRCS:%.c=%)
EXES_hidden=$(SRCS_hidden:%.c=%)

CFLAGS_CORE=-W -Wall
CFLAGS=$(CFLAGS_CORE) -O3

all: $(EXES)

# suppress warning
ex7_3/ex7_3:			CFLAGS=$(CFLAGS_CORE) -O3 -Wno-unused-variable

# avoid segmentation fault
chapter11/ex11_9/ex11_9:	CFLAGS=$(CFLAGS_CORE) -O2

# avoid segmentation fault, try it but it could not
chapter04/ex4_1/ex4_1:		CFLAGS=$(CFLAGS_CORE) -O1

#
# normally build
#
clean:
	rm -f $(EXES)
	rm -rf $(EXES:%=%.dSYM)

patches:
	find . -name '*~' | while read f; do echo diff -u $$f $$(dirname $$f)/$$(basename $$f \~) && diff -u $$f $$(dirname $$f)/$$(basename $$f \~); done | dd of=patches.patch

exec:
	for x in $(EXES); do (cd $$(dirname $$x) && echo "(cd $$(dirname $$x) && ./$$(basename $$x))" && ./$$(basename $$x)); done

fullclean:
	find . -depth -type d ! -name . \( -name 'chapter*' -o -name 'ex*' \) -exec rm -rf {} \;

prepare:
	./obtain.sh && patch -p0 < patches.patch
#
# abnormally build
#
# usage:
#	$ make hidden.clean hidden hidden.exec
#
hidden: $(EXES_hidden)

hidden.clean:
	rm -f $(EXES_hidden)
	rm -rf $(EXES_hidden:%=%.dSYM)

hidden.exec:
	for x in $(EXES_hidden); do (cd $$(dirname $$x) && echo "(cd $$(dirname $$x) && ./$$(basename $$x))" && ./$$(basename $$x)); done
