diff --git a/CMakeLists.txt b/CMakeLists.txt index d12bd85..6b45ae4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,14 @@ include(cmake/CPM.cmake) CPMAddPackage( NAME 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 ---- @@ -40,17 +47,16 @@ 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: add_library(Greeter INTERFACE) add_library(Greeter ${headers} ${sources}) set_target_properties(Greeter PROPERTIES CXX_STANDARD 17) # being a cross-platform target, we enforce standards conformance on MSVC -target_compile_options(Greeter PUBLIC "$<$:/permissive->") +target_compile_options(Greeter PUBLIC "$<$:/permissive>") -# Link dependencies (if required) target_link_libraries(Greeter PUBLIC cxxopts) +# Link dependencies +target_link_libraries(Greeter PUBLIC fmt::fmt) target_include_directories( Greeter PUBLIC $ @@ -72,5 +78,6 @@ packageProject( INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} VERSION_HEADER "${VERSION_HEADER_LOCATION}" - DEPENDENCIES "" + COMPATIBILITY SameMajorVersion + DEPENDENCIES "fmt 7.1.3" ) diff --git a/all/CMakeLists.txt b/all/CMakeLists.txt index e131b89..db5f5fb 100644 --- a/all/CMakeLists.txt +++ b/all/CMakeLists.txt @@ -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) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 7e35741..73ee2d0 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -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() diff --git a/source/greeter.cpp b/source/greeter.cpp index b1c54a7..11fb416 100644 --- a/source/greeter.cpp +++ b/source/greeter.cpp @@ -1,19 +1,20 @@ +#include #include 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); } } diff --git a/standalone/source/main.cpp b/standalone/source/main.cpp index 5464fa5..938a386 100644 --- a/standalone/source/main.cpp +++ b/standalone/source/main.cpp @@ -6,15 +6,15 @@ #include #include -const std::unordered_map languages{ - {"en", greeter::LanguageCode::EN}, - {"de", greeter::LanguageCode::DE}, - {"es", greeter::LanguageCode::ES}, - {"fr", greeter::LanguageCode::FR}, -}; +auto main(int argc, char** argv) -> int { + const std::unordered_map languages{ + {"en", greeter::LanguageCode::EN}, + {"de", greeter::LanguageCode::DE}, + {"es", greeter::LanguageCode::ES}, + {"fr", greeter::LanguageCode::FR}, + }; -int main(int argc, char** argv) { - cxxopts::Options options(argv[0], "A program to welcome the world!"); + cxxopts::Options options(*argv, "A program to welcome the world!"); std::string language; std::string name; @@ -33,7 +33,9 @@ int main(int argc, char** argv) { if (result["help"].as()) { std::cout << options.help() << std::endl; return 0; - } else if (result["version"].as()) { + } + + if (result["version"].as()) { std::cout << "Greeter, version " << GREETER_VERSION << std::endl; return 0; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 302d7f2..a54ecc3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,7 +18,7 @@ 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) @@ -39,14 +39,13 @@ 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,8 +56,8 @@ 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) +# Note: doctest and similar testing frameworks can automatically configure CMake tests. For other +# testing frameworks add the tests target instead: add_test(NAME greeterTests COMMAND GreeterTests) include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake) doctest_discover_tests(GreeterTests) diff --git a/test/source/greeter.cpp b/test/source/greeter.cpp index 3d507e0..2ff31ba 100644 --- a/test/source/greeter.cpp +++ b/test/source/greeter.cpp @@ -18,4 +18,4 @@ TEST_CASE("Greeter") { TEST_CASE("Greeter version") { static_assert(std::string_view(GREETER_VERSION) == std::string_view("1.0")); CHECK(std::string(GREETER_VERSION) == std::string("1.0")); -} \ No newline at end of file +}