FindCurses.cmake 8.4 KB

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