# sub directories to compile, could be specified by user
COMMON_LIB_DIRS=Common
#PLATFORM_LIB_DIRS=Driver Rtos osp
MICRO_CODE_DIR=MicroCode
APE_DIRS=APE0 APE3 APE4 APE5 APE6 APE7
APE_ALL_DIRS=APE0 APE3 APE4 APE5 APE6 APE7 ECS
# should we remove individual microcode test module from compile
REMOVE_MC_TEST:=1

############################
# 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
# export MaPU_TC_HOME=/home/useryb/ucpcompile/toolchain
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
#############################
export UCP_HOME
export COMMON_LIB_DIRS
export MICRO_CODE_DIR
############################
# 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:=${MaPU_TC_HOME}/include/ucp2
INC_DIRS+=$(shell find ${MaPU_TC_HOME}/include/ucp2/newlib -type d)
INC_DIRS+=${MaPU_TC_HOME}/include/ucp2/llvm
export INC_DIRS
INC_DIRS+=$(shell find ./${COMMON_LIB_DIRS} -name inc -type d)
INC_DIRS+= ./Interface
INC_DIRS+= ./Inc
INC_DIRS_OPTION:=$(patsubst %,-I%,${INC_DIRS})
WORK_DIR:=$(shell realpath ${PWD})
PROJECT_NAME:=$(shell basename ${WORK_DIR})

# 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 ifeq (${debug},SP)
	ASM_DEBUG_FLAGS:=
	C_DEBUG_FLAGS:=-O2 -fstack-protector-all
else
	ASM_DEBUG_FLAGS:=
	C_DEBUG_FLAGS:=-O2
endif

ifneq (${REMOVE_MC_TEST}, 0)
	REMOVE_MC_TEST_OPTION:=-DREMOVE_MC_TEST
else
	REMOVE_MC_TEST_OPTION:=
endif
export REMOVE_MC_TEST_OPTION

tb:=
ifeq (${tb}, SECOND)
	TEST_OPTION:=
	TEST_OPTION+=-DCORE_ODD
	#TEST_OPTION+=-DCLOSEDLLOG	
else 
	TEST_OPTION:=
	#TEST_OPTION+=-DCLOSEDLLOG	
endif
export TEST_OPTION
export tb 

prj:=
ifeq (${prj}, cpri)
	PRJ_OPTION:=-DDISTRIBUTED
else 
	PRJ_OPTION:=-DINTEGRATION 
endif
export PRJ_OPTION
export prj
############################
MPU_ASM_INC:=-I../MpuAsmInc
mpu:=
ifeq (${mpu}, dyn)
	MPU_DYNAMIC_OPTION:=--defsym enable_dynamic_mim=1
	MIM_DYNAMIC_OPTION:=-DMIM_DYN
else 
	MPU_DYNAMIC_OPTION:=
	MIM_DYNAMIC_OPTION:=
endif
export MIM_DYNAMIC_OPTION
export mpu

rfm:=
export rfm
ifeq (${rfm}, 1)
	TEST_OPTION+=-DTARGET_RFM
endif
diag:=
ifeq (${diag}, stack)
	DIAG_STACK_OPTION:=-mllvm -enable-ucps2-stack-diagnostic
else
	DIAG_STACK_OPTION:=
endif
export DIAG_STACK_OPTION
export diag

all: ${APE_DIRS}

