FindCurses.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindCurses
  5. ----------
  6. Finds the curses or ncurses library:
  7. .. code-block:: cmake
  8. find_package(Curses [...])
  9. Curses is a terminal control library for Unix-like systems, used to build text
  10. user interface (TUI) applications. Originally developed in 1978, it has since
  11. evolved into multiple implementations, most notably ncurses (new curses), BSD
  12. curses, and PDCurses (a public domain curses library for non-Unix platforms).
  13. Result Variables
  14. ^^^^^^^^^^^^^^^^
  15. This module defines the following variables:
  16. ``Curses_FOUND``
  17. .. versionadded:: 3.3
  18. Boolean indicating whether Curses was found.
  19. ``CURSES_INCLUDE_DIRS``
  20. .. versionadded:: 3.1
  21. The include directories needed to use Curses.
  22. ``CURSES_LIBRARIES``
  23. The libraries needed to use Curses.
  24. ``CURSES_CFLAGS``
  25. .. versionadded:: 3.16
  26. Compiler flags which ought be given to C/C++ compilers when using Curses.
  27. ``CURSES_HAVE_CURSES_H``
  28. Boolean indicating whether ``curses.h`` is available.
  29. ``CURSES_HAVE_NCURSES_H``
  30. Boolean indicating whether ``ncurses.h`` is available.
  31. ``CURSES_HAVE_NCURSES_NCURSES_H``
  32. Boolean indicating whether ``ncurses/ncurses.h`` is available.
  33. ``CURSES_HAVE_NCURSES_CURSES_H``
  34. Boolean indicating whether ``ncurses/curses.h`` is available.
  35. Hints
  36. ^^^^^
  37. This module accepts the following variables:
  38. ``CURSES_NEED_NCURSES``
  39. Set this variable to ``TRUE`` before calling ``find_package(Curses)`` if the
  40. the ncurses implementation functionality is specifically required.
  41. ``CURSES_NEED_WIDE``
  42. .. versionadded:: 3.10
  43. Set this variable to ``TRUE`` before calling ``find_package(Curses)`` if
  44. Unicode (wide character) support is required.
  45. Deprecated Variables
  46. ^^^^^^^^^^^^^^^^^^^^
  47. The following variables are provided for backward compatibility:
  48. ``CURSES_FOUND``
  49. .. deprecated:: 4.2
  50. Use ``Curses_FOUND``, which has the same value.
  51. Boolean indicating whether Curses was found.
  52. ``CURSES_INCLUDE_DIR``
  53. .. deprecated:: 3.1
  54. Use the ``CURSES_INCLUDE_DIRS`` variable instead.
  55. Path to a Curses include directory.
  56. ``CURSES_LIBRARY``
  57. .. deprecated:: 2.4
  58. Use the ``CURSES_LIBRARIES`` variable instead.
  59. Path to Curses library.
  60. Examples
  61. ^^^^^^^^
  62. Finding Curses and creating an imported interface target for linking it to a
  63. project target:
  64. .. code-block:: cmake
  65. find_package(Curses)
  66. if(Curses_FOUND AND NOT TARGET Curses::Curses)
  67. add_library(Curses::Curses INTERFACE IMPORTED)
  68. set_target_properties(
  69. Curses::Curses
  70. PROPERTIES
  71. INTERFACE_LINK_LIBRARIES "${CURSES_LIBRARIES}"
  72. INTERFACE_INCLUDE_DIRECTORIES "${CURSES_INCLUDE_DIRS}"
  73. )
  74. endif()
  75. add_executable(app app.c)
  76. target_link_libraries(app PRIVATE Curses::Curses)
  77. When ncurses is specifically required:
  78. .. code-block:: cmake
  79. set(CURSES_NEED_NCURSES TRUE)
  80. find_package(Curses)
  81. #]=======================================================================]
  82. include(${CMAKE_CURRENT_LIST_DIR}/CheckLibraryExists.cmake)
  83. # we don't know anything about cursesw, so only ncurses
  84. # may be ncursesw
  85. if(NOT CURSES_NEED_WIDE)
  86. set(NCURSES_LIBRARY_NAME "ncurses")
  87. set(CURSES_FORM_LIBRARY_NAME "form")
  88. else()
  89. set(NCURSES_LIBRARY_NAME "ncursesw")
  90. set(CURSES_FORM_LIBRARY_NAME "formw")
  91. # Also, if we are searching for wide curses - we are actually searching
  92. # for ncurses, we don't know about any other unicode version.
  93. set(CURSES_NEED_NCURSES TRUE)
  94. endif()
  95. find_library(CURSES_CURSES_LIBRARY NAMES curses)
  96. find_library(CURSES_NCURSES_LIBRARY NAMES "${NCURSES_LIBRARY_NAME}" )
  97. set(CURSES_USE_NCURSES FALSE)
  98. if(CURSES_NCURSES_LIBRARY AND ((NOT CURSES_CURSES_LIBRARY) OR CURSES_NEED_NCURSES))
  99. set(CURSES_USE_NCURSES TRUE)
  100. endif()
  101. # http://cygwin.com/ml/cygwin-announce/2010-01/msg00002.html
  102. # cygwin ncurses stopped providing curses.h symlinks see above
  103. # message. Cygwin is an ncurses package, so force ncurses on
  104. # cygwin if the curses.h is missing
  105. if(CURSES_NCURSES_LIBRARY AND CYGWIN)
  106. if (CURSES_NEED_WIDE)
  107. if(NOT EXISTS /usr/include/ncursesw/curses.h)
  108. set(CURSES_USE_NCURSES TRUE)
  109. endif()
  110. else()
  111. if(NOT EXISTS /usr/include/curses.h)
  112. set(CURSES_USE_NCURSES TRUE)
  113. endif()
  114. endif()
  115. endif()
  116. # Not sure the logic is correct here.
  117. # If NCurses is required, use the function wsyncup() to check if the library
  118. # has NCurses functionality (at least this is where it breaks on NetBSD).
  119. # If wsyncup is in curses, use this one.
  120. # If not, try to find ncurses and check if this has the symbol.
  121. # Once the ncurses library is found, search the ncurses.h header first, but
  122. # some web pages also say that even with ncurses there is not always a ncurses.h:
  123. # http://osdir.com/ml/gnome.apps.mc.devel/2002-06/msg00029.html
  124. # So at first try ncurses.h, if not found, try to find curses.h under the same
  125. # prefix as the library was found, if still not found, try curses.h with the
  126. # default search paths.
  127. if(CURSES_CURSES_LIBRARY AND CURSES_NEED_NCURSES)
  128. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  129. cmake_push_check_state()
  130. set(CMAKE_REQUIRED_QUIET ${Curses_FIND_QUIETLY})
  131. check_library_exists("${CURSES_CURSES_LIBRARY}"
  132. wsyncup "" CURSES_CURSES_HAS_WSYNCUP)
  133. if(CURSES_NCURSES_LIBRARY AND NOT CURSES_CURSES_HAS_WSYNCUP)
  134. check_library_exists("${CURSES_NCURSES_LIBRARY}"
  135. wsyncup "" CURSES_NCURSES_HAS_WSYNCUP)
  136. if( CURSES_NCURSES_HAS_WSYNCUP)
  137. set(CURSES_USE_NCURSES TRUE)
  138. endif()
  139. endif()
  140. cmake_pop_check_state()
  141. endif()
  142. if(CURSES_USE_NCURSES)
  143. get_filename_component(_cursesLibDir "${CURSES_NCURSES_LIBRARY}" PATH)
  144. get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)
  145. # Use CURSES_NCURSES_INCLUDE_PATH if set, for compatibility.
  146. if(CURSES_NCURSES_INCLUDE_PATH)
  147. if (CURSES_NEED_WIDE)
  148. find_path(CURSES_INCLUDE_PATH
  149. NAMES ncursesw/ncurses.h ncursesw/curses.h ncursesw.h cursesw.h
  150. PATHS ${CURSES_NCURSES_INCLUDE_PATH}
  151. NO_DEFAULT_PATH
  152. )
  153. else()
  154. find_path(CURSES_INCLUDE_PATH
  155. NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
  156. PATHS ${CURSES_NCURSES_INCLUDE_PATH}
  157. NO_DEFAULT_PATH
  158. )
  159. endif()
  160. endif()
  161. if (CURSES_NEED_WIDE)
  162. set(CURSES_TINFO_LIBRARY_NAME tinfow)
  163. find_path(CURSES_INCLUDE_PATH
  164. NAMES ncursesw/ncurses.h ncursesw/curses.h ncursesw.h cursesw.h
  165. HINTS "${_cursesParentDir}/include"
  166. )
  167. else()
  168. set(CURSES_TINFO_LIBRARY_NAME tinfo)
  169. find_path(CURSES_INCLUDE_PATH
  170. NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
  171. HINTS "${_cursesParentDir}/include"
  172. )
  173. endif()
  174. # Previous versions of FindCurses provided these values.
  175. if(NOT DEFINED CURSES_LIBRARY)
  176. set(CURSES_LIBRARY "${CURSES_NCURSES_LIBRARY}")
  177. endif()
  178. check_library_exists("${CURSES_NCURSES_LIBRARY}"
  179. cbreak "" CURSES_NCURSES_HAS_CBREAK)
  180. check_library_exists("${CURSES_NCURSES_LIBRARY}"
  181. nodelay "" CURSES_NCURSES_HAS_NODELAY)
  182. if(NOT CURSES_NCURSES_HAS_CBREAK OR NOT CURSES_NCURSES_HAS_NODELAY)
  183. find_library(CURSES_EXTRA_LIBRARY "${CURSES_TINFO_LIBRARY_NAME}" HINTS "${_cursesLibDir}")
  184. find_library(CURSES_EXTRA_LIBRARY "${CURSES_TINFO_LIBRARY_NAME}" )
  185. mark_as_advanced(
  186. CURSES_EXTRA_LIBRARY
  187. )
  188. endif()
  189. else()
  190. get_filename_component(_cursesLibDir "${CURSES_CURSES_LIBRARY}" PATH)
  191. get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)
  192. # We can't find anything with CURSES_NEED_WIDE because we know
  193. # only about ncursesw unicode curses version
  194. if(NOT CURSES_NEED_WIDE)
  195. find_path(CURSES_INCLUDE_PATH
  196. NAMES curses.h
  197. HINTS "${_cursesParentDir}/include"
  198. )
  199. endif()
  200. # Previous versions of FindCurses provided these values.
  201. if(NOT DEFINED CURSES_CURSES_H_PATH)
  202. set(CURSES_CURSES_H_PATH "${CURSES_INCLUDE_PATH}")
  203. endif()
  204. if(NOT DEFINED CURSES_LIBRARY)
  205. set(CURSES_LIBRARY "${CURSES_CURSES_LIBRARY}")
  206. endif()
  207. endif()
  208. # Report whether each possible header name exists in the include directory.
  209. if(NOT DEFINED CURSES_HAVE_NCURSES_NCURSES_H)
  210. if(CURSES_NEED_WIDE)
  211. if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
  212. set(CURSES_HAVE_NCURSES_NCURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
  213. endif()
  214. elseif(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
  215. set(CURSES_HAVE_NCURSES_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
  216. endif()
  217. if(NOT DEFINED CURSES_HAVE_NCURSES_NCURSES_H)
  218. set(CURSES_HAVE_NCURSES_NCURSES_H "CURSES_HAVE_NCURSES_NCURSES_H-NOTFOUND")
  219. endif()
  220. endif()
  221. if(NOT DEFINED CURSES_HAVE_NCURSES_CURSES_H)
  222. if(CURSES_NEED_WIDE)
  223. if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
  224. set(CURSES_HAVE_NCURSES_CURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
  225. endif()
  226. elseif(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
  227. set(CURSES_HAVE_NCURSES_CURSES_H "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
  228. endif()
  229. if(NOT DEFINED CURSES_HAVE_NCURSES_CURSES_H)
  230. set(CURSES_HAVE_NCURSES_CURSES_H "CURSES_HAVE_NCURSES_CURSES_H-NOTFOUND")
  231. endif()
  232. endif()
  233. if(NOT CURSES_NEED_WIDE)
  234. # ncursesw can't be found for this paths
  235. if(NOT DEFINED CURSES_HAVE_NCURSES_H)
  236. if(EXISTS "${CURSES_INCLUDE_PATH}/ncurses.h")
  237. set(CURSES_HAVE_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses.h")
  238. else()
  239. set(CURSES_HAVE_NCURSES_H "CURSES_HAVE_NCURSES_H-NOTFOUND")
  240. endif()
  241. endif()
  242. if(NOT DEFINED CURSES_HAVE_CURSES_H)
  243. if(EXISTS "${CURSES_INCLUDE_PATH}/curses.h")
  244. set(CURSES_HAVE_CURSES_H "${CURSES_INCLUDE_PATH}/curses.h")
  245. else()
  246. set(CURSES_HAVE_CURSES_H "CURSES_HAVE_CURSES_H-NOTFOUND")
  247. endif()
  248. endif()
  249. endif()
  250. find_library(CURSES_FORM_LIBRARY "${CURSES_FORM_LIBRARY_NAME}" HINTS "${_cursesLibDir}")
  251. find_library(CURSES_FORM_LIBRARY "${CURSES_FORM_LIBRARY_NAME}" )
  252. # Previous versions of FindCurses provided these values.
  253. if(NOT DEFINED FORM_LIBRARY)
  254. set(FORM_LIBRARY "${CURSES_FORM_LIBRARY}")
  255. endif()
  256. # Need to provide the *_LIBRARIES
  257. set(CURSES_LIBRARIES ${CURSES_LIBRARY})
  258. if(CURSES_EXTRA_LIBRARY)
  259. set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_EXTRA_LIBRARY})
  260. endif()
  261. if(CURSES_FORM_LIBRARY)
  262. set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_FORM_LIBRARY})
  263. endif()
  264. # Provide the *_INCLUDE_DIRS and *_CFLAGS results.
  265. set(CURSES_INCLUDE_DIRS ${CURSES_INCLUDE_PATH})
  266. set(CURSES_INCLUDE_DIR ${CURSES_INCLUDE_PATH}) # compatibility
  267. find_package(PkgConfig QUIET)
  268. if(PkgConfig_FOUND)
  269. pkg_check_modules(NCURSES QUIET ${NCURSES_LIBRARY_NAME})
  270. set(CURSES_CFLAGS ${NCURSES_CFLAGS_OTHER})
  271. endif()
  272. include(FindPackageHandleStandardArgs)
  273. find_package_handle_standard_args(Curses DEFAULT_MSG
  274. CURSES_LIBRARY CURSES_INCLUDE_PATH)
  275. mark_as_advanced(
  276. CURSES_INCLUDE_PATH
  277. CURSES_CURSES_LIBRARY
  278. CURSES_NCURSES_LIBRARY
  279. CURSES_FORM_LIBRARY
  280. )