Skip to content
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

build: use official Findlibsodium + add option to download and build libsodium #4562

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,29 +260,59 @@ endif()

option(WITH_LIBSODIUM "Use libsodium" OFF)
option(WITH_LIBSODIUM_STATIC "Use static libsodium library" OFF)
option(DOWNLOAD_LIBSODIUM "Download and build libsodium from the source" OFF)
option(ENABLE_LIBSODIUM_RANDOMBYTES_CLOSE "Automatically close libsodium randombytes. Not threadsafe without getrandom()" ON)
option(ENABLE_CURVE "Enable CURVE security" OFF)

if(ENABLE_CURVE)
if(WITH_LIBSODIUM)
find_package("sodium")
if(SODIUM_FOUND)
message(STATUS "Using libsodium for CURVE security")
include_directories(${SODIUM_INCLUDE_DIRS})
link_directories(${SODIUM_LIBRARY_DIRS})
if(WITH_LIBSODIUM_STATIC)
add_compile_definitions(SODIUM_STATIC)
endif()
set(ZMQ_USE_LIBSODIUM 1)
set(ZMQ_HAVE_CURVE 1)
if (ENABLE_LIBSODIUM_RANDOMBYTES_CLOSE)
set(ZMQ_LIBSODIUM_RANDOMBYTES_CLOSE 1)
endif()
if (WITH_LIBSODIUM_STATIC)
set(sodium_USE_STATIC_LIBS ON)
set(CPM_LIBSODIUM_SHARED OFF)
else()
message(
ERROR
"libsodium not installed, you may want to install libsodium and run cmake again"
set(sodium_USE_STATIC_LIBS OFF)
set(CPM_LIBSODIUM_SHARED ON)
endif()
if (DOWNLOAD_LIBSODIUM)
include(CPM)
CPMAddPackage(
NAME libsodium
GITHUB_REPOSITORY zeromq/libsodium-cmake
GIT_TAG f568ff02f1bed155ea598c0e803ef3c9db2703d2
URL_HASH SHA256=4e9e745844f406c420d8b03658d30e0ea3647e31f8eb5e51687f5481251e21f8
OPTIONS
SODIUM_DISABLE_TESTS ON
BUILD_SHARED_LIBS ${CPM_LIBSODIUM_SHARED}
)
set(SODIUM_FOUND ON)
set(SODIUM_LIBRARIES sodium)
list(APPEND target_outputs sodium)

get_target_property(SODIUM_INCLUDE_DIRS sodium INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties(sodium PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"$<BUILD_INTERFACE:${SODIUM_INCLUDE_DIRS}>"
)
include_directories($<BUILD_INTERFACE:${SODIUM_INCLUDE_DIRS}>)
else()
find_package("sodium")
if (sodium_FOUND)
set(SODIUM_FOUND ON)
set(SODIUM_LIBRARIES sodium)
get_target_property(SODIUM_INCLUDE_DIRS sodium INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${SODIUM_INCLUDE_DIRS})
else()
message(ERROR
"libsodium not installed, you may want to install libsodium or enable DOWNLOAD_LIBSODIUM and run cmake again")
endif()
endif()
message(STATUS "Using libsodium for CURVE security")
if(WITH_LIBSODIUM_STATIC)
add_compile_definitions(SODIUM_STATIC)
endif()
set(ZMQ_USE_LIBSODIUM 1)
set(ZMQ_HAVE_CURVE 1)
if (ENABLE_LIBSODIUM_RANDOMBYTES_CLOSE)
set(ZMQ_LIBSODIUM_RANDOMBYTES_CLOSE 1)
endif()
endif()
else()
Expand Down
33 changes: 33 additions & 0 deletions builds/cmake/Modules/CPM.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set(CPM_DOWNLOAD_VERSION 0.38.1)

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

function(download_cpm)
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endfunction()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
download_cpm()
else()
# resume download if it previously failed
file(READ ${CPM_DOWNLOAD_LOCATION} check)
if("${check}" STREQUAL "")
download_cpm()
endif()
unset(check)
endif()

include(${CPM_DOWNLOAD_LOCATION})
Loading