#!/bin/sh
# $Id: configure,v 7.55 2000/05/12 22:01:05 paul Exp $
# Copyright (c) Bullseye Testing Technology
# Email: info@bullseye.com
#
# USAGE
#   configure [--prefix=P] [--CC=CMD]
#   configure [--prefix=P] [--hostCC=CMD] [--targetCC=CMD]
#       [--targetar=CMD]
#       [--hpKernel]
#
# DESCRIPTION
#   Configures the C-Cover Makefile for any Unix system.
#   The input is Makefile.in, the output Makefile.
#
# OPTIONS
#   --prefix=P
#       Installs executables in Pbin and libraries in Plib.
#       Be sure to include a trailing "/" if P is a directory.
#       If you do not specify this option, this script prompts you.
#
#   --CC=CMD
#       The host and target compilers are both CMD.
#
#   --hostCC=CMD
#       The host C++ compiler is CMD.
#
#   --hpKernel
#       Build the run-time library for HP-UX kernel
#
#   --targetCC=CMD
#       The target C or C++ compiler is CMD.
#       If you cross-compile test programs, specify the cross-compiler here.
#
#   --targetar=CMD
#       Builds the run-time library with librarian CMD.  The default is "ar".
#       If you do not specify this option, and the host and target compilers
#       differ, this script prompts you.
#
# EXAMPLES
#   configure
#   configure --prefix=$HOME/ccover/ --hostCC=c++
#   configure --hostCC=CC --targetCC="cc68k -I/vw/h"
#
# NOTES
#   This script is intended work with either the Bourne shell (sh) or
#   Korn shell (ksh).  The Korn shell is preferred.
#

#=========== Variables =================================================

ar1="ar -r"     # host   librarian
ar2=            # target librarian
bin=            # alias ${prefix}bin
cc1=            # host C++ compiler command
cc2=            # target compiler command
cflags1=        # command line options for CXXFLAGS (host)
cflags1cmd=     # command line options for host programs (not runtime)
cflags2=        # command line options for CXXFLAGS2 (target)
fail=0          # for internal testing, --fail option
hosted=0        # true if host and target compiler differ
hpKernel=0
i=              # mode 1=host, 2=target
interactive=0   # true if prompted for any input
lflags=         # command line options for LDFLAGS (host)
lib1_o=         # objects needed in Bullseye C library (host)
libc_b=0        # true to use the Bullseye C standard library archive
libh=0          # true if any Bullseye C library header required
library_list=libcov.a
link_b=1        # false if target compiler cannot link
prefix=         # installation directory
rtos=0          # true if hosting known operating system
uname="`uname`" # alias

# If Korn shell
if [ ~ != "~" ]
then
	# Turn off noclobber
	set +o noclobber
	# In case rm is a batch file created by the user
	#   For transmeta.com
	if [ -x /bin/rm ]
	then
		alias rm=/bin/rm
	fi
fi

# Make sure command test is working
if [ "a a" = b ] || [ ! "a a" = "a a" ]
then
	echo The system command \"test\" is broken, \
		contact Bullseye Testing Technology
	exit 1
fi

if [ "$uname" = SunOS ] && uname -r | grep '^[5-9]' >/dev/null
then
	uname=Solaris
fi

# we cannot use -e because if-statement sub-commands normally fail
set -u  # using unset variable is an error

#=========== Functions =================================================

#-------------------------- Function -----------------------------------
# Name:     log_err
# Args:       -0    - begin new section
#             $*    - a message
# Return:     success
# Purpose:  Display an error message and append it to the log
log_err() {
	if [ ".$1" = ".-0" ]
	then
		shift
		echo "" >>config.log
		if [ "$i" = "" ]
		then
			echo "$*" >>config.log
		else
			echo $i "$*" >>config.log
		fi
	else
		echo "$*" >>config.log
	fi
	echo "$*"
}

#-------------------------- Function -----------------------------------
# Name:     log_inf
# Args:       -0    - begin new section
#             $*    - a message
# Return:     success
# Purpose:  Append a message to the log
log_inf() {
	if [ ".$1" = ".-0" ]
	then
		shift
		echo "" >>config.log
		if [ "$i" = "" ]
		then
			echo "$*" >>config.log
		else
			echo $i "$*" >>config.log
		fi
	else
		echo "$*" >>config.log
	fi
}

