Ticket #7375: testcc

File testcc, 4.4 KB (added by drkirkby, 13 years ago)

Checks the type of C compiler, much more thorougly than using grep on output

Line 
1#!/bin/sh
2# Determine the type of C compiler, which can later
3# be used to determine the flags the compiler
4# will want. Do this by defining pre-processing a bit
5# of C++ code, and checking what are defined.
6
7# The Sun and GNU compilers have been tested.
8
9# The HP and GNU compilers have not been tested, but
10# use information gained from the documentation.
11
12# HP-UX C and C++ compiler.
13# http://docs.hp.com/en/7730/newhelp0610/preprocess.htm
14
15# IBM Compiler Reference - XL C/C++ for AIX, V10.1
16# http://www-01.ibm.com/support/docview.wss?uid=swg27012860&aid=1
17
18# Using HP C++ for Tru64 UNIX and Linux Alpha
19# http://h30097.www3.hp.com/cplus/ugu_impl.html#implem_chap
20
21
22# First, make sure the enviroment variable CC is defined.
23
24if [ -z "$CC" ]; then
25   echo "Sorry, you should define the enivronment variable CC"
26   exit 1
27fi
28
29# Create a test file. It does not need to be a complete
30# C++ file, as it is only pre-processed. So there is no
31# need for a 'main'
32
33TESTFILE=/tmp/test.$$.c
34
35# The flags for the GNU compilers do not change with
36# operating system, so there is no need to worry too
37# much about what system this is on.
38
39# However, many compilers will make themselves appears to be
40# GNU compilers, such as those from Intel and Sun, when
41# they are really improved versions of the basic GCC.
42# So care is needed to identify exactly what compiler it is.
43
44echo "#ifdef __GNUC__"                   > $TESTFILE 
45echo "#ifdef __INTEL_COMPILER"           >> $TESTFILE 
46echo "it_is_the_Intel_improved_GCC"      >> $TESTFILE
47echo "#elseif __SUNPRO_C        "        >> $TESTFILE 
48echo "it_is_the_Sun_improved_GCC"        >> $TESTFILE
49echo "#else                  "           >> $TESTFILE 
50echo "it_is_the_GNU_compiler"            >> $TESTFILE
51echo "#endif"                            >> $TESTFILE
52echo "#endif"                            >> $TESTFILE
53
54echo "#ifndef __GNUC__"                  >> $TESTFILE 
55echo "#ifdef __SUNPRO_C"                 >> $TESTFILE
56echo "it_is_the_Sun_compiler"            >> $TESTFILE
57echo "#endif"                            >> $TESTFILE
58echo "#endif"                            >> $TESTFILE
59
60# Not found any offical documention to suggest __DECC
61# would be defined, but __DECCC is defined for C++,
62# and a Google on __DECC shows many hits.
63echo "#ifdef __digital__"                >> $TESTFILE
64echo "#ifdef __DECC"                     >> $TESTFILE
65echo "it_is_the_HP_Tru64_compiler"       >> $TESTFILE
66echo "#endif"                            >> $TESTFILE
67echo "#endif"                            >> $TESTFILE
68
69echo "#ifdef __linux__"                  >> $TESTFILE
70echo "#ifdef __DECCC"                   >> $TESTFILE
71echo "it_is_the_HP_Alpha_Linux_compiler" >> $TESTFILE
72echo "#endif"                            >> $TESTFILE
73echo "#endif"                            >> $TESTFILE
74
75echo "#ifdef __HP_cc"                    >> $TESTFILE
76echo "it_is_the_HP_HPUX_compiler"        >> $TESTFILE
77echo "#endif"                            >> $TESTFILE
78
79echo "#ifdef __xlC__"                    >> $TESTFILE
80echo "it_is_the_IBM_AIX_compiler"        >> $TESTFILE
81echo "#endif"                            >> $TESTFILE
82
83${CC} -E $TESTFILE | grep it_is_the_Intel_improved_GCC >/dev/null 2>&1
84if [ $? = 0 ]; then
85   echo Intel_improved_GCC
86   rm $TESTFILE
87   exit 0
88fi
89
90${CC} -E $TESTFILE | grep it_is_the_Sun_improved_GCC >/dev/null 2>&1
91if [ $? = 0 ]; then
92   echo Sun_improved_GCC
93   rm $TESTFILE
94   exit 0
95fi
96
97${CC} -E $TESTFILE | grep it_is_the_GNU_compiler  >/dev/null 2>&1
98if [ $? = 0 ]; then
99   echo GNU
100   rm $TESTFILE
101   exit 0
102fi
103
104${CC} -E $TESTFILE | grep it_is_the_Sun_compiler  >/dev/null 2>&1
105if [ $? = 0 ]; then
106   echo Sun_on_Solaris
107   rm $TESTFILE
108   exit 0
109fi
110
111# HP make compilers for linux, HP-UX and tru64, which complicates matters.
112
113${CC} -E $TESTFILE | grep it_is_the_HP_Tru64_compiler >/dev/null 2>&1
114if [ $? = 0 ]; then
115   echo HP_on_Tru64
116   rm $TESTFILE
117   exit 0
118fi
119
120${CC} -E $TESTFILE | grep it_is_the_HP_Alpha_Linux_compiler >/dev/null 2>&1
121if [ $? = 0 ]; then
122   echo HP_on_Alpha_Linux
123   rm $TESTFILE
124   exit 0
125fi
126
127${CC} -E $TESTFILE | grep it_is_the_HP_HPUX_compiler >/dev/null 2>&1
128if [ $? = 0 ]; then
129   echo HP_on_HPUX
130   rm $TESTFILE
131   exit 0
132fi
133
134${CC} -E $TESTFILE | grep it_is_the_IBM_AIX_compiler >/dev/null 2>&1
135if [ $? = 0 ]; then
136   echo IBM_on_AIX
137   rm $TESTFILE
138   exit 0
139fi
140
141# Exit saying 'Unknown' if the type of the compiler could not be found.
142echo Unknown
143rm $TESTFILE
144exit 0
145
146