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