# default flavor is C89 with GNU extensions
C_FLAVOR=-std=gnu99
CPPFLAGS=-Wall -Wextra -pedantic $(C_FLAVOR) -DSTAND_ALONE
CFLAGS:=-Wsign-compare -Wshadow -g -O0 -ggdb
# ar: Non-deterministic mode is preferred for make dependency checking
ARFLAGS=rvU

compile.c=$(CC) -Wall -W -o $@ $^ $(if $(PKGS),$(shell pkg-config --libs --cflags $(PKGS)))

TARGETS=\
	acs \
	adler32 \
	args_test \
	argv \
	base32 \
	base26 \
	base85 \
	bdf \
	bitanon \
	bitfield_test \
	bittest \
	bmp \
	bo-test \
	c99array \
	calendar \
	carray \
	cccheck \
	config \
	cp437utf8 \
	crc \
	critbit \
	curses-demo-1 \
	decimal \
	dll_test \
	error \
	fnt2bdf \
	freecell \
	funptr \
	gcd \
	genroom \
	getaddrinfo \
	gunzip \
	heap \
	hexdump \
	hilbert \
	hilbert2 \
	hilberttiny \
	infix \
	inflate \
	ini.huge \
	ini.small \
	test_cfg \
	inplace \
	ipow \
	json \
	jtar \
	jtar-zlib \
	lfsr \
	line2 \
	logger_test \
	makeext \
	map \
	map2 \
	mapfile \
	mask \
	maze \
	memtrck \
	mkenum \
	moon \
	mren \
	myip \
	mud \
	net \
	netdump \
	number \
	odbcdemo \
	oop \
	omp_crc32 \
	parser \
	pdline \
	perlin \
	pwgen \
	queue_test \
	quine1 \
	readline-example \
	rc4rand \
	rc4tiny \
	rfb \
	rickdate \
	ringbuffer \
	roman \
	room \
	scrub \
	select \
	sha1 \
	sha1tiny \
	showif \
	spaces \
	stitch \
	strcmpn \
	stretch \
	strtokex \
	swap \
	tcrypt \
	telnet \
	test-3dsloader \
	test-objloader \
	test-ot \
	test-rc \
	test_ihash \
	test_lzwh \
	tetris \
	texteditor \
	tga \
	udb \
	utf8 \
	wav \
	whirlpool \
	xconv \
	xd \
	xrootbg

all : $(TARGETS)
clean ::
	$(RM) $(TARGETS)
clean-all : clean
	$(RM) *~ *.o

args_test : args.c args_test.c

curses-demo-1 : LDLIBS=-lcurses
wav pdline : LDLIBS=-lm

freecell : freecell.c oldrand.c

mud : CPPFLAGS=-Wall -Wextra -D_POSIX_C_SOURCE=200112L -D_BSD_SOURCE $(C_FLAVOR)
#-DARGV_USE_UNSIGNED_CHAR
mud : LDLIBS=-lsqlite3
mud : net.c mud.c argv.c miniodbc.c whirlpool.c base64.c logger.c

odbcdemo : LDLIBS=-lsqlite3
odbcdemo : miniodbc.c odbcdemo.c

logger_test : logger.c logger_test.c
test-3dsloader : test-3dsloader.c 3dsloader.c fileio.c model.c logger.c
test-objloader : test-objloader.c objloader.c fileio.c model.c logger.c
test-rc : test-rc.c rc.c

readline-example : LDLIBS=-lreadline -ltermcap

# don't use standalone
rfb : CPPFLAGS=-Wall -Wextra -pedantic -std=gnu99
rfb : rfb.c net.c

jtar-zlib : LDLIBS+=-lz

omp_crc32 : C_FLAVOR=-std=gnu99
omp_crc32 : CFLAGS+=-fopenmp
omp_crc32 : omp_crc32.c

tetris : LDLIBS += -lncurses

## shows C source that isn't being built
missing :
	echo C source that is not being built...
	echo $(wildcard *.c) $(TARGETS:%=%.c) | awk 'BEGIN { RS=" "} { print }' | sort | uniq -u

hello-i386 : hello-i386.S
hello-i386 : ASFLAGS = -m32 -nostdlib
hello-gate : hello-gate.S
hello-gate : ASFLAGS = -m32 -nostdlib
clean ::
	$(RM) hello-i386 hello-gate

# sudo apt install libimlib2-dev
xrootbg : xrootbg.c
xrootbg : LDLIBS += -lX11 -lImlib2

cube_vbo : cube_vbo.c
	$(CC) -Wall -W -o $@ $^ $(shell pkg-config --libs --cflags sdl2 gl)

clock : C_FLAVOR=-std=gnu99
clock : LDLIBS += -lncurses

dll_test : dll_test.o libdll.a | dll_plugin1.so
dll_test : LDLIBS += -ldl
libdll.a : libdll.a(dll.o)

dll_plugin1.so : dll_plugin1.c
clean :: ; $(RM) dll_plugin1.so

test_ihash : test_ihash.c
	$(compile.c)

# rules for shared objects
%.so : CFLAGS += -fPIC
%.so : %.o
	$(LINK.o) $^ -shared $(LOADLIBES) $(LDLIBS) -o $@
%.so : %.c
	$(LINK.c) $^ -shared $(LOADLIBES) $(LDLIBS) -o $@

# rules for tests
test_% : test_%.c %.c
	$(compile.c)
