all:	cavacheck sync_master .make_last_install

# Make sure 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 = ../../glibc/make
else
  BUILD_DIR = ${CAVABUILD}/glibc
endif

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

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

sync_master:
	diffcp sysdeps	$(CAVA)/glibc/src/sysdeps/cava
	diffcp linux	$(CAVA)/glibc/src/sysdeps/unix/sysv/linux/cava

$(BUILD_DIR)/Makefile:
	cd  $(BUILD_DIR);						\
	CC=cava-linux-gcc CFLAGS='-O1 -g' ${CAVA}/glibc/src/configure	\
		cava-linux						\
		--prefix=${CAVA}					\
		--with-binutils=${CAVA}/cava-linux/bin			\
		--with-headers=/usr/include				\
		--disable-shared					\
		--disable-profile					\
		--disable-sanity-checks
#		--enable-add-ons=linuxthreads				\

.diffcp_last_updated:
	touch .diffcp_last_updated

rebuild:
	rm -rf $(BUILD_DIR)


#	CC=cava-linux-gcc CFLAGS='-O0 -g -fno-inline' ${CAVA}/glibc/src/configure \

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