Repository: conan-io/conan-center-index

Issue:
[request] nuraft/cci.20220801
### Package Details
  * Package Name/Version: **nuraft/cci.20220801**
  * Website: **https://github.com/eBay/NuRaft**
  * Source code: **https://github.com/eBay/NuRaft**


### Description Of The Library / Tool

A RAFT implementation developed at eBay Inc.


Discussion:

Resolving patch:
diff --git a/recipes/nuraft/all/conanfile.py b/recipes/nuraft/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/nuraft/all/conanfile.py
@@ -0,0 +1,78 @@
+from os.path import join
+from conan import ConanFile
+from conan.errors import ConanInvalidConfiguration
+from conan.tools.files import copy, get, apply_conandata_patches
+from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain
+from conan.tools.build import check_min_cppstd
+
+required_conan_version = ">=1.50.0"
+
+class NuRaftConan(ConanFile):
+    name = "nuraft"
+    homepage = "https://github.corp.ebay.com/sds/NuRaft"
+    description = """Cornerstone based RAFT library."""
+    topics = ("raft")
+    url = "https://github.com/conan-io/conan-center-index"
+    license = "Apache-2.0"
+
+    settings = "os", "compiler", "arch", "build_type"
+
+    options = {
+        "asio": ["boost", "standalone"],
+        "shared": ['True', 'False'],
+        "fPIC": ['True', 'False'],
+    }
+    default_options = {
+        "asio": "boost",
+        "shared": False,
+        "fPIC": True,
+    }
+
+    def export_sources(self):
+        for p in self.conan_data.get("patches", {}).get(self.version, []):
+            copy(self, p["patch_file"], self.recipe_folder, self.export_sources_folder)
+
+    def requirements(self):
+        if self.options.asio == "boost":
+            self.requires("boost/1.79.0")
+        else:
+            self.requires("asio/1.22.1")
+        self.requires("openssl/1.1.1s")
+
+    def validate(self):
+        if self.info.settings.os in ["Windows"]:
+            raise ConanInvalidConfiguration("{} Builds are unsupported".format(self.info.settings.os))
+        if self.info.settings.os in ["Macos"] and self.options.shared:
+            raise ConanInvalidConfiguration("Building Shared Object for {} unsupported".format(self.info.settings.os))
+        if self.info.settings.compiler.cppstd:
+            check_min_cppstd(self, 11)
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+
+    def source(self):
+        get(self, **self.conan_data["sources"][self.version], strip_root=True, destination=self.source_folder)
+
+    def generate(self):
+        tc = CMakeToolchain(self)
+        tc.generate()
+        cmake = CMakeDeps(self)
+        cmake.generate()
+
+    def build(self):
+        apply_conandata_patches(self)
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def package(self):
+        cmake = CMake(self)
+        cmake.install()
+        copy(self, "LICENSE", self.source_folder, join(self.package_folder, "licenses"), keep_path=False)
+
+    def package_info(self):
+        self.cpp_info.libs = ["nuraft"]
+        self.cpp_info.system_libs = ["m"]
+        if self.settings.os in ["Linux", "FreeBSD"]:
+            self.cpp_info.system_libs.extend(["pthread"])


Test patch:
diff --git a/recipes/nuraft/all/test_package/CMakeLists.txt b/recipes/nuraft/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/nuraft/all/test_package/CMakeLists.txt
@@ -0,0 +1,12 @@
+cmake_minimum_required(VERSION 3.8)
+project(test_package LANGUAGES CXX)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup(TARGETS)
+
+find_package(nuraft CONFIG REQUIRED)
+
+add_executable(${PROJECT_NAME})
+target_sources(${PROJECT_NAME} PRIVATE test_package.cpp)
+target_link_libraries(${PROJECT_NAME} nuraft::nuraft)
+target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_11)
diff --git a/recipes/nuraft/all/test_package/conanfile.py b/recipes/nuraft/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/nuraft/all/test_package/conanfile.py
@@ -0,0 +1,18 @@
+import os
+from conan import ConanFile
+from conans import CMake
+from conan.tools.build import cross_building
+
+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 cross_building(self):
+            bin_path = os.path.join("bin", "test_package")
+            self.run(bin_path, run_environment=True)
diff --git a/recipes/nuraft/all/test_package/test_package.cpp b/recipes/nuraft/all/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/nuraft/all/test_package/test_package.cpp
@@ -0,0 +1,61 @@
+#include <iostream>
+#include <libnuraft/nuraft.hxx>
+
+struct ex_logger : ::nuraft::logger {
+  void set_level(int) override {}
+  void debug(const std::string&) override {}
+  void info(const std::string&) override {}
+  void warn(const std::string&) override {}
+  void err(const std::string& log_line) override {
+    std::cout << log_line << std::endl;
+  }
+
+  void put_details(int, const char*, const char*, size_t,
+                   const std::string&) override {}
+};
+
+class echo_state_machine : public nuraft::state_machine {
+  nuraft::ulong last_commit_idx_;
+
+ public:
+  echo_state_machine() : last_commit_idx_(0) {}
+
+  nuraft::ptr<nuraft::buffer> commit(const nuraft::ulong log_idx,
+                                     nuraft::buffer&) override {
+    last_commit_idx_ = log_idx;
+    return nullptr;
+  }
+  nuraft::ptr<nuraft::buffer> pre_commit(const nuraft::ulong,
+                                         nuraft::buffer&) override {
+    return nullptr;
+  }
+  void rollback(const nuraft::ulong, nuraft::buffer&) override {}
+  void save_snapshot_data(nuraft::snapshot&, const nuraft::ulong,
+                          nuraft::buffer&) override {}
+  bool apply_snapshot(nuraft::snapshot&) override { return true; }
+  int read_snapshot_data(nuraft::snapshot&, const nuraft::ulong,
+                         nuraft::buffer&) override {
+    return 0;
+  }
+  nuraft::ptr<nuraft::snapshot> last_snapshot() override { return nullptr; }
+  void create_snapshot(nuraft::snapshot&,
+                       nuraft::async_result<bool>::handler_type&) override {}
+  nuraft::ulong last_commit_index() override { return last_commit_idx_; }
+};
+
+int main(int argc, char** argv) {
+  // State machine.
+  nuraft::ptr<nuraft::state_machine> smachine(
+      nuraft::cs_new<echo_state_machine>());
+
+  // Parameters.
+  nuraft::raft_params params;
+
+  // ASIO service.
+  nuraft::ptr<nuraft::logger> l = nuraft::cs_new<ex_logger>();
+  nuraft::ptr<nuraft::asio_service> asio_svc_ =
+      nuraft::cs_new<nuraft::asio_service>();
+  nuraft::ptr<nuraft::rpc_listener> listener(
+      asio_svc_->create_rpc_listener((ushort)(9001), l));
+  return 0;
+}