# TLS Examples Makefile
CC       = gcc
WOLFSSL_INSTALL_DIR = /usr/local
CFLAGS   = -Wall -I$(WOLFSSL_INSTALL_DIR)/include
LIBS     = -L$(WOLFSSL_INSTALL_DIR)/lib -lm

# option variables
DYN_LIB         = -lwolfssl
STATIC_LIB      = $(WOLFSSL_INSTALL_DIR)/lib/libwolfssl.a
DEBUG_FLAGS     = -g -DDEBUG
DEBUG_INC_PATHS = -MD
OPTIMIZE        = -Os
DEPS            =

# Options
#CFLAGS+=$(DEBUG_FLAGS)
CFLAGS+=$(OPTIMIZE)
#CFLAGS+=$(QAT_FLAGS)
#LIBS+=$(QAT_LIBS)
#LIBS+=$(STATIC_LIB)
LIBS+=$(DYN_LIB)

# Openssl Option
#CFLAGS+=-DUSE_OPENSSL
#LIBS+=-lcrypto

# build targets
SRC=$(wildcard *.c)
IGNORE_FILES=cryptocb-common
TARGETS=$(filter-out $(IGNORE_FILES), $(patsubst %.c, %, $(SRC)))
LINUX_SPECIFIC=client-tls-perf \
               server-tls-poll-perf \
               server-tls-epoll-perf \
               server-tls-epoll-threaded


# Intel QuickAssist
QAT_PATH=../../QAT1.7
QAT_FLAGS=-DDO_CRYPTO -DUSER_SPACE \
    -I$(QAT_PATH)/quickassist/include \
    -I$(QAT_PATH)/quickassist/include/lac \
    -I$(QAT_PATH)/quickassist/utilities/osal/include \
    -I$(QAT_PATH)/quickassist/utilities/osal/src/linux/user_space/include \
    -I$(QAT_PATH)/quickassist/lookaside/access_layer/include \
    -I$(QAT_PATH)/quickassist/lookaside/access_layer/src/common/include \
    -I$(QAT_PATH)/quickassist/utilities/libusdm_drv
QAT_LIBS=-L$(QAT_PATH) -lqat_s -lusdm_drv_s -lpthread

# OS / CPU Detection
OS_DET=UNKNOWN
CPU_DET=UNKNOWN
ifeq ($(OS),Windows_NT)
    OS_DET=WIN32
    ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
        CPU_DET=AMD64
    else
        ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
            CPU_DET=AMD64
        endif
        ifeq ($(PROCESSOR_ARCHITECTURE),x86)
            CPU_DET=IA32
        endif
    endif
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        OS_DET=LINUX
    endif
    ifeq ($(UNAME_S),Darwin)
        OS_DET=OSX
    endif
    UNAME_P := $(shell uname -p)
    ifeq ($(UNAME_P),x86_64)
        CPU_DET=AMD64
    endif
    ifneq ($(filter %86,$(UNAME_P)),)
        CPU_DET=IA32
    endif
    ifneq ($(filter arm%,$(UNAME_P)),)
        CPU_DET=ARM
    endif
endif

# $(info $$OS_DET is [${OS_DET}])
# $(info $$CPU_DET is [${CPU_DET}])

.PHONY: clean all

ifneq ($(OS_DET),LINUX)
all: $(filter-out $(LINUX_SPECIFIC), $(TARGETS))
else
all: $(TARGETS)
endif

debug: CFLAGS+=$(DEBUG_FLAGS)
debug: all

# add the -pthread flag to any threaded examples
%-threaded: CFLAGS+=-pthread
%-writedup: CFLAGS+=-pthread
memory-tls: CFLAGS+=-pthread

# compile tcp examples without the LIBS variable
%-tcp: LIBS=

%-cryptocb: DEPS+=cryptocb-common.c

# build template
%: %.c
	$(CC) -o $@ $(DEPS) $< $(CFLAGS) $(LIBS)

clean:
	rm -f $(TARGETS)
