Findsodium.cmake didn't properly return the package version

This commit is contained in:
Frank Denis 2019-05-12 10:59:03 +02:00
parent 1ae8e5e3b0
commit 1b419ba366

View File

@ -1,38 +1,34 @@
# Written in 2016 by Henrik Steffen Gaßmann <henrik@gassmann.onl> # Written in 2016 by Henrik Steffen Gaßmann <henrik@gassmann.onl>
# #
# To the extent possible under law, the author(s) have dedicated all # To the extent possible under law, the author(s) have dedicated all copyright
# copyright and related and neighboring rights to this software to the # and related and neighboring rights to this software to the public domain
# public domain worldwide. This software is distributed without any warranty. # worldwide. This software is distributed without any warranty.
# #
# You should have received a copy of the CC0 Public Domain Dedication # You should have received a copy of the CC0 Public Domain Dedication along with
# along with this software. If not, see # this software. If not, see
# #
# http://creativecommons.org/publicdomain/zero/1.0/ # http://creativecommons.org/publicdomain/zero/1.0/
# #
######################################################################## # ##############################################################################
# Tries to find the local libsodium installation. # Tries to find the local libsodium installation.
# #
# On Windows the sodium_DIR environment variable is used as a default # On Windows the sodium_DIR environment variable is used as a default hint which
# hint which can be overridden by setting the corresponding cmake variable. # can be overridden by setting the corresponding cmake variable.
# #
# Once done the following variables will be defined: # Once done the following variables will be defined:
# #
# sodium_FOUND # sodium_FOUND sodium_INCLUDE_DIR sodium_LIBRARY_DEBUG sodium_LIBRARY_RELEASE
# sodium_INCLUDE_DIR # sodium_VERSION_STRING
# sodium_LIBRARY_DEBUG
# sodium_LIBRARY_RELEASE
#
# #
# Furthermore an imported "sodium" target is created. # Furthermore an imported "sodium" target is created.
# #
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(_GCC_COMPATIBLE 1) set(_GCC_COMPATIBLE 1)
endif() endif()
# static library option # static library option
if (NOT DEFINED sodium_USE_STATIC_LIBS) if(NOT DEFINED sodium_USE_STATIC_LIBS)
option(sodium_USE_STATIC_LIBS "enable to statically link against sodium" OFF) option(sodium_USE_STATIC_LIBS "enable to statically link against sodium" OFF)
endif() endif()
if(NOT (sodium_USE_STATIC_LIBS EQUAL sodium_USE_STATIC_LIBS_LAST)) if(NOT (sodium_USE_STATIC_LIBS EQUAL sodium_USE_STATIC_LIBS_LAST))
@ -41,65 +37,63 @@ if(NOT (sodium_USE_STATIC_LIBS EQUAL sodium_USE_STATIC_LIBS_LAST))
unset(sodium_LIBRARY_RELEASE CACHE) unset(sodium_LIBRARY_RELEASE CACHE)
unset(sodium_DLL_DEBUG CACHE) unset(sodium_DLL_DEBUG CACHE)
unset(sodium_DLL_RELEASE CACHE) unset(sodium_DLL_RELEASE CACHE)
set(sodium_USE_STATIC_LIBS_LAST ${sodium_USE_STATIC_LIBS} CACHE INTERNAL "internal change tracking variable") set(sodium_USE_STATIC_LIBS_LAST
${sodium_USE_STATIC_LIBS}
CACHE INTERNAL "internal change tracking variable")
endif() endif()
# ##############################################################################
########################################################################
# UNIX # UNIX
if (UNIX) if(UNIX)
# import pkg-config # import pkg-config
find_package(PkgConfig QUIET) find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND) if(PKG_CONFIG_FOUND)
pkg_check_modules(sodium_PKG QUIET libsodium) pkg_check_modules(sodium_PKG QUIET libsodium)
endif() endif()
if(sodium_USE_STATIC_LIBS) if(sodium_USE_STATIC_LIBS)
if (sodium_PKG_STATIC_LIBRARIES) if(sodium_PKG_STATIC_LIBRARIES)
foreach(_libname ${sodium_PKG_STATIC_LIBRARIES}) foreach(_libname ${sodium_PKG_STATIC_LIBRARIES})
if (NOT _libname MATCHES "^lib.*\\.a$") # ignore strings already ending with .a if(NOT _libname MATCHES "^lib.*\\.a$") # ignore strings already ending
# with .a
list(INSERT sodium_PKG_STATIC_LIBRARIES 0 "lib${_libname}.a") list(INSERT sodium_PKG_STATIC_LIBRARIES 0 "lib${_libname}.a")
endif() endif()
endforeach() endforeach()
list(REMOVE_DUPLICATES sodium_PKG_STATIC_LIBRARIES) list(REMOVE_DUPLICATES sodium_PKG_STATIC_LIBRARIES)
else() else()
# if pkgconfig for libsodium doesn't provide # if pkgconfig for libsodium doesn't provide static lib info, then
# static lib info, then override PKG_STATIC here.. # override PKG_STATIC here..
set(sodium_PKG_STATIC_LIBRARIES libsodium.a) set(sodium_PKG_STATIC_LIBRARIES libsodium.a)
endif() endif()
set(XPREFIX sodium_PKG_STATIC) set(XPREFIX sodium_PKG_STATIC)
else() else()
if (sodium_PKG_LIBRARIES STREQUAL "") if(sodium_PKG_LIBRARIES STREQUAL "")
set(sodium_PKG_LIBRARIES sodium) set(sodium_PKG_LIBRARIES sodium)
endif() endif()
set(XPREFIX sodium_PKG) set(XPREFIX sodium_PKG)
endif() endif()
find_path(sodium_INCLUDE_DIR sodium.h find_path(sodium_INCLUDE_DIR sodium.h HINTS ${${XPREFIX}_INCLUDE_DIRS})
HINTS ${${XPREFIX}_INCLUDE_DIRS} find_library(sodium_LIBRARY_DEBUG
) NAMES ${${XPREFIX}_LIBRARIES}
find_library(sodium_LIBRARY_DEBUG NAMES ${${XPREFIX}_LIBRARIES} HINTS ${${XPREFIX}_LIBRARY_DIRS})
HINTS ${${XPREFIX}_LIBRARY_DIRS} find_library(sodium_LIBRARY_RELEASE
) NAMES ${${XPREFIX}_LIBRARIES}
find_library(sodium_LIBRARY_RELEASE NAMES ${${XPREFIX}_LIBRARIES} HINTS ${${XPREFIX}_LIBRARY_DIRS})
HINTS ${${XPREFIX}_LIBRARY_DIRS}
)
# ############################################################################
######################################################################## # Windows
# Windows elseif(WIN32)
elseif (WIN32)
set(sodium_DIR "$ENV{sodium_DIR}" CACHE FILEPATH "sodium install directory") set(sodium_DIR "$ENV{sodium_DIR}" CACHE FILEPATH "sodium install directory")
mark_as_advanced(sodium_DIR) mark_as_advanced(sodium_DIR)
find_path(sodium_INCLUDE_DIR sodium.h find_path(sodium_INCLUDE_DIR sodium.h
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES include PATH_SUFFIXES include)
)
if (MSVC) if(MSVC)
# detect target architecture # detect target architecture
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/arch.c" [=[ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/arch.c" [=[
#if defined _M_IX86 #if defined _M_IX86
@ -109,116 +103,121 @@ elseif (WIN32)
#endif #endif
#error ARCH_VALUE unknown #error ARCH_VALUE unknown
]=]) ]=])
try_compile(_UNUSED_VAR "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/arch.c" try_compile(_UNUSED_VAR "${CMAKE_CURRENT_BINARY_DIR}"
OUTPUT_VARIABLE _COMPILATION_LOG "${CMAKE_CURRENT_BINARY_DIR}/arch.c"
) OUTPUT_VARIABLE _COMPILATION_LOG)
string(REGEX REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*" "\\1" _TARGET_ARCH "${_COMPILATION_LOG}") string(REGEX
REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*"
"\\1"
_TARGET_ARCH
"${_COMPILATION_LOG}")
# construct library path # construct library path
if (_TARGET_ARCH STREQUAL "x86_32") if(_TARGET_ARCH STREQUAL "x86_32")
string(APPEND _PLATFORM_PATH "Win32") string(APPEND _PLATFORM_PATH "Win32")
elseif(_TARGET_ARCH STREQUAL "x86_64") elseif(_TARGET_ARCH STREQUAL "x86_64")
string(APPEND _PLATFORM_PATH "x64") string(APPEND _PLATFORM_PATH "x64")
else() 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() endif()
string(APPEND _PLATFORM_PATH "/$$CONFIG$$") string(APPEND _PLATFORM_PATH "/$$CONFIG$$")
if (MSVC_VERSION LESS 1900) if(MSVC_VERSION LESS 1900)
math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 60") math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 60")
else() else()
math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 50") math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 50")
endif() endif()
string(APPEND _PLATFORM_PATH "/v${_VS_VERSION}") string(APPEND _PLATFORM_PATH "/v${_VS_VERSION}")
if (sodium_USE_STATIC_LIBS) if(sodium_USE_STATIC_LIBS)
string(APPEND _PLATFORM_PATH "/static") string(APPEND _PLATFORM_PATH "/static")
else() else()
string(APPEND _PLATFORM_PATH "/dynamic") string(APPEND _PLATFORM_PATH "/dynamic")
endif() endif()
string(REPLACE "$$CONFIG$$" "Debug" _DEBUG_PATH_SUFFIX "${_PLATFORM_PATH}") string(REPLACE "$$CONFIG$$"
string(REPLACE "$$CONFIG$$" "Release" _RELEASE_PATH_SUFFIX "${_PLATFORM_PATH}") "Debug"
_DEBUG_PATH_SUFFIX
"${_PLATFORM_PATH}")
string(REPLACE "$$CONFIG$$"
"Release"
_RELEASE_PATH_SUFFIX
"${_PLATFORM_PATH}")
find_library(sodium_LIBRARY_DEBUG libsodium.lib find_library(sodium_LIBRARY_DEBUG libsodium.lib
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX})
)
find_library(sodium_LIBRARY_RELEASE libsodium.lib find_library(sodium_LIBRARY_RELEASE libsodium.lib
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX})
) if(NOT sodium_USE_STATIC_LIBS)
if (NOT sodium_USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES_BCK ${CMAKE_FIND_LIBRARY_SUFFIXES}) set(CMAKE_FIND_LIBRARY_SUFFIXES_BCK ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll") set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
find_library(sodium_DLL_DEBUG libsodium find_library(sodium_DLL_DEBUG libsodium
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX})
)
find_library(sodium_DLL_RELEASE libsodium find_library(sodium_DLL_RELEASE libsodium
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX})
)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_BCK}) set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_BCK})
endif() endif()
elseif(_GCC_COMPATIBLE) elseif(_GCC_COMPATIBLE)
if (sodium_USE_STATIC_LIBS) if(sodium_USE_STATIC_LIBS)
find_library(sodium_LIBRARY_DEBUG libsodium.a find_library(sodium_LIBRARY_DEBUG libsodium.a
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES lib PATH_SUFFIXES lib)
)
find_library(sodium_LIBRARY_RELEASE libsodium.a find_library(sodium_LIBRARY_RELEASE libsodium.a
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES lib PATH_SUFFIXES lib)
)
else() else()
find_library(sodium_LIBRARY_DEBUG libsodium.dll.a find_library(sodium_LIBRARY_DEBUG libsodium.dll.a
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES lib PATH_SUFFIXES lib)
)
find_library(sodium_LIBRARY_RELEASE libsodium.dll.a find_library(sodium_LIBRARY_RELEASE libsodium.dll.a
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES lib PATH_SUFFIXES lib)
)
file(GLOB _DLL file(GLOB _DLL
LIST_DIRECTORIES false LIST_DIRECTORIES false
RELATIVE "${sodium_DIR}/bin" RELATIVE "${sodium_DIR}/bin"
"${sodium_DIR}/bin/libsodium*.dll" "${sodium_DIR}/bin/libsodium*.dll")
)
find_library(sodium_DLL_DEBUG ${_DLL} libsodium find_library(sodium_DLL_DEBUG ${_DLL} libsodium
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES bin PATH_SUFFIXES bin)
)
find_library(sodium_DLL_RELEASE ${_DLL} libsodium find_library(sodium_DLL_RELEASE ${_DLL} libsodium
HINTS ${sodium_DIR} HINTS ${sodium_DIR}
PATH_SUFFIXES bin PATH_SUFFIXES bin)
)
endif() endif()
else() else()
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") message(FATAL_ERROR "this platform is not supported by FindSodium.cmake")
endif() endif()
# ############################################################################
######################################################################## # unsupported
# unsupported
else() else()
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") message(FATAL_ERROR "this platform is not supported by FindSodium.cmake")
endif() endif()
# ##############################################################################
########################################################################
# common stuff # common stuff
# extract sodium version # extract sodium version
if (sodium_INCLUDE_DIR) if(sodium_INCLUDE_DIR)
set(_VERSION_HEADER "${_INCLUDE_DIR}/sodium/version.h") set(_VERSION_HEADER "${sodium_INCLUDE_DIR}/sodium/version.h")
if (EXISTS _VERSION_HEADER) if(EXISTS "${_VERSION_HEADER}")
file(READ "${_VERSION_HEADER}" _VERSION_HEADER_CONTENT) file(READ "${_VERSION_HEADER}" _VERSION_HEADER_CONTENT)
string(REGEX REPLACE ".*#[ \t]*define[ \t]*SODIUM_VERSION_STRING[ \t]*\"([^\n]*)\".*" "\\1" string(
sodium_VERSION "${_VERSION_HEADER_CONTENT}") REGEX
set(sodium_VERSION "${sodium_VERSION}" PARENT_SCOPE) REPLACE
".*#[ \t]*define[ \t]*sodium_VERSION_STRING_STRING[ \t]*\"([^\n]*)\".*"
"\\1"
sodium_VERSION_STRING
"${_VERSION_HEADER_CONTENT}")
set(sodium_VERSION_STRING "${sodium_VERSION_STRING}")
endif() endif()
endif() endif()
@ -230,14 +229,13 @@ find_package_handle_standard_args(sodium
sodium_LIBRARY_DEBUG sodium_LIBRARY_DEBUG
sodium_INCLUDE_DIR sodium_INCLUDE_DIR
VERSION_VAR VERSION_VAR
sodium_VERSION sodium_VERSION_STRING)
)
# mark file paths as advanced # mark file paths as advanced
mark_as_advanced(sodium_INCLUDE_DIR) mark_as_advanced(sodium_INCLUDE_DIR)
mark_as_advanced(sodium_LIBRARY_DEBUG) mark_as_advanced(sodium_LIBRARY_DEBUG)
mark_as_advanced(sodium_LIBRARY_RELEASE) mark_as_advanced(sodium_LIBRARY_RELEASE)
if (WIN32) if(WIN32)
mark_as_advanced(sodium_DLL_DEBUG) mark_as_advanced(sodium_DLL_DEBUG)
mark_as_advanced(sodium_DLL_RELEASE) mark_as_advanced(sodium_DLL_RELEASE)
endif() endif()
@ -250,39 +248,46 @@ else()
endif() endif()
add_library(sodium ${_LIB_TYPE} IMPORTED) add_library(sodium ${_LIB_TYPE} IMPORTED)
set_target_properties(sodium PROPERTIES set_target_properties(sodium
INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}" PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C" "${sodium_INCLUDE_DIR}"
) IMPORTED_LINK_INTERFACE_LANGUAGES
"C")
if (sodium_USE_STATIC_LIBS) if(sodium_USE_STATIC_LIBS)
set_target_properties(sodium PROPERTIES set_target_properties(sodium
INTERFACE_COMPILE_DEFINITIONS "SODIUM_STATIC" PROPERTIES INTERFACE_COMPILE_DEFINITIONS
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" "SODIUM_STATIC"
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" IMPORTED_LOCATION
) "${sodium_LIBRARY_RELEASE}"
IMPORTED_LOCATION_DEBUG
"${sodium_LIBRARY_DEBUG}")
else() else()
if (UNIX) if(UNIX)
set_target_properties(sodium PROPERTIES set_target_properties(sodium
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" PROPERTIES IMPORTED_LOCATION
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" "${sodium_LIBRARY_RELEASE}"
) IMPORTED_LOCATION_DEBUG
elseif (WIN32) "${sodium_LIBRARY_DEBUG}")
set_target_properties(sodium PROPERTIES elseif(WIN32)
IMPORTED_IMPLIB "${sodium_LIBRARY_RELEASE}" set_target_properties(sodium
IMPORTED_IMPLIB_DEBUG "${sodium_LIBRARY_DEBUG}" PROPERTIES IMPORTED_IMPLIB
) "${sodium_LIBRARY_RELEASE}"
if (NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND")) IMPORTED_IMPLIB_DEBUG
set_target_properties(sodium PROPERTIES "${sodium_LIBRARY_DEBUG}")
IMPORTED_LOCATION_DEBUG "${sodium_DLL_DEBUG}" if(NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND"))
) set_target_properties(sodium
PROPERTIES IMPORTED_LOCATION_DEBUG
"${sodium_DLL_DEBUG}")
endif() endif()
if (NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND")) if(NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND"))
set_target_properties(sodium PROPERTIES set_target_properties(sodium
IMPORTED_LOCATION_RELWITHDEBINFO "${sodium_DLL_RELEASE}" PROPERTIES IMPORTED_LOCATION_RELWITHDEBINFO
IMPORTED_LOCATION_MINSIZEREL "${sodium_DLL_RELEASE}" "${sodium_DLL_RELEASE}"
IMPORTED_LOCATION_RELEASE "${sodium_DLL_RELEASE}" IMPORTED_LOCATION_MINSIZEREL
) "${sodium_DLL_RELEASE}"
IMPORTED_LOCATION_RELEASE
"${sodium_DLL_RELEASE}")
endif() endif()
endif() endif()
endif() endif()