/ CMakeLists.txt
CMakeLists.txt
1 2 cmake_minimum_required(VERSION 3.23) 3 4 project(C_Skeleton 5 VERSION 0.1.0 6 ) 7 8 option(USE_MISRA "Use MISRA checks" Off) 9 option(USE_CLANG_TIDY "Use clang-tidy checks" On) 10 11 set(CMAKE_EXPORT_COMPILE_COMMANDS true) 12 13 if(USE_CLANG_TIDY) 14 set(CMAKE_CXX_CLANG_TIDY clang-tidy-devel -config-file=.clang-tidy.yaml -format-style=file) 15 set(CMAKE_C_CLANG_TIDY clang-tidy-devel -config-file=.clang-tidy.yaml -format-style=file) 16 endif(USE_CLANG_TIDY) 17 18 19 if(CMAKE_C_COMPILER_ID STREQUAL "GNU") 20 21 message("Found GNU C compiler") 22 23 set(PIE_COMPILE_OPTS -fPIE -pie) 24 set(PIE_LINK_OPTS -fPIE -pie -Wl,-z,relro,-z,now) 25 26 set( 27 SAFETY_COMPILE_OPTS 28 -D_FORTIFY_SOURCE=3 29 -D_GLIBCXX_ASSERTIONS 30 -ftrivial-auto-var-init=zero 31 -fstack-protector-all 32 -fstack-clash-protection 33 -fcf-protection=full 34 ) 35 36 set( 37 SAFETY_LINK_OPTS 38 -fstack-protector-all 39 -fstack-clash-protection 40 -fcf-protection=full 41 -fzero-call-used-regs=all 42 ) 43 44 else(CMAKE_C_COMPILER_ID STREQUAL "GNU") # NOT gcc 45 46 message("Found other C compiler") 47 48 set(PIE_COMPILE_OPTS -fPIE) 49 set(PIE_LINK_OPTS -fPIE -Wl,-z,relro,-z,now) 50 51 set( 52 SAFETY_COMPILE_OPTS 53 -D_FORTIFY_SOURCE=3 54 -D_GLIBCXX_ASSERTIONS 55 -ftrivial-auto-var-init=zero 56 -fstack-protector-all 57 -fstack-clash-protection 58 -fcf-protection=full 59 ) 60 61 set( 62 SAFETY_LINK_OPTS 63 -fstack-protector-all 64 -fstack-clash-protection 65 -fcf-protection=full 66 -fzero-call-used-regs=all 67 ) 68 69 endif(CMAKE_C_COMPILER_ID STREQUAL "GNU") 70 71 add_executable( 72 a_program 73 src/main.c 74 ) 75 76 target_compile_options( 77 a_program 78 PRIVATE 79 -std=c23 80 -Wall 81 -Werror 82 -g 83 -std=gnu17 84 ${PIE_COMPILE_OPTS} 85 ${SAFETY_COMPILE_OPTS} 86 -I/usr/local/include 87 ) 88 89 target_link_libraries( 90 a_program 91 ${PIE_LINK_OPTS} 92 ${SAFETY_LINK_OPTS} 93 -L/usr/local/lib 94 pthread 95 rt 96 ) 97 98 if(USE_MISRA) 99 100 add_custom_target(check 101 COMMAND 102 cppcheck --addon=.misra.json --enable=all --inline-suppr src 103 VERBATIM 104 ) 105 106 else(USE_MISRA) 107 108 add_custom_target(check 109 COMMAND 110 cppcheck --enable=all --inline-suppr src 111 VERBATIM 112 ) 113 114 endif(USE_MISRA)