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

GCC 10.1 support #158

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 36 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.13)
project(cppcoro LANGUAGES CXX)

set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

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

add_subdirectory(lib)
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
DESTINATION .
COMPONENT Devel)
install(FILES cmake/FindCppcoroCoroutines.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 Coroutines_SUPPORTS_MS_FLAG
-- Performing Test Coroutines_SUPPORTS_MS_FLAG - Failed
-- Performing Test Coroutines_SUPPORTS_GNU_FLAG
-- Performing Test Coroutines_SUPPORTS_GNU_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.
37 changes: 37 additions & 0 deletions cmake/FindCppcoroCoroutines.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
include(CheckCXXCompilerFlag)
include(CheckIncludeFileCXX)
include(FindPackageHandleStandardArgs)

check_cxx_compiler_flag(/await Coroutines_SUPPORTS_MS_FLAG)
check_cxx_compiler_flag(-fcoroutines-ts Coroutines_SUPPORTS_CLANG_FLAG)
check_cxx_compiler_flag(-fcoroutines Coroutines_SUPPORTS_GCC_FLAG)
if(Coroutines_SUPPORTS_MS_FLAG OR Coroutines_SUPPORTS_CLANG_FLAG OR Coroutines_SUPPORTS_GCC_FLAG)
set(Coroutines_COMPILER_SUPPORT ON)
endif()

set(Coroutines_ADDITIONAL_FLAG "")
if(Coroutines_SUPPORTS_MS_FLAG)
set(Coroutines_ADDITIONAL_FLAG "/await")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also the (undocumented) /await:heapelide flag for msvc that we probably want to add under Release build configs.

I'm not sure whether it would be more idiomatic to set it here or have the application setting this or perhaps even as part of the CXX_COMPILER_FLAGS on the command-line.

elseif(Coroutines_SUPPORTS_CLANG_FLAG)
set(Coroutines_ADDITIONAL_FLAG "-fcoroutines-ts")
elseif(Coroutines_SUPPORTS_GCC_FLAG)
set(Coroutines_ADDITIONAL_FLAG "-fcoroutines")
endif()

check_include_file_cxx("coroutine" Coroutines_STANDARD_LIBRARY_SUPPORT ${Coroutines_ADDITIONAL_FLAG})
check_include_file_cxx("experimental/coroutine" Coroutines_EXPERIMENTAL_LIBRARY_SUPPORT ${Coroutines_ADDITIONAL_FLAG})

if(Coroutines_EXPERIMENTAL_LIBRARY_SUPPORT OR Coroutines_STANDARD_LIBRARY_SUPPORT)
set(Coroutines_LIBRARY_SUPPORT ON)
endif()

find_package_handle_standard_args(CppcoroCoroutines
REQUIRED_VARS Coroutines_LIBRARY_SUPPORT Coroutines_COMPILER_SUPPORT
FAIL_MESSAGE "Verify that the compiler and the standard library both support the Coroutines TS")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe reword to also be inclusive of C++20 coroutines, not just Coroutines TS?
"... both support either C++20 coroutines or the Coroutines TS"


if(NOT CppcoroCoroutines_FOUND OR TARGET cppcoro::coroutines)
return()
endif()

add_library(cppcoro::coroutines INTERFACE IMPORTED)
target_compile_options(cppcoro::coroutines INTERFACE ${Coroutines_ADDITIONAL_FLAG})
6 changes: 6 additions & 0 deletions cmake/cppcoroConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})

include(CMakeFindDependencyMacro)
find_dependency(CppcoroCoroutines QUIET REQUIRED)

include("${CMAKE_CURRENT_LIST_DIR}/cppcoroTargets.cmake")
6 changes: 3 additions & 3 deletions include/cppcoro/async_auto_reset_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef CPPCORO_ASYNC_AUTO_RESET_EVENT_HPP_INCLUDED
#define CPPCORO_ASYNC_AUTO_RESET_EVENT_HPP_INCLUDED

#include <experimental/coroutine>
#include <cppcoro/coroutine.hpp>
#include <atomic>
#include <cstdint>

Expand Down Expand Up @@ -80,7 +80,7 @@ namespace cppcoro
async_auto_reset_event_operation(const async_auto_reset_event_operation& other) noexcept;

bool await_ready() const noexcept { return m_event == nullptr; }
bool await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept;
bool await_suspend(cppcoro::coroutine_handle<> awaiter) noexcept;
void await_resume() const noexcept {}

private:
Expand All @@ -89,7 +89,7 @@ namespace cppcoro

const async_auto_reset_event* m_event;
async_auto_reset_event_operation* m_next;
std::experimental::coroutine_handle<> m_awaiter;
cppcoro::coroutine_handle<> m_awaiter;
std::atomic<std::uint32_t> m_refCount;

};
Expand Down
Loading