OCL_BUILD_ROOT= $(shell pwd)
OCL_VER=0.12.0
.SILENT:
LLVM_VER=3.3

# Needed by sub target makes
export ARM_LLVM_DIR = /opt/ti-llvm-$(LLVM_VER)

OCL_BUILD_DIR    = build
OCL_SRC_DIR      = opencl
OCL_MONITOR_DIR  = opencl_monitor
OCL_BUILTINS_DIR = opencl_builtins
OCL_LIBM_DIR     = open_libm
OCL_EXAMPLES_DIR = opencl_examples
OCL_MONITOR_OUT  = $(OCL_MONITOR_DIR)/dsp.out
OCL_MONITOR_SYMS  = $(OCL_MONITOR_DIR)/dsp.syms

OCL_EXAMPLES = edmamgr matmpy simple dsplib_fft mandelbrot mandelbrot_native \
               offline offline_embed platforms ccode null ooo vecadd         \
               vecadd_openmp vecadd_openmp_t dspheap sgemm

OCL_OMP_EXAMPLES = vecadd_openmp vecadd_openmp_t dgemm


.PHONY: all clean opencl install clone


OPENCL_CMAKE_OPTS = 

all: opencl


opencl: $(OCL_MONITOR_OUT) $(OCL_MONITOR_SYMS) $(OCL_BUILD_DIR)/Makefile 
	$(MAKE) -C $(OCL_BUILD_DIR)


$(OCL_BUILD_DIR)/Makefile: $(filter-out $(wildcard $(OCL_BUILD_DIR)), $(OCL_BUILD_DIR)) 
	cd $(OCL_BUILD_DIR); cmake $(OPENCL_CMAKE_OPTS) ../opencl


$(OCL_BUILD_DIR): 
	mkdir -p $(OCL_BUILD_DIR)


$(OCL_MONITOR_OUT) $(OCL_MONITOR_SYMS):
	$(error Monitor files "$(OCL_MONITOR_OUT) $(OCL_MONITOR_SYMS)" need to be copied to ARM host prior to build )


install:
	install -m 755 -d ${DESTDIR}/usr/bin
	install -m 755 -d ${DESTDIR}/usr/include
	install -m 755 -d ${DESTDIR}/usr/lib
	install -m 755 -d ${DESTDIR}/usr/share/ti/opencl
	install -m 755 -d ${DESTDIR}/usr/share/ti/examples/opencl
	install -m 755 -d ${DESTDIR}/usr/share/ti/examples/opencl+openmp
	cp ${OCL_SRC_DIR}/clocl/arm/clocl       ${DESTDIR}/usr/bin
	cp -r ${OCL_SRC_DIR}/include/CL         ${DESTDIR}/usr/include
	cp -r ${OCL_BUILTINS_DIR}/include/*     ${DESTDIR}/usr/share/ti/opencl
	cp ${OCL_SRC_DIR}/util/ocl_util.h       ${DESTDIR}/usr/include
	cp ${OCL_BUILD_DIR}/lib/libocl_util.a   ${DESTDIR}/usr/lib
	cp ${OCL_BUILD_DIR}/lib/libOpenCL.so.${OCL_VER}	 ${DESTDIR}/usr/lib
	cp -P ${OCL_BUILD_DIR}/lib/libOpenCL.so ${DESTDIR}/usr/lib
	cp -P ${OCL_BUILD_DIR}/lib/libOpenCL.so.0  ${DESTDIR}/usr/lib
	cp ${OCL_MONITOR_OUT}    	        ${DESTDIR}/usr/share/ti/opencl
	cp ${OCL_MONITOR_SYMS}   	        ${DESTDIR}/usr/share/ti/opencl
	for dir in $(OCL_EXAMPLES); do \
          cp -r  ${OCL_EXAMPLES_DIR}/$$dir ${DESTDIR}/usr/share/ti/examples/opencl; \
	done
	for dir in $(OCL_OMP_EXAMPLES); do \
          cp -r  ${OCL_EXAMPLES_DIR}/$$dir ${DESTDIR}/usr/share/ti/examples/opencl+openmp; \
	done
	cp ${OCL_EXAMPLES_DIR}/make.inc ${DESTDIR}/usr/share/ti/examples/opencl
	cp ${OCL_EXAMPLES_DIR}/make.inc ${DESTDIR}/usr/share/ti/examples/opencl+openmp
	cp ${OCL_EXAMPLES_DIR}/Makefile ${DESTDIR}/usr/share/ti/examples/opencl
	cp ${OCL_EXAMPLES_DIR}/Makefile ${DESTDIR}/usr/share/ti/examples/opencl+openmp



clean:
	$(MAKE) -C $(OCL_SRC_DIR)/clocl clean
	rm -rf $(OCL_BUILD_DIR)


