test/make/toolchain.sh

Tue, 13 Aug 2019 22:15:36 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 13 Aug 2019 22:15:36 +0200
changeset 13
cdf8bf4549c4
parent 10
135c0599f883
child 15
971ac4f0f1df
permissions
-rw-r--r--

fix compiler flag generation

#!/bin/sh
#
# toolchain detection
#

C_COMPILERS="cc gcc clang suncc"
CPP_COMPILERS="CC g++ clang++ sunCC"
unset CC_ARG_CHECKED
unset TOOLCHAIN_DETECTION_ERROR
unset TOOLCHAIN_NAME

check_c_compiler()
{
    cat > $TEMP_DIR/test.c << __EOF__
/* test file */
#include <stdio.h>
int main(int argc, char **argv) {
#if defined(__GNUC__)
    printf("gcc\n");
#elif defined(__clang__)
    printf("clang\n");
#elif defined(__sun)
    printf("suncc\n");
#else
    printf("unknown\n");
#endif
    return 0;
}
__EOF__
    rm -f $TEMP_DIR/checkcc
    $1 -o $TEMP_DIR/checkcc $CFLAGS $LDFLAGS $TEMP_DIR/test.c 2> /dev/null
    
    if [ $? -ne 0 ]; then
        return 1
    fi
    return 0
}

check_cpp_compiler()
{
    cat > $TEMP_DIR/test.cpp << __EOF__
/* test file */
#include <iostream>
int main(int argc, char **argv) {
#if defined(__GNUC__)
    std::cout << "gcc" << std::endl;
#elif defined(__clang__)
    std::cout << "clang" << std::endl;
#elif defined(__sun)
	std::cout << "suncc" << std::endl;
#else
    std::cout << "unknown" << std::endl;
#endif
    return 0;
}
__EOF__
    rm -f $TEMP_DIR/checkcc
    $1 -o $TEMP_DIR/checkcc $CXXFLAGS $LDFLAGS $TEMP_DIR/test.cpp 2> /dev/null
    
    if [ $? -ne 0 ]; then
        return 1
    fi
    return 0
}

printf "detect C compiler... "

for COMP in $C_COMPILERS
do
    check_c_compiler $COMP
    if [ $? -ne 0 ]; then
	    if [ ! -z "$CC" ]; then
        	if [ $COMP = $CC ]; then
            	echo "$CC is not a working C Compiler"
            	TOOLCHAIN_DETECTION_ERROR="error"
            	break
        	fi
		fi
    else
        TOOLCHAIN_NAME=`$TEMP_DIR/checkcc`
		if [ $COMP = "cc" ]; then
		   # we have found a working compiler, but in case
		   # the compiler is gcc or clang, we try to use
		   # these commands and not 'cc'
		   TOOLCHAIN_NAME=`$TEMP_DIR/checkcc`
		   if [ $TOOLCHAIN_NAME = "gcc" ]; then
		       check_c_compiler "gcc"
			   if [ $? -eq 0 ]; then
			       COMP=gcc
			   fi
		   fi
		   if [ $TOOLCHAIN_NAME = "clang" ]; then
		       check_c_compiler "clang"
			   if [ $? -eq 0 ]; then
			       COMP=clang
			   fi
		   fi
		fi
		
		TOOLCHAIN_NAME=`$TEMP_DIR/checkcc`
        TOOLCHAIN_CC=$COMP
        echo $COMP
        break
    fi
done
if [ -z $TOOLCHAIN_CC ]; then
    echo "not found"
fi

printf "detect C++ compiler... "

for COMP in $CPP_COMPILERS
do
    check_cpp_compiler $COMP
    if [ $? -ne 0 ]; then
		if [ ! -z "$CXX" ]; then
        	if [ $COMP = $CXX ]; then
                echo "$CC is not a working C++ Compiler"
                TOOLCHAIN_DETECTION_ERROR="error"
                break
            fi
		fi
    else
        if [ $COMP = "CC" ]; then
		   # we have found a working compiler, but in case
		   # the compiler is gcc or clang, we try to use
		   # these commands and not 'cc'
		   TOOLCHAIN_NAME=`$TEMP_DIR/checkcc`
		   if [ $TOOLCHAIN_NAME = "gcc" ]; then
		       check_cpp_compiler "g++"
			   if [ $? -eq 0 ]; then
			       COMP=g++
			   fi
		   fi
		   if [ $TOOLCHAIN_NAME = "clang" ]; then
		       check_cpp_compiler "clang++"
			   if [ $? -eq 0 ]; then
			       COMP=clang++
			   fi
		   fi
		fi
		
		TOOLCHAIN_NAME=`$TEMP_DIR/checkcc`
        TOOLCHAIN_CXX=$COMP
        echo $COMP
        break
    fi
done
if [ -z $TOOLCHAIN_CXX ]; then
    echo "not found"
fi

TOOLCHAIN_LD=$TOOLCHAIN_CC

if [ -z "$TOOLCHAIN_NAME" ]; then
    TOOLCHAIN_DETECTION_ERROR="error"
else
    cat >> $TEMP_DIR/config.mk << __EOF__
# toolchain
__EOF__
	echo "CC = ${TOOLCHAIN_CC}" >> $TEMP_DIR/config.mk
	if [ ! -z "$TOOLCHAIN_CXX" ]; then
		echo "CXX = ${TOOLCHAIN_CXX}" >> $TEMP_DIR/config.mk
	fi
    echo "LD = ${TOOLCHAIN_LD}" >> $TEMP_DIR/config.mk
    
	cat "make/${TOOLCHAIN_NAME}.mk" > /dev/null 2>&1
    if [ $? -eq 0 ]; then 
        echo "include \$(BUILD_ROOT)/make/${TOOLCHAIN_NAME}.mk" >> $TEMP_DIR/config.mk
    fi
fi

mercurial