Linux: fix build after e78c5951 - ASAN support
[scilab.git] / scilab / m4 / compiler.m4
1 dnl
2 dnl Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 dnl Copyright (C) S/E - 2013/2013 - Sylvestre Ledru
4 dnl Copyright (C) Scilab Enterprises - 2014 - Clement DAVID
5 dnl Copyright (C) ESI Group - 2018 - Clement DAVID
6 dnl
7 dnl Copyright (C) 2012 - 2016 - Scilab Enterprises
8 dnl
9 dnl This file is hereby licensed under the terms of the GNU GPL v2.0,
10 dnl pursuant to article 5.3.4 of the CeCILL v.2.1.
11 dnl This file was originally licensed under the terms of the CeCILL v2.1,
12 dnl and continues to be available under such terms.
13 dnl For more information, see the COPYING file which you should have received
14 dnl along with this program.
15
16 dnl CHECK_COMPILER_ARG(LANG, ARG, ACTION-IF-NOT_FOUND)
17 AC_DEFUN([CHECK_COMPILER_ARG],[
18     AC_LANG_PUSH([$1])
19     AC_MSG_CHECKING([if the $1 compiler accepts $2])
20     ac_save_$3="$$3"
21     $3="$$3 $$2"
22
23     AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
24        [AC_MSG_RESULT([yes])
25        $4="$2"
26        ],
27        [AC_MSG_RESULT([no])]
28     )
29     $3="$ac_save_$3";
30     AC_LANG_POP([$1])
31
32 ])
33
34
35 AC_DEFUN([AC_CHECK_STDCPP_STATIC],[
36
37 AC_ARG_ENABLE(static-system-lib,
38     AC_HELP_STRING([--enable-static-system-lib],[Enable the usage of the static stdc++ and libgcc libs]),
39 )
40
41 # Check if -static-libstdc++ and -static-libgcc are supported.
42 saved_LDFLAGS="$LDFLAGS"
43
44 compiler_manage_static_libs=no
45 USE_STATIC_SYSTEM_LIB=no
46
47 if test "x$enable_static_system_lib" == "xyes"; then
48 # The user ask for an explicit enable.
49    USE_STATIC_SYSTEM_LIB=yes
50 fi
51
52
53 LDFLAGS="$LDFLAGS -static-libstdc++ -static-libgcc"
54 AC_MSG_CHECKING([whether g++ accepts -static-libstdc++ -static-libgcc])
55 AC_LANG_PUSH(C++)
56 AC_LINK_IFELSE([AC_LANG_SOURCE([
57 #if (__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 5)
58 #error -static-libstdc++ not implemented
59 #endif
60 int main() {return 0;}])],
61   [AC_MSG_RESULT([yes]); compiler_manage_static_libs=yes],
62   [AC_MSG_RESULT([no])])
63 AC_LANG_POP(C++)
64
65 LDFLAGS="$saved_LDFLAGS"
66 if test "x$compiler_manage_static_libs" == "xno" -a "x$enable_static_system_lib" == "xyes"; then
67    AC_MSG_ERROR([The $CC compiler does not support the options -static-libstdc++/-static-libgcc. Update your compiler or use the option --disable-static-system-lib])
68 fi
69
70 if test "x$compiler_manage_static_libs" == "xno"; then
71    echo "-static-libstdc++ -static-libgcc unsupported by the compiler. Skipped"
72    USE_STATIC_SYSTEM_LIB=no
73 fi
74
75 AM_CONDITIONAL(USE_STATIC_SYSTEM_LIB, test "$USE_STATIC_SYSTEM_LIB" == yes)
76
77 ])
78
79 AC_DEFUN([AC_CHECK_ADDRESS_SANTIZER],[
80
81 AC_ARG_ENABLE(address-sanitizer,
82     AS_HELP_STRING([--enable-address-sanitizer], [Enable AddressSanitizer instrumentation]))
83
84 # Check if -fsanitize=address is supported at compile time and link time
85 saved_CFLAGS="$CFLAGS"
86 saved_LDFLAGS="$LDFLAGS"
87 asan_supported=no
88
89 CFLAGS="$CFLAGS -fsanitize=address"
90 LDFLAGS="$LDFLAGS -fsanitize=address"
91 AC_MSG_CHECKING([whether the C compiler accepts -fsanitize=address])
92 AC_LANG_PUSH(C)
93 AC_RUN_IFELSE([AC_LANG_PROGRAM([[const char hw[] = "Hello, AddressSanitizer\n";]], [])], [AC_MSG_RESULT([yes]); asan_supported=yes], [AC_MSG_RESULT([no])])
94 AC_LANG_POP(C)
95
96 CFLAGS="$saved_CFLAGS"
97 LDFLAGS="$saved_LDFLAGS"
98 if test "x$asan_supported" == "xno" -a "x$enable_address_sanitizer" == "xyes"; then
99     AC_MSG_ERROR([The $CC compiler does not support the options -fsanitize=address . Update your compiler and/or install the AddressSanitizer runtime library.])
100 elif test "x$enable_address_sanitizer" == "xyes"; then
101     COMPILER_CFLAGS="$COMPILER_CFLAGS -fsanitize=address"
102     COMPILER_CXXFLAGS="$COMPILER_CXXFLAGS -fsanitize=address"
103     COMPILER_LDFLAGS="$COMPILER_LDFLAGS -fsanitize=address"
104 fi
105
106 AM_CONDITIONAL(USE_ADDRESS_SANITIZER, test "x$enable_address_sanitizer" == "xyes")
107
108 ])