Skip to content

Commit 780f1a4

Browse files
committed
Added CMake build system
1 parent 99bb7f8 commit 780f1a4

File tree

7 files changed

+361
-2
lines changed

7 files changed

+361
-2
lines changed

CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(cppcoro LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_EXTENSIONS OFF)
5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
7+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
8+
include(CTest)
9+
10+
add_subdirectory(lib)
11+
if(BUILD_TESTING)
12+
add_subdirectory(test)
13+
endif()
14+
15+
export(EXPORT cppcoroTargets
16+
FILE "${PROJECT_BINARY_DIR}/cppcoro/cppcoroTargets.cmake"
17+
NAMESPACE cppcoro::)
18+
configure_file(cmake/cppcoroConfig.cmake
19+
"${PROJECT_BINARY_DIR}/cppcoro/cppcoroConfig.cmake"
20+
COPYONLY)
21+
22+
set(config_package_location lib/cmake/cppcoro)
23+
install(DIRECTORY include
24+
DESTINATION .
25+
COMPONENT Devel)
26+
install(FILES cmake/FindCppcoroCoroutines.cmake
27+
DESTINATION ${config_package_location}
28+
COMPONENT Devel)
29+
install(EXPORT cppcoroTargets
30+
FILE cppcoroTargets.cmake
31+
NAMESPACE cppcoro::
32+
DESTINATION ${config_package_location})
33+
install(
34+
FILES ${CMAKE_CURRENT_BINARY_DIR}/cppcoro/cppcoroConfig.cmake
35+
DESTINATION ${config_package_location}
36+
COMPONENT Devel)

README.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,18 +2859,20 @@ Given a type, `S`, that implements the `DelayedScheduler` and an instance, `s` o
28592859

28602860
The cppcoro library supports building under Windows with Visual Studio 2017 and Linux with Clang 5.0+.
28612861

