-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_check
executable file
·415 lines (358 loc) · 10.8 KB
/
c_check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/bin/sh
# Checking cross compile
hostos=`uname -s | sed -e 's/\-.*//'`
hostarch=`uname -m | sed -e 's/i.86/x86/'`
if [ "$hostos" = "AIX" ] || [ "$hostos" = "SunOS" ]; then
hostarch=`uname -p`
fi
case "$hostarch" in
amd64) hostarch=x86_64 ;;
arm*) [ "$hostarch" = "arm64" ] || hostarch='arm' ;;
aarch64) hostarch=arm64 ;;
powerpc*|ppc*) hostarch=power ;;
s390x) hostarch=zarch ;;
esac
makefile="$1"
config="$2"
compiler_name="$3"
shift 3
flags="$*"
# First, we need to know the target OS and compiler name
{
data=`$compiler_name $flags -E ctest.c`
} || {
printf '%s\n' "C Compiler ($compiler_name) is something wrong." >&2
exit 1
}
cross_suffix=""
if [ "`dirname \"$compiler_name\"`" != '.' ]; then
cross_suffix="$cross_suffix`dirname \"$compiler_name\"`/"
fi
bn=`basename $compiler_name`
case "$bn" in
*-*) cross_suffix="$cross_suffix${bn%-*}-"
esac
compiler=""
case "$data" in
*COMPILER_LSB*) compiler=LSB ;;
*COMPILER_CLANG*) compiler=CLANG ;;
*COMPILER_PGI*) compiler=PGI ;;
*COMPILER_PATHSCALE*) compiler=PATHSCALE ;;
*COMPILER_INTEL*) compiler=INTEL ;;
*COMPILER_OPEN64*) compiler=OPEN64 ;;
*COMPILER_SUN*) compiler=SUN ;;
*COMPILER_IBM*) compiler=IBM ;;
*COMPILER_DEC*) compiler=DEC ;;
*COMPILER_FUJITSU*) compiler=FUJITSU ;;
esac
if [ -z "$compiler" ]; then
compiler=GCC
fi
case "$data" in *OS_LINUX*) os=Linux ;; esac
case "$data" in *OS_FREEBSD*) os=FreeBSD ;; esac
case "$data" in *OS_NETBSD*) os=NetBSD ;; esac
case "$data" in *OS_OPENBSD*) os=OpenBSD ;; esac
case "$data" in *OS_DRAGONFLY*) os=DragonFly ;; esac
case "$data" in *OS_DARWIN*) os=Darwin ;; esac
case "$data" in *OS_SUNOS*) os=SunOS ;; esac
case "$data" in *OS_AIX*) os=AIX ;; esac
case "$data" in *OS_OSF*) os=osf ;; esac
case "$data" in *OS_WINNT*) os=WINNT ;; esac
case "$data" in *OS_CYGWIN_NT*) os=CYGWIN_NT ;; esac
case "$data" in *OS_INTERIX*) os=Interix ;; esac
case "$data" in *OS_ANDROID*) os=Android ;; esac
case "$data" in *OS_HAIKU*) os=Haiku ;; esac
case "$data" in
*ARCH_X86_64*) architecture=x86_64 ;;
*ARCH_X86*) architecture=x86 ;;
*ARCH_E2K*) architecture=e2k ;;
*ARCH_POWER*) architecture=power ;;
*ARCH_MIPS64*) architecture=mips64 ;;
*ARCH_MIPS*) architecture=mips ;;
*ARCH_ALPHA*) architecture=alpha ;;
*ARCH_SPARC*) architecture=sparc ;;
*ARCH_IA64*) architecture=ia64 ;;
*ARCH_ARM64*) architecture=arm64 ;;
*ARCH_ARM*) architecture=arm ;;
*ARCH_ZARCH*) architecture=zarch ;;
*ARCH_RISCV64*) architecture=riscv64 ;;
*ARCH_LOONGARCH64*) architecture=loongarch64 ;;
esac
defined=0
if [ "$os" = "AIX" ]; then
case "$BINARY" in
32) compiler_name="$compiler_name -maix32" ;;
64) compiler_name="$compiler_name -maix64" ;;
esac
defined=1
fi
case "$architecture" in
mips)
compiler_name="$compiler_name -mabi=32"
defined=1
;;
mips64)
case "$BINARY" in
32) compiler_name="$compiler_name -mabi=n32" ;;
64) compiler_name="$compiler_name -mabi=64" ;;
esac
defined=1
;;
arm|arm64) defined=1 ;;
zarch|e2k|alpha|ia64|riscv64|loonarch64)
defined=1
BINARY=64
;;
x86)
[ "$os" != "Darwin" ] && [ "$os" != "SunOS" ] && {
defined=1
BINARY=32
}
;;
esac
case "$compiler" in
PGI)
case "$BINARY" in
32) compiler_name="$compiler_name -tp p7" ;;
64) compiler_name="$compiler_name -tp p7-64" ;;
esac
openmp='-mp'
defined=1
;;
IBM)
case "$BINARY" in
32) compiler_name="$compiler_name -q32" ;;
64) compiler_name="$compiler_name -q64" ;;
esac
openmp='-qsmp=omp'
defined=1
;;
INTEL) openmp='-openmp' ;;
PATHSCALE|OPEN64) openmp='-mp' ;;
CLANG|GCC|LSB) openmp='-fopenmp' ;;
FUJITSU) openmp='-Kopenmp' ;;
esac
if [ "$defined" -eq 0 ]; then
case "$BINARY" in
32) compiler_name="$compiler_name -m32" ;;
64) compiler_name="$compiler_name -m64" ;;
esac
fi
# Do again
{
data="$($compiler_name $flags -E ctest.c)"
} || {
printf '%s\n' "C Compiler ($compiler_name) is something wrong." >&2
exit 1
}
have_msa=0
if [ "$architecture" = "mips" ] || [ "$architecture" = "mips64" ]; then
tmpd="$(mktemp -d)"
tmpf="$tmpd/a.c"
code='"addvi.b $w0, $w1, 1"'
msa_flags='-mmsa -mfp64 -mload-store-pairs'
printf "#include <msa.h>\n\n" >> "$tmpf"
printf "void main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
args="$msa_flags -o $tmpf.o $tmpf"
have_msa=1
{
$compiler_name $flags $args >/dev/null 2>&1
} || {
have_msa=0
}
rm -rf "$tmpd"
fi
case "$data" in
*ARCH_X86_64*) architecture=x86_64 ;;
*ARCH_X86*) architecture=x86 ;;
*ARCH_E2K*) architecture=e2k ;;
*ARCH_POWER*) architecture=power ;;
*ARCH_MIPS64*) architecture=mips64 ;;
*ARCH_MIPS*) architecture=mips ;;
*ARCH_ALPHA*) architecture=alpha ;;
*ARCH_SPARC*) architecture=sparc ;;
*ARCH_IA64*) architecture=ia64 ;;
*ARCH_ARM64*) architecture=arm64 ;;
*ARCH_ARM*) architecture=arm ;;
*ARCH_ZARCH*) architecture=zarch ;;
*ARCH_LOONGARCH64*) architecture=loongarch64 ;;
esac
binformat='bin32'
case "$data" in
*BINARY_64*) binformat='bin64' ;;
esac
no_avx512=0
if [ "$architecture" = "x86" ] || [ "$architecture" = "x86_64" ]; then
tmpd=`mktemp -d`
tmpf="$tmpd/a.c"
code='"vbroadcastss -4 * 4(%rsi), %zmm2"'
printf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
if [ "$compiler" = "PGI" ]; then
args=" -tp skylake -c -o $tmpf.o $tmpf"
else
args=" -march=skylake-avx512 -c -o $tmpf.o $tmpf"
fi
no_avx512=0
{
$compiler_name $flags $args >/dev/null 2>&1
} || {
no_avx512=1
}
rm -rf "$tmpd"
fi
no_rv64gv=0
if [ "$architecture" = "riscv64" ]; then
tmpd=`mktemp -d`
tmpf="$tmpd/a.c"
code='"vsetvli zero, zero, e8, m1\n"'
printf "int main(void){ __asm__ volatile(%s); }\n" "$code" >> "$tmpf"
args=" -march=rv64gv -c -o $tmpf.o $tmpf"
no_rv64gv=0
{
$compiler_name $flags $args >/dev/null 2>&1
} || {
no_rv64gv=1
}
rm -rf "$tmpd"
fi
c11_atomics=0
case "$data" in
*HAVE_C11*)
tmpd=`mktemp -d`
tmpf="$tmpd/a.c"
printf "#include <stdatomic.h>\nint main(void){}\n" >> "$tmpf"
args=" -c -o $tmpf.o $tmpf"
c11_atomics=1
{
$compiler_name $flags $args >/dev/null 2>&1
} || {
c11_atomics=0
}
rm -rf "$tmpd"
;;
esac
oldgcc=0
no_avx2=0
if [ "$compiler" = "GCC" ]; then
case "$architecture" in x86|x86_64)
no_avx2=0
oldgcc=0
data=`$compiler_name -dumpversion`
case "$data" in *.*.*)
data="${data%.*}"
esac
if awk -v n1=$data -v n2=4.6 'BEGIN { exit !(n1 <= n2) }'; then
no_avx2=1
oldgcc=1
fi
esac
fi
data=`$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`
need_fu=''
if echo "$data" | grep 'globl[[:space:]][_\.]'; then
need_fu="${data##*globl[[:space:]]}"
need_fu="${need_fu%%[!_\.]*}"
fi
cross=0
if [ "$architecture" != "$hostarch" ]; then
cross=1
[ "$hostarch" = "x86_64" ] && [ "$architecture" = "x86" ] && cross=0
[ "$hostarch" = "mips64" ] && [ "$architecture" = "mips" ] && cross=0
fi
[ "$os" != "$hostos" ] && cross=1
[ "$os" = "Android" ] && [ "$hostos" = "Linux" ] && [ -n "$TERMUX_APP_PID" ] \
&& cross=0
[ "$USE_OPENMP" != 1 ] && openmp=''
linker_L=""
linker_l=""
linker_a=""
link=`$compiler_name $flags -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $flags $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`
link=`echo "$link" | sed 's/\-Y[[:space:]]P\,/\-Y/g'`
flags=`echo $link | tr "'[[:space:]],\n" " "`
# Strip trailing quotes
old_flags="$flags"
flags=''
for flag in $old_flags; do
f=`echo "$flag" | tr '"' ' '`
flags="$flags $f"
done
for flag in $flags; do
case "$flag" in -L*)
case "$flag" in
-LIST:*|-LANG:*) ;;
*) linker_L="$linker_L $flag" ;;
esac
esac
case "$flag" in -Y*)
linker_L="$linker_L -Wl,$flag" ;;
esac
case "$flag" in --exclude-libs*)
linker_L="$linker_L -Wl,$flag"
flags=""
;;
esac
case "$flag" in -l*)
case "$flag" in
*gfortranbegin*|*frtbegin*|*pathfstart*|*numa*|*crt[0-9]*|\
*gcc*|*user32*|*kernel32*|*advapi32*|*shell32*|*omp*|\
*[0-9]*) ;;
*) linker_l="$linker_l $flag" ;;
esac
esac
case "$flag" in *.a) linker_a="$linker_a $flag" ;; esac
done
[ "$makefile" = "-" ] && {
[ "$no_rv64gv" -eq 1 ] && printf "NO_RV64GV=1\n"
[ "$no_avx512" -eq 1 ] && printf "NO_AVX512=1\n"
[ "$no_avx2" -eq 1 ] && printf "NO_AVX2=1\n"
[ "$oldgcc" -eq 1 ] && printf "OLDGCC=1\n"
exit 0
}
:> "$makefile" || exit 1
:> "$config" || exit 1
# print $data, "\n";
{
printf "OSNAME=%s\n" "$os"
printf "ARCH=%s\n" "$architecture"
printf "C_COMPILER=%s\n" "$compiler"
[ $binformat != 'bin32' ] && printf "BINARY32=\n"
[ $binformat != 'bin64' ] && printf "BINARY64=\n"
[ "$binformat" = "bin32" ] && printf "BINARY32=1\n"
[ "$binformat" = "bin64" ] && printf "BINARY64=1\n"
[ -n "$need_fu" ] && printf 'FU=%s\n' "$need_fu"
[ "$cross" -ne 0 ] && [ -n "$cross_suffix" ] && \
printf "CROSS_SUFFIX=%s\n" "$cross_suffix"
[ "$cross" -ne 0 ] && printf "CROSS=1\n"
printf "CEXTRALIB=%s %s %s\n" "$linker_L" "$linker_l" "$linker_a"
[ "$have_msa" -eq 1 ] && {
printf "HAVE_MSA=1\n"
printf "MSA_FLAGS=%s\n" "$msa_flags"
}
[ "$no_rv64gv" -eq 1 ] && printf "NO_RV64GV=1\n"
[ "$no_avx512" -eq 1 ] && printf "NO_AVX512=1\n"
[ "$no_avx2" -eq 1 ] && printf "NO_AVX2=1\n"
[ "$oldgcc" -eq 1 ] && printf "OLDGCC=1\n"
} >> "$makefile"
os=`echo "$os" | tr '[[:lower:]]' '[[:upper:]]'/ `
architecture=`echo "$architecture" | tr '[[:lower:]]' '[[:upper:]]' `
compiler=`echo "$compiler" | tr '[[:lower:]]' '[[:upper:]]' `
{
printf "#define OS_%s\t1\n" "$os"
printf "#define ARCH_%s\t1\n" "$architecture"
printf "#define C_%s\t1\n" "$compiler"
[ "$binformat" = "bin32" ] && printf "#define __32BIT__\t1\n"
[ "$binformat" = "bin64" ] && printf "#define __64BIT__\t1\n"
[ -n "$need_fu" ] && printf "#define FUNDERSCORE\t%s\n" "$need_fu"
[ "$have_msa" -eq 1 ] && printf "#define HAVE_MSA\t1\n"
[ "$c11_atomics" -eq 1 ] && printf "#define HAVE_C11\t1\n"
} >> "$config"
if [ "$os" = "LINUX" ]; then
# @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
# if ($pthread[2] ne "") {
# print CONFFILE "#define PTHREAD_CREATE_FUNC $pthread[2]\n";
# } else {
printf "#define PTHREAD_CREATE_FUNC pthread_create\n" >> "$config"
# }
else
printf "#define PTHREAD_CREATE_FUNC pthread_create\n" >> "$config"
fi