FindNettle.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (C) 2020 Dieter Baron and Thomas Klausner
  2. #
  3. # The authors can be contacted at <[email protected]>
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. #
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # 3. The names of the authors may not be used to endorse or promote
  18. # products derived from this software without specific prior
  19. # written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  22. # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  25. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  29. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  31. # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. #[=======================================================================[.rst:
  33. FindNettle
  34. -------
  35. Finds the Nettle library.
  36. Imported Targets
  37. ^^^^^^^^^^^^^^^^
  38. This module provides the following imported targets, if found:
  39. ``Nettle::Nettle``
  40. The Nettle library
  41. Result Variables
  42. ^^^^^^^^^^^^^^^^
  43. This will define the following variables:
  44. ``Nettle_FOUND``
  45. True if the system has the Nettle library.
  46. ``Nettle_VERSION``
  47. The version of the Nettle library which was found.
  48. ``Nettle_INCLUDE_DIRS``
  49. Include directories needed to use Nettle.
  50. ``Nettle_LIBRARIES``
  51. Libraries needed to link to Nettle.
  52. Cache Variables
  53. ^^^^^^^^^^^^^^^
  54. The following cache variables may also be set:
  55. ``Nettle_INCLUDE_DIR``
  56. The directory containing ``nettle/aes.h``.
  57. ``Nettle_LIBRARY``
  58. The path to the Nettle library.
  59. #]=======================================================================]
  60. find_package(PkgConfig)
  61. pkg_check_modules(PC_Nettle QUIET nettle)
  62. find_path(Nettle_INCLUDE_DIR
  63. NAMES nettle/aes.h nettle/md5.h nettle/pbkdf2.h nettle/ripemd160.h nettle/sha.h
  64. PATHS ${PC_Nettle_INCLUDE_DIRS}
  65. )
  66. find_library(Nettle_LIBRARY
  67. NAMES nettle
  68. PATHS ${PC_Nettle_LIBRARY_DIRS}
  69. )
  70. # Extract version information from the header file
  71. if(Nettle_INCLUDE_DIR)
  72. # This file only exists in nettle>=3.0
  73. if(EXISTS ${Nettle_INCLUDE_DIR}/nettle/version.h)
  74. file(STRINGS ${Nettle_INCLUDE_DIR}/nettle/version.h _ver_major_line
  75. REGEX "^#define NETTLE_VERSION_MAJOR *[0-9]+"
  76. LIMIT_COUNT 1)
  77. string(REGEX MATCH "[0-9]+"
  78. Nettle_MAJOR_VERSION "${_ver_major_line}")
  79. file(STRINGS ${Nettle_INCLUDE_DIR}/nettle/version.h _ver_minor_line
  80. REGEX "^#define NETTLE_VERSION_MINOR *[0-9]+"
  81. LIMIT_COUNT 1)
  82. string(REGEX MATCH "[0-9]+"
  83. Nettle_MINOR_VERSION "${_ver_minor_line}")
  84. set(Nettle_VERSION "${Nettle_MAJOR_VERSION}.${Nettle_MINOR_VERSION}")
  85. unset(_ver_major_line)
  86. unset(_ver_minor_line)
  87. else()
  88. if(PC_Nettle_VERSION)
  89. set(Nettle_VERSION ${PC_Nettle_VERSION})
  90. else()
  91. set(Nettle_VERSION "1.0")
  92. endif()
  93. endif()
  94. endif()
  95. include(FindPackageHandleStandardArgs)
  96. find_package_handle_standard_args(Nettle
  97. FOUND_VAR Nettle_FOUND
  98. REQUIRED_VARS
  99. Nettle_LIBRARY
  100. Nettle_INCLUDE_DIR
  101. VERSION_VAR Nettle_VERSION
  102. )
  103. if(Nettle_FOUND)
  104. set(Nettle_LIBRARIES ${Nettle_LIBRARY})
  105. set(Nettle_INCLUDE_DIRS ${Nettle_INCLUDE_DIR})
  106. set(Nettle_DEFINITIONS ${PC_Nettle_CFLAGS_OTHER})
  107. endif()
  108. if(Nettle_FOUND AND NOT TARGET Nettle::Nettle)
  109. add_library(Nettle::Nettle UNKNOWN IMPORTED)
  110. set_target_properties(Nettle::Nettle PROPERTIES
  111. IMPORTED_LOCATION "${Nettle_LIBRARY}"
  112. INTERFACE_COMPILE_OPTIONS "${PC_Nettle_CFLAGS_OTHER}"
  113. INTERFACE_INCLUDE_DIRECTORIES "${Nettle_INCLUDE_DIR}"
  114. )
  115. endif()
  116. mark_as_advanced(
  117. Nettle_INCLUDE_DIR
  118. Nettle_LIBRARY
  119. )
  120. # compatibility variables
  121. set(Nettle_VERSION_STRING ${Nettle_VERSION})