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

Merge branch 'feature/modernize-cmake' into develop

This commit is contained in:
ClausKlein 2021-02-17 11:20:05 +01:00
commit 49fc73f82c
19 changed files with 213 additions and 102 deletions

View file

@ -40,10 +40,16 @@ parse:
flags: []
spelling: CPMFindPackage
kwargs: *cpmaddpackagekwargs
packageproject:
cpmdeclarepackage:
pargs:
nargs: '*'
flags: []
spelling: CPMDeclarePackage
kwargs: *cpmaddpackagekwargs
packageproject:
pargs:
nargs: '*'
flags: [NO_VERSION_SUFFIX]
spelling: packageProject
kwargs:
NAME: 1
@ -55,3 +61,12 @@ parse:
COMPATIBILITY: 1
VERSION_HEADER: 1
DEPENDENCIES: +
cpmusepackagelock:
pargs: 1
spelling: CPMUsePackageLock
cpmregisterpackage:
pargs: 1
spelling: CPMRegisterPackage
cpmgetpackageversion:
pargs: 2
spelling: CPMGetPackageVersion

View file

@ -1,12 +1,15 @@
---
name: Install
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
env:
CTEST_OUTPUT_ON_FAILURE: 1
@ -26,12 +29,12 @@ jobs:
- name: build and install library
run: |
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
sudo cmake --build build --target install
rm -rf build
- name: configure
run: cmake -Stest -Bbuild -DTEST_INSTALLED_VERSION=1
run: cmake -S test -B build -DTEST_INSTALLED_VERSION=1
- name: build
run: cmake --build build --config Debug -j4

View file

@ -1,12 +1,15 @@
---
name: MacOS
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
env:
CTEST_OUTPUT_ON_FAILURE: 1
@ -25,7 +28,7 @@ jobs:
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
- name: configure
run: cmake -Stest -Bbuild -DCMAKE_BUILD_TYPE=Debug
run: cmake -S test -B build -DCMAKE_BUILD_TYPE=Debug
- name: build
run: cmake --build build -j4

View file

@ -1,12 +1,13 @@
---
name: Standalone
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
env:
CPM_SOURCE_CACHE: ${{ github.workspace }}/cpm_modules
@ -24,10 +25,12 @@ jobs:
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
- name: configure
run: cmake -Sstandalone -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCMAKE_BUILD_TYPE=Debug
run: cmake -S standalone -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_BUILD_TYPE=Debug
- name: build
run: cmake --build build -j4
- name: run
run: ./build/Greeter
- name: test
run: |
cd build
ctest --build-config Debug --verbose

View file

@ -1,12 +1,15 @@
---
name: Style
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
env:
CPM_SOURCE_CACHE: ${{ github.workspace }}/cpm_modules
@ -29,7 +32,7 @@ jobs:
pip3 install cmake_format==0.6.11 pyyaml
- name: configure
run: cmake -Stest -Bbuild
run: cmake -S test -B build
- name: check style
run: cmake --build build --target check-format

View file

@ -1,12 +1,15 @@
---
name: Ubuntu
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
env:
CTEST_OUTPUT_ON_FAILURE: 1
@ -26,7 +29,7 @@ jobs:
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
- name: configure
run: cmake -Stest -Bbuild -DENABLE_TEST_COVERAGE=1 -DCMAKE_BUILD_TYPE=Debug
run: cmake -S test -B build -DENABLE_TEST_COVERAGE=1 -DCMAKE_BUILD_TYPE=Debug
- name: build
run: cmake --build build -j4

View file

@ -1,12 +1,15 @@
---
name: Windows
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
env:
CTEST_OUTPUT_ON_FAILURE: 1
@ -25,7 +28,7 @@ jobs:
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
- name: configure
run: cmake -Stest -Bbuild
run: cmake -S test -B build # XXX -G "Visual Studio 16 2019" -T ClangCL
- name: build
run: cmake --build build --config Debug -j4
@ -33,4 +36,4 @@ jobs:
- name: test
run: |
cd build
ctest --build-config Debug
ctest --build-config Debug --verbose

4
.gitignore vendored
View file

@ -1,4 +1,6 @@
stagedir/
build/
/build*
/.vscode
/cpm_modules
.DS_Store
.DS_Store

View file

