summaryrefslogtreecommitdiff
path: root/cmake/readline.cmake
blob: 7661f12cce21b03f25bbd103373af50d39843f4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright (C) 2009 Sun Microsystems, Inc
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 

MACRO(SET_WITH_BUNDLED_READLINE option)
  IF(option)
   SET(not_option OFF)
  ELSE()
   SET(not_option ON)
  ENDIF()
  SET(WITH_READLINE ${option} CACHE BOOL "Use bundled readline")
  SET(WITH_LIBEDIT ${not_option} CACHE BOOL "Use bundled libedit")
ENDMACRO()

MACRO (MYSQL_CHECK_MULTIBYTE)
  CHECK_INCLUDE_FILE(wctype.h HAVE_WCTYPE_H)
  CHECK_INCLUDE_FILE(wchar.h HAVE_WCHAR_H)
  IF(HAVE_WCHAR_H)
    SET(CMAKE_EXTRA_INCLUDE_FILES wchar.h)
    CHECK_TYPE_SIZE(mbstate_t SIZEOF_MBSTATE_T)
    SET(CMAKE_EXTRA_INCLUDE_FILES)
    IF(SIZEOF_MBSTATE_T)
      SET(HAVE_MBSTATE_T 1)
    ENDIF()
  ENDIF()

  CHECK_C_SOURCE_COMPILES("
  #include <langinfo.h>
  int main(int ac, char **av)
  {
    char *cs = nl_langinfo(CODESET);
    return 0;
  }"
  HAVE_LANGINFO_CODESET)
  
  CHECK_FUNCTION_EXISTS(mbrlen HAVE_MBRLEN)
  CHECK_FUNCTION_EXISTS(mbscmp HAVE_MBSCMP)
  CHECK_FUNCTION_EXISTS(mbsrtowcs HAVE_MBSRTOWCS)
  CHECK_FUNCTION_EXISTS(wcrtomb HAVE_WCRTOMB)
  CHECK_FUNCTION_EXISTS(mbrtowc HAVE_MBRTOWC)
  CHECK_FUNCTION_EXISTS(wcscoll HAVE_WCSCOLL)
  CHECK_FUNCTION_EXISTS(wcsdup HAVE_WCSDUP)
  CHECK_FUNCTION_EXISTS(wcwidth HAVE_WCWIDTH)
  CHECK_FUNCTION_EXISTS(wctype HAVE_WCTYPE)
  CHECK_FUNCTION_EXISTS(iswlower HAVE_ISWLOWER)
  CHECK_FUNCTION_EXISTS(iswupper HAVE_ISWUPPER)
  CHECK_FUNCTION_EXISTS(towlower HAVE_TOWLOWER)
  CHECK_FUNCTION_EXISTS(towupper HAVE_TOWUPPER)
  CHECK_FUNCTION_EXISTS(iswctype HAVE_ISWCTYPE)

  SET(CMAKE_EXTRA_INCLUDE_FILES wchar.h)
  CHECK_TYPE_SIZE(wchar_t SIZEOF_WCHAR_T)
  IF(SIZEOF_WCHAR_T)
    SET(HAVE_WCHAR_T 1)
  ENDIF()

  SET(CMAKE_EXTRA_INCLUDE_FILES wctype.h)
  CHECK_TYPE_SIZE(wctype_t SIZEOF_WCTYPE_T)
  IF(SIZEOF_WCTYPE_T)
    SET(HAVE_WCTYPE_T 1)
  ENDIF()
  CHECK_TYPE_SIZE(wint_t SIZEOF_WINT_T)
  IF(SIZEOF_WINT_T)
    SET(HAVE_WINT_T 1)
  ENDIF()
  SET(CMAKE_EXTRA_INCLUDE_FILES)

ENDMACRO()

MACRO (FIND_CURSES)
 INCLUDE (FindCurses)
 MARK_AS_ADVANCED(CURSES_CURSES_H_PATH CURSES_FORM_LIBRARY CURSES_HAVE_CURSES_H)
 IF(NOT CURSES_FOUND)
   MESSAGE(FATAL_ERROR "curses library not found")
 ENDIF()

 IF(CURSES_HAVE_CURSES_H)
   SET(HAVE_CURSES_H 1 CACHE INTERNAL "")
 ELSEIF(CURSES_HAVE_NCURSES_H)
   SET(HAVE_NCURSES_H 1 CACHE INTERNAL "")
 ENDIF()
 IF(CMAKE_SYSTEM_NAME MATCHES "HP")
   # CMake uses full path to library /lib/libcurses.sl 
   # On Itanium, it results into architecture mismatch+
   # the library is for  PA-RISC
   SET(CURSES_LIBRARY "curses" CACHE INTERNAL "" FORCE)
   SET(CURSES_CURSES_LIBRARY "curses" CACHE INTERNAL "" FORCE)
 ENDIF()
