| 1 |
# Written in 2016 by Henrik Steffen Gaßmann <[email protected]> |
| 2 |
# |
| 3 |
# To the extent possible under law, the author(s) have dedicated all |
| 4 |
# copyright and related and neighboring rights to this software to the |
| 5 |
# public domain worldwide. This software is distributed without any warranty. |
| 6 |
# |
| 7 |
# You should have received a copy of the CC0 Public Domain Dedication |
| 8 |
# along with this software. If not, see |
| 9 |
# |
| 10 |
# http://creativecommons.org/publicdomain/zero/1.0/ |
| 11 |
# |
| 12 |
######################################################################## |
| 13 |
# Tries to find the local libsodium installation. |
| 14 |
# |
| 15 |
# On Windows the sodium_DIR environment variable is used as a default |
| 16 |
# hint which can be overridden by setting the corresponding cmake variable. |
| 17 |
# |
| 18 |
# Once done the following variables will be defined: |
| 19 |
# |
| 20 |
# sodium_FOUND |
| 21 |
# sodium_INCLUDE_DIR |
| 22 |
# sodium_LIBRARY_DEBUG |
| 23 |
# sodium_LIBRARY_RELEASE |
| 24 |
# |
| 25 |
# |
| 26 |
# Furthermore an imported "sodium" target is created. |
| 27 |
# |
| 28 |
|
| 29 |
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" |
| 30 |
OR CMAKE_C_COMPILER_ID STREQUAL "Clang") |
| 31 |
set(_GCC_COMPATIBLE 1) |
| 32 |
endif() |
| 33 |
|
| 34 |
# static library option |
| 35 |
if (NOT DEFINED sodium_USE_STATIC_LIBS) |
| 36 |
option(sodium_USE_STATIC_LIBS "enable to statically link against sodium" OFF) |
| 37 |
endif() |
| 38 |
if(NOT (sodium_USE_STATIC_LIBS EQUAL sodium_USE_STATIC_LIBS_LAST)) |
| 39 |
unset(sodium_LIBRARY CACHE) |
| 40 |
unset(sodium_LIBRARY_DEBUG CACHE) |
| 41 |
unset(sodium_LIBRARY_RELEASE CACHE) |
| 42 |
unset(sodium_DLL_DEBUG CACHE) |
| 43 |
unset(sodium_DLL_RELEASE CACHE) |
| 44 |
set(sodium_USE_STATIC_LIBS_LAST ${sodium_USE_STATIC_LIBS} CACHE INTERNAL "internal change tracking variable") |
| 45 |
endif() |
| 46 |
|
| 47 |
|
| 48 |
######################################################################## |
| 49 |
# UNIX |
| 50 |
if (UNIX) |
| 51 |
# import pkg-config |
| 52 |
find_package(PkgConfig QUIET) |
| 53 |
if (PKG_CONFIG_FOUND) |
| 54 |
pkg_check_modules(sodium_PKG QUIET libsodium) |
| 55 |
endif() |
| 56 |
|
| 57 |
if(sodium_USE_STATIC_LIBS) |
| 58 |
foreach(_libname ${sodium_PKG_STATIC_LIBRARIES}) |
| 59 |
if (NOT _libname MATCHES "^lib.*\\.a$") # ignore strings already ending with .a |
| 60 |
list(INSERT sodium_PKG_STATIC_LIBRARIES 0 "lib${_libname}.a") |
| 61 |
endif() |
| 62 |
endforeach() |
| 63 |
list(REMOVE_DUPLICATES sodium_PKG_STATIC_LIBRARIES) |
| 64 |
|
| 65 |
# if pkgconfig for libsodium doesn't provide |
| 66 |
# static lib info, then override PKG_STATIC here.. |
| 67 |
if (NOT sodium_PKG_STATIC_FOUND) |
| 68 |
set(sodium_PKG_STATIC_LIBRARIES libsodium.a) |
| 69 |
endif() |
| 70 |
|
| 71 |
set(XPREFIX sodium_PKG_STATIC) |
| 72 |
else() |
| 73 |
if (NOT sodium_PKG_FOUND) |
| 74 |
set(sodium_PKG_LIBRARIES sodium) |
| 75 |
endif() |
| 76 |
|
| 77 |
set(XPREFIX sodium_PKG) |
| 78 |
endif() |
| 79 |
|
| 80 |
find_path(sodium_INCLUDE_DIR sodium.h |
| 81 |
HINTS ${${XPREFIX}_INCLUDE_DIRS} |
| 82 |
) |
| 83 |
find_library(sodium_LIBRARY_DEBUG NAMES ${${XPREFIX}_LIBRARIES} |
| 84 |
HINTS ${${XPREFIX}_LIBRARY_DIRS} |
| 85 |
) |
| 86 |
find_library(sodium_LIBRARY_RELEASE NAMES ${${XPREFIX}_LIBRARIES} |
| 87 |
HINTS ${${XPREFIX}_LIBRARY_DIRS} |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
######################################################################## |
| 92 |
# Windows |
| 93 |
elseif (WIN32) |
| 94 |
set(sodium_DIR "$ENV{sodium_DIR}" CACHE FILEPATH "sodium install directory") |
| 95 |
mark_as_advanced(sodium_DIR) |
| 96 |
|
| 97 |
find_path(sodium_INCLUDE_DIR sodium.h |
| 98 |
HINTS ${sodium_DIR} |
| 99 |
PATH_SUFFIXES include |
| 100 |
) |
| 101 |
|
| 102 |
if (MSVC) |
| 103 |
# detect target architecture |
| 104 |
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp" [=[ |
| 105 |
#if defined _M_IX86 |
| 106 |
#error ARCH_VALUE x86_32 |
| 107 |
#elif defined _M_X64 |
| 108 |
#error ARCH_VALUE x86_64 |
| 109 |
#endif |
| 110 |
#error ARCH_VALUE unknown |
| 111 |
]=]) |
| 112 |
try_compile(_UNUSED_VAR "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp" |
| 113 |
OUTPUT_VARIABLE _COMPILATION_LOG |
| 114 |
) |
| 115 |
string(REGEX REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*" "\\1" _TARGET_ARCH "${_COMPILATION_LOG}") |
| 116 |
|
| 117 |
# construct library path |
| 118 |
if (_TARGET_ARCH STREQUAL "x86_32") |
| 119 |
string(APPEND _PLATFORM_PATH "Win32") |
| 120 |
elseif(_TARGET_ARCH STREQUAL "x86_64") |
| 121 |
string(APPEND _PLATFORM_PATH "x64") |
| 122 |
else() |
| 123 |
message(FATAL_ERROR "the ${_TARGET_ARCH} architecture is not supported by Findsodium.cmake.") |
| 124 |
endif() |
| 125 |
string(APPEND _PLATFORM_PATH "/$$CONFIG$$") |
| 126 |
|
| 127 |
if (MSVC_VERSION LESS 1900) |
| 128 |
math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 60") |
| 129 |
else() |
| 130 |
math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 50") |
| 131 |
endif() |
| 132 |
string(APPEND _PLATFORM_PATH "/v${_VS_VERSION}") |
| 133 |
|
| 134 |
if (sodium_USE_STATIC_LIBS) |
| 135 |
string(APPEND _PLATFORM_PATH "/static") |
| 136 |
else() |
| 137 |
string(APPEND _PLATFORM_PATH "/dynamic") |
| 138 |
endif() |
| 139 |
|
| 140 |
string(REPLACE "$$CONFIG$$" "Debug" _DEBUG_PATH_SUFFIX "${_PLATFORM_PATH}") |
| 141 |
string(REPLACE "$$CONFIG$$" "Release" _RELEASE_PATH_SUFFIX "${_PLATFORM_PATH}") |
| 142 |
|
| 143 |
find_library(sodium_LIBRARY_DEBUG libsodium.lib |
| 144 |
HINTS ${sodium_DIR} |
| 145 |
PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} |
| 146 |
) |
| 147 |
find_library(sodium_LIBRARY_RELEASE libsodium.lib |
| 148 |
HINTS ${sodium_DIR} |
| 149 |
PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} |
| 150 |
) |
| 151 |
if (NOT sodium_USE_STATIC_LIBS) |
| 152 |
set(CMAKE_FIND_LIBRARY_SUFFIXES_BCK ${CMAKE_FIND_LIBRARY_SUFFIXES}) |
| 153 |
set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll") |
| 154 |
find_library(sodium_DLL_DEBUG libsodium |
| 155 |
HINTS ${sodium_DIR} |
| 156 |
PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} |
| 157 |
) |
| 158 |
find_library(sodium_DLL_RELEASE libsodium |
| 159 |
HINTS ${sodium_DIR} |
| 160 |
PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} |
| 161 |
) |
| 162 |
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_BCK}) |
| 163 |
endif() |
| 164 |
|
| 165 |
elseif(_GCC_COMPATIBLE) |
| 166 |
if (sodium_USE_STATIC_LIBS) |
| 167 |
find_library(sodium_LIBRARY_DEBUG libsodium.a |
| 168 |
HINTS ${sodium_DIR} |
| 169 |
PATH_SUFFIXES lib |
| 170 |
) |
| 171 |
find_library(sodium_LIBRARY_RELEASE libsodium.a |
| 172 |
HINTS ${sodium_DIR} |
| 173 |
PATH_SUFFIXES lib |
| 174 |
) |
| 175 |
else() |
| 176 |
find_library(sodium_LIBRARY_DEBUG libsodium.dll.a |
| 177 |
HINTS ${sodium_DIR} |
| 178 |
PATH_SUFFIXES lib |
| 179 |
) |
| 180 |
find_library(sodium_LIBRARY_RELEASE libsodium.dll.a |
| 181 |
HINTS ${sodium_DIR} |
| 182 |
PATH_SUFFIXES lib |
| 183 |
) |
| 184 |
|
| 185 |
file(GLOB _DLL |
| 186 |
LIST_DIRECTORIES false |
| 187 |
RELATIVE "${sodium_DIR}/bin" |
| 188 |
"${sodium_DIR}/bin/libsodium*.dll" |
| 189 |
) |
| 190 |
find_library(sodium_DLL_DEBUG ${_DLL} libsodium |
| 191 |
HINTS ${sodium_DIR} |
| 192 |
PATH_SUFFIXES bin |
| 193 |
) |
| 194 |
find_library(sodium_DLL_RELEASE ${_DLL} libsodium |
| 195 |
HINTS ${sodium_DIR} |
| 196 |
PATH_SUFFIXES bin |
| 197 |
) |
| 198 |
endif() |
| 199 |
else() |
| 200 |
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") |
| 201 |
endif() |
| 202 |
|
| 203 |
|
| 204 |
######################################################################## |
| 205 |
# unsupported |
| 206 |
else() |
| 207 |
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") |
| 208 |
endif() |
| 209 |
|
| 210 |
|
| 211 |
######################################################################## |
| 212 |
# common stuff |
| 213 |
|
| 214 |
# extract sodium version |
| 215 |
if (sodium_INCLUDE_DIR) |
| 216 |
set(_VERSION_HEADER "${_INCLUDE_DIR}/sodium/version.h") |
| 217 |
if (EXISTS _VERSION_HEADER) |
| 218 |
file(READ "${_VERSION_HEADER}" _VERSION_HEADER_CONTENT) |
| 219 |
string(REGEX REPLACE ".*#[ \t]*define[ \t]*SODIUM_VERSION_STRING[ \t]*\"([^\n]*)\".*" "\\1" |
| 220 |
sodium_VERSION "${_VERSION_HEADER_CONTENT}") |
| 221 |
set(sodium_VERSION "${sodium_VERSION}" PARENT_SCOPE) |
| 222 |
endif() |
| 223 |
endif() |
| 224 |
|
| 225 |
# communicate results |
| 226 |
include(FindPackageHandleStandardArgs) |
| 227 |
find_package_handle_standard_args( |
| 228 |
Sodium # The name must be either uppercase or match the filename case. |
| 229 |
REQUIRED_VARS |
| 230 |
sodium_LIBRARY_RELEASE |
| 231 |
sodium_LIBRARY_DEBUG |
| 232 |
sodium_INCLUDE_DIR |
| 233 |
VERSION_VAR |
| 234 |
sodium_VERSION |
| 235 |
) |
| 236 |
|
| 237 |
if(Sodium_FOUND) |
| 238 |
set(sodium_LIBRARIES |
| 239 |
optimized ${sodium_LIBRARY_RELEASE} debug ${sodium_LIBRARY_DEBUG}) |
| 240 |
endif() |
| 241 |
|
| 242 |
# mark file paths as advanced |
| 243 |
mark_as_advanced(sodium_INCLUDE_DIR) |
| 244 |
mark_as_advanced(sodium_LIBRARY_DEBUG) |
| 245 |
mark_as_advanced(sodium_LIBRARY_RELEASE) |
| 246 |
if (WIN32) |
| 247 |
mark_as_advanced(sodium_DLL_DEBUG) |
| 248 |
mark_as_advanced(sodium_DLL_RELEASE) |
| 249 |
endif() |
| 250 |
|
| 251 |
# create imported target |
| 252 |
if(sodium_USE_STATIC_LIBS) |
| 253 |
set(_LIB_TYPE STATIC) |
| 254 |
else() |
| 255 |
set(_LIB_TYPE SHARED) |
| 256 |
endif() |
| 257 |
|
| 258 |
if(NOT TARGET sodium) |
| 259 |
add_library(sodium ${_LIB_TYPE} IMPORTED) |
| 260 |
endif() |
| 261 |
|
| 262 |
set_target_properties(sodium PROPERTIES |
| 263 |
INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}" |
| 264 |
IMPORTED_LINK_INTERFACE_LANGUAGES "C" |
| 265 |
) |
| 266 |
|
| 267 |
if (sodium_USE_STATIC_LIBS) |
| 268 |
set_target_properties(sodium PROPERTIES |
| 269 |
INTERFACE_COMPILE_DEFINITIONS "SODIUM_STATIC" |
| 270 |
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" |
| 271 |
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" |
| 272 |
) |
| 273 |
else() |
| 274 |
if (UNIX) |
| 275 |
set_target_properties(sodium PROPERTIES |
| 276 |
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" |
| 277 |
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" |
| 278 |
) |
| 279 |
elseif (WIN32) |
| 280 |
set_target_properties(sodium PROPERTIES |
| 281 |
IMPORTED_IMPLIB "${sodium_LIBRARY_RELEASE}" |
| 282 |
IMPORTED_IMPLIB_DEBUG "${sodium_LIBRARY_DEBUG}" |
| 283 |
) |
| 284 |
if (NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND")) |
| 285 |
set_target_properties(sodium PROPERTIES |
| 286 |
IMPORTED_LOCATION_DEBUG "${sodium_DLL_DEBUG}" |
| 287 |
) |
| 288 |
endif() |
| 289 |
if (NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND")) |
| 290 |
set_target_properties(sodium PROPERTIES |
| 291 |
IMPORTED_LOCATION_RELWITHDEBINFO "${sodium_DLL_RELEASE}" |
| 292 |
IMPORTED_LOCATION_MINSIZEREL "${sodium_DLL_RELEASE}" |
| 293 |
IMPORTED_LOCATION_RELEASE "${sodium_DLL_RELEASE}" |
| 294 |
) |
| 295 |
endif() |
| 296 |
endif() |
| 297 |
endif() |