#
# First part of Makefile for libc, headers and crt0 for CRIS.
#
# The makefile is split in two, of which the second (Real.makefile) is used at
# a second, recursive invocation of make
#  The second invocation is needed because there is no portable way to expand
# shell commands into make-variables (usable for targets) than in a command
# for a rule, and we need to get:
#  * The C files that are not directly simulator-dependant.
#  * The version of the compiler.
#
# This part of the makefile should be kept as short as possible, only enough
# to deduce what to pass on at the $(MAKE) line.
#
#  If I really want to, I could have just one Makefile, but it's not worth the
# hassle (to communicate the structure for maintenance purposes), when it's
# logically split in two anyway.

VPATH=math:stdlib:string:signal:stdio:time:support:errno:crt0:setjmp:.
SECOND_PART=Real.makefile

# CC always have a default-value.  Let's not get trapped by it.  Same for AR
# and RANLIB.
# (We cannot check whether it's empty, and get it from Real.makefile in that
# case; it *is* non-empty).
CC=gcc-cris
AR=ar-cris
RANLIB=ranlib-cris

# Base directory for the library files
dirprefix=/usr/local/cris/lib

# Compiler-version part to be added to the dirprefix
installdir=$(dirprefix)/gcc-lib/cris/$(CCVER)
# Where are the include-files
includedir=$(installdir)/include
# Where are the support programs (for now, only "ar" is used).
bindir=$(installdir)

# -Wall for development, only annoying for the user.
CFLAGS=-g # -Wall            # Keep debugging info if somebody wants to use it.
CPPFLAGS=-nostdinc -I. -I$(includedir)  # In case includedir is *not* the standard
AR=$(bindir)/ar
RANLIB=$(bindir)/ranlib
ARFLAGS=rv

#
# The shell call must be inside the for-loop or the argument gets too long (I guess).
# rfiles has to be "filtered", or it'll contain newlines.  Make does not like
# newlines in macro=value commandline-switches.  And don't remove them between
# multiple commandline-assignments

#
# SunOS needs a command-dependency checking prefix "!" for lines with the $?
# macro.  (Needed at development, but maybe not when building from clean?)
scrt0.o check-env almostall headers c-check install uninstall libc.a libsc.a libm.a libg.a all .c.o clean dist distrib: ChangeLog
	@if $(MAKE) --version 2>/dev/null | grep -i -c GNU >/dev/null; \
          then mflags=`echo --no-print-directory -s`; xc=; \
        else mflags=-s; if test "X`uname`" = "XSunOS"; then xc='!'; else xc=; fi; fi; \
	regexp="`grep 'regular expression:' read.me | tail +21c`"; \
	nofilespath=`$(SHELL) -c "egrep $$regexp read.me" | awk '-F:' '{print $$1;}'`; \
	nofiles=`for f in $$nofilespath; do basename $$f; done`; \
	cases=`for file in $$nofiles; do echo $$file' );;'; done; echo '*)'`; \
	cfiles=`$(MAKE) -f $(SECOND_PART) $$mflags echo-cfiles`; \
	rfiles=`for f in $$cfiles; do $(SHELL) -c "case $$f in $$cases echo $$f;; esac"; done`; \
	allcfiles=`$(MAKE) -f $(SECOND_PART) $$mflags echo-all-cfiles`; \
	fallcfiles=`echo $$allcfiles | sed -e 's/ / -o -name /g;'`;\
	pallcfiles=`find . -follow \( -name "$$fallcfiles" \) -print`; \
	newhfiles=`cat $$pallcfiles /dev/null | sed -n -e 's/^#[ ]*include[ ]*[<"]\([^>"]*\)[>"]/\1/p' | sort -u`; \
	nchfiles=0; ochfiles=-1; phfiles=;\
	while [ $$nchfiles -ne $$ochfiles ]; do \
         ochfiles=$$nchfiles; \
	 fhfiles=`echo $$newhfiles | sed -e 's/ / -o -name /g;'`; \
	 newhfiles=`find . -follow \( -name "$$fhfiles" \) -print`; \
	 phfiles=`echo $$phfiles $$newhfiles | tr ' ' '\012' | sort -u`; \
         newhfiles=`cat $$newhfiles /dev/null | sed -n -e 's/^#[ ]*include[ ]*[<"]\([^>"]*\)[>"]/\1/p' | sort -u`; \
	 nchfiles=`echo $$phfiles | wc -w`; done;\
	hfiles=`for f in $$phfiles; do basename $$f; done`;\
	if [ "X$(CCVER)" = "X" ]; then ccver=`$(CC) --version 2>/dev/null || echo none`; else ccver=$(CCVER); fi;\
	$(MAKE) -f $(SECOND_PART) $@ "XC=$$xc" "CCVER=$$ccver" "RFILES=`echo $$rfiles`" \
           "PCFILES=`echo $$pallcfiles`" "PHFILES=`echo $$phfiles`" \
           "HFILES=`echo $$hfiles`" "VPATH=$(VPATH)" "CC=$(CC)" "AR=$(AR)" \
           "dirprefix=${dirprefix}" "installdir=${installdir}" \
           "includedir=${includedir}" "bindir=${bindir}" \
	   "CFLAGS=${CFLAGS}" "CPPFLAGS=${CPPFLAGS}" \
	   "RANLIB=${RANLIB}" "ARFLAGS=${ARFLAGS}"

# The "|| echo none" above is needed so we can "make clean" (which should
# have nothing to do with compiler versions).
# End of Makefile
