# Builds the INSEL user blocks in ../src into libInselUB, in the current directory.
# It is installed in INSEL_HOME, and the GUI runs it from insel.work/inselUB/resources:
#   make -f <INSEL_HOME>/makefile
# From this repository: mkdir -p build && make -C build -f ../makefile

sourcesF := $(wildcard ../src/*.f)
sourcesF90 := $(wildcard ../src/*.f90)
sourcesC := $(wildcard ../src/*.cpp)
objects := $(notdir $(sourcesF:.f=.o) $(sourcesF90:.f90=.o) $(sourcesC:.cpp=.o)) insel_library.o
# Block files start with ub, in lower case. Each one defines the block function of the same name,
# e.g. SUBROUTINE UBFOR in ubfor.f, which is listed in insel_library(). Other files may contain helper functions.
empty :=
comma := ,
blocks := $(filter ub%,$(basename $(notdir $(sourcesF) $(sourcesF90) $(sourcesC))))
INSEL_BLOCKS := $(subst $(empty) $(empty),$(comma),$(strip $(blocks)))

MODULE = libInselUB
FCFLAGS = -g -Og -c -Wall -Wno-intrinsic-shadow -Wno-unused-dummy-argument -Wno-integer-division -fno-underscoring -fdefault-real-8 -fdefault-double-8 -fmessage-length=0 -Werror=line-truncation -finit-real=snan -finit-integer=-999999999
CCFLAGS = -std=c++17 -g -Og -c -Wall -fmessage-length=0

ifeq ($(OS),Windows_NT)
  ARCHITECTURE = Windows
  # INSEL_HOME is set by the installer
  LIBRARY = $(MODULE).dll
  FC = g++
  CC = g++
  L = g++
  RM = del
  FCFLAGS += -m64
  CCFLAGS += -m64
  LFLAGS = -g -m64 -shared -L"$(INSEL_HOME)" -lInselTools -lgfortran
else ifeq ($(shell uname),Linux)
  ARCHITECTURE = Linux
  INSEL_HOME ?= /usr/local/insel
  LIBRARY = $(MODULE).so
  FC = g++
  CC = g++
  L = g++
  RM = rm -f
  M64 = $(if $(filter x86_64,$(shell uname -m)),-m64)
  FCFLAGS += $(M64) -fPIC -std=legacy
  CCFLAGS += $(M64) -fPIC
  # --no-undefined: blocks written for an older API fail to link, e.g. undefined reference to `id`
  # -lgfortran: Fortran runtime, e.g. for WRITE or TRIM, as in INSEL block libraries
  LFLAGS = -g $(M64) -shared -Wl,--no-undefined -L$(INSEL_HOME) -lInselTools -ldl -lgfortran
else
  ARCHITECTURE = macOS
  INSEL_HOME ?= /usr/local/insel
  LIBRARY = $(MODULE).dylib
  # Same compilers as INSEL itself: gfortran (brew install gcc) for Fortran, clang/libc++ for C++
  FC = $(shell brew --prefix gcc)/bin/gfortran
  CC = clang++
  L = clang++
  RM = rm -f
  export MACOSX_DEPLOYMENT_TARGET = 14.0
  LFLAGS = -g -dynamiclib -Wl,-install_name,@loader_path/$(LIBRARY) -L$(INSEL_HOME) -lInselTools $(INSEL_HOME)/libgfortran.5.dylib
endif
# Printed if linking fails. Kept free of quotes and parentheses, so that both sh and cmd.exe can echo it.
API_HINT = HINT: An undefined reference to id or ID comes from a block written for an older INSEL version. \
  Please update it to the new block API, e.g. by creating a new user block and copying the computation into it.
# insel_block.inc, insel_block.h and insel_library.cpp
FCFLAGS += -I"$(INSEL_HOME)/include"
CCFLAGS += -I"$(INSEL_HOME)/include"

all:
	@echo Compile $(MODULE) for $(ARCHITECTURE)
ifeq ($(OS),Windows_NT)
# A loaded DLL cannot be overwritten, but it can be moved away. Comments must not be indented: cmd.exe would run them.
	@powershell.exe -ExecutionPolicy Bypass -File "$(INSEL_HOME)\ub_helper.ps1"
endif
ifneq ($(sourcesF),)
	@$(FC) $(FCFLAGS) $(sourcesF)
	@echo Fortran user blocks compiled.
endif
ifneq ($(sourcesF90),)
	@$(FC) $(FCFLAGS) $(sourcesF90)
	@echo Fortran90 user blocks compiled.
endif
ifneq ($(sourcesC),)
	@$(CC) $(CCFLAGS) $(sourcesC)
	@echo C++ user blocks compiled.
endif
	@$(CC) $(CCFLAGS) "$(INSEL_HOME)/include/insel_library.cpp" -DINSEL_BLOCKS=$(INSEL_BLOCKS)
	@$(L) $(objects) $(LFLAGS) -o $(LIBRARY) || ( echo $(API_HINT) && exit 1 )
ifeq ($(ARCHITECTURE),macOS)
# INSEL's dylibs use @loader_path install names, which would resolve to this directory instead of INSEL_HOME
	@install_name_tool -change @loader_path/libInselTools.dylib $(INSEL_HOME)/libInselTools.dylib $(LIBRARY)
	@install_name_tool -change @loader_path/libgfortran.5.dylib $(INSEL_HOME)/libgfortran.5.dylib $(LIBRARY)
	@codesign -f -s - $(LIBRARY)
endif
	@echo $(MODULE) completed.
	@$(RM) *.o

clean:
	@$(RM) *.o $(LIBRARY)
