Skip to content

Commit ad5f1b4

Browse files
Fix build paths. Fix inspect to work with CppInterOp
1 parent 9b353e7 commit ad5f1b4

File tree

4 files changed

+76
-29
lines changed

4 files changed

+76
-29
lines changed

CMakeLists.txt

+20-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
1212

1313
set(XEUS_CPP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
1414

15+
if ("${CMAKE_INSTALL_INCLUDEDIR}" STREQUAL "")
16+
set(CMAKE_INSTALL_INCLUDEDIR "include")
17+
endif()
18+
if ("${CMAKE_INSTALL_LIBDIR}" STREQUAL "")
19+
set(CMAKE_INSTALL_LIBDIR "lib")
20+
endif()
21+
if ("${CMAKE_INSTALL_BINDIR}" STREQUAL "")
22+
set(CMAKE_INSTALL_BINDIR "bin")
23+
endif()
24+
1525
# Versionning
1626
# ===========
1727

@@ -198,7 +208,8 @@ include(CheckCXXCompilerFlag)
198208

199209
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
200210

201-
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib; ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
211+
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
212+
list(REMOVE_DUPLICATES CMAKE_INSTALL_RPATH)
202213

203214
macro(xeus_cpp_set_common_options target_name)
204215
if (MSVC)
@@ -287,7 +298,7 @@ macro(xeus_cpp_create_target target_name linkage output_name)
287298
set(XEUS_CPP_XEUS_TARGET xeus-static)
288299
endif ()
289300

290-
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl)
301+
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl Python::Python)
291302
if (WIN32 OR CYGWIN)
292303
#
293304
elseif (APPLE)
@@ -390,17 +401,20 @@ set(XEUS_CPP_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NA
390401
if (XEUS_CPP_BUILD_SHARED)
391402
install(TARGETS ${XEUS_CPP_TARGETS}
392403
EXPORT ${PROJECT_NAME}-targets
393-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
394-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
395-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
396-
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp)
404+
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
405+
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
406+
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}
407+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp)
397408

398409
# Makes the project importable from the build directory
399410
export(EXPORT ${PROJECT_NAME}-targets
400411
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
401412
endif ()
402413

403414
# Install xcpp
415+
if ("${CMAKE_VENV_PATH}" STREQUAL "")
416+
set(CMAKE_VENV_PATH "${CMAKE_INSTALL_PREFIX}")
417+
endif()
404418
if (XEUS_CPP_BUILD_EXECUTABLE)
405419
install(TARGETS xcpp
406420
RUNTIME DESTINATION ${CMAKE_VENV_PATH}/bin)

include/xeus-cpp/xinterpreter.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <vector>
1818

1919
//#include <clang/Interpreter/Interpreter.h>
20-
#include "clang/Interpreter/CppInterOp.h"
20+
#include "clang/Interpreter/CppInterOp.h" // from CppInterOp package
2121

2222
#include <nlohmann/json.hpp>
2323

src/xinspect.hpp

+48-15
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#ifndef XEUS_CPP_INSPECT_HPP
1010
#define XEUS_CPP_INSPECT_HPP
1111

12+
#include <filesystem>
1213
#include <fstream>
1314
#include <string>
1415

15-
1616
#include <pugixml.hpp>
1717

1818
#include <xtl/xsystem.hpp>
@@ -23,8 +23,11 @@
2323
#include "xdemangle.hpp"
2424
#include "xparser.hpp"
2525

26-
#include "llvm/Support/FileSystem.h"
27-
#include "llvm/Support/Path.h"
26+
//#include "llvm/Support/FileSystem.h"
27+
//#include "llvm/Support/Path.h"
28+
29+
//#include "clang/Interpreter/CppInterOp.h"
30+
2831

2932
namespace xcpp
3033
{
@@ -81,27 +84,57 @@ namespace xcpp
8184
}
8285
};
8386

