CompilerFlags.cmake 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. include(AddIfFlagCompiles)
  2. # Makes +/- operations on void pointers be considered an error
  3. # https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html
  4. add_if_flag_compiles(-Werror=pointer-arith CMAKE_C_FLAGS)
  5. # Generates error whenever a function is used before being declared
  6. # https://gcc.gnu.org/onlinedocs/gcc-4.0.1/gcc/Warning-Options.html
  7. add_if_flag_compiles(-Werror=implicit-function-declaration CMAKE_C_FLAGS)
  8. # Allows some casting of pointers without generating a warning
  9. add_if_flag_compiles(-fno-strict-aliasing CMAKE_C_FLAGS)
  10. if (ENABLE_MSAN AND ENABLE_ASAN)
  11. # MSAN and ASAN both work on memory - ASAN does more things
  12. MESSAGE(WARNING "Compiling with both AddressSanitizer and MemorySanitizer is not recommended")
  13. endif()
  14. if (ENABLE_ASAN)
  15. # If enabled it would generate errors/warnings for all kinds of memory errors
  16. # (like returning a stack variable by reference)
  17. # https://clang.llvm.org/docs/AddressSanitizer.html
  18. add_if_flag_compiles(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS)
  19. add_if_flag_compiles(-fsanitize=address CMAKE_C_FLAGS CMAKE_LINKER_FLAGS)
  20. endif()
  21. if (ENABLE_UBSAN)
  22. # If enabled this will generate errors for undefined behavior points
  23. # (like adding +1 to the maximum int value)
  24. # https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
  25. add_if_flag_compiles(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS)
  26. add_if_flag_compiles(-fsanitize=undefined CMAKE_C_FLAGS CMAKE_LINKER_FLAGS)
  27. endif()
  28. if (ENABLE_MSAN)
  29. # If enabled this will generate warnings for places where uninitialized memory is used
  30. # https://clang.llvm.org/docs/MemorySanitizer.html
  31. add_if_flag_compiles(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS)
  32. add_if_flag_compiles(-fsanitize=memory CMAKE_C_FLAGS CMAKE_LINKER_FLAGS)
  33. endif()
  34. if(CMAKE_VERSION VERSION_LESS "3.1")
  35. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  36. add_if_flag_compiles(-std=gnu99 CMAKE_C_FLAGS)
  37. endif()
  38. else()
  39. set (CMAKE_C_STANDARD 99)
  40. endif()
  41. if(${PLATFORM} MATCHES "Android")
  42. # If enabled will remove dead code during the linking process
  43. # https://gcc.gnu.org/onlinedocs/gnat_ugn/Compilation-options.html
  44. add_if_flag_compiles(-ffunction-sections CMAKE_C_FLAGS)
  45. # If enabled will generate some exception data (usually disabled for C programs)
  46. # https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Code-Gen-Options.html
  47. add_if_flag_compiles(-funwind-tables CMAKE_C_FLAGS)
  48. # If enabled adds stack protection guards around functions that allocate memory
  49. # https://www.keil.com/support/man/docs/armclang_ref/armclang_ref_cjh1548250046139.htm
  50. add_if_flag_compiles(-fstack-protector-strong CMAKE_C_FLAGS)
  51. # Marks that the library will not be compiled with an executable stack
  52. add_if_flag_compiles(-Wa,--noexecstack CMAKE_C_FLAGS)
  53. # Do not expand symbolic links or resolve paths like "/./" or "/../", etc.
  54. # https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html
  55. add_if_flag_compiles(-no-canonical-prefixes CMAKE_C_FLAGS)
  56. endif()