############################
# mpu libs need to link to this APE, could be specified by user
 MICRO_CODE_LIBS:=ByteCopy SyncVer
############################
# tool path, could be specified by user
#UCP_HOME=/opt/sdk/ucp2.0_sdk/bin
#GDB_PATH=/opt/sdk/ucp2.0_sdk/simulator/ucp2-gdb
#SIM_PATH=/opt/sdk/ucp2.0_sdk/simulator/UCP_Simulator_V2.0
#OPENOCD_GDB_PATH=/opt/sdk/ucp_sdk/apc_tools_ima/bin/gdb-ucps-openocd
#LPATH1=/opt/sdk/ucp2.0_sdk/lib/ucps2/lib
#LPATH2=/opt/sdk/ucp2.0_sdk/lib/clang/12.0.0/lib/ucps2

UCP_HOME=${MaPU_TC_HOME}/bin/ucp2
GDB_PATH=${MaPU_TC_HOME}/bin/ucp2/ucp2-gdb
SIM_PATH=${MaPU_TC_HOME}/bin/ucp2/ucp2-simulator
OPENOCD_GDB_PATH=${MaPU_TC_HOME}/ucps-openocd/ucps-openocd-gdb
LPATH1=${MaPU_TC_HOME}/lib/ucp2/release
LPATH2=${MaPU_TC_HOME}/lib/ucp2/release
#############################
export UCP_HOME
############################
# varaibles for project construction using new ucp toolchain
# DO NOT MODIFY
SPU_MAIN_C_FILE:=$(shell find . -name "app.s.c")
SPU_C_FILES:=$(shell find . -name "*.s.c")
SPU_C_OBJECT_FILES:=$(subst .c,.c.o,${SPU_C_FILES})
SPU_ASM_FILES:=$(shell find . -name "*.s.asm")
SPU_ASM_OBJECT_FILES:=$(subst .asm,.asmb.o,${SPU_ASM_FILES})
MPU_ASM_FILES:=$(shell find . -name "*.m.asm" -or -name "*.m0.asm" -or -name "*.m1.asm")
MPU_ASM_OBJECT_FILES:=$(subst .asm,.asmb.o,${MPU_ASM_FILES})
##INC_DIRS+=$(shell find ${MaPU_TC_HOME}/include/ucp2 -type d)
INC_DIRS+=$(shell find ../${MICRO_CODE_DIR} -name inc -type d)
INC_DIRS+=$(shell find ../${COMMON_LIB_DIRS} -name inc -type d)
INC_DIRS+= ../Interface
INC_DIRS+= ../Inc
INC_DIRS+=$(shell find ./ -name inc -type d)
override INC_DIRS_OPTION:=$(patsubst %,-I%,${INC_DIRS})
override WORK_DIR:=$(abspath $(lastword $(MAKEFILE_LIST)))
override PROJECT_NAME:=$(notdir $(patsubst %/,%,$(dir $(WORK_DIR))))
ifeq (${mpu}, dyn)
	LD_SCRIPT=$(shell realpath ${PWD}/Common/Scripts/ape4-Cache128-noMPUC-dynamic.ld)
else
	LD_SCRIPT=$(shell realpath ${PWD}/Common/Scripts/ape4-Cache128-noMPUC.ld)
endif
EXE_FILE_NAME=${PROJECT_NAME}.out
#LINK_L_OPTION:=$(patsubst %,-L../%,${COMMON_LIB_DIRS})
LINK_L_OPTION=-L../Lib
LINK_l_OPTION:=$(patsubst %,-l%,${COMMON_LIB_DIRS})
APE_LINK_L_OPTION=-L../Lib
APE_LINK_l_OPTION:=$(patsubst %,-l%,${PROJECT_NAME})
ifneq (${MICRO_CODE_LIBS}, )
  LINK_MPU_L_OPTION=-L../Lib/MicroLib
  LINK_MPU_l_OPTION:=$(patsubst %,-l%,${MICRO_CODE_LIBS})
else
  LINK_MPU_L_OPTION:=
  LINK_MPU_l_OPTION:=
endif
LINK_PLATFORM_L_OPTION=-L../Lib/OspLib
#LINK_PLATFORM_l_OPTION:=-lape
ifeq (${tb}, PDSCH)
	LINK_PLATFORM_l_OPTION:=
else ifeq (${tb}, DL)
	LINK_PLATFORM_l_OPTION:=
