Repository: conan-io/conan-center-index

Issue:
[request] cppcoro/latest
### Package Details
  * Package Name/Version: **cppcoro/latest**, possibly `cci.20201012` on a87e97f
  * Website: **https://github.com/lewissbaker/cppcoro**
  * Source code: **https://github.com/lewissbaker/cppcoro**


### Description Of The Library / Tool
cppcoro is a support library for C++20 coroutines.
Relevant wishlist issue: https://github.com/conan-io/wishlist/issues/242


Discussion:

Resolving patch:
diff --git a/recipes/andreasbuhr-cppcoro/all/conanfile.py b/recipes/andreasbuhr-cppcoro/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/andreasbuhr-cppcoro/all/conanfile.py
@@ -0,0 +1,108 @@
+import os
+from conans import ConanFile, tools, CMake
+from conans.errors import ConanInvalidConfiguration
+
+required_conan_version = ">=1.33.0"
+
+class AndreasbuhrCppCoroConan(ConanFile):
+    name = "andreasbuhr-cppcoro"
+    description = "A library of C++ coroutine abstractions for the coroutines TS"
+    topics = ("conan", "cpp", "async", "coroutines")
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/andreasbuhr/cppcoro"
+    license = "MIT"
+    settings = "os", "compiler", "build_type", "arch"
+    provides = "cppcoro"
+
+    exports_sources = "CMakeLists.txt"
+    generators = "cmake"
+
+    options = {
+        "shared": [True, False],
+        "fPIC": [True, False],
+    }
+    default_options = {
+        "shared": False,
+        "fPIC": True,
+    }
+
+    _cmake = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _minimum_compilers_version(self):
+        return {
+            "Visual Studio": "15",
+            "gcc": "10",
+            "clang": "8",
+            "apple-clang": "10",
+        }
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.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
+        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))
+        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))
+
+        # 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))
+
+        # TODO remove once figured out why clang 11/libc++ doesn't build on CCI
+        # (due to unable to find std::experiemental::noop_coroutine, author is unable to reproduce on local machine)
+        if self.settings.compiler == "clang" and self.settings.compiler.version == "11":
+            raise ConanInvalidConfiguration("WIP: {} currently doesn't build on clang 11".format(self.name))
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+
+    def source(self):
+       tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
+
+    def _configure_cmake(self):
+        if not self._cmake:
+            self._cmake = CMake(self)
+            self._cmake.configure()
+        return self._cmake
+
+    def build(self):
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def package(self):
+        self.copy("LICENSE.txt", dst="licenses", src=self._source_subfolder)
+        cmake = self._configure_cmake()
+        cmake.install()
+        tools.rmdir(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"
+
+        if self.settings.compiler == "Visual Studio":
+            comp.cxxflags.append("/await")
+        elif self.settings.compiler == "gcc":
+            comp.cxxflags.append("-fcoroutines")
+        elif self.settings.compiler == "clang" or self.settings.compiler == "apple-clang":
+            comp.cxxflags.append("-fcoroutines-ts")


Test patch:
diff --git a/recipes/andreasbuhr-cppcoro/all/test_package/CMakeLists.txt b/recipes/andreasbuhr-cppcoro/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/andreasbuhr-cppcoro/all/test_package/CMakeLists.txt
@@ -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)
diff --git a/recipes/andreasbuhr-cppcoro/all/test_package/conanfile.py b/recipes/andreasbuhr-cppcoro/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/andreasbuhr-cppcoro/all/test_package/conanfile.py
@@ -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)
diff --git a/recipes/andreasbuhr-cppcoro/all/test_package/test_package.cpp b/recipes/andreasbuhr-cppcoro/all/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/andreasbuhr-cppcoro/all/test_package/test_package.cpp
@@ -0,0 +1,24 @@
+#include <vector>
+#include <iostream>
+#include <cppcoro/generator.hpp>
+
+cppcoro::generator<int> intYielder() {
+	co_yield 0;
+	co_yield 1;
+}
+
+int main() {
+	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;
+    }
+}