Skip to content

Commit 58b0658

Browse files
authored
Merge branch 'main' into tagfiles-lite
2 parents 889f32f + ebf17f8 commit 58b0658

12 files changed

+70
-73
lines changed

CMakeLists.txt

+5-20
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ function(configure_native_kernel kernel)
148148
endfunction()
149149

150150
function(configure_wasm_kernel kernel)
151+
set(XEUS_CPP_RESOURCE_DIR "/lib/clang/${CPPINTEROP_LLVM_VERSION_MAJOR}" PARENT_SCOPE)
151152

152153
configure_file (
153154
"${CMAKE_CURRENT_SOURCE_DIR}${kernel}wasm_kernel.json.in"
@@ -169,17 +170,9 @@ endfunction()
169170

170171
message("Configure kernels: ...")
171172
if(EMSCRIPTEN)
172-
# TODO: Currently jupyterlite-xeus and xeus-lite do not provide
173-
# methods to fetch information from the arguments present in the
174-
# generated emscripten kernel.
175-
# The following needs to be done here :
176-
# 1) We need to configure the kernel properly
177-
# Check issue https://github.com/compiler-research/xeus-cpp/issues/185.
178-
# 2) Once the above is done we need to add support in jupyterlite-xeus & xeus-lite
179-
# to be able to deal with arguments present in kernel.json
180-
# 3) Finally we should fetch the C++ version from the kernel.json file and
181-
# be able to pass it to our wasm interpreter rather than forcing a version.
173+
configure_wasm_kernel("/share/jupyter/kernels/xcpp17/")
182174
configure_wasm_kernel("/share/jupyter/kernels/xcpp20/")
175+
configure_wasm_kernel("/share/jupyter/kernels/xcpp23/")
183176
else()
184177
configure_native_kernel("/share/jupyter/kernels/xcpp17/")
185178
configure_native_kernel("/share/jupyter/kernels/xcpp20/")
@@ -448,16 +441,8 @@ if(EMSCRIPTEN)
448441
PUBLIC "SHELL: --preload-file ${XCPP_TAGCONFS_DIR}@/etc/xeus-cpp/tags.d"
449442
PUBLIC "SHELL: --post-js ${CMAKE_CURRENT_SOURCE_DIR}/wasm_patches/post.js"
450443
)
451-
# TODO: Emscripten supports preloading files just once before it generates
452-
# the xcpp.data file (containing the binary representation of the file(s) we
453-
# want to include in our application).
454-
# Hence although we are adding support for Standard Headers, Libraries etc
455-
# through emscripten's sysroot for now, we need to do the following:
456-
# 1) Enable CppInterOp to provide us with a resource dir.
457-
# 2) If the above cannot be done, we can use the resource dir provided
458-
# by llvm on emscripten-forge but would involve adding a dependency.
459-
# 3) Shift the resource dir and the sysroot to a common location.
460-
# 4) Preload everything required together.
444+
# TODO: Uncomment the above line regarding preloading clang's resource dir
445+
# once has been supported through cppinterop.
461446
endif()
462447
# Tests
463448
# =====

include/xeus-cpp/xinterpreter_wasm.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace xcpp
1919
{
2020
public:
2121

22-
wasm_interpreter();
22+
wasm_interpreter(int argc = 0, char** argv = nullptr);
2323
virtual ~wasm_interpreter() = default;
2424

2525
};

include/xeus-cpp/xutils.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ namespace xcpp
2424
XEUS_CPP_API
2525
void stop_handler(int sig);
2626

27-
XEUS_CPP_API
28-
interpreter_ptr build_interpreter(int argc, char** argv);
29-
3027
XEUS_CPP_API
3128
std::string retrieve_tagconf_dir();
3229

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"display_name": "C++17",
3+
"argv": [
4+
"@XEUS_CPP_KERNELSPEC_PATH@xcpp",
5+
"-resource-dir", "@XEUS_CPP_RESOURCE_DIR@",
6+
"-std=c++17"
7+
],
8+
"language": "cpp",
9+
"metadata": {
10+
"debugger": false,
11+
"shared": {
12+
"libclangCppInterOp.so": "lib/libclangCppInterOp.so"
13+
}
14+
}
15+
}

share/jupyter/kernels/xcpp20/wasm_kernel.json.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"display_name": "C++20",
33
"argv": [
44
"@XEUS_CPP_KERNELSPEC_PATH@xcpp",
5-
"-f",
6-
"{connection_file}",
5+
"-resource-dir", "@XEUS_CPP_RESOURCE_DIR@",
76
"-std=c++20"
87
],
98
"language": "cpp",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"display_name": "C++23",
3+
"argv": [
4+
"@XEUS_CPP_KERNELSPEC_PATH@xcpp",
5+
"-resource-dir", "@XEUS_CPP_RESOURCE_DIR@",
6+
"-std=c++23"
7+
],
8+
"language": "cpp",
9+
"metadata": {
10+
"debugger": false,
11+
"shared": {
12+
"libclangCppInterOp.so": "lib/libclangCppInterOp.so"
13+
}
14+
}
15+
}

src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main(int argc, char* argv[])
5959
signal(SIGINT, xcpp::stop_handler);
6060

6161
std::string file_name = xeus::extract_filename(argc, argv);
62-
interpreter_ptr interpreter = xcpp::build_interpreter(argc, argv);
62+
auto interpreter = std::make_unique<xcpp::interpreter>(argc, argv);
6363
std::unique_ptr<xeus::xcontext> context = xeus::make_zmq_context();
6464

