-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
openssl: let openssl create FindOpenSSL.cmake script equivalent to official #486
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# This script should only run when included in a FindXXX.cmake module | ||
if(NOT OpenSSL_LIB_DIRS) | ||
return() | ||
endif() | ||
|
||
# library names of crypto and ssl libraries | ||
set(CONAN_CRYPTO_LIBRARY "@CONAN_CRYPTO_LIBRARY@") | ||
set(CONAN_SSL_LIBRARY "@CONAN_SSL_LIBRARY@") | ||
|
||
# absolute paths of crypto and ssl libraries | ||
find_library(CONAN_CRYPTO_ABSOLUTE_LIBRARY NAME ${CONAN_CRYPTO_LIBRARY} PATHS ${OpenSSL_LIB_DIRS} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH) | ||
find_library(CONAN_SSL_ABSOLUTE_LIBRARY NAME ${CONAN_SSL_LIBRARY} PATHS ${OpenSSL_LIB_DIRS} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH) | ||
|
||
# https://cmake.org/cmake/help/latest/module/FindOpenSSL.html | ||
|
||
set(OPENSSL_FOUND ON) | ||
set(OPENSSL_INCLUDE_DIR ${OpenSSL_INCLUDE_DIRS}) | ||
set(OPENSSL_LIBRARIES ${OpenSSL_LIBRARIES} ${OpenSSL_SYSTEM_LIBS}) | ||
set(OPENSSL_VERSION "${OpenSSL_VERSION}") | ||
|
||
set(OPENSSL_CRYPTO_LIBRARY ${CONAN_CRYPTO_ABSOLUTE_LIBRARY}) | ||
set(OPENSSL_CRYPTO_LIBRARIES ${CONAN_CRYPTO_ABSOLUTE_LIBRARY} ${OpenSSL_SYSTEM_LIBS}) | ||
set(OPENSSL_SSL_LIBRARY ${CONAN_CRYPTO_ABSOLUTE_LIBRARY}) | ||
set(OPENSSL_SSL_LIBRARIES ${CONAN_CRYPTO_ABSOLUTE_LIBRARY} ${OpenSSL_SYSTEM_LIBS}) | ||
|
||
if(NOT CMAKE_VERSION VERSION_LESS 3.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are conan recipes targeting cmake versions older than 3.0 (cmake 3 was released in 2014 https://cmake.org/pipermail/cmake/2014-June/057793.html)? Maybe people using conan are using a reasonably recent version of cmake? Is that a valid assumption? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just copied the same logic as the But more generally, I believe conan should add a feature for conan would need a feature that allows this: def package_info(self):
self.cpp_info.name = "OpenSSL"
self.cpp_info.libs = [*self._ssl_crypto_library]
system_libs = []
if self.settings.os == "Windows":
system_libs.extend(["crypt32", "msi", "ws2_32", "advapi32", "user32", "gdi32"])
elif self.settings.os == "Linux":
system_libs.extend(["dl", "pthread"])
self.cpp_info.system_libs.extend(system_libs)
# By default: the generated cmake targets should inherit the dependencies of this recipe
ssl_system_libs = ["crypt32", "msi", "ws2_32", "advapi32", "user32", "gdi32"]
crypto_system_libs = ["crypt32", "msi", "advapi32", "user32", "gdi32"]
for generator in ("cmake_find_package", "cmake_find_package_multi"):
ssl_library, crypto_library = self._ssl_crypto_library
# Set OPENSSL_FOUND unconditionally, is a string by default
self.cpp_info.generators[generator].add_variable("OPENSSL_FOUND", "ON")
self.cpp_info.generators[generator].add_variable("OPENSSL_VERSION", self.version, type=CMakeVariableType.String)
# Add OPENSSL_SSL_LIBRARIES variable: it will be set by find_library calls + is genex ==> will be a generator expression when generator == cmake_find_package_multi
sslLibraryVariable = self.cpp_info.generators[generator].add_variable("OPENSSL_SSL_LIBRARY", self._ssl_library, type=CMakeVariableType.Library, genex=True)
sslCMakeTarget = self.cpp_info.generators[generator].createCMakeTarget("OpenSSL::SSL")
# - override libraries
sslCMakeTarget.libs = [ssl_library]
# - override system libraries (SSL requires different system_libs then crypto)
sslCMakeTarget.system_libs = ssl_system_libs
# - inherit include directories
# sslCMakeTarget.includedirs = ["include"]
# - inherit package dependencies (so in CMake, link to all dependencies (CONAN_PKG::dep1, CONAN_PKG_dep2)
# sslCMakeTarget.deps = ["dep1", "dep2"]
# - inherit defines
# sslCMakeTarget.defines = []
# - inherit link flags
# sslCMakeTarget.linkflags = []
# Add OPENSSL_SSL_LIBRARIES variable: it will be set by find_library calls + is genex ==> will be a generator expression when generator == cmake_find_package_multi
cryptoLibraryVariable = self.cpp_info.generators[generator].add_variable("OPENSSL_CRYPTO_LIBRARY", self._crypto_library, type=CMakeVariableType.Library, genex=True)
cryptoCMakeTarget = self.cpp_info.generators[generator].createCMakeTarget("OpenSSL::Crypto")
cryptoCMakeTarget.libs = [crypto_library]
cryptoCMakeTarget.system_libs = crypto_system_libs
# inherit other variables ....
# Add OPENSSL_LIBRARIES: it will be a list of these variables (which can be cmake variables themselves) + is gen_ex
self.cpp_info.generators[generator].add_variable("OPENSSL_LIBRARIES", [sslLibraryVariable, cryptoLibraryVariable] + system_libs, type=CMakeVariableType.List, genex=True)
# Add OPENSSL_INCLUDE_DIR variable: is a path + generator expression
self.cpp_info.generators[generator].add_variable("OPENSSL_INCLUDE_DIR", os.path.join(self.package_folder, "include"), type=CMakeVariableType.Path, genex=True) The setting of variables might need some dependency tracking (optionally), but I think this interface can serve a lot of use cases. |
||
if(NOT TARGET OpenSSL::Crypto) | ||
add_library(OpenSSL::Crypto INTERFACE IMPORTED) | ||
if(OpenSSL_INCLUDE_DIRS) | ||
set_target_properties(OpenSSL::Crypto PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${OpenSSL_INCLUDE_DIRS}") | ||
endif() | ||
set_property(TARGET OpenSSL::Crypto PROPERTY INTERFACE_LINK_LIBRARIES ${CONAN_CRYPTO_ABSOLUTE_LIBRARY} ${OpenSSL_SYSTEM_LIBS} "${OpenSSL_LINKER_FLAGS_LIST}") | ||
set_property(TARGET OpenSSL::Crypto PROPERTY INTERFACE_COMPILE_DEFINITIONS ${OpenSSL_COMPILE_DEFINITIONS}) | ||
set_property(TARGET OpenSSL::Crypto PROPERTY INTERFACE_COMPILE_OPTIONS "${OpenSSL_COMPILE_OPTIONS_LIST}") | ||
endif() | ||
if(NOT TARGET OpenSSL::SSL) | ||
add_library(OpenSSL::SSL INTERFACE IMPORTED) | ||
if(OpenSSL_INCLUDE_DIRS) | ||
set_target_properties(OpenSSL::SSL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${OpenSSL_INCLUDE_DIRS}") | ||
endif() | ||
set_property(TARGET OpenSSL::SSL PROPERTY INTERFACE_LINK_LIBRARIES ${CONAN_SSL_ABSOLUTE_LIBRARY} ${OpenSSL_SYSTEM_LIBS} "${OpenSSL_LINKER_FLAGS_LIST}") | ||
set_property(TARGET OpenSSL::SSL PROPERTY INTERFACE_COMPILE_DEFINITIONS ${OpenSSL_COMPILE_DEFINITIONS}) | ||
set_property(TARGET OpenSSL::SSL PROPERTY INTERFACE_COMPILE_OPTIONS "${OpenSSL_COMPILE_OPTIONS_LIST}") | ||
endif() | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
PROJECT(test_package) | ||
cmake_minimum_required(VERSION 2.8) | ||
|
||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) | ||
conan_basic_setup() | ||
|
||
add_executable(digest digest.cpp) | ||
|
||
if(USE_FIND_PACKAGE) | ||
set(OpenSSL_DEBUG 1) | ||
find_package(OpenSSL REQUIRED) | ||
message("LINK WITH ${OPENSSL_LIBRARIES}") | ||
|
||
target_include_directories(digest PRIVATE ${OPENSSL_INCLUDE_DIRS}) | ||
target_link_libraries(digest ${OPENSSL_LIBRARIES}) | ||
|
||
find_package(Threads) | ||
target_link_libraries(digest PRIVATE ${CMAKE_THREAD_LIBS_INIT}) | ||
|
||
if(WIN32) | ||
target_link_libraries(digest PRIVATE ws2_32 crypt32) | ||
endif() | ||
if(UNIX AND NOT APPLE) | ||
target_link_libraries(digest PRIVATE ${CMAKE_DL_LIBS}) | ||
endif() | ||
|
||
add_executable(test_crypto test_crypto.cpp) | ||
target_link_libraries(test_crypto OpenSSL::Crypto) | ||
|
||
add_executable(test_crypto_bad EXCLUDE_FROM_ALL test_crypto.cpp) | ||
target_link_libraries(test_crypto OpenSSL::SSL) | ||
|
||
add_executable(test_ssl test_ssl.c) | ||
target_link_libraries(test_ssl OpenSSL::SSL) | ||
|
||
add_executable(test_ssl_bad EXCLUDE_FROM_ALL test_ssl.c) | ||
target_link_libraries(test_ssl OpenSSL::Crypto) | ||
else() | ||
message("LINK WITH ${CONAN_LIBS}") | ||
target_include_directories(digest PRIVATE ${CONAN_INCLUDE_DIRS}) | ||
target_link_libraries(digest PRIVATE ${CONAN_LIBS}) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is probably a good idea to call find_package_handle_standard_args (https://cmake.org/cmake/help/latest/module/FindPackageHandleStandardArgs.html) to report the error if the files could not be found and set some of the variables that are set in the next few lines.
There is more documentation on regarding writing finders in https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/How-To-Find-Libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea about
find_package_handle_standard_args
.