#-------------------------- Function -----------------------------------
# Name:     f_cc
# Args:       $* - compiler options
# Return:     success of compiler
# Purpose:  Compile (but do not link) config.c with specified options
f_cc() {
	# Work around bug in /bin/sh on HP-UX 9.05 where $* changes
	star="$*"
	eval cmd=\"\$cc$i -Ilibc$i \$cflags$i -c\"
	cmd=`echo $cmd | sed -e 's/ -l[a-zA-Z_0-9]*//g' -e 's/ [^. ]*\.a//g' `
	log_inf -0 $cmd $star config.c
	rm -f config.o
	if $cmd $star config.c >>config.log 2>&1 && [ -f config.o ]
	then
		log_inf cc success
		return 0
	else
		log_inf cc fail
		return 1
	fi
}

#-------------------------- Function -----------------------------------
# Name:     f_ld
# Args:       $* - compiler options
# Return:     success of compiler
# Purpose:  Compile and link config.c with specified options
#           If link_b is false and using target compiler, skip linking.
f_ld() {
	# Work around bug in /bin/sh on HP-UX 9.05 where $* changes
	star="$*"
	rm -f config
	if [ $link_b = 0 -a $i = 2 ]
	then
		log_inf skip link
		f_cc $star
		# do not insert any commands here, they will interfere with $?
		return $?
	else
		eval cmd=\"\$cc$i -Ilibc$i \$cflags$i -o config\"
		# If $* contains config.c already
		if echo $star | grep config.c >/dev/null 2>&1
		then
			cmd="$cmd $star"
		else
			cmd="$cmd $star config.c"
		fi
		log_inf -0 $cmd
		if $cmd >>config.log 2>&1 && [ -f config ]
		then
			log_inf ld success
			return 0
		else
			log_inf ld fail
			return 1
		fi
	fi
}

#-------------------------- Function -----------------------------------
# Name:     run
# Args:       $* - compiler options
# Return:     success of compiler
# Purpose:  Compile, link and run config.c with specified options.
run() {
	if f_ld $*
	then
		if [ $i = 1 -o $hosted != 1 ]
		then
			if ./config >>config.log 2>&1
			then
				log_inf run success
				return 0
			else
				log_inf run fail
				return 1
			fi
		else
			log_inf run skip
			return 0
		fi
	else
		return 1
	fi
}

#=========== Main ======================================================

rm -f Makefile
if [ -r Makefile.in ]
then
	cp Makefile.in Makefile
	chmod +w Makefile
else
	log_err -0 error: Makefile.in missing or not accessible
	exit 1
fi

# parse command line options
if [ "$#" != 0 ]
then
	for i
	do
		case "$i" in
		--hpKernel)   hpKernel=1 ;;
		--prefix=*)   prefix=`echo $i | sed 's,[^=]*=,,'` ;;
		--CC=*)       cc1=`echo $i | sed 's,[^=]*=,,'`; cc2="$cc1" ;;
		--hostCC=*)   cc1=`echo $i | sed 's,[^=]*=,,'` ;;
		--targetCC=*) cc2=`echo $i | sed 's,[^=]*=,,'` ;;
		--targetar=*) ar2=`echo $i | sed 's,[^=]*=,,'` ;;
		--fail)       fail=1 ;; # for internal testing only
		*)            echo $0: unknown option \"$i\"; exit 1 ;;
		esac
	done
fi

# remove previous build, changes may be incompatible
rm -f *.a *.o *.obj config.log

# Check grep -e
if echo x | grep -e x >/dev/null 2>&1
then
	grep=grep
	log_inf -0 using grep -e
else
	if echo x | egrep -e x >/dev/null 2>&1
	then
		grep=egrep
		log_inf -0 using egrep -e
	else
		log_err -0 error: neither grep nor egrep supports option -e
		exit 1
	fi
fi

if [ ! -x which ]
then
	# Build command which
	if [ "$cc1" = "" ]
	then
		t=cc
	else
		t=$cc1
	fi
	$t -o which which.c >/dev/null 2>&1 || 
		gcc -o which which.c
fi

# Disable Coverage Build
if ./which cov01 >/dev/null 2>&1 && \
	[ -x "`./which cov01`" -a "`./which cov01`" != ./cov01 ]
then
	cov01 -0q >/dev/null 2>&1
fi

# get installation directory
if [ "$prefix" = "" ]
then
	echo Enter the install directory prefix.  Shell variables allowed.  '[$HOME/ccover/]'
	read prefix
	interactive=1
	if [ "$prefix" = "" ]
	then
		prefix=$HOME/ccover/
	else
		eval p=\"$prefix\"
		if [ "$p" != "$prefix" ]
		then
			# shell variable used
			prefix="$p"
			echo installation directory: $prefix
		fi
	fi
fi

