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 the definition of a given macro against multiple values
     76 # $1: macro name
     77 # $2+: list of acceptable values.
     78 macro_multi_check () {
     79     echo -n "Checking $1: "
     80     local VAL=$(macro_val $1)
     81     if [ -z "$VAL" ]; then
     82       echo "Missing built-in macro definition: $1"
     83       return 1
     84     fi
     85     local VAL2 FOUND
     86     shift
     87     for VAL2 in "$@"; do
     88       if [ "$VAL2" = "$VAL" ]; then
     89         FOUND=true
     90         break
     91       fi
     92     done
     93     if [ -z "$FOUND" ]; then
     94       echo "Invalid built-in macro definition: '$VAL', expected one of: $@"
     95       return 1
     96     fi
     97     return 0
     98 }
     99 
    100 # Check that a given macro is undefined
    101 macro_check_undef () {
    102     if [ -n "$2" ]; then
    103         echo -n "Checking undefined $1 ($2): "
    104     else
    105         echo -n "Checking undefined $1: "
    106     fi
    107     local VAL="$(macro_val $1)"
    108     if [ -n "$VAL" ]; then
    109         echo "KO: Unexpected value '$VAL' encounteded"
    110         FAILURES=$(( $FAILURES + 1 ))
    111     else
    112         echo "ok"
    113     fi
    114     COUNT=$(( $COUNT + 1 ))
    115 }
    116 
    117 echo "Checking built-in macros for: $CC $CFLAGS"
    118 
    119 # All toolchains must define the following prebuilt macros.
    120 macro_check __ANDROID__ 1   "Android target system"
    121 macro_check __linux__ 1     "Linux target system"
    122 macro_check __unix__ 1      "Unix target system"
    123 macro_check __ELF__ 1       "ELF target system"
    124 
    125 # Either __pic__ or __PIC__ must be defined. Defining both is ok, not
    126 # having anyone of them defined is an error.
    127 #
    128 # The value should be 1 on all platforms, except x86 where it will be 2
    129 # (No idea why).
    130 case $ABI in
    131     x86) PICVAL=2;;
    132     *) PICVAL=1;;
    133 esac
    134 
    135 case $ABI in
    136     armeabi|armeabi-v7a)
    137         macro_check __arm__ 1              "ARM CPU architecture"
    138         macro_check __ARM_EABI__ 1         "ARM EABI runtime"
    139         macro_check __ARMEL__ 1            "ARM little-endian"
    140         macro_check __THUMB_INTERWORK__ 1  "ARM thumb-interwork"
    141         macro_check __PIC__ 1              "Position independent code (-fpic)"
    142         macro_check __WCHAR_TYPE__         "unsigned"
    143         macro_check __WCHAR_MAX__          "4294967295U"
    144         # Clang doesn't define __WCHAR_MIN__ so don't check it"
    145 
    146         case $ABI in
    147             armeabi)
    148                 macro_check __ARM_ARCH_5TE__ 1   "ARMv5TE instructions (for armeabi)"
    149                 macro_check __SOFTFP__ 1         "ARM Soft-floating point"
    150                 ;;
    151             armeabi-v7a)
    152                 macro_check __ARM_ARCH_7A__ 1    "ARMv7-A instructions (for armeabi-v7a)"
    153 
    154                 # This macro seems to be ill-named. It is only defined when we
    155                 # don't use -mfloat-abi=softfp or -mfloat-abi=hard. I can only
    156                 # assume it corresponds to -mfloat-abi=soft, which corresponds
    157                 # to all FP operations implemented (slowly) through software.
    158                 #
    159                 # Not to be confused with -mfloat-abi=softfp which indicates
    160                 # that the FPU is used for all FP operations, but that FP
    161                 # values are passsed in core registers between function calls,
    162                 # which is mandated by the armeabi-v7a definition.
    163                 #
    164                 macro_check_undef __SOFTFP__ 1   "ARM soft-floating point"
    165                 ;;
    166         esac
    167         ;;
    168 
    169     x86)
    170         macro_check __i386__ 1       "x86 CPU architecture"
    171         macro_check __i686__ 1       "i686 instruction set"
    172         macro_check __PIC__ 2        "Position independent code (-fPIC)"
    173         macro_check __MMX__  1       "MMX instruction set"
    174         macro_check __SSE__ 1        "SSE instruction set"
    175         macro_check __SSE2__ 1       "SSE2 instruction set"
    176         macro_check __SSE3__ 1       "SSE3 instruction set"
    177         macro_check __SSE_MATH__ 1   "Use SSE for math operations"
    178         macro_check __SSE2_MATH__ 1  "Use SSE2 for math operations"
    179         # GCC defines is as 'long', and Clang as 'int'
    180         macro_multi_check __WCHAR_TYPE__   "long" "int"
    181         # GCC defines it with an L suffix, Clang doesn't.
    182         macro_multi_check __WCHAR_MAX__    "2147483647L" "2147483647"
    183         ;;
    184 
    185     mips)
    186         macro_check __mips__ 1          "Mips CPU architecture"
    187         macro_check _MIPS_ARCH_MIPS32 1 "Mips 32-bit ABI"
    188         macro_check __MIPSEL__ 1        "Mips little-endian"
    189         macro_check __PIC__ 1           "Position independent code (-fpic)"
    190         # GCC defines it as "signed int", and Clang as "int"
    191         macro_multi_check __WCHAR_TYPE__   "signed int" "int"
    192         macro_check __WCHAR_MAX__       "2147483647"
    193         ;;
    194     *)
    195         echo "Unknown ABI: $ABI"
    196         exit 1
    197 esac
    198 
    199 macro_check "__SIZEOF_SHORT__"       "2"   "short is 16-bit"
    200 macro_check "__SIZEOF_INT__"         "4"   "int is 32-bit"
    201 macro_check "__SIZEOF_FLOAT__"       "4"   "float is 32-bit"
    202 macro_check "__SIZEOF_DOUBLE__"      "8"   "double is 64-bit"
    203 macro_check "__SIZEOF_LONG_DOUBLE__" "8"   "long double is 64-bit"
    204 macro_check "__SIZEOF_LONG_LONG__"   "8"   "long long is 64-bit"
    205 macro_check "__SIZEOF_POINTER__"     "4"   "pointers are 32-bit"
    206 macro_check "__SIZEOF_WCHAR_T__"     "4"   "wchar_t is 32-bit"
    207 
    208 if [ "$FAILURES" = 0 ]; then
    209     echo "$COUNT/$COUNT tests passed. Nice job."
    210     exit 0
    211 fi
    212 
    213 echo "$FAILURES/$COUNT tests failed !!"
    214 exit 1
    215