ax_add_fortify_source.m4 (3788B)
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 Ubuntu or Gentoo Linux) enable _FORTIFY_SOURCE globally in 14 # their 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, 2023 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 10 40 41 AC_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 ax_add_fortify_3_failed= 46 AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=3 to CPPFLAGS]) 47 AC_LINK_IFELSE([ 48 AC_LANG_PROGRAM([], 49 [[ 50 #ifndef _FORTIFY_SOURCE 51 return 0; 52 #else 53 _FORTIFY_SOURCE_already_defined; 54 #endif 55 ]] 56 )], 57 AC_LINK_IFELSE([ 58 AC_LANG_SOURCE([[ 59 #define _FORTIFY_SOURCE 3 60 #include <string.h> 61 int main(void) { 62 char *s = " "; 63 strcpy(s, "x"); 64 return strlen(s)-1; 65 } 66 ]] 67 )], 68 [ 69 AC_MSG_RESULT([yes]) 70 CFLAGS=$ac_save_cflags 71 CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=3" 72 ], [ 73 AC_MSG_RESULT([no]) 74 ax_add_fortify_3_failed=1 75 ], 76 ), 77 [ 78 AC_MSG_RESULT([no]) 79 ax_add_fortify_3_failed=1 80 ]) 81 if test -n "$ax_add_fortify_3_failed" 82 then 83 AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=2 to CPPFLAGS]) 84 AC_LINK_IFELSE([ 85 AC_LANG_PROGRAM([], 86 [[ 87 #ifndef _FORTIFY_SOURCE 88 return 0; 89 #else 90 _FORTIFY_SOURCE_already_defined; 91 #endif 92 ]] 93 )], 94 AC_LINK_IFELSE([ 95 AC_LANG_SOURCE([[ 96 #define _FORTIFY_SOURCE 2 97 #include <string.h> 98 int main(void) { 99 char *s = " "; 100 strcpy(s, "x"); 101 return strlen(s)-1; 102 } 103 ]] 104 )], 105 [ 106 AC_MSG_RESULT([yes]) 107 CFLAGS=$ac_save_cflags 108 CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2" 109 ], [ 110 AC_MSG_RESULT([no]) 111 CFLAGS=$ac_save_cflags 112 ], 113 ), 114 [ 115 AC_MSG_RESULT([no]) 116 CFLAGS=$ac_save_cflags 117 ]) 118 fi 119 ])