Skip to content

Commit

Permalink
(#18804) enjincppsdk: migrate to Conan v2
Browse files Browse the repository at this point in the history
* enjincppsdk: migrate to Conan v2

* enjincppsdk: fix RapidJSON version check
  • Loading branch information
valgur authored Nov 8, 2023
1 parent 734b1ec commit a9cd59b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 77 deletions.
7 changes: 0 additions & 7 deletions recipes/enjincppsdk/all/CMakeLists.txt

This file was deleted.

120 changes: 62 additions & 58 deletions recipes/enjincppsdk/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import os

required_conan_version = ">=1.43.0"
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import is_apple_os
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rmdir, save, replace_in_file
from conan.tools.scm import Version

required_conan_version = ">=1.53.0"


class EnjinCppSdk(ConanFile):
name = "enjincppsdk"
description = "A C++ SDK for development on the Enjin blockchain platform."
license = "Apache-2.0"
topics = ("enjin", "sdk", "blockchain")
homepage = "https://github.com/enjin/enjin-cpp-sdk"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/enjin/enjin-cpp-sdk"
topics = ("enjin", "sdk", "blockchain")

settings = "os", "compiler", "arch", "build_type"
generators = "cmake", "cmake_find_package", "cmake_find_package_multi"

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
Expand All @@ -29,108 +34,107 @@ class EnjinCppSdk(ConanFile):
"with_default_ws_client": False,
}

_cmake = None
short_paths = True

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

@property
def _build_subfolder(self):
return "build_subfolder"

@property
def _minimum_compilers_version(self):
return {
"Visual Studio": "16",
"msvc": "192",
"gcc": "9",
"clang": "10",
"apple-clang": "12",
}

def export_sources(self):
self.copy("CMakeLists.txt")

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

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

if self.options.with_default_http_client:
self.options["cpp-httplib"].with_openssl = True

self.options["spdlog"].header_only = True

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

def requirements(self):
if self.options.with_default_http_client:
self.requires("cpp-httplib/0.8.5")
self.requires("cpp-httplib/0.14.1")

if self.options.with_default_ws_client:
self.requires("ixwebsocket/11.0.4")

self.requires("rapidjson/1.1.0")
self.requires("spdlog/1.8.2")
self.requires("ixwebsocket/11.4.3")

def build_requirements(self):
self.build_requires("cmake/3.16.9")
self.requires("rapidjson/cci.20220822")
self.requires("spdlog/1.12.0")

def validate(self):
# Validations for OS
if self.settings.os == "Macos":
raise ConanInvalidConfiguration("macOS is not supported at this time. Contributions are welcomed.")
if is_apple_os(self):
raise ConanInvalidConfiguration(f"{self.settings.os} is not supported at this time. Contributions are welcomed.")

# Validations for minimum required C++ standard
compiler = self.settings.compiler

if compiler.get_safe("cppstd"):
tools.check_min_cppstd(self, 17)

check_min_cppstd(self, 17)
minimum_version = self._minimum_compilers_version.get(str(compiler), False)
if not minimum_version:
self.output.warn("C++17 support is required. Your compiler is unknown. Assuming it supports C++17.")
elif tools.Version(compiler.version) < minimum_version:
self.output.warning("C++17 support is required. Your compiler is unknown. Assuming it supports C++17.")
elif Version(compiler.version) < minimum_version:
raise ConanInvalidConfiguration("C++17 support is required, which your compiler does not support.")

if compiler == "clang" and compiler.libcxx != "libstdc++11":
raise ConanInvalidConfiguration("libstdc++11 is required for clang.")

# Validations for dependencies
if not self.options["spdlog"].header_only:
if not self.dependencies["spdlog"].options.header_only:
raise ConanInvalidConfiguration(f"{self.name} requires spdlog:header_only=True to be enabled.")

if self.options.with_default_http_client and not self.options["cpp-httplib"].with_openssl:
raise ConanInvalidConfiguration(f"{self.name} requires cpp-httplib:with_openssl=True when using "
f"with_default_http_client=True.")

def source(self):
tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
if self.options.with_default_http_client and not self.dependencies["cpp-httplib"].options.with_openssl:
raise ConanInvalidConfiguration(
f"{self.name} requires cpp-httplib:with_openssl=True when using with_default_http_client=True."
)

def _configure_cmake(self):
if self._cmake:
return self._cmake
def build_requirements(self):
self.tool_requires("cmake/[>=3.16]")

self._cmake = CMake(self)
self._cmake.definitions["ENJINSDK_BUILD_SHARED"] = self.options.shared
self._cmake.definitions["ENJINSDK_BUILD_TESTS"] = False
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["ENJINSDK_BUILD_SHARED"] = self.options.shared
tc.variables["ENJINSDK_BUILD_TESTS"] = False
tc.generate()
tc = CMakeDeps(self)
tc.generate()

def _patch_sources(self):
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"), "RAPIDJSON", "RapidJSON")
# Remove version check for RapidJSON
save(self, os.path.join(self.source_folder, "cmake", "enjinsdk_find_rapidjson.cmake"),
"find_package(RapidJSON REQUIRED CONFIG)")

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

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

def package_info(self):
self.cpp_info.libs = ["enjinsdk"]
self.cpp_info.set_property("cmake_file_name", "enjinsdk")
self.cpp_info.set_property("cmake_target_name", "enjinsdk::enjinsdk")

# TODO: to remove in conan v2 once cmake_find_package_* generators removed
self.cpp_info.names["cmake_find_package"] = "enjinsdk"
self.cpp_info.names["cmake_find_package_multi"] = "enjinsdk"
self.cpp_info.libs = ["enjinsdk"]
5 changes: 1 addition & 4 deletions recipes/enjincppsdk/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 CXX)

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

find_package(enjinsdk REQUIRED CONFIG)

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

required_conan_version = ">=1.43.0"


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

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

def layout(self):
cmake_layout(self)

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

def test(self):
if not tools.cross_building(self):
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.bindir, "test_package")
self.run(bin_path, env="conanrun")
8 changes: 8 additions & 0 deletions recipes/enjincppsdk/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package)

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

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/
${CMAKE_CURRENT_BINARY_DIR}/test_package/)
19 changes: 19 additions & 0 deletions recipes/enjincppsdk/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from conans import ConanFile, CMake, tools
import os

required_conan_version = ">=1.43.0"


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):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit a9cd59b

Please sign in to comment.