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

andreasbuhr-cppcoro: Conan 2.0 support #20611

Merged
merged 19 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 0 additions & 7 deletions recipes/andreasbuhr-cppcoro/all/CMakeLists.txt

This file was deleted.

95 changes: 46 additions & 49 deletions recipes/andreasbuhr-cppcoro/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
from conans import ConanFile, tools, CMake
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import rmdir, get, copy
from conan.tools.scm import Version
from conan.errors import ConanInvalidConfiguration

required_conan_version = ">=1.33.0"
Ahajha marked this conversation as resolved.
Show resolved Hide resolved
required_conan_version = ">=1.49.0"

class AndreasbuhrCppCoroConan(ConanFile):
name = "andreasbuhr-cppcoro"
Expand All @@ -14,9 +17,6 @@ class AndreasbuhrCppCoroConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
provides = "cppcoro"

exports_sources = "CMakeLists.txt"
generators = "cmake"

options = {
Ahajha marked this conversation as resolved.
Show resolved Hide resolved
"shared": [True, False],
"fPIC": [True, False],
Expand All @@ -26,12 +26,6 @@ class AndreasbuhrCppCoroConan(ConanFile):
"fPIC": True,
}

_cmake = None

@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _minimum_compilers_version(self):
return {
Expand All @@ -41,72 +35,75 @@ def _minimum_compilers_version(self):
"apple-clang": "10",
}

def layout(self):
cmake_layout(self, src_folder="src")

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
self.options.rm_safe("fPIC")

def validate(self):
# We can't simply check for C++20, because clang and MSVC support the coroutine TS despite not having labeled (__cplusplus macro) C++20 support
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
min_version = self._minimum_compilers_version.get(str(self.settings.compiler))
if not min_version:
self.output.warn("{} recipe lacks information about the {} compiler support.".format(
self.name, self.settings.compiler))
self.output.warning(f"{self.name} recipe lacks information about the {self.settings.compiler} compiler support.")
else:
if tools.Version(self.settings.compiler.version) < min_version:
raise ConanInvalidConfiguration("{} requires coroutine TS support. The current compiler {} {} does not support it.".format(
self.name, self.settings.compiler, self.settings.compiler.version))
if Version(self.settings.compiler.version) < min_version:
raise ConanInvalidConfiguration(
f"{self.name} requires coroutine TS support. The current compiler {self.settings.compiler} {self.settings.compiler.version} does not support it."
)

# Currently clang expects coroutine to be implemented in a certain way (under std::experiemental::), while libstdc++ puts them under std::
# There are also other inconsistencies, see https://bugs.llvm.org/show_bug.cgi?id=48172
# This should be removed after both gcc and clang implements the final coroutine TS
if self.settings.compiler == "clang" and self.settings.compiler.get_safe("libcxx") == "libstdc++":
raise ConanInvalidConfiguration("{} does not support clang with libstdc++. Use libc++ instead.".format(self.name))
raise ConanInvalidConfiguration(f"{self.name} does not support clang with libstdc++. Use libc++ instead.")
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved

def configure(self):
if self.options.shared:
del self.options.fPIC
self.options.rm_safe("fPIC")

def source(self):
tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def _configure_cmake(self):
if not self._cmake:
self._cmake = CMake(self)
if self.settings.os == "Windows" and self.options.shared:
self._cmake.definitions["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = "ON"
self._cmake.configure()
return self._cmake
def generate(self):
tc = CMakeToolchain(self)
if self.settings.os == "Windows" and self.options.shared:
tc.variables["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = "ON"
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
tc.generate()

def build(self):
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
self.copy("LICENSE.txt", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
copy(self, "LICENSE.txt", dst="licenses", src=self.source_folder)
cmake = CMake(self)
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.filenames["cmake_find_package"] = "cppcoro"
self.cpp_info.filenames["cmake_find_package_multi"] = "cppcoro"
self.cpp_info.names["cmake_find_package"] = "cppcoro"
self.cpp_info.names["cmake_find_package_multi"] = "cppcoro"

comp = self.cpp_info.components["cppcoro"]
comp.names["cmake_find_package"] = "cppcoro"
comp.names["cmake_find_package_multi"] = "cppcoro"
comp.libs = ["cppcoro"]
self.cpp_info.libs = ["cppcoro"]

if self.settings.os == "Linux" and self.options.shared:
comp.system_libs = ["pthread"]
self.cpp_info.system_libs = ["pthread"]
if self.settings.os == "Windows":
comp.system_libs = ["synchronization"]
self.cpp_info.system_libs = ["synchronization"]

if self.settings.compiler == "Visual Studio":
comp.cxxflags.append("/await")
if self.settings.compiler == "msvc":
self.cpp_info.cxxflags.append("/await")
elif self.settings.compiler == "gcc":
comp.cxxflags.append("-fcoroutines")
comp.defines.append("CPPCORO_COMPILER_SUPPORTS_SYMMETRIC_TRANSFER=1")
elif self.settings.compiler == "clang" or self.settings.compiler == "apple-clang":
comp.cxxflags.append("-fcoroutines-ts")
self.cpp_info.cxxflags.append("-fcoroutines")
self.cpp_info.defines.append("CPPCORO_COMPILER_SUPPORTS_SYMMETRIC_TRANSFER=1")
elif self.settings.compiler in ["clang", "apple-clang"]:
self.cpp_info.cxxflags.append("-fcoroutines-ts")
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved

self.cpp_info.set_property("cmake_file_name", "cppcoro")
self.cpp_info.set_property("cmake_target_name", "cppcoro::cppcoro")

# TODO: to remove in conan v2 once cmake_find_package_* generators removed
self.cpp_info.filenames["cmake_find_package"] = "cppcoro"
self.cpp_info.filenames["cmake_find_package_multi"] = "cppcoro"
self.cpp_info.names["cmake_find_package"] = "cppcoro"
self.cpp_info.names["cmake_find_package_multi"] = "cppcoro"
5 changes: 1 addition & 4 deletions recipes/andreasbuhr-cppcoro/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.15)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

find_package(cppcoro CONFIG REQUIRED)

add_executable(${PROJECT_NAME} test_package.cpp)
Expand Down
22 changes: 16 additions & 6 deletions recipes/andreasbuhr-cppcoro/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os
from conans import ConanFile, CMake, tools


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self.settings):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
11 changes: 11 additions & 0 deletions recipes/andreasbuhr-cppcoro/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.1)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

find_package(cppcoro CONFIG REQUIRED)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} cppcoro::cppcoro)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
16 changes: 16 additions & 0 deletions recipes/andreasbuhr-cppcoro/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
from conans import ConanFile, CMake, tools

class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self.settings):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
32 changes: 32 additions & 0 deletions recipes/andreasbuhr-cppcoro/all/test_v1_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <vector>
#include <iostream>
#include <cppcoro/generator.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>

cppcoro::generator<int> intYielder() {
co_yield 0;
co_yield 1;
}

cppcoro::task<int> task() {
co_return 42;
}

int main() {
auto _ = cppcoro::sync_wait(task());

std::vector<int> v;
for (int n : intYielder()) {
std::cout << "yielded " << n << '\n';
v.push_back(n);
}

bool success = v[0] == 0 && v[1] == 1;
if (success) {
std::cout << "success";
return 0;
} else {
return 1;
}
}