else
	LINK_PLATFORM_l_OPTION:=-lape_spu
endif

# this flag could be specified by user, default to build debug info
debug:=0
export debug
ifeq (${debug},1)
	ASM_DEBUG_FLAGS:=-g
	C_DEBUG_FLAGS:=-O0 -g
else ifeq (${debug},3)
	ASM_DEBUG_FLAGS:=
	C_DEBUG_FLAGS:=-O3
else
	ASM_DEBUG_FLAGS:=
	C_DEBUG_FLAGS:=-O2
endif
############################

all:
	@for dir in ${COMMON_LIB_DIRS}; do \
		make LIB -C $${dir} ; \
	done

%.s.c.o: %.s.c
	@${UCP_HOME}/clang --target=ucps2 -c ${INC_DIRS_OPTION} $^ -o $@ ${C_DEBUG_FLAGS} ${TEST_OPTION} ${PRJ_OPTION} ${MIM_DYNAMIC_OPTION} ${DIAG_STACK_OPTION}

%.s.asmb.o: %.s.asm
	@${UCP_HOME}/llvm-mc -arch=ucps2 -filetype=obj $^ -o $@ ${ASM_DEBUG_FLAGS}

${EXE_FILE_NAME}: 
	${UCP_HOME}/ld.lld -m ucps2 -o $@ $(LPATH1)/crt0.o \
	$(LPATH1)/crtend.o \
	${APE_LINK_L_OPTION} ${APE_LINK_l_OPTION} \
	${LINK_L_OPTION} ${LINK_l_OPTION} \
    ${LINK_MPU_L_OPTION} ${LINK_MPU_l_OPTION} \
	${LINK_PLATFORM_L_OPTION} ${LINK_PLATFORM_l_OPTION} \
		-L$(LPATH2) \
		-L$(LPATH1) -lm -lc -lgloss \
		-lclang_rt.builtins-ucps2 -T ${LD_SCRIPT}

APP: ${EXE_FILE_NAME}

lib%.a: ${SPU_C_OBJECT_FILES} ${SPU_ASM_OBJECT_FILES} ${MPU_ASM_OBJECT_FILES}
	ar rcs -o $@ ${SPU_C_OBJECT_FILES}	${SPU_ASM_OBJECT_FILES} ${MPU_ASM_OBJECT_FILES} && chmod +x $@

LIB: lib${PROJECT_NAME}.a

Dis: APP
	${UCP_HOME}/llvm-objdump -d --start-address=0x40000 --line-numbers --source ${EXE_FILE_NAME} > app.s.dis; 
	#${UCP_HOME}/llvm-objdump -arch-name=ucpm2 -no-show-raw-insn -no-leading-addr -d ${EXE_FILE_NAME} -start-address=0x40000 -stop-address=0x60000 > app.m.dis; 

# direct simulaton without gui
DSim: APP
	@${SIM_PATH} -m=printChar -elf-file=${EXE_FILE_NAME} -ores -allcf && \
		echo "software simulation finished"

DSimGDB: APP
	@${SIM_PATH} -m=gdb -elf-file=${EXE_FILE_NAME} -allcf -exec-path=./ -port=1234&& \
		echo "software simulation finished"
GDB: APP
	@${OPENOCD_GDB_PATH} ./${EXE_FILE_NAME}&& \
		echo "software GDB finished"

clean:
	@for dir in ${COMMON_LIB_DIRS}; do \
		if [ -e $${dir} ]; then \
		  make clean -C $${dir} ; \
		fi;\
	done
	@rm -rf *.lof  *.out  *.map  *.mmap  *.sct  *.tab  *.s  *.dis  *~  *.def *.xml 
	@rm -rf *.daidir work  *.lib++ *~ *.rc csrc *.key filelist.f simv*
	@rm -rf DVEfiles vc_hdrs.h urgReport *.rc *.vpd *.log
	@rm -rf *.sym *.data *.bak slot-*.txt PC_Line*.txt codes.txt UCPMDis.txt *.saif
	@rm -rf Simulator_*.dat saif.cmds Run_Cycle.dat *.elf ucps_pc_code.txt Receiver_sync TestTask Transform_func
	@rm -rf Executed_Pipeline.csv
	@find . -name "*.o" -exec rm -f {} \;
	@find . -name "*.a" -exec rm -f {} \;

.PRECIOUS: %.s.c.o
.PHONY: APP clean all
