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