# $Id: Makefile,v 1.3 2007-11-21 01:38:37 kiesling Exp $

all: gnumacrocomp stdh features imacros undef moveincludes

##
##   Compare C99 include macro definitions between ctpp 
##   and GNU cpp.  Note that we sort the output of the
##   -dM argument, because cpp retrieves symbols from
##   its hash table, while ctpp retrieves the symbols from 
##   a name space list.
##   Linux does not have tgmath.h; Solaris 2.8 also does not
##   have complex.h and stdint.h.
##
##   Expect some warnings about conflicting types for builtin
##   functions - use -fno-builtins with GCC to make them go away.
##
c99includes=assert.h ctype.h errno.h float.h inttypes.h \
	iso646.h limits.h locale.h math.h setjmp.h signal.h stdarg.h \
	stdbool.h stddef.h stdio.h stdlib.h string.h \
	time.h wchar.h wctype.h

ctppsource=`ls ../ctpp/*.c`

gnumacrocomp: 
	echo ========================== ; \
	echo C99 macro diffs with GNU cpp.; \
	echo ========================== ; \
	for i in $(c99includes) ; do \
		echo $$i; \
		echo ========================== ; \
		echo "#include <$$i>" | ctpp -dM - | sort | >$$i.ctpp; \
		echo "#include <$$i>" | cpp -dM - | sort | >$$i.cpp; \
		diff $$i.ctpp $$i.cpp ; \
		rm -f $$i.ctpp $$i.cpp ; \
	done

stdh:
	echo ========================== ; \
	echo C99 header files.          ; \
	echo ========================== ; \
	ctpp stdh.c stdh.i && gcc stdh.i -o stdh ; \
	./stdh ; \
	rm -f stdh.i stdh

features:
	echo ========================== ; \
	echo Preprocessor features.     ; \
	echo ========================== ; \
	ctpp features.c features.i ; rm -f features.i

imacros:
	echo ========================== ; \
	echo Imacros option. ; \
	echo ========================== ; \
	ctpp -dF imacros.out imacros.inc imacros.i ; \
	rm -f imacros.i ; \
	ctpp -imacros imacros.out imacros.c imacros.i ; \
	gcc -c -fno-builtin imacros.i -o imacros ; \
	rm -f imacros.i imacros.out imacros 

undef:
	echo ========================== ; \
	echo Undef option. ; \
	echo ========================== ; \
	echo ' ' | ctpp -undef -dM - >undef.out ; \
	if [ -s undef.out ] ; then \
		echo 'Failed.'; \
	else \
		echo 'Success.'; \
	fi ;
	rm -f undef.out

moveincludes:
	echo ========================== ; \
	echo Move includes.             ; \
	echo ========================== ; \
        ctpp -include imacros.inc -move-includes moveincs.c moveincs.i ; \
	gcc moveincs.i -o moveincs ; ./moveincs ; \
	rm -f moveincs.i moveincs

##
##  Not a documented target because the command line arguments vary
##  with system-specific installation and testing needs.
##
source:
	for i in $(ctppsource) ; do \
		echo $$i; \
		ctpp -I../include -I../ctpp \
			-DHOST_OS=linux-gnu -DHOST_CPU=i386 \
			$$i $$i.i && \
		gcc -c $$i.i -o $$i.o; \
		rm -f $$i.i $$i.o; \
	done