Building cURL with OpenSSL support from CMake
Date: Wed, 29 Apr 2020 09:01:07 -0700
Hi All,
I have a question with regard to building cURL with OpenSSL support. I
am trying to setup an automated build system in CMake where all
dependencies of my project are compiled from source through CMake and
made available for other dependencies. Because of the project is
cross-platform and the build process will be run on continuous
integration agents, I wanted to avoid having a pre-built dependency
script to install dependencies ahead of time. All other dependencies
have worked well so far, but I am banging my head against a wall when
it comes to OpenSSL for cURL.
Basically, because cURL CMake scripts uses `find_package(OpenSSL)`
without any options for the user to enter an alternate path, cURL
always tries to find the system OpenSSL package (which on Mac, by
default, does not support x86_x64 architecture).
I have a little toy CMake project that shows what I'm talking about if
anyone wants to take a look. I tried this project on Mac, Windows and
Linux. Only Linux works because I have libssl-dev installed and the
find_package can find the proper headers. On MacOS, this would not
link as the libcrypto.dylib package provided by MacOS natively does
not link against x86_x64 architecture. On Windows, this builds but
does not support the HTTPS protocol.
CMakeLists.txt
------------------------------------------------------------------------------------------------------------------
CMAKE_MINIMUM_REQUIRED(VERSION 3.15)
PROJECT(test-curl
LANGUAGES CXX)
#========== Add CPM package manager =================
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM.cmake")
set(CPM_VERSION 0.18)
if(NOT EXISTS ${CPM_DOWNLOAD_LOCATION})
message(STATUS "Downloading CPM.cmake")
file(DOWNLOAD
https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION})
endif(NOT EXISTS ${CPM_DOWNLOAD_LOCATION})
include(${CPM_DOWNLOAD_LOCATION})
#========== Add dependency =================
CPMAddPackage(
NAME openssl-cmake
GITHUB_REPOSITORY janbar/openssl-cmake
GIT_TAG master
OPTIONS
"WITH_APPS OFF"
)
if (openssl-cmake_ADDED)
set(OPENSSL_INCLUDE_DIR "${openssl-cmake_BINARY_DIR}/include")
target_include_directories(ssl INTERFACE ${OPENSSL_INCLUDE_DIR})
target_include_directories(crypto INTERFACE ${OPENSSL_INCLUDE_DIR})
endif()
CPMAddPackage(
NAME curl
VERSION 7.67.0
URL https://github.com/curl/curl/releases/download/curl-7_67_0/curl-7.67.0.tar.gz
OPTIONS
"CMAKE_USE_LIBSSH2 OFF"
"BUILD_CURL_EXE OFF"
"BUILD_SHARED_LIBS OFF"
"HTTP_ONLY ON"
"BUILD_TESTING OFF"
)
add_executable(test-curl main.cpp)
target_link_libraries(test-curl libcurl)
main.cpp (test program)
-----------------------------------------------------------
#include <iostream>
#include <string>
#include "curl/curlver.h"
#include "curl/curl.h"
static size_t WriteCallback(void *contents, size_t size, size_t nmemb,
void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main() {
std::cout << "hello world" << std::endl;
std::cout << "LIBCURL VERSION: " << LIBCURL_VERSION << std::endl;
CURL* curl;
CURLcode res;
std::string readBuffer;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
std::cout << "Testing cURL with https protocol\n";
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "testing=1");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
std::cout << "Contents of CURL: " << readBuffer << std::endl;
return 0;
}
Thank you for your help,
David
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2020-04-29