# If not specified, assume the opencl source directory is at same location as
# monitor
   ifeq ($(OPENCL_SRC_DIR),)
         OPENCL_SRC_DIR = ../opencl
   endif

# If not specified, pick a default location for TI dependent packages
ifeq ($(DEFAULT_DEV_INSTALL_DIR),)
   DEFAULT_DEV_INSTALL_DIR = /opt/ti
endif

# And for the devkit, when doing an x86 build ...
UNAME_M :=$(shell uname -m)

ifneq (,$(findstring 86, $(UNAME_M)))
ifeq ($(LINUX_DEVKIT_ROOT),)
   LINUX_DEVKIT_ROOT = $(DEFAULT_DEV_INSTALL_DIR)/mcsdk_linux_3_00_04_18/linux-devkit/arago-2013.12/sysroots/cortexa15hf-vfp-neon-3.8-oe-linux-gnueabi
endif
endif

ifeq ($(TI_OCL_CGT_INSTALL),)
  $(error Monitor requires TI_OCL_CGT_INSTALL to point to C6000 Codegen tools)
endif

ifeq ($(SDK_DIR),)
   SDK_DIR = $(DEFAULT_DEV_INSTALL_DIR)/desktop-linux-sdk_01_00_00_07
endif

ifeq ($(MPM_DIR),)
   MPM_DIR = $(DEFAULT_DEV_INSTALL_DIR)/mpm_2.00.01.04
endif

ifeq ($(BIOS_DIR),)
   BIOS_DIR = $(DEFAULT_DEV_INSTALL_DIR)/bios_6_37_00_20
endif

ifeq ($(OEM_DIR),)
   OEM_DIR = $(DEFAULT_DEV_INSTALL_DIR)/openem_1.10.0.0
endif

ifeq ($(OMP_DIR),)
   OMP_DIR = $(DEFAULT_DEV_INSTALL_DIR)/openmp_dsp_2_01_16_02
endif

ifeq ($(IPC_DIR),)
   IPC_DIR = $(DEFAULT_DEV_INSTALL_DIR)/ipc_3_00_04_29
endif

ifeq ($(FC_DIR),)
   FC_DIR  = $(DEFAULT_DEV_INSTALL_DIR)/framework_components_3_30_00_06
endif

ifeq ($(EDMA3LLD_DIR),)
   EDMA3LLD_DIR  = $(DEFAULT_DEV_INSTALL_DIR)/edma3_lld_02_11_11_15
endif

ifeq ($(XDAIS_DIR),)
   XDAIS_DIR = $(DEFAULT_DEV_INSTALL_DIR)/xdais_7_24_00_04
endif

ifeq ($(C6678_PDK_DIR),)
   C6678_PDK_DIR = $(DEFAULT_DEV_INSTALL_DIR)/pdk_C6678_1_1_2_6   
endif

ifeq ($(C6636_PDK_DIR),)
   C6636_PDK_DIR = $(DEFAULT_DEV_INSTALL_DIR)/pdk_keystone2_3_00_04_18
endif

ifeq ($(XDC_DIR),)
   XDC_DIR = $(DEFAULT_DEV_INSTALL_DIR)/xdctools_3_25_05_94
endif

ifeq ($(ULM_DIR),)
   ULM_DIR = $(LINUX_DEVKIT_ROOT)/usr/share/ti/ulm
endif

ifeq ($(GDB_SERVER_DIR),)
   GDB_SERVER_DIR = $(LINUX_DEVKIT_ROOT)/usr/share/ti/gdbc6x
endif

export XDC = $(XDC_DIR)

