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