Skip to content

Commit

Permalink
(#5353) Add support for cpr 1.6.2
Browse files Browse the repository at this point in the history
* Add support for cpr 1.6.2

* Limit darwinssl to macos

* Refactor SSL options to use string value

* Style fixes

* Use new deprecation form

* Remove f-strings

* Move overriding of options to configure_options

* Remove stray typechecking import

* Remove unneeded error checking for ssl validators

* Remove unneeded change of libcurl ssl

* Require CURL in 1.6 builds
  • Loading branch information
ollien authored May 12, 2021
1 parent d5786a0 commit 9447db0
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 39 deletions.
7 changes: 7 additions & 0 deletions recipes/cpr/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ sources:
"1.6.0":
sha256: "5a20a9f014b47988f5230d008174e5db2b33f8c547df825e6beae0c78fc05a9e"
url: https://github.com/whoshuu/cpr/archive/1.6.0.tar.gz
"1.6.2":
sha256: "c45f9c55797380c6ba44060f0c73713fbd7989eeb1147aedb8723aa14f3afaa3"
url: https://github.com/whoshuu/cpr/archive/1.6.2.tar.gz

patches:
"1.3.0":
- patch_file: "patches/001-fix-curl-define.patch"
Expand All @@ -39,3 +43,6 @@ patches:
base_path: "source_subfolder"
- patch_file: "patches/007-patch-sanitizer.patch"
base_path: "source_subfolder"
"1.6.2":
- patch_file: "patches/008-fix-curl-components.patch"
base_path: "source_subfolder"
140 changes: 102 additions & 38 deletions recipes/cpr/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@


class CprConan(ConanFile):
_AUTO_SSL = "auto"
_NO_SSL = "off"

name = "cpr"
description = "C++ Requests: Curl for People, a spiritual port of Python Requests"
url = "https://github.com/conan-io/conan-center-index"
Expand All @@ -16,14 +19,16 @@ class CprConan(ConanFile):
options = {
"shared": [True, False],
"fPIC": [True, False],
"with_openssl": [True, False],
"with_winssl": [True, False],
"with_openssl": [True, False, "deprecated"],
"with_winssl": [True, False, "deprecated"],
"with_ssl": ["openssl", "darwinssl", "winssl", _AUTO_SSL, _NO_SSL]
}
default_options = {
"shared": False,
"fPIC": True,
"with_openssl": True,
"with_winssl": False,
"with_openssl": "deprecated",
"with_winssl": "deprecated",
"with_ssl": _AUTO_SSL
}

_cmake = None
Expand All @@ -40,13 +45,28 @@ def _build_subfolder(self):
def _supports_openssl(self):
# https://github.com/whoshuu/cpr/commit/b036a3279ba62720d1e43362d32202bf412ea152
# https://github.com/whoshuu/cpr/releases/tag/1.5.0
return tools.Version(self.version) >= "1.5.0"
return tools.Version(self.version) >= "1.5.0" and not tools.is_apple_os(self.settings.os)

@property
def _supports_winssl(self):
# https://github.com/whoshuu/cpr/commit/18e1fc5c3fc0ffc07695f1d78897fb69e7474ea9
# https://github.com/whoshuu/cpr/releases/tag/1.5.1
return tools.Version(self.version) >= "1.5.1"
return tools.Version(self.version) >= "1.5.1" and self.settings.os == "Windows"

@property
def _supports_darwinssl(self):
# https://github.com/whoshuu/cpr/releases/tag/1.6.1
return tools.Version(self.version) >= "1.6.1" and tools.is_apple_os(self.settings.os)

@property
def _can_auto_ssl(self):
# https://github.com/whoshuu/cpr/releases/tag/1.6.0
return not self._uses_old_cmake_options and not (
# https://github.com/whoshuu/cpr/issues/546
tools.Version(self.version) in ["1.6.0", "1.6.1"]
and tools.is_apple_os(self.settings.os)
)


@property
def _uses_old_cmake_options(self):
Expand All @@ -67,6 +87,15 @@ def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

ssl_library = self._get_ssl_library()
if not self._can_auto_ssl and ssl_library == CprConan._AUTO_SSL:
if self._supports_openssl:
self.output.info("Auto SSL is not available below version 1.6.0. Falling back to openssl")
self.options.with_ssl = "openssl"
else:
self.output.info("Auto SSL is not available below version 1.6.0 (or below 1.6.2 on macOS), and openssl not supported. Disabling SSL")
self.options.with_ssl = CprConan._NO_SSL

def configure(self):
if self.options.shared:
del self.options.fPIC
Expand All @@ -75,14 +104,6 @@ def configure(self):
if not self._supports_winssl:
del self.options.with_winssl

# Make sure libcurl uses the same SSL implementation
if self.options.get_safe("with_openssl", False):
# self.options["libcurl"].with_openssl = True # deprecated in https://github.com/conan-io/conan-center-index/pull/2880
self.options["libcurl"].with_ssl = "openssl"
if self.options.get_safe("with_winssl", False):
# self.options["libcurl"].with_winssl = True # deprecated in https://github.com/conan-io/conan-center-index/pull/2880
self.options["libcurl"].with_ssl = "schannel"


def source(self):
tools.get(**self.conan_data["sources"][self.version])
Expand Down Expand Up @@ -120,42 +141,85 @@ def _configure_cmake(self):
self._cmake.definitions[self._get_cmake_option("CPR_BUILD_TESTS")] = False
self._cmake.definitions[self._get_cmake_option("CPR_GENERATE_COVERAGE")] = False
self._cmake.definitions[self._get_cmake_option("CPR_USE_SYSTEM_GTEST")] = False
if self._supports_openssl:
self._cmake.definitions["CMAKE_USE_OPENSSL"] = self.options.get_safe("with_openssl", False)
if self._supports_winssl: # The CMake options changed
# https://github.com/whoshuu/cpr/commit/18e1fc5c3fc0ffc07695f1d78897fb69e7474ea9#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aR39-R40
self._cmake.definitions[self._get_cmake_option("CPR_FORCE_OPENSSL_BACKEND")] = self.options.get_safe("with_openssl", False)
self._cmake.definitions[self._get_cmake_option("CPR_FORCE_WINSSL_BACKEND")] = self.options.get_safe("with_winssl", False)

supports_any_ssl = self.options.get_safe("with_openssl", False) or self.options.get_safe("with_winssl", False)
if not self._uses_old_cmake_options and not supports_any_ssl:

ssl_value = self._get_ssl_library()
SSL_OPTIONS = {
"CPR_FORCE_DARWINSSL_BACKEND": ssl_value == "darwinssl",
"CPR_FORCE_OPENSSL_BACKEND": ssl_value == "openssl",
"CPR_FORCE_WINSSL_BACKEND": ssl_value == "winssl",
"CMAKE_USE_OPENSSL": ssl_value == "openssl"
}

for cmake_option, value in SSL_OPTIONS.items():
self._cmake.definitions[self._get_cmake_option(cmake_option)] = value

# If we are on a version where disabling SSL requires a cmake option, disable it
if not self._uses_old_cmake_options and self._get_ssl_library() == CprConan._NO_SSL:
self._cmake.definitions["CPR_ENABLE_SSL"] = False

self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake

# Check if the system supports the given ssl library
def _supports_ssl_library(self, library):
if library == CprConan._NO_SSL:
return True
elif library == CprConan._AUTO_SSL:
return self._can_auto_ssl

validators = {
"openssl": self._supports_openssl,
"darwinssl": self._supports_darwinssl,
"winssl": self._supports_winssl,
CprConan._AUTO_SSL: self._can_auto_ssl
}

# A KeyError should never happen, as the options are validated by conan.
return validators[library]

def _print_deprecation_messages(self):
if self.options.get_safe("with_openssl") != "deprecated":
self.output.warn("with_openssl is deprecated. Please use the with_ssl option.")
elif self.options.get_safe("with_winssl") != "deprecated":
self.output.warn("with_winssl is deprecated. Please use the with_ssl option.")

# Get the configured ssl library
def _get_ssl_library(self):
ssl_library = str(self.options.get_safe("with_ssl"))
# These must check for True so that we don't take "deprecated" to be truthy
if self.options.get_safe("with_openssl") == True:
return "openssl"
elif self.options.get_safe("with_winssl") == True:
return "winssl"

return ssl_library

def validate(self):
if not self._uses_valid_abi_and_compiler:
raise ConanInvalidConfiguration("Cannot compile CPR/1.6.0 with libstdc++ on clang < 9")
SSL_FAILURE_MESSAGES = {
"openssl": "OpenSSL is not supported on macOS or on CPR versions < 1.5.0",
"darwinssl": "DarwinSSL is only supported on macOS and on CPR versions >= 1.6.1",
"winssl": "WinSSL is only on Windows and on CPR versions >= 1.5.1",
CprConan._AUTO_SSL: "Automatic SSL selection is only available on CPR versions >= 1.6.0 (and only >= 1.6.2 on macOS)"
}

if self.options.get_safe("with_openssl", False) and tools.is_apple_os(self.settings.os):
# https://github.com/whoshuu/cpr/issues/546
raise ConanInvalidConfiguration("cpr cannot be built on macOS with openssl")
self._print_deprecation_messages()
if not self._uses_valid_abi_and_compiler:
raise ConanInvalidConfiguration("Cannot compile cpr/1.6.0 with libstdc++ on clang < 9")

if self.options.get_safe("with_winssl", False) and self.settings.os != "Windows":
raise ConanInvalidConfiguration("cpr only supports winssl on Windows")
ssl_library = self._get_ssl_library()
if not self._supports_ssl_library(ssl_library):
raise ConanInvalidConfiguration(
"Invalid SSL selection for the given configuration: {}".format(SSL_FAILURE_MESSAGES[ssl_library])
if ssl_library in SSL_FAILURE_MESSAGES
else "Invalid value of ssl option, {}".format(ssl_library)
)

if self.options.get_safe("with_openssl", False) and self.options.get_safe("with_winssl", False):
raise ConanInvalidConfiguration("cpr can not be built with both openssl and winssl")
if ssl_library not in (CprConan._AUTO_SSL, CprConan._NO_SSL) and ssl_library != self.options["libcurl"].with_ssl:
raise ConanInvalidConfiguration("cpr requires libcurl to be built with the option with_ssl='{}'.".format(self.options.get_safe('ssl')))

if self.settings.compiler == "Visual Studio" and self.options.shared and "MT" in self.settings.compiler.runtime:
raise ConanInvalidConfiguration("Visual Studio build for shared library with MT runtime is not supported")

if self.options.get_safe("with_openssl", False) and self.options["libcurl"].with_ssl != "openssl":
raise ConanInvalidConfiguration("cpr requires libcurl to be built with the option with_ssl='openssl'.")

if self.options.get_safe("with_winssl", False) and self.options["libcurl"].with_ssl != "schannel":
raise ConanInvalidConfiguration("cpr requires libcurl to be built with the option with_ssl='schannel'.")


def build(self):
self._patch_sources()
Expand Down
2 changes: 1 addition & 1 deletion recipes/cpr/all/patches/006-fix-curl-components.patch
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- message(FATAL_ERROR "Curl not found on this system. To use the build in version set CPR_FORCE_USE_SYSTEM_CURL to OFF.")
- endif()
- endif()
+ find_package(CURL)
+ find_package(CURL REQUIRED)
+ if(CURL_FOUND)
+ message(STATUS "Curl found on this system.")
else()
Expand Down
36 changes: 36 additions & 0 deletions recipes/cpr/all/patches/008-fix-curl-components.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -120,29 +120,11 @@ endif()

# Curl configuration
if(CPR_FORCE_USE_SYSTEM_CURL)
- if(CPR_ENABLE_SSL)
- find_package(CURL COMPONENTS HTTP HTTPS SSL)
- if(CURL_FOUND)
- message(STATUS "Curl ${CURL_VERSION_STRING} found on this system.")
- # To be able to load certificates under Windows when using OpenSSL:
- if(CMAKE_USE_OPENSSL AND WIN32 AND (NOT (CURL_VERSION_STRING VERSION_GREATER_EQUAL "7.71.0")))
- message(FATAL_ERROR "Your system curl version (${CURL_VERSION_STRING}) is too old to support OpenSSL on Windows which requires curl >= 7.71.0. Update your curl version, use WinSSL, disable SSL or use the build in version of curl.")
- endif()
- else()
- find_package(CURL COMPONENTS HTTP)
- if(CURL_FOUND)
- message(FATAL_ERROR "Curl found on this system but WITHOUT HTTPS/SSL support. Either disable SSL by setting CPR_ENABLE_SSL to OFF or use the build in version of curl by setting CPR_FORCE_USE_SYSTEM_CURL to OFF.")
- else()
- message(FATAL_ERROR "Curl not found on this system. To use the build in version set CPR_FORCE_USE_SYSTEM_CURL to OFF.")
- endif()
- endif()
+ find_package(CURL REQUIRED)
+ if(CURL_FOUND)
+ message(STATUS "Curl found on this system.")
else()
- find_package(CURL COMPONENTS HTTP)
- if(CURL_FOUND)
- message(STATUS "Curl found on this system.")
- else()
- message(FATAL_ERROR "Curl not found on this system. To use the build in version set CPR_FORCE_USE_SYSTEM_CURL to OFF.")
- endif()
+ message(FATAL_ERROR "Curl not found on this system. To use the build in version set CPR_FORCE_USE_SYSTEM_CURL to OFF.")
endif()
else()
message(STATUS "Configuring build in curl...")
2 changes: 2 additions & 0 deletions recipes/cpr/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ versions:
folder: all
"1.6.0":
folder: all
"1.6.2":
folder: all

0 comments on commit 9447db0

Please sign in to comment.