6565
if (!file_name.empty())

src/main_emscripten_kernel.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,30 @@
1515

1616
#include "xeus-cpp/xinterpreter_wasm.hpp"
1717

18+
template <class interpreter_type>
19+
static interpreter_type* builder_with_args(emscripten::val js_args)
20+
{
21+
// TODO: Refactor interpreter constructor to avoid static args-to-argv conversion.
22+
static std::vector<std::string> args = emscripten::vecFromJSArray<std::string>(js_args);
23+
static std::vector<const char*> argv_vec;
24+
25+
for (const auto& s : args)
26+
{
27+
argv_vec.push_back(s.c_str());
28+
}
29+
30+
int argc = static_cast<int>(argv_vec.size());
31+
char** argv = const_cast<char**>(argv_vec.data());
32+
33+
auto* res = new interpreter_type(argc, argv);
34+
argv_vec.clear();
35+
args.clear();
36+
return res;
37+
}
38+
1839
EMSCRIPTEN_BINDINGS(my_module)
1940
{
2041
xeus::export_core();
2142
using interpreter_type = xcpp::wasm_interpreter;
22-
xeus::export_kernel<interpreter_type>("xkernel");
43+
xeus::export_kernel<interpreter_type, &builder_with_args<interpreter_type>>("xkernel");
2344
}

src/xinterpreter.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "xinput.hpp"
1919
#include "xinspect.hpp"
2020
#include "xmagics/os.hpp"
21+
#include <iostream>
2122
#ifndef EMSCRIPTEN
2223
#include "xmagics/xassist.hpp"
2324
#endif
@@ -27,25 +28,23 @@
2728
using Args = std::vector<const char*>;
2829

2930
void* createInterpreter(const Args &ExtraArgs = {}) {
30-
Args ClangArgs = {/*"-xc++"*/"-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"};
31-
#ifdef EMSCRIPTEN
32-
ClangArgs.push_back("-std=c++20");
33-
#else
31+
Args ClangArgs = {/*"-xc++"*/"-v"};
3432
if (std::find_if(ExtraArgs.begin(), ExtraArgs.end(), [](const std::string& s) {
3533
return s == "-resource-dir";}) == ExtraArgs.end()) {
3634
std::string resource_dir = Cpp::DetectResourceDir();
37-
if (resource_dir.empty())
38-
std::cerr << "Failed to detect the resource-dir\n";
39-
ClangArgs.push_back("-resource-dir");
40-
ClangArgs.push_back(resource_dir.c_str());
35+
if (!resource_dir.empty()) {
36+
ClangArgs.push_back("-resource-dir");
37+
ClangArgs.push_back(resource_dir.c_str());
38+
} else {
39+
std::cerr << "Failed to detect the resource-dir\n";
40+
}
4141
}
4242
std::vector<std::string> CxxSystemIncludes;
4343
Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes);
4444
for (const std::string& CxxInclude : CxxSystemIncludes) {
4545
ClangArgs.push_back("-isystem");
4646
ClangArgs.push_back(CxxInclude.c_str());
4747
}
48-
#endif
4948
ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
5049
// FIXME: We should process the kernel input options and conditionally pass
5150
// the gpu args here.

src/xinterpreter_wasm.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
namespace xcpp
1717
{
18-
19-
wasm_interpreter::wasm_interpreter()
20-
: interpreter(0, nullptr)
18+
wasm_interpreter::wasm_interpreter(int argc, char** argv)
19+
: interpreter(argc, argv)
2120
{
2221
}
2322
}

src/xutils.cpp

-15
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,6 @@ namespace xcpp
5050
exit(0);
5151
}
5252

53-
interpreter_ptr build_interpreter(int argc, char** argv)
54-
{
55-
std::vector<const char*> interpreter_argv(argc);
56-
interpreter_argv[0] = "xeus-cpp";
57-
58-
// Copy all arguments in the new vector excepting the process name.
59-
for (int i = 1; i < argc; i++)
60-
{
61-
interpreter_argv[i] = argv[i];
62-
}
63-
64-
interpreter_ptr interp_ptr = std::make_unique<interpreter>(argc, interpreter_argv.data());
65-
return interp_ptr;
66-
}
67-
6853
std::string retrieve_tagconf_dir()
6954
{
7055
const char* tagconf_dir_env = std::getenv("XCPP_TAGCONFS_DIR");

test/test_interpreter.cpp

-18
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,6 @@ TEST_SUITE("trim"){
344344

345345
}
346346

347-
TEST_SUITE("build_interpreter")
348-
{
349-
// This test case checks if the function `build_interpreter` returns a non-null pointer
350-
// when valid arguments are passed. It sets up a scenario with valid command line arguments
351-
// and checks if the function returns a non-null pointer.
352-
TEST_CASE("build_interpreter_pointer_not_null")
353-
{
354-
char arg1[] = "program_name";
355-
char arg2[] = "-option1";
356-
char* argv[] = {arg1, arg2};
357-
int argc = 2;
358-
359-
interpreter_ptr interp_ptr = xcpp::build_interpreter(argc, argv);
360-
361-
REQUIRE(interp_ptr != nullptr);
362-
}
363-
}
364-
365347
TEST_SUITE("is_match_magics_manager")
366348
{
367349
// This test case checks if the function `is_match` correctly identifies strings that match

0 commit comments

Comments
 (0)