# If prefix ends with a / but does not exist
if [ $prefix != `echo $prefix | sed 's,/$,,'` -a ! -d $prefix ]
then
	echo Creating directory $prefix
	mkdir `echo $prefix | sed 's,/$,,'`
fi

# If prefix not an existing directory, prompt to create it
if [ ! -d $prefix -a $interactive = 1 ]
then
	echo "Is prefix \"$prefix\" a directory? [y]"
	read yn
	interactive=1
	if [ "$yn" != n ]; then
		echo Creating directory $prefix
		mkdir `echo $prefix | sed 's,/$,,'`
	fi
fi

# If prefix is a directory but does not end with a /, add one
if [ -d $prefix -a $prefix = `echo $prefix | sed 's,/$,,'` ]
then
	prefix=$prefix/
fi
bin=${prefix}bin

# If $bin exists already
if [ -d "$bin" ]
then
	# If $bin empty or (covc exists and all files linked together)
	if [ "`ls $bin`" = "" -o \( -f $bin/covc -a \
		`ls -i $bin | fgrep -v .cfg | sed -e 's/^[ ]*//' -e 's/ .*//' | \
		uniq | wc -l` = 1 \) ]
	then
		# Okay to install
		true
	else
		log_err Directory $bin contains files other than C-Cover.
		log_err Specify an empty or non-existing installation directory,
		log_err or a directory containing C-Cover v3+ to overwrite.
		exit 1
	fi
fi

# Create sub directories if needed
for i in bin include lib
do
	if [ -d $prefix$i ]
	then
		true
	else
		echo Creating directory $prefix$i
		mkdir $prefix$i
		chmod 777 $prefix$i
	fi
done

# Get compiler commands
if [ "$cc1" = "" -o "$cc2" = "" ]
then
	t=cc
	if [ "$uname" = HP-UX ]
	then
		t=aCC
	fi
	if [ "$uname" = Solaris ] &&
		[ "`CC -c -fast -O3 -DCONFIG_compile=1 config.c 2>&1`" = "" ]
	then
		t="CC -fast -O3 -w"
	fi
	# We prefer GNU C++
	if [ -x /usr/local/bin/c++ -o -x /usr/bin/c++ -o -x $HOME/bin/c++ -o \
		-x c++ ]
	then
		t="c++ -O2 -Wall"
	fi
	if [ "$cc1" = "" ]
	then
		echo Enter the host C++ compiler command \[$t\]
		read cc1
		interactive=1
		if [ "$cc1" = "" ]
		then
			cc1=$t
		else
			eval cc1=\"$cc1\"
		fi
	fi
	if [ "$cc2" = "" ]
	then
		echo Enter the target C or C++ compiler command \[$cc1\]
		read cc2
		interactive=1
		if [ "$cc2" = "" ]
		then
			cc2="$cc1"
		else
			eval cc2=\"$cc2\"
		fi
	fi
fi

# Remove -c from compiler commands
cc1=`echo $cc1" " | sed -e 's/ -c / /g' -e 's/[ ]*$//' `
cc2=`echo $cc2" " | sed -e 's/ -c / /g' -e 's/[ ]*$//' `

if [ "$cc1" != "$cc2" -o $hpKernel = 1 ]
then
	hosted=1
	library_list="$library_list libcov2.a"
fi

# for internal testing, add fail macro
if [ "$fail" = 1 ]
then
	cflags1="$cflags1 -DCONFIG_fail=1"
	cflags2="$cflags2 -DCONFIG_fail=1"
fi

# set target librarian
if [ "$uname" = QNX ]
then
	ar1="cc -A"
fi
if [ "$ar2" = "" -a $hosted = 1 -a $hpKernel = 0 ]
then
	echo Enter the target librarian command \[$ar1\]
	read ar2
	interactive=1
fi
if [ "$ar2" = "" ]
then
	ar2="$ar1"
fi

# Check that compilers are found in our bin first
list="
	`echo $cc1 | sed -e 's, .*,,' -e 's,.*/,,'`
	`echo $cc2 | sed -e 's, .*,,' -e 's,.*/,,'`
	CC
	aCC
	c++
	c89
	cc
	g++
	gcc
	xlC
"
for i in `echo "$list" | sort -u`
do
	touch $bin/$i
	chmod 777 $bin/$i
	# If compiler not found in our bin
	if ./which $i >/dev/null 2>&1 && [ -x "`./which $i`" ]
	then
		if [ "`./which $i`" != $bin/$i  ]
		then
			log_err Directory $bin does not occur in PATH before `./which $i`
			log_err Add $bin to the front of your PATH environment variable.
		fi
	else
		log_err Add $bin to the front of your PATH environment variable.
	fi
	rm $bin/$i