2862-
This library makes use of the [Cake build system](https://github.com/lewissbaker/cake) (no, not the [C# one](http://cakebuild.net/)).
2862+
This library makes use of either the [Cake build system](https://github.com/lewissbaker/cake) (no, not the [C# one](http://cakebuild.net/)) or CMake.
28632863

28642864
The cake build system is checked out automatically as a git submodule so you don't need to download or install it separately.
28652865

28662866
## Building on Windows
28672867

28682868
This library currently requires Visual Studio 2017 or later and the Windows 10 SDK.
28692869

2870-
Support for Clang ([#3](https://github.com/lewissbaker/cppcoro/issues/3)) and Linux ([#15](https://github.com/lewissbaker/cppcoro/issues/15)) is planned.
2870+
Support for Linux ([#15](https://github.com/lewissbaker/cppcoro/issues/15)) is planned.
28712871

28722872
### Prerequisites
28732873

2874+
The CMakeLists requires version 3.13 or later.
2875+
28742876
The Cake build-system is implemented in Python and requires Python 2.7 to be installed.
28752877

28762878
Ensure Python 2.7 interpreter is in your PATH and available as 'python'.
@@ -2903,6 +2905,68 @@ c:\Code\cppcoro> git submodule update --init --recursive
29032905

29042906
### Building from the command-line
29052907

2908+
#### With CMake
2909+
2910+
Cppcoro follows the usual CMake workflow with no custom options added. Notable [standard CMake options](https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html):
2911+
2912+
| Flag | Description | Default Value |
2913+
|----------------------|------------------------------|------------------------|
2914+
| BUILD_TESTING | Build the unit tests | ON |
2915+
| BUILD_SHARED_LIBS | Build as a shared library | OFF |
2916+
| CMAKE_BUILD_TYPE | Build as `Debug`/`Release` | <empty> |
2917+
| CMAKE_INSTALL_PREFIX | Where to install the library | `/usr/local` (on Unix) |
2918+
2919+
CMake also respects the [conventional environment variables](https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html):
2920+
2921+
| Environment Variable | Description |
2922+
|----------------------|-------------------------------|
2923+
| CXX | Path to the C++ compiler |
2924+
| CXXFLAGS | C++ compiler flags to prepend |
2925+
| LDFLAGS | Linker flags to prepend |
2926+
2927+
Example:
2928+
2929+
```bash
2930+
cd <this/repo>
2931+
mkdir build
2932+
cd build
2933+
export CXX=clang++
2934+
export CXXFLAGS="-stdlib=libc++ -march=native"
2935+
export LDFLAGS="-stdlib=libc++ -fuse-ld=lld -Wl,--gdb-index"
2936+
cmake .. [-GNinja] -DCMAKE_INSTALL_PREFIX=$HOME/.local -DBUILD_SHARED_LIBS=ON
2937+
ninja # or make -jN
2938+
ninja test # Run the tests
2939+
ninja install
2940+
```
2941+
2942+
The CMake build scripts will also install a `cppcoroConfig.cmake` file for consumers to use.
2943+
It will check at the consumer site that coroutines are indeed supported by the system and enable the appropriate compiler flag for Clang or MSVC, respectively.
2944+
Assuming cppcoro has been installed to `$HOME/.local` like in the example above it can be consumed like this:
2945+
2946+
```cmake
2947+
find_package(cppcoro REQUIRED)
2948+
add_executable(app main.cpp)
2949+
target_link_libraries(app PRIVATE cppcoro::cppcoro)
2950+
```
2951+
2952+
```bash
2953+
$ cmake . -Dcppcoro_ROOT=$HOME/.local
2954+
# ...
2955+
-- Performing Test Coroutines_SUPPORTS_MS_FLAG
2956+
-- Performing Test Coroutines_SUPPORTS_MS_FLAG - Failed
2957+
-- Performing Test Coroutines_SUPPORTS_GNU_FLAG
2958+
-- Performing Test Coroutines_SUPPORTS_GNU_FLAG - Success
2959+
-- Looking for C++ include coroutine
2960+
-- Looking for C++ include coroutine - not found
2961+
-- Looking for C++ include experimental/coroutine
2962+
-- Looking for C++ include experimental/coroutine - found
2963+
-- Configuring done
2964+
-- Generating done
2965+
# ...
2966+
```
2967+
2968+
#### With Cake
2969+
29062970
To build from the command-line just run 'cake.bat' in the workspace root.
29072971

29082972
eg.

cmake/CMakeLists.txt

Whitespace-only changes.

cmake/FindCppcoroCoroutines.cmake

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
include(CheckCXXCompilerFlag)
2+
include(CheckIncludeFileCXX)
3+
include(FindPackageHandleStandardArgs)
4+
5+
check_cxx_compiler_flag(/await Coroutines_SUPPORTS_MS_FLAG)
6+
check_cxx_compiler_flag(-fcoroutines-ts Coroutines_SUPPORTS_GNU_FLAG)
7+
if(Coroutines_SUPPORTS_MS_FLAG OR Coroutines_SUPPORTS_GNU_FLAG)
8+
set(Coroutines_COMPILER_SUPPORT ON)
9+
endif()
10+
11+
if(Coroutines_SUPPORTS_MS_FLAG)
12+
check_include_file_cxx("coroutine" Coroutines_STANDARD_LIBRARY_SUPPORT "/await")
13+
check_include_file_cxx("experimental/coroutine" Coroutines_EXPERIMENTAL_LIBRARY_SUPPORT "/await")
14+
elseif(Coroutines_SUPPORTS_GNU_FLAG)
15+
check_include_file_cxx("coroutine" Coroutines_STANDARD_LIBRARY_SUPPORT "-fcoroutines-ts")
16+
check_include_file_cxx("experimental/coroutine" Coroutines_EXPERIMENTAL_LIBRARY_SUPPORT "-fcoroutines-ts")
17+
endif()
18+
19+
if(Coroutines_EXPERIMENTAL_LIBRARY_SUPPORT OR Coroutines_STANDARD_LIBRARY_SUPPORT)
20+
set(Coroutines_LIBRARY_SUPPORT ON)
21+
endif()
22+
23+
find_package_handle_standard_args(CppcoroCoroutines
24+
REQUIRED_VARS Coroutines_LIBRARY_SUPPORT Coroutines_COMPILER_SUPPORT
25+
FAIL_MESSAGE "Verify that the compiler and the standard library both support the Coroutines TS")
26+
27+
if(NOT CppcoroCoroutines_FOUND OR TARGET cppcoro::coroutines)
28+
return()
29+
endif()
30+
31+
add_library(cppcoro::coroutines INTERFACE IMPORTED)
32+
if(Coroutines_SUPPORTS_MS_FLAG)
33+
target_compile_options(cppcoro::coroutines INTERFACE /await)
34+
elseif(Coroutines_SUPPORTS_GNU_FLAG)
35+
target_compile_options(cppcoro::coroutines INTERFACE -fcoroutines-ts)
36+
endif()

cmake/cppcoroConfig.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
2+
3+
include(CMakeFindDependencyMacro)
4+
find_dependency(CppcoroCoroutines QUIET REQUIRED)
5+
6+
include("${CMAKE_CURRENT_LIST_DIR}/cppcoroTargets.cmake")

lib/CMakeLists.txt

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
set(includes
2+
awaitable_traits.hpp
3+
is_awaitable.hpp
4+
async_auto_reset_event.hpp
5+
async_manual_reset_event.hpp
6+
async_generator.hpp
7+
async_mutex.hpp
8+
async_latch.hpp
9+
async_scope.hpp
10+
broken_promise.hpp
11+
cancellation_registration.hpp
12+
cancellation_source.hpp
13+
cancellation_token.hpp
14+
task.hpp
15+
sequence_barrier.hpp
16+
sequence_traits.hpp
17+
single_producer_sequencer.hpp
18+
multi_producer_sequencer.hpp
19+
shared_task.hpp
20+
shared_task.hpp
21+
single_consumer_event.hpp
22+
single_consumer_async_auto_reset_event.hpp
23+
sync_wait.hpp
24+
task.hpp
25+
io_service.hpp
26+
config.hpp
27+
on_scope_exit.hpp
28+
file_share_mode.hpp
29+
file_open_mode.hpp
30+
file_buffering_mode.hpp
31+
file.hpp
32+
fmap.hpp
33+
when_all.hpp
34+
when_all_ready.hpp
35+
resume_on.hpp
36+
schedule_on.hpp
37+
generator.hpp
38+
readable_file.hpp
39+
recursive_generator.hpp
40+
writable_file.hpp
41+
read_only_file.hpp
42+
write_only_file.hpp
43+
read_write_file.hpp
44+
file_read_operation.hpp
45+
file_write_operation.hpp
46+
static_thread_pool.hpp
47+
)
48+
list(TRANSFORM includes PREPEND "${PROJECT_SOURCE_DIR}/include/cppcoro/")
49+
50+
set(netIncludes
51+
ip_address.hpp
52+
ip_endpoint.hpp
53+
ipv4_address.hpp
54+
ipv4_endpoint.hpp
55+
ipv6_address.hpp
56+
ipv6_endpoint.hpp
57+
socket.hpp
58+
)
59+
list(TRANSFORM netIncludes PREPEND "${PROJECT_SOURCE_DIR}/include/cppcoro/net/")
60+
61+
set(detailIncludes
62+
void_value.hpp
63+
when_all_ready_awaitable.hpp
64+
when_all_counter.hpp
65+
when_all_task.hpp
66+
get_awaiter.hpp
67+
is_awaiter.hpp
68+
any.hpp
69+
sync_wait_task.hpp
70+
unwrap_reference.hpp
71+
lightweight_manual_reset_event.hpp
72+
)
73+
list(TRANSFORM detailIncludes PREPEND "${PROJECT_SOURCE_DIR}/include/cppcoro/detail/")
74+
75+
set(privateHeaders
76+
cancellation_state.hpp
77+
socket_helpers.hpp
78+
auto_reset_event.hpp
79+
spin_wait.hpp
80+
spin_mutex.hpp
81+
)
82+
83+
set(sources
84+
async_auto_reset_event.cpp
85+
async_manual_reset_event.cpp
86+
async_mutex.cpp
87+
cancellation_state.cpp
88+
cancellation_token.cpp
89+
cancellation_source.cpp
90+
cancellation_registration.cpp
91+
lightweight_manual_reset_event.cpp
92+
ip_address.cpp
93+
ip_endpoint.cpp
94+
ipv4_address.cpp
95+
ipv4_endpoint.cpp
96+
ipv6_address.cpp
97+
ipv6_endpoint.cpp
98+
static_thread_pool.cpp
99+
auto_reset_event.cpp
100+
spin_wait.cpp
101+
spin_mutex.cpp
102+
)
103+
104+
if(WIN32)
105+
set(win32DetailIncludes
106+
win32.hpp
107+
win32_overlapped_operation.hpp
108+
)
109+
list(TRANSFORM win32DetailIncludes PREPEND "${PROJECT_SOURCE_DIR}/include/cppcoro/detail/")
110+
list(APPEND detailIncludes ${win32DetailIncludes})
111+
112+
set(win32NetIncludes
113+
socket.hpp
114+
socket_accept_operation.hpp
115+
socket_connect_operation.hpp
116+
socket_disconnect_operation.hpp
117+
socket_recv_operation.hpp
118+
socket_recv_from_operation.hpp
119+
socket_send_operation.hpp
120+
socket_send_to_operation.hpp
121+
)
122+
list(TRANSFORM win32NetIncludes PREPEND "${PROJECT_SOURCE_DIR}/include/cppcoro/net/")
123+
list(APPEND netIncludes ${win32NetIncludes})
124+
125+
set(win32Sources
126+
win32.cpp
127+
io_service.cpp
128+
file.cpp
129+
readable_file.cpp
130+
writable_file.cpp
131+
read_only_file.cpp
132+
write_only_file.cpp
133+
read_write_file.cpp
134+
file_read_operation.cpp
135+
file_write_operation.cpp
136+
socket_helpers.cpp
137+
socket.cpp
138+
socket_accept_operation.cpp
139+
socket_connect_operation.cpp
140+
socket_disconnect_operation.cpp
141+
socket_send_operation.cpp
142+
socket_send_to_operation.cpp
143+
socket_recv_operation.cpp
144+
socket_recv_from_operation.cpp
145+
)
146+
list(APPEND sources ${win32Sources})
147+
endif()
148+
149+
add_library(cppcoro
150+
${includes}
151+
${netIncludes}
152+
${detailIncludes}
153+
${privateHeaders}
154+
${sources}
155+
)
156+
157+
target_include_directories(cppcoro PUBLIC
158+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
159+
$<INSTALL_INTERFACE:include>)
160+
target_compile_features(cppcoro PUBLIC cxx_std_20)
161+
162+
find_package(CppcoroCoroutines REQUIRED)
163+
target_link_libraries(cppcoro PUBLIC cppcoro::coroutines)
164+
165+
install(TARGETS cppcoro EXPORT cppcoroTargets
166+
LIBRARY DESTINATION lib
167+
ARCHIVE DESTINATION lib
168+
RUNTIME DESTINATION bin)

test/CMakeLists.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
add_library(doctest::doctest INTERFACE IMPORTED)
2+
target_include_directories(doctest::doctest INTERFACE doctest)
3+
4+
find_package(Threads REQUIRED)
5+
6+
add_executable(run
7+
counted.hpp
8+
io_service_fixture.hpp
9+
10+
main.cpp
11+
counted.cpp
12+
generator_tests.cpp
13+
recursive_generator_tests.cpp
14+
async_generator_tests.cpp
15+
async_auto_reset_event_tests.cpp
16+
async_manual_reset_event_tests.cpp
17+
async_mutex_tests.cpp
18+
async_latch_tests.cpp
19+
cancellation_token_tests.cpp
20+
task_tests.cpp
21+
sequence_barrier_tests.cpp
22+
shared_task_tests.cpp
23+
sync_wait_tests.cpp
24+
single_consumer_async_auto_reset_event_tests.cpp
25+
single_producer_sequencer_tests.cpp
26+
multi_producer_sequencer_tests.cpp
27+
when_all_tests.cpp
28+
when_all_ready_tests.cpp
29+
ip_address_tests.cpp
30+
ip_endpoint_tests.cpp
31+
ipv4_address_tests.cpp
32+
ipv4_endpoint_tests.cpp
33+
ipv6_address_tests.cpp
34+
ipv6_endpoint_tests.cpp
35+
static_thread_pool_tests.cpp
36+
)
37+
38+
if(WIN32)
39+
target_sources(run PRIVATE
40+
scheduling_operator_tests.cpp
41+
io_service_tests.cpp
42+
file_tests.cpp
43+
socket_tests.cpp
44+
)
45+
endif()
46+
47+
target_link_libraries(run PRIVATE cppcoro Threads::Threads)
48+
49+
add_test(NAME test COMMAND run)

0 commit comments

Comments
 (0)