|
1 | | -cmake_minimum_required(VERSION 3.15.0) |
| 1 | +cmake_minimum_required(VERSION 3.15) |
| 2 | + |
| 3 | +project(modbus_basic_tests) |
2 | 4 |
|
3 | 5 | set(TargetName modbus_basic_tests) |
4 | | -project(${TargetName}) |
5 | 6 |
|
6 | 7 | add_executable(${TargetName}) |
7 | 8 |
|
8 | | -function(Generate name) |
9 | | - set(source_name "${name}_generated.h") |
10 | | - set_source_files_properties(${source_name} GENERATED) |
11 | | - target_sources(${TargetName} PUBLIC ${source_name}) |
12 | | - add_custom_command( |
13 | | - COMMENT "Generating source ${name}" |
14 | | - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
15 | | - DEPENDS "${name}.fbs" |
16 | | - OUTPUT "${name}_generated.h" |
17 | | - COMMAND flatc --cpp --grpc --cpp-std=c++17 --cpp-ptr-type naked --gen-mutable --scoped-enums --gen-name-strings --cpp-static-reflection --gen-object-api |
18 | | - ARGS ${name}.fbs |
| 9 | +# === FlatBuffers Code Generation === |
| 10 | +function(GenerateFlatbuffers name) |
| 11 | + set(source_name "${name}_generated.h") |
| 12 | + set_source_files_properties(${source_name} GENERATED) |
| 13 | + target_sources(${TargetName} PRIVATE ${source_name}) |
| 14 | + |
| 15 | + add_custom_command( |
| 16 | + COMMENT "Generating source ${name}" |
| 17 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 18 | + DEPENDS "${name}.fbs" |
| 19 | + OUTPUT "${source_name}" |
| 20 | + COMMAND flatc |
| 21 | + ARGS --cpp --grpc --cpp-std=c++17 --cpp-ptr-type naked --gen-mutable |
| 22 | + --scoped-enums --gen-name-strings --cpp-static-reflection |
| 23 | + --gen-object-api ${name}.fbs |
19 | 24 | ) |
20 | 25 | endfunction() |
21 | 26 |
|
22 | | -Generate("input_registers") |
23 | | -Generate("holding_registers") |
24 | | - |
25 | | - |
26 | | -target_include_directories(${TargetName} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include") |
| 27 | +GenerateFlatbuffers("input_registers") |
| 28 | +GenerateFlatbuffers("holding_registers") |
27 | 29 |
|
28 | | -#SET(CMAKE_CXX_COMPILER clang++-9) |
29 | | -#SET(CMAKE_CXX_COMPILER /usr/bin/g++-8) |
30 | | -option(RUN_SANITIZER ON) |
31 | | - |
32 | | -if(${RUN_SANITIZER}) |
33 | | -target_compile_options( |
34 | | -${TargetName} |
35 | | -PRIVATE |
36 | | - -fsanitize=address |
37 | | - -fno-omit-frame-pointer |
38 | | - -fsanitize=undefined) |
| 30 | +# === Compiler Features and Flags === |
| 31 | +set_property(TARGET ${TargetName} PROPERTY CXX_STANDARD 17) |
| 32 | +target_compile_features(${TargetName} PUBLIC cxx_std_17) |
39 | 33 |
|
40 | | -target_link_libraries( ${TargetName} asan) |
41 | | -#target_link_libraries( ${TargetName} tsan) |
42 | | -target_link_libraries( ${TargetName} ubsan) |
43 | | -#target_link_libraries( ${TargetName} msan) |
44 | | -endif() |
| 34 | +target_compile_definitions(${TargetName} |
| 35 | + PRIVATE |
| 36 | + TESTING |
| 37 | + UNITTESTING |
| 38 | + LINUX |
| 39 | +) |
45 | 40 |
|
46 | | -target_compile_options( |
47 | | -${TargetName} |
48 | | -PRIVATE |
49 | | - -Wall |
50 | | - -Wextra |
51 | | - -pedantic |
52 | | - -Wpedantic |
53 | | - #-pedantic-errors |
54 | | - #-Werror=pedantic |
55 | | - |
56 | | - -Wconversion |
57 | | - $<$<COMPILE_LANGUAGE:C>:-Wimplicit> |
58 | | - $<$<COMPILE_LANGUAGE:C>:-Wunused> |
59 | | - |
60 | | - #$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions> |
61 | | - #$<$<COMPILE_LANGUAGE:CXX>:-Wabi> |
62 | | - $<$<CXX_COMPILER_ID:GNU>:-Wsuggest-final-types> |
63 | | - $<$<CXX_COMPILER_ID:GNU>:-Wmaybe-uninitialized> |
64 | | - $<$<CXX_COMPILER_ID:GNU>:-Wdouble-promotion> |
65 | | - $<$<CXX_COMPILER_ID:GNU>:-Wnull-dereference> |
66 | | - -Winit-self |
67 | | - |
68 | | - $<$<CXX_COMPILER_ID:GNU>:-Wduplicated-cond> |
69 | | - $<$<CXX_COMPILER_ID:GNU>:-Wduplicated-branches> |
70 | | - $<$<CXX_COMPILER_ID:GNU>:-Wlogical-op> |
71 | | - $<$<CXX_COMPILER_ID:GNU>:-Wrestrict> |
72 | | - $<$<C_COMPILER_ID:GNU>:$<$<COMPILE_LANGUAGE:C>:-Wjump-misses-init>> |
73 | | - $<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast> |
74 | | - -Wshadow |
| 41 | +target_compile_options(${TargetName} |
| 42 | + PRIVATE |
| 43 | + -Wall |
| 44 | + -Wextra |
| 45 | + -pedantic |
| 46 | + -Wconversion |
| 47 | + -Wshadow |
| 48 | + $<$<CXX_COMPILER_ID:GNU>:-Wsuggest-final-types> |
| 49 | + $<$<CXX_COMPILER_ID:GNU>:-Wmaybe-uninitialized> |
| 50 | + $<$<CXX_COMPILER_ID:GNU>:-Wdouble-promotion> |
| 51 | + $<$<CXX_COMPILER_ID:GNU>:-Wnull-dereference> |
| 52 | + $<$<CXX_COMPILER_ID:GNU>:-Wduplicated-cond> |
| 53 | + $<$<CXX_COMPILER_ID:GNU>:-Wduplicated-branches> |
| 54 | + $<$<CXX_COMPILER_ID:GNU>:-Wlogical-op> |
| 55 | + $<$<CXX_COMPILER_ID:GNU>:-Wrestrict> |
| 56 | + $<$<COMPILE_LANGUAGE:C>:-Wimplicit> |
| 57 | + $<$<COMPILE_LANGUAGE:C>:-Wunused> |
| 58 | + $<$<C_COMPILER_ID:GNU>:$<$<COMPILE_LANGUAGE:C>:-Wjump-misses-init>> |
| 59 | + $<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast> |
| 60 | + -Winit-self |
75 | 61 | ) |
76 | 62 |
|
77 | | -target_compile_features(${TargetName} PUBLIC cxx_std_17) |
78 | | -target_compile_definitions(${TargetName} PRIVATE TESTING) |
79 | | -target_compile_definitions(${TargetName} PRIVATE UNITTESTING) |
80 | | -target_compile_definitions(${TargetName} PRIVATE LINUX) |
81 | | -set_property(TARGET ${TargetName} PROPERTY CXX_STANDARD 17) |
82 | | -#add_definitions(-D__CORE_CMINSTR_H) |
83 | | -#add_definitions(-D__CORE_CMINSTR_H) |
84 | | -#add_definitions(-D__GNUC__) |
| 63 | +# === Source Files === |
| 64 | +set(TestSourceDir ${CMAKE_CURRENT_SOURCE_DIR}/source) |
| 65 | +file(GLOB_RECURSE sources "${TestSourceDir}/*.cpp") |
85 | 66 |
|
86 | | -#target_compile_options(${TargetName} PRIVATE -fpermissive) |
87 | | -#target_compile_options(-fprofile-arcs -ftest-coverage) |
| 67 | +target_sources(${TargetName} |
| 68 | + PRIVATE |
| 69 | + ${sources} |
| 70 | + ${TestSourceDir}/main.cpp |
| 71 | +) |
88 | 72 |
|
89 | | -#SET(GCC_COVERAGE_LINK_FLAGS "-coverage -lgcov") |
90 | | -#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") |
| 73 | +# === Include Directories === |
| 74 | +set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include) |
| 75 | +set(EXTERNAL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../external) |
| 76 | + |
| 77 | +target_include_directories(${TargetName} |
| 78 | + PUBLIC |
| 79 | + ${CMAKE_CURRENT_SOURCE_DIR} |
| 80 | + ${TestSourceDir} |
| 81 | + ${INCLUDE_DIR} |
| 82 | + ${INCLUDE_DIR}/CppUtilities/include |
| 83 | + ${INCLUDE_DIR}/CppUtilities/include/sprout |
| 84 | + ${INCLUDE_DIR}/DataStores/include |
| 85 | + ${EXTERNAL_DIR}/flatbuffers/include |
| 86 | +) |
91 | 87 |
|
92 | | -#set(FSL_FEATURE_ADC_HAS_CTRL_LPWRMODE true) |
93 | | -SET(CMAKE_VERBOSE_MAKEFILE ON) |
| 88 | +# === Sanitizers === |
| 89 | +option(RUN_SANITIZER "Enable address/UB sanitizers" ON) |
94 | 90 |
|
95 | | -#fixme this should have all the sources |
96 | | -set(TestSource ${CMAKE_CURRENT_SOURCE_DIR}/source) |
97 | | -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source sources) |
| 91 | +if(RUN_SANITIZER) |
| 92 | + target_compile_options(${TargetName} |
| 93 | + PRIVATE |
| 94 | + -fsanitize=address |
| 95 | + -fno-omit-frame-pointer |
| 96 | + -fsanitize=undefined |
| 97 | + ) |
| 98 | + target_link_libraries(${TargetName} |
| 99 | + PRIVATE |
| 100 | + asan |
| 101 | + ubsan |
| 102 | + ) |
| 103 | +endif() |
98 | 104 |
|
99 | | -target_sources(${TargetName} PUBLIC ${sources}) |
100 | | -target_sources(${TargetName} PUBLIC ${TestSource}/main.cpp) |
| 105 | +# === GoogleTest and pthread === |
| 106 | +find_library(PTHREAD_LIB pthread REQUIRED) |
101 | 107 |
|
| 108 | +target_link_libraries(${TargetName} |
| 109 | + PRIVATE |
| 110 | + gtest |
| 111 | + gtest_main |
| 112 | + ${PTHREAD_LIB} |
| 113 | +) |
102 | 114 |
|
103 | | -#find_library(GTEST_LIB NAMES gtest) |
104 | 115 | find_library(GTEST_LIB gtest REQUIRED) |
105 | | -#set(GTEST_LIB "/usr/src/googletest/lib/libgtest.a") |
106 | | -message("GTEST: ${GTEST_LIB}") |
107 | 116 |
|
108 | | -find_library(PTHREAD_LIB pthread) |
109 | | -message("PTHREAD: ${PTHREAD_LIB}") |
110 | | -target_link_libraries(${TargetName} ${GTEST_LIB}) |
111 | | -#target_link_libraries(${TargetName} gtest) |
112 | | -target_link_libraries(${TargetName} ${PTHREAD_LIB}) |
113 | | - |
114 | | -set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include) |
115 | | -include_directories(${TargetName} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) |
116 | | -include_directories(${TargetName} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/source) |
117 | | -include_directories(${TargetName} PUBLIC ${INCLUDE_DIR}) |
118 | | -target_include_directories(${TargetName} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/source) |
119 | | -include_directories(${TargetName} PUBLIC ${INCLUDE_DIR}/CppUtilities/include) |
120 | | -target_include_directories(${TargetName} PUBLIC ${INCLUDE_DIR}/CppUtilities/include/sprout) |
121 | | -target_include_directories(${TargetName} PUBLIC ${INCLUDE_DIR}/DataStores/include) |
122 | | -target_include_directories(${TargetName} PUBLIC /home/simon/software/flatbuffers/include) |
123 | 117 |
|
| 118 | +# === Static Analysis === |
124 | 119 | if(RUN_ANALYSIS) |
125 | | - include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeStaticAnalysis/StaticAnalysis.cmake) |
126 | | - include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeStaticAnalysis/Utilities.cmake) |
127 | | -GetStaticAnalysisFiles(${TargetName} "${StaticExcludes}") |
128 | | -message(STATUS "Analyze ${AnalyseFiles}") |
129 | | -StaticAnalysis("${AnalyseFiles}") |
| 120 | + include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeStaticAnalysis/StaticAnalysis.cmake) |
| 121 | + include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeStaticAnalysis/Utilities.cmake) |
| 122 | + GetStaticAnalysisFiles(${TargetName} "${StaticExcludes}") |
| 123 | + message(STATUS "Analyze ${AnalyseFiles}") |
| 124 | + StaticAnalysis("${AnalyseFiles}") |
130 | 125 | endif() |
131 | 126 |
|
| 127 | +# === Enable Test === |
| 128 | +enable_testing() |
132 | 129 | add_test(NAME ${TargetName} COMMAND ${TargetName}) |
| 130 | + |
| 131 | +set(CMAKE_VERBOSE_MAKEFILE ON) |
0 commit comments