########################################################################
## Makefile that check submodules and run their Makefile
##
##
## Authors: 
##      - Benoit Rat (Seven Solutions, www.sevensols.com)
##
## GNU Lesser General Public License Usage
## This file may be used under the terms of the GNU Lesser
## General Public License version 2.1 as published by the Free Software
## Foundation and appearing in the file LICENSE.LGPL included in the
## packaging of this file.  Please review the following information to
## ensure the GNU Lesser General Public License version 2.1 requirements
## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
########################################################################

SUBMOD_DIRS=spec-sw wr-nic fmc-dio coht-vic
MAKE_DIRS = spec-sw wr-nic fmc-dio coht-vic #tools

LM32_XCOMPILER_URL="https://www.ohwr.org/project/wrpc-sw/uploads/2776ce0ba43503d1486ae205b48fb450"
LM32_XCOMPILER_NAME="lm32_host_64bit.tar.xz"
XCOMPILER_DIR="xcompiler"
LM32_XCOMPILER_NAME_DIR="lm32-gcc-4.5.3"

#RUNME := $(shell test -d $(FMC_DRV) || git submodule update --init)

## Call the sub folder Makefiles
all clean install: init
	@for d in $(MAKE_DIRS); do $(MAKE) -C $$d $@ || exit 1; done
	@if [ "x$@" = "xinstall" ]; then ./scripts/wr-ssk-get -i; cp wrpc-sw/tools/wrpc-vuart /usr/local/sbin; fi

## Init repo and create the .INIT file to not do it again
init: .INIT

.INIT:
	git submodule init
	@ $(MAKE) update
	@ $(MAKE) patch
	@touch .INIT; \

patch:
	./scripts/wr-git-patch

## Force updating the submodules and fetch new gateware
update:
	./scripts/wr-ssk-get -f
	git submodule update
	@rm -rf $(XCOMPILER_DIR)
	mkdir -p $(XCOMPILER_DIR)
	cd $(XCOMPILER_DIR) && wget $(LM32_XCOMPILER_URL)/$(LM32_XCOMPILER_NAME) \
	&& tar xvf $(LM32_XCOMPILER_NAME)
	cd wrpc-sw && make spec_defconfig && \
	CROSS_COMPILE="$(shell pwd)/$(XCOMPILER_DIR)/$(LM32_XCOMPILER_NAME_DIR)/bin/lm32-elf-" make