# Verify that the dependencies exist where stated
ifneq ($(MAKECMDGOALS),clean)
   ifneq ($(MAKECMDGOALS),k2h)
      ifeq (,$(wildcard $(SDK_DIR)/.)) 
         $(error Monitor requires $(SDK_DIR)) 
      endif
      ifeq (,$(wildcard $(C6678_PDK_DIR)/.))
         $(error Monitor requires $(C6678_PDK_DIR))
      endif
   else
      ifeq (,$(wildcard $(C6636_PDK_DIR)/.))
         $(error Monitor requires $(C6636_PDK_DIR))
      endif
   endif
   ifeq (,$(wildcard $(MPM_DIR)/.)) 
      $(error Monitor requires $(MPM_DIR))
   endif
   ifeq (,$(wildcard $(BIOS_DIR)/.)) 
      $(error Monitor requires $(BIOS_DIR)) 
   endif
   ifeq (,$(wildcard $(OEM_DIR)/.))
      $(error Monitor requires $(OEM_DIR))
   endif
   ifeq (,$(wildcard $(OMP_DIR)/.))
      $(error Monitor requires $(OMP_DIR))
   endif
   ifeq (,$(wildcard $(IPC_DIR)/.))
      $(error Monitor requires $(IPC_DIR))
   endif
   ifeq (,$(wildcard $(FC_DIR)/.))
      $(error Monitor requires $(FC_DIR))
   endif
   ifeq (,$(wildcard $(EDMA3LLD_DIR)/.))
      $(error Monitor requires $(EDMA3LLD_DIR))
   endif
   ifeq (,$(wildcard $(XDAIS_DIR)/.))
      $(error Monitor requires $(XDAIS_DIR))
   endif
   ifeq (,$(wildcard $(XDC_DIR)/.))
      $(error Monitor requires $(XDC_DIR))
   endif
   ifeq (,$(wildcard $(ULM_DIR)/.))
      $(error Monitor requires $(ULM_DIR))
   endif
   ifeq (,$(wildcard $(GDB_SERVER_DIR)/.))
      $(error Monitor requires $(GDB_SERVER_DIR))
   endif
endif

PROJECT    = monitor
COMP_OPTS  = $(PROJECT)/compiler.opt
LINK_CMD   = $(PROJECT)/linker.cmd
BUILD_TYPE = release

## !Need -O* to inline CACHE_* functions!
CC= cl6x -o3 -mv6600 --abi=eabi --gcc --mem_model:const=data --mem_model:data=far
#CC= cl6x -o0 -g -mv6600 --abi=eabi --gcc --mem_model:const=data --mem_model:data=far

#INCLUDES= -i $(OPENCL_SRC_DIR)/inc \
#	  -i $(OPENCL_SRC_DIR)/src/core/dsp \
#	  -D MAILBOX_ON_HOST 

INCLUDES=  -i $(OPENCL_SRC_DIR)/src/core/dsp \
           -D MAILBOX_ON_HOST 

SOURCES = monitor.c dsp_rpc.asm util.c ocl_em_init.c stats2.c touch.asm sem.c 
SOURCES+= cppi_device.c em_pdk_hal.c osal.c qmss_device.c ti_em_init.c 
SOURCES+= omp_config_api.c
SOURCES+= builtins.c
SOURCES+= edma.c edma_config.c

HEADERS = edma.h monitor.h ti_em_init.h util.h event_machine_config.h ocl_em_init.h trace.h

OBJS1  =$(patsubst %.c,%.obj,$(SOURCES))
OBJS   =$(patsubst %.asm,%.obj,$(OBJS1))

dspc: override PDK_DIR = $(C6678_PDK_DIR)
dspc: override PLATFORM = dspc868x
dspc: INCLUDES += -DTI_EM_C6678 \
	          -i $(SDK_DIR)/sdk/pciedrv \
	          -i $(SDK_DIR)/sdk/mailBox \
	          -i $(SDK_DIR)/sdk/mailBox/inc \
	          -i $(SDK_DIR)/sdk/buffmgr \
	          -i $(SDK_DIR)/sdk/config 
dspc: $(COMP_OPTS) $(LINK_CMD)  dsp_k1

k2h: override PDK_DIR = $(C6636_PDK_DIR)
k2h: override PLATFORM = evmk2h
k2h: INCLUDES += -DDEVICE_K2K -DTI_EM_C6634 -DEM_32_BIT -DK2_PG1_MSMC_STALL_WA \
	         -i $(MPM_DIR)/include -i$(ULM_DIR) -DULM_ENABLED -i $(GDB_SERVER_DIR)/include -DGDB_ENABLED
k2h: $(COMP_OPTS) $(LINK_CMD)  dsp_k2h


LIBS      = monitor.cmd 
DSPC_LIBS = $(LIBS) $(SDK_DIR)/sdk/mailBox/c66x/lib/mailBox.a
K2H_LIBS  = $(LIBS) $(MPM_DIR)/lib_c66x/mpmmailbox.ae66 $(ULM_DIR)/libtiulm.ae66 $(GDB_SERVER_DIR)/lib/gdb_server.ae66

dsp_k1: $(OBJS) $(LINK_CMD) $(DSPC_LIBS)
	@echo Linking $@
	@$(CC) -z -w -x -m$@.map $^ -I$(TI_OCL_CGT_INSTALL)/lib -llibc.a -o dsp.out
	@echo Creating dsp.syms
	@nm6x -l $@.out | egrep -e "GLOB|PROT" | egrep -e "\|__touch|\|GOMP_|\|omp_" | awk -F\| '{ print $$8"="$$2";" };' > dsp.syms

