From ad5f1b428a830d82381befddd5edfb5fe3538636 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Sun, 19 Nov 2023 14:03:35 +0000 Subject: [PATCH] Fix build paths. Fix inspect to work with CppInterOp --- CMakeLists.txt | 26 ++++++++++--- include/xeus-cpp/xinterpreter.hpp | 2 +- src/xinspect.hpp | 63 +++++++++++++++++++++++-------- src/xinterpreter.cpp | 14 +++---- 4 files changed, 76 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37ef1da4..dc0bbbb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,16 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}") set(XEUS_CPP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) +if ("${CMAKE_INSTALL_INCLUDEDIR}" STREQUAL "") + set(CMAKE_INSTALL_INCLUDEDIR "include") +endif() +if ("${CMAKE_INSTALL_LIBDIR}" STREQUAL "") + set(CMAKE_INSTALL_LIBDIR "lib") +endif() +if ("${CMAKE_INSTALL_BINDIR}" STREQUAL "") + set(CMAKE_INSTALL_BINDIR "bin") +endif() + # Versionning # =========== @@ -198,7 +208,8 @@ include(CheckCXXCompilerFlag) string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) -set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib; ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") +set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") +list(REMOVE_DUPLICATES CMAKE_INSTALL_RPATH) macro(xeus_cpp_set_common_options target_name) if (MSVC) @@ -287,7 +298,7 @@ macro(xeus_cpp_create_target target_name linkage output_name) set(XEUS_CPP_XEUS_TARGET xeus-static) endif () - target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl) + target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl Python::Python) if (WIN32 OR CYGWIN) # elseif (APPLE) @@ -390,10 +401,10 @@ set(XEUS_CPP_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NA if (XEUS_CPP_BUILD_SHARED) install(TARGETS ${XEUS_CPP_TARGETS} EXPORT ${PROJECT_NAME}-targets - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp) + ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp) # Makes the project importable from the build directory export(EXPORT ${PROJECT_NAME}-targets @@ -401,6 +412,9 @@ if (XEUS_CPP_BUILD_SHARED) endif () # Install xcpp +if ("${CMAKE_VENV_PATH}" STREQUAL "") + set(CMAKE_VENV_PATH "${CMAKE_INSTALL_PREFIX}") +endif() if (XEUS_CPP_BUILD_EXECUTABLE) install(TARGETS xcpp RUNTIME DESTINATION ${CMAKE_VENV_PATH}/bin) diff --git a/include/xeus-cpp/xinterpreter.hpp b/include/xeus-cpp/xinterpreter.hpp index 92ccbbe8..c8cddca2 100644 --- a/include/xeus-cpp/xinterpreter.hpp +++ b/include/xeus-cpp/xinterpreter.hpp @@ -17,7 +17,7 @@ #include //#include -#include "clang/Interpreter/CppInterOp.h" +#include "clang/Interpreter/CppInterOp.h" // from CppInterOp package #include diff --git a/src/xinspect.hpp b/src/xinspect.hpp index f602fecf..75342ffa 100644 --- a/src/xinspect.hpp +++ b/src/xinspect.hpp @@ -9,10 +9,10 @@ #ifndef XEUS_CPP_INSPECT_HPP #define XEUS_CPP_INSPECT_HPP +#include #include #include - #include #include @@ -23,8 +23,11 @@ #include "xdemangle.hpp" #include "xparser.hpp" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/Path.h" +//#include "llvm/Support/FileSystem.h" +//#include "llvm/Support/Path.h" + +//#include "clang/Interpreter/CppInterOp.h" + namespace xcpp { @@ -81,7 +84,27 @@ namespace xcpp } }; - std::string find_type(const std::string& expression, clang::Interpreter& interpreter) + + std::string find_type_slow(const std::string& expression) { + static unsigned long long var_count = 0; + + if (auto type = Cpp::GetType(expression)) + return Cpp::GetQualifiedName(type); + + // Here we might need to deal with integral types such as 3.14. + + std::string id = "__Xeus_GetType_" + std::to_string(var_count++); + std::string using_clause = "using " + id + " = __typeof__(" + expression + ");\n"; + + if (!Cpp::Declare(using_clause.c_str(), /*silent=*/false)) { + Cpp::TCppScope_t lookup = Cpp::GetNamed(id, 0); + Cpp::TCppType_t lookup_ty = Cpp::GetTypeFromScope(lookup); + return Cpp::GetQualifiedCompleteName(Cpp::GetCanonicalType(lookup_ty)); + } + return ""; + } +/* + std::string find_type(const std::string& expression) { auto PTU = interpreter.Parse(expression + ";"); if (llvm::Error Err = PTU.takeError()) { @@ -89,19 +112,29 @@ namespace xcpp return ""; } - clang::Decl *D = *PTU->TUPart->decls_begin(); - if (!llvm::isa(D)) - return ""; + clang::Decl *D = *PTU->TUPart->decls_begin(); + if (!llvm::isa(D)) + return ""; - clang::Expr *E = llvm::cast(llvm::cast(D)->getStmt()); + clang::Expr *E = llvm::cast(llvm::cast(D)->getStmt()); - clang::QualType QT = E->getType(); + clang::QualType QT = E->getType(); return QT.getAsString(); } - +*/ static nl::json read_tagconfs(const char* path) { nl::json result = nl::json::array(); + for (auto &entry: std::filesystem::directory_iterator(path)) { + if (entry.path().extension() != ".json") + continue; + std::ifstream i(entry.path()); + nl::json json_entry; + i >> json_entry; + result.emplace_back(std::move(json_entry)); + } + return result; +/* std::error_code EC; for (llvm::sys::fs::directory_iterator File(path, EC), FileEnd; File != FileEnd && !EC; File.increment(EC)) { @@ -115,19 +148,19 @@ namespace xcpp result.emplace_back(std::move(entry)); } return result; +*/ } - std::pair is_inspect_request(const std::string code, std::regex re) + std::pair is_inspect_request(const std::string code, std::regex re) { std::smatch inspect; if (std::regex_search(code, inspect, re)){ return std::make_pair(true, inspect); } return std::make_pair(false, inspect); - } - void inspect(const std::string& code, nl::json& kernel_res, clang::Interpreter& interpreter) + void inspect(const std::string& code, nl::json& kernel_res) { std::string tagconf_dir = XCPP_TAGCONFS_DIR; std::string tagfiles_dir = XCPP_TAGFILES_DIR; @@ -150,7 +183,7 @@ namespace xcpp // Method or variable of class found (xxxx.yyyy) if (std::regex_search(to_inspect, method, std::regex(R"((.*)\.(\w*)$)"))) { - std::string typename_ = find_type(method[1], interpreter); + std::string typename_ = find_type_slow(method[1]); if (!typename_.empty()) { @@ -184,7 +217,7 @@ namespace xcpp } else { - std::string typename_ = find_type(to_inspect, interpreter); + std::string typename_ = find_type_slow(to_inspect); find_string = (typename_.empty()) ? to_inspect : typename_; } diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 4257e601..281f6b98 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -27,7 +27,7 @@ #include "xeus-cpp/xmagics.hpp" #include "xinput.hpp" -// #include "xinspect.hpp" +#include "xinspect.hpp" // #include "xmagics/executable.hpp" // #include "xmagics/execution.hpp" #include "xmagics/os.hpp" @@ -124,12 +124,12 @@ namespace xcpp // Attempt normal evaluation try { - std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)"; - std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)"); + std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)"; + std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)"); auto inspect_request = is_inspect_request(code, re); if (inspect_request.first) - inspect(inspect_request.second[0], kernel_res, *m_interpreter); - + inspect(inspect_request.second[0], kernel_res); + Cpp::BeginStdStreamCapture(Cpp::kStdErr); Cpp::BeginStdStreamCapture(Cpp::kStdOut); compilation_result = Cpp::Process(code.c_str()); @@ -148,7 +148,7 @@ namespace xcpp errorlevel = 1; ename = "Error :"; } - + if (compilation_result) { errorlevel = 1; @@ -227,7 +227,7 @@ namespace xcpp auto inspect_request = is_inspect_request(code.substr(0, cursor_pos), re); if (inspect_request.first) { - inspect(inspect_request.second[0], kernel_res, *m_interpreter); + inspect(inspect_request.second[0], kernel_res); } return kernel_res; }