A general-purpose CMake library that makes using CMake easier
NOTE: It is planned to transfer this repository to cpp-best-practices organization. Stay tuned for that.
cmake_minimum_required(VERSION 3.16)
# Set the project name to your project name, my_project isn't very descriptive
project(myproject LANGUAGES CXX)
# Add cmakelib
include(FetchContent)
FetchContent_Declare(cmakelib URL https://github.com/aminya/cmakelib/archive/refs/heads/main.zip)
FetchContent_MakeAvailable(cmakelib)
include(${cmakelib_SOURCE_DIR}/Index.cmake)
add_library(${PROJECT_NAME} INTERFACE)
# Initialize cmakelib
# uncomment the options to enable them
cmakelib(
ENABLE_CACHE
ENABLE_CONAN
# WARNINGS_AS_ERRORS
# ENABLE_CPPCHECK
# ENABLE_CLANG_TIDY
# ENABLE_INCLUDE_WHAT_YOU_USE
# ENABLE_COVERAGE
# ENABLE_PCH
# ENABLE_DOXYGEN
# ENABLE_IPO
# ENABLE_USER_LINKER
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
# ENABLE_SANITIZER_ADDRESS
# ENABLE_SANITIZER_LEAK
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
# ENABLE_SANITIZER_THREAD
# ENABLE_SANITIZER_MEMORY
)
# project_options is defined inside cmakelib
target_compile_features(project_options INTERFACE cxx_std_17)
target_link_libraries(${PROJECT_NAME} INTERFACE project_options project_warnings)
# add src, tests, etc here:
add_executable(myprogram main.cpp)
target_link_libraries(myprogram PRIVATE ${PROJECT_NAME})
# ...
WARNINGS_AS_ERRORS
: Treat compiler warnings as errorsENABLE_CPPCHECK
: Enable static analysis with CppcheckENABLE_CLANG_TIDY
: Enable static analysis with clang-tidyENABLE_INCLUDE_WHAT_YOU_USE
: Enable static analysis with include-what-you-useENABLE_COVERAGE
: Enable coverage reporting for gcc/clangENABLE_CACHE
: Enable cache if availableENABLE_PCH
: Enable Precompiled HeadersENABLE_CONAN
: Use Conan for dependency managementENABLE_DOXYGEN
: Enable Doxygen doc builds of sourceENABLE_IPO
: Enable Interprocedural Optimization, aka Link Time Optimization (LTO)ENABLE_USER_LINKER
: Enable a specific linker if availableENABLE_BUILD_WITH_TIME_TRACE
: Enable-ftime-trace
to generate time tracing.json
files on clangENABLE_UNITY
: Enable Unity builds of projectsENABLE_SANITIZER_ADDRESS
: Enable address sanitizerENABLE_SANITIZER_LEAK
: Enable leak sanitizerENABLE_SANITIZER_UNDEFINED_BEHAVIOR
: Enable undefined behavior sanitizerENABLE_SANITIZER_THREAD
: Enable thread sanitizerENABLE_SANITIZER_MEMORY
: Enable memory sanitizerMSVC_WARNINGS
: Override the defaults for the MSVC warningsCLANG_WARNINGS
: Override the defaults for the CLANG warningsGCC_WARNINGS
: Override the defaults for the GCC warnings
However, if you still want to change the CMake options on the fly (e.g. to enable sanitizers inside CI), you can include the GlobalOptions.cmake
, which adds global options for the arguments of cmakelib
function.
cmake_minimum_required(VERSION 3.16)
# Set the project name to your project name, my_project isn't very descriptive
project(myproject LANGUAGES CXX)
# Add cmakelib
include(FetchContent)
FetchContent_Declare(cmakelib URL https://github.com/aminya/cmakelib/archive/refs/heads/main.zip)
FetchContent_MakeAvailable(cmakelib)
include(${cmakelib_SOURCE_DIR}/Index.cmake)
add_library(${PROJECT_NAME} INTERFACE)
# Add global CMake options
include(${cmakelib_SOURCE_DIR}/src/GlobalOptions.cmake)
# Initialize cmakelib
# uncomment the options to enable them
cmakelib(
ENABLE_CACHE
ENABLE_CONAN
# WARNINGS_AS_ERRORS
# ENABLE_CPPCHECK
# ENABLE_CLANG_TIDY
# ENABLE_INCLUDE_WHAT_YOU_USE
# ENABLE_COVERAGE
# ENABLE_PCH
# ENABLE_DOXYGEN
# ENABLE_IPO
# ENABLE_USER_LINKER
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
${ENABLE_SANITIZER_ADDRESS} # ❗ Now, the address sanitizer is enabled through CMake options
# ENABLE_SANITIZER_LEAK
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
# ENABLE_SANITIZER_THREAD
# ENABLE_SANITIZER_MEMORY
)
# project_options is defined inside cmakelib
target_compile_features(project_options INTERFACE cxx_std_17)
target_link_libraries(${PROJECT_NAME} INTERFACE project_options project_warnings)
# add src, tests, etc here:
add_executable(myprogram main.cpp)
target_link_libraries(myprogram PRIVATE ${PROJECT_NAME})