From ea6eaccfe49022f936e5481f448ba87b479f5e9b Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Mon, 16 Jan 2023 18:16:44 +0100
Subject: [PATCH] Prepare building on non-Linux systems and respect SICSTUSDIR

---
 Makefile                     |  4 +++-
 src/common.mk                | 37 +++++++++++++++++++++++++++++++++++-
 src/helper_files/Makefile    |  4 ++--
 src/methods/method_common.mk |  6 +++---
 4 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile
index 4f91bd7..19c84b2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,5 @@
+include src/common.mk
+
 methods := \
 	adaboost \
 	approx_kfn \
@@ -56,4 +58,4 @@ clean: clean_helper $(clean_methods)
 
 .PHONY: test
 test: methods
-	sicstus -l test_all.pl --goal "run_tests, halt ; halt(1)."
+	$(sicstus) -l test_all.pl --goal "run_tests, halt ; halt(1)."
diff --git a/src/common.mk b/src/common.mk
index 15577fb..a818719 100644
--- a/src/common.mk
+++ b/src/common.mk
@@ -1,3 +1,38 @@
-splfr=/usr/local/sicstus4.7.1/bin/splfr
+# by default, the target system is the same as the compiling system
+TARGET_SYSTEM := $(shell uname -s)
+# Under MinGW, Cygwin, MSYS2, etc. on Windows,
+# uname -s has the format "<name>_NT-<version>",
+# e. g. "MINGW64_NT-10.0-19044" or "CYGWIN_NT-10.0".
+# To simplify checking for Windows,
+# we detect this format and replace it with simply "Windows".
+ifneq ($(findstring NT-,$(TARGET_SYSTEM)),)
+	TARGET_SYSTEM := Windows
+endif
+
+ifeq ($(TARGET_SYSTEM),Windows)
+	lib_ext := dll
+	foreign_ext := dll
+	bin_suffix := .exe
+else
+	# If not Windows, assume Unix-like.
+	
+	ifeq ($(TARGET_SYSTEM),Darwin)
+		lib_ext := dylib
+		foreign_ext := bundle
+	else
+		lib_ext := so
+		foreign_ext := so
+	endif
+	
+	bin_suffix :=
+endif
+
+ifdef SICSTUSDIR
+	sicstus = $(SICSTUSDIR)/bin/sicstus$(bin_suffix)
+	splfr = $(SICSTUSDIR)/bin/splfr$(bin_suffix)
+else
+	sicstus := sicstus
+	splfr := splfr
+endif
 
 common_splfr_flags := -larmadillo -fopenmp -lmlpack -lstdc++ -cxx --struct
diff --git a/src/helper_files/Makefile b/src/helper_files/Makefile
index 58e17fb..9739c7c 100644
--- a/src/helper_files/Makefile
+++ b/src/helper_files/Makefile
@@ -2,7 +2,7 @@ include ../common.mk
 
 METHOD_NAME=helper_tests
 
-$(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp
+$(METHOD_NAME).$(foreign_ext): $(METHOD_NAME).pl $(METHOD_NAME).cpp
 	$(splfr) $(common_splfr_flags) $(CXXFLAGS) $(LDFLAGS) $(METHOD_NAME).pl $(METHOD_NAME).cpp helper.cpp $(LDLIBS)
 clean:
-	rm $(METHOD_NAME).so
+	$(RM) $(METHOD_NAME).bundle $(METHOD_NAME).dll $(METHOD_NAME).so
diff --git a/src/methods/method_common.mk b/src/methods/method_common.mk
index 66d3d85..b001be8 100644
--- a/src/methods/method_common.mk
+++ b/src/methods/method_common.mk
@@ -5,7 +5,7 @@
 # which is one directory below this one.
 include ../../common.mk
 
-$(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp
-	$(splfr) $(common_splfr_flags) $(METHOD_NAME).pl $(METHOD_NAME).cpp ../../helper_files/helper.cpp
+$(METHOD_NAME).$(foreign_ext): $(METHOD_NAME).pl $(METHOD_NAME).cpp
+	$(splfr) $(common_splfr_flags) $(CXXFLAGS) $(LDFLAGS) $(METHOD_NAME).pl $(METHOD_NAME).cpp ../../helper_files/helper.cpp $(LDLIBS)
 clean:
-	rm $(METHOD_NAME).so
+	$(RM) $(METHOD_NAME).bundle $(METHOD_NAME).dll $(METHOD_NAME).so
-- 
GitLab