#----------------------------------------------------------------------------
# include libm functions in monitor
#----------------------------------------------------------------------------
LIBM_DIR=../open_libm
LIBM=$(LIBM_DIR)/libm.lib

$(LIBM): 
	@$(MAKE) -C $(LIBM_DIR) -j4 

#----------------------------------------------------------------------------
# include builtins functions in monitor
#----------------------------------------------------------------------------
BUILTINS_DIR=../opencl_builtins
BUILTINS=$(BUILTINS_DIR)/dsp.lib

$(BUILTINS): 
	@$(MAKE) -C $(BUILTINS_DIR) -j4 

#---------------------------------------------------------------------------
# Build k2h monitor
#----------------------------------------------------------------------------
dsp_k2h: $(OBJS) $(BUILTINS) $(LIBM) $(LINK_CMD) $(K2H_LIBS)
	@echo Linking $@
	@$(CC) -z --priority -w -x -m$@.map $^ -I$(TI_OCL_CGT_INSTALL)/lib -llibc.a -o dsp.out
	@echo Creating dsp.syms
	@nm6x -l dsp.out | grep PROT | awk -F\| '{ print $$8"="$$2";" };' >  $@.syms.tmp
	@nm6x -l dsp.out | grep FUNC | grep -fexport_syms | awk -F\| '{ print $$8"="$$2";" };' >> $@.syms.tmp
	@sort $@.syms.tmp | uniq > $@.syms.tmp1
	@cat $@.syms.tmp1 dsp_link_commands > dsp.syms
	@rm -f $@.syms.tmp* library.fcns dsp.nm6x

%.obj : %.c $(COMP_OPTS) $(HEADERS)
	@echo Compiling $<
	@$(CC) -@$(COMP_OPTS) $(INCLUDES) -c $< 

%.obj : %.asm $(COMP_OPTS)
	@echo Compiling $<
	@$(CC) -@$(COMP_OPTS) $(INCLUDES) -c $< 

.PHONY: clean
clean:
	@rm -fr *.out $(OBJS) *.map $(PROJECT) $(BUILD_TYPE) *.syms *.obj libm.cmd libm.fcns builtins.cmd builtins.fcns
	@$(MAKE) -C ocl_platforms/dspc868x clean
	@$(MAKE) -C ocl_platforms/evmk2h clean
	@$(MAKE) -C $(LIBM_DIR) clean
	@$(MAKE) -C $(BUILTINS_DIR) clean

#--------------------------------------------------------------------------
# Make sure the platform is built before we try to build the monitor.
#--------------------------------------------------------------------------
ocl_platforms/dspc868x/package/package_ocl_platforms.dspc868x.c: ocl_platforms/dspc868x/Platform.xdc
	 $(MAKE) -C ocl_platforms/dspc868x 

ocl_platforms/evmk2h/package/package_ocl_platforms.evmk2h.c: ocl_platforms/evmk2h/Platform.xdc
	 $(MAKE) -C ocl_platforms/evmk2h

#--------------------------------------------------------------------------
# generate and build config packages
#--------------------------------------------------------------------------
XDCPATH  = ./;$(OEM_DIR)/packages;$(PDK_DIR)/packages;$(BIOS_DIR)/packages;$(OMP_DIR)/packages;$(EDMA3LLD_DIR)/packages;$(IPC_DIR)/packages;$(FC_DIR)/packages;$(XDAIS_DIR)/packages;


XDCTARGET= ti.targets.elf.C66

$(COMP_OPTS) : $(LINK_CMD)

$(LINK_CMD): $(PROJECT).cfg ocl_platforms/dspc868x/package/package_ocl_platforms.dspc868x.c ocl_platforms/evmk2h/package/package_ocl_platforms.evmk2h.c
	@echo Making $(PROJECT) files
	@mkdir -p $(BUILD_TYPE)
	@$(XDC)/xs --xdcpath "$(XDCPATH)" xdc.tools.configuro -c $(TI_OCL_CGT_INSTALL) --cb -t $(XDCTARGET) -p ocl_platforms.$(PLATFORM) -r $(BUILD_TYPE) $(PROJECT).cfg
	@sed '/c6xabi\.exidx/d' $(LINK_CMD) > lnk_tmp
	@mv lnk_tmp $(LINK_CMD)


