OUT_PATH  = lib
SRC_PATH  = jni
INC_PATH  = $(SRC_PATH)/include

ifneq ($(PREFIX),)
    INSTALL_DIR = $(DESTDIR)$(PREFIX)
else
    INSTALL_DIR = $(DESTDIR)/usr/local
endif

ifeq ($(LIBDIR),)
    LIBDIR=lib
endif

ifeq ($(INSTALL),)
    INSTALL=install
endif

ifeq ($(WOLFSSL_LIBNAME),)
    WOLFSSL_LIBNAME=wolfssl
endif

OBJ_LIST  = jni_fips.o jni_native_struct.o jni_pwdbased.o jni_aes.o \
			jni_aesgcm.o jni_des3.o jni_md5.o jni_sha.o jni_hmac.o jni_rng.o \
			jni_rsa.o jni_dh.o jni_ecc.o jni_ed25519.o jni_curve25519.o \
			jni_chacha.o jni_error.o jni_asn.o jni_logging.o \
			jni_feature_detect.o jni_wolfobject.o jni_wolfcrypt.o \
			jni_wolfssl_cert_manager.o jni_jce_wolfsslkeystore.o
OBJS      = $(patsubst %,$(OUT_PATH)/%,$(OBJ_LIST))
TARGET    = $(OUT_PATH)/libwolfcryptjni.dylib

JAVA_HOME ?= $(shell /usr/libexec/java_home)
CC        = gcc
override CCFLAGS   += -Wall -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin \
			-I$(INC_PATH) -I$(INSTALL_DIR)/include
override LDFLAGS   += -dynamiclib -L$(INSTALL_DIR)/$(LIBDIR) -l$(WOLFSSL_LIBNAME)

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CCFLAGS) $(LDFLAGS) -o $@ $^

$(OUT_PATH)/%.o: $(SRC_PATH)/%.c
	@mkdir -p $(OUT_PATH)
	$(CC) $(CCFLAGS) -c -o $@ $<

.PHONY: clean

clean:
	rm -f $(OUT_PATH)/*.o $(TARGET)
