# Makefile

# Get the include path for Erlang headers
ERLANG_PATH := $(shell erl -eval 'io:format("~s", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)

# Define path to the FIT SDK
FIT_SDK_DIR := c_src/fit_sdk/cpp

# Use g++ for C++ compilation
CXX := g++

# Define all C++ source files: your nif.cpp + all .cpp files in the SDK
SOURCES := c_src/nif.cpp $(wildcard $(FIT_SDK_DIR)/*.cpp)

# Define compiler flags, including include paths for Erlang and the FIT SDK
# -fPIC is necessary for creating a shared library
CXXFLAGS := -fPIC -I"$(ERLANG_PATH)" -I"$(FIT_SDK_DIR)"

# --- OS-specific linker flags ---
# Detect the operating system
UNAME_S := $(shell uname -s)

# Set linker flags based on the OS
ifeq ($(UNAME_S), Darwin)
	# macOS linker flags for creating a dynamic library
	LDFLAGS := -undefined dynamic_lookup -dynamiclib
else ifeq ($(UNAME_S), Linux)
	# Linux linker flags for creating a shared library
	LDFLAGS := -shared
endif
# --- End OS-specific flags ---

# Define the output file
OUTPUT := priv/nif.so

# Default target
all: $(OUTPUT)

# Rule to build the output file
$(OUTPUT): $(SOURCES)
	@mkdir -p priv
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)

# Rule to clean up build artifacts
clean:
	rm -f "$(OUTPUT)"
