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