done

i=
log_inf -0 begin
log_err configure '$Revision: 7.55 $ working...' | sed 's/$[^ ]* //g'
log_inf pwd=`pwd`
log_inf "uname -s=`uname -s` -m=`uname -m` -v=`uname -v` -a=`uname -a`"
log_inf s=`grep 'ababbcbc,' *.cpp || true`
if [ -f ../ccoverLicense.txt ]
then
	log_inf ccoverLicense.txt=$(cat ../ccoverLicense.txt)
fi
$cc1 -v >>config.log 2>&1

# make directories for enabled Bullseye C Library include files
rm -f -r libc1 libc2 libc3
mkdir libc1 libc1/sys libc2 libc2/sys libc3 libc3/sys
chmod 777 libc1 libc1/sys libc2 libc2/sys libc3 libc3/sys
ln libc0/libcdef.h libc1/
ln libc0/libcdef.h libc2/

# identify system with macro SYS__$sysname, e.g. -DSYS__SunOS=1
sysname=`echo $uname | sed 's/[^a-zA-Z_0-9]//g'`
cflags1="$cflags1 -DSYS__$sysname=1"
cflags2="$cflags2 -DSYS__$sysname=1"

#---------------------------------------
# Check host compiler is valid command
#
i=1
cmd="$cc1 -Ilibc1 $cflags1 -o config"
log_inf -0 $cmd -DCONFIG_mode_Cpp=1 config.c 
if $cmd -DCONFIG_mode_Cpp=1 config.c >>config.log 2>&1 && [ -f config ]
then
	log_inf $cc1 link okay
else
	log_inf -0 $cmd -DCONFIG_mode_C=1 config.c 
	if $cmd -DCONFIG_mode_C=1 config.c >>config.log 2>&1 && [ -f config ]
	then
		log_err error: \"$cc1\" compiles .c files as C.  Specify a command which compiles .c files as C++.  The most common are \"c++\", \"g++\", and \"CC\"
	else
		log_err error: \"$cc1\" is not a C++ compiler
	fi
	exit 1
fi
if [ -x config ] && ./config
then
	log_inf $cc1 run okay
else
	log_err error creating executable with \"$cc1\"
	exit 1
fi

#---------------------------------------
# Check target compiler is valid command
#
if [ $hosted = 1 ]
then
	i=2
	if f_cc -DCONFIG_compile=1
	then
		log_inf $cc2 compile okay
	else
		log_err error running compiler \"$cc2\"
		exit 1
	fi

	if f_cc -DCONFIG_pSOS=1
	then
		log_inf pSOS targeted.
		sed "/^CXXFLAGS2/s/SYS_UNIX/SYS_pSOS/" Makefile >f1.tmp
		mv f1.tmp Makefile
		rtos=1
	fi

	if f_cc -DCONFIG_VxWorks=1
	then
		log_inf VxWorks targeted.
		sed "/^CXXFLAGS2/s/SYS_UNIX/SYS_VxWorks/" Makefile >f1.tmp
		mv f1.tmp Makefile
		rtos=1
	fi

	if [ $hpKernel = 1 ]
	then
		log_inf hpKernel targeted.
		sed "/^CXXFLAGS2/s,SYS_UNIX,SYS_hpKernel," Makefile >f1.tmp
		mv f1.tmp Makefile
		rtos=1
	fi
fi

#---------------------------------------
# Tests for both host and target compiler
#
if [ $hosted != 1 ]
then
	# host and target are same, run tests only once
	each=1
else
	each="1 2"
fi

