debian: fix build-deps for focal
[amule.git] / cmake / FindReadline.cmake
blobb13fb79c8cca61691520d124f693f8cfb723dd41
1 include (CheckIncludeFile)
2 include (CheckFunctionExists)
3 include (FindPackageHandleStandardArgs)
5 macro (_ADD_LIBRARY_IF_EXISTS _LST _LIB)
6         string (TOUPPER "${_LIB}" _LIBVAR)
7         find_library (${_LIBVAR}_LIB "${_LIB}")
9         if (${_LIBVAR}_LIB)
10                 list (APPEND ${_LST} ${${_LIBVAR}_LIB})
11         endif (${_LIBVAR}_LIB)
13         unset (${_LIBVAR}_LIB CACHE)
14 endmacro()
16 # Modified version of the library CHECK_FUNCTION_EXISTS
17 # This version will not produce any output, and the result variable is only
18 # set when the function is found
19 macro (_CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
20         unset (_RESULT_VAR)
21         try_compile (_RESULT_VAR
22                 ${CMAKE_BINARY_DIR}
23                 ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
24                 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING="-DCHECK_FUNCTION_EXISTS=${FUNCTION}"
25                         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}"
26                 OUTPUT_VARIABLE OUTPUT)
28         if (_RESULT_VAR)
29                 set (${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
30                 file (APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
31                         "Determining if the function ${FUNCTION} exists passed with the following output:\n${OUTPUT}\n\n")
32         else()
33                 file (APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
34                         "Determining if the function ${FUNCTION} exists failed with the following output:\n${OUTPUT}\n\n")
35         endif()
36 endmacro()
38 if (HAVE_LIBREADLINE MATCHES ^HAVE_LIBREADLINE$)
39         foreach (_maybe_readline_lib "readline" "edit" "editline")
40                 _ADD_LIBRARY_IF_EXISTS (_readline_libs ${_maybe_readline_lib})
41         endforeach()
43         foreach (_maybe_termcap_lib "termcap" "curses" "ncurses")
44                 _ADD_LIBRARY_IF_EXISTS (_termcap_libs ${_maybe_termcap_lib})
45         endforeach()
47         message (STATUS "Looking for readline")
49         foreach (_readline_lib IN LISTS _readline_libs)
50                 set (CMAKE_REQUIRED_LIBRARIES "${_readline_lib}")
51                 _CHECK_FUNCTION_EXISTS (readline HAVE_LIBREADLINE)
53                 if (HAVE_LIBREADLINE)
54                         break()
55                 endif()
57                 foreach (_termcap_lib IN LISTS _termcap_libs)
58                         set (CMAKE_REQUIRED_LIBRARIES "${_readline_lib}" "${_termcap_lib}")
59                         _CHECK_FUNCTION_EXISTS (readline HAVE_LIBREADLINE)
61                         if (HAVE_LIBREADLINE)
62                                 break()
63                         endif()
64                 endforeach()
66                 if (HAVE_LIBREADLINE)
67                         break()
68                 endif()
69         endforeach()
71         if (HAVE_LIBREADLINE)
72                 message (STATUS "Looking for readline - found")
73         else (HAVE_LIBREADLINE)
74                 message (STATUS "Looking for readline - not found")
75         endif (HAVE_LIBREADLINE)
76 endif()
78 if (HAVE_LIBREADLINE)
79         set (READLINE_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}"
80                 CACHE STRING "Required link libraries for the readline library"
81         )
83         check_function_exists (add_history HAVE_READLINE_HISTORY)
84         set (CMAKE_REQUIRED_LIBRARIES "")
85 endif()
87 check_include_file (stdio.h HAVE_STDIO_H)
89 # Stripped down and output modified version of the library CHECK_INCLUDE_FILES
90 macro (_CHECK_INCLUDE_FILES INCLUDE VARIABLE)
91         if (${VARIABLE} MATCHES ^${VARIABLE}$)
92                 set (CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
94                 foreach (FILE ${INCLUDE})
95                         set (CMAKE_CONFIGURABLE_FILE_CONTENT "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
96                         set (_LAST_INCLUDE "${FILE}")
97                 endforeach()
99                 set (CMAKE_CONFIGURABLE_FILE_CONTENT "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n\nint main(){return 0;}\n")
100                 configure_file ("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
101                         "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c" @ONLY IMMEDIATE
102                 )
104                 message (STATUS "Looking for ${_LAST_INCLUDE}")
105                 try_compile (${VARIABLE}
106                         ${CMAKE_BINARY_DIR}
107                         ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c
108                         OUTPUT_VARIABLE OUTPUT
109                 )
111                 if (${VARIABLE})
112                         message (STATUS "Looking for ${_LAST_INCLUDE} - found")
113                         set (${VARIABLE} 1 CACHE INTERNAL "Have ${_LAST_INCLUDE}")
114                         file (APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
115                                 "Determining if ${_LAST_INCLUDE} exist passed with the following output:\n${OUTPUT}\n\n"
116                         )
117                 else()
118                         message (STATUS "Looking for ${_LAST_INCLUDE} - not found.")
119                         set (${VARIABLE} 0 CACHE INTERNAL "Have ${_LAST_INCLUDE}")
120                         file (APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
121                                 "Determining if ${_LAST_INCLUDE} exist failed with the following output:\n${OUTPUT}\n"
122                                 "Source:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n"
123                         )
124                 endif()
125         endif()
126 endmacro()
128 if (HAVE_STDIO_H)
129         _check_include_files ("stdio.h;readline.h" HAVE_READLINE_H)
131         if (HAVE_READLINE_H)
132                 _check_include_files ("stdio.h;readline.h;history.h" HAVE_HISTORY_H)
133         else()
134                 _check_include_files ("stdio.h;readline/readline.h" HAVE_READLINE_READLINE_H)
136                 if (HAVE_READLINE_READLINE_H)
137                         _check_include_files ("stdio.h;readline/readline.h;readline/history.h" HAVE_READLINE_HISTORY_H)
139                         if (NOT HAVE_READLINE_HISTORY_H)
140                                 _check_include_files ("stdio.h;readline/readline.h;history.h" HAVE_HISTORY_H)
141                         endif(NOT HAVE_READLINE_HISTORY_H)
142                 endif()
143         endif()
144 endif()
146 find_package_handle_standard_args (readline DEFAULT_MSG READLINE_LIBRARIES)