
Default::

all:

# Point this to the unpacked content of LetsBeRational.7z as downloaded from http://www.jaeckel.org/ in section 'Open-source Reference Implementation of "Let's Be Rational"'.
LBR_DIR=../LetsBeRational

CURDIRBASE := $(shell basename "$(CURDIR)")
TARGET := $(subst XLL,,$(CURDIRBASE))

# For WIN32 (automatically defines C preprocessor macros WIN32 and _WIN32, works with Cygwin's gcc version 7.4.0):
CC=i686-w64-mingw32-g++

# For WIN64 (automatically defines C preprocessor macro WIN32, _WIN32, WIN64, and _WIN64, works with Cygwin's gcc version 7.4.0):
#CC=x86_64-w64-mingw32-g++

# DEFINES += VERBOSE_DEBUG_LOG

# If XL_CATEGORY_NAME is not defined, the Excel function category is determined dynamically as the XLL's base file name (without path and without extension).
DEFINES += XL_CATEGORY_NAME=$(TARGET)

# You can use this syntax to convert Microsoft "BOM"-marked UTF8 files into files that preserver UTF8 but are also understood by gcc.
Example_in_utf8_without_bom.cpp:	Example_in_utf8_with_bom.cpp
	uconv --to-code UTF-8 --remove-signature  $< | dos2unix > $@

HEADERS := XLCall.h
SOURCES := main.cpp XLOper.cpp XLFunctions.cpp $(TARGET).cpp

#EXTRA_OBJECTS := other.o # in case there are other precompiled object files to be consumed into the target XLL
EXTRA_OBJECTS := 

# Include directories
INCDIRS := .

HEADERS += $(LBR_DIR)/normaldistribution.h
SOURCES += $(LBR_DIR)/erf_cody.cpp $(LBR_DIR)/normaldistribution.cpp
INCDIRS += $(LBR_DIR)

DEFFLAGS := $(DEFINES:%=-D%)

INCFLAGS := $(INCDIRS:%=-I%)

LIBFLAGS    =

CCFLAGS = -finput-charset=UTF-8 -DNDEBUG -O3 -fextended-identifiers

LDFLAGS = -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic -Wl,--kill-at

# ===========================================================================================================
#
# WIN32 build with Microsoft Visual Studio 16.11.21 (2019) [_MSC_VER=1929, see https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros]:
#
#   call "%VS160COMNTOOLS%\..\..\VC\Auxiliary\Build\vcvarsall.bat" x86
#
# Note that the invocation of cl.exe automatically defines WIN32 and _WIN32 in this configuration.
#
# Build DLL (we use /MT for static linkage):
#   cl.exe main.cpp ImpliedNormalVolatility.cpp ..\LetsBeRational\erf_cody.cpp ..\LetsBeRational\normaldistribution.cpp XLFunctions.cpp XLOper.cpp /I . /I ..\LetsBeRational /EHa /Ox /LD /MT /D XL_CATEGORY_NAME=ImpliedNormalVolatility /D NDEBUG /D _WINDOWS /D _USRDLL /D _WINDLL /link /NOLOGO /IMPLIB:ImpliedNormalVolatility.lib /OUT:ImpliedNormalVolatility.xll /DLL /MANIFEST /MANIFESTFILE:ImpliedNormalVolatility.xll.manifest /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DYNAMICBASE /NXCOMPAT /MACHINE:X86
#
# ===========================================================================================================
#
# WIN64 build with Microsoft Visual Studio 16.11.21 (2019) [_MSC_VER=1929, see https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros]:
# 
#   call "%VS160COMNTOOLS%\..\..\VC\Auxiliary\Build\vcvarsall.bat" x64
#
# Note that the invocation of cl.exe automatically defines WIN32, _WIN32, and _WIN64 in this configuration.
#
# Build DLL (we use /MT for static linkage):
#   cl.exe main.cpp ImpliedNormalVolatility.cpp ..\LetsBeRational\erf_cody.cpp ..\LetsBeRational\normaldistribution.cpp XLFunctions.cpp XLOper.cpp /I . /I ..\LetsBeRational /EHa /Ox /LD /MT /D XL_CATEGORY_NAME=ImpliedNormalVolatility /D NDEBUG /D _WINDOWS /D _USRDLL /D _WINDLL /link /NOLOGO /IMPLIB:ImpliedNormalVolatility.lib /OUT:ImpliedNormalVolatility.xll /DLL /MANIFEST /MANIFESTFILE:ImpliedNormalVolatility.xll.manifest /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DYNAMICBASE /NXCOMPAT /MACHINE:X64
#
# ===========================================================================================================

other.o:	./other.cpp Makefile
	$(CC) $(CCFLAGS) $(INCFLAGS) -c $< -o $@

$(TARGET).xll:	Makefile $(HEADERS) $(SOURCES) $(EXTRA_OBJECTS)
	$(CC) $(CCFLAGS) $(DEFFLAGS) $(INCFLAGS) $(SOURCES) $(EXTRA_OBJECTS) $(LIBFLAGS) $(LDFLAGS) -shared -s -o "$@"

clean:
	rm -f $(EXTRA_OBJECTS) *.obj *.o *.exp *.lib *.pdb *.idb *.ndb *.ncb *~

veryclean:	clean
	rm -f Example_in_utf8_without_bom.cpp

sterile:	veryclean
	rm -f $(TARGET).xll

Default::	$(TARGET).xll

all:	$(TARGET).xll veryclean

archive:
	cd .. ; mkdir -p 7z; mv -f 7z/"$(TARGET)".7z /tmp; rm -f 7z/"$(TARGET)".7z; 7z a -t7z -m0=lzma -xr\!.svn 7z/"$(TARGET)".7z "$(CURDIRBASE)"
