# sample 
CC=g++
CFLAGS+=-c -pthread -std=c++11
LFLAGS+=-pthread

obj_dir ?= $(BUILD_DIR)/obj/sample
lib_obj_dir ?= $(BUILD_DIR)/obj/library

exe_sample := $(BUILD_DIR)/capisample
exe_sample-dyn := $(BUILD_DIR)/capisample-dyn
sources := $(wildcard ./src/*.cpp)
objects = $(sources:%.cpp=$(obj_dir)/%.o)
lib_objects = $(wildcard $(lib_obj_dir)/src/*.o)
include_dirs := ../library/include
CPPFLAGS += $(addprefix -I ,$(include_dirs))

# On Mac, rpath is set differently than on Linux
# Specify where OpenSSL is installed since Mac and Linux differ
ifeq ($(PLATFORM),macosx)
OPENSSL_LIB=/usr/local/opt/openssl@1.1/lib
LFLAGS+=-L$(OPENSSL_LIB) -Wl,-rpath,'@executable_path'
else
LFLAGS+=-Wl,-rpath,'$$ORIGIN'
endif

all: $(exe_sample)

# Sample app statically linked to -lndicapi
# Note: g++ requires LFLAGS_CRYPTO at the end for some reason
$(exe_sample): $(objects) $(exe_sample-dyn)
	$(CXX) $(LFLAGS) $(lib_objects) $(objects) -o $@ $(LFLAGS_CRYPTO)
	@echo "sample exe successful!"

# Sample app dynamically linked to -lndicapi
# Note: g++ requires LFLAGS_CRYPTO at the end for some reason
$(exe_sample-dyn): $(objects) $(LIB_LIBRARY)
	$(CXX) $(objects) -L$(BUILD_DIR) -lndicapi $(LFLAGS) -o $@ $(LFLAGS_CRYPTO)
	@echo "dyn sample exe successful!"

$(objects): $(obj_dir)/%.o: %.cpp
	@echo Compiling $<
	mkdir -p $(@D)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $<

.PHONY: clean
clean:
	@echo "doing clean in sample library"
	$(RM) -r $(obj_dir)
	$(RM) -f $(exe_sample) $(exe_sample-dyn)
