Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake support #174

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.12)
project(cppcoro LANGUAGES CXX)

set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

add_subdirectory(lib)

enable_testing()
if(BUILD_TESTING)
add_subdirectory(test)
endif()

export(EXPORT cppcoroTargets
FILE "${PROJECT_BINARY_DIR}/cppcoro/cppcoroTargets.cmake"
NAMESPACE cppcoro::)
configure_file(cmake/cppcoroConfig.cmake
"${PROJECT_BINARY_DIR}/cppcoro/cppcoroConfig.cmake"
COPYONLY)

set(config_package_location lib/cmake/cppcoro)
install(DIRECTORY include/cppcoro
DESTINATION include
COMPONENT Devel)
install(FILES cmake/FindCoroutines.cmake
DESTINATION ${config_package_location}
COMPONENT Devel)
install(EXPORT cppcoroTargets
FILE cppcoroTargets.cmake
NAMESPACE cppcoro::
DESTINATION ${config_package_location})
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/cppcoro/cppcoroConfig.cmake
DESTINATION ${config_package_location}
COMPONENT Devel)
68 changes: 66 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2859,18 +2859,20 @@ Given a type, `S`, that implements the `DelayedScheduler` and an instance, `s` o

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

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

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

## Building on Windows

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

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

### Prerequisites

The CMakeLists requires version 3.13 or later.

The Cake build-system is implemented in Python and requires Python 2.7 to be installed.

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

### Building from the command-line

#### With CMake

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):

| Flag | Description | Default Value |
|----------------------|------------------------------|------------------------|
| BUILD_TESTING | Build the unit tests | ON |
| BUILD_SHARED_LIBS | Build as a shared library | OFF |
| CMAKE_BUILD_TYPE | Build as `Debug`/`Release` | <empty> |
| CMAKE_INSTALL_PREFIX | Where to install the library | `/usr/local` (on Unix) |

CMake also respects the [conventional environment variables](https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html):

| Environment Variable | Description |
|----------------------|-------------------------------|
| CXX | Path to the C++ compiler |
| CXXFLAGS | C++ compiler flags to prepend |
| LDFLAGS | Linker flags to prepend |

Example:

```bash
cd <this/repo>
mkdir build
cd build
export CXX=clang++
export CXXFLAGS="-stdlib=libc++ -march=native"
export LDFLAGS="-stdlib=libc++ -fuse-ld=lld -Wl,--gdb-index"
cmake .. [-GNinja] -DCMAKE_INSTALL_PREFIX=$HOME/.local -DBUILD_SHARED_LIBS=ON
ninja # or make -jN
ninja test # Run the tests
ninja install
```

The CMake build scripts will also install a `cppcoroConfig.cmake` file for consumers to use.
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.
Assuming cppcoro has been installed to `$HOME/.local` like in the example above it can be consumed like this:

```cmake
find_package(cppcoro REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE cppcoro::cppcoro)
```

```bash
$ cmake . -Dcppcoro_ROOT=$HOME/.local
# ...
-- Performing Test _CXX_COROUTINES_SUPPORTS_MS_FLAG
-- Performing Test _CXX_COROUTINES_SUPPORTS_MS_FLAG - Failed
-- Performing Test _CXX_COROUTINES_SUPPORTS_CORO_FLAG
-- Performing Test _CXX_COROUTINES_SUPPORTS_CORO_FLAG - Success
-- Looking for C++ include coroutine
-- Looking for C++ include coroutine - not found
-- Looking for C++ include experimental/coroutine
-- Looking for C++ include experimental/coroutine - found
-- Configuring done
-- Generating done
# ...
```

#### With Cake

To build from the command-line just run 'cake.bat' in the workspace root.

eg.
Expand Down
Empty file added cmake/CMakeLists.txt
Empty file.
Loading