cmake_minimum_required(VERSION 3.13) project(ctestapp VERSION 0.1.0) # TODO: replace the name `cpptestapp` at every occurrence in this file! # # Only do these if this is the main project, and not if it is included through add_subdirectory # if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) # include(CTest) # endif() set(CMAKE_CXX_STANDARD 14) 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,-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" AND CLANG_TIDY) set(CMAKE_CXX_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 # # example for a header only library: https://github.com/nlohmann/json # add_library(lib_json INTERFACE) # target_include_directories(lib_json INTERFACE ${PROJECT_SOURCE_DIR}/thirdparty/nlohmann_json) # # example for a compiled library: https://github.com/emilk/loguru # find_package(Threads) # add_library(lib_loguru STATIC # ${PROJECT_SOURCE_DIR}/thirdparty/loguru/loguru.cpp # ${PROJECT_SOURCE_DIR}/thirdparty/loguru/loguru.hpp # ) # target_include_directories(lib_loguru PUBLIC ${PROJECT_SOURCE_DIR}/thirdparty/loguru) # target_link_libraries(lib_loguru PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS}) # add libraries required by loguru # set_target_properties(lib_loguru PROPERTIES CXX_CLANG_TIDY "") # do not run clang-tidy here ## CONFIGURE MAIN EXECUTABLE # add our own files set(SOURCES ${PROJECT_SOURCE_DIR}/src/main.cpp # add additional sources here ) add_executable(${PROJECT_NAME} ${SOURCES}) # add libraries # target_link_libraries(${PROJECT_NAME} PRIVATE lib_json) # target_link_libraries(${PROJECT_NAME} PRIVATE lib_loguru) # define which compile target should be installed with `make install` install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin) ## CONFIGURE TESTS # # Testing only available if this is the main app # if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) AND BUILD_TESTING) # if (CMAKE_BUILD_TYPE STREQUAL "Release") # set(CMAKE_CXX_CLANG_TIDY "") # do not run clang-tidy for the tests # endif() # add_subdirectory(test) # endif() ## CONFIGURE ADDITIONAL DEVELOPMENT TARGETS # add a manual clang-tidy make target (run with `make tidy`) # 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. if(CLANG_TIDY) # get include directories and convert them to compiler flags (prepend "-I") get_target_property(TIDY_INCLUDE_DIRS ${PROJECT_NAME} 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) add_custom_target(tidy COMMAND ${CLANG_TIDY} -header-filter=${CLANG_TIDY_HEADER_FILTER} -checks=${CLANG_TIDY_CHECKS} -p=${PROJECT_BINARY_DIR} ${SOURCES} -- ${TIDY_INCLUDE_DIRS_ARG} ) endif() ## CONFIGURE DOXYGEN find_package(Doxygen) if (DOXYGEN_FOUND) set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CMAKE_CURRENT_BINARY_DIR}/docs) # set input and output files set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in) set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.out) set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.out) # request to configure the file configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY) message("Doxygen build started") # Note: do not put "ALL" - this builds docs together with application EVERY TIME! add_custom_target(docs COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating API documentation with Doxygen" VERBATIM ) else (DOXYGEN_FOUND) message("Doxygen needs to be installed to generate the documentation") endif (DOXYGEN_FOUND)