Skip to content

Commit

Permalink
Add Initial Support for xeus-cpp-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
anutosh491 committed Dec 22, 2024
1 parent a7649c0 commit 7a91638
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/deploy-github-page.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ jobs:
echo "PREFIX=$PREFIX" >> $GITHUB_ENV
export CMAKE_PREFIX_PATH=$PREFIX
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
export SYSROOT_PATH=$HOME/emsdk/upstream/emscripten/cache/sysroot
emcmake cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=$PREFIX \
-DCMAKE_INSTALL_PREFIX=$PREFIX \
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
-DSYSROOT_PATH=$SYSROOT_PATH \
..
emmake make -j ${{ env.ncpus }} install
Expand All @@ -76,6 +78,8 @@ jobs:
micromamba activate xeus-lite-host
python -m pip install jupyterlite-xeus
jupyter lite build --XeusAddon.prefix=${{ env.PREFIX }} --output-dir dist
#cp xcpp.data dist/extensions/@jupyterlite/xeus/static
#cp $PREFIX/lib/libclangCppInterOp.so dist/extensions/@jupyterlite/xeus/static
mkdir -p dist/files
mv notebooks dist/files
mv README.md dist/files
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,15 @@ jobs:
echo "PREFIX=$PREFIX" >> $GITHUB_ENV
export CMAKE_PREFIX_PATH=$PREFIX
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
export SYSROOT_PATH=$HOME/emsdk/upstream/emscripten/cache/sysroot
emcmake cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=$PREFIX \
-DCMAKE_INSTALL_PREFIX=$PREFIX \
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
-DSYSROOT_PATH=$SYSROOT_PATH \
..
emmake make -j ${{ env.ncpus }} install
Expand All @@ -274,6 +276,8 @@ jobs:
micromamba activate xeus-lite-host
python -m pip install jupyterlite-xeus
jupyter lite build --XeusAddon.prefix=${{ env.PREFIX }}
#cp xcpp.data _output/extensions/@jupyterlite/xeus/static
#cp $PREFIX/lib/libclangCppInterOp.so _output/extensions/@jupyterlite/xeus/static
- name: Setup tmate session
if: ${{ failure() && runner.debug }}
Expand Down
36 changes: 32 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ if(EMSCRIPTEN)
set(XEUS_CPP_USE_SHARED_XEUS_CPP OFF)
set(XEUS_CPP_BUILD_TESTS OFF)
# ENV (https://github.com/emscripten-core/emscripten/commit/6d9681ad04f60b41ef6345ab06c29bbc9eeb84e0)
set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"")
set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXPORTED_RUNTIME_METHODS=[ENV']\"")
endif()

# Dependencies
Expand Down Expand Up @@ -152,8 +152,22 @@ function(configure_kernel kernel)
endfunction()

message("Configure kernels: ...")
configure_kernel("/share/jupyter/kernels/xcpp17/")
configure_kernel("/share/jupyter/kernels/xcpp20/")
if(NOT EMSCRIPTEN)
configure_kernel("/share/jupyter/kernels/xcpp17/")
configure_kernel("/share/jupyter/kernels/xcpp20/")
else()
# TODO: Currently jupyterlite-xeus and xeus-lite do not provide
# methods to fetch information from the arguments present in the
# generated emscripten kernel.
# The following needs to be done here :
# 1) We need to configure the kernel properly
# Check issue https://github.com/compiler-research/xeus-cpp/issues/185.
# 2) Once the above is done we need to add support in jupyterlite-xeus & xeus-lite
# to be able to deal with arguments present in kernel.json
# 3) Finally we should fetch the C++ version from the kernel.json file and
# be able to pass it to our wasm interpreter rather than forcing a version.
configure_kernel("/share/jupyter/kernels/xcpp20/")
endif()

# Source files
# ============
Expand Down Expand Up @@ -401,8 +415,22 @@ if(EMSCRIPTEN)
xeus_cpp_set_kernel_options(xcpp)
xeus_wasm_compile_options(xcpp)
xeus_wasm_link_options(xcpp "web,worker")
target_link_options(xcpp PUBLIC
-sEXPORTED_RUNTIME_METHODS=FS,PATH,ERRNO_CODES
# add sysroot location here
--preload-file ${SYSROOT_PATH}/include@/include
)
# TODO: Emscripten supports preloading files just once before it generates
# the xcpp.data file (containing the binary representation of the file(s) we
# want to include in our application).
# Hence although we are adding supporting for Standard Headers, Libraries etc
# through emscripten's sysroot for now, we need to do the following:
# 1) Enable CppInterOp to provide us with a resource dir.
# 2) If the above cannot be done, we can use the resource dir provided
# by llvm on emscripten-forge but would involve adding a dependency.
# 3) Shift the resource dir and the sysroot to a common location.
# 4) Preload everything required together.
endif()

# Tests
# =====

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ pushd build
export PREFIX=$MAMBA_ROOT_PREFIX/envs/xeus-cpp-wasm-host
export CMAKE_PREFIX_PATH=$PREFIX
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
export SYSROOT_PATH=$HOME/emsdk/upstream/emscripten/cache/sysroot

emcmake cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=$PREFIX \
-DCMAKE_INSTALL_PREFIX=$PREFIX \
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
-DSYSROOT_PATH=$SYSROOT_PATH \
..
emmake make install
```
Expand All @@ -105,12 +107,20 @@ micromamba activate xeus-lite-host
python -m pip install jupyterlite-xeus
jupyter lite build --XeusAddon.prefix=$PREFIX
```

We now need to shift necessary files like `xcpp.data` which contains the binary representation of the file(s)
we want to include in our application. As of now this would contain all important files like Standard Headers,
Libraries etc coming out of emscripten's sysroot. Assuming we are still inside build we should do the following
```bash
cp xcpp.data _output/extensions/@jupyterlite/xeus/static
cp $PREFIX/lib/libclangCppInterOp.so _output/extensions/@jupyterlite/xeus/static
```

Once the Jupyter Lite site has built you can test the website locally by executing
```bash
jupyter lite serve --XeusAddon.prefix=$PREFIX
```


## Trying it online

To try out xeus-cpp interactively in your web browser, just click on the binder link:
Expand Down
3 changes: 3 additions & 0 deletions include/xeus-cpp/xinterpreter_wasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace xcpp
wasm_interpreter();
virtual ~wasm_interpreter() = default;

private:
static std::vector<const char*> create_args();

};
}

