include config.mk

# ERC32 applications
ERC32_PROG = rtems-hello rtems-cdtest \
	rtems-tasks rtems-synctrap

# LEON2 applications
LEON2_PROG = rtems-ttcp rtems-hello rtems-cdtest \
	rtems-tasks rtems-synctrap \
	rtems-ttcpw rtems-ttcp rtems-shell \
	rtems-spw-router-pci rtems-tlib rtems-soft-float

# LEON3 applications
LEON3_PROGS=rtems-brm_bm rtems-brm_rt rtems-brm_bc \
            rtems-occan rtems-occan_tx rtems-occan_rx \
            rtems-spwtest_2boards_rx rtems-spwtest_2boards_tx \
            rtems-spwtest_loopback rtems-i2cmst \
	    rtems-grcan rtems-grcan_rx rtems-grcan_tx \
	    rtems-pci rtems-b1553rt rtems-spi rtems-tlib \
	    rtems-gpio
# rtems-watchdog

LEON3MP_PROGS=rtems-mp1 rtems-mp2

all: leon3v8 leon3v8fp leon3 leon3fp leon2v8 leon2v8fp leon2 leon2fp

clean:
	rm -rf bin/*/*/* bin/erc32/* bin/ngmp/* bin/gr712/* bin/ut699/* > /dev/null
	rm -rf $(LEON2_PROG) $(LEON3_PROGS) $(LEON3MP_PROGS) > /dev/null
	rm -rf b_*.c *.mkprom *.grmon *.grmon2 *.out *.o tarfile *.srec *.exe *.ali *.o core xdump.s > /dev/null

build: $(addprefix $(OUTDIR),$(LEON2_PROG))

build_leon3: $(addprefix $(OUTDIR),$(LEON3_PROGS))

build_leon3mp: $(addprefix $(OUTDIR),$(LEON3MP_PROGS))

build_erc32: $(addprefix $(OUTDIR),$(ERC32_PROG))

.PHONY: leon2 leon2fp leon2fpv8 leon2v8 leon3 leon3fp leon3fpv8 leon3v8 erc32 gr712 ut699 ngmp
leon2:
	$(MAKE) OUTDIR="bin/leon2/v7/" CPU=$@ build

leon2fp:
	$(MAKE) OUTDIR="bin/leon2/v7fp/" CPU=$@ build

leon2v8fp:
	$(MAKE) OUTDIR="bin/leon2/v8fp/" CPU=$@ build

leon2v8:
	$(MAKE) OUTDIR="bin/leon2/v8/" CPU=$@ build

leon3:
	$(MAKE) OUTDIR="bin/leon3/v7/" CPU=$@ build build_leon3 build_leon3mp

leon3fp:
	$(MAKE) OUTDIR="bin/leon3/v7fp/" CPU=$@ build  build_leon3 build_leon3mp

leon3v8fp:
	$(MAKE) OUTDIR="bin/leon3/v8fp/" CPU=$@ build build_leon3 build_leon3mp

leon3v8:
	$(MAKE) OUTDIR="bin/leon3/v8/" CPU=$@ build build_leon3 build_leon3mp

ngmp:
	$(MAKE) OUTDIR="bin/$@/" CPU=$@ build build_leon3 build_leon3mp

gr712:
	$(MAKE) OUTDIR="bin/$@/" CPU=$@ build build_leon3 build_leon3mp

ut699:
	$(MAKE) OUTDIR="bin/$@/" CPU=$@ build build_leon3

erc32:
	$(MAKE) OUTDIR="bin/$@/" CPU=$@ build_erc32

$(OUTDIR)rtems-hello: rtems-hello.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-hello-multiple: rtems-hello-multiple.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# assume for debugging
$(OUTDIR)rtems-uart-loopback: rtems-uart-loopback.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-tasks: rtems-tasks.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-irq: rtems-irq.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-synctrap: rtems-synctrap.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-cdtest: rtems-cdtest.cc $(CONFIG_DEPS) | $(OUTDIR)
	$(CPP) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-ttcp: rtems-ttcp.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DREAD_TEST_ONLY $< -o $@

$(OUTDIR)rtems-io: rtems-io.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-ttcpw: rtems-ttcp.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DWRITE_TEST_ONLY $< -o $@

$(OUTDIR)rtems-pd: rtems-pd.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DREAD_TEST_ONLY $< -o $@

$(OUTDIR)rtems-soft-float: rtems-soft-float.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# only LEON3
$(OUTDIR)rtems-occan: rtems-occan.c occan_lib.h occan_lib.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DTASK_TX -DTASK_RX $< occan_lib.c -o $@

$(OUTDIR)rtems-occan_tx: rtems-occan.c occan_lib.h occan_lib.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DMULTI_BOARD -DTASK_TX $< occan_lib.c -o $@
	
$(OUTDIR)rtems-occan_rx: rtems-occan.c occan_lib.h occan_lib.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DMULTI_BOARD -DTASK_RX $< occan_lib.c -o $@

$(OUTDIR)rtems-spwtest_2boards_rx: rtems-spwtest-2boards.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DTASK_RX $< -o $@
	
$(OUTDIR)rtems-spwtest_2boards_tx: rtems-spwtest-2boards.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DTASK_TX $< -o $@

$(OUTDIR)rtems-spwtest_loopback: rtems-spwtest-2boards.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DTASK_TX -DTASK_RX $< -o $@

$(OUTDIR)rtems-brm_bc: rtems-brm.c brm_lib.c brm_lib.h $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DBRM_BC_TEST $< brm_lib.c -o $@

$(OUTDIR)rtems-brm_rt: rtems-brm.c brm_lib.c brm_lib.h $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< brm_lib.c -o $(OUTDIR)rtems-brm_rt

$(OUTDIR)rtems-brm_bm: rtems-brm.c brm_lib.c brm_lib.h $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DBRM_BM_TEST $< brm_lib.c -o $@

$(OUTDIR)rtems-i2cmst: rtems-i2cmst.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-spi: rtems-spi.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# Used to receive messages from rtems-grcan_tx running on another board
$(OUTDIR)rtems-grcan_rx: rtems-grcan.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DCANRX_ONLY $< -o $@

# Used to transmit messages to rtems-grcan_rx running on another board
$(OUTDIR)rtems-grcan_tx: rtems-grcan.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) -DCANTX_ONLY $< -o $@

# This test assumes an external board is responding to the transmitted 
# messages. similar to rtems-canloopback.
$(OUTDIR)rtems-grcan: rtems-grcan.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# Sets up PCI configuration space and prints out AMBA & PCI device found
$(OUTDIR)rtems-pci: rtems-pci.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# Sets up PCI configuration space and prints out AMBA & PCI device found
$(OUTDIR)rtems-gpio: rtems-gpio.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# Sets up PCI configuration space and prints out AMBA & PCI device found
$(OUTDIR)rtems-spw-router-pci: rtems-spw-router-pci.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# GRPWM 4 channel example application
$(OUTDIR)rtems-grpwm: rtems-grpwm.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-b1553rt: rtems-b1553rt.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-watchdog: rtems-watchdog.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-shell: rtems-shell.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-flash: rtems-flash.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

$(OUTDIR)rtems-tlib: rtems-tlib.c $(CONFIG_DEPS) | $(OUTDIR)
	$(CC) $(CFLAGS) $< -o $@

# AMP Targets 
$(OUTDIR)rtems-mp-batch-ram.grmon2: | $(OUTDIR)
	@echo "\# GRMON 2.0 script: Load and run RTEMS AMP example" > $@
	@echo "bp del" 				 >> $@
	@echo "load rtems-mp1 cpu0" 		 >> $@
	@echo "ep $(word 1,$(MP_TEXT)) cpu0"	 >> $@
	@echo "stack $(word 1,$(MP_STACK)) cpu0" >> $@
	@echo "load rtems-mp2 cpu1" 		 >> $@
	@echo "ep $(word 2,$(MP_TEXT)) cpu1" 	 >> $@
	@echo "stack $(word 2,$(MP_STACK)) cpu1" >> $@
	@echo "run" 				 >> $@

$(OUTDIR)rtems-mp-batch-ram.grmon: | $(OUTDIR)
	@echo "\# GRMON 1.1 script: Load and run RTEMS AMP example" > $@
	@echo "cpu act 0" 		    >> $@
	@echo "del" 			    >> $@
	@echo "cpu act 1" 		    >> $@
	@echo "del" 			    >> $@
	@echo "load rtems-mp2" 	    	    >> $@
	@echo "load rtems-mp1" 	    	    >> $@
	@echo "ep $(word 2,$(MP_TEXT))"     >> $@
	@echo "stack $(word 2,$(MP_STACK))" >> $@
	@echo "cpu act 0" 	    	    >> $@
	@echo "ep $(word 1,$(MP_TEXT))"	    >> $@
	@echo "stack $(word 1,$(MP_STACK))" >> $@
	@echo "run" 		    	    >> $@

$(OUTDIR)rtems-mp1: rtems-mp.c $(CONFIG_DEPS) | $(OUTDIR)rtems-mp-batch-ram.grmon $(OUTDIR)rtems-mp-batch-ram.grmon2 $(OUTDIR)
	$(CC) $(CFLAGS) -qleon3mp -DSHM_START=$(MP_SHM_START) -DSHM_SIZE=$(MP_SHM_SIZE) -DNODE_NUMBER=1 -Wl,-Ttext,$(word 1,$(MP_TEXT)) $< -o $@ 

$(OUTDIR)rtems-mp2: rtems-mp.c $(CONFIG_DEPS) | $(OUTDIR)rtems-mp-batch-ram.grmon $(OUTDIR)rtems-mp-batch-ram.grmon2 $(OUTDIR)
	$(CC) $(CFLAGS) -qleon3mp -DSHM_START=$(MP_SHM_START) -DSHM_SIZE=$(MP_SHM_SIZE) -DNODE_NUMBER=2 -Wl,-Ttext,$(word 2,$(MP_TEXT)) $< -o $@ 

.PHONY: rtems-mp
rtems-mp: $(OUTDIR)rtems-mp1 $(OUTDIR)rtems-mp2 $(OUTDIR)rtems-mp.mkprom 

# Create PROM images using mkprom2, setup MKPROMFLAGS before use.
$(OUTDIR)rtems-mp.mkprom: $(OUTDIR)rtems-mp1 $(OUTDIR)rtems-mp2 | $(OUTDIR)
	@echo "### NOTE: Make sure the MKPROMFLAGS are set to flags that correspond to your target system"
	-$(MKPROM2) $(MP_MKRPROMFLAGS) $(MKPROMFLAGS) -o $@ $^


