# SPDX-FileCopyrightText: Copyright (c) 2021-2022 KUNLUNXIN CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

include ../../../build/globaldefs.mk
include ../../../build/globalrules.mk

cmbase = ../../../..
CMBASE = $(shell cd $(cmbase); pwd )


# the path where to find header files
INCFLAGS = -I../../../inc
INCFLAGS += -I../../../../../../common_inc

ifeq ($(TRACE),y)
  INCFLAGS += -I../../debug_trace \
      -I../../../source/common \
      -I../../../source/hevc \
      -I../../../source/jpeg
else ifeq ($(INTERNAL_TEST),y)
  INCFLAGS += -I../../../source/common
endif

  INCFLAGS += -I../../../source/common
# list of used sourcefiles
SRCS = JpegTestBench.c EncGetOption.c

vpath %.c
vpath %.c ../../../source/jpeg

# name of the control code library 
LIB = ../../libh2enc.a

# System model library
MODELLIB = $(CMBASE)/system/models/ench2_asic_model.a

# name of the output executable
TARGET = jpeg_testenc


#Here are rules for building codes and generating executable
all:    $(VERSION_H) tags
	@echo ---------------------------------------
	@echo "Usage: make [ system | eval | versatile ]"
	@echo "system     - PC system model (== pclinux)"
	@echo "eval       - PC system model for evaluation with frame limit"
	@echo "integrator - ARM integrator with FPGA HW"
	@echo "versatile  - ARM versatile with FPGA HW"
	@echo "NOTE! Make sure to do 'make libclean'"
	@echo "between compiling to different targets!"
	@echo ---------------------------------------

# Use other makefiles to build the libraries
$(MODELLIB):
	$(MAKE) -w -C ../../../../system/models

$(LIB):
	$(MAKE) -w -C ../.. $(TARGETENV) \
                PCIE_FPGA_VERI_LINEBUF=$(PCIE_FPGA_VERI_LINEBUF)

version.h:../../../../.git/HEAD ../../../../.git/index
	echo "const char *git_version = \"$(shell git rev-parse HEAD)\";" > $@



.PHONY: eval
evaluation: eval
eval: TARGET = jpeg_enc_h2_eval
eval: TARGETENV = eval
eval: $(MODELLIB) $(LIB) $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LIB) $(MODELLIB) $(LIB) -lm -lpthread -o $(TARGET)

pclinux: system

.PHONY: system
system: TARGETENV = system
system: $(VERSION_H) .depend $(MODELLIB) $(LIB) $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LIB) $(MODELLIB) $(LIB) -lm -lpthread -o $(TARGET)

.PHONY: system_multifile
system_multifile: TARGET = jpeg_testenc_multifile
system_multifile: TARGETENV = system
system_multifile: $(VERSION_H) .depend $(MODELLIB) $(LIB) $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LIB) $(MODELLIB) $(LIB) -lm -lpthread -o $(TARGET)

.PHONY: system_static
system_static: TARGETENV = system
system_static: $(VERSION_H) .depend $(MODELLIB) $(LIB) $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LIB) $(MODELLIB) $(LIB) -lm -lpthread -o $(TARGET) -static

.PHONY: versatile
versatile: TARGETENV = versatile
versatile: $(VERSION_H) $(OBJS)
	$(MAKE) -w -C ../.. $@ \
            CROSS_COMPILE=$(CROSS_COMPILE) ARCH="$(ARCH)"
	$(CC) $(CFLAGS) $(OBJS) $(LIB) -lm -lpthread -o $(TARGET) $(STATIC)

.PHONY: versatile_multifile
versatile_multifile: TARGETENV = versatile
versatile_multifile: TARGET = jpeg_testenc_multifile
versatile_multifile: $(OBJS)
	$(MAKE) -w -C ../.. versatile \
            CROSS_COMPILE=$(CROSS_COMPILE) ARCH="$(ARCH)"
	$(CC) $(CFLAGS) $(OBJS) $(LIB) -o $(TARGET)

.PHONY: integrator
integrator: TARGETENV = integrator
integrator: $(OBJS)
	$(MAKE) -w -C ../.. $@ \
            CROSS_COMPILE=$(CROSS_COMPILE) ARCH="$(ARCH)"
	$(CC) $(CFLAGS) $(OBJS) $(LIB) -o $(TARGET)

.PHONY: pci
pci: TARGETENV = pci
pci: $(VERSION_H) .depend $(LIB) $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LIB) -lm -lpthread -o $(TARGET)


system_cov: CC = covc --retain -t!JpegTestBench.c,!EncGetOption.c g++
system_cov: TARGETENV = system_cov
system_cov: $(MODELLIB) $(LIB) $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LIB) $(MODELLIB) $(LIB) -o $(TARGET)

        
.PHONY: clean
clean:
	$(RM) .depend
	$(RM) *.o *.gcno *.gcda core* *~ $(TARGET) $(TARGET).* $(VERSION_H)

.PHONY: libclean
libclean: clean
	$(MAKE) -w -C ../.. clean

.PHONY: tags
tags:
	ctags ../../../inc/*.h *.c ../../../source/jpeg/*.[ch] \
            ../../../source/common/*.[ch] ../../ewl/*c
            
depend .depend: $(SRCS)
	@echo -e "\n [DEP]\t $^"
	@$(CC) -M $(DEBFLAGS) $(INCFLAGS) $^ > .depend

ifeq (,$(findstring clean, $(MAKECMDGOALS)))
ifeq (.depend, $(wildcard .depend))
include .depend
endif
endif

