Skip to content

Commit

Permalink
enabling <coroutine> from std library for folly when c++ version is 20
Browse files Browse the repository at this point in the history
Summary:
When upgrading the C++ version from 17 -> 20 for fbcode and xplat, the folly library coroutine depends on the <coroutine> header which has been added to the std library in v20 as opposed to <experimental/coroutine> header. This diff ensures that the given a c++ version, the right coroutine header is always selected

Problem:
The `LLVM_COROUTINES` flag is used to enable the folly coroutines library, and based on the c++ version, it should either include the <coroutine> (v20) or <experimental/coroutine> (v17) header. The current issue when upgrading however occurs in the if directive changed within this diff; It checks `!defined(LLVM_COROUTINES)` when determining which library to include.

Because it has to be defined for folly to utilize coroutines, the check always fails, resulting in the experimental coroutine always being included. This results in compilation errors when it is not included in the header files for c++20. So a reasonable approach is needed to ensure that the right coroutine header, when present, is selected.

Proposed Solution:
- Within the if directive, check to determine if the current c++ version is 20 and above. This ensures that the flag `FOLLY_USE_STD_COROUTINE` is set if so, and folly uses the coroutines from the std library.
  Potential Issue: Not all c++20 toolchains might have coroutines (like bespoke embedded hardware compilers)

- *Utilize the `LLVM_COROUTINES_CPP20`  flag which would be set in the buckconfig to indicate that the toolchain supports c++20 coroutines.

- Use either of the available coroutine feature macros `__cpp_impl_coroutine` or `__cpp_lib_coroutine` to check if coroutines are implemented in the standard library

Reviewed By: Gownta

Differential Revision: D68659268

fbshipit-source-id: 689570387c160bf71f362ceab23489c75d590e67
  • Loading branch information
Uche Uba authored and facebook-github-bot committed Jan 31, 2025
1 parent 7828628 commit 2f68715
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions folly/coro/Coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
// libc++'s <coroutine> header only provides its declarations for C++20 and
// above, so we need to fall back to <experimental/coroutine> when building with
// C++17.
#if __has_include(<coroutine>) && !defined(LLVM_COROUTINES) && \
(!defined(_LIBCPP_VERSION) || __cplusplus > 201703L)
#if (__has_include(<coroutine>) && !defined(LLVM_COROUTINES)) || defined(__cpp_impl_coroutine)
#define FOLLY_USE_STD_COROUTINE 1
#else
#define FOLLY_USE_STD_COROUTINE 0
Expand Down

0 comments on commit 2f68715

Please sign in to comment.