1
0

ax_harden_compiler_flags.m4 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # ===========================================================================
  2. # https://github.com/BrianAker/ddm4/
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_HARDEN_COMPILER_FLAGS()
  8. # AX_HARDEN_LINKER_FLAGS()
  9. # AX_HARDEN_CC_COMPILER_FLAGS()
  10. # AX_HARDEN_CXX_COMPILER_FLAGS()
  11. #
  12. # DESCRIPTION
  13. #
  14. # Any compiler flag that "hardens" or tests code. C99 is assumed.
  15. #
  16. # NOTE: Implementation based on AX_APPEND_FLAG.
  17. #
  18. # LICENSE
  19. #
  20. # Copyright (C) 2012 Brian Aker
  21. # All rights reserved.
  22. #
  23. # Redistribution and use in source and binary forms, with or without
  24. # modification, are permitted provided that the following conditions are
  25. # met:
  26. #
  27. # * Redistributions of source code must retain the above copyright
  28. # notice, this list of conditions and the following disclaimer.
  29. #
  30. # * Redistributions in binary form must reproduce the above
  31. # copyright notice, this list of conditions and the following disclaimer
  32. # in the documentation and/or other materials provided with the
  33. # distribution.
  34. #
  35. # * The names of its contributors may not be used to endorse or
  36. # promote products derived from this software without specific prior
  37. # written permission.
  38. #
  39. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  40. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  41. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  42. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  43. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  45. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  46. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  47. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  48. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  49. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. # The Following flags are not checked for
  51. # -Wdeclaration-after-statement is counter to C99
  52. # AX_APPEND_COMPILE_FLAGS([-std=c++11]) -- Not ready yet
  53. # AX_APPEND_COMPILE_FLAGS([-pedantic]) -- ?
  54. # AX_APPEND_COMPILE_FLAGS([-Wstack-protector]) -- Issues on 32bit compile
  55. # AX_APPEND_COMPILE_FLAGS([-fstack-protector-all]) -- Issues on 32bit compile
  56. # AX_APPEND_COMPILE_FLAGS([-Wlong-long]) -- Don't turn on for compatibility issues memcached_stat_st
  57. # AX_APPEND_COMPILE_FLAGS([-Wold-style-definition],,[$ax_append_compile_cflags_extra])
  58. # AX_APPEND_COMPILE_FLAGS([-std=c99],,[$ax_append_compile_cflags_extra])
  59. # AX_APPEND_COMPILE_FLAGS([-Wlogical-op],,[$ax_append_compile_cflags_extra])
  60. # AX_APPEND_COMPILE_FLAGS([-fstack-check],,[$ax_append_compile_cflags_extra]) -- problems with fastmath stack size checks
  61. # AX_APPEND_COMPILE_FLAGS([-floop-parallelize-all],,[$ax_append_compile_cflags_extra]) -- causes RSA verify problem on x64
  62. # AX_APPEND_COMPILE_FLAGS([-Wunreachable-code],,[$ax_append_compile_cflags_extra]) -- older clang and when gcc had it are buggy
  63. # AX_APPEND_COMPILE_FLAGS([-fPIE],,[$ax_append_compile_cflags_extra]) -- Flag for executables not libraries
  64. #serial 4.2
  65. # changes: deleted the clearing of CFLAGS
  66. AC_DEFUN([AX_HARDEN_LINKER_FLAGS], [
  67. AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG])
  68. AC_REQUIRE([AX_VCS_CHECKOUT])
  69. AC_REQUIRE([AX_DEBUG])
  70. dnl If we are inside of VCS we append -Werror, otherwise we just use it to test other flags
  71. AX_HARDEN_LIB=
  72. ax_append_compile_link_flags_extra=
  73. AS_IF([test "x$ac_cv_vcs_checkout" = "xyes"],[
  74. AX_CHECK_LINK_FLAG([-Werror],[
  75. AX_HARDEN_LIB="-Werror $AX_HARDEN_LIB"
  76. ])
  77. ],[
  78. AX_CHECK_LINK_FLAG([-Werror],[
  79. ax_append_compile_link_flags_extra='-Werror'
  80. ])
  81. ])
  82. AX_CHECK_LINK_FLAG([-z relro -z now],[
  83. AX_HARDEN_LIB="-z relro -z now $AX_HARDEN_LIB"
  84. ],,[$ax_append_compile_link_flags_extra])
  85. AX_CHECK_LINK_FLAG([-pie],[
  86. AX_HARDEN_LIB="-pie $AX_HARDEN_LIB"
  87. ],,[$ax_append_compile_link_flags_extra])
  88. LIB="$LIB $AX_HARDEN_LIB"
  89. ])
  90. AC_DEFUN([AX_HARDEN_CC_COMPILER_FLAGS], [
  91. AX_REQUIRE_DEFINED([AX_APPEND_COMPILE_FLAGS])
  92. AC_REQUIRE([AX_HARDEN_LINKER_FLAGS])
  93. AC_LANG_PUSH([C])
  94. ac_cv_warnings_as_errors=no
  95. ax_append_compile_cflags_extra=
  96. AS_IF([test "$ac_cv_vcs_checkout" = "yes"],[
  97. AX_APPEND_COMPILE_FLAGS([-Werror])
  98. ac_cv_warnings_as_errors=yes
  99. ],[
  100. AX_APPEND_COMPILE_FLAGS([-Werror],[ax_append_compile_cflags_extra])
  101. ])
  102. AS_IF([test "$ax_enable_debug" = "yes"], [
  103. AX_APPEND_COMPILE_FLAGS([-g])
  104. AX_APPEND_COMPILE_FLAGS([-ggdb],,[$ax_append_compile_cflags_extra])
  105. AX_APPEND_COMPILE_FLAGS([-O0],,[$ax_append_compile_cflags_extra])
  106. ],[])
  107. AX_APPEND_COMPILE_FLAGS([-Wno-pragmas],,[$ax_append_compile_cflags_extra])
  108. AX_APPEND_COMPILE_FLAGS([-Wall],,[$ax_append_compile_cflags_extra])
  109. AX_APPEND_COMPILE_FLAGS([-Wextra],,[$ax_append_compile_cflags_extra])
  110. AX_APPEND_COMPILE_FLAGS([-Wunknown-pragmas],,[$ax_append_compile_cflags_extra])
  111. AX_APPEND_COMPILE_FLAGS([-Wthis-test-should-fail],,[$ax_append_compile_cflags_extra])
  112. dnl Anything below this comment please keep sorted.
  113. AS_IF([test "$CC" = "clang"],[],[
  114. AX_APPEND_COMPILE_FLAGS([--param=ssp-buffer-size=1],,[$ax_append_compile_cflags_extra])
  115. ])
  116. AX_APPEND_COMPILE_FLAGS([-Waddress],,[$ax_append_compile_cflags_extra])
  117. AX_APPEND_COMPILE_FLAGS([-Warray-bounds],,[$ax_append_compile_cflags_extra])
  118. AX_APPEND_COMPILE_FLAGS([-Wbad-function-cast],,[$ax_append_compile_cflags_extra])
  119. dnl Not in use -Wc++-compat
  120. AX_APPEND_COMPILE_FLAGS([-Wchar-subscripts],,[$ax_append_compile_cflags_extra])
  121. AX_APPEND_COMPILE_FLAGS([-Wcomment],,[$ax_append_compile_cflags_extra])
  122. AX_APPEND_COMPILE_FLAGS([-Wfloat-equal],,[$ax_append_compile_cflags_extra])
  123. AX_APPEND_COMPILE_FLAGS([-Wformat-security],,[$ax_append_compile_cflags_extra])
  124. AX_APPEND_COMPILE_FLAGS([-Wformat=2],,[$ax_append_compile_cflags_extra])
  125. AX_APPEND_COMPILE_FLAGS([-Wmaybe-uninitialized],,[$ax_append_compile_cflags_extra])
  126. AX_APPEND_COMPILE_FLAGS([-Wmissing-field-initializers],,[$ax_append_compile_cflags_extra])
  127. AX_APPEND_COMPILE_FLAGS([-Wmissing-noreturn],,[$ax_append_compile_cflags_extra])
  128. AX_APPEND_COMPILE_FLAGS([-Wmissing-prototypes],,[$ax_append_compile_cflags_extra])
  129. AX_APPEND_COMPILE_FLAGS([-Wnested-externs],,[$ax_append_compile_cflags_extra])
  130. AX_APPEND_COMPILE_FLAGS([-Wnormalized=id],,[$ax_append_compile_cflags_extra])
  131. AX_APPEND_COMPILE_FLAGS([-Woverride-init],,[$ax_append_compile_cflags_extra])
  132. AX_APPEND_COMPILE_FLAGS([-Wpointer-arith],,[$ax_append_compile_cflags_extra])
  133. AX_APPEND_COMPILE_FLAGS([-Wpointer-sign],,[$ax_append_compile_cflags_extra])
  134. dnl AX_APPEND_COMPILE_FLAGS([-Wredundant-decls],,[$ax_append_compile_cflags_extra])
  135. AX_APPEND_COMPILE_FLAGS([-Wshadow],,[$ax_append_compile_cflags_extra])
  136. AX_APPEND_COMPILE_FLAGS([-Wshorten-64-to-32],,[$ax_append_compile_cflags_extra])
  137. AX_APPEND_COMPILE_FLAGS([-Wsign-compare],,[$ax_append_compile_cflags_extra])
  138. AX_APPEND_COMPILE_FLAGS([-Wstrict-overflow=1],,[$ax_append_compile_cflags_extra])
  139. AX_APPEND_COMPILE_FLAGS([-Wstrict-prototypes],,[$ax_append_compile_cflags_extra])
  140. AX_APPEND_COMPILE_FLAGS([-Wswitch-enum],,[$ax_append_compile_cflags_extra])
  141. AX_APPEND_COMPILE_FLAGS([-Wundef],,[$ax_append_compile_cflags_extra])
  142. AX_APPEND_COMPILE_FLAGS([-Wunused],,[$ax_append_compile_cflags_extra])
  143. AX_APPEND_COMPILE_FLAGS([-Wunused-result],,[$ax_append_compile_cflags_extra])
  144. AX_APPEND_COMPILE_FLAGS([-Wunused-variable],,[$ax_append_compile_cflags_extra])
  145. AX_APPEND_COMPILE_FLAGS([-Wwrite-strings],,[$ax_append_compile_cflags_extra])
  146. AX_APPEND_COMPILE_FLAGS([-fwrapv],,[$ax_append_compile_cflags_extra])
  147. AC_LANG_POP
  148. ])
  149. AC_DEFUN([AX_HARDEN_CXX_COMPILER_FLAGS], [
  150. AC_REQUIRE_DEFINED([AX_APPEND_COMPILE_FLAGS])
  151. AC_REQUIRE([AX_HARDEN_CC_COMPILER_FLAGS])
  152. AC_LANG_PUSH([C++])
  153. ax_append_compile_cxxflags_extra=
  154. AS_IF([test "$ac_cv_warnings_as_errors" = "yes"],[
  155. AX_APPEND_COMPILE_FLAGS([-Werror])
  156. ],[
  157. AX_APPEND_COMPILE_FLAGS([-Werror],[ax_append_compile_cxxflags_extra])
  158. ])
  159. AS_IF([test "$ax_enable_debug" = "yes" ], [
  160. AX_APPEND_COMPILE_FLAGS([-g],,[$ax_append_compile_cxxflags_extra])
  161. AX_APPEND_COMPILE_FLAGS([-O0],,[$ax_append_compile_cxxflags_extra])
  162. AX_APPEND_COMPILE_FLAGS([-ggdb],,[$ax_append_compile_cxxflags_extra])
  163. ],[
  164. AX_APPEND_COMPILE_FLAGS([-D_FORTIFY_SOURCE=2],,[$ax_append_compile_cxxflags_extra])
  165. ])
  166. AS_IF([test "$ac_cv_vcs_checkout" = "yes" ], [
  167. AX_APPEND_COMPILE_FLAGS([-Werror],,[$ax_append_compile_cxxflags_extra])
  168. ],[
  169. AX_APPEND_COMPILE_FLAGS([-Wno-pragmas],,[$ax_append_compile_cxxflags_extra])
  170. ])
  171. AX_APPEND_COMPILE_FLAGS([-Wall],,[$ax_append_compile_cxxflags_extra])
  172. AX_APPEND_COMPILE_FLAGS([-Wextra],,[$ax_append_compile_cxxflags_extra])
  173. AX_APPEND_COMPILE_FLAGS([-Wunknown-pragmas],,[$ax_append_compile_cxxflags_extra])
  174. AX_APPEND_COMPILE_FLAGS([-Wthis-test-should-fail],,[$ax_append_compile_cxxflags_extra])
  175. dnl Anything below this comment please keep sorted.
  176. AX_APPEND_COMPILE_FLAGS([--param=ssp-buffer-size=1],,[$ax_append_compile_cxxflags_extra])
  177. AX_APPEND_COMPILE_FLAGS([-Waddress],,[$ax_append_compile_cxxflags_extra])
  178. AX_APPEND_COMPILE_FLAGS([-Warray-bounds],,[$ax_append_compile_cxxflags_extra])
  179. AX_APPEND_COMPILE_FLAGS([-Wchar-subscripts],,[$ax_append_compile_cxxflags_extra])
  180. AX_APPEND_COMPILE_FLAGS([-Wcomment],,[$ax_append_compile_cxxflags_extra])
  181. AX_APPEND_COMPILE_FLAGS([-Wctor-dtor-privacy],,[$ax_append_compile_cxxflags_extra])
  182. AX_APPEND_COMPILE_FLAGS([-Wfloat-equal],,[$ax_append_compile_cxxflags_extra])
  183. AX_APPEND_COMPILE_FLAGS([-Wformat=2],,[$ax_append_compile_cxxflags_extra])
  184. AX_APPEND_COMPILE_FLAGS([-Wmaybe-uninitialized],,[$ax_append_compile_cxxflags_extra])
  185. AX_APPEND_COMPILE_FLAGS([-Wmissing-field-initializers],,[$ax_append_compile_cxxflags_extra])
  186. AX_APPEND_COMPILE_FLAGS([-Wmissing-noreturn],,[$ax_append_compile_cxxflags_extra])
  187. AX_APPEND_COMPILE_FLAGS([-Wnon-virtual-dtor],,[$ax_append_compile_cxxflags_extra])
  188. AX_APPEND_COMPILE_FLAGS([-Wnormalized=id],,[$ax_append_compile_cxxflags_extra])
  189. AX_APPEND_COMPILE_FLAGS([-Woverloaded-virtual],,[$ax_append_compile_cxxflags_extra])
  190. AX_APPEND_COMPILE_FLAGS([-Wpointer-arith],,[$ax_append_compile_cxxflags_extra])
  191. dnl AX_APPEND_COMPILE_FLAGS([-Wredundant-decls],,[$ax_append_compile_cxxflags_extra])
  192. AX_APPEND_COMPILE_FLAGS([-Wshadow],,[$ax_append_compile_cxxflags_extra])
  193. AX_APPEND_COMPILE_FLAGS([-Wshorten-64-to-32],,[$ax_append_compile_cxxflags_extra])
  194. AX_APPEND_COMPILE_FLAGS([-Wsign-compare],,[$ax_append_compile_cxxflags_extra])
  195. AX_APPEND_COMPILE_FLAGS([-Wstrict-overflow=1],,[$ax_append_compile_cxxflags_extra])
  196. AX_APPEND_COMPILE_FLAGS([-Wswitch-enum],,[$ax_append_compile_cxxflags_extra])
  197. AX_APPEND_COMPILE_FLAGS([-Wundef],,[$ax_append_compile_cxxflags_extra])
  198. AX_APPEND_COMPILE_FLAGS([-Wc++11-compat],,[$ax_append_compile_cxxflags_extra])
  199. AX_APPEND_COMPILE_FLAGS([-Wunused],,[$ax_append_compile_cxxflags_extra])
  200. AX_APPEND_COMPILE_FLAGS([-Wunused-result],,[$ax_append_compile_cxxflags_extra])
  201. AX_APPEND_COMPILE_FLAGS([-Wunused-variable],,[$ax_append_compile_cxxflags_extra])
  202. AX_APPEND_COMPILE_FLAGS([-Wwrite-strings],,[$ax_append_compile_cxxflags_extra])
  203. AX_APPEND_COMPILE_FLAGS([-Wformat-security],,[$ax_append_compile_cxxflags_extra])
  204. AX_APPEND_COMPILE_FLAGS([-fwrapv],,[$ax_append_compile_cxxflags_extra])
  205. AC_LANG_POP
  206. ])
  207. AC_DEFUN([AX_HARDEN_COMPILER_FLAGS], [
  208. AC_REQUIRE([AX_HARDEN_CXX_COMPILER_FLAGS])
  209. ])
  210. AC_DEFUN([AX_CC_OTHER_FLAGS], [
  211. AX_REQUIRE_DEFINED([AX_APPEND_COMPILE_FLAGS])
  212. AC_REQUIRE([AX_HARDEN_CC_COMPILER_FLAGS])
  213. AC_LANG_PUSH([C])
  214. AX_APPEND_COMPILE_FLAGS([-pipe],,[$ax_append_compile_cflags_extra])
  215. AC_LANG_POP
  216. ])