C/Cpp: Fix CMake clang-tidy, add version generator

This commit is contained in:
Jannik Beyerstedt 2021-05-17 11:33:00 +02:00
parent f6d6a34177
commit f186badabe
4 changed files with 51 additions and 13 deletions

View File

@ -11,19 +11,41 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
# set(CMAKE_VERBOSE_MAKEFILE ON)
# configure clang-tidy
find_program(CLANG_TIDY "clang-tidy")
set(CLANG_TIDY_HEADER_FILTER "src/")
set(CLANG_TIDY_CHECKS "-*,bugprone-*,cert-*,modernize-*,-modernize-use-trailing-return-type,readability-*,performance-*,llvm-*,-llvm-header-guard,google-*,-google-readability-todo,cppcoreguidelines-*,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-macro-usage,-cppcoreguidelines-non-private-member-variables-in-classes")
set(CLANG_TIDY_CHECKS "-*,bugprone-*,cert-*,modernize-*,-modernize-use-trailing-return-type,readability-*,performance-*,llvm-*,-llvm-header-guard,google-*,-google-readability-todo,cppcoreguidelines-*,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-macro-usage,-cppcoreguidelines-non-private-member-variables-in-classes,-cppcoreguidelines-pro-type-union-access,-readability-function-cognitive-complexity")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# run clang-tidy on Release builds, disable with `set_target_properties(lib_foobar PROPERTIES CXX_CLANG_TIDY "")`
if (CMAKE_BUILD_TYPE STREQUAL "Release")
if (CMAKE_BUILD_TYPE STREQUAL "Release" AND CLANG_TIDY)
set(CMAKE_CXX_CLANG_TIDY
clang-tidy;
${CLANG_TIDY};
-header-filter=${CLANG_TIDY_HEADER_FILTER};
-checks=${CLANG_TIDY_CHECKS};
)
endif()
# generate a header file with the version number from git tag/ commit
# execute_process(
# COMMAND git describe --always --tags --dirty
# OUTPUT_VARIABLE GIT_VERSION_TAG
# OUTPUT_STRIP_TRAILING_WHITESPACE
# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
# )
# execute_process(
# COMMAND git rev-parse --abbrev-ref HEAD
# OUTPUT_VARIABLE GIT_BRANCH_NAME
# OUTPUT_STRIP_TRAILING_WHITESPACE
# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
# )
# message(STATUS "Git version: ${GIT_BRANCH_NAME}, ${GIT_VERSION_TAG}")
# configure_file(
# autogen/version.h.in
# generated/version.h
# )
# set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CMAKE_CURRENT_BINARY_DIR}/generated/version.h)
## CONFIGURE LIBRARIES
@ -78,12 +100,11 @@ install(TARGETS ctestapp
# Caveat: This only works well, when all sources are added to the SOURCES variable.
# If your project is separated into multiple (internal) libraries, use the integrated
# clang-tidy runner of CMake, which is already enabled on Release builds.
find_program(CLANG_TIDY "clang-tidy")
if(CLANG_TIDY)
# get include directories and convert them to compiler flags (prepend "-I")
get_property(TIDY_INCLUDE_DIRS TARGET ctestapp PROPERTY INCLUDE_DIRECTORIES)
foreach(X IN ITEMS ${TIDY_INCLUDE_DIRS})
list(APPEND TIDY_INCLUDE_DIRS_ARG "-I${X}")
get_target_property(TIDY_INCLUDE_DIRS ctestapp INCLUDE_DIRECTORIES)
foreach(dir IN ITEMS ${TIDY_INCLUDE_DIRS})
list(APPEND TIDY_INCLUDE_DIRS_ARG "-I${dir}")
endforeach()
# add a Make target named "tidy" (to run clang-tidy manually)

17
cpp/autogen/version.h.in Normal file
View File

@ -0,0 +1,17 @@
/**
* @file version.h
* @brief Auto-generated version number preprocessor definitions
*
* These values will be calculated by CMake. Do not modify manually!
*
* @author Jannik Beyerstedt <beyerstedt@consider-it.de>
* @copyright (c) consider it GmbH, 2020
*/
#ifndef VERSION_H
#define VERSION_H
#define GIT_VERSION_TAG "@GIT_VERSION_TAG@"
#define GIT_BRANCH_NAME "@GIT_BRANCH_NAME@"
#endif /* VERSION_H */

View File

@ -1,7 +1,7 @@
/**
* @file template-file.h
* @brief File containing example of doxygen usage for quick reference.
*
*
* Here typically goes a more extensive explanation of what the header defines.
*
* @author Name <email@example.com>

View File

@ -1,7 +1,7 @@
/**
* @file template-file.h
* @brief File containing example of doxygen usage for quick reference.
*
*
* Here typically goes a more extensive explanation of what the header defines.
*
* @author Name <email@example.com>
@ -10,8 +10,8 @@
* @copyright Licensed under the GNU GPLv3 License <http://www.gnu.org/licenses/gpl-3.0.txt>
*/
#ifndef _TEMPLATE_FILE_H
#define _TEMPLATE_FILE_H
#ifndef TEMPLATE_FILE_H
#define TEMPLATE_FILE_H
// only include what is needed by this header file here!
// starting with the most generic (standard library)
@ -31,7 +31,7 @@ typedef enum BoxEnum_enum {
/**
* @brief This method adds two integers.
*
*
* Detailed explanation.
* @param a First integer to add.
* @param b Second integer to add.
@ -39,4 +39,4 @@ typedef enum BoxEnum_enum {
*/
int add(int a, int b);
#endif /* _TEMPLATE_FILE_H */
#endif /* TEMPLATE_FILE_H */