# Note: Makefile for this example is a GNU Makefile and is designed to be used with a native x86_64 Linux toolchain.

DPI_SDK ?= ../../../

# Engine and Bundle build information
include $(DPI_SDK)/src/mk/qmdpi_engine_info.mk
include $(DPI_SDK)/src/mk/qmdpi_bundle_info.mk

# app name
DPI_APP = pdb_swap

# list of source files
SRC +=  \
        main.c

# optional CFLAGS
#CFLAGS +=
# optional LDFLAGS
LDFLAGS += -lpcap -ldl

# specific LDFLAGS for the application
LDFLAGS_EXTRA_LIBS = -lpthread -lrt

STATIC_LDFLAGS_LIBS = -Wl,--whole-archive -Wl,--start-group  \
        $(DPI_SDK)/lib/libqmengine.a \
        $(DPI_SDK)/lib/libqmbundle.a \
        -Wl,--end-group -Wl,--no-whole-archive

DYNAMIC_LDFLAGS_LIBS = -Wl,--whole-archive -Wl,--start-group -L$(DPI_SDK)/lib/  \
        -lqmengine \
        -lqmbundle \
        -Wl,--end-group -Wl,--no-whole-archive

ifeq ($(STATIC),1)
LDFLAGS_LIBS = $(STATIC_LDFLAGS_LIBS)
else
LDFLAGS_LIBS = $(DYNAMIC_LDFLAGS_LIBS)
endif

# define default compiler
CC     = gcc
AR     = ar
RANLIB = ranlib
LD     = ld

CFLAGS_WARNING += -Wall -Wextra -Wno-comment -Wno-sign-compare -Wno-missing-field-initializers \
                  -Wstrict-prototypes -Wno-unused-parameter -Werror

CFLAGS_DEFINES += -D_GNU_SOURCE
CFLAGS_INCLUDE += -I$(DPI_SDK)/include -I$(DPI_SDK)/src/include

ifeq ($(DEBUG),1)
CFLAGS_OPT += -O0 -g
else
CFLAGS_OPT += -O2
endif

# create final flags 
CFLAGS  += $(CFLAGS_ARCH) $(CFLAGS_DEFINES) $(CFLAGS_WARNING) $(CFLAGS_INCLUDE) $(CFLAGS_OPT) $(CFLAGS_EXTRA)
LDFLAGS += $(CFLAGS_ARCH) $(LDFLAGS_USER) $(LDFLAGS_LIBS) $(LDFLAGS_EXTRA_LIBS)

# application targets

OBJS = $(SRC:.c=.o) 
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

$(DPI_APP):  $(OBJS)
	$(CC) $(OBJS) -Wl,--no-as-needed $(LDFLAGS) -o $@ $(LDFLAGS_FOOTER)

INSTALLDIR ?= $(DPI_SDK)/src/bin

install: $(DPI_APP) 
	mkdir -p $(INSTALLDIR)
	cp $(DPI_APP) $(INSTALLDIR)/$(DPI_APP)

clean:
	$(RM) $(DPI_APP) $(OBJS)

.PHONY: clean install

.DEFAULT_GOAL := $(DPI_APP)


