Skip to content

Commit

Permalink
build(cmake): detect compiler version for co_context::generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Codesire-Deng committed Nov 25, 2023
1 parent 05653c1 commit a34d011
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 17 deletions.
4 changes: 4 additions & 0 deletions cmake/CompileOption.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
20 changes: 16 additions & 4 deletions cmake/check/check_compile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 5 additions & 4 deletions example/iota.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if defined(__GNUG__) && !defined(__clang__)
#ifndef CO_CONTEXT_NO_GENERATOR

#include <co_context/generator.hpp>

Expand All @@ -22,13 +22,14 @@ int main() {
return 0;
}

#else
#else // ifndef CO_CONTEXT_NO_GENERATOR

#include <iostream>

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;
}

Expand Down
2 changes: 1 addition & 1 deletion include/co_context/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
#include <co_context/utility/defer.hpp>
#include <co_context/utility/polymorphism.hpp>

#if defined(__GNUG__) && !defined(__clang__)
#ifndef CO_CONTEXT_NO_GENERATOR
#include <co_context/generator.hpp>
#endif
15 changes: 9 additions & 6 deletions include/co_context/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <generator>

namespace co_context {
using std::generator;
}
#elif !defined(CO_CONTEXT_NO_GENERATOR)
#pragma GCC system_header
#include <algorithm>
#include <cassert>
Expand All @@ -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 {
Expand Down Expand Up @@ -622,3 +624,4 @@ class generator
} // namespace co_context

// NOLINTEND
#endif
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(co_context_tests
liburingcxx_netcat
move_shared_task
mpl_type_list
generator_test
)

foreach(test_target ${co_context_tests})
Expand Down
20 changes: 18 additions & 2 deletions test/generator_test.cppx → test/generator_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ifndef CO_CONTEXT_NO_GENERATOR
#include <co_context/generator.hpp>

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -49,7 +50,8 @@ co_context::generator<uint64_t, uint64_t> nested_sequences_example() {
);
#else
co_yield co_context::ranges::elements_of{
std::array<const uint64_t, 5>{2, 4, 6, 8, 10}};
std::array<const uint64_t, 5>{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)};
Expand Down Expand Up @@ -316,6 +318,20 @@ int main() {

stateful_alloc_example(std::allocator_arg, stateful_allocator<double>{42});

[[maybe_unused]] member_coro m;
[[maybe_unused]]
member_coro m;
assert(*m.f().begin() == 42);
}

#else // ifndef CO_CONTEXT_NO_GENERATOR

#include <iostream>

int main() {
std::cout
<< "This program requires g++ 11.3 or clang 17 as the compiler. exit..."
<< std::endl;
return 0;
}

#endif

0 comments on commit a34d011

Please sign in to comment.