1dnl
2dnl a set of macros related to gxx
3dnl
4dnl Helfer Thomas 22/12/06
5
6AC_DEFUN([AC_GCC_CHECK_MACHINE_TYPE],[
7  AC_REQUIRE([AC_CANONICAL_HOST])dnl
8
9  AC_CHECKING([for MACHINE_TYPE])
10
11  GCC_MACHINE_TYPE="unknown"
12  case "$build_cpu" in
13  x86_64*)  GCC_MACHINE_TYPE="64" ;; dnl opteron 64 bits
14  i[[3456]]86*linux*) GCC_MACHINE_TYPE="32" ;; dnl intel et compatible 32 bits
15  *)        GCC_MACHINE_TYPE="32" ;; dnl defaut 32 bits
16  esac])
17
18dnl
19dnl @synopsis AC_CHECK_FLAG(-flag,variable where we add flag if ok, optional flag)
20dnl
21dnl Check flags for C++ compiler
22dnl
23dnl @author Stephane Bernaud - 06/04/2010
24dnl
25AC_DEFUN([AC_GCC_CHECK_FLAG], [
26	dogxx=`$CXX $1 $3 -S -o /dev/null -xc /dev/null 2>&1 && echo "__OK__"`
27	if echo $dogxx | grep -e "__OK__" > /dev/null 2>&1 ; then
28	    $2="${$2} $1"
29	fi
30	]) dnl end of AC_GCC_CHECK_FLAG
31
32
33dnl
34dnl @synopsis AC_GCC_LINUX_OPTIMISATIONS
35dnl
36dnl set CXXFLAGS, CFLAGS and FFLAGS regarding gcc version and processor type
37dnl
38dnl @author Stephane Bernaud - 06/04/2010
39dnl  Reference : http://www.pixelbeat.org/
40dnl  Reference : http://en.gentoo-wiki.com/wiki/Safe_Cflags
41dnl
42
43AC_DEFUN([AC_GCC_LINUX_OPTIMISATIONS],[
44	AC_CHECKING(for gcc options regarding gcc version and processor type)
45	AC_REQUIRE([AC_GCC_CHECK_MACHINE_TYPE])
46
47	AC_SUBST(CXXFLAGS)
48	AC_SUBST(CFLAGS)
49	AC_SUBST(FFLAGS)
50
51	if test -f /proc/cpuinfo; then
52	    vendor_id=`cat /proc/cpuinfo |
53         grep "vendor_id" |
54	 uniq |
55         cut -d':' -f2 |
56	 sed -e 's/ //g'`
57
58	    cpu_family=`cat /proc/cpuinfo |
59         grep "cpu family" |
60	 uniq |
61         cut -d':' -f2 |
62         sed -e 's/ //g'`
63
64	    cpu_model=`cat /proc/cpuinfo |
65         grep "model" |
66	 grep -v "model name" |
67	 uniq |
68         cut -d':' -f2 |
69         sed -e 's/ //g'`
70
71	    flags=`cat /proc/cpuinfo |
72         grep "flags" |
73	 uniq |
74         cut -d':' -f2`
75	else
76	    AC_MSG_ERROR("Unable to access /proc/cpuinfo.")
77	fi
78
79	dnl AC_MSG_NOTICE([vendor_id =$vendor_id-])
80	dnl AC_MSG_NOTICE([cpu_family =$cpu_family])
81	dnl AC_MSG_NOTICE([cpu_model =$cpu_model])
82	dnl AC_MSG_NOTICE([flags =$flags])
83
84	case "$vendor_id" in
85	    GenuineIntel)
86		if test $cpu_family -eq 3 ; then
87		    line="i386"
88		fi
89		if test  $cpu_family -eq 4 ; then
90		    line="i486"
91		fi
92		if test $cpu_family -eq 5 ; then
93		    if test $cpu_model -ne 4 ; then
94			line="pentium"
95		    else
96			line="pentium-mmx pentium"
97		    fi
98		fi
99		if test $cpu_family -eq 6 ; then
100		    if test  $cpu_model -eq 0 -o  $cpu_model -eq 1 ; then
101			line="pentiumpro"
102		    fi
103		    if test $cpu_model -ge 3  -a  $cpu_model -le 6 ; then
104			line="pentium2 pentiumpro pentium-mmx pentium i486 i386"
105		    fi
106		    if test $cpu_model -eq 9  -o $cpu_model -eq 13 ; then
107			line="pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
108		    fi
109		    if test $cpu_model -eq 14  -o $cpu_model -eq 22 ; then
110			line="prescott pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
111		    fi
112		    if test  $cpu_model -eq 28 ; then
113			line="atom core2 pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
114		    fi
115		    if test $cpu_model -ge 15 ; then
116			if test $GCC_MACHINE_TYPE -eq 32 ; then
117			    line="core2 prescott pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
118			else
119			    line="core2 nocona pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
120			fi
121		    fi
122		    if test  $cpu_model -ge 7  -a $cpu_model -le 11 ; then
123			line="pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
124		    fi
125		fi
126		if test $cpu_family -eq 15 ; then
127		    line="pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
128		    if test $cpu_model -ge 3 ; then
129			if test $GCC_MACHINE_TYPE -eq 32 ; then
130			    line="prescott $line"
131			else
132			    line="nocona $line"
133			fi
134		    fi
135		fi
136		dnl failsafe with default value
137		if test "x$line" == "x" ; then
138		    if test $GCC_MACHINE_TYPE -eq 32 ; then
139			line="prescott"
140		    else
141			line="nocona"
142		    fi
143		fi
144		for f in $line
145		  do
146		  AC_GCC_CHECK_FLAG(-march=$f,MY_ARCH_FLAG)
147		  if test "x$MY_ARCH_FLAG" != "x" ; then
148		      break
149		  fi
150		done
151		;;
152	    AuthenticAMD)
153		if test $cpu_family -eq 4 ; then
154		    line="i486"
155		fi
156		if test $cpu_family -eq 5 ; then
157		    if test $cpu_model -lt 4 ; then
158			line="pentium"
159		    fi
160		    if test $cpu_model -eq 6  -o $cpu_model -eq 7 ; then
161			line="k6"
162		    fi
163		    if test $cpu_model -eq 8  -o $cpu_model -eq 12 ; then
164			line="k6-2 k6"
165		    fi
166		    if test $cpu_model -eq 9  -o $cpu_model -eq 13 ; then
167			line="k6-3 k6-2 k6"
168		    fi
169		    if test $cpu_model -eq 10 ; then
170			line="geode k6-2 k6"
171		    fi
172		fi
173		if test $cpu_family -eq 6 ; then
174		    if test $cpu_model -le 3 ; then
175			line="athlon k6-3 k6-2 k6"
176		    fi
177		    if test $cpu_model -eq 4 ; then
178			line="athlon-tbird athlon k6-3 k6-2 k6"
179		    fi
180		    if test $cpu_model -ge 6 ; then
181			line="athlon-4 athlon k6-3 k6-2 k6"
182		    fi
183		fi
184		if test $cpu_family -eq 15 ; then
185		    line="k8 athlon-4 athlon k6-3 k6-2 k6"
186		fi
187		if test  $cpu_family -eq 16 -o $cpu_family -eq 17 ; then
188		    line="amdfam10 k8 athlon-4 athlon k6-3 k6-2 k6"
189		fi
190		dnl failsafe with default value
191		if test "x$line" == "x" ; then
192		    line="k8"
193		fi
194		for f in $line
195		  do
196		  AC_GCC_CHECK_FLAG(-march=$f,MY_ARCH_FLAG)
197		  if test "x$MY_ARCH_FLAG" != "x" ; then
198		      break
199		  fi
200		done
201		;;
202	    *)
203		AC_MSG_ERROR([Unsupported CPU vendor : $vendor_id])
204		;;
205	esac dnl end of case "$vendor_id"
206
207	dnl Do we have a sse capable processor
208	echo $flags | grep -q "sse"
209	if test "$?" = "0" ; then
210	    AC_GCC_CHECK_FLAG(-mfpmath=sse,MY_FPMATH_FLAG,$MY_ARCH_FLAG)
211	fi
212
213	dnl we need sse FPU. So exit if not found
214	if test -z $MY_FPMATH_FLAG ; then
215	    AC_MSG_ERROR([Unable to set FPU to sse. Check gcc version or processor.])
216	fi
217
218	dnl optimize for sse2, sse3
219	echo $flags | grep -q -E "sse3|pni"
220	if test "$?" = "0" ; then
221	    AC_GCC_CHECK_FLAG(-msse3,MY_SSE_FLAG,$MY_ARCH_FLAG $MY_FPMATH_FLAG )
222	else
223	    echo $flags | grep -q "sse2"
224	    if test "$?" = "0" ; then
225		AC_GCC_CHECK_FLAG(-msse2,MY_SSE_FLAG,$MY_ARCH_FLAG $MY_FPMATH_FLAG )
226	    fi
227	fi
228
229	AC_MSG_NOTICE([detected arch flag   =$MY_ARCH_FLAG])
230	AC_MSG_NOTICE([detected fpmath flag =$MY_FPMATH_FLAG])
231	AC_MSG_NOTICE([detected sse flag    =$MY_SSE_FLAG])
232
233	CXXFLAGS="-O2 $MY_ARCH_FLAG $MY_FPMATH_FLAG $MY_SSE_FLAG $CXXFLAGS"
234	CFLAGS="-O2 $MY_ARCH_FLAG $MY_FPMATH_FLAG $MY_SSE_FLAG $CFLAGS"
235	OPTIMISATION_FLAGS="-O2 $MY_ARCH_FLAG $MY_FPMATH_FLAG $MY_SSE_FLAG $OPTIMISATION_FLAGS"
236
237]) dnl end of AC_GCC_LINUX_OPTIMISATIONS
238
239AC_DEFUN([AC_GCC_DEFAULT_OPTIMISATIONS],
240    [
241	OPTIMISATION_FLAGS="-O2 $OPTIMISATION_FLAGS"
242	CXXFLAGS="-O2 $CXXFLAGS"
243	CFLAGS="-O2 $CFLAGS"
244])
245
246dnl
247dnl guess architecture specific options
248dnl
249dnl Helfer Thomas 22/12/06
250dnl Helfer Thomas 30/10/14 (add test on '-march=native')
251AC_DEFUN([AC_GCC_ARCH_OPTIMISATIONS],[
252	if test "x$host" = "x$build"; then
253	    echo $host | grep "linux" &> /dev/null
254	    if test "x$?" = "x0"; then
255		sys="linux"
256	    fi
257	    case "$sys" in
258		linux*)
259		    AC_GCC_LINUX_OPTIMISATIONS
260		    ;;
261		*)
262		    AC_GCC_DEFAULT_OPTIMISATIONS
263		    ;;
264    esac
265    else
266    AC_GCC_DEFAULT_OPTIMISATIONS
267    fi
268    ])
269
270AC_DEFUN([AC_CHECK_GXX],
271    [
272	if test "x$GXX" = "xyes"; then
273
274	    dnl gcc default warning options
275	    CFLAGS="$CFLAGS -W -Wall"
276
277	    dnl g++ default warning options
278
279	    COMPILER_WARNINGS="-W -Wall -Wshadow"
280	    COMPILER_WARNINGS="$COMPILER_WARNINGS -Wpointer-arith -Wcast-qual -Wcast-align"
281	    COMPILER_WARNINGS="$COMPILER_WARNINGS -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor"
282	    COMPILER_WARNINGS="$COMPILER_WARNINGS -Woverloaded-virtual -Wreturn-type"
283	    COMPILER_WARNINGS="$COMPILER_WARNINGS -Wno-endif-labels  -Wmissing-format-attribute"
284	    COMPILER_WARNINGS="$COMPILER_WARNINGS -Wno-multichar -Wno-deprecated-declarations"
285	    COMPILER_WARNINGS="$COMPILER_WARNINGS -Wredundant-decls -Wdisabled-optimization"
286	    COMPILER_WARNINGS="$COMPILER_WARNINGS -Wunknown-pragmas -Wundef  -Wreorder"
287
288            CXXFLAGS="$CXXFLAGS $COMPILER_WARNINGS"
289	    CXXFLAGS="$CXXFLAGS -std=c++11 -DNO_RUNTIME_CHECK_BOUNDS $COMPILER_WARNINGS"
290
291	    if test "${CXX}" = "clang++"; then
292               CXXFLAGS="$CXXFLAGS -ftemplate-depth-250"
293	       CXXFLAGS="$CXXFLAGS -Wno-mismatched-tags"
294	       COMPILER_SPECIFIC_OPTIONS="-ftemplate-depth-250 -Wno-mismatched-tags $COMPILER_SPECIFIC_OPTIONS"
295            fi
296
297	    if test "${CXX}" != "clang++"; then
298		dnl gxx versions prior to 3.3 are known to be buggy
299		dnl when two much metaprogramming is involved
300		AC_MSG_CHECKING(for gxx 3.4.0 or later)
301		GXXVERSION="`"$CXX" -dumpversion`"
302		AC_MSG_RESULT($GXXVERSION)
303		GXXVERSION="`"$CXX" -dumpversion |
304                awk 'BEGIN { FS = "." };
305                      1 { if ((@S|@1 * 10000 + @S|@2 * 100 + @S|@3) >= 30400) { print "yes" } }'
306                `"
307		if test "x$GXXVERSION" = "x$no"; then
308		    echo "Found $GXXVERSION"
309		    AC_MSG_ERROR("g++ version prior to 3.3 are not supported. Please use at least g++3.4")
310		fi
311		GXXVERSION="`"$CXX" -dumpversion |
312                awk 'BEGIN { FS = "." };
313                      1 { if ((@S|@1 * 10000 + @S|@2 * 100 + @S|@3) >= 40000) { print "yes" } }'
314                `"
315		if test "x$GXXVERSION" = "x$no"; then
316		    AC_MSG_WARN("g++3.4 does not work with FSAlgorithm specialisation nicely")
317		    AC_MSG_WARN("Using the -DNO_FSALGORITHM_SPECIALISATION flag")
318		    CPPFLAGS="-DNO_FSALGORITHM_SPECIALISATION -fno-builtin-abs $CPPFLAGS"
319		    COMPILER_SPECIFIC_OPTIONS="-DNO_FSALGORITHM_SPECIALISATION -fno-builtin-abs $COMPILER_SPECIFIC_OPTIONS"
320		else
321		    CPPFLAGS=" $CPPFLAGS"
322		    CXXFLAGS="$CXXFLAGS"
323		fi
324	    fi
325	fi
326
327	dnl symbol visibility
328	GCC_SYMBOL_VISIBILITY=""
329	AC_GCC_CHECK_FLAG(-fvisibility=hidden,GCC_SYMBOL_VISIBILITY)
330	if test "x$GCC_SYMBOL_VISIBILITY" != "x";
331	then
332	   CXXFLAGS="$CXXFLAGS $GCC_SYMBOL_VISIBILITY"
333	   CFLAGS="$CFLAGS $GCC_SYMBOL_VISIBILITY"
334	   OPTIMISATION_FLAGS0="$GCC_SYMBOL_VISIBILITY $OPTIMISATION_FLAGS0"
335	fi
336
337	dnl symbol visibility
338	GCC_SYMBOL_VISIBILITY=""
339	AC_GCC_CHECK_FLAG(-fvisibility-inlines-hidden,GCC_SYMBOL_VISIBILITY)
340	if test "x$GCC_SYMBOL_VISIBILITY" != "x"; then
341	dnl a small test because this compilation flag may cause problems
342	dnl on some systems
343	cat > test-fvisibility-inlines-hidden.cxx << EOF
344	#include<string>
345	#include<sstream>
346	void function(const double a)
347	{
348	  using namespace std;
349	  ostringstream c;
350	  c << a;
351	}
352EOF
353	$CXX -fvisibility-inlines-hidden --shared -DPIC -fPIC test-fvisibility-inlines-hidden.cxx -o libtest-fvisibility-inlines-hidden.so &> /dev/null
354	if test x"$?" == "x0" ; then
355	  CXXFLAGS="$CXXFLAGS $GCC_SYMBOL_VISIBILITY"
356	  OPTIMISATION_FLAGS0="$GCC_SYMBOL_VISIBILITY $OPTIMISATION_FLAGS0"
357	  AC_MSG_NOTICE([-fvisibility-inlines-hidden enabled])
358	  rm libtest-fvisibility-inlines-hidden.so
359	else
360	  AC_MSG_NOTICE([-fvisibility-inlines-hidden disabled])
361	fi
362	rm test-fvisibility-inlines-hidden.cxx
363      fi
364
365	if test "x$enable_optimizations" != "xno"; then
366	    if test "x$enable_debug" != "xyes"; then
367		dnl g++ debug options
368		CPPFLAGS="-DNDEBUG $CPPFLAGS"
369		AC_GCC_ARCH_OPTIMISATIONS
370	    fi
371	fi
372
373	if test "x$enable_debug" == "xyes"; then
374	    dnl g++ debug options
375	    CXXFLAGS="-g $CXXFLAGS"
376	fi
377
378	]) dnl end of AC_CHECK GXX
379