#Requires: system_top.hdf

PLATFORM=platform_xilinx

CROSS_COMPILE=mb

CC := $(CROSS_COMPILE)-gcc
AR := $(CROSS_COMPILE)-ar

LN_FLAGS := -Wl,--start-group,-lxil,-lgcc,-lc,-lad9361,--gc-sections,--end-group -Wl,-defsym -Wl,_HEAP_SIZE=0x100000
CFLAGS= -Wall -I$(PLATFORM) -mlittle-endian -mxl-soft-mul -mcpu=v9.2 -mxl-soft-mul -Os -ffunction-sections -fdata-sections -DXILINX_PLATFORM -DCONSOLE_COMMANDS

LIB_C_SOURCES := $(filter-out main.c, $(wildcard *.c)) $(wildcard $(PLATFORM)/*.c)
LIB_SOURCES := $(patsubst %.c, %.o, $(LIB_C_SOURCES))
LIB_INCLUDES := $(wildcard *.h) $(wildcard $(PLATFORM)/*.h)

MAIN_SOURCES := main.c console_commands/command.c console_commands/console.c

LIBS := bsp/sys_mb/lib/libxil.a libad9361.a
LSCRIPT := -Tlscript.ld
INCLUDEPATH := -Ibsp/sys_mb/include -I. -I$(PLATFORM) -Iconsole_commands
LIBPATH := -Lbsp/sys_mb/lib -L.
EXEC=ad9361_mb

all: $(EXEC)

$(EXEC): $(LIBS) $(MAIN_SOURCES)
	$(CC) $(CFLAGS) -o $@ $(MAIN_SOURCES) $(LIBPATH) $(LN_FLAGS) $(INCLUDEPATH) $(LSCRIPT)

bsp/sys_mb/lib/libxil.a: system_top.hdf
	echo '<project name="SDK Script" default="main">' > my_SDKproj.xml
	echo '  <target name="main">' >> my_SDKproj.xml
	echo '    <createHwProject projname="hw_platform_0" hwspecpath="system_top.hdf" />' >> my_SDKproj.xml
	echo '    <createAppProject projname="dummy" hwprojname="hw_platform_0" processor="sys_mb" bspprojname="bsp" os="standalone" template="Empty Application" language="C" />' >> my_SDKproj.xml
	echo '  </target>' >> my_SDKproj.xml
	echo '</project>' >> my_SDKproj.xml
	xsdk -wait -script my_SDKproj.xml -workspace .
	xsdk -wait -eclipseargs -nosplash -application org.eclipse.cdt.managedbuilder.core.headlessbuild -build all -data . -vmargs -Dorg.eclipse.cdt.core.console=org.eclipse.cdt.core.systemConsole
	xsdk -wait -eclipseargs -nosplash -application org.eclipse.cdt.managedbuilder.core.headlessbuild -import hw_platform_0 -importAll . -data . -vmargs -Dorg.eclipse.cdt.core.console=org.eclipse.cdt.core.systemConsole
	cp dummy/src/lscript.ld .
#	rm -Rf dummy

libad9361.a: $(LIB_SOURCES) $(LIB_INCLUDES)
	$(AR) rvs libad9361.a $+


ad9361_api.o: ad9361_api.c ad9361.h common.h ad9361_api.h util.h \
	$(PLATFORM)/platform.h util.h

ad9361.o: ad9361.c ad9361.h common.h $(PLATFORM)/platform.h util.h ad9361.h \
	common.h util.h

util.o: util.c util.h ad9361.h common.h

$(PLATFORM)/platform.o: $(PLATFORM)/platform.c util.h ad9361.h common.h \
	$(PLATFORM)/parameters.h

.c.o:
	$(CC) -c $(CFLAGS) $< -o $@ $(INCLUDEPATH)

clean:
	rm -rf *.a *.o */*.o sys_mb bsp .metadata hw_platform_0 dummy
	rm -f $(EXEC) lscript.ld SDK.log my_SDKproj.xml
