FindCurses.cmake 7.14 KB
Newer Older
1 2 3 4
#.rst:
# FindCurses
# ----------
#
5
# Find the curses or ncurses include file and library.
6
#
7 8
# Result Variables
# ^^^^^^^^^^^^^^^^
9
#
10
# This module defines the following variables:
11
#
12 13 14 15 16 17 18 19 20 21 22 23 24 25
# ``CURSES_FOUND``
#   True if Curses is found.
# ``CURSES_INCLUDE_DIRS``
#   The include directories needed to use Curses.
# ``CURSES_LIBRARIES``
#   The libraries needed to use Curses.
# ``CURSES_HAVE_CURSES_H``
#   True if curses.h is available.
# ``CURSES_HAVE_NCURSES_H``
#   True if ncurses.h is available.
# ``CURSES_HAVE_NCURSES_NCURSES_H``
#   True if ``ncurses/ncurses.h`` is available.
# ``CURSES_HAVE_NCURSES_CURSES_H``
#   True if ``ncurses/curses.h`` is available.
26
#
27 28
# Set ``CURSES_NEED_NCURSES`` to ``TRUE`` before the
# ``find_package(Curses)`` call if NCurses functionality is required.
29
#
30 31
# Backward Compatibility
# ^^^^^^^^^^^^^^^^^^^^^^
32
#
33
# The following variable are provided for backward compatibility:
34
#
35 36 37 38
# ``CURSES_INCLUDE_DIR``
#   Path to Curses include.  Use ``CURSES_INCLUDE_DIRS`` instead.
# ``CURSES_LIBRARY``
#   Path to Curses library.  Use ``CURSES_LIBRARIES`` instead.
39

40
#=============================================================================
41
# Copyright 2001-2014 Kitware, Inc.
42 43 44 45 46 47 48 49
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
50
# (To distribute this file outside of CMake, substitute the full
51 52
#  License text for the above reference.)

53 54
include(${CMAKE_CURRENT_LIST_DIR}/CheckLibraryExists.cmake)

55
find_library(CURSES_CURSES_LIBRARY NAMES curses )
56

57 58
find_library(CURSES_NCURSES_LIBRARY NAMES ncurses )
set(CURSES_USE_NCURSES FALSE)
59

60
if(CURSES_NCURSES_LIBRARY  AND ((NOT CURSES_CURSES_LIBRARY) OR CURSES_NEED_NCURSES))
61
  set(CURSES_USE_NCURSES TRUE)
62
endif()
63 64 65 66
# http://cygwin.com/ml/cygwin-announce/2010-01/msg00002.html
# cygwin ncurses stopped providing curses.h symlinks see above
# message.  Cygwin is an ncurses package, so force ncurses on
# cygwin if the curses.h is missing
67 68 69 70 71
if(CYGWIN)
  if(NOT EXISTS /usr/include/curses.h)
    set(CURSES_USE_NCURSES TRUE)
  endif()
endif()
72 73 74 75 76 77 78 79 80 81 82


# Not sure the logic is correct here.
# If NCurses is required, use the function wsyncup() to check if the library
# has NCurses functionality (at least this is where it breaks on NetBSD).
# If wsyncup is in curses, use this one.
# If not, try to find ncurses and check if this has the symbol.
# Once the ncurses library is found, search the ncurses.h header first, but
# some web pages also say that even with ncurses there is not always a ncurses.h:
# http://osdir.com/ml/gnome.apps.mc.devel/2002-06/msg00029.html
# 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 87 88
  include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  cmake_push_check_state()
  set(CMAKE_REQUIRED_QUIET ${Curses_FIND_QUIETLY})
89
  CHECK_LIBRARY_EXISTS("${CURSES_CURSES_LIBRARY}"
90
    wsyncup "" CURSES_CURSES_HAS_WSYNCUP)
91

92
  if(CURSES_NCURSES_LIBRARY  AND NOT  CURSES_CURSES_HAS_WSYNCUP)
93
    CHECK_LIBRARY_EXISTS("${CURSES_NCURSES_LIBRARY}"
94
      wsyncup "" CURSES_NCURSES_HAS_WSYNCUP)
95 96
    if( CURSES_NCURSES_HAS_WSYNCUP)
      set(CURSES_USE_NCURSES TRUE)
97 98
    endif()
  endif()
99
  cmake_pop_check_state()
100

101
endif()
102

103
if(CURSES_USE_NCURSES)
104 105
  get_filename_component(_cursesLibDir "${CURSES_NCURSES_LIBRARY}" PATH)
  get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)
106

107 108 109 110 111 112 113
  # Use CURSES_NCURSES_INCLUDE_PATH if set, for compatibility.
  if(CURSES_NCURSES_INCLUDE_PATH)
    find_path(CURSES_INCLUDE_PATH
      NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
      PATHS ${CURSES_NCURSES_INCLUDE_PATH}
      NO_DEFAULT_PATH
      )
114
  endif()