84-
std::string find_type(const std::string& expression, clang::Interpreter& interpreter)
87+
88+
std::string find_type_slow(const std::string& expression) {
89+
static unsigned long long var_count = 0;
90+
91+
if (auto type = Cpp::GetType(expression))
92+
return Cpp::GetQualifiedName(type);
93+
94+
// Here we might need to deal with integral types such as 3.14.
95+
96+
std::string id = "__Xeus_GetType_" + std::to_string(var_count++);
97+
std::string using_clause = "using " + id + " = __typeof__(" + expression + ");\n";
98+
99+
if (!Cpp::Declare(using_clause.c_str(), /*silent=*/false)) {
100+
Cpp::TCppScope_t lookup = Cpp::GetNamed(id, 0);
101+
Cpp::TCppType_t lookup_ty = Cpp::GetTypeFromScope(lookup);
102+
return Cpp::GetQualifiedCompleteName(Cpp::GetCanonicalType(lookup_ty));
103+
}
104+
return "";
105+
}
106+
/*
107+
std::string find_type(const std::string& expression)
85108
{
86109
auto PTU = interpreter.Parse(expression + ";");
87110
if (llvm::Error Err = PTU.takeError()) {
88111
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
89112
return "";
90113
}
91114
92-
clang::Decl *D = *PTU->TUPart->decls_begin();
93-
if (!llvm::isa<clang::TopLevelStmtDecl>(D))
94-
return "";
115+
clang::Decl *D = *PTU->TUPart->decls_begin();
116+
if (!llvm::isa<clang::TopLevelStmtDecl>(D))
117+
return "";
95118
96-
clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt());
119+
clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt());
97120
98-
clang::QualType QT = E->getType();
121+
clang::QualType QT = E->getType();
99122
return QT.getAsString();
100123
}
101-
124+
*/
102125
static nl::json read_tagconfs(const char* path)
103126
{
104127
nl::json result = nl::json::array();
128+
for (auto &entry: std::filesystem::directory_iterator(path)) {
129+
if (entry.path().extension() != ".json")
130+
continue;
131+
std::ifstream i(entry.path());
132+
nl::json json_entry;
133+
i >> json_entry;
134+
result.emplace_back(std::move(json_entry));
135+
}
136+
return result;
137+
/*
105138
std::error_code EC;
106139
for (llvm::sys::fs::directory_iterator File(path, EC), FileEnd;
107140
File != FileEnd && !EC; File.increment(EC)) {
@@ -115,19 +148,19 @@ namespace xcpp
115148
result.emplace_back(std::move(entry));
116149
}
117150
return result;
151+
*/
118152
}
119153

120-
std::pair<bool, std::smatch> is_inspect_request(const std::string code, std::regex re)
154+
std::pair<bool, std::smatch> is_inspect_request(const std::string code, std::regex re)
121155
{
122156
std::smatch inspect;
123157
if (std::regex_search(code, inspect, re)){
124158
return std::make_pair(true, inspect);
125159
}
126160
return std::make_pair(false, inspect);
127-
128161
}
129162

130-
void inspect(const std::string& code, nl::json& kernel_res, clang::Interpreter& interpreter)
163+
void inspect(const std::string& code, nl::json& kernel_res)
131164
{
132165
std::string tagconf_dir = XCPP_TAGCONFS_DIR;
133166
std::string tagfiles_dir = XCPP_TAGFILES_DIR;
@@ -150,7 +183,7 @@ namespace xcpp
150183
// Method or variable of class found (xxxx.yyyy)
151184
if (std::regex_search(to_inspect, method, std::regex(R"((.*)\.(\w*)$)")))
152185
{
153-
std::string typename_ = find_type(method[1], interpreter);
186+
std::string typename_ = find_type_slow(method[1]);
154187

155188
if (!typename_.empty())
156189
{
@@ -184,7 +217,7 @@ namespace xcpp
184217
}
185218
else
186219
{
187-
std::string typename_ = find_type(to_inspect, interpreter);
220+
std::string typename_ = find_type_slow(to_inspect);
188221
find_string = (typename_.empty()) ? to_inspect : typename_;
189222
}
190223

src/xinterpreter.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "xeus-cpp/xmagics.hpp"
2828

2929
#include "xinput.hpp"
30-
// #include "xinspect.hpp"
30+
#include "xinspect.hpp"
3131
// #include "xmagics/executable.hpp"
3232
// #include "xmagics/execution.hpp"
3333
#include "xmagics/os.hpp"
@@ -124,12 +124,12 @@ namespace xcpp
124124
// Attempt normal evaluation
125125
try
126126
{
127-
std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)";
128-
std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)");
127+
std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)";
128+
std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)");
129129
auto inspect_request = is_inspect_request(code, re);
130130
if (inspect_request.first)
131-
inspect(inspect_request.second[0], kernel_res, *m_interpreter);
132-
131+
inspect(inspect_request.second[0], kernel_res);
132+
133133
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
134134
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
135135
compilation_result = Cpp::Process(code.c_str());
@@ -148,7 +148,7 @@ namespace xcpp
148148
errorlevel = 1;
149149
ename = "Error :";
150150
}
151-
151+
152152
if (compilation_result)
153153
{
154154
errorlevel = 1;
@@ -227,7 +227,7 @@ namespace xcpp
227227
auto inspect_request = is_inspect_request(code.substr(0, cursor_pos), re);
228228
if (inspect_request.first)
229229
{
230-
inspect(inspect_request.second[0], kernel_res, *m_interpreter);
230+
inspect(inspect_request.second[0], kernel_res);
231231
}
232232
return kernel_res;
233233
}

0 commit comments

Comments
 (0)