From 35668d5b9f05b11697719792642da5c99c0314ac Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Fri, 24 Mar 2017 09:50:52 +0100 Subject: [PATCH] Make GNU Readline dependency optional Reviewers: florijan, buda Reviewed By: buda Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D171 --- CMakeLists.txt | 12 ++++++++++-- cmake/FindReadline.cmake | 27 +++++++++++++++++++++++++++ src/query/console.cpp | 13 +++++++++++++ tests/manual/CMakeLists.txt | 14 +++++++++++--- 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 cmake/FindReadline.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c88bae3a8..a95637a6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,13 @@ add_custom_target(clean_all # threading find_package(Threads REQUIRED) -# find_package(readline REQUIRED) +# optional readline +find_package(Readline REQUIRED) +if (READLINE_FOUND) + include_directories(SYSTEM ${READLINE_INCLUDE_DIR}) + add_definitions(-DHAS_READLINE) +endif() + # ----------------------------------------------------------------------------- # c++14 @@ -424,7 +430,9 @@ if (MEMGRAPH) target_link_libraries(${MEMGRAPH_BUILD_NAME} yaml-cpp) target_link_libraries(${MEMGRAPH_BUILD_NAME} antlr_opencypher_parser_lib) target_link_libraries(${MEMGRAPH_BUILD_NAME} dl) - target_link_libraries(${MEMGRAPH_BUILD_NAME} readline) + if (READLINE_FOUND) + target_link_libraries(${MEMGRAPH_BUILD_NAME} ${READLINE_LIBRARY}) + endif() endif() # utility target to copy hardcoded queries diff --git a/cmake/FindReadline.cmake b/cmake/FindReadline.cmake new file mode 100644 index 000000000..a2c93348f --- /dev/null +++ b/cmake/FindReadline.cmake @@ -0,0 +1,27 @@ +# Find the GNU Readline library. +# This module plugs into CMake's `find_package` so the example usage is: +# `find_package(Readline REQUIRED)` +# Options to `find_package` are as documented in CMake documentation. +# READLINE_LIBRARY will be a path to the library. +# READLINE_INCLUDE_DIR will be a path to the include directory. +# READLINE_FOUND will be TRUE if the library is found. +if (READLINE_LIBRARY AND READLINE_INCLUDE_DIR) + set(READLINE_FOUND TRUE) +else() + find_library(READLINE_LIBRARY readline) + find_path(READLINE_INCLUDE_DIR readline/readline.h) + if (READLINE_LIBRARY AND READLINE_INCLUDE_DIR) + set(READLINE_FOUND TRUE) + if (NOT READLINE_FIND_QUIETLY) + message(STATUS "Found Readline: ${READLINE_LIBRARY} ${READLINE_INCLUDE_DIR}") + endif() + else() + set(READLINE_FOUND FALSE) + if (READLINE_FIND_REQUIRED) + message(FATAL_ERROR "Could not find Readline") + elseif (NOT READLINE_FIND_QUIETLY) + message(STATUS "Could not find Readline") + endif() + endif() + mark_as_advanced(READLINE_LIBRARY READLINE_INCLUDE_DIR) +endif() diff --git a/src/query/console.cpp b/src/query/console.cpp index 92838a52d..198d35129 100644 --- a/src/query/console.cpp +++ b/src/query/console.cpp @@ -12,6 +12,8 @@ #include "query/exceptions.hpp" #include "query/interpreter.hpp" +#ifdef HAS_READLINE + #include "readline/history.h" #include "readline/readline.h" @@ -34,6 +36,17 @@ std::string ReadLine(const char *prompt) { return r_val; } +#else + +std::string ReadLine(const char *prompt) { + std::cout << prompt; + std::string line; + std::getline(std::cin, line); + return line; +} + +#endif // HAS_READLINE + /** * Helper function that outputs a collection of items to * the given stream, separating them with the given delimiter. diff --git a/tests/manual/CMakeLists.txt b/tests/manual/CMakeLists.txt index d07669988..b1417422f 100644 --- a/tests/manual/CMakeLists.txt +++ b/tests/manual/CMakeLists.txt @@ -1,5 +1,11 @@ find_package(Threads REQUIRED) -# find_package(readline REQUIRED) + +# optional readline +find_package(Readline) +if (READLINE_FOUND) + include_directories(SYSTEM ${READLINE_INCLUDE_DIR}) + add_definitions(-DHAS_READLINE) +endif() # set current directory name as a test type get_filename_component(test_type ${CMAKE_CURRENT_SOURCE_DIR} NAME) @@ -38,10 +44,12 @@ foreach(test_cpp ${test_type_cpps}) # yaml parser lib target_link_libraries(${target_name} yaml-cpp) # antlr - target_link_libraries(${target_name} antlr_opencypher_parser_lib) + target_link_libraries(${target_name} antlr_opencypher_parser_lib) # dynamic lib target_link_libraries(${target_name} dl) # readline lib - target_link_libraries(${target_name} readline) + if (READLINE_FOUND) + target_link_libraries(${target_name} ${READLINE_LIBRARY}) + endif() endforeach()