Skip to content

Commit

Permalink
cmake check for format
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslan-ilesik committed Jul 19, 2024
1 parent e1dec28 commit fa7a831
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
42 changes: 41 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ option(RUN_LDCONFIG "Run ldconfig after installation" ON)
option(DPP_INSTALL "Generate the install target" ON)
option(DPP_BUILD_TEST "Build the test program" ON)
option(DPP_NO_VCPKG "No VCPKG" OFF)
option(DPP_CORO "Experimental support for C++20 coroutines" OFF)
option(DPP_CORO "Experimental support for C++20 coroutines" ON)
option(DPP_USE_EXTERNAL_JSON "Use an external installation of nlohmann::json" OFF)
option(DPP_USE_PCH "Use precompiled headers to speed up compilation" OFF)
option(AVX_TYPE "Force AVX type for speeding up audio mixing" OFF)
Expand Down Expand Up @@ -115,3 +115,43 @@ else()
# that made no sense, it seems they may have changed their parsing rules somehow.
message("-- Using bundled nlohmann::json")
endif()

if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
include(CheckCXXSourceCompiles)
unset(HAS_FORMAT CACHE)
set(temp_flags "${CMAKE_REQUIRED_FLAGS}")
set(CMAKE_REQUIRED_FLAGS "-std=c++20")
check_cxx_source_compiles(
"
#include <format>
#include <string>
struct MyType {
int value;
std::string str() const { return std::to_string(value); }
};
template <>
struct std::formatter<MyType> : std::formatter<std::string> {
auto format(const MyType& item, std::format_context& ctx) const -> decltype(ctx.out()) {
return std::format_to(ctx.out(), \"{}\", item.str());
}
};
int main() {
MyType my{42};
auto s = std::format(\"{}\", my);
return 0;
}
"
HAS_FORMAT

)
set(CMAKE_REQUIRED_FLAGS ${temp_flags})
if(HAS_FORMAT)
message("-- INFO: Found std::format support")
target_compile_definitions(dpp PUBLIC DPP_HAS_FORMAT)
else ()
message("-- INFO: std::format support not found")
endif()
endif ()
12 changes: 2 additions & 10 deletions include/dpp/snowflake.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,11 @@
#include <cstdint>
#include <type_traits>

#if __cplusplus >= 202002L
//fix for https://github.com/llvm/llvm-project/issues/77773 and
// apple support on clang 14 https://developer.apple.com/xcode/cpp/
#if (defined(__cpp_lib_format) || \
(defined(__clang__) && __clang_major__ >= 14 && !defined(__APPLE__) && __has_include(<format>))) || ( defined(__GNUC__) && __GNUC__ >= 13)

#define DPP_HAS_FORMAT

#ifdef DPP_HAS_FORMAT
#include <format>

#endif
#endif


/**
* @brief The main namespace for D++ functions. classes and types
*/
Expand Down

0 comments on commit fa7a831

Please sign in to comment.