################################################################################
# initialization
################################################################################

# set some defaults if this is not being called from the main makefile
ifndef CALLED_FROM_MAIN

	# compilers
	CC := gcc#  # C compiler

	# aliases
	MV     := mv -v
	RM     := rm -rfv

	# color definitions
	blue := $(shell tput setaf 6)
	sgr0 := $(shell tput sgr0)

	# detect os (linux, darwin)
	OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')

	# detect bit size
	UNAME_M := $(shell uname -m)
	ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 arm64 aarch64))
		ARCH := 64
	else
		ARCH := 32
	endif

endif


################################################################################
# compilation parameters
################################################################################

# define output file
OUT := slbmtestc
OUTDIR := ../bin

# includes
INC := -I../GeoTessCPP/include -I../SLBM/include -I../SLBM_C_shell/include

# libraries
LIBDIR := ../lib
LIBS := -L$(LIBDIR) -lgeotesscpp -lslbm -lslbmCshell

# compiling parameters
ifeq ($(UNAME_M),$(filter $(UNAME_M),arm64 aarch64))
	CCFLAGS := -mcpu=native
else
	CCFLAGS := -march=native -m$(ARCH)
endif
CCFLAGS += -D$(OS) -O1

# specific LD_FLAGS on Mac
ifeq ($(OS),darwin)
	# if using clang instead of gnu compiler, set additional flags
	CLANG := $(shell $(CXX) -v 2>&1 | grep -qi clang; echo $$?)
	ifeq ($(CLANG),0)
		OTHER_LDFLAGS += -undefined dynamic_lookup
	endif

endif

# list of objects to compile
OBJS = src/slbmtestc.o


################################################################################
# recipes
################################################################################

# recipe to make everything
all: initialize slbmtest finalize

# command that tells us which recipes don't build source files
.PHONY: all initialize msg_intro slbmtest finalize clean cleanall


# initialize compilation script
initialize: msg_intro

# print intro message
msg_intro:
	@echo "$(blue)------------------------------------------------------------$(sgr0)"
	@echo "$(blue)Making RSTT C test...$(sgr0)"
	@echo "$(blue)------------------------------------------------------------$(sgr0)"


# to build any *.o file, compile its corresponding *.cc file
%.o: %.c
	@printf "$(blue)(Building obj) $(sgr0)"
	$(CC) $(CCFLAGS) $(INC) -o $@ -c $<


# build library after building each prerequisite in $(OBJS)
slbmtest: initialize $(OUT)

# MacOS
ifeq ($(OS),darwin)
$(OUT): $(OBJS)
	@printf "$(blue)(Building lib) $(sgr0)"
	$(CC) $(CCFLAGS) $(OTHER_LDFLAGS) -Wl,-rpath,"@executable_path/$(LIBDIR)" -o $(OUT) $^ $(LIBS)

# linux
else
$(OUT): $(OBJS)
	@printf "$(blue)(Building lib) $(sgr0)"
	$(CC) $(CCFLAGS) -Wl,-rpath,'$$ORIGIN/$(LIBDIR)' -o $(OUT) $^ $(LIBS)

endif


# move files and clean up
finalize:
	@printf "$(blue)(Moving files) $(sgr0)"
	$(MV) $(OUT) $(OUTDIR)

clean:
	@printf "$(blue)(Clean *.o) $(sgr0)"
	$(RM) src/*.o

cleanall: clean
	@printf "$(blue)(Clean bin) $(sgr0)"
	$(RM) $(OUTDIR)/$(OUT)
