# Makefile for building towerops_nif.so

# Erlang NIF paths
ERL_INCLUDE_PATH ?= $(shell erl -eval 'io:format("~s", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)

# Detect OS
UNAME_S := $(shell uname -s)

# Compiler flags
CFLAGS = -O3 -std=c99 -fPIC -Wall -Wextra -Wno-unused-parameter
CFLAGS += -I$(ERL_INCLUDE_PATH)

# Libraries (must come after source files in link command)
LIBS = -lnetsnmp

ifeq ($(UNAME_S),Darwin)
    # macOS specific flags
    CFLAGS += -dynamiclib -undefined dynamic_lookup
else
    # Linux flags
    CFLAGS += -shared
endif

# Target
TARGET = ../priv/towerops_nif.so

# Source files
SRC = towerops_nif.c

all: $(TARGET)

$(TARGET): $(SRC)
	@mkdir -p ../priv
	$(CC) $(CFLAGS) -o $@ $< $(LIBS)

clean:
	rm -f $(TARGET)

.PHONY: all clean
