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

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

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

# aliases
CXX      := g++
MAKE     := make
CP       := cp -rv
MV       := mv -v
RM       := rm -rfv
PY3      := python3
PIP      := $(PY3) -m pip
SPHINX   := sphinx-build
LIBEXT   := $(shell $(PY3) -c 'import importlib.machinery as il; print(il.EXTENSION_SUFFIXES[0])')

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


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

# define output file
OUT := libslbmPyshell$(LIBEXT)
OUTDIR := ../lib

# includes
INC := -Icc/include -I../GeoTessCPP/include -I../SLBM/include $(shell $(PY3) -m pybind11 --includes)

# libraries
LIBS := -L$(OUTDIR) -lgeotesscpp -lslbm

# compiling parameters
CXXFLAGS = -march=native -D$(OS) -m$(ARCH) -O1 -fPIC
CXXFLAGS += -std=c++11

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


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

# recipe to make everything and run tests
all: initialize slbmpyshell slbmpymod finalize msg_outro

# command that tells us which recipes don't build source files
.PHONY: all initialize msg_intro slbmpyshell slbmpymod msg_docs docs finalize msg_outro clean cleanall


# initialize compilation script
initialize: msg_intro

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

# print outro message
msg_outro:
	@printf "$(blue)To install the 'rstt' module, run:\n$(sgr0)"
	@printf "$(blue) $$ $(PIP) install --no-index --find-links=$(shell pwd)/wheel rstt\n$(sgr0)"

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


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

# MacOS
ifeq ($(OS),darwin)
$(OUT): $(OBJS)
	@printf "$(blue)(Building lib) $(sgr0)"
	$(CXX) $(CXXFLAGS) -undefined dynamic_lookup -dynamiclib -Wl,-rpath,@loader_path -install_name @rpath/$(OUT) -o $(OUT) $(OBJS) $(LIBS)

# linux
else
$(OUT): $(OBJS)
	@printf "$(blue)(Building lib) $(sgr0)"
	$(CXX) $(CXXFLAGS) -shared -Wl,-rpath,'$$ORIGIN' -o $(OUT) $(OBJS) $(LIBS)

endif


# build module
slbmpymod: slbmpyshell
	@printf "$(blue)(Copying shared libs) $(sgr0)"
	$(CP) ../lib/libgeotesscpp.* ../lib/libslbm.* ./python/rstt/
	$(MV) ./libslbmPyshell.* ./python/rstt/
	@printf "$(blue)(Compiling module) $(sgr0)"
	$(PIP) wheel --no-index -w wheel python/


# print docs msg
msg_docs:
	@echo "$(blue)------------------------------------------------------------$(sgr0)"
	@echo "$(blue)Making RSTT Python documentation...$(sgr0)"
	@echo "$(blue)------------------------------------------------------------$(sgr0)"

# documentation
docs: msg_docs doc/html
doc/html:
ifeq (, $(shell which $(SPHINX)))  # sphinx is not installed
	@printf "$(blue)(Building HTML docs) $(sgr0)"
	@echo "$(SPHINX) is not installed. Skipping..."

else  # sphinx is installed
	@printf "$(blue)(Building HTML docs) $(sgr0)"
	cd doc; $(MAKE) html
	$(MV) doc/_build/html doc/
	$(RM) doc/_build/doctrees

endif


# move files and clean up
finalize:
	@printf "$(blue)(Moving files) $(sgr0)"
	$(CP) ./python/rstt ../lib/

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

cleanall: clean
	@printf "$(blue)(Clean docs) $(sgr0)"
	$(RM) doc/html
	@printf "$(blue)(Clean libs) $(sgr0)"
	$(RM) $(OUTDIR)/rstt python/rstt/*.dylib python/rstt/*.so
	@printf "$(blue)(Clean module) $(sgr0)"
	$(RM) wheel python/rstt/__pycache__
