FindCurses.cmake 9.0 KB

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