PKG_CPPFLAGS=-I../inst/swipl/lib/swipl/include -D_REENTRANT -D__SWI_PROLOG__ -D__SWI_EMBEDDED__
PKG_LIBS=`pkg-config ../inst/swipl/share/pkgconfig/swipl.pc --libs` -Wl,-rpath,'$$ORIGIN/../swipl/lib'
PKG_CXXFLAGS=$(CXX_VISIBILITY) 

CMAKE_VERSION := $(shell cmake --version 2>/dev/null)
CMAKE_VERSION_MACOS := $(shell /Applications/CMake.app/Contents/bin/cmake --version 2>/dev/null)
CTEST_VERSION := $(shell ctest --version 2>/dev/null)
CTEST_VERSION_MACOS := $(shell /Applications/CMake.app/Contents/bin/ctest --version 2>/dev/null)

BUILD_TYPE=$(if $(findstring -fsanitize=address,$(CXX)),Sanitize,Release)

ASAN=$(if $(and $(if $(findstring clang++,$(CXX)),,true),$(findstring g++,$(CXX)),$(findstring -fsanitize=address,$(CXX))),$(realpath $(shell $(CXX) -print-file-name=libasan.so)))
STDCXX=$(if $(findstring -fsanitize=address,$(CXX)),$(realpath $(shell $(CXX) -print-file-name=libstdc++.so)))
PRELOAD_CTEST=$(if $(STDCXX),LD_PRELOAD=$(ASAN):$(STDCXX))

CFLAGS1=$(subst -pedantic,,$(subst -Wstrict-prototypes,,$(CFLAGS)))

all: $(SHLIB)

$(SHLIB): ../build/done

RcppExports.o: ../build/done

rswipl.o: ../build/done

../build/done:
	-patch -N -p2 < ../inst/patch/02-clib.patch
	-patch -N -p2 < ../inst/patch/03-cpp.patch
	-patch -N -p2 < ../inst/patch/04-cpp.patch
	mkdir -p ../build
ifdef CMAKE_VERSION
	(cd ../build ; CC="$(CC)" CXX="$(CXX)" CFLAGS="$(CFLAGS1)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" CXXFLAGS="$(CXX17FLAGS) $(CXX17PICFLAGS)" cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX=../inst/swipl -DMACOSX_DEPENDENCIES_FROM=Homebrew -DSWIPL_INSTALL_IN_LIB=ON -DINSTALL_DOCUMENTATION=OFF -DBUILD_TESTING=ON -DINSTALL_TESTS=ON -DSWIPL_PACKAGES_JAVA=OFF -DSWIPL_PACKAGES_GUI=OFF -DSWIPL_PACKAGES_ODBC=OFF -DSWIPL_PACKAGES_PYTHON=OFF -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -S ../src/swipl-devel)
	LD_LIBRARY_PATH=$(CURDIR)/../build/src:${LD_LIBRARY_PATH} cmake --build ../build
	cmake --install ../build
else
ifdef CMAKE_VERSION_MACOS
	(cd ../build ; CC="$(CC)" CXX="$(CXX)" CFLAGS="$(CFLAGS1)" CPPFLAGS="$(CPPFLAGS) -I/opt/R/arm64/include -I/Library/Developer/CommandLineTools/usr/include/c++/v1" LDFLAGS="-L/opt/R/arm64/lib $(LDFLAGS)" CXXFLAGS="$(CXXFLAGS)" /Applications/CMake.app/Contents/bin/cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX=../inst/swipl -DMACOSX_DEPENDENCIES_FROM=Homebrew -DSWIPL_INSTALL_IN_LIB=ON -DINSTALL_DOCUMENTATION=OFF -DBUILD_TESTING=ON -DINSTALL_TESTS=ON -DSWIPL_PACKAGES_JAVA=OFF -DSWIPL_PACKAGES_GUI=OFF -DSWIPL_PACKAGES_ODBC=OFF -DSWIPL_PACKAGES_PYTHON=OFF -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -S ../src/swipl-devel)
	/Applications/CMake.app/Contents/bin/cmake --build ../build
	/Applications/CMake.app/Contents/bin/cmake --install ../build
else
	$(error Please install cmake on your system)
endif
endif
ifdef CTEST_VERSION
	-(cd ../build ; LD_LIBRARY_PATH=$(CURDIR)/../build/src:${LD_LIBRARY_PATH} $(PRELOAD_CTEST) ctest --output-on-failure)
else
ifdef CTEST_VERSION_MACOS
	-(cd ../build ; /Applications/CMake.app/Contents/bin/ctest --output-on-failure)
else
	$(message Please install ctest on your system)
endif
endif
	rm ../inst/swipl/bin/swipl
	rm ../inst/swipl/bin/swipl-ld
	-rm ../inst/swipl/lib/swipl/bin/*/swipl
	-rm ../inst/swipl/lib/swipl/bin/*/swipl-ld
	touch ../build/done
