From 62ceff43c89d755085e1900c34fc71f0ce1dc4b4 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Thu, 10 Oct 2024 12:17:58 +0200 Subject: [PATCH] build: make dependency on fast_float optional in CMake build The fast_float library isn't widely available, especially for older distributions. Furthermore, `FOLLY_CONV_ATOD_MODE` isn't even set by the CMake build, so conversion doesn't actually use fast_float. --- CMake/folly-config.h.cmake | 2 ++ CMake/folly-deps.cmake | 7 +++++-- folly/Conv.cpp | 6 +++++- folly/Conv.h | 2 ++ folly/test/ConvTest.cpp | 2 ++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CMake/folly-config.h.cmake b/CMake/folly-config.h.cmake index 84954764499..73c23788ce8 100644 --- a/CMake/folly-config.h.cmake +++ b/CMake/folly-config.h.cmake @@ -33,6 +33,8 @@ #cmakedefine FOLLY_HAVE_PTHREAD 1 #cmakedefine FOLLY_HAVE_PTHREAD_ATFORK 1 +#cmakedefine FOLLY_HAVE_FASTFLOAT 1 + #cmakedefine FOLLY_HAVE_LIBGFLAGS 1 #cmakedefine FOLLY_UNUSUAL_GFLAGS_NAMESPACE 1 #cmakedefine FOLLY_GFLAGS_NAMESPACE @FOLLY_GFLAGS_NAMESPACE@ diff --git a/CMake/folly-deps.cmake b/CMake/folly-deps.cmake index 6ce4c679a42..362c67c2f74 100644 --- a/CMake/folly-deps.cmake +++ b/CMake/folly-deps.cmake @@ -52,8 +52,11 @@ find_package(DoubleConversion MODULE REQUIRED) list(APPEND FOLLY_LINK_LIBRARIES ${DOUBLE_CONVERSION_LIBRARY}) list(APPEND FOLLY_INCLUDE_DIRECTORIES ${DOUBLE_CONVERSION_INCLUDE_DIR}) -find_package(FastFloat MODULE REQUIRED) -list(APPEND FOLLY_INCLUDE_DIRECTORIES ${FASTFLOAT_INCLUDE_DIR}) +find_package(FastFloat MODULE) +set(FOLLY_HAVE_FASTFLOAT ${FASTFLOAT_FOUND}) +if(FASTFLOAT_FOUND) + list(APPEND FOLLY_INCLUDE_DIRECTORIES ${FASTFLOAT_INCLUDE_DIR}) +endif() find_package(Gflags MODULE) set(FOLLY_HAVE_LIBGFLAGS ${LIBGFLAGS_FOUND}) diff --git a/folly/Conv.cpp b/folly/Conv.cpp index 4e392685613..d7b8ed4f274 100644 --- a/folly/Conv.cpp +++ b/folly/Conv.cpp @@ -21,7 +21,9 @@ #include +#if FOLLY_HAVE_FASTFLOAT #include // @manual=fbsource//third-party/fast_float:fast_float +#endif namespace folly { namespace detail { @@ -462,6 +464,7 @@ Expected str_to_floating_double_conversion( return Tgt(result); } +#if FOLLY_HAVE_FASTFLOAT /// Uses `fast_float::from_chars` to convert from string to an integer. template Expected str_to_floating_fast_float_from_chars( @@ -505,6 +508,7 @@ template Expected str_to_floating_fast_float_from_chars(StringPiece* src) noexcept; template Expected str_to_floating_fast_float_from_chars(StringPiece* src) noexcept; +#endif /** * StringPiece to double, with progress information. Alters the @@ -512,7 +516,7 @@ str_to_floating_fast_float_from_chars(StringPiece* src) noexcept; */ template Expected str_to_floating(StringPiece* src) noexcept { -#if defined(FOLLY_CONV_ATOD_MODE) && FOLLY_CONV_ATOD_MODE == 1 +#if FOLLY_HAVE_FASTFLOAT && defined(FOLLY_CONV_ATOD_MODE) && FOLLY_CONV_ATOD_MODE == 1 return detail::str_to_floating_fast_float_from_chars(src); #else return detail::str_to_floating_double_conversion(src); diff --git a/folly/Conv.h b/folly/Conv.h index c7b61cacfc8..839ae90ff1e 100644 --- a/folly/Conv.h +++ b/folly/Conv.h @@ -1461,6 +1461,7 @@ extern template Expected str_to_floating( extern template Expected str_to_floating( StringPiece* src) noexcept; +#if FOLLY_HAVE_FASTFLOAT template Expected str_to_floating_fast_float_from_chars( StringPiece* src) noexcept; @@ -1469,6 +1470,7 @@ extern template Expected str_to_floating_fast_float_from_chars(StringPiece* src) noexcept; extern template Expected str_to_floating_fast_float_from_chars(StringPiece* src) noexcept; +#endif template Expected digits_to(const char* b, const char* e) noexcept; diff --git a/folly/test/ConvTest.cpp b/folly/test/ConvTest.cpp index 0680ff692b7..98188a7ad75 100644 --- a/folly/test/ConvTest.cpp +++ b/folly/test/ConvTest.cpp @@ -1772,6 +1772,7 @@ TEST(Conv, TryStringToFloat) { tryStringToFloat(StrToFloatTryTo()); } +#if FOLLY_HAVE_FASTFLOAT /// Uses `folly::detail::str_to_floating_fast_float_from_chars` to convert a /// string to a float. template @@ -1792,6 +1793,7 @@ TEST(Conv, TryStringToFloat_FastFloatFromChars) { tryStringToFloat( StrToFloatFastFloatFromChars()); } +#endif template void tryToDouble() {