UMLIBS:
	rm ./Lib/MicroLib/*.a -rf
	make clean -C ${MICRO_CODE_DIR}
	make MLIBS -C ${MICRO_CODE_DIR}

UCLIBS:
	cp Platform/build/libape_spu.a Lib/OspLib
	cp Platform/build/libecs_rfm_spu0.a Lib/OspLib
	cp Platform/build/ecs_rfm_spu0.out ./
	cp Platform/build/ecs_rfm_spu1.out ./
	cp Platform/build/pet_rfm_spu0.out ./
	cp Platform/build/pet_rfm_spu1.out ./
	make clean -C ${COMMON_LIB_DIRS}
	@for dir in ${COMMON_LIB_DIRS}; do \
		make LIB -C $${dir} || exit $$?; \
		cp $${dir}/*.a ./Lib; \
	done


APE0: 
	rm ./MicroCode/MpuAsmInc -rf
	cp ./APE0/MpuAsmInc ./MicroCode/ -rf
	make UMLIBS
	cp ./APELib/MicroLib/*.a ./Lib/MicroLib/
	make UCLIBS
	cp -rf APELib/Transmitter APE0
	cp -rf APELib/TestTask APE0
	cp -rf APELib/Transform_func APE0
	make LIB -C APE0 || exit $$?; \
	cp APE0/*.a ./Lib; \
	make APP -C APE0
	cp ./APE0/APE0.out ./

APE3: 
	rm ./MicroCode/MpuAsmInc -rf
	cp ./APE3/MpuAsmInc ./MicroCode/ -rf
	make UMLIBS
	cp ./APELib/MicroLib/*.a ./Lib/MicroLib/
	make UCLIBS
	cp -rf APELib/Receiver_symb APE3
	cp -rf APELib/Transform_func APE3
	make LIB -C APE3 || exit $$?; \
	cp APE3/*.a ./Lib; \
	make APP -C APE3
	cp ./APE3/APE3.out ./	

APE4: 
	rm ./MicroCode/MpuAsmInc -rf
	cp ./APE4/MpuAsmInc ./MicroCode/ -rf
	make UMLIBS
	cp ./APELib/MicroLib/*.a ./Lib/MicroLib/
	make UCLIBS
	cp -rf APELib/Receiver_sync APE4
	cp -rf APELib/TestTask APE4
	cp -rf APELib/Transform_func APE4
	make LIB -C APE4 || exit $$?; \
	cp APE4/*.a ./Lib; \
	make APP -C APE4
	cp ./APE4/APE4.out ./


APE5: 
	rm ./MicroCode/MpuAsmInc -rf
	cp ./APE5/MpuAsmInc ./MicroCode/ -rf
	make UMLIBS
	cp ./APELib/MicroLib/*.a ./Lib/MicroLib/
	make UCLIBS
	cp -rf APELib/Receiver_symb APE5
	cp -rf APELib/Transform_func APE5
	make LIB -C APE5 || exit $$?; \
	cp APE5/*.a ./Lib; \
	make APP -C APE5
	cp ./APE5/APE5.out ./

APE6: 
	rm ./MicroCode/MpuAsmInc -rf
	cp ./APE6/MpuAsmInc ./MicroCode/ -rf
	make UMLIBS
	cp ./APELib/MicroLib/*.a ./Lib/MicroLib/
	make UCLIBS
	cp -rf APELib/Receiver_bit APE6
	cp -rf APELib/TestTask APE6
	cp -rf APELib/Transform_func APE6
	make LIB -C APE6 || exit $$?; \
	cp APE6/*.a ./Lib; \
	make APP -C APE6
	cp ./APE6/APE6.out ./

APE7: 
	rm ./MicroCode/MpuAsmInc -rf
	cp ./APE7/MpuAsmInc ./MicroCode/ -rf
	make UMLIBS
	cp ./APELib/MicroLib/*.a ./Lib/MicroLib/
	make UCLIBS
	cp -rf APELib/Receiver_sync_first APE7
	cp -rf APELib/Transform_func APE7
	make LIB -C APE7 || exit $$?; \
	cp APE7/*.a ./Lib; \
	make APP -C APE7
	cp ./APE7/APE7.out ./


ECS: UCLIBS
	make LIB -C ECS || exit $$?; \
	cp ECS/*.a ./Lib; \
	make APP -C ECS
	cp ./ECS/ECS.out ./

%.s.c.o: %.s.c
	@${UCP_HOME}/clang --target=ucps2 -c ${INC_DIRS_OPTION} ${REMOVE_MC_TEST_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}

%.m.asmb.o: %.m.asm
	@${UCP_HOME}/llvm-mc -arch=ucpm2 -filetype=obj ${MPU_ASM_INC} $^ -o $@ ${ASM_DEBUG_FLAGS} ${MPU_DYNAMIC_OPTION}

%.m0.asmb.o: %.m0.asm
	@${UCP_HOME}/llvm-mc -arch=ucpm2 -filetype=obj ${MPU_ASM_INC} $^ -o $@ ${ASM_DEBUG_FLAGS} ${MPU_DYNAMIC_OPTION}

%.m1.asmb.o: %.m1.asm
	@${UCP_HOME}/llvm-mc -arch=ucpm2 -filetype=obj ${MPU_ASM_INC} $^ -o $@ ${ASM_DEBUG_FLAGS} ${MPU_DYNAMIC_OPTION}

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

clean:
	@for dir in ${COMMON_LIB_DIRS}; do \
		if [ -e $${dir} ] && [ -e $${dir}/Makefile ]; then \
		  make clean -C $${dir} ; \
		fi;\
	done
	@if [ -e ${MICRO_CODE_DIR} ] && [ -e ${MICRO_CODE_DIR}/Makefile ]; then \
	  make clean -C ${MICRO_CODE_DIR} ; \
	fi;
	@for dir in ${APE_ALL_DIRS}; do \
		if [ -e $${dir} ] && [ -e $${dir}/Makefile ]; 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* *.a
	@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
	@rm -rf Executed_Pipeline.csv
	@find . -name "*.o" -exec rm -f {} \;
	#@find . -name "*.a" -exec rm -f {} \;

.PRECIOUS: %.s.c.o %.s.asmb.o %.m.asmb.o %.m0.asmb.o %.m1.asmb.o
.PHONY: APP clean all APE0 APE3 APE4 APE5 APE6 APE7
