Skip to content

Commit a34d011

Browse files
committed
build(cmake): detect compiler version for co_context::generator
1 parent 05653c1 commit a34d011

File tree

7 files changed

+54
-17
lines changed

7 files changed

+54
-17
lines changed

cmake/CompileOption.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ target_link_libraries(co_context PUBLIC Threads::Threads)
5151
if (USE_MIMALLOC)
5252
target_link_libraries(co_context PUBLIC mimalloc)
5353
endif()
54+
55+
if (co_context_no_generator) # set by check/check_compile.cmake
56+
target_compile_definitions(co_context PRIVATE CO_CONTEXT_NO_GENERATOR)
57+
endif()

cmake/check/check_compile.cmake

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@ if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
22
message(FATAL_ERROR "[g++/clang] is required, but the compiler id is ${CMAKE_CXX_COMPILER_ID}.${CMAKE_CXX_COMPILER_ID} is not supported now")
33
endif()
44

5-
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.3.0")
6-
message(FATAL_ERROR "Insufficient gcc version, requires gcc 11.3 or above")
5+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
6+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.2.0")
7+
message(FATAL_ERROR "Insufficient gcc version, requires gcc 11.2 or above")
8+
endif()
9+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.3.0")
10+
message(NOTICE "co_context::generator will be disabled for insufficient gcc version (requires gcc 11.3 or above).")
11+
set(co_context_no_generator ON)
12+
endif()
713
endif()
814

9-
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0.0")
10-
message(FATAL_ERROR "Insufficient clang version, requires clang 14.0 or above")
15+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
16+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0.0")
17+
message(FATAL_ERROR "Insufficient clang version, requires clang 14.0 or above")
18+
endif()
19+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "17.0.0")
20+
message(NOTICE "co_context::generator will be disabled for insufficient clang version (requires clang 17 or above).")
21+
set(co_context_no_generator ON)
22+
endif()
1123
endif()

example/iota.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined(__GNUG__) && !defined(__clang__)
1+
#ifndef CO_CONTEXT_NO_GENERATOR
22

33
#include <co_context/generator.hpp>
44

@@ -22,13 +22,14 @@ int main() {
2222
return 0;
2323
}
2424

25-
#else
25+
#else // ifndef CO_CONTEXT_NO_GENERATOR
2626

2727
#include <iostream>
2828

2929
int main() {
30-
std::cout << "This program requires g++ as the compiler. exit..."
31-
<< std::endl;
30+
std::cout
31+
<< "This program requires g++ 11.3 or clang 17 as the compiler. exit..."
32+
<< std::endl;
3233
return 0;
3334
}
3435

include/co_context/all.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
#include <co_context/utility/defer.hpp>
1515
#include <co_context/utility/polymorphism.hpp>
1616

17-
#if defined(__GNUG__) && !defined(__clang__)
17+
#ifndef CO_CONTEXT_NO_GENERATOR
1818
#include <co_context/generator.hpp>
1919
#endif

include/co_context/generator.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// Authors: Casey Carter, Lewis Baker, Corentin Jabot.
44
// https://godbolt.org/z/5hcaPcfvP
55
//
6-
// NOLINTBEGIN
76
#pragma once
8-
#if !defined(__GNUG__) || defined(__clang__)
9-
#error Up to now this header can only be compiled by g++.
10-
#error PS: Clang 14/16 is known to be not happy with this header.
11-
#endif
7+
#ifdef CO_CONTEXT_STD_GENERATOR
8+
#include <generator>
9+
10+
namespace co_context {
11+
using std::generator;
12+
}
13+
#elif !defined(CO_CONTEXT_NO_GENERATOR)
1214
#pragma GCC system_header
1315
#include <algorithm>
1416
#include <cassert>
@@ -34,7 +36,7 @@
3436
#define NO_UNIQUE_ADDRESS [[no_unique_address]]
3537
#endif
3638

37-
// namespace std {
39+
// NOLINTBEGIN
3840
namespace co_context {
3941

4042
struct alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) _Aligned_block {
@@ -622,3 +624,4 @@ class generator
622624
} // namespace co_context
623625

624626
// NOLINTEND
627+
#endif

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(co_context_tests
1111
liburingcxx_netcat
1212
move_shared_task
1313
mpl_type_list
14+
generator_test
1415
)
1516

1617
foreach(test_target ${co_context_tests})

test/generator_test.cppx renamed to test/generator_test.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifndef CO_CONTEXT_NO_GENERATOR
12
#include <co_context/generator.hpp>
23

34
/////////////////////////////////////////////////////////////////////////////
@@ -49,7 +50,8 @@ co_context::generator<uint64_t, uint64_t> nested_sequences_example() {
4950
);
5051
#else
5152
co_yield co_context::ranges::elements_of{
52-
std::array<const uint64_t, 5>{2, 4, 6, 8, 10}};
53+
std::array<const uint64_t, 5>{2, 4, 6, 8, 10}
54+
};
5355
#endif
5456
std::printf("yielding elements_of nested co_context::generator\n");
5557
co_yield co_context::ranges::elements_of{fib(10)};
@@ -316,6 +318,20 @@ int main() {
316318

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

319-
[[maybe_unused]] member_coro m;
321+
[[maybe_unused]]
322+
member_coro m;
320323
assert(*m.f().begin() == 42);
321324
}
325+
326+
#else // ifndef CO_CONTEXT_NO_GENERATOR
327+
328+
#include <iostream>
329+
330+
int main() {
331+
std::cout
332+
<< "This program requires g++ 11.3 or clang 17 as the compiler. exit..."
333+
<< std::endl;
334+
return 0;
335+
}
336+
337+
#endif

0 commit comments

Comments
 (0)