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) # set(CMAKE_VERBOSE_MAKEFILE ON)
# configure clang-tidy # configure clang-tidy
find_program(CLANG_TIDY "clang-tidy")
set(CLANG_TIDY_HEADER_FILTER "src/") 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) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# run clang-tidy on Release builds, disable with `set_target_properties(lib_foobar PROPERTIES CXX_CLANG_TIDY "")` # 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 set(CMAKE_CXX_CLANG_TIDY
clang-tidy; ${CLANG_TIDY};
-header-filter=${CLANG_TIDY_HEADER_FILTER}; -header-filter=${CLANG_TIDY_HEADER_FILTER};
-checks=${CLANG_TIDY_CHECKS}; -checks=${CLANG_TIDY_CHECKS};
) )
endif() 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 ## CONFIGURE LIBRARIES
@ -78,12 +100,11 @@ install(TARGETS ctestapp
# Caveat: This only works well, when all sources are added to the SOURCES variable. # 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 # If your project is separated into multiple (internal) libraries, use the integrated
# clang-tidy runner of CMake, which is already enabled on Release builds. # clang-tidy runner of CMake, which is already enabled on Release builds.
find_program(CLANG_TIDY "clang-tidy")
if(CLANG_TIDY) if(CLANG_TIDY)
# get include directories and convert them to compiler flags (prepend "-I") # get include directories and convert them to compiler flags (prepend "-I")
get_property(TIDY_INCLUDE_DIRS TARGET ctestapp PROPERTY INCLUDE_DIRECTORIES) get_target_property(TIDY_INCLUDE_DIRS ctestapp INCLUDE_DIRECTORIES)
foreach(X IN ITEMS ${TIDY_INCLUDE_DIRS}) foreach(dir IN ITEMS ${TIDY_INCLUDE_DIRS})
list(APPEND TIDY_INCLUDE_DIRS_ARG "-I${X}") list(APPEND TIDY_INCLUDE_DIRS_ARG "-I${dir}")
endforeach() endforeach()
# add a Make target named "tidy" (to run clang-tidy manually) # 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

@ -10,8 +10,8 @@
* @copyright Licensed under the GNU GPLv3 License <http://www.gnu.org/licenses/gpl-3.0.txt> * @copyright Licensed under the GNU GPLv3 License <http://www.gnu.org/licenses/gpl-3.0.txt>
*/ */
#ifndef _TEMPLATE_FILE_H #ifndef TEMPLATE_FILE_H
#define _TEMPLATE_FILE_H #define TEMPLATE_FILE_H
// only include what is needed by this header file here! // only include what is needed by this header file here!
// starting with the most generic (standard library) // starting with the most generic (standard library)
@ -39,4 +39,4 @@ typedef enum BoxEnum_enum {
*/ */
int add(int a, int b); int add(int a, int b);
#endif /* _TEMPLATE_FILE_H */ #endif /* TEMPLATE_FILE_H */