1
0
Fork 0
mirror of https://github.com/TheLartians/ModernCppStarter.git synced 2025-08-30 21:51:12 +02:00

add options.cmake project config file

common options used for every cmake project
prevent reame of standalone executable via cmake properties
This commit is contained in:
ClausKlein 2021-02-17 10:31:56 +01:00
parent 9391ce636a
commit 6ea6bdd2ea
11 changed files with 84 additions and 103 deletions

View file

@ -30,5 +30,7 @@ jobs:
- name: build
run: cmake --build build -j4
- name: run
run: ./build/bin/Greeter
- name: test
run: |
cd build
ctest --build-config Debug --verbose

View file

@ -20,20 +20,7 @@ endif()
# ---- Project settings ----
option(BUILD_SHARED_LIBS "Create shared libraries if ON" YES)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS NO)
endif()
# Set default visibility to hidden for all targets
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
include(cmake/options.cmake)
# ---- Add dependencies via CPM ----
# see https://github.com/cpm-cmake/CPM.cmake for more info
@ -67,10 +54,11 @@ file(GLOB_RECURSE sources CONFIGURE_DEPENDS source/*.cpp)
# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface
# target! EITHER: add_library(Greeter INTERFACE) OR:
add_library(Greeter ${headers} ${sources})
set_target_properties(Greeter PROPERTIES SOVERSION 1 VERSION ${PROJECT_VERSION})
set_target_properties(Greeter PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD})
# for SameMajorVersion upgrade stategie
set_target_properties(Greeter PROPERTIES SOVERSION 1 VERSION ${PROJECT_VERSION})
# being a cross-platform target, we enforce standards conformance on MSVC:
target_compile_options(Greeter PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive>")

View file

@ -1,6 +1,6 @@
#
# CURDIR=$(/bin/pwd)
ROOT?=${CURDIR}/stagedir
# Note: make var CURDIR:=$(/bin/pwd)
ROOT?=$(CURDIR)/stagedir
#XXX GENERATOR?="Unix Makefiles"
GENERATOR?=Ninja
@ -24,32 +24,35 @@ distclean: clean
update:
wget -q -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/get_cpm.cmake
lock: standalone all
lock: all standalone doc
cmake --build build/all --target cpm-update-package-lock
cmake --build build/test --target cpm-update-package-lock
cmake --build build/install --target cpm-update-package-lock
cmake --build build/standalone --target cpm-update-package-lock
cmake --build build/documentation --target cpm-update-package-lock
# install the library to stagedir
install:
cmake -S . -B build/install -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DCMAKE_INSTALL_PREFIX=${ROOT} # --trace-expand
cmake --build build/install --target install
cmake -S . -B build/$@ -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DCMAKE_INSTALL_PREFIX=${ROOT} # --trace-expand
cmake --build build/$@ --target $@
# test the library
test: install
cmake -S test -B build/test -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DTEST_INSTALLED_VERSION=1
cmake --build build/test
cmake --build build/test --target test
cmake -S $@ -B build/$@ -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DTEST_INSTALLED_VERSION=1
cmake --build build/$@
cmake --build build/$@ --target $@
# all together
all: test
cmake -S all -B build/all -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DTEST_INSTALLED_VERSION=1 -DENABLE_TEST_COVERAGE=1
cmake --build build/all
cmake --build build/all --target test
cmake --build build/all --target check-format
cmake -S $@ -B build/$@ -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DTEST_INSTALLED_VERSION=1 -DENABLE_TEST_COVERAGE=1
cmake --build build/$@
cmake --build build/$@ --target test
cmake --build build/$@ --target check-format
doc: all
cmake --build build/all --target GenerateDocs
# GenerateDocs
doc:
cmake -S documentation -B build/documentation -G "${GENERATOR}"
cmake --build build/documentation --target GenerateDocs
format: distclean
find . -name CMakeLists.txt | xargs cmake-format -i
@ -58,8 +61,8 @@ format: distclean
find . -name '*.h' | xargs clang-format -i
standalone:
cmake -S standalone -B build/standalone -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake --build build/standalone --target all
cmake -S $@ -B build/$@ -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake --build build/$@
# check the library
check: standalone

View file

@ -1,19 +0,0 @@
# CPM Package Lock This file should be committed to version control
# Ccache.cmake
CPMDeclarePackage(
Ccache.cmake
NAME Ccache.cmake
VERSION 1.2.1
GITHUB_REPOSITORY TheLartians/Ccache.cmake
)
# Format.cmake
CPMDeclarePackage(
Format.cmake
NAME Format.cmake
VERSION 1.6
GITHUB_REPOSITORY TheLartians/Format.cmake
OPTIONS "FORMAT_CHECK_CMAKE ON"
)
# MCSS (unversioned) CPMDeclarePackage(MCSS NAME MCSS GIT_TAG
# 42d4a9a48f31f5df6e246c948403b54b50574a2a DOWNLOAD_ONLY YES GITHUB_REPOSITORY mosra/m.css )

50
cmake/options.cmake Normal file
View file

@ -0,0 +1,50 @@
# only activate options for top level project
if(NOT PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
return()
endif()
option(BUILD_SHARED_LIBS "Create shared libraries" YES)
# Set default visibility to hidden for all targets
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
# build the dynamic libraries and executables together at bin directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
if(NOT DEFINED CMAKE_CXX_STANDARD)
option(CXX_STANDARD_REQUIRED "Require c++ standard" YES)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS NO)
endif()
# this reduce build time if using Nina generators
option(CMAKE_DEPENDS_IN_PROJECT_ONLY "do NOT use system header files for dependency checking" YES)
if(NOT MSVC)
if(CMAKE_DEPENDS_IN_PROJECT_ONLY)
set(CMAKE_DEPFILE_FLAGS_C
"-MMD"
CACHE STRING "dependency flag" FORCE
)
set(CMAKE_DEPFILE_FLAGS_CXX
"-MMD"
CACHE STRING "dependency flag" FORCE
)
else()
set(CMAKE_DEPFILE_FLAGS_C
"-MD"
CACHE STRING "dependency flag" FORCE
)
set(CMAKE_DEPFILE_FLAGS_CXX
"-MD"
CACHE STRING "dependency flag" FORCE
)
endif()
endif()
option(CMAKE_EXPORT_COMPILE_COMMANDS "support clang-tidy, cppcheck, ..." YES)
if(CMAKE_EXPORT_COMPILE_COMMANDS)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
endif()

View file

@ -9,15 +9,7 @@ endif()
include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake)
CPMUsePackageLock(package-lock.cmake)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS NO)
endif()
include(${CMAKE_CURRENT_LIST_DIR}/options.cmake)
# option: default not set
set(USE_STATIC_ANALYZER

View file

@ -1,9 +0,0 @@
# CPM Package Lock This file should be committed to version control
# PackageProject.cmake
CPMDeclarePackage(
PackageProject.cmake
NAME PackageProject.cmake
VERSION 1.4.1
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
)

View file

@ -25,14 +25,14 @@ CPMAddPackage(NAME Greeter SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
file(GLOB sources CONFIGURE_DEPENDS source/*.cpp)
add_executable(GreeterStandalone ${sources})
# TODO: why rename in this way? Seems only for CI with GitHub actions! CK
set_target_properties(
GreeterStandalone PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD} OUTPUT_NAME "Greeter"
)
set_target_properties(GreeterStandalone PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD})
# WORKAROUND missing ALIAS target error! CK
if(NOT TARGET cxxopts::cxxopts)
add_library(cxxopts::cxxopts ALIAS cxxopts)
endif()
target_link_libraries(GreeterStandalone Greeter::Greeter cxxopts::cxxopts)
# ---- run the standalone executable ----
enable_testing()
add_test(NAME GreeterStandalone COMMAND GreeterStandalone)

View file

@ -1,9 +0,0 @@
# CPM Package Lock This file should be committed to version control
# Ccache.cmake
CPMDeclarePackage(
Ccache.cmake
NAME Ccache.cmake
VERSION 1.2.1
GITHUB_REPOSITORY TheLartians/Ccache.cmake
)

View file

@ -61,7 +61,7 @@ endif()
enable_testing()
add_test(NAME greeterTests COMMAND GreeterTests)
add_test(NAME greeterTests COMMAND GreeterTests -s false)
# ---- code coverage ----

View file

@ -1,17 +0,0 @@
# CPM Package Lock This file should be committed to version control
# Ccache.cmake
CPMDeclarePackage(
Ccache.cmake
NAME Ccache.cmake
VERSION 1.2.1
GITHUB_REPOSITORY TheLartians/Ccache.cmake
)
# Format.cmake
CPMDeclarePackage(
Format.cmake
NAME Format.cmake
VERSION 1.6
GITHUB_REPOSITORY TheLartians/Format.cmake
OPTIONS "FORMAT_CHECK_CMAKE ON"
)