ax_add_fortify_source.m4 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # ===========================================================================
  2. # Modified from 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 CFLAGS without macro
  12. # redefinition warnings. Some distributions (such as Gentoo Linux) enable
  13. # _FORTIFY_SOURCE globally in their compilers, leading to unnecessary
  14. # 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 CFLAGS.
  22. #
  23. # LICENSE
  24. #
  25. # Copyright (c) 2017 David Seifert <soap@gentoo.org>
  26. #
  27. # Copying and distribution of this file, with or without modification, are
  28. # permitted in any medium without royalty provided the copyright notice
  29. # and this notice are preserved. This file is offered as-is, without any
  30. # warranty.
  31. #serial 1
  32. AC_DEFUN([AX_ADD_FORTIFY_SOURCE],[
  33. AC_MSG_CHECKING([whether to add -D_FORTIFY_SOURCE=2 to CFLAGS])
  34. AC_LINK_IFELSE([
  35. AC_LANG_SOURCE(
  36. [[
  37. int main() {
  38. #ifndef _FORTIFY_SOURCE
  39. return 0;
  40. #else
  41. this_is_an_error;
  42. #endif
  43. }
  44. ]]
  45. )], [
  46. AC_MSG_RESULT([yes])
  47. CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2"
  48. ], [
  49. AC_MSG_RESULT([no])
  50. ])
  51. ])