mirror of
https://github.com/TheLartians/ModernCppStarter.git
synced 2025-08-30 13:41:13 +02:00
Feature/extend build dependency (#80)
* add build time dependency to static lib the header only fmt lib is used to show this * use add_test() and add a warning why doctest_discover_tests() is only availabe if doctest is not imported with find_packag() * cmake-format file * fix missed comment * back to version 1.0 respect most review comments * fix typo * update used CMP package versions * respect most review comments no default cxx language settings install fmt link against fmt::fmt export dependency too if not needed ... * use generic expression as recommended * Update CMakeLists.txt Co-authored-by: Lars Melchior <TheLartians@users.noreply.github.com> * changes acording review comments update to current PackageProject version 1.5.0 too * Update CMakeLists.txt Co-authored-by: Lars Melchior <TheLartians@users.noreply.github.com> * respect review comment remove NOLINT comment * back to begining needs to use doctest cmake modules ... it is not my idea! Co-authored-by: Lars Melchior <TheLartians@users.noreply.github.com>
This commit is contained in:
parent
b58e0717f4
commit
2db60f2ccb
7 changed files with 44 additions and 30 deletions
|
@ -27,7 +27,14 @@ include(cmake/CPM.cmake)
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME PackageProject.cmake
|
NAME PackageProject.cmake
|
||||||
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
|
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
|
||||||
VERSION 1.4
|
VERSION 1.5.0
|
||||||
|
)
|
||||||
|
|
||||||
|
CPMAddPackage(
|
||||||
|
NAME fmt
|
||||||
|
GIT_TAG 7.1.3
|
||||||
|
GITHUB_REPOSITORY fmtlib/fmt # to get an installable target
|
||||||
|
OPTIONS "FMT_INSTALL YES"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---- Add source files ----
|
# ---- Add source files ----
|
||||||
|
@ -40,17 +47,16 @@ file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/
|
||||||
# ---- Create library ----
|
# ---- Create library ----
|
||||||
|
|
||||||
# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface
|
# 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
|
# target: add_library(Greeter INTERFACE)
|
||||||
# INTERFACE_COMPILE_FEATURES cxx_std_17)
|
|
||||||
|
|
||||||
add_library(Greeter ${headers} ${sources})
|
add_library(Greeter ${headers} ${sources})
|
||||||
|
|
||||||
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
|
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
|
||||||
|
|
||||||
# being a cross-platform target, we enforce standards conformance on MSVC
|
# being a cross-platform target, we enforce standards conformance on MSVC
|
||||||
target_compile_options(Greeter PUBLIC "$<$<BOOL:${MSVC}>:/permissive->")
|
target_compile_options(Greeter PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive>")
|
||||||
|
|
||||||
# Link dependencies (if required) target_link_libraries(Greeter PUBLIC cxxopts)
|
# Link dependencies
|
||||||
|
target_link_libraries(Greeter PUBLIC fmt::fmt)
|
||||||
|
|
||||||
target_include_directories(
|
target_include_directories(
|
||||||
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
||||||
|
@ -72,5 +78,6 @@ packageProject(
|
||||||
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
|
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
|
||||||
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
|
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
|
||||||
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
|
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
|
||||||
DEPENDENCIES ""
|
COMPATIBILITY SameMajorVersion
|
||||||
|
DEPENDENCIES "fmt 7.1.3"
|
||||||
)
|
)
|
||||||
|
|
|
@ -7,6 +7,9 @@ project(BuildAll LANGUAGES CXX)
|
||||||
|
|
||||||
include(../cmake/tools.cmake)
|
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}/../standalone ${CMAKE_BINARY_DIR}/standalone)
|
||||||
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../test ${CMAKE_BINARY_DIR}/test)
|
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}/../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)
|
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")
|
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
|
||||||
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
|
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
|
||||||
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
|
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}))
|
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
|
||||||
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
|
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
|
||||||
file(DOWNLOAD
|
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}
|
${CPM_DOWNLOAD_LOCATION}
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
|
#include <fmt/format.h>
|
||||||
#include <greeter/greeter.h>
|
#include <greeter/greeter.h>
|
||||||
|
|
||||||
using namespace greeter;
|
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 {
|
std::string Greeter::greet(LanguageCode lang) const {
|
||||||
switch (lang) {
|
switch (lang) {
|
||||||
default:
|
default:
|
||||||
case LanguageCode::EN:
|
case LanguageCode::EN:
|
||||||
return "Hello, " + name + "!";
|
return fmt::format("Hello, {}!", name);
|
||||||
case LanguageCode::DE:
|
case LanguageCode::DE:
|
||||||
return "Hallo " + name + "!";
|
return fmt::format("Hallo {}!", name);
|
||||||
case LanguageCode::ES:
|
case LanguageCode::ES:
|
||||||
return "¡Hola " + name + "!";
|
return fmt::format("¡Hola {}!", name);
|
||||||
case LanguageCode::FR:
|
case LanguageCode::FR:
|
||||||
return "Bonjour " + name + "!";
|
return fmt::format("Bonjour {}!", name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
auto main(int argc, char** argv) -> int {
|
||||||
const std::unordered_map<std::string, greeter::LanguageCode> languages{
|
const std::unordered_map<std::string, greeter::LanguageCode> languages{
|
||||||
{"en", greeter::LanguageCode::EN},
|
{"en", greeter::LanguageCode::EN},
|
||||||
{"de", greeter::LanguageCode::DE},
|
{"de", greeter::LanguageCode::DE},
|
||||||
|
@ -13,8 +14,7 @@ const std::unordered_map<std::string, greeter::LanguageCode> languages{
|
||||||
{"fr", greeter::LanguageCode::FR},
|
{"fr", greeter::LanguageCode::FR},
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
cxxopts::Options options(*argv, "A program to welcome the world!");
|
||||||
cxxopts::Options options(argv[0], "A program to welcome the world!");
|
|
||||||
|
|
||||||
std::string language;
|
std::string language;
|
||||||
std::string name;
|
std::string name;
|
||||||
|
@ -33,7 +33,9 @@ int main(int argc, char** argv) {
|
||||||
if (result["help"].as<bool>()) {
|
if (result["help"].as<bool>()) {
|
||||||
std::cout << options.help() << std::endl;
|
std::cout << options.help() << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
} else if (result["version"].as<bool>()) {
|
}
|
||||||
|
|
||||||
|
if (result["version"].as<bool>()) {
|
||||||
std::cout << "Greeter, version " << GREETER_VERSION << std::endl;
|
std::cout << "Greeter, version " << GREETER_VERSION << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ include(../cmake/CPM.cmake)
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME doctest
|
NAME doctest
|
||||||
GITHUB_REPOSITORY onqtam/doctest
|
GITHUB_REPOSITORY onqtam/doctest
|
||||||
GIT_TAG 2.3.7
|
GIT_TAG 2.4.5
|
||||||
)
|
)
|
||||||
|
|
||||||
if(TEST_INSTALLED_VERSION)
|
if(TEST_INSTALLED_VERSION)
|
||||||
|
@ -39,14 +39,13 @@ CPMAddPackage(
|
||||||
|
|
||||||
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
|
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
|
||||||
add_executable(GreeterTests ${sources})
|
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)
|
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
|
||||||
|
|
||||||
# enable compiler warnings
|
# enable compiler warnings
|
||||||
if(NOT TEST_INSTALLED_VERSION)
|
if(NOT TEST_INSTALLED_VERSION)
|
||||||
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
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)
|
elseif(MSVC)
|
||||||
target_compile_options(Greeter PUBLIC /W4 /WX)
|
target_compile_options(Greeter PUBLIC /W4 /WX)
|
||||||
target_compile_definitions(GreeterTests PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
|
target_compile_definitions(GreeterTests PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
|
||||||
|
@ -57,8 +56,8 @@ endif()
|
||||||
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
# Note: doctest and similar testing frameworks can automatically configure CMake tests For other
|
# Note: doctest and similar testing frameworks can automatically configure CMake tests. For other
|
||||||
# testing frameworks add the tests target instead: ADD_TEST(GreeterTests GreeterTests)
|
# testing frameworks add the tests target instead: add_test(NAME greeterTests COMMAND GreeterTests)
|
||||||
|
|
||||||
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
|
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
|
||||||
doctest_discover_tests(GreeterTests)
|
doctest_discover_tests(GreeterTests)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue