diff --git a/cmake/CompileOption.cmake b/cmake/CompileOption.cmake index 52e92dd..146312b 100644 --- a/cmake/CompileOption.cmake +++ b/cmake/CompileOption.cmake @@ -51,3 +51,7 @@ target_link_libraries(co_context PUBLIC Threads::Threads) if (USE_MIMALLOC) target_link_libraries(co_context PUBLIC mimalloc) endif() + +if (co_context_no_generator) # set by check/check_compile.cmake + target_compile_definitions(co_context PRIVATE CO_CONTEXT_NO_GENERATOR) +endif() diff --git a/cmake/check/check_compile.cmake b/cmake/check/check_compile.cmake index ad08f4e..1c3ef4f 100644 --- a/cmake/check/check_compile.cmake +++ b/cmake/check/check_compile.cmake @@ -2,10 +2,22 @@ if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") message(FATAL_ERROR "[g++/clang] is required, but the compiler id is ${CMAKE_CXX_COMPILER_ID}.${CMAKE_CXX_COMPILER_ID} is not supported now") endif() -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.3.0") - message(FATAL_ERROR "Insufficient gcc version, requires gcc 11.3 or above") +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.2.0") + message(FATAL_ERROR "Insufficient gcc version, requires gcc 11.2 or above") + endif() + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.3.0") + message(NOTICE "co_context::generator will be disabled for insufficient gcc version (requires gcc 11.3 or above).") + set(co_context_no_generator ON) + endif() endif() -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0.0") - message(FATAL_ERROR "Insufficient clang version, requires clang 14.0 or above") +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0.0") + message(FATAL_ERROR "Insufficient clang version, requires clang 14.0 or above") + endif() + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "17.0.0") + message(NOTICE "co_context::generator will be disabled for insufficient clang version (requires clang 17 or above).") + set(co_context_no_generator ON) + endif() endif() diff --git a/example/iota.cpp b/example/iota.cpp index 192e9ff..f42e107 100644 --- a/example/iota.cpp +++ b/example/iota.cpp @@ -1,4 +1,4 @@ -#if defined(__GNUG__) && !defined(__clang__) +#ifndef CO_CONTEXT_NO_GENERATOR #include @@ -22,13 +22,14 @@ int main() { return 0; } -#else +#else // ifndef CO_CONTEXT_NO_GENERATOR #include int main() { - std::cout << "This program requires g++ as the compiler. exit..." - << std::endl; + std::cout + << "This program requires g++ 11.3 or clang 17 as the compiler. exit..." + << std::endl; return 0; } diff --git a/include/co_context/all.hpp b/include/co_context/all.hpp index ea3fdba..20464bb 100644 --- a/include/co_context/all.hpp +++ b/include/co_context/all.hpp @@ -14,6 +14,6 @@ #include #include -#if defined(__GNUG__) && !defined(__clang__) +#ifndef CO_CONTEXT_NO_GENERATOR #include #endif diff --git a/include/co_context/generator.hpp b/include/co_context/generator.hpp index 597566f..489357f 100644 --- a/include/co_context/generator.hpp +++ b/include/co_context/generator.hpp @@ -3,12 +3,14 @@ // Authors: Casey Carter, Lewis Baker, Corentin Jabot. // https://godbolt.org/z/5hcaPcfvP // -// NOLINTBEGIN #pragma once -#if !defined(__GNUG__) || defined(__clang__) -#error Up to now this header can only be compiled by g++. -#error PS: Clang 14/16 is known to be not happy with this header. -#endif +#ifdef CO_CONTEXT_STD_GENERATOR +#include + +namespace co_context { +using std::generator; +} +#elif !defined(CO_CONTEXT_NO_GENERATOR) #pragma GCC system_header #include #include @@ -34,7 +36,7 @@ #define NO_UNIQUE_ADDRESS [[no_unique_address]] #endif -// namespace std { +// NOLINTBEGIN namespace co_context { struct alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) _Aligned_block { @@ -622,3 +624,4 @@ class generator } // namespace co_context // NOLINTEND +#endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4499aa5..89b1d93 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,6 +11,7 @@ set(co_context_tests liburingcxx_netcat move_shared_task mpl_type_list + generator_test ) foreach(test_target ${co_context_tests}) diff --git a/test/generator_test.cppx b/test/generator_test.cpp similarity index 96% rename from test/generator_test.cppx rename to test/generator_test.cpp index 166d80e..97a1809 100644 --- a/test/generator_test.cppx +++ b/test/generator_test.cpp @@ -1,3 +1,4 @@ +#ifndef CO_CONTEXT_NO_GENERATOR #include ///////////////////////////////////////////////////////////////////////////// @@ -49,7 +50,8 @@ co_context::generator nested_sequences_example() { ); #else co_yield co_context::ranges::elements_of{ - std::array{2, 4, 6, 8, 10}}; + std::array{2, 4, 6, 8, 10} + }; #endif std::printf("yielding elements_of nested co_context::generator\n"); co_yield co_context::ranges::elements_of{fib(10)}; @@ -316,6 +318,20 @@ int main() { stateful_alloc_example(std::allocator_arg, stateful_allocator{42}); - [[maybe_unused]] member_coro m; + [[maybe_unused]] + member_coro m; assert(*m.f().begin() == 42); } + +#else // ifndef CO_CONTEXT_NO_GENERATOR + +#include + +int main() { + std::cout + << "This program requires g++ 11.3 or clang 17 as the compiler. exit..." + << std::endl; + return 0; +} + +#endif