# Modified to replace the autoconf processed file nrnmech_makefile.in

# Mechanisms version are by default 0.0, but should be overriden
MECH_NAME =
MECH_VERSION = 0.0
MODS_PATH = .
OUTPUT = aarch64
DESTDIR =
UserINCFLAGS =
UserLDFLAGS =

# install dirs
bindir := ${ROOT}/bin
libdir := ${ROOT}/lib
incdir := ${ROOT}/include
datadir:= ${ROOT}/share/nrn
datadir_lib := ${ROOT}/share/nrn/lib

# Additional variables set in CMAKE usable here
# - @NRN_COMPILE_DEFS
# - @NRN_LINK_DEFS
LDFLAGS = $(LINKFLAGS) $(UserLDFLAGS)  -lreadline -lcurses -lform -lpython3 -lX11 -lXext
NRNLIB_FLAGS = -L$(libdir) -lnrniv
OS_NAME := $(shell uname)
_cm =,

# We rebuild the include dirs since a lot of stuff changes place
INCLUDES = -I. -I.. $(INCFLAGS) $(UserINCFLAGS) -I$(incdir)
INCLUDES += $(if , -I$(subst ;, -I,),)

CC ?= /usr/lib64/openmpi/bin/mpicc
CXX ?= /usr/lib64/openmpi/bin/mpicxx
CFLAGS =    -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -mbranch-protection=standard -fasynchronous-unwind-tables -fstack-clash-protection -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -mbranch-protection=standard -fasynchronous-unwind-tables -fstack-clash-protection
CXXFLAGS =    -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -mbranch-protection=standard -fasynchronous-unwind-tables -fstack-clash-protection -std=c++11

COMPILE = $(CC) $(CFLAGS)  $(INCLUDES)
CXXCOMPILE = $(CXX) $(CXXFLAGS)  $(INCLUDES)
CXX_LINK_EXE = $(CXX) $(CXXFLAGS) -Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld
CXX_LINK_SHARED = $(CXX) $(CXXFLAGS) -shared -fPIC -Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld

# File path config (internal)
MODC_DIR = $(OUTPUT)
OBJS_DIR = $(OUTPUT)
mod_files = $(sort $(notdir $(wildcard $(MODS_PATH)/*.mod)))
mod_names = $(mod_files:.mod=)
modc_files = $(addprefix $(MODC_DIR)/,$(addsuffix .c,$(mod_names)))
mod_objs   = $(addprefix $(OBJS_DIR)/,$(addsuffix .o,$(mod_names)))

mod_func_o = $(OBJS_DIR)/mod_func.o
mod_func_c = $(MODC_DIR)/mod_func.c

special  = $(OUTPUT)/special
LIB_SUFFIX_ = $(if $(MECH_NAME),_$(MECH_NAME),)
mech_libname = nrnmech
mech_lib = $(OUTPUT)/lib$(mech_libname).so

# If no DESTDIR (we are probably just building) we use $ORIGIN (@loader_path in OSX)
_ORIGIN := $(if $(filter Darwin,$(OS_NAME)),@loader_path,$$ORIGIN)
_SONAME := -Wl,$(if $(filter Darwin,$(OS_NAME)),-install_name${_cm}@rpath/,-soname${_cm})$(notdir ${mech_lib})
DESTDIR_RPATH = $(if $(DESTDIR),$(DESTDIR)/lib,$(_ORIGIN))

C_RESET := \033[0m
C_GREEN := \033[32m


# ======== MAIN BUILD RULES ============

# Take the main and link with nrnmech.
# RPATH is set for DESTDIR_RPATH and coreneuron lib
special: $(mech_lib)
	@printf " => $(C_GREEN)LINKING$(C_RESET) executable $(special) LDFLAGS are: $(LDFLAGS)\n"
	(cd .. ;  $(CXX_LINK_EXE) -I $(incdir) -I $(incdir)/nrncvode -DAUTO_DLOPEN_NRNMECH=0 $(datadir)/nrnmain.cpp -o $(special) \
	  -L $(OBJS_DIR) -l$(mech_libname) $(NRNLIB_FLAGS) -l$(mech_libname) $(LDFLAGS))

$(mech_lib): mod_func.o $(mod_objs) build_always
	@printf " => $(C_GREEN)LINKING$(C_RESET) library $(mech_lib) Mod files: $(mod_files)\n"
	(cd .. ; $(CXX_LINK_SHARED) -I $(incdir) -o ${mech_lib} ${_SONAME} \
	  $(mod_func_o) $(mod_objs) $(NRNLIB_FLAGS) $(LDFLAGS))
	(cd .. ; rm -f $(OBJS_DIR)/.libs/libnrnmech.so ; mkdir -p $(OBJS_DIR)/.libs ; ln -s ../../$(mech_lib) $(OBJS_DIR)/.libs/libnrnmech.so)

mod_func.o: mod_func.c
	@printf " -> $(C_GREEN)Compiling$(C_RESET) $<\n"
	$(COMPILE) -fPIC -c $< -o $@

# Generic build c->o. Need PIC for shared lib
$(OBJS_DIR)/%.o: $(MODC_DIR)/%.c | $(OBJS_DIR)
	@printf " -> $(C_GREEN)Compiling$(C_RESET) $<\n"
	(cd .. ; $(COMPILE) -fPIC -c $< -o $@)


# Build c files with nocmodl_openmpi
$(MODC_DIR)/%.c: $(MODS_PATH)/%.mod | $(MODC_DIR)
	@printf " -> $(C_GREEN)NMODL$(C_RESET) $<\n"
	MODLUNIT=$(datadir_lib)/nrnunits.lib \
	  $(bindir)/nocmodl_openmpi $<

# If .mod doesnt exist attempt from previously built opt mods in shared/
$(MODC_DIR)/%.cpp: $(datadir_lib)/%.cpp | $(MODC_DIR)
	ln -s $< $@

install: special $(mech_lib)
	install -d $(DESTDIR)/bin $(DESTDIR)/lib
	install $(mech_lib) $(DESTDIR)/lib
	install $(special) $(DESTDIR)/bin

libnrnmech.la: $(mech_lib)

# == INIT ==
$(MODC_DIR):
	mkdir -p $(MODC_DIR)
#$(OBJS_DIR):
#	mkdir -p $(OBJS_DIR)

.PHONY: build_always

$(VERBOSE).SILENT:
