# Modified to replace the autoconf processed file nrnmech_makefile.in

# Arguments:
# MODOBJFILES
# COBJFILES
# UserLDFLAGS
# UserINCFLAGS
# LinkCoreNEURON
# Rules to build MODOBJFILES from mod files and COBJFILES from $nrnivmodl_cfiles
# are found in makemod2c_inc

# Mechanisms version are by default 0.0, but should be overriden
MECH_NAME =
MECH_VERSION = 0.0
MODS_PATH = .
# in the x86_64 folder
OUTPUT = .
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.11 -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. $(INCFLAGS) $(UserINCFLAGS) -I$(incdir)
INCLUDES += $(if , -I$(subst ;, -I,),)

# CC/CXX are always defined. If the definition comes from default change it
ifeq ($(origin CC), default)
    CC = /usr/bin/gcc
    CXX = /usr/bin/g++
endif
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  -m64  -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-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  -m64  -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -std=c++11

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

NOCMODL = $(bindir)/nocmodl
NRNUNITS = $(datadir_lib)/nrnunits.lib

# File path config (internal)
MODC_DIR = .
OBJS_DIR = .
mod_objs   = $(MODOBJFILES)
c_objs = $(COBJFILES)

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

# In case of static library we need to link NRNLIB_FLAGS twice to avoid undefined
# symbols from circular dependencies. But for shared library we don't do it to avoid
# pthread linking with wheels.
ifeq (ON, ON)
    mech_lib = $(OUTPUT)/lib$(mech_libname).so
    mech_lib_type = mech_lib_shared
    extra_lib_link =
else
    mech_lib = $(OUTPUT)/lib$(mech_libname).a
    mech_lib_type = mech_lib_static
    extra_lib_link = $(NRNLIB_FLAGS)
endif

# add coreneuron flags
ifeq ($(LinkCoreNEURON), true)
    EXTRA_LDFLAGS = 
endif

# 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"
	$(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) $(extra_lib_link) $(LDFLAGS) $(EXTRA_LDFLAGS)

$(mech_lib): $(mech_lib_type)

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

mech_lib_static: mod_func.o $(mod_objs) $(c_objs) build_always
	@printf " => $(C_GREEN)LINKING$(C_RESET) static library $(mech_lib)\n"
	ar cq ${mech_lib} $(mod_func_o) $(mod_objs) $(cobjs);

mod_func.o: mod_func.c
	@printf " -> $(C_GREEN)Compiling$(C_RESET) $<\n"
	$(COMPILE) $(INCLUDES) -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"
	$(COMPILE) $(INCLUDES) -fPIC -c $< -o $@


include makemod2c_inc

# 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):
	(cd .. ; mkdir -p $(MODC_DIR))
#$(OBJS_DIR):
#	mkdir -p $(OBJS_DIR)

.PHONY: build_always

$(VERBOSE).SILENT:
