jimmy-park / openssl-cmake

Build OpenSSL in parallel within CMake
MIT License
32 stars 10 forks source link
ccache cmake cross-platform openssl parallel-build

openssl-cmake

CI

Build OpenSSL in parallel within CMake

Features

CMake Options

Option Type Default Description
OPENSSL_BUILD_OPTIONS list (undefined) make-compatible options
OPENSSL_BUILD_TARGET string build_libs Makefile target for build
OPENSSL_BUILD_VERBOSE bool OFF Enable verbose output from build
OPENSSL_CONFIGURE_OPTIONS list (undefined) Use OpenSSL's Configure options
OPENSSL_CONFIGURE_VERBOSE bool OFF Enable verbose output from configuration
OPENSSL_ENABLE_PARALLEL bool ON Build and test in parallel if possible
OPENSSL_INSTALL bool OFF Install OpenSSL components
OPENSSL_INSTALL_CERT bool OFF Install cert.pem to the openssldir directory
OPENSSL_INSTALL_TARGET string install_dev Makefile target for install
OPENSSL_PATCH list (undefined) Apply patches to OpenSSL source
OPENSSL_SOURCE path (undefined) Specify the location of OpenSSL source (URL or path)
OPENSSL_TARGET_PLATFORM string (undefined) Use OpenSSL's Configure target (see below)
OPENSSL_TARGET_VERSION string 3.4.x Use the latest OpenSSL version by default
OPENSSL_TEST bool OFF Enable testing and build OpenSSL self tests
OPENSSL_USE_CCACHE bool ON Use ccache if available

[!IMPORTANT]

  • OPENSSL_PATCH
    • Since OpenSSL source is distributed with LF, patch files also should be LF
  • OPENSSL_USE_CCACHE
    • Whenever you change this option, perform a fresh configuration (or just delete CMakeCache.txt)
    • This option will remove /Zi /Fd on MSVC

[!NOTE]

  • OPENSSL_CONFIGURE_OPTIONS
    • no-shared is added when BUILD_SHARED_LIBS is OFF
    • no-tests is added when OPENSSL_TEST is OFF
  • OPENSSL_TARGET_PLATFORM
    • If this option is empty, the target platform will be detected
    • If openssl-cmake fails to detect, you need to set this option explicitly

[!TIP]

  • OPENSSL_INSTALL
    • To change the installation path, add --prefix=<path> to OPENSSL_CONFIGURE_OPTIONS
  • OPENSSL_INSTALL_CERT
  • OPENSSL_SOURCE
    • You can download OpenSSL source from the internet or use the local OpenSSL source
    • If this option is empty or invalid, OpenSSL source will be downloaded from the official website

Prerequisites

Requirements

Linux

Install CMake from official website or Snapcraft

# Debian
sudo snap install cmake --classic
sudo apt-get install -y build-essential perl ninja-build ccache

macOS

Install packages from Homebrew

brew install cmake perl ninja ccache
xcode-select --install

Windows

Install packages from Chocolatey

# Powershell (run as administrator)
choco install -y cmake jom strawberryperl nasm ccache --installargs 'ADD_CMAKE_TO_PATH=System'

# Append "C:\Program Files\NASM" to the PATH environment variable
# or run this code
[Environment]::SetEnvironmentVariable("PATH", "$ENV:PATH;C:\Program Files\NASM", "USER")

Usage

Build

cmake --list-presets all                    # List all CMake presets
cmake --preset windows                      # Configure
cmake --build --preset windows              # Build
ctest --preset windows                      # Test
cmake --build --preset windows -t install   # Install

Integration

FetchContent_Declare(
    openssl-cmake
    URL https://github.com/jimmy-park/openssl-cmake/archive/main.tar.gz
)
FetchContent_MakeAvailable(openssl-cmake)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE
    OpenSSL::SSL
    OpenSSL::Crypto
    OpenSSL::applink
)

[!IMPORTANT]

  • FetchContent_MakeAvailable(openssl-cmake) must be preceded before find_package(OpenSSL)
  • If some libraries link OpenSSL using OPENSSL_LIBRARIES variable rather than OpenSSL::SSL target, add_dependencies() may help change the build order

Using CPM.cmake

CPMAddPackage(
    NAME openssl-cmake
    URL https://github.com/jimmy-park/openssl-cmake/archive/main.tar.gz
    OPTIONS
    "OPENSSL_CONFIGURE_OPTIONS no-shared\\\\;no-tests"
)

[!TIP] For some reason, CPM requires double-escaping for semicolon-separated lists.

Benchmarks

Configuration Time Speed
Sequential 168 s 1.00 x
Sequential w/ ccache (cold) 460 s 0.37 x
Sequential w/ ccache (warm) 111 s 1.51 x
Parallel 34 s 4.94 x
Parallel w/ ccache (cold) 78 s 2.15 x
Parallel w/ ccache (warm) 21 s 8.00 x

[!TIP] There are benchmark workflows