Skip to content

Commit 06b171f

Browse files
Initial merge with xeus-clang-repl (mini) (#46)
* Initial merge with xeus-clang-repl * Allow for cpp-argparse 3.0. * Update README.md * Undo new kernel configuration logic * Undo some xcpp kernel settings * Fix kernel config target path * Fix new version of xeus-lite not working. Must be <2.0 * Fix resource path subst in kernel specs --------- Co-authored-by: Sylvain Corlay <[email protected]>
1 parent 6150986 commit 06b171f

File tree

10 files changed

+183
-397
lines changed

10 files changed

+183
-397
lines changed

CMakeLists.txt

+30-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ set(CMAKE_CXX_EXTENSIONS NO)
1515
set(CMAKE_CXX_STANDARD 17)
1616
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
1717

18+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
19+
20+
include(GNUInstallDirs)
21+
1822
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
1923

2024
set(XEUS_CPP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
@@ -33,20 +37,6 @@ set(${PROJECT_NAME}_VERSION
3337
${XEUS_CPP_VERSION_MAJOR}.${XEUS_CPP_VERSION_MINOR}.${XEUS_CPP_VERSION_PATCH})
3438
message(STATUS "Building xeus-cpp v${${PROJECT_NAME}_VERSION}")
3539

36-
# Configuration
37-
# =============
38-
39-
include(GNUInstallDirs)
40-
41-
if (NOT DEFINED XEUS_CPP_KERNELSPEC_PATH)
42-
set(XEUS_CPP_KERNELSPEC_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/")
43-
endif ()
44-
45-
configure_file (
46-
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp/kernel.json.in"
47-
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp/kernel.json"
48-
)
49-
5040
# Build options
5141
# =============
5242

@@ -121,13 +111,29 @@ if(EMSCRIPTEN)
121111
set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"")
122112
endif()
123113

124-
find_package(Clang REQUIRED)
125-
include(AddLLVM)
126-
include(HandleLLVMOptions)
127-
add_definitions(${LLVM_DEFINITIONS})
114+
find_package(CppInterOp REQUIRED)
115+
if(CppInterOp_FOUND)
116+
message(STATUS "Found CppInterOp: config=${CPPINTEROP_CONFIG} dir=${CPPINTEROP_DIR} (found version=${CPPINTEROP_VERSION} compatible with Clang ${CPPINTEROP_LLVM_VERSION_MAJOR}.x)")
117+
endif()
118+
128119
find_package(argparse REQUIRED)
129120
find_package(pugixml REQUIRED)
130121

122+
# Configuration
123+
# =============
124+
125+
if (NOT DEFINED XEUS_CPP_KERNELSPEC_PATH)
126+
set(XEUS_CPP_KERNELSPEC_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/")
127+
endif ()
128+
129+
set(XEUS_CPP_PATH "$ENV{PATH}")
130+
set(XEUS_CPP_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}")
131+
set(XEUS_CPP_RESOURCE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/clang/${CppInterOp_CLANG_VERSION})
132+
configure_file (
133+
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp/kernel.json.in"
134+
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/xcpp/kernel.json"
135+
)
136+
131137
# Source files
132138
# ============
133139

@@ -174,7 +180,8 @@ include(CheckCXXCompilerFlag)
174180

175181
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
176182

177-
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib; ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
183+
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
184+
list(REMOVE_DUPLICATES CMAKE_INSTALL_RPATH)
178185

179186
macro(xeus_cpp_set_common_options target_name)
180187
if (MSVC)
@@ -228,7 +235,6 @@ macro(xeus_cpp_set_kernel_options target_name)
228235
find_package(Threads)
229236
target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
230237
endif()
231-
232238
endmacro()
233239

234240
# Common macro for shared and static library
@@ -264,7 +270,8 @@ macro(xeus_cpp_create_target target_name linkage output_name)
264270
set(XEUS_CPP_XEUS_TARGET xeus-static)
265271
endif ()
266272

267-
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangInterpreter pugixml argparse::argparse xtl)
273+
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl)
274+
268275
if (WIN32 OR CYGWIN)
269276
#
270277
elseif (APPLE)
@@ -274,7 +281,7 @@ macro(xeus_cpp_create_target target_name linkage output_name)
274281
find_package(Threads) # TODO: add Threads as a dependence of xeus-static?
275282
target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
276283
endif()
277-
284+
278285
endmacro()
279286

280287
# xeus-cpp-headers
@@ -371,7 +378,7 @@ if(XEUS_CPP_BUILD_EXECUTABLE OR EMSCRIPTEN)
371378
set(XJUPYTER_DATA_DIR "share/jupyter" CACHE STRING "Jupyter data directory")
372379

373380
# Install xcpp Jupyter kernelspec
374-
set(KERNELSPEC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels)
381+
set(KERNELSPEC_DIR ${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels)
375382
install(DIRECTORY ${KERNELSPEC_DIR}
376383
DESTINATION ${XJUPYTER_DATA_DIR}
377384
PATTERN "*.in" EXCLUDE)

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ source activate "xeus-cpp"
3232
```
3333
We will now install the dependencies needed to compile xeux-cpp from source within this environment by executing the following
3434
```bash
35-
mamba install notebook cmake cxx-compiler xeus-zmq nlohmann_json=3.11.2 cppzmq xtl jupyterlab clangdev=16 cpp-argparse pugixml doctest -c conda-forge
35+
mamba install notebook cmake cxx-compiler xeus-zmq nlohmann_json=3.11.2 cppzmq xtl jupyterlab CppInterOp cpp-argparse<3.1 pugixml doctest -c conda-forge
3636
```
3737
Now you can compile the kernel from the source by executing (replace `$CONDA_PREFIX` with a custom installation prefix if need be)
3838
```bash
@@ -65,13 +65,13 @@ http://xeus-cpp.readthedocs.io
6565
- [clang](https://github.com/llvm/llvm-project/)
6666
- [argparse](https://github.com/p-ranav/argparse)
6767

68-
| `xeus-cpp` | `xeus-zmq` | `xtl` | `clang` | `pugixml` | `cppzmq` | `cpp-argparse`| `nlohmann_json` | `dirent` (windows only) |
69-
|------------|-----------------|-----------------|-----------|-----------|----------|---------------|-----------------|-------------------------|
70-
| main | >=1.0.0,<2.0.0 | >=0.7.7,<0.8.0 | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
71-
| 0.3.0 | >=1.0.0,<2.0.0 | >=0.7.7,<0.8.0 | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
72-
| 0.2.0 | >=1.0.0,<2.0.0 | >=0.7.7,<0.8.0 | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
73-
| 0.1.0 | >=1.0.0,<2.0.0 | >=0.7.0,<0.8.0 | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
74-
| 0.0.1 | >=1.0.0,<2.0.0 | >=0.7.0,<0.8.0 | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
68+
| `xeus-cpp` | `xeus-zmq` | `xtl` | `CppInterOp` | `clang` | `pugixml` | `cppzmq` | `cpp-argparse`| `nlohmann_json` | `dirent` (windows only) |
69+
|------------|-----------------|-----------------|--------------|-----------|-----------|----------|---------------|-----------------|-------------------------|
70+
| main | >=1.0.0,<2.0.0 | >=0.7.7,<0.8.0 | >=1.2.0 | | ~1.8.1 | ~4.3.0 | <3.1 | >=3.11.2,<4.0 | >=2.3.2,<3 |
71+
| 0.3.0 | >=1.0.0,<2.0.0 | >=0.7.7,<0.8.0 | | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
72+
| 0.2.0 | >=1.0.0,<2.0.0 | >=0.7.7,<0.8.0 | | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
73+
| 0.1.0 | >=1.0.0,<2.0.0 | >=0.7.0,<0.8.0 | | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
74+
| 0.0.1 | >=1.0.0,<2.0.0 | >=0.7.0,<0.8.0 | | >=16,<17 | ~1.8.1 | ~4.3.0 | ~2.9 | >=3.6.1,<4.0 | >=2.3.2,<3 |
7575

7676
## Contributing
7777

environment-dev.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ dependencies:
1111
- nlohmann_json=3.11.2
1212
- cppzmq
1313
- xtl
14-
- clangdev >=16,<17
14+
- CppInterOp
1515
- pugixml
16-
- cpp-argparse
16+
- cpp-argparse <3.1
1717
- zlib
1818
# Test dependencies
1919
- pytest

environment-wasm-host.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ channels:
44
- https://repo.mamba.pm/conda-forge
55
dependencies:
66
- nlohmann_json
7-
- xeus-lite
7+
- xeus-lite <2.0
88
- xeus >=3.0.5,<4.0
99
- xtl >=0.7,<0.8
10-
- llvm =16.0.6
10+
- CppInterOp
1111
- cpp-argparse
1212
- pugixml

include/xcpp/xdisplay.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "xcpp/xmime.hpp"
1616

17+
#include "xeus/xinterpreter.hpp"
18+
1719
namespace nl = nlohmann;
1820

1921
namespace xcpp

include/xeus-cpp/xinterpreter.hpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <string>
1717
#include <vector>
1818

19-
#include <clang/Interpreter/Interpreter.h>
19+
#include "clang/Interpreter/CppInterOp.h" // from CppInterOp package
2020

2121
#include <nlohmann/json.hpp>
2222

@@ -76,10 +76,6 @@ namespace xcpp
7676
void init_preamble();
7777
void init_magic();
7878

79-
std::string get_stdopt(int argc, const char* const* argv);
80-
81-
std::unique_ptr<clang::Interpreter> m_interpreter;
82-
8379
std::string m_version;
8480

8581
xmagics_manager xmagics;

share/jupyter/kernels/xcpp/kernel.json.in

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
{
22
"display_name": "cpp 14 (xcpp)",
3+
"env": {
4+
"PATH":"@XEUS_CPP_PATH@",
5+
"LD_LIBRARY_PATH":"@XEUS_CPP_LD_LIBRARY_PATH@"
6+
},
37
"argv": [
48
"@XEUS_CPP_KERNELSPEC_PATH@xcpp",
59
"-f",
6-
"{connection_file}"
10+
"{connection_file}",
11+
"-resource-dir", "@XEUS_CPP_RESOURCE_DIR@",
12+
"-I", "@CMAKE_INSTALL_PREFIX@/include",
13+
"-std=c++14"@XEUS_CPP_OMP@
714
],
815
"language": "cpp",
916
"metadata": {"debugger": false

src/xinspect.hpp

+32-34
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <fstream>
1414
#include <string>
1515

16-
1716
#include <pugixml.hpp>
1817

1918
#include <xtl/xsystem.hpp>
@@ -24,6 +23,8 @@
2423
#include "xdemangle.hpp"
2524
#include "xparser.hpp"
2625

26+
#include "clang/Interpreter/CppInterOp.h"
27+
2728
namespace xcpp
2829
{
2930
struct node_predicate
@@ -79,22 +80,23 @@ namespace xcpp
7980
}
8081
};
8182

82-
std::string find_type(const std::string& expression, clang::Interpreter& interpreter)
83-
{
84-
auto PTU = interpreter.Parse(expression + ";");
85-
if (llvm::Error Err = PTU.takeError()) {
86-
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
87-
return "";
88-
}
83+
std::string find_type_slow(const std::string& expression) {
84+
static unsigned long long var_count = 0;
85+
86+
if (auto *type = Cpp::GetType(expression))
87+
return Cpp::GetQualifiedName(type);
8988

90-
clang::Decl *D = *PTU->TUPart->decls_begin();
91-
if (!llvm::isa<clang::TopLevelStmtDecl>(D))
92-
return "";
89+
// Here we might need to deal with integral types such as 3.14.
9390

94-
clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt());
91+
std::string id = "__Xeus_GetType_" + std::to_string(var_count++);
92+
std::string using_clause = "using " + id + " = __typeof__(" + expression + ");\n";
9593

96-
clang::QualType QT = E->getType();
97-
return QT.getAsString();
94+
if (!Cpp::Declare(using_clause.c_str(), /*silent=*/false)) {
95+
Cpp::TCppScope_t lookup = Cpp::GetNamed(id, nullptr);
96+
Cpp::TCppType_t lookup_ty = Cpp::GetTypeFromScope(lookup);
97+
return Cpp::GetQualifiedCompleteName(Cpp::GetCanonicalType(lookup_ty));
98+
}
99+
return "";
98100
}
99101

100102
static nl::json read_tagconfs(const char* path)
@@ -111,17 +113,16 @@ namespace xcpp
111113
return result;
112114
}
113115

114-
std::pair<bool, std::smatch> is_inspect_request(const std::string code, std::regex re)
116+
std::pair<bool, std::smatch> is_inspect_request(const std::string& code,
117+
const std::regex& re)
115118
{
116119
std::smatch inspect;
117-
if (std::regex_search(code, inspect, re)){
120+
if (std::regex_search(code, inspect, re))
118121
return std::make_pair(true, inspect);
119-
}
120122
return std::make_pair(false, inspect);
121-
122123
}
123124

124-
void inspect(const std::string& code, nl::json& kernel_res, clang::Interpreter& interpreter)
125+
void inspect(const std::string& code, nl::json& kernel_res)
125126
{
126127
std::string tagconf_dir = XCPP_TAGCONFS_DIR;
127128
std::string tagfiles_dir = XCPP_TAGFILES_DIR;
@@ -144,18 +145,18 @@ namespace xcpp
144145
// Method or variable of class found (xxxx.yyyy)
145146
if (std::regex_search(to_inspect, method, std::regex(R"((.*)\.(\w*)$)")))
146147
{
147-
std::string typename_ = find_type(method[1], interpreter);
148+
std::string type_name = find_type_slow(method[1]);
148149

149-
if (!typename_.empty())
150+
if (!type_name.empty())
150151
{
151152
for (nl::json::const_iterator it = tagconfs.cbegin(); it != tagconfs.cend(); ++it)
152153
{
153154
url = it->at("url");
154155
tagfile = it->at("tagfile");
155156
std::string filename = tagfiles_dir + "/" + tagfile;
156157
pugi::xml_document doc;
157-
doc.load_file(filename.c_str());
158-
class_member_predicate predicate{typename_, "function", method[2]};
158+
pugi::xml_parse_result result = doc.load_file(filename.c_str());
159+
class_member_predicate predicate{type_name, "function", method[2]};
159160
auto node = doc.find_node(predicate);
160161
if (!node.empty())
161162
{
@@ -178,8 +179,8 @@ namespace xcpp
178179
}
179180
else
180181
{
181-
std::string typename_ = find_type(to_inspect, interpreter);
182-
find_string = (typename_.empty()) ? to_inspect : typename_;
182+
std::string type_name = find_type_slow(to_inspect);
183+
find_string = (type_name.empty()) ? to_inspect : type_name;
183184
}
184185

185186
for (nl::json::const_iterator it = tagconfs.cbegin(); it != tagconfs.cend(); ++it)
@@ -188,7 +189,7 @@ namespace xcpp
188189
tagfile = it->at("tagfile");
189190
std::string filename = tagfiles_dir + "/" + tagfile;
190191
pugi::xml_document doc;
191-
doc.load_file(filename.c_str());
192+
pugi::xml_parse_result result = doc.load_file(filename.c_str());
192193
for (auto c : check)
193194
{
194195
node_predicate predicate{c, find_string};
@@ -266,8 +267,7 @@ namespace xcpp
266267
using xpreamble::pattern;
267268
const std::string spattern = R"(^\?)";
268269

269-
xintrospection(clang::Interpreter& p)
270-
: m_interpreter{p}
270+
xintrospection()
271271
{
272272
pattern = spattern;
273273
}
@@ -277,17 +277,15 @@ namespace xcpp
277277
std::regex re(spattern + R"((.*))");
278278
std::smatch to_inspect;
279279
std::regex_search(code, to_inspect, re);
280-
inspect(to_inspect[1], kernel_res, m_interpreter);
280+
inspect(to_inspect[1], kernel_res);
281281
}
282282

283283
virtual xpreamble* clone() const override
284284
{
285285
return new xintrospection(*this);
286286
}
287-
288-
private:
289-
290-
clang::Interpreter& m_interpreter;
291287
};
288+
292289
}
293-
#endif
290+
291+
#endif // XEUS_CPP_INSPECT_HPP

0 commit comments

Comments
 (0)