diff --git a/cmake/find_readline_edit.cmake b/cmake/find_readline_edit.cmake index d23910155db..3bf70c6a4a0 100644 --- a/cmake/find_readline_edit.cmake +++ b/cmake/find_readline_edit.cmake @@ -1,44 +1,68 @@ -if (NOT READLINE_PATHS) - set (READLINE_PATHS "/usr/local/opt/readline/lib") +set (READLINE_PATHS "/usr/local/opt/readline/lib") +# First try find custom lib for macos users (default lib without history support) +if (USE_STATIC_LIBRARIES) + find_library (READLINE_LIB NAMES libreadline.a PATHS ${READLINE_PATHS} NO_DEFAULT_PATH) +else () + find_library (READLINE_LIB NAMES readline PATHS ${READLINE_PATHS} NO_DEFAULT_PATH) +endif () +if (NOT READLINE_LIB) if (USE_STATIC_LIBRARIES) find_library (READLINE_LIB NAMES libreadline.a PATHS ${READLINE_PATHS}) else () find_library (READLINE_LIB NAMES readline PATHS ${READLINE_PATHS}) endif () - - if (USE_STATIC_LIBRARIES) - find_library (TERMCAP_LIB NAMES libtermcap.a termcap) - else () - find_library (TERMCAP_LIB NAMES termcap) - endif () - - if (USE_STATIC_LIBRARIES) - find_library (EDIT_LIB NAMES libedit.a) - else () - find_library (EDIT_LIB NAMES edit) - endif () - - set(READLINE_INCLUDE_PATHS "/usr/local/opt/readline/include") - if (READLINE_LIB) - find_path (READLINE_INCLUDE_DIR NAMES readline/readline.h PATHS ${READLINE_INCLUDE_PATHS}) - add_definitions (-D USE_READLINE) - set (LINE_EDITING_LIBS ${READLINE_LIB} ${TERMCAP_LIB}) - message (STATUS "Using line editing libraries (readline): ${READLINE_INCLUDE_DIR} : ${LINE_EDITING_LIBS}") - elseif (EDIT_LIB) - if (USE_STATIC_LIBRARIES) - find_library (CURSES_LIB NAMES libcurses.a) - else () - find_library (CURSES_LIB NAMES curses) - endif () - add_definitions (-D USE_LIBEDIT) - find_path (READLINE_INCLUDE_DIR NAMES editline/readline.h PATHS ${READLINE_INCLUDE_PATHS}) - set (LINE_EDITING_LIBS ${EDIT_LIB} ${CURSES_LIB} ${TERMCAP_LIB}) - message (STATUS "Using line editing libraries (edit): ${READLINE_INCLUDE_DIR} : ${LINE_EDITING_LIBS}") - else () - message (STATUS "Not using any library for line editing.") - endif () - if (READLINE_INCLUDE_DIR) - include_directories (${READLINE_INCLUDE_DIR}) - endif () - endif () + +if (USE_STATIC_LIBRARIES) + find_library (TERMCAP_LIB NAMES libtermcap.a termcap) +else () + find_library (TERMCAP_LIB NAMES termcap) +endif () + +if (USE_STATIC_LIBRARIES) + find_library (EDIT_LIB NAMES libedit.a) +else () + find_library (EDIT_LIB NAMES edit) +endif () + +set(READLINE_INCLUDE_PATHS "/usr/local/opt/readline/include") +if (READLINE_LIB) + find_path (READLINE_INCLUDE_DIR NAMES readline/readline.h PATHS ${READLINE_INCLUDE_PATHS} NO_DEFAULT_PATH) + if (NOT READLINE_INCLUDE_DIR) + find_path (READLINE_INCLUDE_DIR NAMES readline/readline.h PATHS ${READLINE_INCLUDE_PATHS}) + endif () + set (USE_READLINE 1) + set (LINE_EDITING_LIBS ${READLINE_LIB} ${TERMCAP_LIB}) + message (STATUS "Using line editing libraries (readline): ${READLINE_INCLUDE_DIR} : ${LINE_EDITING_LIBS}") +elseif (EDIT_LIB) + if (USE_STATIC_LIBRARIES) + find_library (CURSES_LIB NAMES libcurses.a) + else () + find_library (CURSES_LIB NAMES curses) + endif () + set(USE_LIBEDIT 1) + find_path (READLINE_INCLUDE_DIR NAMES editline/readline.h PATHS ${READLINE_INCLUDE_PATHS}) + set (LINE_EDITING_LIBS ${EDIT_LIB} ${CURSES_LIB} ${TERMCAP_LIB}) + message (STATUS "Using line editing libraries (edit): ${READLINE_INCLUDE_DIR} : ${LINE_EDITING_LIBS}") +else () + message (STATUS "Not using any library for line editing.") +endif () +if (READLINE_INCLUDE_DIR) + include_directories (${READLINE_INCLUDE_DIR}) +endif () + +include (CheckCXXSourceRuns) + +set (CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${LINE_EDITING_LIBS}) +check_cxx_source_runs(" + #include + #include + int main() { + add_history(nullptr); + append_history(1,nullptr); + } +" HAVE_READLINE_HISTORY) + +#if (HAVE_READLINE_HISTORY) +# add_definitions (-D HAVE_READLINE_HISTORY) +#endif () diff --git a/dbms/src/Client/Client.cpp b/dbms/src/Client/Client.cpp index f6330b4c57e..25186aba610 100644 --- a/dbms/src/Client/Client.cpp +++ b/dbms/src/Client/Client.cpp @@ -60,6 +60,7 @@ #include +#include /// Different line editing libraries can be used depending on the environment. #ifdef USE_READLINE @@ -497,7 +498,7 @@ private: std::replace(logged_query.begin(), logged_query.end(), '\n', ' '); add_history(logged_query.c_str()); -#ifdef USE_READLINE +#if USE_READLINE && HAVE_READLINE_HISTORY if (!history_file.empty() && append_history(1, history_file.c_str())) throwFromErrno("Cannot append history to file " + history_file, ErrorCodes::CANNOT_APPEND_HISTORY); #endif diff --git a/libs/libcommon/include/common/config_common.h.in b/libs/libcommon/include/common/config_common.h.in index a743aeb05b0..ec8e2508c1c 100644 --- a/libs/libcommon/include/common/config_common.h.in +++ b/libs/libcommon/include/common/config_common.h.in @@ -4,3 +4,7 @@ #cmakedefine01 APPLE_SIERRA_OR_NEWER #cmakedefine01 ENABLE_LIBTCMALLOC + +#cmakedefine01 USE_READLINE +#cmakedefine01 USE_LIBEDIT +#cmakedefine01 HAVE_READLINE_HISTORY