PLATFORM = Linux

CFLAGS += -I. -I../inc -O2
CFLAGS += \
	-pedantic \
	-Wall \
	-Wextra \
	-Waggregate-return \
	-Walloca \
	-Warray-bounds \
	-Wbad-function-cast \
	-Wcast-align \
	-Wcast-qual \
	-Wconversion \
	-Wdisabled-optimization \
	-Wdouble-promotion \
	-Wenum-compare \
	-Wfloat-equal \
	-Wframe-larger-than=1048576 \
	-Wimplicit \
	-Wimplicit-fallthrough \
	-Winit-self \
	-Winline \
	-Winvalid-pch \
	-Wmissing-braces \
	-Wmissing-declarations \
	-Wmissing-field-initializers \
	-Wmissing-format-attribute \
	-Wmissing-include-dirs \
	-Wmissing-noreturn \
	-Wmissing-prototypes \
	-Wnested-externs \
	-Wnull-dereference \
	-Wold-style-definition \
	-Woverlength-strings \
	-Wpacked \
	-Wpadded \
	-Wpointer-arith \
	-Wredundant-decls \
	-Wshadow \
	-Wsign-compare \
	-Wsign-conversion \
	-Wstack-protector \
	-Wstrict-aliasing=2 \
	-Wstrict-overflow=5 \
	-Wstrict-prototypes \
	-Wswitch-default \
	-Wswitch-enum \
	-Wtype-limits \
	-Wundef \
	-Wuninitialized \
	-Wunreachable-code \
	-Wvariadic-macros \
	-Wwrite-strings

CC ?= clang
CFLAGS += -fPIC
ifeq ($(CC),clang)
	CFLAGS += -Werror -Wno-gnu-folding-constant -Wformat=2
endif

BLST_DIR = ../blst
BLST_LIBRARY = ../blst/libblst.a
BLST_BUILDSCRIPT = ../blst/build.sh
BLST_BUILDSCRIPT_FLAGS = -D__BLST_PORTABLE__

LIBS = $(BLST_LIBRARY)

SOURCE_FILES := $(shell find . -name '*.c' | sed 's|^\./||' | sort)
HEADER_FILES := $(shell find . -name '*.h' | sed 's|^\./||' | sort)

HEADER_FILES := $(filter-out test/tests.h, $(HEADER_FILES))
HEADER_FILES := $(filter-out test/tinytest.h, $(HEADER_FILES))

all: ckzg

.PHONY: build_blst
build_blst:
	@echo "[+] building blst"
	@cd $(dir $(BLST_BUILDSCRIPT)) && \
	./$(notdir $(BLST_BUILDSCRIPT)) $(BLST_BUILDSCRIPT_FLAGS)

.PHONY: blst
blst:
	$(MAKE) build_blst;

.PHONY: ckzg
ckzg: CFLAGS += -O0
ckzg: blst $(SOURCE_FILES) $(HEADER_FILES)

.PHONY: clean
clean:
	@echo "[+] cleaning"
	@rm -f *.o */*.o *.profraw *.profdata *.html xray-log.* *.prof *.pdf $(BLST_LIBRARY)