for i in $each
do
	# variables local to this loop
	ar=             # alias ar1 or ar2
	cc=             # alias cc1 or cc2
	cflags=         # compiler options, assigned to cflags[12] at loop end

	eval ar=\"\$ar$i\"
	eval cc=\"\$cc$i\"
	eval cflags=\"\$cflags$i\"

	# check include directory
	if f_cc -DCONFIG_include=1
	then
		true
	else
		log_err error on \#include, add -I to the compiler command \"$cc\"
		log_err for example, \"$cc -I/usr/include\"
		exit 1
	fi

	if [ $i = 2 ]
	then
		# determine if target compiler can link (VxWorks cannot)
		if f_ld -DCONFIG_link=1
		then
			log_inf link okay first
		else
			link_b=0
		fi
		if f_ld -DCONFIG_link=1 -UCONFIG_fail
		then
			log_inf link okay second
		else
			log_err error linking, contact Bullseye Testing Technology
			exit 1
		fi
	fi

	#---------------------------------------
	# Check librarian
	#
	log_inf -0 ar $ar check
	f_cc -DCONFIG_include=1
	rm -f f1.a
	if $ar f1.a config.o >>config.log 2>&1
	then
		log_inf $ar okay
		if [ "$ar" != "ar -r" ]
		then
			# Use non-/ delimiters to allow / in $ar
			sed "/^AR$i /s|=.*#|= $ar #|" Makefile >f1.tmp
			mv f1.tmp Makefile
		fi
	else
	if [ "$ar" = "ar -r" ]
	then
		# try without the dash
		if ar r f1.a config.o >>config.log 2>&1
		then
			ar="ar r"
			log_inf $ar okay
			# Use non-/ delimiters to allow / in $ar
			sed "/^AR$i /s|=.*#|= $ar #|" Makefile >f1.tmp
			mv f1.tmp Makefile
		fi
	else
		# Check for MRI librarian which takes the library and object in reverse
		rm -f f1.a
		if $ar config.o f1.a >>config.log 2>&1 && [ -f f1.a ]
		then
			# Use non-/ delimiters to allow / in $ar
			sed "/^AR$i /s|=.*#|= arMRI.sh $ar #|" Makefile >f1.tmp
			mv f1.tmp Makefile
		else
			log_err error running librarian $ar
			exit 1
		fi
	fi
	fi

	log_inf -0 ranlib check
	if [ -x /bin/ranlib -o -x /usr/bin/ranlib ] &&
		[ "`ranlib f1.a | tee -a config.log`" = "" ]
	then
		log_inf ranlib found
	else
		log_inf ranlib not needed
		fgrep -v RANLIB$i Makefile >f1.tmp
		mv f1.tmp Makefile
	fi
	rm -f f1.a

	log_inf -0 strip check
	if ./which strip >/dev/null 2>&1
	then
		log_inf strip found
	else
		log_inf strip not found
		fgrep -v strip Makefile >f1.tmp
		mv f1.tmp Makefile
	fi

	#---------------------------------------
	# sys/time.h "extern struct" problem on HP-UX 10.20
	#
	if f_cc -DCONFIG_time=1
	then
		true
	else
		if [ -f /usr/include/sys/time.h ]
		then
			sed 's/extern struct sigevent;/struct sigevent;/' /usr/include/sys/time.h >libc$i/sys/time.h
			if f_cc -DCONFIG_time=1
			then
				log_err "sys/time.h not working, patching to libc$i/sys/time.h"
				if [ $i = 1 ]
				then
					ln libc1/sys/time.h libc3/sys/.
				fi
			else
				rm libc$i/sys/time.h
			fi
		fi
	fi

	#---------------------------------------
	# Configure for standard headers
	#
	if [ $i = 1 ]
	then
		list="limits stdarg stddef stdlib unistd"
	else
		list="limits stdlib unistd"
	fi
	for m in $list
	do
		if f_cc -DCONFIG_$m=1
		then
			true
		else
			ln libc0/$m.h libc$i/
			if f_cc -DCONFIG_$m=1 -UCONFIG_fail
			then
				log_err $m.h not working, using libc0/$m.h
				libh=1
			else
				if f_cc -DCONFIG_$m=1 -UCONFIG_fail -Dsize_t=unsigned
				then
					log_err $m.h not working, using libc0/$m.h and -Dsize_t=unsigned
					libh=1
					cflags="$cflags -Dsize_t=unsigned"
				else
					log_err $m.h not working, contact Bullseye Testing Technology
					rm -f libc$i/$m.h
					echo press enter to continue; read EAT
				fi
			fi
		fi
	done

	if [ $i = 2 -a $rtos = 1 ] || f_cc -DCONFIG_off=1
	then
		true
	else
		log_err off_t not declared, using -Doff_t=long
		cflags="$cflags -Doff_t=long"
	fi

	eval cc$i=\""$cc"\"
	eval cflags$i=\""$cflags"\"
done

if [ $hosted != 1 -o $hpKernel = 1 ]
then
	cflags2="$cflags1"
fi

#---------------------------------------
# Host tests
#
i=1

if run -DConfig_bool
then
	true
else
	log_inf using -Dbool=int -Dfalse=0 -Dtrue=1
	cflags1="$cflags1 -Dbool=int -Dfalse=0 -Dtrue=1"
fi

#---------------------------------------
# Check for known compiler defects
#
if run -DCONFIG_defect=1
then
	true
else
	log_err A defect was detected in the compiler $cc1.  Use a different compiler
	exit 1
fi