Expand Down
17 changes: 12 additions & 5 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using Args = std::vector<const char*>;

void* createInterpreter(const Args &ExtraArgs = {}) {
Args ClangArgs = {/*"-xc++"*/"-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"};
#ifndef EMSCRIPTEN
if (std::find_if(ExtraArgs.begin(), ExtraArgs.end(), [](const std::string& s) {
return s == "-resource-dir";}) == ExtraArgs.end()) {
std::string resource_dir = Cpp::DetectResourceDir();
Expand All @@ -42,6 +43,7 @@ void* createInterpreter(const Args &ExtraArgs = {}) {
ClangArgs.push_back("-isystem");
ClangArgs.push_back(CxxInclude.c_str());
}
#endif
ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
// FIXME: We should process the kernel input options and conditionally pass
// the gpu args here.
Expand Down Expand Up @@ -73,6 +75,7 @@ namespace xcpp
static std::string get_stdopt()
{
// We need to find what's the C++ version the interpreter runs with.
#ifndef EMSCRIPTEN
const char* code = R"(
int __get_cxx_version () {
#if __cplusplus > 202302L
Expand All @@ -93,12 +96,14 @@ int __get_cxx_version () {
}
__get_cxx_version ()
)";

auto cxx_version = Cpp::Evaluate(code);
return std::to_string(cxx_version);
auto cxx_version = Cpp::Evaluate(code);
return std::to_string(cxx_version);
#else
constexpr int cxx_version = 20;
return std::to_string(cxx_version);
#endif
}


interpreter::interpreter(int argc, const char* const* argv) :
xmagics()
, p_cout_strbuf(nullptr)
Expand Down Expand Up @@ -357,7 +362,9 @@ __get_cxx_version ()

void interpreter::init_includes()
{
Cpp::AddIncludePath((xeus::prefix_path() + "/include/").c_str());
#ifndef EMSCRIPTEN
Cpp::AddIncludePath((xeus::prefix_path() + "/include/").c_str());
#endif
}

void interpreter::init_preamble()
Expand Down
8 changes: 7 additions & 1 deletion src/xinterpreter_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ namespace xcpp
{

wasm_interpreter::wasm_interpreter()
: interpreter(0, nullptr)
: interpreter(create_args().size(), create_args().data())
{
}

std::vector<const char*> wasm_interpreter::create_args()
{
static std::vector<const char*> Args = {"-Xclang", "-std=c++20"};
return Args;
}
}

0 comments on commit 7a91638

Please sign in to comment.