Skip to content

Commit de4ceff

Browse files
committed
Merge remote-tracking branch 'mygithub/add_cmake_support' into HEAD
2 parents 132ae88 + 07bb85e commit de4ceff

File tree

9 files changed

+878
-2
lines changed

9 files changed

+878
-2
lines changed

CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
9+
add_subdirectory(lib)
10+
11+
enable_testing()
12+
if(BUILD_TESTING)
13+
add_subdirectory(test)
14+
endif()
15+
16+
export(EXPORT cppcoroTargets
17+
FILE "${PROJECT_BINARY_DIR}/cppcoro/cppcoroTargets.cmake"
18+
NAMESPACE cppcoro::)
19+
configure_file(cmake/cppcoroConfig.cmake
20+
"${PROJECT_BINARY_DIR}/cppcoro/cppcoroConfig.cmake"
21+
COPYONLY)
22+
23+
set(config_package_location lib/cmake/cppcoro)
24+
install(DIRECTORY include/cppcoro
25+
DESTINATION include
26+
COMPONENT Devel)
27+
install(FILES cmake/FindCoroutines.cmake
28+
DESTINATION ${config_package_location}
29+
COMPONENT Devel)
30+
install(EXPORT cppcoroTargets
31+
FILE cppcoroTargets.cmake
32+
NAMESPACE cppcoro::
33+
DESTINATION ${config_package_location})
34+
install(
35+
FILES ${CMAKE_CURRENT_BINARY_DIR}/cppcoro/cppcoroConfig.cmake
36+
DESTINATION ${config_package_location}
37+
COMPONENT Devel)

README.md

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

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

2897-
This library makes use of the [Cake build system](https://github.com/lewissbaker/cake) (no, not the [C# one](http://cakebuild.net/)).
2897+
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.
28982898

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

29012901
## Building on Windows
29022902

29032903
This library currently requires Visual Studio 2017 or later and the Windows 10 SDK.
29042904

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

29072907
### Prerequisites
29082908

2909+
The CMakeLists requires version 3.13 or later.
2910+
29092911
The Cake build-system is implemented in Python and requires Python 2.7 to be installed.
29102912

29112913
Ensure Python 2.7 interpreter is in your PATH and available as 'python'.
@@ -2938,6 +2940,68 @@ c:\Code\cppcoro> git submodule update --init --recursive
29382940

29392941
### Building from the command-line
29402942

2943+
#### With CMake
2944+
2945+
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):
2946+
2947+
| Flag | Description | Default Value |
2948+
|----------------------|------------------------------|------------------------|
2949+
| BUILD_TESTING | Build the unit tests | ON |
2950+
| BUILD_SHARED_LIBS | Build as a shared library | OFF |
2951+
| CMAKE_BUILD_TYPE | Build as `Debug`/`Release` | <empty> |
2952+
| CMAKE_INSTALL_PREFIX | Where to install the library | `/usr/local` (on Unix) |
2953+
2954+
CMake also respects the [conventional environment variables](https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html):
2955+
2956+
| Environment Variable | Description |
2957+
|----------------------|-------------------------------|
2958+
| CXX | Path to the C++ compiler |
2959+
| CXXFLAGS | C++ compiler flags to prepend |
2960+
| LDFLAGS | Linker flags to prepend |
2961+
2962+
Example:
2963+
2964+
```bash
2965+
cd <this/repo>
2966+
mkdir build
2967+
cd build
2968+
export CXX=clang++
2969+
export CXXFLAGS="-stdlib=libc++ -march=native"
2970+
export LDFLAGS="-stdlib=libc++ -fuse-ld=lld -Wl,--gdb-index"
2971+
cmake .. [-GNinja] -DCMAKE_INSTALL_PREFIX=$HOME/.local -DBUILD_SHARED_LIBS=ON
2972+
ninja # or make -jN
2973+
ninja test # Run the tests
2974+
ninja install
2975+
```
2976+
2977+
The CMake build scripts will also install a `cppcoroConfig.cmake` file for consumers to use.
2978+
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.
2979+
Assuming cppcoro has been installed to `$HOME/.local` like in the example above it can be consumed like this:
2980+
2981+
```cmake
2982+
find_package(cppcoro REQUIRED)
2983+
add_executable(app main.cpp)
2984+
target_link_libraries(app PRIVATE cppcoro::cppcoro)
2985+
```
2986+
2987+
```bash
2988+
$ cmake . -Dcppcoro_ROOT=$HOME/.local
2989+
# ...
2990+
-- Performing Test _CXX_COROUTINES_SUPPORTS_MS_FLAG
2991+
-- Performing Test _CXX_COROUTINES_SUPPORTS_MS_FLAG - Failed
2992+
-- Performing Test _CXX_COROUTINES_SUPPORTS_CORO_FLAG
2993+
-- Performing Test _CXX_COROUTINES_SUPPORTS_CORO_FLAG - Success
2994+
-- Looking for C++ include coroutine
2995+
-- Looking for C++ include coroutine - not found
2996+
-- Looking for C++ include experimental/coroutine
2997+
-- Looking for C++ include experimental/coroutine - found
2998+
-- Configuring done
2999+
-- Generating done
3000+
# ...
3001+
```
3002+
3003+
#### With Cake
3004+
29413005
To build from the command-line just run 'cake.bat' in the workspace root.
29423006

29433007
eg.

cmake/CMakeLists.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)