if f_cc -DCONFIG_wait=1
then
	true
else
	ln libc0/wait.h libc1/sys
	if f_cc -DCONFIG_wait=1 -UCONFIG_fail
	then
		log_err wait.h not working, using libc0/wait.h
		libh=1
	else
		log_err wait.h not working, contact Bullseye Testing Technology
		rm -f libc1/sys/wait.h
		echo press enter to continue; read EAT
	fi
fi

if run -DConfig_exceptions
then
	true
else
	success=0
	for m in \
		-fhandle-exceptions \
		+eh
	do
		if run -DConfig_exceptions $m
		then
			log_inf "using $m"
			cflags1cmd="$cflags1cmd $m"
			success=1
			break
		fi
	done
	if [ $success = 0 ]
	then
		log_err "Cannot enable exception handling, contact Bullseye Testing Technology"
		exit 1
	fi
fi

chmod +w config.c
if run -DCONFIG_fcntl=1
then
	true
else
	log_err fcntl not working. This build is safe for single user access only.
	cflags1="$cflags1 -DFILE_no_fcntl=1"

	# Determine whether to declare flock
	if f_cc -DCONFIG_fcntl=1 -UCONFIG_fail
	then
		true
	else
		log_inf using -DStd_fcntl=1
		cflags1="$cflags1 -DStd_fcntl=1"
	fi
fi

if f_cc -DCONFIG_pid=1
then
	true
else
	log_err pid_t not declared, using -Dpid_t=int
	cflags1="$cflags1 -Dpid_t=int"
fi

if f_ld -DCONFIG_remove=1
then
	true
else
	log_err remove not working, using unlink
	cflags1="$cflags1 -Dremove=unlink"
fi

if f_cc -DConfig_typename
then
	true
else
	log_inf "using -Dtypename=class"
	cflags1cmd="$cflags1cmd -Dtypename=class"
fi

if run -DCONFIG_ctype=1
then
	true
else
	log_err ctype not working, using libc0/ctype.h and stdCtype.cpp
	ln libc0/ctype.h libc1/
	lib1_o="$lib1_o stdCtype.o"
	libc_b=1
	libh=1
fi

if run -DCONFIG_fnmatch=1
then
	true
else
	log_err fnmatch not working, using libc0/fnmatch.h
	ln libc0/fnmatch.h libc1/
	lib1_o="$lib1_o stdFnmatch.o"
	libc_b=1
	libh=1
fi

# sys/unistd.h pthread_atfork problem on HP-UX 11
if fgrep 'pthread_atfork(void (*prepare)()' /usr/include/sys/unistd.h >/dev/null 2>&1
then
	log_err sys/unistd.h not working, patching to libc1/sys/unistd.h
	sed '/pthread_atfork(void/{ N
		s/()/(void)/g
		}' /usr/include/sys/unistd.h >libc1/sys/unistd.h
	ln libc1/sys/unistd.h libc3/sys/.
fi

