BASEDIR=$(shell pwd)
PRXXDIR=$(shell dirname $(BASEDIR))
SRC=$(BASEDIR)/src
BUILD=$(BASEDIR)/build
UCLIBCVERS=0.9.26

BRANCH=labpro-cubis

BINDIR=$(PRXXDIR)/local/m68k-uclinux/bin

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),distclean)
ifeq "$(wildcard $(BINDIR)/m68k-uclinux-gcc)" ""
    $(error cannot find cross software in $(BINDIR))
endif
endif
endif

DEVPREFIX=/
export PREFIX=$(PRXXDIR)/local/m68k-uclinux

INSTALLDIR=$(PREFIX)/$(DEVPREFIX)

#
##############################################################
##
## EDIT these to suit your system and source locations
##
#
TARGET=m68k-uclinux
LINUXDIR=$(PRXXDIR)/$(BRANCH)-linux/linux-2.6.10
#
## set your install directory here and add the correct PATH

#
#
GCCVERS=3.4.0

#
## set $ROOTDIR to the root directory of your uClinux source tree.
## Needed to build uClibc with -mid-shared-library support.
##
##ROOTDIR="/home/uClinux-dist"
#
##
## Either 2.4.x or 2.6.x are supported by uClibc-0.9.26+
##
##KERNEL="${ROOTDIR}/linux-2.4.x"
#KERNEL="${ROOTDIR}/linux-2.6.10"
#
##
## Override this host's CFLAGS to build the tools for older hosts
##
CFLAGS=-O2
CXXFLAGS=-O2
CPPFLAGS=
#

HOST_TARGET=--host=i686-pc-linux-gnu

#
##############################################################
##
## Don't edit these
##

all : uclibc_make install #uclibc_make_nofloat
	@if [ -f $(INSTALLDIR)/include/uclibc-build.h ] ; then cat $(INSTALLDIR)/include/uclibc-build.h ; fi

uclibc_make: patch_kernel_source
	if [ -x bin/which_build.pl ] ; then \
		bin/which_build.pl -dUCLIBC_BUILD uClibc-$(UCLIBCVERS)/include/uclibc-build.h ; \
	fi
	cd uClibc-$(UCLIBCVERS) && \
	$(MAKE) CROSS=$(BINDIR)/$(TARGET)-

patch_kernel_source:
	cp -vu $(BRANCH)-uClibc.config uClibc-$(UCLIBCVERS)/.config
	grep 'KERNEL_SOURCE="$(LINUXDIR)"' uClibc-$(UCLIBCVERS)/.config || \
	(echo "Patching" ; perl -pi.old -e's:KERNEL_SOURCE=.*:KERNEL_SOURCE="$(LINUXDIR)":' uClibc-$(UCLIBCVERS)/.config)
	grep 'DEVEL_PREFIX="$(DEVPREFIX)"' uClibc-$(UCLIBCVERS)/.config || \
	(echo "Patching" ; perl -pi -e's:DEVEL_PREFIX=.*:DEVEL_PREFIX="$(DEVPREFIX)":' uClibc-$(UCLIBCVERS)/.config)

install:
	mkdir -p $(INSTALLDIR) $(INSTALLDIR)/include
	cd uClibc-$(UCLIBCVERS) && $(MAKE) install
	cp uClibc-$(UCLIBCVERS)/include/libintl.h $(INSTALLDIR)/include
	cp uClibc-$(UCLIBCVERS)/include/getopt.h $(INSTALLDIR)/include
	cp uClibc-$(UCLIBCVERS)/include/reg*.h $(INSTALLDIR)/include
	cp uClibc-$(UCLIBCVERS)/include/printf.h $(INSTALLDIR)/include
	cp -vu $(INSTALLDIR)/lib/*.* $(INSTALLDIR)/lib/gcc/m68k-uclinux/$(GCCVERS)/

postinstall:
	@true	# nothing to do any more

clean:
	cd uClibc-$(UCLIBCVERS) && \
	$(MAKE) clean

distclean:
	find . -type f -name "*~"  -print0 | xargs -0 -r rm -v
	find . -type f -name ".#*" -print0 | xargs -0 -r rm -v
	rm -f uClibc-$(UCLIBCVERS)/.config*
