Skip to content

Commit

Permalink
[openssl 3.x.x] fix "official" OpenSSL vars for CMakeDeps gen
Browse files Browse the repository at this point in the history
this extends the improvements of PR conan-io#12838 done for the 1.x.x recipe
to the 3.x.x one; also there is a new 3.x.x test_package using
the CMakeDeps generator
  • Loading branch information
jngrb committed Nov 25, 2022
1 parent f4d0d6f commit b51d60a
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 44 deletions.
24 changes: 18 additions & 6 deletions recipes/openssl/3.x.x/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,20 +659,19 @@ def package(self):
if not self.options.no_fips:
provdir = os.path.join(self._source_subfolder, "providers")
if self.settings.os == "Macos":
self.copy("fips.dylib", src=provdir,dst="lib/ossl-modules")
self.copy("fips.dylib", src=provdir, dst="lib/ossl-modules")
elif self.settings.os == "Windows":
self.copy("fips.dll", src=provdir,dst="lib/ossl-modules")
self.copy("fips.dll", src=provdir, dst="lib/ossl-modules")
else:
self.copy("fips.so", src=provdir,dst="lib/ossl-modules")
self.copy("fips.so", src=provdir, dst="lib/ossl-modules")

rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))

self._create_cmake_module_variables(
os.path.join(self.package_folder, self._module_file_rel_path)
)