#---------------------------------------
# Run-time tests
#
run_time() {
	i=$1
	eval cc=\"\$cc$i\"
	eval cflags=\"\$cflags$i\"

	# Check target -E option
	log_inf -0 check -E
	cmd=`echo $cc | sed -e 's/ -l[a-zA-Z_0-9]*//g' -e 's/ [^. ]*\.a//g' `
	$cmd -DCONFIG_cpp=1 -E config.c 2>>config.log | sed 's/#.*//' >tmp.c || true
	if grep cpp_check <tmp.c >>config.log 2>&1
	then
		log_inf output -E
		log_inf $cc -c -UCONFIG_fail tmp.c
		if $cc -c -UCONFIG_fail tmp.c >>config.log 2>&1 && [ -f tmp.o ]
		then
			log_inf compile -E
			if [ $i = 2 ]
			then
				log_inf run -E skip
			else
				if $cc -o config tmp.o >>config.log 2>&1 && [ -f config ] &&
					[ "`./config`" = "cpp_check" ]
				then
					log_inf run -E
				else
					log_err cannot run output of $cc -E, contact Bullseye Testing Technology
					echo press enter to continue; read EAT
				fi
			fi
		else
			log_err cannot compile output of $cc -E, contact Bullseye Testing Technology
			echo press enter to continue; read EAT
		fi
		rm -f tmp.o
	else
		log_err $cc does not preprocess with -E, contact Bullseye Testing Technology
		echo press enter to continue; read EAT
	fi

	if [ $i = 1 -o $rtos = 0 ]
	then
		#---------------------------------------
		# Check target library functions
		#
		if run -DCONFIG_atexit=1
		then
			log_inf atexit found
		else
			if run -DCONFIG_onexit=1
			then
				log_err atexit missing or broken, on_exit found
				cflags="$cflags -Datexit=on_exit"
			else
				cflags="$cflags -DCOVRT_NOATX=1"
				log_err atexit missing or broken
				echo To save measurements, test programs compiled with "$cc" must call one of:
				echo "  "cov_write
				echo "  "exit, _exit, abort
				echo "  "execl, execle, execlp, execlpe
				echo "  "execv, execve, execvp, execvpe
				echo press enter to continue; read EAT
			fi
		fi

		chmod +w config.c
		if run -DCONFIG_fcntl=1
		then
			true
		else
			log_err fcntl not working. This build is safe for single user access only.
			cflags="$cflags -DCOVRT_NOFCNTL=1"
		fi

		if f_ld -DCONFIG_getenv=1
		then
			true
		else
			log_err getenv not working, using -DCOVRT_NOENV=1
			cflags="$cflags -DCOVRT_NOENV=1"
		fi

		if run -DCONFIG_nanosleep=1
		then
			true
		else
			# Try -lposix4 before -lrt since it is more compatible
			if run -lposix4 -DCONFIG_nanosleep=1
			then
				log_err using -lposix4
				cflags="$cflags -DCovpat_lposix4=1"
			else
			if run -lrt -DCONFIG_nanosleep=1
			then
				log_err using -lrt
				cflags="$cflags -DCovpat_lrt=1"
			else
			if run -DPOSIX_4D9 -DCONFIG_nanosleep=1
			then
				log_err using -DPOSIX_4D9 for nanosleep
				cflags="$cflags -DPOSIX_4D9"
			else
				log_err nanosleep not working, using libc0/time.h
				if [ -f libc$i/time.h ]
				then
					true
				else
					ln libc0/time.h libc$i
				fi
			fi
			fi
			fi
		fi
	fi

	# Check for standard pthread first
	if run -DCONFIG_pthread=1
	then
		true
	else
		if run -lpthread -DCONFIG_pthread=1
		then
			log_err using -lpthread
			cflags="$cflags -DCOVPAT_lpthread=1"
			# If Tru64
			if [ $uname = OSF1 ]
			then
				# Check whether linking statically fails without -lexc
				#   If GNU, use -static else native option -non_shared
				if run -DConfig_gnu
				then
					opt=-static
				else
					opt=-non_shared
				fi
				#   If failure linking statically with -lpthread
				if f_ld -DCONFIG_pthread=1 $opt config.c -lpthread
				then
					true
				else
					# And if adding -lexc solves the problem
					if f_ld -DCONFIG_pthread=1 $opt config.c -lpthread -lexc
					then
						log_err using -lexc
						cflags="$cflags -DCovpat_lexc=1"
					else
						log_err error linking non-shared with -lpthread, contact Bullseye Testing Technology
						echo Press enter to continue; read EAT
					fi
				fi
			fi
		else
			if run -DCONFIG_exemplar=1
			then
				log_err pthread not found, using libc0/pthread.h for Exemplar
				ln libc0/pthread.h libc$i
				cflags="$cflags -DPTHREAD_exemplar=1"
			else
			if run -DCONFIG_pthread=1 -I/usr/include -I/usr/include/pthread/mit /lib/libpthread.so.0
			then
				# Linux Redhat 4
				log_err pthread not found, using -I/usr/include/pthread/mit /lib/libpthread.so.0
				cflags="$cflags -I/usr/include -I/usr/include/pthread/mit -DCovpat_lib_libpthread_so_0=1"
			else
			if f_cc -DCONFIG_VxWorks=1
			then
				log_err pthread not found, using libc0/pthread.h for VxWorks
				ln libc0/pthread.h libc$i
			else
			# Solaris last
			if run -DCONFIG_lwp=1
			then
				log_err pthread not found, using libc0/pthread.h for Solaris
				ln libc0/pthread.h libc$i
				cflags="$cflags -DPTHREAD_lwp=1"
			else
				log_err pthread not working, using libc0/pthread.h
				ln libc0/pthread.h libc$i
			fi
			fi
			fi
			fi
		fi
	fi

	# Check for shared library options
	# Check option -K before -f, for Solaris cc which takes both
	pic="
		-Kpic
		-fpic
	"
	PIC="
		-KPIC
		-fPIC
	"
	if [ "$uname" = HP-UX ]
	then
		pic="$pic +z"
		PIC="$PIC +Z"
	fi
	if [ "$uname" = SunOS ]
	then
		pic="$pic -pic"
		PIC="$PIC -PIC"
	fi
	for size in pic PIC
	do
		eval t=\$$size
		for option in $t
		do
			echo "$cc -DCONFIG_compile=1 $option config.c" >>config.log
			if $cc -DCONFIG_compile=1 $option config.c >f1.tmp 2>&1
			then
				if $grep <f1.tmp >/dev/null 2>&1 \
					-e "unrecognized option" \
					-e '[Uu]known' \
					-e '[Ww]arning' \
					-e "$option"
				then
					[ -f f1.tmp ] && cat f1.tmp >>config.log
				else
					library_list="$library_list libcov$i-$size.a"
					sed "s/@$size$i/$option/" Makefile >f1.tmp
					mv f1.tmp Makefile
					break
				fi
			fi
			[ -f f1.tmp ] && cat f1.tmp >>config.log
		done
	done

	eval cflags$i=\"\$cflags\"
}