115 116 117 118 119 120 121 122 123

  find_path(CURSES_INCLUDE_PATH
    NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
    HINTS "${_cursesParentDir}/include"
    )

  # Previous versions of FindCurses provided these values.
  if(NOT DEFINED CURSES_LIBRARY)
    set(CURSES_LIBRARY "${CURSES_NCURSES_LIBRARY}")
124
  endif()
125

126 127 128 129 130 131
  CHECK_LIBRARY_EXISTS("${CURSES_NCURSES_LIBRARY}"
    cbreak "" CURSES_NCURSES_HAS_CBREAK)
  if(NOT CURSES_NCURSES_HAS_CBREAK)
    find_library(CURSES_EXTRA_LIBRARY tinfo HINTS "${_cursesLibDir}")
    find_library(CURSES_EXTRA_LIBRARY tinfo )
  endif()
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
else()
  get_filename_component(_cursesLibDir "${CURSES_CURSES_LIBRARY}" PATH)
  get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)

  find_path(CURSES_INCLUDE_PATH
    NAMES curses.h
    HINTS "${_cursesParentDir}/include"
    )

  # Previous versions of FindCurses provided these values.
  if(NOT DEFINED CURSES_CURSES_H_PATH)
    set(CURSES_CURSES_H_PATH "${CURSES_INCLUDE_PATH}")
  endif()
  if(NOT DEFINED CURSES_LIBRARY)
    set(CURSES_LIBRARY "${CURSES_CURSES_LIBRARY}")
  endif()
endif()

# Report whether each possible header name exists in the include directory.
if(NOT DEFINED CURSES_HAVE_NCURSES_NCURSES_H)
  if(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
    set(CURSES_HAVE_NCURSES_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
  else()
    set(CURSES_HAVE_NCURSES_NCURSES_H "CURSES_HAVE_NCURSES_NCURSES_H-NOTFOUND")
  endif()
endif()
if(NOT DEFINED CURSES_HAVE_NCURSES_CURSES_H)
  if(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
    set(CURSES_HAVE_NCURSES_CURSES_H "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
  else()
    set(CURSES_HAVE_NCURSES_CURSES_H "CURSES_HAVE_NCURSES_CURSES_H-NOTFOUND")
  endif()
endif()
if(NOT DEFINED CURSES_HAVE_NCURSES_H)
  if(EXISTS "${CURSES_INCLUDE_PATH}/ncurses.h")
    set(CURSES_HAVE_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses.h")
  else()
    set(CURSES_HAVE_NCURSES_H "CURSES_HAVE_NCURSES_H-NOTFOUND")
  endif()
endif()
if(NOT DEFINED CURSES_HAVE_CURSES_H)
  if(EXISTS "${CURSES_INCLUDE_PATH}/curses.h")
    set(CURSES_HAVE_CURSES_H "${CURSES_INCLUDE_PATH}/curses.h")
  else()
    set(CURSES_HAVE_CURSES_H "CURSES_HAVE_CURSES_H-NOTFOUND")
  endif()
178
endif()
179

180 181
find_library(CURSES_FORM_LIBRARY form HINTS "${_cursesLibDir}")
find_library(CURSES_FORM_LIBRARY form )
Berk Geveci's avatar
Berk Geveci committed
182

183 184 185 186
# Previous versions of FindCurses provided these values.
if(NOT DEFINED FORM_LIBRARY)
  set(FORM_LIBRARY "${CURSES_FORM_LIBRARY}")
endif()
Alexander Neundorf's avatar
Alexander Neundorf committed
187

Bill Hoffman's avatar
Bill Hoffman committed
188
# Need to provide the *_LIBRARIES
189
set(CURSES_LIBRARIES ${CURSES_LIBRARY})
Bill Hoffman's avatar
Bill Hoffman committed
190

191 192
if(CURSES_EXTRA_LIBRARY)
  set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_EXTRA_LIBRARY})
193
endif()
Bill Hoffman's avatar
Bill Hoffman committed
194

195 196
if(CURSES_FORM_LIBRARY)
  set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_FORM_LIBRARY})
197
endif()
Bill Hoffman's avatar
Bill Hoffman committed
198

199 200 201
# Provide the *_INCLUDE_DIRS result.
set(CURSES_INCLUDE_DIRS ${CURSES_INCLUDE_PATH})
set(CURSES_INCLUDE_DIR ${CURSES_INCLUDE_PATH}) # compatibility
Bill Hoffman's avatar
Bill Hoffman committed
202

203
# handle the QUIETLY and REQUIRED arguments and set CURSES_FOUND to TRUE if
204
# all listed variables are TRUE
205
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
206 207
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Curses DEFAULT_MSG
  CURSES_LIBRARY CURSES_INCLUDE_PATH)
208

209
mark_as_advanced(
Bill Hoffman's avatar
Bill Hoffman committed
210
  CURSES_INCLUDE_PATH
Alexander Neundorf's avatar
Alexander Neundorf committed
211
  CURSES_CURSES_LIBRARY
212
  CURSES_NCURSES_LIBRARY
Bill Hoffman's avatar
Bill Hoffman committed
213 214
  CURSES_EXTRA_LIBRARY
  )