|
1 #!/bin/sh |
|
2 # |
|
3 # toolchain detection |
|
4 # |
|
5 |
|
6 C_COMPILERS="cc gcc clang suncc" |
|
7 CPP_COMPILERS="CC g++ clang++ sunCC" |
|
8 unset CC_ARG_CHECKED |
|
9 unset TOOLCHAIN_DETECTION_ERROR |
|
10 unset TOOLCHAIN_NAME |
|
11 |
|
12 check_c_compiler() |
|
13 { |
|
14 cat > $TEMP_DIR/test.c << __EOF__ |
|
15 /* test file */ |
|
16 #include <stdio.h> |
|
17 int main(int argc, char **argv) { |
|
18 #if defined(__clang__) |
|
19 printf("clang\n"); |
|
20 #elif defined(__GNUC__) |
|
21 printf("gcc\n"); |
|
22 #elif defined(__sun) |
|
23 printf("suncc\n"); |
|
24 #else |
|
25 printf("unknown\n"); |
|
26 #endif |
|
27 return 0; |
|
28 } |
|
29 __EOF__ |
|
30 rm -f $TEMP_DIR/checkcc |
|
31 $1 -o $TEMP_DIR/checkcc $CFLAGS $LDFLAGS $TEMP_DIR/test.c 2> /dev/null |
|
32 |
|
33 if [ $? -ne 0 ]; then |
|
34 return 1 |
|
35 fi |
|
36 return 0 |
|
37 } |
|
38 |
|
39 check_cpp_compiler() |
|
40 { |
|
41 cat > $TEMP_DIR/test.cpp << __EOF__ |
|
42 /* test file */ |
|
43 #include <iostream> |
|
44 int main(int argc, char **argv) { |
|
45 #if defined(__clang__) |
|
46 std::cout << "clang" << std::endl; |
|
47 #elif defined(__GNUC__) |
|
48 std::cout << "gcc" << std::endl; |
|
49 #elif defined(__sun) |
|
50 std::cout << "suncc" << std::endl; |
|
51 #else |
|
52 std::cout << "unknown" << std::endl; |
|
53 #endif |
|
54 return 0; |
|
55 } |
|
56 __EOF__ |
|
57 rm -f $TEMP_DIR/checkcc |
|
58 $1 -o $TEMP_DIR/checkcc $CXXFLAGS $LDFLAGS $TEMP_DIR/test.cpp 2> /dev/null |
|
59 |
|
60 if [ $? -ne 0 ]; then |
|
61 return 1 |
|
62 fi |
|
63 return 0 |
|
64 } |
|
65 |
|
66 printf "detect C compiler... " |
|
67 |
|
68 for COMP in $C_COMPILERS |
|
69 do |
|
70 check_c_compiler $COMP |
|
71 if [ $? -ne 0 ]; then |
|
72 if [ ! -z "$CC" ]; then |
|
73 if [ $COMP = $CC ]; then |
|
74 echo "$CC is not a working C Compiler" |
|
75 TOOLCHAIN_DETECTION_ERROR="error" |
|
76 break |
|
77 fi |
|
78 fi |
|
79 else |
|
80 TOOLCHAIN_NAME=`$TEMP_DIR/checkcc` |
|
81 USE_TOOLCHAIN=$TOOLCHAIN_NAME |
|
82 if [ $COMP = "cc" ]; then |
|
83 # we have found a working compiler, but in case |
|
84 # the compiler is gcc or clang, we try to use |
|
85 # these commands and not 'cc' |
|
86 TOOLCHAIN_NAME=`$TEMP_DIR/checkcc` |
|
87 if [ $TOOLCHAIN_NAME = "gcc" ]; then |
|
88 check_c_compiler "gcc" |
|
89 if [ $? -eq 0 ]; then |
|
90 COMP=gcc |
|
91 USE_TOOLCHAIN="gcc" |
|
92 fi |
|
93 fi |
|
94 if [ $TOOLCHAIN_NAME = "clang" ]; then |
|
95 check_c_compiler "clang" |
|
96 if [ $? -eq 0 ]; then |
|
97 COMP=clang |
|
98 USE_TOOLCHAIN="clang" |
|
99 fi |
|
100 fi |
|
101 fi |
|
102 |
|
103 TOOLCHAIN_NAME=$USE_TOOLCHAIN |
|
104 TOOLCHAIN_CC=$COMP |
|
105 echo $COMP |
|
106 break |
|
107 fi |
|
108 done |
|
109 if [ -z $TOOLCHAIN_CC ]; then |
|
110 echo "not found" |
|
111 fi |
|
112 |
|
113 printf "detect C++ compiler... " |
|
114 |
|
115 for COMP in $CPP_COMPILERS |
|
116 do |
|
117 check_cpp_compiler $COMP |
|
118 if [ $? -ne 0 ]; then |
|
119 if [ ! -z "$CXX" ]; then |
|
120 if [ $COMP = $CXX ]; then |
|
121 echo "$CC is not a working C++ Compiler" |
|
122 TOOLCHAIN_DETECTION_ERROR="error" |
|
123 break |
|
124 fi |
|
125 fi |
|
126 else |
|
127 if [ $COMP = "CC" ]; then |
|
128 # we have found a working compiler, but in case |
|
129 # the compiler is gcc or clang, we try to use |
|
130 # these commands and not 'cc' |
|
131 TOOLCHAIN_NAME=`$TEMP_DIR/checkcc` |
|
132 USE_TOOLCHAIN=$TOOLCHAIN_NAME |
|
133 if [ $TOOLCHAIN_NAME = "gcc" ]; then |
|
134 check_cpp_compiler "g++" |
|
135 if [ $? -eq 0 ]; then |
|
136 COMP=g++ |
|
137 USE_TOOLCHAIN="gcc" |
|
138 fi |
|
139 fi |
|
140 if [ $TOOLCHAIN_NAME = "clang" ]; then |
|
141 check_cpp_compiler "clang++" |
|
142 if [ $? -eq 0 ]; then |
|
143 COMP=clang++ |
|
144 USE_TOOLCHAIN="clang" |
|
145 fi |
|
146 fi |
|
147 fi |
|
148 |
|
149 TOOLCHAIN_NAME=$USE_TOOLCHAIN |
|
150 TOOLCHAIN_CXX=$COMP |
|
151 echo $COMP |
|
152 break |
|
153 fi |
|
154 done |
|
155 if [ -z $TOOLCHAIN_CXX ]; then |
|
156 echo "not found" |
|
157 fi |
|
158 |
|
159 TOOLCHAIN_LD=$TOOLCHAIN_CC |
|
160 |
|
161 if [ -z "$TOOLCHAIN_NAME" ]; then |
|
162 TOOLCHAIN_DETECTION_ERROR="error" |
|
163 else |
|
164 cat >> $TEMP_DIR/config.mk << __EOF__ |
|
165 # toolchain |
|
166 __EOF__ |
|
167 echo "CC = ${TOOLCHAIN_CC}" >> $TEMP_DIR/config.mk |
|
168 if [ ! -z "$TOOLCHAIN_CXX" ]; then |
|
169 echo "CXX = ${TOOLCHAIN_CXX}" >> $TEMP_DIR/config.mk |
|
170 fi |
|
171 echo "LD = ${TOOLCHAIN_LD}" >> $TEMP_DIR/config.mk |
|
172 echo >> $TEMP_DIR/config.mk |
|
173 |
|
174 cat "make/${TOOLCHAIN_NAME}.mk" > /dev/null 2>&1 |
|
175 if [ $? -eq 0 ]; then |
|
176 echo "include \$(BUILD_ROOT)/make/${TOOLCHAIN_NAME}.mk" >> $TEMP_DIR/config.mk |
|
177 else |
|
178 echo "SHLIB_CFLAGS = -fPIC" >> $TEMP_DIR/config.mk |
|
179 echo "SHLIB_LDFLAGS = -shared" >> $TEMP_DIR/config.mk |
|
180 fi |
|
181 fi |