run_time 1
if [ $hosted = 1 ]
then
	run_time 2
fi

#---------------------------------------
# Platform-specific adjustments
#
# If Convex, silence optimization reports
if [ $uname = ConvexOS -o $uname = HP-UX ]
then
	i=1
	if f_cc -DCONFIG_convex=1 -or none
	then
		cc1="$cc1 -or none"
	fi
	i=2
	if f_cc -DCONFIG_convex=1 -or none
	then
		cc2="$cc2 -or none"
	fi
fi

#---------------------------------------
# Update Makefile
#
i=

sed "/^prefix =/s|=.*|= $prefix|" Makefile >f1.tmp
mv f1.tmp Makefile

# Move options -I,-D,-U to end of cflags
for arg in $cc1
do
	case $arg in
	-I*|-D*|-U*) cflags1="$cflags1 $arg" ;;
	esac
done
for arg in $cc2
do
	case $arg in
	-I*|-D*|-U*) cflags2="$cflags2 $arg" ;;
	esac
done
cc1=`echo $cc1 | sed -e 's/ -[IDU][^ ]*//g' `
cc2=`echo $cc2 | sed -e 's/ -[IDU][^ ]*//g' `

# Remove libraries (-lxxx and xxx.a)
cc_c1=`echo $cc1 | sed -e 's/ -l[a-zA-Z_0-9]*//g' -e 's/ [^. ]*\.a//g' `
cc_c2=`echo $cc2 | sed -e 's/ -l[a-zA-Z_0-9]*//g' -e 's/ [^. ]*\.a//g' `

sed <Makefile \
	-e "/^CXX  /s,c++ -O,$cc1," \
	-e "/^CXX2 /s,c++ -O,$cc_c2," \
	-e "/^CXXFLAGS  =/s,[ ]*#,$cflags1 $cflags1cmd #," \
	-e "/^CXXFLAGSRT=/s,[ ]*#,$cflags1 #," \
	-e "/^CXXFLAGS2 =/s,[ ]*#,$cflags2 #," \
	-e "/^LD  /s,c++ -O,$cc1," \
	-e "/^LDFLAGS/s|[ ]*#|$lflags #|" \
	-e "s/ -DCONFIG_fail=1//g" \
	-e "s/ libcov.a/ $library_list/g" \
	-e "s/libcov1/libcov/g" >f1.tmp
mv f1.tmp Makefile

if [ $hosted = 1 ]
then
	if [ $hpKernel = 1 ]
	then
		t=hpKernel
	else
		t=`echo $cc2 | sed -e 's/ .*//' -e 's/[^ ]*\///g' -e 's/[^a-zA-Z0-9]//g'`
	fi

	sed -e "s/libcov2/libcov-${t}/g" \
		-e "s/successful./successful.  Target library is libcov-${t}.a/g" \
		Makefile >f1.tmp
	mv f1.tmp Makefile
fi

if [ $libc_b = 1 ]
then
	# uncomment lines containing @libc
	sed '/@libc/s/^#//' Makefile >f1.tmp
	mv f1.tmp Makefile
	for m in $lib1_o
	do
		sed "/^LIBC_O/s/[ ]*#/ $m #/" Makefile >f1.tmp
		mv f1.tmp Makefile
	done
	# Remove the comments for DEC Alpha make
	sed "s,# @libc.*,," Makefile >f1.tmp
	mv f1.tmp Makefile
fi

rm -f config.o config f1.* core
i=
if [ -s Makefile ]
then
	echo Makefile configured successfully.
	log_inf -0 diff Makefile.in Makefile
	diff Makefile.in Makefile >>config.log
	log_inf -0 end
else
	log_err Error configuring Makefile.
fi
# end of file
