Skip to content

Commit

Permalink
build: Honor CMake USE_STD_FORMAT option (#761)
Browse files Browse the repository at this point in the history
* Honor use_std_format option

Signed-off-by: Uilian Ries <[email protected]>

* use format option in contribution guide

Signed-off-by: Uilian Ries <[email protected]>

* Revert CMakePresets

Signed-off-by: Uilian Ries <[email protected]>

* Avoid cmake expression-generator

Signed-off-by: Uilian Ries <[email protected]>

* Add status message when not fiding std format

Signed-off-by: Uilian Ries <[email protected]>

---------

Signed-off-by: Uilian Ries <[email protected]>
  • Loading branch information
uilianries authored Jun 29, 2024
1 parent e18ad28 commit 213ce21
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
23 changes: 15 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ project(faker-cxx LANGUAGES CXX)
include(cmake/CompilerWarnings.cmake)

option(USE_SYSTEM_DEPENDENCIES "Use fmt and GTest from system" OFF)
option(USE_STD_FORMAT "Use std::format when available" ON)
option(BUILD_EXAMPLES "Build examples" OFF)
option(BUILD_TESTING "Build tests" ON)

if (USE_STD_FORMAT)
if (MSVC)
set(CMAKE_REQUIRED_FLAGS /std:c++20)
else()
set(CMAKE_REQUIRED_FLAGS -std=c++20)
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("#include <format>\nint main(){ auto var = std::format(\"{}\", \"Hello\"); return 0; }"
HAS_STD_FORMAT)
endif ()
endif()
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("#include <format>\nint main(){ auto var = std::format(\"{}\", \"Hello\"); return 0; }"
HAS_STD_FORMAT)
if (NOT HAS_STD_FORMAT)
message(STATUS "Compiler has no support for std::format. Need to use fmt library as dependency.")
endif()

include(CMakeDependentOption)
cmake_dependent_option(USE_STD_FORMAT "Use std::format when available" ON "HAS_STD_FORMAT" OFF)

if (BUILD_TESTING)
enable_testing()
Expand Down Expand Up @@ -119,8 +126,8 @@ install(EXPORT ${CMAKE_PROJECT_NAME}-targets
FILE ${CMAKE_PROJECT_NAME}-config.cmake
DESTINATION lib/cmake/${CMAKE_PROJECT_NAME})

if (HAS_STD_FORMAT)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE HAS_STD_FORMAT)
if (USE_STD_FORMAT)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE USE_STD_FORMAT)
elseif (USE_SYSTEM_DEPENDENCIES)
find_package(fmt CONFIG REQUIRED)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE fmt::fmt)
Expand Down
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ Follow the steps below to build the project:
cmake --build --preset unixlike-gcc-debug-static
```

#### Choosing between STL std::format and fmt library

The faker-cxx project uses formated string feature, which can be solved by:

- [fmt](https://github.com/fmtlib/fmt) library
- [std::format](https://en.cppreference.com/w/cpp/utility/format/format)

The `std::format` requires C++20 standard support from the compiler. The feature is checked via CMake when building the project.
In case available, the option `USE_STD_FORMAT` will be available:

```sh
cmake -S . --preset unixlike-gcc-debug-static -DUSE_STD_FORMAT=ON
```

In case `std::format` is not available, faker-cxx will use `fmt` library instead. It can be used as external dependency via
git submodules, or consumed from your system (installed by Conan or system package manager). In order to manage the way to
acquire `fmt`, the CMake option `USE_SYSTEM_DEPENDENCIES` manages if should be used from system, or from git submodule:

```sh
cmake -S . --preset unixlike-gcc-debug-static -DUSE_SYSTEM_DEPENDENCIES=OFF // Install from submodule
```

In case passing `USE_STD_FORMAT=ON` and `std::format` is not available, CMake will try to use `fmt` library automatically.
Then, in case not finding `fmt`, it will fail.

### Testing the Project

After building the project, you can run the tests to ensure everything is working correctly. We use CTest for running
Expand Down
4 changes: 2 additions & 2 deletions src/common/FormatHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "faker-cxx/types/Precision.h"
#include "faker-cxx/Export.h"

#if !defined(HAS_STD_FORMAT)
#if !defined(USE_STD_FORMAT)
#include <fmt/chrono.h>
#include <fmt/core.h>
#else
Expand All @@ -21,7 +21,7 @@ namespace faker
class FormatHelper
{
public:
#if !defined(HAS_STD_FORMAT)
#if !defined(USE_STD_FORMAT)
template <typename... Args>
static std::string format(fmt::format_string<Args...> fmt, Args&&... args)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ else ()
target_link_libraries(${PROJECT_NAME} PRIVATE gtest_main gmock_main faker-cxx)
endif ()

if (HAS_STD_FORMAT)
if (USE_STD_FORMAT)
target_include_directories(${PROJECT_NAME} PRIVATE ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_STD_FORMAT)
target_compile_definitions(${PROJECT_NAME} PRIVATE USE_STD_FORMAT)
else ()
target_link_libraries(${PROJECT_NAME} PRIVATE $<IF:$<TARGET_EXISTS:fmt::fmt>,fmt::fmt,fmt>)
target_include_directories(${PROJECT_NAME} PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
Expand Down

0 comments on commit 213ce21

Please sign in to comment.