ENDMACRO()

MACRO (MYSQL_USE_BUNDLED_READLINE)
  SET_WITH_BUNDLED_READLINE(ON)
  SET(USE_NEW_READLINE_INTERFACE 1)
  SET(READLINE_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/cmd-line-utils)
  SET(READLINE_LIBRARY readline)
  FIND_CURSES()
  ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/cmd-line-utils/readline)
ENDMACRO()

MACRO (MYSQL_USE_BUNDLED_LIBEDIT)
  SET_WITH_BUNDLED_READLINE(OFF)
  SET(USE_LIBEDIT_INTERFACE 1 CACHE INTERNAL "")
  SET(HAVE_HIST_ENTRY 1 CACHE INTERNAL "")
  SET(READLINE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/cmd-line-utils/libedit")
  SET(READLINE_LIBRARY edit)
  FIND_CURSES()
  ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/cmd-line-utils/libedit)
ENDMACRO()

MACRO (MYSQL_FIND_SYSTEM_READLINE name)
  FIND_PATH(SYSTEM_READLINE_INCLUDE_DIR readline/readline.h )
  FIND_LIBRARY(SYSTEM_READLINE_LIBRARY NAMES ${name})
  MARK_AS_ADVANCED(SYSTEM_READLINE_INCLUDE_DIR  SYSTEM_READLINE_LIBRARY)

  INCLUDE(CheckCXXSourceCompiles)
  SET(CMAKE_REQUIRES_LIBRARIES ${SYSTEM_READLINE_LIBRARY})

  IF(SYSTEM_READLINE_LIBRARY AND SYSTEM_READLINE_INCLUDE_DIR)
    SET(SYSTEM_READLINE_FOUND 1)
    SET(CMAKE_REQUIRED_LIBRARIES ${SYSTEM_READLINE_LIBRARY})
    CHECK_CXX_SOURCE_COMPILES("
    #include <stdio.h>
    #include <readline/readline.h>
    int main(int argc, char **argv)
    {
       HIST_ENTRY entry;
       return 0;
    }"
    HAVE_HIST_ENTRY)

    CHECK_CXX_SOURCE_COMPILES("
    #include <stdio.h>
    #include <readline/readline.h>
    int main(int argc, char **argv)
    {
      char res= *(*rl_completion_entry_function)(0,0);
      completion_matches(0,0);
    }"
    USE_LIBEDIT_INTERFACE)


    CHECK_CXX_SOURCE_COMPILES("
    #include <stdio.h>
    #include <readline/readline.h>
    int main(int argc, char **argv)
    {
      rl_completion_func_t *func1= (rl_completion_func_t*)0;
      rl_compentry_func_t *func2= (rl_compentry_func_t*)0;
    }"
    USE_NEW_READLINE_INTERFACE)
    
    IF(USE_LIBEDIT_INTERFACE  OR USE_NEW_READLINE_INTERFACE)
      SET(READLINE_LIBRARY ${SYSTEM_READLINE_LIBRARY})
      SET(READLINE_INCLUDE_DIR ${SYSTEM_READLINE_INCLUDE_DIR})
      SET(READLINE_FOUND 1)
    ENDIF()
  ENDIF()
ENDMACRO()

MACRO (MYSQL_CHECK_READLINE)
  IF (NOT WIN32)
    MYSQL_CHECK_MULTIBYTE()
    OPTION(WITH_READLINE "Use bundled readline" OFF)
    IF(NOT CYGWIN)	
      # Bundled libedit does not compile on cygwin
      OPTION(WITH_LIBEDIT  "Use bundled libedit" ON)
    ELSE()
      OPTION(WITH_LIBEDIT  "Use bundled libedit" OFF)
    ENDIF()

    IF(WITH_READLINE)
     MYSQL_USE_BUNDLED_READLINE()
    ELSEIF(WITH_LIBEDIT)
     MYSQL_USE_BUNDLED_LIBEDIT()
    ELSE()
      MYSQL_FIND_SYSTEM_READLINE(readline)
      IF(NOT READLINE_FOUND)
        MYSQL_FIND_SYSTEM_READLINE(edit)
        IF(NOT READLINE_FOUND)
          MESSAGE(FATAL_ERROR "Cannot find system readline or libedit libraries.Use WITH_READLINE or WITH_LIBEDIT")
        ENDIF()
      ENDIF()
    ENDIF()
  ENDIF(NOT WIN32)
ENDMACRO()