GDB ?= FALSE
ARCH := $(shell uname -m)

# use pkg-config for getting CFLAGS and LDLIBS
SHARED_LIBS=libavdevice                          \
            libavformat                          \
            libavfilter                          \
            libavcodec                           \
            libswresample                        \
            libswscale                           \
            libavutil

ifeq ($(GDB), TRUE)
    OPTFLAGS = -g
endif

cpu_flags = $(shell cat /proc/cpuinfo | grep ^flags | head -n 1 | awk -F ':' '{print $$2}')

OPTFLAGS += -ftree-vectorize -O2
ifeq ($(ARCH),x86_64)
OPTFLAGS += $(if $(findstring  avx2 ,   $(cpu_flags)),-mavx2,)
OPTFLAGS += $(if $(findstring  avx ,    $(cpu_flags)),-mavx,)
OPTFLAGS += $(if $(findstring  fma ,    $(cpu_flags)),-mfma,)
OPTFLAGS += $(if $(findstring  sse4a ,  $(cpu_flags)),-msse4a,)
OPTFLAGS += $(if $(findstring  sse4_1 , $(cpu_flags)),-msse4.1,)
OPTFLAGS += $(if $(findstring  sse4_2 , $(cpu_flags)),-msse4.2,)
OPTFLAGS += $(if $(findstring  ssse3 ,  $(cpu_flags)),-mssse3,)
OPTFLAGS += $(if $(findstring  sse2 ,   $(cpu_flags)),-msse2,)
OPTFLAGS += $(if $(findstring  sse ,    $(cpu_flags)),-msse,)
endif

#Check ffmpeg version. Versions prior to 4.2.1 don't support ROI and some other features
#Get libavcodec version and extract major, minor and micro
LIBAVCODEC_VERSION := $(shell pkg-config --modversion libavcodec)
LIBAVCODEC_MAJOR := $(shell echo "$(LIBAVCODEC_VERSION)" | awk -F. '{print $$1}')
LIBAVCODEC_MINOR := $(shell echo "$(LIBAVCODEC_VERSION)" | awk -F. '{print $$2}')
LIBAVCODEC_MICRO := $(shell echo "$(LIBAVCODEC_VERSION)" | awk -F. '{print $$3}')
#Check if libavcodec version is 58.54.100 or above
FFMPEG_421_AND_ABOVE = $(shell echo "$(LIBAVCODEC_MAJOR) $(LIBAVCODEC_MINOR) $(LIBAVCODEC_MICRO)" | awk '{if ($$1 > 58 || ($$1 == 58 && $$2 > 54) || ($$1 == 58 && $$2 == 54 && $$3 >= 100)) print "TRUE"; else print "FALSE"}')
FFMPEG_413_AND_ABOVE = $(shell echo "$(LIBAVCODEC_MAJOR) $(LIBAVCODEC_MINOR) $(LIBAVCODEC_MICRO)" | awk '{if ($$1 > 58 || ($$1 == 58 && $$2 > 35) || ($$1 == 58 && $$2 == 35 && $$3 >= 100)) print "TRUE"; else print "FALSE"}')
#Check if libavcodec version is 57.48.101
FFMPEG_311_VERSION = $(shell echo "$(LIBAVCODEC_MAJOR) $(LIBAVCODEC_MINOR) $(LIBAVCODEC_MICRO)" | awk '{if (($$1 == 57 && $$2 == 48 && $$3 == 101)) print "TRUE"; else print "FALSE"}')

ifeq ($(FFMPEG_311_VERSION),TRUE)
CFLAGS := $(shell pkg-config --cflags $(SHARED_LIBS) xcoder_quadra) -Wall ${OPTFLAGS}
LDLIBS := $(shell pkg-config --libs $(SHARED_LIBS) xcoder_quadra) -lpthread -lm
else
CFLAGS := $(shell pkg-config --cflags $(SHARED_LIBS) xcoder) -Wall ${OPTFLAGS}
LDLIBS := $(shell pkg-config --libs $(SHARED_LIBS) xcoder) -lpthread -lm
endif

EXAMPLES=
DEPS_COMMON=
DEPS_PROCESS=

ifeq ($(FFMPEG_311_VERSION),TRUE)
EXAMPLES += ni_roi_detect_311

DEPS_POSTPROCESS += yolov5_postprocess_311  \
                    yolov4_postprocess_311  \
                    ni_log  \
                    ni_yolo_utils

DEPS_COMMON += netint_network_311
endif

ifeq ($(FFMPEG_421_AND_ABOVE),TRUE)
EXAMPLES += ni_roi_transcoder \
            ni_roi_detect \
            ni_ai_transcode \
            ni_defog \
            ni_lpr \
            ni_scene_classify \
            ni_yolov8_detect

DEPS_POSTPROCESS += yolov5_postprocess  \
                    yolov4_postprocess  \
                    yolov8_postprocess  \
                    defog_network \
                    lpr_network \
                    ni_log  \
                    ni_yolo_utils \
                    scene_classify_postprocess
endif

ifeq ($(FFMPEG_413_AND_ABOVE),TRUE)
EXAMPLES += ni_ocr_vqe_transcoder

DEPS_POSTPROCESS += ocr_postprocess    \
                    vqe_postprocess

DEPS_COMMON += netint_network
endif

OBJS := $(addsuffix .o,$(EXAMPLES))

DEP_OBJS := $(addsuffix .o,$(DEPS_COMMON) $(DEPS_POSTPROCESS))

.phony: all clean

all: $(EXAMPLES)
	$(RM) $(OBJS) $(DEP_OBJS)

$(EXAMPLES): $(OBJS) $(DEP_OBJS)
	$(CC) $(addsuffix .o,$@) $(DEP_OBJS) $(CFLAGS) $(LDLIBS) -o $@

%.o : %.c
	${CC} ${CFLAGS} -c -o $@ $<

clean:
	$(RM) $(EXAMPLES) $(OBJS)
