• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5macro_assign () {
6    local _VARNAME=$1
7    local _VARVALUE="$2"
8    eval macro_$_VARNAME=\"$_VARVALUE\"
9}
10
11macro_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.
18MACRO_LINES=$($CC $CFLAGS -dM -E - < /dev/null | sort -u | tr ' ' '^^^' | tr '"' '~')
19
20for 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
33done
34
35# Now perform some checks
36
37FAILURES=0
38COUNT=0
39
40# $1: variable name
41# $2: expected value
42macro_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
60macro_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.
78macro_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
101macro_check_undef () {
102    echo -n "Checking undefined $1: "
103    local VAL="$(macro_val $1)"
104    if [ -n "$VAL" ]; then
105        echo "KO: Unexpected value '$VAL' encounteded"
106        FAILURES=$(( $FAILURES + 1 ))
107    else
108        echo "ok"
109    fi
110    COUNT=$(( $COUNT + 1 ))
111}
112
113echo "Checking built-in macros for: $CC $CFLAGS"
114
115# All toolchains must define the following prebuilt macros.
116macro_check __ANDROID__ 1   "Android target system"
117macro_check __linux__ 1     "Linux target system"
118macro_check __unix__ 1      "Unix target system"
119macro_check __ELF__ 1       "ELF target system"
120
121# Either __pic__ or __PIC__ must be defined. Defining both is ok, not
122# having anyone of them defined is an error.
123#
124# The value should be 1 on all platforms, except x86 where it will be 2
125# (No idea why).
126case $ABI in
127    x86) PICVAL=2;;
128    *) PICVAL=1;;
129esac
130
131case $ABI in
132    armeabi|armeabi-v7a|armeabi-v7a-hard)
133        macro_check __arm__ 1              "ARM CPU architecture"
134	macro_check_undef __LP64__         "LP64 data model"
135        macro_check __ARM_EABI__ 1         "ARM EABI runtime"
136        macro_check __ARMEL__ 1            "ARM little-endian"
137        macro_check __THUMB_INTERWORK__ 1  "ARM thumb-interwork"
138        macro_check __PIC__ 1              "Position independent code (-fpic)"
139        macro_check __WCHAR_TYPE__         "unsigned"
140        macro_check __WCHAR_MAX__          "4294967295U"
141        # Clang doesn't define __WCHAR_MIN__ so don't check it"
142
143        case $ABI in
144            armeabi)
145                macro_check __ARM_ARCH_5TE__ 1   "ARMv5TE instructions (for armeabi)"
146                macro_check __SOFTFP__ 1         "ARM soft-floating point"
147                ;;
148            armeabi-v7a)
149                macro_check __ARM_ARCH_7A__ 1    "ARMv7-A instructions (for armeabi-v7a)"
150
151                # This macro seems to be ill-named. It is only defined when we
152                # don't use -mfloat-abi=softfp or -mfloat-abi=hard. I can only
153                # assume it corresponds to -mfloat-abi=soft, which corresponds
154                # to all FP operations implemented (slowly) through software.
155                #
156                # Not to be confused with -mfloat-abi=softfp which indicates
157                # that the FPU is used for all FP operations, but that FP
158                # values are passsed in core registers between function calls,
159                # which is mandated by the armeabi-v7a definition.
160                #
161                macro_check_undef __SOFTFP__      "ARM soft-floating point"
162                ;;
163            armeabi-v7a-hard)
164                macro_check __ARM_ARCH_7A__ 1     "ARMv7-A instructions (for armeabi-v7a)"
165                macro_check __ARM_PCS_VFP__ 1     "ARM hard-floating point"
166                macro_check_undef __SOFTFP__      "ARM soft-floating point"
167                ;;
168        esac
169        ;;
170
171    x86)
172        macro_check __i386__ 1       "x86 CPU architecture"
173        macro_check_undef __LP64__   "LP64 data model"
174        macro_check __i686__ 1       "i686 instruction set"
175        macro_check __PIC__ 2        "Position independent code (-fPIC)"
176        macro_check __MMX__  1       "MMX instruction set"
177        macro_check __SSE__ 1        "SSE instruction set"
178        macro_check __SSE2__ 1       "SSE2 instruction set"
179        macro_check __SSE3__ 1       "SSE3 instruction set"
180        macro_check __SSE_MATH__ 1   "Use SSE for math operations"
181        macro_check __SSE2_MATH__ 1  "Use SSE2 for math operations"
182        # GCC defines is as 'long', and Clang as 'int'
183        macro_multi_check __WCHAR_TYPE__   "long" "int"
184        # GCC defines it with an L suffix, Clang doesn't.
185        macro_multi_check __WCHAR_MAX__    "2147483647L" "2147483647"
186        ;;
187
188    mips)
189        macro_check __mips__ 1          "Mips CPU architecture"
190        macro_check_undef __LP64__      "LP64 data model"
191        macro_check _MIPS_ARCH_MIPS32 1 "Mips 32-bit ABI"
192        macro_check __MIPSEL__ 1        "Mips little-endian"
193        macro_check __PIC__ 1           "Position independent code (-fpic)"
194        # GCC defines it as "signed int", and Clang as "int"
195        macro_multi_check __WCHAR_TYPE__   "signed int" "int"
196        macro_check __WCHAR_MAX__       "2147483647"
197        ;;
198    arm64-v8a)
199        macro_check __aarch64__ 1          "ARM CPU architecture"
200        macro_check __LP64__ 1             "LP64 data model"
201        macro_check __AARCH64EL__ 1        "ARM AARCH64 little-endian runtime"
202        macro_check __PIC__ 1              "Position independent code (-fpic)"
203        macro_check __WCHAR_TYPE__         "unsigned"
204        macro_check __WCHAR_MAX__          "4294967295U"
205        # Clang doesn't define __WCHAR_MIN__ so don't check it"
206        ;;
207
208    x86_64)
209        macro_check __x86_64__ 1     "x86_64 CPU architecture"
210        macro_check __LP64__ 1       "LP64 data model"
211        macro_check __PIC__ 2        "Position independent code (-fPIC)"
212        macro_check __MMX__  1       "MMX instruction set"
213        macro_check __SSE__ 1        "SSE instruction set"
214        macro_check __SSE2__ 1       "SSE2 instruction set"
215        macro_check __SSE3__ 1       "SSE3 instruction set"
216        macro_check __SSE_MATH__ 1   "Use SSE for math operations"
217        macro_check __SSE2_MATH__ 1  "Use SSE2 for math operations"
218        #macro_check __SSSE3__ 1     "SSSE3 instruction set"
219        macro_check __WCHAR_TYPE__   "int"
220        macro_check __WCHAR_MAX__    "2147483647"
221        ;;
222
223    mips64)
224        macro_check __mips__ 1            "Mips CPU architecture"
225        macro_check __mips64 1            "Mips 64-bit CPU architecture"
226        macro_check __LP64__ 1            "LP64 data model"
227        macro_check __MIPSEL__ 1          "Mips little-endian"
228        macro_check __PIC__ 1             "Position independent code (-fpic)"
229        macro_check __WCHAR_TYPE__        "int"
230        macro_check __WCHAR_MAX__         "2147483647"
231        ;;
232    *)
233        echo "Unknown ABI: $ABI"
234        exit 1
235esac
236
237macro_check "__SIZEOF_SHORT__"       "2"   "short is 16-bit"
238macro_check "__SIZEOF_INT__"         "4"   "int is 32-bit"
239macro_check "__SIZEOF_FLOAT__"       "4"   "float is 32-bit"
240macro_check "__SIZEOF_DOUBLE__"      "8"   "double is 64-bit"
241if [ "$ABI" = "${ABI%%64*}" ]; then
242    macro_check "__SIZEOF_LONG_DOUBLE__" "8"   "long double is 64-bit"
243    macro_check "__SIZEOF_POINTER__"     "4"   "pointers are 32-bit"
244else
245    macro_check "__SIZEOF_LONG_DOUBLE__" "16"   "long double is 128-bit"
246    macro_check "__SIZEOF_POINTER__"     "8"   "pointers are 64-bit"
247fi
248macro_check "__SIZEOF_LONG_LONG__"   "8"   "long long is 64-bit"
249macro_check "__SIZEOF_WCHAR_T__"     "4"   "wchar_t is 32-bit"
250
251if [ "$FAILURES" = 0 ]; then
252    echo "$COUNT/$COUNT tests passed. Nice job."
253    exit 0
254fi
255
256echo "$FAILURES/$COUNT tests failed !!"
257exit 1
258