#!/usr/bin/env bash1#2# Determine whether the C compiler $CC $CFLAGS supports certain3# compiler flags. "supported" simply means here that a very basic4# program compiles.5#6# Usage: testcflags.sh [CFLAG1] [CFLAG2]...7#8# This will test all given flags one by one (together with $CFLAGS) and9# print (on stdout) a space-separated list of supported flags. Note10# that the order is important: if CFLAG1 works, it will be added while11# testing CFLAG2.12#13# The exit code is 0 if all given flags are supported, 1 if some flag14# is not supported, 2 if some other error occurred.15#16# For example, running17# $ testcflags.sh --foobar -Wall -march=native18# will return (on recent gcc versions)19# -Wall -march=native20# with exit status 1 (because --foobar is not supported).21#22# A typical use would be:23# $ CFLAGS="$CFLAGS `testcflags.sh -ffoo-bar`"24#25# It is allowed to group flags, these will then be tested together.26# For example, running27# $ testcflags.sh '-march=native -mno-avx'28# will test these flags together. So, the output can be either empty29# or it can be "-march=native -mno-avx", but not "-march=native".30#31#32# AUTHORS:33#34# - Jeroen Demeyer (2012-01-27): initial version (#12367)3536# - Jeroen Demeyer (2012-04-11): various fixes (#12821)37#38#*****************************************************************************39# Copyright (C) 2012 Jeroen Demeyer <[email protected]>40#41# Distributed under the terms of the GNU General Public License (GPL)42# as published by the Free Software Foundation; either version 2 of43# the License, or (at your option) any later version.44# http://www.gnu.org/licenses/45#*****************************************************************************4647usage()48{49cat >&2 <<EOF50Usage: $0 [CFLAG1] [CFLAG2]...5152Determine whether the C compiler \$CC \$CFLAGS supports certain53compiler flags. "supported" simply means here that a very basic54program compiles.5556This will test all given flags one by one (together with \$CFLAGS) and57print (on stdout) a space-separated list of supported flags. Note58that the order is important: if CFLAG1 works, it will be added while59testing CFLAG2.6061The exit code is 0 if all given flags are supported, 1 if some flag62is not supported, 2 if some other error occurred.63EOF64}6566if [ -z "$CC" ]; then67echo >&2 "$0: set \$CC before running this script."68echo69usage70exit 271fi7273if [ $# -lt 1 ]; then74usage75exit 176fi7778if [ -z "$SAGE_ROOT" ]; then79echo "The SAGE_ROOT environment variable must be set to"80echo "the root of the Sage installation"81exit 182fi8384mkdir -p "$SAGE_LOCAL/var/tmp/sage/build"85if [ $? -ne 0 ]; then86echo "Error while trying to create the build directory."87exit 188fi8990cd "$SAGE_LOCAL/var/tmp/sage/build"91if [ $? -ne 0 ]; then92echo "Error while trying to change into the build directory."93exit 194fi95outfile=sage-testcflags-$$96cfile=$outfile.c9798cat >$cfile <<EOF99/* The following function yields assembler errors on OS X 10.7 with100* certain versions of XCode when compiling code for a machine with101* support for AVX (Advanced Vector Extensions). */102volatile double d;103unsigned long double_to_ulong()104{105return (unsigned long)d;106}107108int main(int argc, char **argv)109{110return 0;111}112EOF113if [ $? -ne 0 ]; then114echo >&2 "Error: cannot write to file `pwd`/$cfile"115exit 2116fi117118status=0119NEWCFLAGS=""120for testflag in "$@"; do121if $CC $CFLAGS $NEWCFLAGS $testflag $cfile -o $outfile 2>/dev/null; then122# Success123NEWCFLAGS="$NEWCFLAGS $testflag"124else125# Failure126status=1127fi128done129130# Some OS X systems create a directory with debug info,131# so we really need -r here (#13945).132rm -rf $outfile*133134echo $NEWCFLAGS135exit $status136137138