mirror of
https://github.com/TheLartians/ModernCppStarter.git
synced 2025-08-30 21:51:12 +02:00
Merge pull request #1 from ClausKlein/feature/extend-build-dependency
Feature/extend build dependency
This commit is contained in:
commit
d4c654f026
12 changed files with 171 additions and 44 deletions
26
.clang-tidy
Normal file
26
.clang-tidy
Normal file
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
Checks: "-*,\
|
||||
boost-*,\
|
||||
cert-*,\
|
||||
clang-analyzer-*,\
|
||||
cppcoreguidelines-*,\
|
||||
-cppcoreguidelines-macro-usage,\
|
||||
-cppcoreguidelines-owning-memory,\
|
||||
-hicpp-*,\
|
||||
misc-*,\
|
||||
-misc-non-private-member-variables-in-classes,\
|
||||
-modernize-*,\
|
||||
-modernize-use-trailing-return-type,\
|
||||
performance-*,\
|
||||
portability-*,\
|
||||
readability-*,\
|
||||
-*magic-numbers,\
|
||||
-*avoid-c-arrays,\
|
||||
"
|
||||
WarningsAsErrors: ''
|
||||
HeaderFilterRegex: '.*'
|
||||
AnalyzeTemporaryDtors: false
|
||||
FormatStyle: file
|
||||
User: clausklein
|
||||
...
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
cmake_minimum_required(VERSION 3.14...3.19)
|
||||
|
||||
# ---- Project ----
|
||||
|
||||
|
@ -18,16 +18,35 @@ if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
|
|||
)
|
||||
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()
|
||||
|
||||
# ---- Add dependencies via CPM ----
|
||||
# see https://github.com/TheLartians/CPM.cmake for more info
|
||||
# see https://github.com/cpm-cmake/CPM.cmake for more info
|
||||
|
||||
include(cmake/CPM.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
|
||||
VERSION 1.4.1
|
||||
)
|
||||
|
||||
# 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
|
||||
GITHUB_REPOSITORY fmtlib/fmt # to get an installable target
|
||||
OPTIONS "FMT_INSTALL YES"
|
||||
)
|
||||
|
||||
# ---- Add source files ----
|
||||
|
@ -40,17 +59,21 @@ file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/
|
|||
# ---- Create library ----
|
||||
|
||||
# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface
|
||||
# target: add_library(Greeter INTERFACE) set_target_properties(Greeter PROPERTIES
|
||||
# INTERFACE_COMPILE_FEATURES cxx_std_17)
|
||||
# target! EITHER: add_library(Greeter INTERFACE) OR:
|
||||
add_library(Greeter SHARED ${headers} ${sources})
|
||||
|
||||
add_library(Greeter ${headers} ${sources})
|
||||
|
||||
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
|
||||
# EITHER:
|
||||
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
|
||||
target_compile_options(Greeter PUBLIC "$<$<BOOL:${MSVC}>:/permissive->")
|
||||
if(MSVC)
|
||||
target_compile_options(Greeter PUBLIC /permissive)
|
||||
endif()
|
||||
|
||||
# Link dependencies (if required) target_link_libraries(Greeter PUBLIC cxxopts)
|
||||
# Link dependencies EITHER:
|
||||
target_link_libraries(Greeter PRIVATE $<BUILD_INTERFACE:fmt::fmt-header-only>)
|
||||
# OR: target_link_libraries(Greeter PUBLIC fmt::fmt)
|
||||
|
||||
target_include_directories(
|
||||
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
||||
|
@ -72,5 +95,6 @@ packageProject(
|
|||
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
|
||||
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
|
||||
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
|
||||
DEPENDENCIES ""
|
||||
COMPATIBILITY SameMajorVersion
|
||||
# NOTE: not needed DEPENDENCIES "fmt 7.1.3"
|
||||
)
|
||||
|
|
58
GNUmakefile
Executable file
58
GNUmakefile
Executable file
|
@ -0,0 +1,58 @@
|
|||
#
|
||||
# CURDIR=$(/bin/pwd)
|
||||
#
|
||||
# GENERATOR="Unix Makefiles"
|
||||
GENERATOR=Ninja
|
||||
|
||||
export CPM_SOURCE_CACHE=${HOME}/.cache/CPM
|
||||
export CPM_USE_LOCAL_PACKAGES=1
|
||||
|
||||
.PHONY: update format all test check clean distclean lock
|
||||
|
||||
# the default target does just all
|
||||
check: all
|
||||
|
||||
all: test
|
||||
|
||||
test: install
|
||||
|
||||
|
||||
distclean:
|
||||
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
|
||||
cmake --build build/install --target cpm-update-package-lock
|
||||
|
||||
# install the library
|
||||
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
|
||||
|
||||
# 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
|
||||
|
||||
# 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
|
||||
|
||||
format: distclean
|
||||
find . -name CMakeLists.txt | xargs cmake-format -i
|
||||
find . -name '*.cpp' | xargs clang-format -i
|
||||
find . -name '*.h' | xargs clang-format -i
|
||||
|
||||
# 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
|
||||
run-clang-tidy.py -p build/standalone -checks='-*,modernize-*,misc-*,hicpp-*,cert-*,readability-*,portability-*,performance-*,google-*' standalone
|
|
@ -7,6 +7,9 @@ project(BuildAll LANGUAGES CXX)
|
|||
|
||||
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)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
set(CPM_DOWNLOAD_VERSION 0.28.0)
|
||||
set(CPM_DOWNLOAD_VERSION 0.28.4)
|
||||
|
||||
if(CPM_SOURCE_CACHE)
|
||||
# Expand relative path. This is important if the provided path contains a tilde (~)
|
||||
get_filename_component(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE} ABSOLUTE)
|
||||
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
|
||||
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
|
||||
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
|
||||
|
@ -11,7 +13,7 @@ endif()
|
|||
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
|
||||
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
|
||||
file(DOWNLOAD
|
||||
https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
|
||||
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
|
||||
${CPM_DOWNLOAD_LOCATION}
|
||||
)
|
||||
endif()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
cmake_minimum_required(VERSION 3.14...3.19)
|
||||
|
||||
project(GreeterDocs)
|
||||
|
||||
|
|
|
@ -18,14 +18,14 @@ namespace greeter {
|
|||
* @brief Creates a new greeter
|
||||
* @param name the name to greet
|
||||
*/
|
||||
Greeter(std::string name);
|
||||
explicit 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
|
||||
*/
|
||||
std::string greet(LanguageCode lang = LanguageCode::EN) const;
|
||||
[[nodiscard]] std::string greet(LanguageCode lang = LanguageCode::EN) const;
|
||||
};
|
||||
|
||||
} // namespace greeter
|
||||
|
|
|
@ -1,19 +1,20 @@
|
|||
#include <fmt/format.h>
|
||||
#include <greeter/greeter.h>
|
||||
|
||||
using namespace greeter;
|
||||
|
||||
Greeter::Greeter(std::string _name) : name(_name) {}
|
||||
Greeter::Greeter(std::string _name) : name(std::move(_name)) {}
|
||||
|
||||
std::string Greeter::greet(LanguageCode lang) const {
|
||||
switch (lang) {
|
||||
default:
|
||||
case LanguageCode::EN:
|
||||
return "Hello, " + name + "!";
|
||||
return fmt::format("Hello, {}!", name);
|
||||
case LanguageCode::DE:
|
||||
return "Hallo " + name + "!";
|
||||
return fmt::format("Hallo {}!", name);
|
||||
case LanguageCode::ES:
|
||||
return "¡Hola " + name + "!";
|
||||
return fmt::format("¡Hola {}!", name);
|
||||
case LanguageCode::FR:
|
||||
return "Bonjour " + name + "!";
|
||||
return fmt::format("Bonjour {}!", name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
cmake_minimum_required(VERSION 3.14...3.19)
|
||||
|
||||
project(GreeterStandalone LANGUAGES CXX)
|
||||
|
||||
|
@ -27,4 +27,8 @@ add_executable(GreeterStandalone ${sources})
|
|||
|
||||
set_target_properties(GreeterStandalone PROPERTIES CXX_STANDARD 17 OUTPUT_NAME "Greeter")
|
||||
|
||||
target_link_libraries(GreeterStandalone Greeter::Greeter cxxopts)
|
||||
# WORKAROUND for ALIAS target is missing error! CK
|
||||
if(NOT TARGET cxxopts::cxxopts)
|
||||
add_library(cxxopts::cxxopts ALIAS cxxopts)
|
||||
endif()
|
||||
target_link_libraries(GreeterStandalone Greeter::Greeter cxxopts::cxxopts)
|
||||
|
|
|
@ -6,15 +6,20 @@
|
|||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
const std::unordered_map<std::string, greeter::LanguageCode> languages{
|
||||
{"en", greeter::LanguageCode::EN},
|
||||
{"de", greeter::LanguageCode::DE},
|
||||
{"es", greeter::LanguageCode::ES},
|
||||
{"fr", greeter::LanguageCode::FR},
|
||||
};
|
||||
|
||||
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
|
||||
int main(int argc, char** argv) {
|
||||
cxxopts::Options options(argv[0], "A program to welcome the world!");
|
||||
// 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},
|
||||
{"es", greeter::LanguageCode::ES},
|
||||
{"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;
|
||||
std::string name;
|
||||
|
@ -33,7 +38,10 @@ int main(int argc, char** argv) {
|
|||
if (result["help"].as<bool>()) {
|
||||
std::cout << options.help() << std::endl;
|
||||
return 0;
|
||||
} else if (result["version"].as<bool>()) {
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
cmake_minimum_required(VERSION 3.14...3.19)
|
||||
|
||||
project(GreeterTests LANGUAGES CXX)
|
||||
|
||||
|
@ -18,11 +18,11 @@ include(../cmake/CPM.cmake)
|
|||
CPMAddPackage(
|
||||
NAME doctest
|
||||
GITHUB_REPOSITORY onqtam/doctest
|
||||
GIT_TAG 2.3.7
|
||||
GIT_TAG 2.4.5
|
||||
)
|
||||
|
||||
if(TEST_INSTALLED_VERSION)
|
||||
find_package(Greeter REQUIRED)
|
||||
find_package(Greeter ${PROJECT_VERSION} REQUIRED)
|
||||
else()
|
||||
CPMAddPackage(NAME Greeter SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
|
||||
endif()
|
||||
|
@ -39,14 +39,14 @@ CPMAddPackage(
|
|||
|
||||
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
|
||||
add_executable(GreeterTests ${sources})
|
||||
target_link_libraries(GreeterTests doctest Greeter::Greeter)
|
||||
target_link_libraries(GreeterTests doctest::doctest Greeter::Greeter)
|
||||
|
||||
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
|
||||
|
||||
# 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 -pedantic -Wextra -Werror)
|
||||
target_compile_options(Greeter PUBLIC -Wall -Wpedantic -Wextra -Werror)
|
||||
elseif(MSVC)
|
||||
target_compile_options(Greeter PUBLIC /W4 /WX)
|
||||
target_compile_definitions(GreeterTests PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
|
||||
|
@ -57,15 +57,11 @@ endif()
|
|||
|
||||
enable_testing()
|
||||
|
||||
# Note: doctest and similar testing frameworks can automatically configure CMake tests For other
|
||||
# testing frameworks add the tests target instead: ADD_TEST(GreeterTests GreeterTests)
|
||||
|
||||
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
|
||||
doctest_discover_tests(GreeterTests)
|
||||
add_test(greeterTests GreeterTests)
|
||||
|
||||
# ---- code coverage ----
|
||||
|
||||
if(ENABLE_TEST_COVERAGE)
|
||||
if(ENABLE_TEST_COVERAGE AND NOT Greeter_FOUND)
|
||||
target_compile_options(Greeter PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
|
||||
target_link_options(Greeter PUBLIC -fprofile-arcs -ftest-coverage)
|
||||
endif()
|
||||
|
|
|
@ -16,6 +16,11 @@ 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
|
||||
#else
|
||||
static_assert(std::string_view(GREETER_VERSION) == std::string_view("1.0"));
|
||||
CHECK(std::string(GREETER_VERSION) == std::string("1.0"));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue