FLAGS := -Wno-write-strings -D_GNU_SOURCE -fno-threadsafe-statics \
 -pthread -I../foreign -I. -Wno-literal-suffix
LIBS  := -lX11 -lXfixes -lGL -lrt -ldl -lm -lfreetype -lfontconfig

# main targets

debug: FLAGS += -DFRED_SUPER=1 -DFRED_INTERNAL=1 -O0 -g
debug: ../4ed ../4ed_app.so ../4coder_custom.so

super: FLAGS += -DFRED_SUPER=1
super: alpha ../4coder_custom.so

alpha: FLAGS += -O3 -Wl,-s -fno-stack-protector -U_FORTIFY_SOURCE -DFRED_KEEP_ASSERT=1
alpha: ../4ed ../4ed_app.so

# auto-generated dependencies

-include ../build/*.d

# file targets

../build:
	mkdir $@

../build/fsmgen: fsm_table_generator.cpp | ../build
	$(CXX) -MMD $(FLAGS) $< -o $@
	$@

../build/metagen: 4ed_metagen.cpp | ../build
	$(CXX) -MMD $(FLAGS) $< -o $@
	$@

../4ed: linux_4ed.cpp ../build/fsmgen ../build/metagen
	$(CXX) -MMD -MF ../build/4ed.d $(FLAGS) $(shell pkg-config --cflags freetype2) $< -o $@ $(LIBS)

../4ed_app.so: 4ed_app_target.cpp ../build/fsmgen ../build/metagen
	$(CXX) -MMD -MF ../build/app.d $(FLAGS) -fPIC -shared $< -o $@

../4coder_custom.so: power/4coder_experiments.cpp ../build/fsmgen ../build/metagen
	$(CXX) -MMD -MF ../build/custom.d $(FLAGS) -I. -fPIC -shared $< -o $@

# cleaning

clean:
	$(RM) ../4ed_app.so ../4ed ../4coder_custom.so

veryclean: clean
	$(RM) ../build/fsmgen ../build/metagen ../build/*.d

# packaging

PACKAGE_FILES := ../4ed ../4ed_app.so README.txt TODO.txt
V_MAJ := $(shell grep "define MAJOR" 4coder_version.h | cut -d\  -f 3)
V_MIN := $(shell grep "define MINOR" 4coder_version.h | cut -d\  -f 3)
V_PCH := $(shell grep "define PATCH" 4coder_version.h | cut -d\  -f 3)

super_zip: PACKAGE_FILES += ../4coder_custom.so 4coder_*.h 4coder_*.cpp buildsuper.sh
super_zip: ../4coder-linux-64-$(V_MAJ)-$(V_MIN)-$(V_PCH)-super.zip
alpha_zip: ../4coder-linux-64-$(V_MAJ)-$(V_MIN)-$(V_PCH)-alpha.zip

../4coder-linux-64-$(V_MAJ)-$(V_MIN)-$(V_PCH)-%.zip: %
	@echo === Creating $@ ===
	zip -j $@ $(PACKAGE_FILES)
	$(MAKE) clean

package: clean
	$(MAKE) alpha_zip
	$(MAKE) super_zip

.PHONY: debug alpha super all clean veryclean package alpha_zip super_zip