janbar / openssl-cmake

Build OpenSSL with CMake on MacOS, Win32, Win64 and cross compile for Android, IOS
Other
277 stars 144 forks source link

How to include this project in other cmake project? #23

Closed dearplain closed 3 years ago

dearplain commented 3 years ago

I try this, not working.

FetchContent_Declare(
        OpenSSL
        GIT_REPOSITORY https://github.com/janbar/openssl-cmake.git
        GIT_TAG "1.1.1k-20210430"
    )
    FetchContent_GetProperties(OpenSSL)
    FetchContent_MakeAvailable(OpenSSL)
anb0s commented 3 years ago

Hi, i'm using this instead of find_package(OpenSSL <version>) with released archives:

include(FetchContent)
FetchContent_Declare(
  ssl_content
  URL      https://github.com/janbar/openssl-cmake/archive/refs/tags/1.1.1k-20210430.tar.gz
  URL_HASH MD5=f4294e5de7ebb20f9a695155660f1afd
)
FetchContent_MakeAvailable(ssl_content)
add_library(OpenSSL::SSL ALIAS ssl)
add_library(OpenSSL::Crypto ALIAS crypto)

and then it can be used in same way as dependency:

target_link_libraries(<my-target>
  PRIVATE
    OpenSSL::Crypto
    OpenSSL::SSL
)

but it should work also with git repo...

anb0s commented 3 years ago

it works with Git repo and Tag too:

  include(FetchContent)
  FetchContent_Declare(
    ssl_content
    GIT_REPOSITORY https://github.com/janbar/openssl-cmake.git
    GIT_TAG "1.1.1k-20210430"
  )
  FetchContent_MakeAvailable(ssl_content)
  add_library(OpenSSL::SSL ALIAS ssl)
  add_library(OpenSSL::Crypto ALIAS crypto)

The alias definitions are optional if you want to use alias names like defined by find_package(OpenSSL <version>) - if you remove this:

  add_library(OpenSSL::SSL ALIAS ssl)
  add_library(OpenSSL::Crypto ALIAS crypto)

just use the raw target names instead:

target_link_libraries(<my-target>
  PRIVATE
    crypto
    ssl
)
dearplain commented 3 years ago
include(FetchContent)
FetchContent_Declare(
  ssl_content
  URL      https://github.com/janbar/openssl-cmake/archive/refs/tags/1.1.1k-20210430.tar.gz
  URL_HASH MD5=f4294e5de7ebb20f9a695155660f1afd
)
FetchContent_MakeAvailable(ssl_content)
add_library(OpenSSL::SSL ALIAS ssl)
add_library(OpenSSL::Crypto ALIAS crypto)

Greate answer, but I still get error:

CMake Error at /usr/local/Cellar/cmake/3.21.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY
  OPENSSL_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.21.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/Cellar/cmake/3.21.2/share/cmake/Modules/FindOpenSSL.cmake:574 (find_package_handle_standard_args)
  build/webrtc/_deps/libdatachannel-src/deps/libsrtp/CMakeLists.txt:61 (find_package)
dearplain commented 3 years ago

I finally make it work with patch command, thank you.

include(FetchContent)
FetchContent_Declare(
  ssl_content
  URL      https://github.com/janbar/openssl-cmake/archive/refs/tags/1.1.1k-20210430.tar.gz
  URL_HASH MD5=f4294e5de7ebb20f9a695155660f1afd
)
FetchContent_MakeAvailable(ssl_content)
add_library(OpenSSL::SSL ALIAS ssl)
add_library(OpenSSL::Crypto ALIAS crypto)

FetchContent_Declare(
        libdatachannel
        URL xxx
        PATCH_COMMAND ${SED_EXECUTABLE} -i.bak "s/find_package(OpenSSL REQUIRED)//g" CMakeLists.txt
        && ${SED_EXECUTABLE} -i.bak "s/find_package(OpenSSL REQUIRED)//g" deps/libsrtp/CMakeLists.txt
    )