FindCurses.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #.rst:
  4. # FindCurses
  5. # ----------
  6. #
  7. # Find the curses or ncurses include file and library.
  8. #
  9. # Result Variables
  10. # ^^^^^^^^^^^^^^^^
  11. #
  12. # This module defines the following variables:
  13. #
  14. # ``CURSES_FOUND``
  15. # True if Curses is found.
  16. # ``CURSES_INCLUDE_DIRS``
  17. # The include directories needed to use Curses.
  18. # ``CURSES_LIBRARIES``
  19. # The libraries needed to use Curses.
  20. # ``CURSES_HAVE_CURSES_H``
  21. # True if curses.h is available.
  22. # ``CURSES_HAVE_NCURSES_H``
  23. # True if ncurses.h is available.
  24. # ``CURSES_HAVE_NCURSES_NCURSES_H``
  25. # True if ``ncurses/ncurses.h`` is available.
  26. # ``CURSES_HAVE_NCURSES_CURSES_H``
  27. # True if ``ncurses/curses.h`` is available.
  28. #
  29. # Set ``CURSES_NEED_NCURSES`` to ``TRUE`` before the
  30. # ``find_package(Curses)`` call if NCurses functionality is required.
  31. # Set ``CURSES_NEED_WIDE`` to ``TRUE`` before the
  32. # ``find_package(Curses)`` call if unicode functionality is required.
  33. #
  34. # Backward Compatibility
  35. # ^^^^^^^^^^^^^^^^^^^^^^
  36. #
  37. # The following variable are provided for backward compatibility:
  38. #
  39. # ``CURSES_INCLUDE_DIR``
  40. # Path to Curses include. Use ``CURSES_INCLUDE_DIRS`` instead.
  41. # ``CURSES_LIBRARY``
  42. # Path to Curses library. Use ``CURSES_LIBRARIES`` instead.
  43. include(${CMAKE_CURRENT_LIST_DIR}/CheckLibraryExists.cmake)
  44. # we don't know anything about cursesw, so only ncurses
  45. # may be ncursesw
  46. if(NOT CURSES_NEED_WIDE)
  47. set(NCURSES_LIBRARY_NAME "ncurses")
  48. else()
  49. set(NCURSES_LIBRARY_NAME "ncursesw")
  50. # Also, if we are searchig fo wide curses - we are actually searching
  51. # for ncurses, we don't know about any other unicode version.
  52. set(CURSES_NEED_NCURSES TRUE)
  53. endif()
  54. find_library(CURSES_CURSES_LIBRARY NAMES curses )
  55. find_library(CURSES_NCURSES_LIBRARY NAMES "${NCURSES_LIBRARY_NAME}" )
  56. set(CURSES_USE_NCURSES FALSE)
  57. if(CURSES_NCURSES_LIBRARY AND ((NOT CURSES_CURSES_LIBRARY) OR CURSES_NEED_NCURSES))
  58. set(CURSES_USE_NCURSES TRUE)
  59. endif()
  60. # http://cygwin.com/ml/cygwin-announce/2010-01/msg00002.html
  61. # cygwin ncurses stopped providing curses.h symlinks see above
  62. # message. Cygwin is an ncurses package, so force ncurses on
  63. # cygwin if the curses.h is missing
  64. if(CYGWIN)
  65. if (CURSES_NEED_WIDE)
  66. if(NOT EXISTS /usr/include/ncursesw/curses.h)
  67. set(CURSES_USE_NCURSES TRUE)
  68. endif()
  69. else()
  70. if(NOT EXISTS /usr/include/curses.h)
  71. set(CURSES_USE_NCURSES TRUE)
  72. endif()
  73. endif()
  74. endif()
  75. # Not sure the logic is correct here.
  76. # If NCurses is required, use the function wsyncup() to check if the library
  77. # has NCurses functionality (at least this is where it breaks on NetBSD).
  78. # If wsyncup is in curses, use this one.
  79. # If not, try to find ncurses and check if this has the symbol.
  80. # Once the ncurses library is found, search the ncurses.h header first, but
  81. # some web pages also say that even with ncurses there is not always a ncurses.h:
  82. # http://osdir.com/ml/gnome.apps.mc.devel/2002-06/msg00029.html
  83. # So at first try ncurses.h, if not found, try to find curses.h under the same
  84. # prefix as the library was found, if still not found, try curses.h with the
  85. # default search paths.
  86. if(CURSES_CURSES_LIBRARY AND CURSES_NEED_NCURSES)
  87. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  88. cmake_push_check_state()
  89. set(CMAKE_REQUIRED_QUIET ${Curses_FIND_QUIETLY})
  90. CHECK_LIBRARY_EXISTS("${CURSES_CURSES_LIBRARY}"
  91. wsyncup "" CURSES_CURSES_HAS_WSYNCUP)
  92. if(CURSES_NCURSES_LIBRARY AND NOT CURSES_CURSES_HAS_WSYNCUP)
  93. CHECK_LIBRARY_EXISTS("${CURSES_NCURSES_LIBRARY}"
  94. wsyncup "" CURSES_NCURSES_HAS_WSYNCUP)
  95. if( CURSES_NCURSES_HAS_WSYNCUP)
  96. set(CURSES_USE_NCURSES TRUE)
  97. endif()
  98. endif()
  99. cmake_pop_check_state()
  100. endif()
  101. if(CURSES_USE_NCURSES)
  102. get_filename_component(_cursesLibDir "${CURSES_NCURSES_LIBRARY}" PATH)
  103. get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)
  104. # Use CURSES_NCURSES_INCLUDE_PATH if set, for compatibility.
  105. if(CURSES_NCURSES_INCLUDE_PATH)
  106. if (CURSES_NEED_WIDE)
  107. find_path(CURSES_INCLUDE_PATH
  108. NAMES ncursesw/ncurses.h ncursesw/curses.h
  109. PATHS ${CURSES_NCURSES_INCLUDE_PATH}
  110. NO_DEFAULT_PATH
  111. )
  112. else()
  113. find_path(CURSES_INCLUDE_PATH
  114. NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
  115. PATHS ${CURSES_NCURSES_INCLUDE_PATH}
  116. NO_DEFAULT_PATH
  117. )
  118. endif()
  119. endif()
  120. if (CURSES_NEED_WIDE)
  121. find_path(CURSES_INCLUDE_PATH
  122. NAMES ncursesw/ncurses.h ncursesw/curses.h
  123. HINTS "${_cursesParentDir}/include"
  124. )
  125. else()
  126. find_path(CURSES_INCLUDE_PATH
  127. NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
  128. HINTS "${_cursesParentDir}/include"
  129. )
  130. endif()
  131. # Previous versions of FindCurses provided these values.
  132. if(NOT DEFINED CURSES_LIBRARY)
  133. set(CURSES_LIBRARY "${CURSES_NCURSES_LIBRARY}")
  134. endif()
  135. CHECK_LIBRARY_EXISTS("${CURSES_NCURSES_LIBRARY}"
  136. cbreak "" CURSES_NCURSES_HAS_CBREAK)
  137. if(NOT CURSES_NCURSES_HAS_CBREAK)
  138. find_library(CURSES_EXTRA_LIBRARY tinfo HINTS "${_cursesLibDir}")
  139. find_library(CURSES_EXTRA_LIBRARY tinfo )
  140. endif()
  141. else()
  142. get_filename_component(_cursesLibDir "${CURSES_CURSES_LIBRARY}" PATH)
  143. get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)
  144. #We can't find anything with CURSES_NEED_WIDE because we know
  145. #only about ncursesw unicode curses version
  146. if(NOT CURSES_NEED_WIDE)
  147. find_path(CURSES_INCLUDE_PATH
  148. NAMES curses.h
  149. HINTS "${_cursesParentDir}/include"
  150. )
  151. endif()
  152. # Previous versions of FindCurses provided these values.
  153. if(NOT DEFINED CURSES_CURSES_H_PATH)
  154. set(CURSES_CURSES_H_PATH "${CURSES_INCLUDE_PATH}")
  155. endif()
  156. if(NOT DEFINED CURSES_LIBRARY)
  157. set(CURSES_LIBRARY "${CURSES_CURSES_LIBRARY}")
  158. endif()
  159. endif()
  160. # Report whether each possible header name exists in the include directory.
  161. if(NOT DEFINED CURSES_HAVE_NCURSES_NCURSES_H)
  162. if(CURSES_NEED_WIDE)
  163. if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
  164. set(CURSES_HAVE_NCURSES_NCURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
  165. endif()
  166. elseif(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
  167. set(CURSES_HAVE_NCURSES_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
  168. endif()
  169. if(NOT DEFINED CURSES_HAVE_NCURSES_NCURSES_H)
  170. set(CURSES_HAVE_NCURSES_NCURSES_H "CURSES_HAVE_NCURSES_NCURSES_H-NOTFOUND")
  171. endif()
  172. endif()
  173. if(NOT DEFINED CURSES_HAVE_NCURSES_CURSES_H)
  174. if(CURSES_NEED_WIDE)
  175. if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
  176. set(CURSES_HAVE_NCURSES_CURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
  177. endif()
  178. elseif(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
  179. set(CURSES_HAVE_NCURSES_CURSES_H "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
  180. endif()
  181. if(NOT DEFINED CURSES_HAVE_NCURSES_CURSES_H)
  182. set(CURSES_HAVE_NCURSES_CURSES_H "CURSES_HAVE_NCURSES_CURSES_H-NOTFOUND")
  183. endif()
  184. endif()
  185. if(NOT CURSES_NEED_WIDE)
  186. #ncursesw can't be found for this paths
  187. if(NOT DEFINED CURSES_HAVE_NCURSES_H)
  188. if(EXISTS "${CURSES_INCLUDE_PATH}/ncurses.h")
  189. set(CURSES_HAVE_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses.h")
  190. else()
  191. set(CURSES_HAVE_NCURSES_H "CURSES_HAVE_NCURSES_H-NOTFOUND")
  192. endif()
  193. endif()
  194. if(NOT DEFINED CURSES_HAVE_CURSES_H)
  195. if(EXISTS "${CURSES_INCLUDE_PATH}/curses.h")
  196. set(CURSES_HAVE_CURSES_H "${CURSES_INCLUDE_PATH}/curses.h")
  197. else()
  198. set(CURSES_HAVE_CURSES_H "CURSES_HAVE_CURSES_H-NOTFOUND")
  199. endif()
  200. endif()
  201. endif()
  202. find_library(CURSES_FORM_LIBRARY form HINTS "${_cursesLibDir}")
  203. find_library(CURSES_FORM_LIBRARY form )
  204. # Previous versions of FindCurses provided these values.
  205. if(NOT DEFINED FORM_LIBRARY)
  206. set(FORM_LIBRARY "${CURSES_FORM_LIBRARY}")
  207. endif()
  208. # Need to provide the *_LIBRARIES
  209. set(CURSES_LIBRARIES ${CURSES_LIBRARY})
  210. if(CURSES_EXTRA_LIBRARY)
  211. set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_EXTRA_LIBRARY})
  212. endif()
  213. if(CURSES_FORM_LIBRARY)
  214. set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_FORM_LIBRARY})
  215. endif()
  216. # Provide the *_INCLUDE_DIRS result.
  217. set(CURSES_INCLUDE_DIRS ${CURSES_INCLUDE_PATH})
  218. set(CURSES_INCLUDE_DIR ${CURSES_INCLUDE_PATH}) # compatibility
  219. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  220. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Curses DEFAULT_MSG
  221. CURSES_LIBRARY CURSES_INCLUDE_PATH)
  222. mark_as_advanced(
  223. CURSES_INCLUDE_PATH
  224. CURSES_CURSES_LIBRARY
  225. CURSES_NCURSES_LIBRARY
  226. CURSES_EXTRA_LIBRARY
  227. CURSES_FORM_LIBRARY
  228. )