optimize  := -ffast-math
warnbasic := -Wall -pedantic #-ansi # -std=c99
warnclang := -Wextra -Weverything \
-Wno-comma \
-Wno-parentheses \
-Wno-logical-op-parentheses \
-Wno-parentheses \
-Wno-documentation-unknown-command \
-Wno-documentation \
-Wno-shift-op-parentheses \
-Wno-empty-body \
-Wno-padded \
-Wno-switch-enum \
-Wno-missing-noreturn \
-Wno-implicit-fallthrough
# https://stackoverflow.com/a/12099167
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
	warnclang += -Wno-poison-system-directories
endif
# Some stuff is really new, comment out the warnclang?
warn := $(warnbasic) $(warnclang)
CC   := clang # gcc
CF   := $(optimize) $(warn)
OF   :=
ifeq (release, $(firstword $(MAKECMDGOALS)))
	CF += -funroll-loops -Ofast -D NDEBUG # -O3
	OF += -Ofast
else
	CF += -g
endif

projects := bin/test-text bin/test-journal bin/test-source bin/test-kjv bin/kjv bin/flight bin/score
#docs := $(patsubst test/test_%.c, doc/%.md, $(wildcard test/test_*.c))

default: $(projects)
	# success

bin/test-text: build/text.o build/test_text.o
bin/test-journal: build/text.o build/journal.o build/test_journal.o
bin/test-source: build/text.o build/pair.o build/journal.o build/source.o build/test_source.o
bin/test-kjv: build/text.o build/pair.o build/kjvcite.o build/test_kjv.o
bin/kjv: build/text.o build/pair.o build/journal.o build/kjvcite.o build/kjv.o build/source.o
bin/flight: build/text.o build/pair.o build/journal.o build/source.o build/flights.o build/flighthours.o
bin/score: build/text.o build/pair.o build/journal.o build/scores.o

bin/%:
	@echo "\033[1;36mlinking $@\033[0m"
	@mkdir -p bin
	$(CC) $(OF) -o $@ $^

build/%.o: src/%.c #src/%.h
	@echo "\033[0;36mcompile src $@\033[0m"
	@mkdir -p build
	$(CC) $(CF) -c -o $@ $<

build/%.o: test/%.c
	@echo "\033[0;36mcompile test $@\033[0m"
	@mkdir -p build
	$(CC) $(CF) -c -o $@ $<

build/%.o: build/%.c #src/%.h
	@echo "\033[0;36mcompile generated $@\033[0m"
	$(CC) $(CF) -c -o $@ $<

build/%.c: src/%.re.c
	@echo "\033[0;34mhttps://re2c.org/ generate $@\033[0m"
	@mkdir -p build
	re2c -W --tags --conditions -o $@ $<

#doc/%.md: src/%.h
#	# https://github.com/neil-edelman/cdoc documentation
#	-cdoc -o $@ $<

.SECONDARY: build/kjv.c build/journal.c build/source.c build/scan_kjv.c build/flights.c build/kjvcite.c build/scores.c
.PHONY: clean release test

test: $(projects)
	@for project in $(projects); do \
		echo "\033[1;36m\033[1m*** Testing $$project ***\033[0m" ; \
		$$project ; \
	done

clean:
	-rm -rf build/

#docs: $(docs)
