1# Attempts to discover terminfo library with a linkable setupterm function.
2#
3# Example usage:
4#
5# find_package(Terminfo)
6#
7# If successful, the following variables will be defined:
8# Terminfo_FOUND
9# Terminfo_LIBRARIES
10#
11# Additionally, the following import target will be defined:
12# Terminfo::terminfo
13
14find_library(Terminfo_LIBRARIES NAMES terminfo tinfo curses ncurses ncursesw)
15
16if(Terminfo_LIBRARIES)
17  include(CMakePushCheckState)
18  cmake_push_check_state()
19  list(APPEND CMAKE_REQUIRED_LIBRARIES ${Terminfo_LIBRARIES})
20  set(Terminfo_LINKABLE_SRC [=[
21    #ifdef __cplusplus
22    extern "C" {
23    #endif
24    int setupterm(char *term, int filedes, int *errret);
25    #ifdef __cplusplus
26    }
27    #endif
28    int main(void) { return setupterm(0, 0, 0); }
29    ]=])
30  if(DEFINED CMAKE_C_COMPILER)
31    include(CheckCSourceCompiles)
32    check_c_source_compiles("${Terminfo_LINKABLE_SRC}" Terminfo_LINKABLE)
33  else()
34    include(CheckCXXSourceCompiles)
35    check_cxx_source_compiles("${Terminfo_LINKABLE_SRC}" Terminfo_LINKABLE)
36  endif()
37  cmake_pop_check_state()
38endif()
39
40include(FindPackageHandleStandardArgs)
41find_package_handle_standard_args(Terminfo
42                                  FOUND_VAR
43                                    Terminfo_FOUND
44                                  REQUIRED_VARS
45                                    Terminfo_LIBRARIES
46                                    Terminfo_LINKABLE)
47mark_as_advanced(Terminfo_LIBRARIES
48                 Terminfo_LINKABLE)
49
50if(Terminfo_FOUND)
51  if(NOT TARGET Terminfo::terminfo)
52    add_library(Terminfo::terminfo UNKNOWN IMPORTED)
53    set_target_properties(Terminfo::terminfo PROPERTIES IMPORTED_LOCATION "${Terminfo_LIBRARIES}")
54  endif()
55endif()
56