jrgnicho / cmake_common_scripts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CMake Common Scripts

This contains a collection of useful CMake macros.

This CMake macro will add clang tidy to a provided target.

Note

Each of the macros can take an ENABLE ON/OFF so they can easly be enable by external flag. If not provided it is automatically enabled.

target_clang_tidy(${PACKAGE_NAME} ARGUMENTS ${ARGN})

This configures clang-tidy with default arguments where any violation will produce compiler warnings.

target_clang_tidy(${PACKAGE_NAME} ARGUMENTS ${DEFAULT_CLANG_TIDY_WARNING_ARGS})

This configures clang-tidy with default arguments where any violation will produce compiler errors.

target_clang_tidy(${PACKAGE_NAME} ARGUMENTS ${DEFAULT_CLANG_TIDY_ERROR_ARGS})

Note

In some situation you may want to disable clang-tidy which is explained here.

This CMake macro will add IWYU to a given target

Note

Each of the macros can take an ENABLE ON/OFF so they can easly be enable by external flag. If not provided it is automatically enabled.

target_include_what_you_use(${PACKAGE_NAME} ARGUMENTS ${ARGN})

This CMake macro will add IWYU to a given target with default arguments.

target_include_what_you_use(${PACKAGE_NAME} ARGUMENTS ${DEFAULT_IWYU_ARGS})

This CMake macro will add IWYU to all targets

include_what_you_use(ARGUMENTS ${ARGN})

This CMake macro will add IWYU to all targets with default arguments.

include_what_you_use(ARGUMENTS ${DEFAULT_IWYU_ARGS})

This CMake macro will add CppCheck to a given target

Note

Each of the macros can take an ENABLE ON/OFF so they can easly be enable by external flag. If not provided it is automatically enabled.

target_cppcheck(${PACKAGE_NAME} ARGUMENTS ${ARGN})

This CMake macro will add CppCheck to a given target with default arguments.

target_cppcheck(${PACKAGE_NAME} ARGUMENTS ${DEFAULT_CPPCHECK_ARGS})

This CMake macro will add CppCheck to all targets

cppcheck(ARGUMENTS ${ARGN})

This CMake macro will add CppCheck to all targets with default arguments.

cppcheck(ARGUMENTS ${DEFAULT_CPPCHECK_ARGS})

This CMake macro simplifies the CMake package configure and install by performing multiple operations

  • It installs the provided targets
  • It exports the provided targets under the provided namespace
  • It installs the package.xml file
  • It creates and installs the ${PROJECT_NAME}-config.cmake and ${PROJECT_NAME}-config-version.cmake
configure_package(NAMESPACE <PACKAGE_NAMESPACE> TARGETS <TARGET_NAME_A> <TARGET_NAME_B>)

This CMake macro simplifies setting the CXX version for the target

target_cxx_version(${PACKAGE_NAME} <INTERFACE|PRIVATE|PUBLIC> VERSION <CXX_VERSION>)

Example: Set the version to 14 and PUBLIC.

target_cxx_version(${PACKAGE_NAME} PUBLIC VERSION 14)

This CMake macro adds a custom target that will run the tests after they are finished building. You may pass an optional argument true|false adding the ability do disable the running of tests as part of the build for CI which calls make test.

Add run test target (These will automatically run the test after build finishes)

add_run_tests_target(<TARGET_NAME>)
add_run_tests_target(<TARGET_NAME> true)

Add empty run test target

add_run_tests_target(<TARGET_NAME> false)

This CMake macro call the appropriate gtest function to add a test based on the CMake version

add_gtest_discover_tests(<TARGET_NAME>)

This CMake macro adds a custom target that will run the benchmarks after they are finished building.

Add run benchmark target (These will automatically run the benchmark after build finishes)

add_run_benchmark_target(<TARGET_NAME>)
add_run_benchmark_target(<TARGET_NAME> true)

Add empty run benchmark target

add_run_benchmark_target(<TARGET_NAME> false)

These CMake macros add code coverage.

From this point, there are two primary methods for adding instrumentation to targets: 1. A blanket instrumentation by calling add_code_coverage(), where all targets in that directory and all subdirectories are automatically instrumented. 2. Per-target instrumentation by calling target_code_coverage(<TARGET_NAME>), where the target is given and thus only that target is instrumented. This applies to both libraries and executables.

To add coverage targets, such as calling make ccov to generate the actual coverage information for perusal or consumption, call target_code_coverage(<TARGET_NAME>) on an executable target.

Note

Each of the macros can take an ENABLE ON/OFF so they can easly be enable by external flag. If not provided it is automatically enabled.

Example 1: All targets instrumented

In this case, the coverage information reported will will be that of the theLib library target and theExe executable.

1a: Via global command

add_code_coverage() # Adds instrumentation to all targets
add_library(theLib lib.cpp)
add_executable(theExe main.cpp)
target_link_libraries(theExe PRIVATE theLib)
target_code_coverage(theExe) # As an executable target, adds the 'ccov-theExe' target (instrumentation already added via global anyways) for generating code coverage reports.

1b: Via target commands

add_library(theLib lib.cpp)
target_code_coverage(theLib) # As a library target, adds coverage instrumentation but no targets.
add_executable(theExe main.cpp)
target_link_libraries(theExe PRIVATE theLib)
target_code_coverage(theExe) # As an executable target, adds the 'ccov-theExe' target and instrumentation for generating code coverage reports.

Example 2: Target instrumented, but with regex pattern of files to be excluded from report

add_executable(theExe main.cpp non_covered.cpp)
target_code_coverage(theExe EXCLUDE non_covered.cpp test/*) # As an executable target, the reports will exclude the non-covered.cpp file, and any files in a test/ folder.

Example 3: Target added to the 'ccov' and 'ccov-all' targets

add_code_coverage_all_targets(EXCLUDE test/*) # Adds the 'ccov-all' target set and sets it to exclude all files in test/ folders.
add_executable(theExe main.cpp non_covered.cpp)
target_code_coverage(theExe AUTO ALL EXCLUDE non_covered.cpp test/*) # As an executable target, adds to the 'ccov' and ccov-all' targets, and the reports will exclude the non-covered.cpp file, and any files in a test/ folder.

About

License:Apache License 2.0


Languages

Language:CMake 100.0%