1# Written in 2016 by Henrik Steffen Gaßmann <henrik@gassmann.onl> 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 29if (CMAKE_C_COMPILER_ID STREQUAL "GNU" 30 OR CMAKE_C_COMPILER_ID STREQUAL "Clang") 31 set(_GCC_COMPATIBLE 1) 32endif() 33 34# static library option 35if (NOT DEFINED sodium_USE_STATIC_LIBS) 36 option(sodium_USE_STATIC_LIBS "enable to statically link against sodium" OFF) 37endif() 38if(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") 45endif() 46 47 48######################################################################## 49# UNIX 50if (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 (sodium_PKG_STATIC_LIBRARIES STREQUAL "") 68 set(sodium_PKG_STATIC_LIBRARIES libsodium.a) 69 endif() 70 71 set(XPREFIX sodium_PKG_STATIC) 72 else() 73 if (sodium_PKG_LIBRARIES STREQUAL "") 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 93elseif (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.c" [=[ 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.c" 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 206else() 207 message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") 208endif() 209 210 211######################################################################## 212# common stuff 213 214# extract sodium version 215if (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() 223endif() 224 225# communicate results 226include(FindPackageHandleStandardArgs) 227find_package_handle_standard_args(sodium 228 REQUIRED_VARS 229 sodium_LIBRARY_RELEASE 230 sodium_LIBRARY_DEBUG 231 sodium_INCLUDE_DIR 232 VERSION_VAR 233 sodium_VERSION 234) 235 236# mark file paths as advanced 237mark_as_advanced(sodium_INCLUDE_DIR) 238mark_as_advanced(sodium_LIBRARY_DEBUG) 239mark_as_advanced(sodium_LIBRARY_RELEASE) 240if (WIN32) 241 mark_as_advanced(sodium_DLL_DEBUG) 242 mark_as_advanced(sodium_DLL_RELEASE) 243endif() 244 245# create imported target 246if(sodium_USE_STATIC_LIBS) 247 set(_LIB_TYPE STATIC) 248else() 249 set(_LIB_TYPE SHARED) 250endif() 251add_library(sodium ${_LIB_TYPE} IMPORTED) 252 253set_target_properties(sodium PROPERTIES 254 INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}" 255 IMPORTED_LINK_INTERFACE_LANGUAGES "C" 256) 257 258if (sodium_USE_STATIC_LIBS) 259 set_target_properties(sodium PROPERTIES 260 INTERFACE_COMPILE_DEFINITIONS "SODIUM_STATIC" 261 IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" 262 IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" 263 ) 264else() 265 if (UNIX) 266 set_target_properties(sodium PROPERTIES 267 IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" 268 IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" 269 ) 270 elseif (WIN32) 271 set_target_properties(sodium PROPERTIES 272 IMPORTED_IMPLIB "${sodium_LIBRARY_RELEASE}" 273 IMPORTED_IMPLIB_DEBUG "${sodium_LIBRARY_DEBUG}" 274 ) 275 if (NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND")) 276 set_target_properties(sodium PROPERTIES 277 IMPORTED_LOCATION_DEBUG "${sodium_DLL_DEBUG}" 278 ) 279 endif() 280 if (NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND")) 281 set_target_properties(sodium PROPERTIES 282 IMPORTED_LOCATION_RELWITHDEBINFO "${sodium_DLL_RELEASE}" 283 IMPORTED_LOCATION_MINSIZEREL "${sodium_DLL_RELEASE}" 284 IMPORTED_LOCATION_RELEASE "${sodium_DLL_RELEASE}" 285 ) 286 endif() 287 endif() 288endif() 289