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