• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ===========================================================================
2#  https://www.gnu.org/software/autoconf-archive/ax_add_fortify_source.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AX_ADD_FORTIFY_SOURCE
8#
9# DESCRIPTION
10#
11#   Check whether -D_FORTIFY_SOURCE=2 can be added to CPPFLAGS without macro
12#   redefinition warnings, other cpp warnings or linker. Some distributions
13#   (such as Gentoo Linux) enable _FORTIFY_SOURCE globally in their
14#   compilers, leading to unnecessary warnings in the form of
15#
16#     <command-line>:0:0: error: "_FORTIFY_SOURCE" redefined [-Werror]
17#     <built-in>: note: this is the location of the previous definition
18#
19#   which is a problem if -Werror is enabled. This macro checks whether
20#   _FORTIFY_SOURCE is already defined, and if not, adds -D_FORTIFY_SOURCE=2
21#   to CPPFLAGS.
22#
23#   Newer mingw-w64 msys2 package comes with a bug in
24#   headers-git-7.0.0.5546.d200317d-1. It broke -D_FORTIFY_SOURCE support,
25#   and would need -lssp or -fstack-protector.  See
26#   https://github.com/msys2/MINGW-packages/issues/5803. Try to actually
27#   link it.
28#
29# LICENSE
30#
31#   Copyright (c) 2017 David Seifert <soap@gentoo.org>
32#   Copyright (c) 2019 Reini Urban <rurban@cpan.org>
33#
34#   Copying and distribution of this file, with or without modification, are
35#   permitted in any medium without royalty provided the copyright notice
36#   and this notice are preserved.  This file is offered as-is, without any
37#   warranty.
38
39#serial 4
40
41AC_DEFUN([AX_ADD_FORTIFY_SOURCE],[
42    ac_save_cflags=$CFLAGS
43    ac_cwerror_flag=yes
44    AX_CHECK_COMPILE_FLAG([-Werror],[CFLAGS="$CFLAGS -Werror"])
45    AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=2 to CPPFLAGS])
46    AC_LINK_IFELSE([
47        AC_LANG_PROGRAM([],
48            [[
49                #ifndef _FORTIFY_SOURCE
50                    return 0;
51                #else
52                    this_is_an_error;
53                #endif
54            ]]
55        )],
56        AC_LINK_IFELSE([
57            AC_LANG_SOURCE([[
58                #define _FORTIFY_SOURCE 2
59                #include <string.h>
60                int main() {
61                    char *s = " ";
62                    strcpy(s, "x");
63                    return strlen(s)-1;
64                }
65              ]]
66            )],
67            [
68              AC_MSG_RESULT([yes])
69              CFLAGS=$ac_save_cflags
70              CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2"
71            ], [
72              AC_MSG_RESULT([no])
73              CFLAGS=$ac_save_cflags
74            ],
75        ),
76        [
77          AC_MSG_RESULT([no])
78          CFLAGS=$ac_save_cflags
79        ])
80])
81