Arda30
Arda30

Reputation: 127

How to add cppcheck and clang-format inside cmake?

I have a project where I want to run cppcheck and clang-format using cmake when building the project. Here is my code and the output. What am I missing? The output does not run either the cppcheck and clang-format. Both programs are on my path.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

# set your relative path to the gcc compiler
SET(CMAKE_C_COMPILER "gcc")

project(eec_cmocka_example VERSION 1.0 LANGUAGES C)

include(cmake/cppcheck.cmake)
include(cmake/clang-format.cmake)
include(cmake/FetchCMocka.cmake)

file(GLOB_RECURSE TEST_SRC_FILES unit-tests/*.c)
file(GLOB_RECURSE SRC_FILES src/*.c)

add_executable(eec_cmocka_example main.c ${SRC_FILES} ${TEST_SRC_FILES})

target_compile_features(eec_cmocka_example PRIVATE c_std_99)
target_link_libraries(eec_cmocka_example PRIVATE cmocka-static)

enable_testing()
add_test(NAME eec_cmocka_example COMMAND eec_cmocka_example)

set_property(TARGET eec_cmocka_example PROPERTY LINK_FLAGS "${DEFAULT_LINK_FLAGS} -Wl,\
--wrap=get_rx_dali_flag,\
--wrap=set_led_frequency,\
--wrap=application_process")

cppcheck.cmake:

file(GLOB_RECURSE ALL_SOURCE_FILES src/*.c src/*.h)

add_custom_target(
        cppcheck 
        ALL
        COMMAND /usr/bin/cppcheck
        --enable=warning,performance,portability,information,missingInclude
        --suppress=missingIncludeSystem
        --std=c99
        --library=std.cfg
        --template="[{severity}][{id}] {message} {callstack} \(On {file}:{line}\)"
        --verbose
        --quiet
        ${ALL_SOURCE_FILES}
)

clang-format.cmake:

file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.h)

add_custom_target(
    clangformat
    ALL
    COMMAND /usr/bin/clang-format
    -style=Microsoft
    -i
    ${ALL_SOURCE_FILES}
)

Output:

arda@arda-VirtualBox:~/Desktop/eec/UnitTesting_example/build$ make clangformat
Built target clangformat
arda@arda-VirtualBox:~/Desktop/eec/UnitTesting_example/build$ make cppcheck
Built target cppcheck
arda@arda-VirtualBox:~/Desktop/eec/UnitTesting_example/build$ make
[  0%] Built target cppcheck
[  0%] Built target clangformat
Consolidate compiler generated dependencies of target cmocka-static
[ 22%] Built target cmocka-static
Consolidate compiler generated dependencies of target eec_cmocka_example
[ 77%] Built target eec_cmocka_example
Consolidate compiler generated dependencies of target cmocka
[100%] Built target cmocka
arda@arda-VirtualBox:~/Desktop/eec/UnitTesting_example/build$ 

Upvotes: 4

Views: 3196

Answers (1)

Mo_
Mo_

Reputation: 863

The clang-format code in the question works for me. As pointed out in the comments, the targets are indeed run in your case:

arda@arda-VirtualBox:~/Desktop/eec/UnitTesting_example/build$ make
[  0%] Built target cppcheck
[  0%] Built target clangformat

However, there are two things I'd do differently:

  1. Replace -i (i.e. inplace editing) with --dry-run so your build system does not change the code, but rather reports the issues. Add -Werror if you want to check for return code with $?.
  2. Don't GLOB_RECURSE over the files. You will likely pick up files you don't want formatted (build direcotry, git submodule dependencies etc.). At least specify the directories, even better be 100% explicit.

There is also an example by cmake (although they also GLOB_RECURSE...):

cmake_minimum_required(VERSION 3.28)
project(my-project)

add_executable(my-app main.c)

file(GLOB_RECURSE ALL_SOURCE_FILES
    *.c *.h *.cpp *.hpp *.cxx *.hxx *.cc *.hh *.cppm *.ipp *.ixx)
add_custom_target(format
    COMMAND clang-format
    -i
    ${ALL_SOURCE_FILES}
)

To test the run they do:

cmake -B build
cmake --build build --target format

Upvotes: 0

Related Questions