Skip to content

Commit

Permalink
tgbot: migrate to Conan v2
Browse files Browse the repository at this point in the history
  • Loading branch information
valgur committed Jul 22, 2023
1 parent 9eabe04 commit 038abbf
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 58 deletions.
7 changes: 0 additions & 7 deletions recipes/tgbot/all/CMakeLists.txt

This file was deleted.

93 changes: 52 additions & 41 deletions recipes/tgbot/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import os

required_conan_version = ">=1.33.0"
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
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, replace_in_file

required_conan_version = ">=1.53.0"


class TgbotConan(ConanFile):
name = "tgbot"

url = "https://github.com/conan-io/conan-center-index"
homepage = "http://reo7sp.github.io/tgbot-cpp"
description = "C++ library for Telegram bot API"
topics = ("tgbot", "telegram", "telegram-api", "telegram-bot", "bot")
license = "MIT"
url = "https://github.com/conan-io/conan-center-index"
homepage = "http://reo7sp.github.io/tgbot-cpp"
topics = ("telegram", "telegram-api", "telegram-bot", "bot")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
Expand All @@ -24,69 +28,76 @@ class TgbotConan(ConanFile):
"fPIC": True,
}

generators = "cmake", "cmake_find_package"
exports_sources = ["CMakeLists.txt"]

_cmake = None

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

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")

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

def requirements(self):
self.requires("boost/1.79.0")
self.requires("libcurl/7.84.0")
self.requires("openssl/1.1.1q")
self.requires("boost/1.82.0", transitive_headers=True)
self.requires("libcurl/8.1.2", transitive_headers=True)
self.requires("openssl/[>=1.1 <4]")

@property
def _required_boost_components(self):
return ["system"]

def validate(self):
if self.settings.compiler.cppstd:
tools.check_min_cppstd(self, 11)
miss_boost_required_comp = any(getattr(self.options["boost"], "without_{}".format(boost_comp), True) for boost_comp in self._required_boost_components)
if self.options["boost"].header_only or miss_boost_required_comp:
raise ConanInvalidConfiguration("{0} requires non header-only boost with these components: {1}".format(self.name, ", ".join(self._required_boost_components)))
check_min_cppstd(self, 11)
miss_boost_required_comp = any(
self.dependencies["boost"].options.get_safe(f"without_{boost_comp}", True)
for boost_comp in self._required_boost_components
)
if self.dependencies["boost"].options.header_only or miss_boost_required_comp:
raise ConanInvalidConfiguration(
f"{self.name} requires non header-only boost with these components: "
+ ", ".join(self._required_boost_components)
)

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 generate(self):
tc = CMakeToolchain(self)
tc.variables["ENABLE_TESTS"] = False
tc.generate()
tc = CMakeDeps(self)
tc.generate()

def _patch_sources(self):
# Don't force PIC
tools.replace_in_file(
os.path.join(self._source_subfolder, "CMakeLists.txt"),
replace_in_file(
self,
os.path.join(self.source_folder, "CMakeLists.txt"),
"set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)",
""
"",
)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["ENABLE_TESTS"] = False
self._cmake.configure()
return self._cmake

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

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

def package_info(self):
self.cpp_info.libs = ["TgBot"]
self.cpp_info.requires = ["boost::headers", "boost::system", "libcurl::libcurl", "openssl::openssl"]
self.cpp_info.requires = [
"boost::headers",
"boost::system",
"libcurl::libcurl",
"openssl::openssl",
]
7 changes: 2 additions & 5 deletions recipes/tgbot/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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(tgbot REQUIRED)
find_package(tgbot REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
Expand Down
20 changes: 15 additions & 5 deletions recipes/tgbot/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_layout, CMake
import os
from conans import ConanFile, CMake, tools


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake_find_package", "cmake"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain"
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):
self.run(os.path.join("bin", "test_package"), 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/tgbot/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/)
16 changes: 16 additions & 0 deletions recipes/tgbot/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_find_package", "cmake"

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

def test(self):
if not tools.cross_building(self):
self.run(os.path.join("bin", "test_package"), run_environment=True)

0 comments on commit 038abbf

Please sign in to comment.