From 7f5594d6cd0f3bd198859fb5984620ee5d866d82 Mon Sep 17 00:00:00 2001 From: Coldwings Date: Mon, 9 Oct 2023 15:51:21 +0800 Subject: [PATCH] build static openssl when building static libcurl Signed-off-by: Coldwings --- CMake/FindCURL.cmake | 13 +++++--- CMake/FindOpenSSL.cmake | 67 +++++++++++++++++++++++++++++++++++++++++ CMake/Findphoton.cmake | 7 +++++ CMakeLists.txt | 2 +- README.md | 8 +++++ 5 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 CMake/FindOpenSSL.cmake diff --git a/CMake/FindCURL.cmake b/CMake/FindCURL.cmake index 56ed0b76..ab4e44b4 100644 --- a/CMake/FindCURL.cmake +++ b/CMake/FindCURL.cmake @@ -17,6 +17,7 @@ if(${BUILD_CURL_FROM_SOURCE}) if (NOT curl_bundle_POPULATED) FetchContent_Populate(curl_bundle) endif() + find_package(OpenSSL) add_custom_command( OUTPUT ${curl_bundle_BINARY_DIR}/lib/libcurl.a WORKING_DIRECTORY ${curl_bundle_SOURCE_DIR} @@ -25,22 +26,24 @@ if(${BUILD_CURL_FROM_SOURCE}) export CXX=${CMAKE_CXX_COMPILER} && export LD=${CMAKE_LINKER} && export CFLAGS=-fPIC && - autoreconf -i && sh configure --with-openssl --without-libssh2 - --enable-static --enable-shared=no --enable-optimize - --enable-symbol-hiding --disable-manual --without-libidn + autoreconf -i && sh configure --with-ssl="${OPENSSL_ROOT_DIR}" + --without-libssh2 --enable-static --enable-shared=no --enable-optimize + --disable-manual --without-libidn --disable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-dict --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smb --disable-smtp --disable-gopher --without-nghttp2 --enable-http --with-pic=PIC - --prefix=${curl_bundle_BINARY_DIR} && make -j && make install) + --prefix="${curl_bundle_BINARY_DIR}" && make -j 8 && make install) add_custom_target(libcurl_static_build DEPENDS ${curl_bundle_BINARY_DIR}/lib/libcurl.a) + add_dependencies(libcurl_static_build openssl102_static_build) + make_directory(${curl_bundle_BINARY_DIR}/include) endif() set(CURL_FOUND yes) set(CURL_LIBRARY ${curl_bundle_BINARY_DIR}/lib/libcurl.a) - set(CURL_THIRDPARTY_DEPS crypto ssl z) + set(CURL_THIRDPARTY_DEPS OpenSSL::SSL OpenSSL::Crypto z) set(CURL_LIBRARIES ${CURL_LIBRARY} ${CURL_THIRDPARTY_DEPS}) set(CURL_INCLUDE_DIR ${curl_bundle_BINARY_DIR}/include) set(CURL_INCLUDE_DIRS ${CURL_INCLUDE_DIR}) diff --git a/CMake/FindOpenSSL.cmake b/CMake/FindOpenSSL.cmake new file mode 100644 index 00000000..02e55b9d --- /dev/null +++ b/CMake/FindOpenSSL.cmake @@ -0,0 +1,67 @@ +include(FetchContent) + +if(${BUILD_CURL_FROM_SOURCE}) + message("Add and build standalone libopenssl") + include(FetchContent) + + # make openssl into bundle + FetchContent_Declare( + openssl102 + GIT_REPOSITORY https://github.com/openssl/openssl.git + GIT_TAG OpenSSL_1_0_2-stable + GIT_PROGRESS 1) + + FetchContent_GetProperties(openssl102) + + if(NOT TARGET openssl102_static_build) + if(NOT openssl102_POPULATED) + FetchContent_Populate(openssl102) + endif() + add_custom_command( + OUTPUT ${openssl102_BINARY_DIR}/lib/libssl.a + WORKING_DIRECTORY ${openssl102_SOURCE_DIR} + COMMAND + sh config -fPIC no-unit-test no-shared + --openssldir="${openssl102_BINARY_DIR}" + --prefix="${openssl102_BINARY_DIR}" && make depend -j && make + -j 8 && make install) + add_custom_target(openssl102_static_build + DEPENDS ${openssl102_BINARY_DIR}/lib/libssl.a) + make_directory(${openssl102_BINARY_DIR}/include) + endif() + + set(OPENSSL_FOUND yes) + set(OPENSSL_ROOT_DIR ${openssl102_BINARY_DIR}) + set(OPENSSL_INCLUDE_DIR ${OPENSSL_ROOT_DIR}/include) + set(OPENSSL_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR}) + set(OPENSSL_SSL_LIBRARY ${OPENSSL_ROOT_DIR}/lib/libssl.a) + set(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY}) + set(OPENSSL_CRYPTO_LIBRARY ${OPENSSL_ROOT_DIR}/lib/libcrypto.a) + set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY}) + set(OPENSSL_LINK_DIR ${OPENSSL_ROOT_DIR}/lib) + set(OPENSSL_LINK_DIRS ${OPENSSL_LINK_DIR}) + + if(NOT TARGET OpenSSL::SSL) + add_library(OpenSSL::SSL STATIC IMPORTED) + add_dependencies(OpenSSL::SSL openssl102_static_build) + set_target_properties( + OpenSSL::SSL + PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${OPENSSL_SSL_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIRS}" + INTERFACE_LINK_LIBRARIES "${OPENSSL_SSL_LIBRARY}") + endif() + + if(NOT TARGET OpenSSL::Crypto) + add_library(OpenSSL::Crypto STATIC IMPORTED) + add_dependencies(OpenSSL::Crypto openssl102_static_build) + set_target_properties( + OpenSSL::Crypto + PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${OPENSSL_CRYPTO_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIRS}" + INTERFACE_LINK_LIBRARIES "${OPENSSL_CRYPTO_LIBRARY}") + endif() +else() + include(${CMAKE_ROOT}/Modules/FindOpenSSL.cmake) +endif() diff --git a/CMake/Findphoton.cmake b/CMake/Findphoton.cmake index 11d3bd31..203ada89 100644 --- a/CMake/Findphoton.cmake +++ b/CMake/Findphoton.cmake @@ -14,4 +14,11 @@ if(BUILD_TESTING) else() FetchContent_MakeAvailable(photon) endif() + +if (BUILD_CURL_FROM_SOURCE) + find_package(OpenSSL REQUIRED) + find_package(CURL REQUIRED) + add_dependencies(photon_obj CURL::libcurl OpenSSL::SSL OpenSSL::Crypto) +endif() + set(PHOTON_INCLUDE_DIR ${photon_SOURCE_DIR}/include/) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51fbac88..82650611 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ endif() set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED on) set(ENABLE_MIMIC_VDSO off) -option(BUILD_CURL_FROM_SOURCE "Compile static libcurl" on) +option(BUILD_CURL_FROM_SOURCE "Compile static libcurl" off) find_package(photon REQUIRED) find_package(tcmu REQUIRED) diff --git a/README.md b/README.md index be15549c..398fc069 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,14 @@ make -j sudo make install ``` +Considering some libcurl and libopenssl has API changes, if want to build a make-sured compatible version libcurl and openssl, and link to executable as static library. + +Noticed that building libcurl and openssl depends on `autoconf` `automake` and `libtool`. + +```bash +cmake -D BUILD_CURL_FROM_SOURCE=1 .. +``` + If you want to use the [original libext2fs](https://github.com/tytso/e2fsprogs) instead of our [customized libext2fs](https://github.com/data-accelerator/e2fsprogs). ```bash