|
1 | 1 | cmake_minimum_required(VERSION 3.15.0)
|
2 | 2 |
|
3 | 3 | project(ModbusBasic)
|
| 4 | + |
| 5 | +# Enable testing |
4 | 6 | enable_testing()
|
| 7 | + |
| 8 | +# Set up GoogleTest as a subdirectory. It assumes you have googletest located in external/googletest. |
| 9 | +# This will build GoogleTest inside the build directory as well. |
5 | 10 | add_subdirectory(external/googletest)
|
| 11 | + |
| 12 | +# Add the tests subdirectory. |
| 13 | +# This assumes that you have a 'tests' directory in your project, where your test code resides. |
6 | 14 | add_subdirectory(tests)
|
| 15 | + |
| 16 | +# Optional: Set CMake build type (Release, Debug, etc.) |
| 17 | +set(CMAKE_BUILD_TYPE Release) # You can also set it to Debug, RelWithDebInfo, etc. |
| 18 | + |
| 19 | +# Optional: Define output directories for binaries and libraries (useful for keeping everything in the build folder) |
| 20 | +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 21 | +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 22 | +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 23 | + |
| 24 | +# Function to detect changed source files using `git diff` |
| 25 | +function(GetChangedFiles changed_files) |
| 26 | + # Detect changed files that are tracked in the git repository (only .cpp, .h files) |
| 27 | + execute_process( |
| 28 | + COMMAND git diff --name-only HEAD~1 HEAD -- '*.cpp' '*.h' |
| 29 | + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 30 | + OUTPUT_VARIABLE changed_files |
| 31 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 32 | + ) |
| 33 | + # Split the output into a list of changed files |
| 34 | + string(REPLACE "\n" ";" changed_files "${changed_files}") |
| 35 | + list(REMOVE_ITEM changed_files "") # Remove empty items |
| 36 | +endfunction() |
| 37 | + |
| 38 | +# Run static analysis only on the changed files |
| 39 | +function(RunStaticAnalysisOnChangedFiles) |
| 40 | + GetChangedFiles(changed_files) |
| 41 | + |
| 42 | + if(changed_files) |
| 43 | + message(STATUS "Running static analysis on the following files: ${changed_files}") |
| 44 | + # Run tools like cpplint, cppcheck, etc. only on the changed files |
| 45 | + cpplint("${CMAKE_SOURCE_DIR}" "${changed_files}" cpplint) |
| 46 | + Cppcheck("${CMAKE_SOURCE_DIR}" "${changed_files}" cppcheck) |
| 47 | + flint("${CMAKE_SOURCE_DIR}" "${changed_files}" flint) |
| 48 | + flawfinder("${CMAKE_SOURCE_DIR}" "${changed_files}" flawfinder) |
| 49 | + cppclean("${CMAKE_SOURCE_DIR}" "${changed_files}" cppclean) |
| 50 | + clangcheck("${CMAKE_SOURCE_DIR}" "${changed_files}" clang-check) |
| 51 | + else() |
| 52 | + message(STATUS "No changes detected, skipping static analysis.") |
| 53 | + endif() |
| 54 | +endfunction() |
| 55 | + |
| 56 | +# Add a target for static analysis |
| 57 | +add_custom_target(StaticAnalysis |
| 58 | + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/run_static_analysis.cmake |
| 59 | + COMMENT "Running Static Analysis on changed files..." |
| 60 | +) |
0 commit comments