all:	cavacheck sync_master .make_last_install

# Makesure that the CAVA environment is set up.

cavacheck:
        @rc=0 ;                                             \
        if [ "x${CAVA}" = "x" ] ; then                      \
            echo CAVA not set ;                             \
            rc=$$(( $$rc + 1 )) ;                           \
        fi ;                                                \
        if ( echo ${PATH} | grep -vq ${CAVA}/bin) ; then    \
            echo ${CAVA}/bin is not in the PATH ;           \
            rc=$$(( $$rc + 2 )) ;                           \
        fi ;                                                \
        if ( echo ${PATH} | grep -vq ${CAVA}/tools ) ; then \
            echo ${CAVA}/tools is not in the PATH ;         \
            rc=$$(( $$rc + 4 )) ;                           \
        fi ; exit $$rc

#
# If CAVABUILD has a value, use the new scheme for build directories.
# Otherwise, use the old scheme.

ifeq ($(CAVABUILD),)
  BUILD_DIR = ../../binutils/make
else
  BUILD_DIR = ${CAVABUILD}/binutils
endif

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)


.make_last_install:	.diffcp_last_updated $(BUILD_DIR)/Makefile
	make -j2 -C $(BUILD_DIR)
#	make -C $(BUILD_DIR)
	make -C $(BUILD_DIR) install
	touch .make_last_install

sync_master:
	diffcp bfd/reloc.c		${CAVA}/binutils/src/bfd/reloc.c
	diffcp bfd/cpu-cava.c		${CAVA}/binutils/src/bfd/cpu-cava.c
	diffcp bfd/elf64-cava.c		${CAVA}/binutils/src/bfd/elf64-cava.c
	diffcp gas/tc-cava.h		${CAVA}/binutils/src/gas/config/tc-cava.h
	diffcp gas/tc-cava.c		${CAVA}/binutils/src/gas/config/tc-cava.c
	diffcp include/elf_cava.h	${CAVA}/binutils/src/include/elf/cava.h
	diffcp include/opcode_cava.h	${CAVA}/binutils/src/include/opcode/cava.h
	diffcp ld/elf64cava.sh		${CAVA}/binutils/src/ld/elf64cava.sh
	diffcp ld/elf64cava.sh		${CAVA}/binutils/src/ld/emulparams/elf64cava.sh
	diffcp opcodes/cava-opc.c	${CAVA}/binutils/src/opcodes/cava-opc.c
	diffcp opcodes/cava-dis.c	${CAVA}/binutils/src/opcodes/cava-dis.c
	diffcp gdb			${CAVA}/binutils/src/gdb

$(BUILD_DIR)/Makefile:	$(BUILD_DIR)
	cd $(BUILD_DIR);						\
	CFLAGS='-O1 -g' ${CAVA}/binutils/src/configure			\
		--target=cava-linux					\
		--host=i486-linux-gnu					\
		--prefix=${CAVA}
#--enable-maintainer-mode

.diffcp_last_updated:
	touch .diffcp_last_updated
