FindCurses.cmake 9.2 KB

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