#!/bin/ksh
# @(#) manifest.ksh 2.1 94/05/19
# 93/06/18 john h. dubois iii (john@armory.com)
# 94/02/15 Added multiple compilers option.
# 94/05/19 Added option of giving args to compiler.

typeset -L20 word
typeset -i i=0

name=${0##*/}
Usage="Usage: $name [compiler] [args]"

Compiler=$1
[ $# -ge 1 ] && shift
set -A Args -- "$@"

set -- gcc -v icc -# cc -z

if [ "$Compiler" = -h ]; then
    print \
"$name: print compiler manifest defines.
$Usage
If no compiler name is given, the standard compiler (cc) is used.
If any args are given, they are passed to the compiler.  For example,
$name cc -dos
can be used to get the manifest defines for the DOS cross compiler.
The compilers that $name knows how to extract manifest defines from are:"
    while [ $# -gt 0 ]; do
	print -n "$1 "
	shift 2
    done
    print ""
    exit 0
fi

[ -z "$Compiler" ] && Compiler=cc

Tail=${Compiler##*/}

while [ $# -gt 0 ]; do
    if [ "$1" = "$Tail" ]; then
	Compiler="$1 ${Args[*]} $2"
	break
    fi
    shift 2
done

if [ -z "$Compiler" ]; then
    print -u2 "$Compiler: unknown compiler."
    exit 1
fi

tmpfile=/tmp/manif$$.c

> $tmpfile

for word in $($Compiler -c $tmpfile 2>&1); do
    if [[ "$word" = -D* ]]; then
	line="$line${word#-D}"
	let i+=1
	if [ i -eq 4 ]; then
	    echo "${line%*( )}"
	    line=
	    i=0
	fi
    fi
done
rm -f $tmpfile

[ -n "$line" ] && echo "${line%*( )}"
