1#!/bin/sh
2# @(#)cc-config.sh	1.16 19/09/22 Copyright 2002-2019 J. Schilling
3###########################################################################
4# Written 2002-2019 by J. Schilling
5###########################################################################
6# Configuration script called to verify system default C-compiler.
7# It tries to fall back to GCC if the system default could not be found.
8###########################################################################
9# The contents of this file are subject to the terms of the
10# Common Development and Distribution License, Version 1.0 only
11# (the "License").  You may not use this file except in compliance
12# with the License.
13#
14# See the file CDDL.Schily.txt in this distribution for details.
15# A copy of the CDDL is also available via the Internet at
16# http://www.opensource.org/licenses/cddl1.txt
17#
18# When distributing Covered Code, include this CDDL HEADER in each
19# file and include the License file CDDL.Schily.txt from this distribution.
20###########################################################################
21
22#
23# Usage:
24#	sh ./conf/cc-config.sh cc default-cc incs/Dcc.<platform>
25#
26if [ "$#" -lt 3 ]; then
27	echo 'Usage: sh ./conf/cc-config.sh [-echo] <cc> <default-cc> incs/Dcc.<platform>'
28	echo 'Options:'
29	echo '	-echo	Do not write into incs/Dcc.<platform> but echo to stdout'
30	echo
31	echo 'The "cc"         parameter is the name of the preferred C-compiler'
32	echo 'The "default-cc" parameter is the name of the default   C-compiler'
33	exit 1
34fi
35
36echo=echo
37if [ ".$1" = .-echo ]; then
38	echo=:
39	shift
40fi
41
42#
43# Try to make sure all error messages are in english.
44#
45LC_ALL=C
46LANG=C
47export LC_ALL
48export LANG
49
50CC=$1			# Working copy of discovered C-compiler
51CCOM=$1			# Remembered value of $CC
52ARG_CC=$1		# Name of preferred C-compiler
53DEF_CC=$2		# Name of default C-compiler
54PLATFORM_FILE=$3	# Where to write the output to
55CC_FOUND=FALSE
56
57#
58# Check whether we are using something like cc32, cc64, ...
59#
60if echo "$CC" | egrep '32|64' > /dev/null 2> /dev/null; then
61	#
62	# Use the basic command name for our tests
63	#
64	CC=`echo "$CC" | sed -e 's/32//' -e 's/64//'`
65fi
66
67${echo} "Trying to find $CC"
68
69#
70# Check if we are on SunOS-5.x and /usr/ucb is in PATH before /opt/SUNWspro/bin
71# /usr/ucb/cc will not work correctly to compile things on Solaris.
72#
73# This check will also catch the case where no Sun C-compiler is installed and
74# calling cc results in the message:
75#	/usr/ucb/cc:  language optional software package not installed
76#
77xos=`echo "$PLATFORM_FILE" | grep sunos5 `
78if [ -n "$xos" ]; then
79	#
80	# On Solaris, the type builtin is working.
81	#
82	xcc=`type "$CC" | grep '/usr/ucb/*cc' `
83	if [ -n "$xcc" ]; then
84		#
85		# We did find /usr/ucb/cc
86		#
87		echo											1>&2
88		echo 'Warning:' "$xcc"									1>&2
89		echo '         You should not have "/usr/ucb" in your PATH if you like to compile.'	1>&2
90		echo '         "/usr/ucb/cc" is not working correclty on Solaris.'			1>&2
91		echo '         If you did install a C-compiler in /opt/SUNWspro/bin, abort'		1>&2
92		echo '         fix your PATH and start again.'						1>&2
93		echo '         Otherwise GCC will be used.'						1>&2
94		echo											1>&2
95		sleep 60
96		CC=do-no-use-ucb-cc
97	fi
98fi
99
100#
101# There are old shells that don't support the 'type' builtin.
102# For this reason it is not a simple task to find out whether
103# this compiler exists and works.
104#
105# First try to run the default C-compiler without args
106#
107# eval needs a subshell to support the V7 Shell that does not do IO redirection
108# for builtins like eval.
109#
110(eval "$CC > /dev/null 2>&1") 2> /dev/null
111if [ "$?" = 0 ]; then
112	CC_FOUND=TRUE
113else
114	#
115	# Now try to run the default C-compiler and check whether it creates
116	# any output (usually an error message).
117	#
118	# This test will fail if the shell does redirect the error message
119	# "cc: not found". All shells I tested (except ksh) send this message to
120	# the stderr stream the shell itself is attached to and only redirects
121	# the output from the command. As there may no output if there is no
122	# binary, this proves the existence of the default compiler.
123	#
124	ccout=`(eval "$CC 2>&1") 2>/dev/null`
125	ret=$?
126
127	nf=`echo "$ccout" | grep 'not found' `
128	if [ "$ret" = 127 -a -n "$nf" ]; then
129		#
130		# ksh redirects even the error message from the shell, but we
131		# see that there is no executable because the exit code is 127
132		# we detect "command not found" if exit code is 127 and
133		# the message contains 'not found'
134		#
135		ccout=""
136	fi
137
138	if [ -n "$ccout" ]; then
139		CC_FOUND=TRUE
140	fi
141fi
142
143
144if [ "$CC_FOUND" = TRUE ]; then
145	${echo} "Found $CC"
146
147	#
148	# Call $CC and try to find out whether it might be "gcc" or "clang".
149	#
150	CC_V=`(eval "$CC -v > /dev/null") 2>&1`
151	GCC_V=`echo "$CC_V" | grep -i 'gcc.*version' `
152	CLANG_V=`echo "$CC_V" | grep -i clang `
153
154	if [ ".$GCC_V" != . ]; then
155		if (eval "gcc -v 2> /dev/null") 2>/dev/null; then
156			CC="gcc"
157		fi
158	elif [ ".$CLANG_V" != . ]; then
159		if (eval "clang -v 2> /dev/null") 2>/dev/null; then
160			CC="clang"
161		fi
162	fi
163	#
164	# Check whether "cc" or "gcc" are emulated by another compiler
165	#
166	if [ ".$ARG_CC" = .cc -o ".$ARG_CC" = .gcc ]; then
167		if [ "$CC" != "$ARG_CC" ]; then
168			${echo} "$ARG_CC is $CC"
169		fi
170	fi
171
172	if [  ".$CC" = ".$DEF_CC" ]; then
173		${echo} "Creating empty '$PLATFORM_FILE', using $DEF_CC as default compiler"
174		if [ "${echo}" = echo ]; then
175			# (:) needed to work around a V7 shell bug
176			(:)> $PLATFORM_FILE
177		else
178			echo "$DEF_CC"
179		fi
180	else
181		${echo} "Making $CC the default compiler in '$PLATFORM_FILE'"
182		if [ "${echo}" = echo ]; then
183			# (:) needed to work around a V7 shell bug
184			(:)> $PLATFORM_FILE
185			echo DEFCCOM=$CC > $PLATFORM_FILE
186		else
187			echo "$CCOM"
188		fi
189	fi
190	exit
191fi
192
193#
194# If the current default is gcc or anything != cc, try cc.
195# Last resort: try gcc.
196#
197XCC=cc
198if [ ".$CC" = ".gcc" -o ".$CC" != ".cc" ]; then
199	${echo} "Trying to find $XCC"
200	ccout=`(eval "$XCC -c tt.$$.c 2>&1") 2> /dev/null`
201	ret=$?
202	nf=`echo "$ccout" | grep 'not found' `
203	if [ "$ret" = 127 -a -n "$nf" ]; then
204		#
205		# ksh redirects even the error message from the shell, but we
206		# see that there is no executable because the exit code is 127
207		# we detect "command not found" if exit code is 127 and
208		# the message contains 'not found'
209		#
210		ccout=""
211	fi
212	xos=`echo "$PLATFORM_FILE" | grep sunos5 `
213	if [ -n "$xos" ]; then
214		#
215		# Our target is "sunos5"
216		#
217		xcc=`type "$XCC" | grep '/usr/ucb/*cc' `
218		#
219		# If "$ccout" is non-null, then the compiler printed it
220		#
221		if [ -n "$xcc" -a  -n "$ccout" ]; then
222			echo "Cannot use $XCC because $XCC is /usr/ucb/cc"
223		fi
224		if [ -z "$xcc" -a  -n "$ccout" ]; then
225			#
226			# cc found on Solaris, use it.
227			#
228			CC="$XCC"
229			CC_FOUND=TRUE
230		fi
231	else
232		#
233		# cc found on other OS, use it.
234		#
235		CC="$XCC"
236		CC_FOUND=TRUE
237	fi
238fi
239if [ "$CC_FOUND" = FALSE -a ".$CC" = ".$ARG_CC" ]; then
240	#
241	# No CC found yet, try gcc
242	#
243	XCC=gcc
244	${echo} 'Trying to find $XCC'
245	(eval "gcc -v") 2> /dev/null && CC=gcc CC_FOUND=TRUE
246fi
247
248#
249# Call $CC and try to find out whether it might be "gcc" or "clang".
250#
251CC_V=`(eval "$CC -v > /dev/null") 2>&1`
252GCC_V=`echo "$CC_V" | grep -i gcc-version `
253CLANG_V=`echo "$CC_V" | grep -i clang `
254
255if [ ".$GCC_V" != . ]; then
256	if (eval "gcc -v 2> /dev/null") 2>/dev/null; then
257		CC="gcc"
258		CC_FOUND=TRUE
259	fi
260elif [ ".$CLANG_V" != . ]; then
261	if (eval "clang -v 2> /dev/null") 2>/dev/null; then
262		CC="clang"
263		CC_FOUND=TRUE
264	fi
265fi
266#
267# Check whether "cc" or "gcc" are emulated by another compiler
268#
269if [ ".$ARG_CC" = .cc -o ".$ARG_CC" = .gcc ]; then
270	if [ "$CC" != "$ARG_CC" ]; then
271		${echo} "$ARG_CC is $CC"
272	fi
273fi
274
275if [ ".$CC" = ".$DEF_CC" ]; then
276	if [ "$CC_FOUND" = TRUE ]; then
277		${echo} "$XCC found, keeping current global default"
278	else
279		${echo} "$XCC not found, keeping current global default"
280	fi
281	${echo} "Creating empty '$PLATFORM_FILE', using $DEF_CC as default compiler"
282	if [ "${echo}" = echo ]; then
283		# (:) needed to work around a V7 shell bug
284		(:)> $PLATFORM_FILE
285	else
286		echo "$DEF_CC"
287	fi
288else
289	${echo} "Found $CC"
290	${echo} "Making $CC the default compiler in '$PLATFORM_FILE'"
291	if [ "${echo}" = echo ]; then
292		echo DEFCCOM=$CC > $PLATFORM_FILE
293	else
294		echo "$CC"
295	fi
296fi
297