@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.14...3.19)
# Note: update this to your new project's name and version
project(
Greeter
VERSION 1.0
VERSION 1.0.1
LANGUAGES CXX
)
@ -20,28 +20,21 @@ endif()
# ---- Project settings ----
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS NO)
endif()
include(cmake/options.cmake)
# ---- Add dependencies via CPM ----
# see https://github.com/cpm-cmake/CPM.cmake for more info
include(cmake/CPM.cmake)
cpmusepackagelock(package-lock.cmake)
CPMUsePackageLock(package-lock.cmake)
# PackageProject.cmake will be used to make our target installable
CPMAddPackage(
NAME PackageProject.cmake
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
VERSION 1.4.1
VERSION 1.5.0
)
# Note: If fmt is not imported, this is needed to prevent: CMake Error: install(EXPORT
# "GreeterTargets" ...) includes target "Greeter" which requires target "fmt" that is not in any
# export set. see too https://gitlab.kitware.com/cmake/cmake/-/issues/15415
CPMAddPackage(
NAME fmt
GIT_TAG 7.1.3
@ -53,31 +46,30 @@ CPMAddPackage(
# Note: globbing sources is considered bad practice as CMake's generators may not detect new files
# automatically. Keep that in mind when changing files, or explicitly mention them here.
file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
file(GLOB_RECURSE headers CONFIGURE_DEPENDS include/*.h)
file(GLOB_RECURSE sources CONFIGURE_DEPENDS source/*.cpp)
# ---- Create library ----
# 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 SHARED ${headers} ${sources})
# EITHER:
add_library(Greeter ${headers} ${sources})
set_target_properties(Greeter PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD})
# OR target_compile_features(Greeter PUBLIC cxx_std_17)
# being a cross-platform target, we enforce standards conformance on MSVC
if(MSVC)
target_compile_options(Greeter PUBLIC /permissive)
endif()
# 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>")
# Link dependencies EITHER:
target_link_libraries(Greeter PRIVATE $<BUILD_INTERFACE:fmt::fmt-header-only>)
# OR: target_link_libraries(Greeter PUBLIC fmt::fmt)
set(INCLUDE_INSTALL_DIR include/${PROJECT_NAME}-${PROJECT_VERSION})
target_include_directories(
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
Greeter BEFORE PUBLIC $<BUILD_INTERFACE: ${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
)
# ---- Create an installable target ----
@ -86,6 +78,14 @@ target_include_directories(
# the location where the project's version header will be placed should match the project's regular
# header paths
string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
string(TOLOWER ${PROJECT_NAME}/greeter_export.h EXPORT_HEADER_LOCATION)
# TODO: should be done in packageProject()
include(GenerateExportHeader)
generate_export_header(
${PROJECT_NAME} EXPORT_FILE_NAME PackageProjectInclude/${EXPORT_HEADER_LOCATION}
)
# Note: the export header will be installed while installing the version header! CK
packageProject(
NAME ${PROJECT_NAME}
@ -93,8 +93,8 @@ packageProject(
NAMESPACE ${PROJECT_NAME}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
INCLUDE_DESTINATION ${INCLUDE_INSTALL_DIR}
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
COMPATIBILITY SameMajorVersion
# NOTE: not needed DEPENDENCIES "fmt 7.1.3"
# Note: not needed DEPENDENCIES "fmt 7.1.3"
)

View file

@ -1,58 +1,69 @@
#
# CURDIR=$(/bin/pwd)
#
# GENERATOR="Unix Makefiles"
GENERATOR=Ninja
# Note: make var CURDIR:=$(/bin/pwd)
ROOT?=$(CURDIR)/stagedir
#XXX GENERATOR?="Unix Makefiles"
GENERATOR?=Ninja
#XXX export CXX=clang++
export CPM_USE_LOCAL_PACKAGES=0
export CPM_SOURCE_CACHE=${HOME}/.cache/CPM
export CPM_USE_LOCAL_PACKAGES=1
.PHONY: update format all test check clean distclean lock
.PHONY: update format all test standalone doc check clean distclean lock
# the default target does just all
check: all
# the default target does just all, but neither standalone nor doc
all:
all: test
clean:
find . -type d -name build | xargs rm -rf
test: install
distclean:
rm -rf build root
distclean: clean
rm -rf build ${ROOT}
# update CPM.cmake
update:
wget -q -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/get_cpm.cmake
lock: install
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
# install the library to stagedir
install:
cmake -S . -B build/install -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${CURDIR}/root -DCMAKE_INSTALL_PREFIX=${CURDIR}/root -DCMAKE_CXX_STANDARD=20 # --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:
cmake -S test -B build/test -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${CURDIR}/root -DTEST_INSTALLED_VERSION=1
cmake --build build/test
cmake --build build/test --target test
test: install
cmake -S $@ -B build/$@ -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DTEST_INSTALLED_VERSION=1
cmake --build build/$@
cmake --build build/$@ --target $@
# all together
all:
cmake -S all -B build/all -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${CURDIR}/root -DTEST_INSTALLED_VERSION=1 -DENABLE_TEST_COVERAGE=1
cmake --build build/all
cmake --build build/all --target test
cmake --build build/all --target GenerateDocs
cmake --build build/all --target check-format
all: test
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
# 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
find . -name '*.cmake' | xargs cmake-format -i
find . -name '*.cpp' | xargs clang-format -i
find . -name '*.h' | xargs clang-format -i
standalone:
cmake -S $@ -B build/$@ -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${ROOT} -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake --build build/$@
# check the library
check:
cmake -S standalone -B build/standalone -G "${GENERATOR}" -DCMAKE_PREFIX_PATH=${CURDIR}/root -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake --build build/standalone --target all
check: standalone
run-clang-tidy.py -p build/standalone -checks='-*,modernize-*,misc-*,hicpp-*,cert-*,readability-*,portability-*,performance-*,google-*' standalone

View file

@ -1,7 +1,7 @@
# this script adds all subprojects to a single build to allow IDEs understand the full project
# structure.
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
cmake_minimum_required(VERSION 3.14...3.19)
project(BuildAll LANGUAGES CXX)
@ -10,6 +10,6 @@ include(../cmake/tools.cmake)
# needed to generate test target
enable_testing()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../standalone ${CMAKE_BINARY_DIR}/standalone)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../test ${CMAKE_BINARY_DIR}/test)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../documentation ${CMAKE_BINARY_DIR}/documentation)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../standalone standalone)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../test test)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../documentation documentation)

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

@ -7,6 +7,15 @@ if(NOT PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
endif()
include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake)
CPMUsePackageLock(package-lock.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/options.cmake)
# option: default not set
set(USE_STATIC_ANALYZER
""
CACHE STRING "clang-tidy;cppcheck;iwyu"
)
# enables sanitizers support using the the `USE_SANITIZER` flag available values are: Address,
# Memory, MemoryWithOrigins, Undefined, Thread, Leak, 'Address;Undefined'
@ -65,6 +74,7 @@ if(USE_SANITIZER OR USE_STATIC_ANALYZER)
endif()
# enables CCACHE support through the USE_CCACHE flag possible values are: YES, NO or equivalent
option(USE_CCACHE "use ccache" YES)
if(USE_CCACHE)
CPMAddPackage(
NAME Ccache.cmake

View file

@ -5,6 +5,7 @@ project(GreeterDocs)
# ---- Dependencies ----
include(../cmake/CPM.cmake)
CPMUsePackageLock(package-lock.cmake)
CPMAddPackage(NAME Greeter SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
@ -21,16 +22,16 @@ CPMAddPackage(
set(DOXYGEN_PROJECT_NAME Greeter)
set(DOXYGEN_PROJECT_VERSION ${Greeter_VERSION})
set(DOXYGEN_PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/..")
set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doxygen")
set(DOXYGEN_OUTPUT_DIRECTORY doxygen)
configure_file(${CMAKE_CURRENT_LIST_DIR}/Doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${CMAKE_CURRENT_LIST_DIR}/Doxyfile Doxyfile)
configure_file(${CMAKE_CURRENT_LIST_DIR}/conf.py ${CMAKE_CURRENT_BINARY_DIR}/conf.py)
configure_file(${CMAKE_CURRENT_LIST_DIR}/conf.py conf.py)
add_custom_target(
GenerateDocs
${CMAKE_COMMAND} -E make_directory "${DOXYGEN_OUTPUT_DIRECTORY}"
COMMAND "${MCSS_SOURCE_DIR}/documentation/doxygen.py" "${CMAKE_CURRENT_BINARY_DIR}/conf.py"
COMMAND "${MCSS_SOURCE_DIR}/documentation/doxygen.py" conf.py
COMMAND echo "Docs written to: ${DOXYGEN_OUTPUT_DIRECTORY}"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)

View file

@ -1,5 +1,7 @@
#pragma once
#include <greeter/greeter_export.h>
#include <string>
namespace greeter {
@ -18,14 +20,15 @@ namespace greeter {
* @brief Creates a new greeter
* @param name the name to greet
*/
explicit Greeter(std::string name);
explicit GREETER_EXPORT Greeter(std::string _name);
/**
* @brief Creates a localized string containing the greeting
* @param lang the language to greet in
* @return a string containing the greeting
*/
[[nodiscard]] std::string greet(LanguageCode lang = LanguageCode::EN) const;
[[nodiscard]] auto GREETER_EXPORT greet(LanguageCode lang = LanguageCode::EN) const
-> std::string;
};
} // namespace greeter

View file

@ -9,6 +9,7 @@ include(../cmake/tools.cmake)
# ---- Dependencies ----
include(../cmake/CPM.cmake)
CPMUsePackageLock(package-lock.cmake)
CPMAddPackage(
NAME cxxopts
@ -21,14 +22,17 @@ CPMAddPackage(NAME Greeter SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
# ---- Create standalone executable ----
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
file(GLOB sources CONFIGURE_DEPENDS source/*.cpp)
add_executable(GreeterStandalone ${sources})
set_target_properties(GreeterStandalone PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD})
set_target_properties(GreeterStandalone PROPERTIES CXX_STANDARD 17 OUTPUT_NAME "Greeter")
# WORKAROUND for ALIAS target is missing error! CK
# 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

@ -8,8 +8,6 @@
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
int main(int argc, char** argv) {
// prevent warning: initialization of 'languages' with static storage duration may throw an
// exception that cannot be caught [cert-err58-cpp]
const std::unordered_map<std::string, greeter::LanguageCode> languages{
{"en", greeter::LanguageCode::EN},
{"de", greeter::LanguageCode::DE},
@ -17,8 +15,6 @@ int main(int argc, char** argv) {
{"fr", greeter::LanguageCode::FR},
};
// prevent warning: do not use pointer arithmetic
// [cppcoreguidelines-pro-bounds-pointer-arithmetic]
cxxopts::Options options(*argv, "A program to welcome the world!");
std::string language;
@ -40,7 +36,6 @@ int main(int argc, char** argv) {
return 0;
}
// prevent warning: do not use 'else' after 'return' [readability-else-after-return]
if (result["version"].as<bool>()) {
std::cout << "Greeter, version " << GREETER_VERSION << std::endl;
return 0;

View file

@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.14...3.19)
project(GreeterTests LANGUAGES CXX)
project(
GreeterTests
VERSION 1
LANGUAGES CXX
)
# ---- Options ----
@ -14,6 +18,7 @@ include(../cmake/tools.cmake)
# ---- Dependencies ----
include(../cmake/CPM.cmake)
CPMUsePackageLock(package-lock.cmake)
CPMAddPackage(
NAME doctest
@ -37,16 +42,15 @@ CPMAddPackage(
# ---- Create binary ----
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
file(GLOB sources CONFIGURE_DEPENDS source/*.cpp)
add_executable(GreeterTests ${sources})
target_link_libraries(GreeterTests doctest::doctest Greeter::Greeter)
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD})
# enable compiler warnings
if(NOT TEST_INSTALLED_VERSION)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(Greeter PUBLIC -Wall -Wpedantic -Wextra -Werror)
if(CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(Greeter PUBLIC -Wall -Wextra -Werror)
elseif(MSVC)
target_compile_options(Greeter PUBLIC /W4 /WX)
target_compile_definitions(GreeterTests PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
@ -57,7 +61,7 @@ endif()
enable_testing()
add_test(greeterTests GreeterTests)
add_test(NAME greeterTests COMMAND GreeterTests -s false)
# ---- code coverage ----

View file

@ -2,8 +2,6 @@
#include <greeter/greeter.h>
#include <greeter/version.h>
#include <string>
TEST_CASE("Greeter") {
using namespace greeter;
@ -17,10 +15,10 @@ TEST_CASE("Greeter") {
TEST_CASE("Greeter version") {
#if (__cpp_lib_starts_ends_with)
static_assert(std::string_view(GREETER_VERSION).starts_with("1.0")); // TBD C++20 only
CHECK(std::string(GREETER_VERSION).starts_with("1.0")); // SameMajorVersion
static_assert(std::string_view(GREETER_VERSION).starts_with("1")); // TBD C++20 only
CHECK(std::string(GREETER_VERSION).starts_with("1")); // SameMajorVersion
#else
static_assert(std::string_view(GREETER_VERSION) == std::string_view("1.0"));
CHECK(std::string(GREETER_VERSION) == std::string("1.0"));
static_assert(std::string_view(GREETER_VERSION) == std::string_view("1.0.1"));
CHECK(std::string(GREETER_VERSION) == std::string("1.0.1"));
#endif
}