Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/bin/testcflags.sh
4052 views
1
#!/usr/bin/env bash
2
#
3
# Determine whether the C compiler $CC $CFLAGS supports certain
4
# compiler flags. "supported" simply means here that a very basic
5
# program compiles.
6
#
7
# Usage: testcflags.sh [CFLAG1] [CFLAG2]...
8
#
9
# This will test all given flags one by one (together with $CFLAGS) and
10
# print (on stdout) a space-separated list of supported flags. Note
11
# that the order is important: if CFLAG1 works, it will be added while
12
# testing CFLAG2.
13
#
14
# The exit code is 0 if all given flags are supported, 1 if some flag
15
# is not supported, 2 if some other error occurred.
16
#
17
# For example, running
18
# $ testcflags.sh --foobar -Wall -march=native
19
# will return (on recent gcc versions)
20
# -Wall -march=native
21
# with exit status 1 (because --foobar is not supported).
22
#
23
# A typical use would be:
24
# $ CFLAGS="$CFLAGS `testcflags.sh -ffoo-bar`"
25
#
26
# It is allowed to group flags, these will then be tested together.
27
# For example, running
28
# $ testcflags.sh '-march=native -mno-avx'
29
# will test these flags together. So, the output can be either empty
30
# or it can be "-march=native -mno-avx", but not "-march=native".
31
#
32
#
33
# AUTHORS:
34
#
35
# - Jeroen Demeyer (2012-01-27): initial version (#12367)
36
37
# - Jeroen Demeyer (2012-04-11): various fixes (#12821)
38
#
39
#*****************************************************************************
40
# Copyright (C) 2012 Jeroen Demeyer <[email protected]>
41
#
42
# Distributed under the terms of the GNU General Public License (GPL)
43
# as published by the Free Software Foundation; either version 2 of
44
# the License, or (at your option) any later version.
45
# http://www.gnu.org/licenses/
46
#*****************************************************************************
47
48
usage()
49
{
50
cat >&2 <<EOF
51
Usage: $0 [CFLAG1] [CFLAG2]...
52
53
Determine whether the C compiler \$CC \$CFLAGS supports certain
54
compiler flags. "supported" simply means here that a very basic
55
program compiles.
56
57
This will test all given flags one by one (together with \$CFLAGS) and
58
print (on stdout) a space-separated list of supported flags. Note
59
that the order is important: if CFLAG1 works, it will be added while
60
testing CFLAG2.
61
62
The exit code is 0 if all given flags are supported, 1 if some flag
63
is not supported, 2 if some other error occurred.
64
EOF
65
}
66
67
if [ -z "$CC" ]; then
68
echo >&2 "$0: set \$CC before running this script."
69
echo
70
usage
71
exit 2
72
fi
73
74
if [ $# -lt 1 ]; then
75
usage
76
exit 1
77
fi
78
79
if [ -z "$SAGE_ROOT" ]; then
80
echo "The SAGE_ROOT environment variable must be set to"
81
echo "the root of the Sage installation"
82
exit 1
83
fi
84
85
mkdir -p "$SAGE_LOCAL/var/tmp/sage/build"
86
if [ $? -ne 0 ]; then
87
echo "Error while trying to create the build directory."
88
exit 1
89
fi
90
91
cd "$SAGE_LOCAL/var/tmp/sage/build"
92
if [ $? -ne 0 ]; then
93
echo "Error while trying to change into the build directory."
94
exit 1
95
fi
96
outfile=sage-testcflags-$$
97
cfile=$outfile.c
98
99
cat >$cfile <<EOF
100
/* The following function yields assembler errors on OS X 10.7 with
101
* certain versions of XCode when compiling code for a machine with
102
* support for AVX (Advanced Vector Extensions). */
103
volatile double d;
104
unsigned long double_to_ulong()
105
{
106
return (unsigned long)d;
107
}
108
109
int main(int argc, char **argv)
110
{
111
return 0;
112
}
113
EOF
114
if [ $? -ne 0 ]; then
115
echo >&2 "Error: cannot write to file `pwd`/$cfile"
116
exit 2
117
fi
118
119
status=0
120
NEWCFLAGS=""
121
for testflag in "$@"; do
122
if $CC $CFLAGS $NEWCFLAGS $testflag $cfile -o $outfile 2>/dev/null; then
123
# Success
124
NEWCFLAGS="$NEWCFLAGS $testflag"
125
else
126
# Failure
127
status=1
128
fi
129
done
130
131
# Some OS X systems create a directory with debug info,
132
# so we really need -r here (#13945).
133
rm -rf $outfile*
134
135
echo $NEWCFLAGS
136
exit $status
137
138