Skip to content

Commit

Permalink
Updated FindSodium.cmake and removed the dependency on static libsodi…
Browse files Browse the repository at this point in the history
…um for non-Emscripten builds
  • Loading branch information
Syndace committed Dec 10, 2022
1 parent 06a02c8 commit 8ab957a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
23 changes: 9 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ endif (WIN32)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

set(sodium_USE_STATIC_LIBS YES)
find_package(Sodium REQUIRED)
set(sodium_static_INCLUDE_DIR ${sodium_INCLUDE_DIR})

if (NOT DEFINED EMSCRIPTEN)
if (DEFINED EMSCRIPTEN)
set(sodium_USE_STATIC_LIBS YES)
find_package(Sodium REQUIRED)
else ()
set(sodium_USE_STATIC_LIBS NO)
find_package(Sodium REQUIRED)
set(sodium_dynamic_INCLUDE_DIR ${sodium_INCLUDE_DIR})

if (WIN32)
file(COPY "${sodium_DLL_DEBUG}" DESTINATION "${PROJECT_SOURCE_DIR}/bin/")
Expand All @@ -66,10 +64,7 @@ endif ()

include_directories(include)
include_directories(ref10/include)
include_directories(${sodium_static_INCLUDE_DIR})
if (NOT DEFINED EMSCRIPTEN)
include_directories(${sodium_dynamic_INCLUDE_DIR})
endif ()
include_directories(${sodium_INCLUDE_DIR})

##########
# xeddsa #
Expand Down Expand Up @@ -107,7 +102,7 @@ target_link_libraries(xeddsa_static PUBLIC
crypto_stream_objects
crypto_core_objects
kernelrandombytes_objects
sodium_static
sodium
)

install(TARGETS xeddsa_static)
Expand All @@ -126,7 +121,7 @@ if (NOT DEFINED EMSCRIPTEN)
crypto_stream_objects
crypto_core_objects
kernelrandombytes_objects
sodium_shared
sodium
)

if (WIN32)
Expand Down Expand Up @@ -157,12 +152,12 @@ target_include_directories(xeddsa_test_objects PRIVATE test)

add_executable(xeddsa_test_static $<TARGET_OBJECTS:xeddsa_test_objects>)
target_include_directories(xeddsa_test_static PRIVATE test)
target_link_libraries(xeddsa_test_static xeddsa_static sodium_static)
target_link_libraries(xeddsa_test_static xeddsa_static sodium)

if (NOT DEFINED EMSCRIPTEN)
add_executable(xeddsa_test_dynamic $<TARGET_OBJECTS:xeddsa_test_objects>)
target_include_directories(xeddsa_test_dynamic PRIVATE test)
target_link_libraries(xeddsa_test_dynamic xeddsa_dynamic sodium_shared)
target_link_libraries(xeddsa_test_dynamic xeddsa_dynamic sodium)
endif ()

################
Expand Down
48 changes: 22 additions & 26 deletions FindSodium.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
#
# http://creativecommons.org/publicdomain/zero/1.0/
#
# Modified by Tim Henkes (Syndace) as part of libxeddsa to make it
# possible to call find_package twice, to find both the shared library
# and the static one. Also allows statically specifying the location of
# libsodium for Emscripten builds.
# Modified by Tim Henkes (Syndace) as part of libxeddsa to make it possible to
# statically specify the location of libsodium for Emscripten builds.
#
########################################################################
# Tries to find the local libsodium installation.
Expand All @@ -28,8 +26,7 @@
# sodium_LIBRARY_RELEASE
#
#
# Furthermore, based on whether sodium_USE_STATIC_LIBS was set, an
# imported "sodium_static" or "sodium_shared" target is created.
# Furthermore an imported "sodium" target is created.
#

if (CMAKE_C_COMPILER_ID STREQUAL "GNU"
Expand Down Expand Up @@ -141,14 +138,12 @@ elseif (WIN32)
string(REGEX REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*" "\\1" _TARGET_ARCH "${_COMPILATION_LOG}")

# construct library path
unset(_PLATFORM_PATH)

if (_TARGET_ARCH STREQUAL "x86_32")
string(APPEND _PLATFORM_PATH "Win32")
elseif(_TARGET_ARCH STREQUAL "x86_64")
string(APPEND _PLATFORM_PATH "x64")
else()
message(FATAL_ERROR "the ${_TARGET_ARCH} architecture is not supported by FindSodium.cmake.")
message(FATAL_ERROR "the ${_TARGET_ARCH} architecture is not supported by Findsodium.cmake.")
endif()
string(APPEND _PLATFORM_PATH "/$$CONFIG$$")

Expand Down Expand Up @@ -278,43 +273,44 @@ endif()

# create imported target
if(sodium_USE_STATIC_LIBS)
add_library(sodium_static STATIC IMPORTED)
set(_LIB_TYPE STATIC)
else()
set(_LIB_TYPE SHARED)
endif()

set_target_properties(sodium_static PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
)
if(NOT TARGET sodium)
add_library(sodium ${_LIB_TYPE} IMPORTED)
endif()

set_target_properties(sodium_static PROPERTIES
set_target_properties(sodium PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
)

if (sodium_USE_STATIC_LIBS)
set_target_properties(sodium PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "SODIUM_STATIC"
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}"
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}"
)
else()
add_library(sodium_shared SHARED IMPORTED)

set_target_properties(sodium_shared PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
)

if (UNIX)
set_target_properties(sodium_shared PROPERTIES
set_target_properties(sodium PROPERTIES
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}"
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}"
)
elseif (WIN32)
set_target_properties(sodium_shared PROPERTIES
set_target_properties(sodium PROPERTIES
IMPORTED_IMPLIB "${sodium_LIBRARY_RELEASE}"
IMPORTED_IMPLIB_DEBUG "${sodium_LIBRARY_DEBUG}"
)
if (NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND"))
set_target_properties(sodium_shared PROPERTIES
set_target_properties(sodium PROPERTIES
IMPORTED_LOCATION_DEBUG "${sodium_DLL_DEBUG}"
)
endif()
if (NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND"))
set_target_properties(sodium_shared PROPERTIES
set_target_properties(sodium PROPERTIES
IMPORTED_LOCATION_RELWITHDEBINFO "${sodium_DLL_RELEASE}"
IMPORTED_LOCATION_MINSIZEREL "${sodium_DLL_RELEASE}"
IMPORTED_LOCATION_RELEASE "${sodium_DLL_RELEASE}"
Expand Down

0 comments on commit 8ab957a

Please sign in to comment.