Skip to content

Commit

Permalink
Feature/extend build dependency (#80)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ClausKlein and TheLartians authored Feb 19, 2021
1 parent b58e071 commit 2db60f2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
21 changes: 14 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
Expand All @@ -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 "$<$<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(
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
Expand All @@ -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"
)
3 changes: 3 additions & 0 deletions all/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 4 additions & 2 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions source/greeter.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
20 changes: 11 additions & 9 deletions standalone/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#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},
};
auto main(int argc, char** argv) -> int {
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},
};

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;
Expand All @@ -33,7 +33,9 @@ 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>()) {
}

if (result["version"].as<bool>()) {
std::cout << "Greeter, version " << GREETER_VERSION << std::endl;
return 0;
}
Expand Down
11 changes: 5 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/source/greeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 2db60f2

Please sign in to comment.