Home | History | Annotate | Download | only in builtin-macros
      1 # This script is used to check all pre-defined built-in macros in a given
      2 # standalone toolchain. Call from tests/standalone/run.sh only.
      3 #
      4 
      5 macro_assign () {
      6     local _VARNAME=$1
      7     local _VARVALUE="$2"
      8     eval macro_$_VARNAME=\"$_VARVALUE\"
      9 }
     10 
     11 macro_val () {
     12     eval echo -n \"\$macro_$1\"
     13 }
     14 
     15 # Read all the built-in macros, and assign them to our own variables.
     16 # For cygwin/mingw, don't use $NULL defined in parent run.sh to NUL, because
     17 # NUL can't be used as input.  The non-existance /dev/null works well.
     18 MACRO_LINES=$($CC $CFLAGS -dM -E - < /dev/null | sort -u | tr ' ' '^^^' | tr '"' '~')
     19 
     20 for LINE in $MACRO_LINES; do
     21     # for cygwin, it's important to remove trailing '\r' as well
     22     LINE=$(echo "$LINE" | tr '^^^' ' ' | tr '\r' ' ')
     23     VARNAME=$(echo "$LINE" | cut -d' ' -f 2)
     24     VARVALUE=$(echo "$LINE" | cut -d' ' -f 3)
     25 
     26     # Avoid macro names that contain parentheses.
     27     echo "$VARNAME" | grep -q -v -e '('
     28     if [ $? != 0 ]; then
     29         continue
     30     fi
     31 
     32     macro_assign $VARNAME $VARVALUE
     33 done
     34 
     35 # Now perform some checks
     36 
     37 FAILURES=0
     38 COUNT=0
     39 
     40 # $1: variable name
     41 # $2: expected value
     42 macro_expect () {
     43 
     44     local VAL=$(macro_val $1)
     45     if [ -z "$VAL" ]; then
     46         echo "Missing built-in macro definition: $1"
     47         return 1
     48     fi
     49     if [ "$VAL" != "$2" ]; then
     50         echo "Invalid built-in macro definition: '$VAL', expected '$2'"
     51         return 1
     52     fi
     53     return 0
     54 }
     55 
     56 # Check the definition of a given macro
     57 # $1: macro name
     58 # $2: expected value
     59 # $3: textual description for the check
     60 macro_check () {
     61     if [ -n "$3" ]; then
     62         echo -n "Checking $1 ($3): "
     63     else
     64         echo -n "Checking $1: "
     65     fi
     66     macro_expect "$1" "$2"
     67     if [ $? != 0 ]; then
     68         FAILURES=$(( $FAILURES + 1 ))
     69     else
     70         echo "ok"
     71     fi
     72     COUNT=$(( $COUNT + 1 ))
     73 }
     74 
     75 # Check that a given macro is undefined
     76 macro_check_undef () {
     77     if [ -n "$2" ]; then
     78         echo -n "Checking undefined $1 ($2): "
     79     else
     80         echo -n "Checking undefined $1: "
     81     fi
     82     local VAL="$(macro_val $1)"
     83     if [ -n "$VAL" ]; then
     84         echo "KO: Unexpected value '$VAL' encounteded"
     85         FAILURES=$(( $FAILURES + 1 ))
     86     else
     87         echo "ok"
     88     fi
     89     COUNT=$(( $COUNT + 1 ))
     90 }
     91 
     92 echo "Checking built-in macros for: $CC $CFLAGS"
     93 
     94 # All toolchains must define the following prebuilt macros.
     95 macro_check __ANDROID__ 1   "Android target system"
     96 macro_check __linux__ 1     "Linux target system"
     97 macro_check __unix__ 1      "Unix target system"
     98 macro_check __ELF__ 1       "ELF target system"
     99 
    100 # Either __pic__ or __PIC__ must be defined. Defining both is ok, not
    101 # having anyone of them defined is an error.
    102 #
    103 # The value should be 1 on all platforms, except x86 where it will be 2
    104 # (No idea why).
    105 case $ABI in
    106     x86) PICVAL=2;;
    107     *) PICVAL=1;;
    108 esac
    109 
    110 case $ABI in
    111     armeabi|armeabi-v7a)
    112         macro_check __arm__ 1              "ARM CPU architecture"
    113         macro_check __ARM_EABI__ 1         "ARM EABI runtime"
    114         macro_check __ARMEL__ 1            "ARM little-endian"
    115         macro_check __THUMB_INTERWORK__ 1  "ARM thumb-interwork"
    116         macro_check __PIC__ 1              "Position independent code (-fpic)"
    117 
    118         case $ABI in
    119             armeabi)
    120                 macro_check __ARM_ARCH_5TE__ 1   "ARMv5TE instructions (for armeabi)"
    121                 macro_check __SOFTFP__ 1         "ARM Soft-floating point"
    122                 ;;
    123             armeabi-v7a)
    124                 macro_check __ARM_ARCH_7A__ 1    "ARMv7-A instructions (for armeabi-v7a)"
    125 
    126                 # This macro seems to be ill-named. It is only defined when we
    127                 # don't use -mfloat-abi=softfp or -mfloat-abi=hard. I can only
    128                 # assume it corresponds to -mfloat-abi=soft, which corresponds
    129                 # to all FP operations implemented (slowly) through software.
    130                 #
    131                 # Not to be confused with -mfloat-abi=softfp which indicates
    132                 # that the FPU is used for all FP operations, but that FP
    133                 # values are passsed in core registers between function calls,
    134                 # which is mandated by the armeabi-v7a definition.
    135                 #
    136                 macro_check_undef __SOFTFP__ 1   "ARM soft-floating point"
    137                 ;;
    138         esac
    139         ;;
    140 
    141     x86)
    142         macro_check __i386__ 1       "x86 CPU architecture"
    143         macro_check __i686__ 1       "i686 instruction set"
    144         macro_check __PIC__ 2        "Position independent code (-fPIC)"
    145         macro_check __MMX__  1       "MMX instruction set"
    146         macro_check __SSE__ 1        "SSE instruction set"
    147         macro_check __SSE2__ 1       "SSE2 instruction set"
    148         macro_check __SSE3__ 1       "SSE3 instruction set"
    149         macro_check __SSE_MATH__ 1   "Use SSE for math operations"
    150         macro_check __SSE2_MATH__ 1  "Use SSE2 for math operations"
    151         ;;
    152 
    153     mips)
    154         macro_check __mips__ 1          "Mips CPU architecture"
    155         macro_check _MIPS_ARCH_MIPS32 1 "Mips 32-bit ABI"
    156         macro_check __MIPSEL__ 1        "Mips little-endian"
    157         macro_check __PIC__ 1           "Position independent code (-fpic)"
    158         ;;
    159     *)
    160         echo "Unknown ABI: $ABI"
    161         exit 1
    162 esac
    163 
    164 macro_check "__SIZEOF_SHORT__"       "2"   "short is 16-bit"
    165 macro_check "__SIZEOF_INT__"         "4"   "int is 32-bit"
    166 macro_check "__SIZEOF_FLOAT__"       "4"   "float is 32-bit"
    167 macro_check "__SIZEOF_DOUBLE__"      "8"   "double is 64-bit"
    168 macro_check "__SIZEOF_LONG_DOUBLE__" "8"   "long double is 64-bit"
    169 macro_check "__SIZEOF_LONG_LONG__"   "8"   "long long is 64-bit"
    170 macro_check "__SIZEOF_POINTER__"     "4"   "pointers are 32-bit"
    171 macro_check "__SIZEOF_WCHAR_T__"     "4"   "wchar_t is 32-bit"
    172 
    173 if [ "$FAILURES" = 0 ]; then
    174     echo "$COUNT/$COUNT tests passed. Nice job."
    175     exit 0
    176 fi
    177 
    178 echo "$FAILURES/$COUNT tests failed !!"
    179 exit 1
    180