@staticmethod
def _create_cmake_module_variables(module_file):
def _create_cmake_module_variables(self, module_file):
content = textwrap.dedent("""\
set(OPENSSL_FOUND TRUE)
if(DEFINED OpenSSL_INCLUDE_DIR)
Expand All @@ -684,21 +683,33 @@ def _create_cmake_module_variables(module_file):
${OpenSSL_Crypto_DEPENDENCIES}
${OpenSSL_Crypto_FRAMEWORKS}
${OpenSSL_Crypto_SYSTEM_LIBS})
elseif(DEFINED openssl_OpenSSL_Crypto_LIBS_%(config)s)
set(OPENSSL_CRYPTO_LIBRARY ${openssl_OpenSSL_Crypto_LIBS_%(config)s})
set(OPENSSL_CRYPTO_LIBRARIES ${openssl_OpenSSL_Crypto_LIBS_%(config)s}
${openssl_OpenSSL_Crypto_DEPENDENCIES_%(config)s}
${openssl_OpenSSL_Crypto_FRAMEWORKS_%(config)s}
${openssl_OpenSSL_Crypto_SYSTEM_LIBS_%(config)s})
endif()
if(DEFINED OpenSSL_SSL_LIBS)
set(OPENSSL_SSL_LIBRARY ${OpenSSL_SSL_LIBS})
set(OPENSSL_SSL_LIBRARIES ${OpenSSL_SSL_LIBS}
${OpenSSL_SSL_DEPENDENCIES}
${OpenSSL_SSL_FRAMEWORKS}
${OpenSSL_SSL_SYSTEM_LIBS})
elseif(DEFINED openssl_OpenSSL_SSL_LIBS_%(config)s)
set(OPENSSL_SSL_LIBRARY ${openssl_OpenSSL_SSL_LIBS_%(config)s})
set(OPENSSL_SSL_LIBRARIES ${openssl_OpenSSL_SSL_LIBS_%(config)s}
${openssl_OpenSSL_SSL_DEPENDENCIES_%(config)s}
${openssl_OpenSSL_SSL_FRAMEWORKS_%(config)s}
${openssl_OpenSSL_SSL_SYSTEM_LIBS_%(config)s})
endif()
if(DEFINED OpenSSL_LIBRARIES)
set(OPENSSL_LIBRARIES ${OpenSSL_LIBRARIES})
endif()
if(DEFINED OpenSSL_VERSION)
set(OPENSSL_VERSION ${OpenSSL_VERSION})
endif()
""")
""" % {"config":str(self.settings.build_type).upper()})
tools.save(module_file, content)

@property
Expand All @@ -713,6 +724,7 @@ def _module_file_rel_path(self):
def package_info(self):
self.cpp_info.set_property("cmake_file_name", "OpenSSL")
self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_build_modules", [self._module_file_rel_path])
self.cpp_info.set_property("pkg_config_name", "openssl")
self.cpp_info.names["cmake_find_package"] = "OpenSSL"
self.cpp_info.names["cmake_find_package_multi"] = "OpenSSL"
Expand Down
30 changes: 19 additions & 11 deletions recipes/openssl/3.x.x/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)

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

option(OPENSSL_WITH_ZLIB "OpenSSL with zlib support" ON)
option(OPENSSL_WITH_LEGACY "OpenSSL with support for the legacy provider" ON)
option(OPENSSL_WITH_MD4 "OpenSSL with MD4 support (needs legacy provider)" ON)
Expand All @@ -12,14 +9,25 @@ option(OPENSSL_WITH_RIPEMD160 "OpenSSL with RIPEMD16 support (needs legacy provi
set(OpenSSL_DEBUG 1)
find_package(OpenSSL REQUIRED)

message("OPENSSL_FOUND: ${OPENSSL_FOUND}")
message("OPENSSL_INCLUDE_DIR: ${OPENSSL_INCLUDE_DIR}")
message("OPENSSL_CRYPTO_LIBRARY: ${OPENSSL_CRYPTO_LIBRARY}")
message("OPENSSL_CRYPTO_LIBRARIES: ${OPENSSL_CRYPTO_LIBRARIES}")
message("OPENSSL_SSL_LIBRARY: ${OPENSSL_SSL_LIBRARY}")
message("OPENSSL_SSL_LIBRARIES: ${OPENSSL_SSL_LIBRARIES}")
message("OPENSSL_LIBRARIES: ${OPENSSL_LIBRARIES}")
message("OPENSSL_VERSION: ${OPENSSL_VERSION}")
# Test whether variables from https://cmake.org/cmake/help/latest/module/FindOpenSSL.html
# are properly defined in conan generators
set(_custom_vars
OPENSSL_FOUND
OPENSSL_INCLUDE_DIR
OPENSSL_CRYPTO_LIBRARY
OPENSSL_CRYPTO_LIBRARIES
OPENSSL_SSL_LIBRARY
OPENSSL_SSL_LIBRARIES
OPENSSL_LIBRARIES
OPENSSL_VERSION
)
foreach(_custom_var ${_custom_vars})
if(DEFINED ${_custom_var} AND NOT "${${_custom_var}}" STREQUAL "" )
message(STATUS "${_custom_var}: ${${_custom_var}}")
else()
message(FATAL_ERROR "${_custom_var} not defined")
endif()
endforeach()

add_executable(digest digest.c)
if(OPENSSL_WITH_ZLIB)
Expand Down
81 changes: 54 additions & 27 deletions recipes/openssl/3.x.x/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@
from conans import CMake, tools, ConanFile
from conan.tools.build import cross_building
from conan import ConanFile
from conan.tools.scm import Version
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake, CMakeToolchain
import os

required_conan_version = ">=1.50.2 <1.51.0 || >=1.51.2"


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

@property
def _skip_test(self):
# Attempting to use @rpath without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being
# set. This could be because you are using a Mac OS X version less than 10.5
# or because CMake's platform configuration is corrupt.
# FIXME: Remove once CMake on macOS/M1 CI runners is upgraded.
# Actually the workaround should be to add cmake/3.22.0 to build requires,
# but for the specific case of openssl it fails because it is also a requirement of cmake.
# see https://github.com/conan-io/conan/pull/9839
return self.settings.os == "Macos" and self.settings.arch == "armv8" \
and self.options["openssl"].shared

@property
def _with_legacy(self):
return (not self.options["openssl"].no_legacy and
((not self.options["openssl"].no_md4) or
(not self.options["openssl"].no_rmd160)))
openssl = self.dependencies["openssl"]
return (not openssl.options.no_legacy and
((not openssl.options.no_md4) or
(not openssl.options.no_rmd160)))

def build(self):
cmake = CMake(self)
cmake.definitions["OPENSSL_WITH_ZLIB"] = not self.options["openssl"].no_zlib
cmake.definitions["OPENSSL_WITH_LEGACY"] = self._with_legacy()
cmake.definitions["OPENSSL_WITH_MD4"] = not self.options["openssl"].no_md4
cmake.definitions["OPENSSL_WITH_RIPEMD160"] = not self.options["openssl"].no_rmd160
def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def generate(self):
tc = CMakeToolchain(self)
if self.settings.os == "Android":
cmake.definitions["CONAN_LIBCXX"] = ""
cmake.configure()
cmake.build()
tc.cache_variables["CONAN_LIBCXX"] = ""
openssl = self.dependencies["openssl"]
openssl_version = Version(openssl.ref.version)
tc.cache_variables["OPENSSL_WITH_ZLIB"] = not openssl.options.no_zlib
tc.cache_variables["OPENSSL_WITH_LEGACY"] = self._with_legacy
tc.cache_variables["OPENSSL_WITH_MD4"] = not openssl.options.no_md4
tc.cache_variables["OPENSSL_WITH_RIPEMD160"] = not openssl.options.no_rmd160
tc.generate()


def build(self):
if not self._skip_test:
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not cross_building(self):
bin_path = os.path.join("bin", "digest")
self.run(bin_path, run_environment=True)
if not self._skip_test and can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "digest")
self.run(bin_path, env="conanrun")

if not self.options["openssl"].no_legacy:
bin_legacy_path = os.path.join("bin", "digest_legacy")
bin_legacy_path = os.path.join("bin", "digest_legacy")
if os.path.exists(bin_legacy_path):
self.run(bin_legacy_path, run_environment=True)

if not self.options["openssl"].no_stdio:
self.run("openssl version", run_environment=True)
assert os.path.exists(os.path.join(self.deps_cpp_info["openssl"].rootpath, "licenses", "LICENSE.txt"))

for fn in ("libcrypto.pc", "libssl.pc", "openssl.pc",):
assert os.path.isfile(os.path.join(self.build_folder, fn))
55 changes: 55 additions & 0 deletions recipes/openssl/3.x.x/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)

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

option(OPENSSL_WITH_ZLIB "OpenSSL with zlib support" ON)
option(OPENSSL_WITH_LEGACY "OpenSSL with support for the legacy provider" ON)
option(OPENSSL_WITH_MD4 "OpenSSL with MD4 support (needs legacy provider)" ON)
option(OPENSSL_WITH_RIPEMD160 "OpenSSL with RIPEMD16 support (needs legacy provider)" ON)

set(OpenSSL_DEBUG 1)
find_package(OpenSSL REQUIRED)

# Test whether variables from https://cmake.org/cmake/help/latest/module/FindOpenSSL.html
# are properly defined in conan generators
set(_custom_vars
OPENSSL_FOUND
OPENSSL_INCLUDE_DIR
OPENSSL_CRYPTO_LIBRARY
OPENSSL_CRYPTO_LIBRARIES
OPENSSL_SSL_LIBRARY
OPENSSL_SSL_LIBRARIES
OPENSSL_LIBRARIES
OPENSSL_VERSION
)
foreach(_custom_var ${_custom_vars})
if(DEFINED ${_custom_var} AND NOT "${${_custom_var}}" STREQUAL "" )
message(STATUS "${_custom_var}: ${${_custom_var}}")
else()
message(FATAL_ERROR "${_custom_var} not defined")
endif()
endforeach()

add_executable(digest ../test_package/digest.c)
if(OPENSSL_WITH_ZLIB)
target_compile_definitions(digest PRIVATE WITH_ZLIB)
endif()
target_link_libraries(digest OpenSSL::Crypto)

if(OPENSSL_WITH_LEGACY)
add_executable(digest_legacy ../test_package/digest_legacy.c)
# do now show deperecation warnings
target_compile_definitions(digest_legacy PRIVATE OPENSSL_SUPPRESS_DEPRECATED)
if(OPENSSL_WITH_MD4)
target_compile_definitions(digest_legacy PRIVATE OPENSSL_WITH_MD4)
endif()
if(OPENSSL_WITH_RIPEMD160)
target_compile_definitions(digest_legacy PRIVATE OPENSSL_WITH_RIPEMD160)
endif()
if(OPENSSL_WITH_ZLIB)
target_compile_definitions(digest_legacy PRIVATE WITH_ZLIB)
endif()
target_link_libraries(digest_legacy OpenSSL::Crypto)
endif()
40 changes: 40 additions & 0 deletions recipes/openssl/3.x.x/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from conans import CMake, tools, ConanFile
from conan.tools.build import cross_building
import os


class TestPackageConan(ConanFile):
settings = "os", "compiler", "arch", "build_type"
generators = "cmake", "cmake_find_package", "pkg_config"

def _with_legacy(self):
return (not self.options["openssl"].no_legacy and
((not self.options["openssl"].no_md4) or
(not self.options["openssl"].no_rmd160)))

def build(self):
cmake = CMake(self)
cmake.definitions["OPENSSL_WITH_ZLIB"] = not self.options["openssl"].no_zlib
cmake.definitions["OPENSSL_WITH_LEGACY"] = self._with_legacy()
cmake.definitions["OPENSSL_WITH_MD4"] = not self.options["openssl"].no_md4
cmake.definitions["OPENSSL_WITH_RIPEMD160"] = not self.options["openssl"].no_rmd160
if self.settings.os == "Android":
cmake.definitions["CONAN_LIBCXX"] = ""
cmake.configure()
cmake.build()

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

if not self.options["openssl"].no_legacy:
bin_legacy_path = os.path.join("bin", "digest_legacy")
self.run(bin_legacy_path, run_environment=True)

if not self.options["openssl"].no_stdio:
self.run("openssl version", run_environment=True)
assert os.path.exists(os.path.join(self.deps_cpp_info["openssl"].rootpath, "licenses", "LICENSE.txt"))

for fn in ("libcrypto.pc", "libssl.pc", "openssl.pc",):
assert os.path.isfile(os.path.join(self.build_folder, fn))

0 comments on commit b51d60a

Please sign in to comment.