make/toolchain.sh

changeset 32
e5f4d8af567e
parent 0
2483f517c562
equal deleted inserted replaced
31:bf810176ddb8 32:e5f4d8af567e
1 #!/bin/sh 1 #!/bin/sh
2 # 2 #
3 # toolchain detection 3 # toolchain detection
4 # 4 #
5 5
6 C_COMPILERS="cc gcc clang suncc" 6 if isplatform "bsd" && notisplatform "openbsd"; then
7 CPP_COMPILERS="CC g++ clang++ sunCC" 7 C_COMPILERS="clang gcc cc"
8 unset CC_ARG_CHECKED 8 CPP_COMPILERS="clang++ g++ CC"
9 unset TOOLCHAIN_DETECTION_ERROR 9 else
10 C_COMPILERS="gcc clang suncc cc"
11 CPP_COMPILERS="g++ clang++ sunCC CC"
12 fi
13 unset TOOLCHAIN
10 unset TOOLCHAIN_NAME 14 unset TOOLCHAIN_NAME
15 unset TOOLCHAIN_CC
16 unset TOOLCHAIN_CXX
11 17
12 check_c_compiler() 18 check_c_compiler()
13 { 19 {
14 cat > $TEMP_DIR/test.c << __EOF__ 20 cat > "$TEMP_DIR/test.c" << __EOF__
15 /* test file */ 21 /* test file */
16 #include <stdio.h> 22 #include <stdio.h>
17 int main(int argc, char **argv) { 23 int main(int argc, char **argv) {
18 #if defined(__clang__) 24 #if defined(_MSC_VER)
19 printf("clang\n"); 25 printf("msc\n");
26 #elif defined(__clang__)
27 printf("clang gnuc\n");
20 #elif defined(__GNUC__) 28 #elif defined(__GNUC__)
21 printf("gcc\n"); 29 printf("gcc gnuc\n");
22 #elif defined(__sun) 30 #elif defined(__sun)
23 printf("suncc\n"); 31 printf("suncc\n");
24 #else 32 #else
25 printf("unknown\n"); 33 printf("unknown\n");
26 #endif 34 #endif
27 return 0; 35 return 0;
28 } 36 }
29 __EOF__ 37 __EOF__
30 rm -f $TEMP_DIR/checkcc 38 rm -f "$TEMP_DIR/checkcc"
31 $1 -o $TEMP_DIR/checkcc $CFLAGS $LDFLAGS $TEMP_DIR/test.c 2> /dev/null 39 $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 } 40 }
38 41
39 check_cpp_compiler() 42 check_cpp_compiler()
40 { 43 {
41 cat > $TEMP_DIR/test.cpp << __EOF__ 44 cat > "$TEMP_DIR/test.cpp" << __EOF__
42 /* test file */ 45 /* test file */
43 #include <iostream> 46 #include <iostream>
44 int main(int argc, char **argv) { 47 int main(int argc, char **argv) {
45 #if defined(__clang__) 48 #if defined(_MSC_VER)
46 std::cout << "clang" << std::endl; 49 std::cout << "msc" << std::endl;
50 #elif defined(__clang__)
51 std::cout << "clang gnuc" << std::endl;
47 #elif defined(__GNUC__) 52 #elif defined(__GNUC__)
48 std::cout << "gcc" << std::endl; 53 std::cout << "gcc gnuc" << std::endl;
49 #elif defined(__sun) 54 #elif defined(__sun)
50 std::cout << "suncc" << std::endl; 55 std::cout << "suncc" << std::endl;
51 #else 56 #else
52 std::cout << "unknown" << std::endl; 57 std::cout << "cc" << std::endl;
53 #endif 58 #endif
54 return 0; 59 return 0;
55 } 60 }
56 __EOF__ 61 __EOF__
57 rm -f $TEMP_DIR/checkcc 62 rm -f "$TEMP_DIR/checkcc"
58 $1 -o $TEMP_DIR/checkcc $CXXFLAGS $LDFLAGS $TEMP_DIR/test.cpp 2> /dev/null 63 $1 -o "$TEMP_DIR/checkcc" $CXXFLAGS $LDFLAGS "$TEMP_DIR/test.cpp" 2> /dev/null
59 64 }
60 if [ $? -ne 0 ]; then 65
61 return 1 66 create_libtest_source()
62 fi 67 {
63 return 0 68 # $1: filename
64 } 69 # $2: optional include
65 70 cat > "$TEMP_DIR/$1" << __EOF__
66 printf "detect C compiler... " 71 /* libtest file */
67 72 int main(int argc, char **argv) {
68 for COMP in $C_COMPILERS 73 return 0;
69 do 74 }
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__ 75 __EOF__
167 echo "CC = ${TOOLCHAIN_CC}" >> $TEMP_DIR/config.mk 76 if [ -n "$2" ]; then
168 if [ ! -z "$TOOLCHAIN_CXX" ]; then 77 echo "#include <$2>" >> "$TEMP_DIR/$1"
169 echo "CXX = ${TOOLCHAIN_CXX}" >> $TEMP_DIR/config.mk 78 fi
170 fi 79 }
171 echo "LD = ${TOOLCHAIN_LD}" >> $TEMP_DIR/config.mk 80
172 echo >> $TEMP_DIR/config.mk 81 check_c_lib()
173 82 {
174 cat "make/${TOOLCHAIN_NAME}.mk" > /dev/null 2>&1 83 # $1: libname
175 if [ $? -eq 0 ]; then 84 # $2: optional include
176 echo "include \$(BUILD_ROOT)/make/${TOOLCHAIN_NAME}.mk" >> $TEMP_DIR/config.mk 85 if [ -z "$TOOLCHAIN_CC" ]; then
177 else 86 return 1
178 echo "SHLIB_CFLAGS = -fPIC" >> $TEMP_DIR/config.mk 87 fi
179 echo "SHLIB_LDFLAGS = -shared" >> $TEMP_DIR/config.mk 88 create_libtest_source "test.c" "$2"
180 fi 89 rm -f "$TEMP_DIR/checklib"
181 fi 90 $TOOLCHAIN_CC -o "$TEMP_DIR/checklib" $CFLAGS $LDFLAGS "-l$1" "$TEMP_DIR/test.c" 2> /dev/null
91 }
92
93 check_cpp_lib()
94 {
95 # $1: libname
96 # $2: optional include
97 if [ -z "$TOOLCHAIN_CXX" ]; then
98 return 1
99 fi
100 create_libtest_source "test.cpp" "$2"
101 rm -f "$TEMP_DIR/checklib"
102 $TOOLCHAIN_CXX -o "$TEMP_DIR/checklib" $CXXFLAGS $LDFLAGS "-l$1" "$TEMP_DIR/test.cpp" 2> /dev/null
103 }
104
105 check_lib()
106 {
107 # $1: libname
108 # $2: optional include
109 if [ -n "$TOOLCHAIN_CC" ]; then
110 check_c_lib "$1" "$2"
111 elif [ -n "$TOOLCHAIN_CXX" ]; then
112 check_cpp_lib "$1" "$2"
113 fi
114 }
115
116 detect_c_compiler()
117 {
118 if [ -n "$TOOLCHAIN_CC" ]; then
119 return 0
120 fi
121 printf "detect C compiler... "
122 if [ -n "$CC" ]; then
123 if check_c_compiler "$CC"; then
124 TOOLCHAIN_CC=$CC
125 TOOLCHAIN=`"$TEMP_DIR/checkcc"`
126 TOOLCHAIN_NAME=`echo "$TOOLCHAIN" | cut -f1 -d' ' -`
127 echo "$CC"
128 return 0
129 else
130 echo "$CC is not a working C compiler"
131 return 1
132 fi
133 else
134 for COMP in $C_COMPILERS
135 do
136 if check_c_compiler "$COMP"; then
137 TOOLCHAIN_CC=$COMP
138 TOOLCHAIN=`"$TEMP_DIR/checkcc"`
139 TOOLCHAIN_NAME=`echo "$TOOLCHAIN" | cut -f1 -d' ' -`
140 echo "$COMP"
141 return 0
142 fi
143 done
144 echo "not found"
145 return 1
146 fi
147 }
148
149 detect_cpp_compiler()
150 {
151 if [ -n "$TOOLCHAIN_CXX" ]; then
152 return 0
153 fi
154 printf "detect C++ compiler... "
155
156 if [ -n "$CXX" ]; then
157 if check_cpp_compiler "$CXX"; then
158 TOOLCHAIN_CXX=$CXX
159 TOOLCHAIN=`"$TEMP_DIR/checkcc"`
160 TOOLCHAIN_NAME=`echo "$TOOLCHAIN" | cut -f1 -d' ' -`
161 echo "$CXX"
162 return 0
163 else
164 echo "$CXX is not a working C++ compiler"
165 return 1
166 fi
167 else
168 for COMP in $CPP_COMPILERS
169 do
170 if check_cpp_compiler "$COMP"; then
171 TOOLCHAIN_CXX=$COMP
172 TOOLCHAIN=`"$TEMP_DIR/checkcc"`
173 TOOLCHAIN_NAME=`echo "$TOOLCHAIN" | cut -f1 -d' ' -`
174 echo "$COMP"
175 return 0
176 fi
177 done
178 echo "${TOOLCHAIN_CXX:-"not found"}"
179 return 1
180 fi
181 }
182
183 write_toolchain_defaults()
184 {
185 echo "# toolchain" >> "$1"
186 if [ -n "$TOOLCHAIN_CC" ]; then
187 echo "CC = ${TOOLCHAIN_CC}" >> "$1"
188 fi
189 if [ -n "$TOOLCHAIN_CXX" ]; then
190 echo "CXX = ${TOOLCHAIN_CXX}" >> "$1"
191 fi
192 echo >> "$1"
193 if [ -f "make/${TOOLCHAIN_NAME}.mk" ]; then
194 cat "make/${TOOLCHAIN_NAME}.mk" >> "$1"
195 elif [ -f "make/cc.mk" ]; then
196 cat "make/cc.mk" >> "$1"
197 else
198 echo "!!! WARNING !!! Default toolchain flags not found. Configuration might be incomplete."
199 fi
200 }

mercurial