Skip to content

Commit

Permalink
Merge pull request #22 from anutosh491/fixing_wasm
Browse files Browse the repository at this point in the history
Attempt to fix wasm build
  • Loading branch information
SylvainCorlay authored Jan 3, 2024
2 parents 7c7d42d + 5a5aca5 commit 63aabc0
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 122 deletions.
105 changes: 4 additions & 101 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
call C:\Users\runneradmin\micromamba-root\condabin\micromamba.bat activate xeus-cpp
mkdir -p bld
cd bld
cmake .. -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DXEUS_BUILD_TESTS=ON -DDEPENDENCY_SEARCH_PREFIX="%CONDA_PREFIX%\Library" -DCMAKE_PREFIX_PATH="%CONDA_PREFIX%\Library" -DCMAKE_INSTALL_PREFIX="%CONDA_PREFIX%"
cmake .. -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DXEUS_BUILD_TESTS=ON -DDEPENDENCY_SEARCH_PREFIX="%CONDA_PREFIX%\Library" -DCMAKE_PREFIX_PATH="%CONDA_PREFIX%\Library" -DCMAKE_INSTALL_PREFIX="%CONDA_PREFIX%"
- name: build
shell: cmd
Expand All @@ -99,111 +99,14 @@ jobs:
set CL=/MP
nmake install
- name: test
shell: cmd
run: |
call C:\Users\runneradmin\micromamba-root\condabin\micromamba.bat activate xeus-cpp
cd test
pytest . --reruns 5
emscripten_wasm_docker:
runs-on: ubuntu-20.04

strategy:
fail-fast: false
matrix:
container: ["emscripten/emsdk:2.0.34", "emscripten/emsdk:3.1.3", "emscripten/emsdk:latest"]


container: ${{ matrix.container}}

steps:
- uses: actions/checkout@v2

- name: Verify emscripten
run: emcc -v

- name: "install xtl"
run: |
mkdir -p /opt/xtl/build
git clone https://github.com/xtensor-stack/xtl.git /opt/xtl/src
cd /opt/xtl/src
git checkout 0.7.2
cd /opt/xtl/build
emcmake cmake ../src/ -DCMAKE_INSTALL_PREFIX=/custom_sysroot
emmake make -j2 install
- name: "install nlohmann-json"
run: |
mkdir -p /opt/nlohmannjson/build
git clone https://github.com/nlohmann/json.git /opt/nlohmannjson/src
cd /opt/nlohmannjson/src
git checkout v3.9.0
cd /opt/nlohmannjson/build
emcmake cmake ../src/ -DJSON_BuildTests=OFF -DCMAKE_INSTALL_PREFIX=/custom_sysroot
emmake make -j2 install
- name: "install xeus"
run: |
mkdir -p /opt/xeus/build
git clone https://github.com/jupyter-xeus/xeus.git /opt/xeus/src
cd /opt/xeus/build
emcmake cmake ../src \
-DXEUS_EMSCRIPTEN_WASM_BUILD=ON \
-DCMAKE_INSTALL_PREFIX=/custom_sysroot\
-Dxtl_DIR=/custom_sysroot/share/cmake/xtl \
-Dnlohmann_json_DIR=/custom_sysroot/lib/cmake/nlohmann_json
emmake make -j2
emmake make -j2 install
- name: "install xeus-lite"
run: |
mkdir -p /opt/xeus-lite/build
git clone https://github.com/jupyter-xeus/xeus-lite.git /opt/xeus-lite/src
cd /opt/xeus-lite/build
emcmake cmake ../src \
-DXEUS_EMSCRIPTEN_WASM_BUILD=ON \
-DCMAKE_INSTALL_PREFIX=/custom_sysroot \
-Dxtl_DIR=/custom_sysroot/share/cmake/xtl \
-Dnlohmann_json_DIR=/custom_sysroot/lib/cmake/nlohmann_json \
-Dxeus_DIR=/custom_sysroot/lib/cmake/xeus
emmake make -j2
emmake make -j2 install
- name: cmake configure rendered project
shell: bash -l {0}
run: |
mkdir -p bld
cd bld
emcmake cmake .. \
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON \
-Dxtl_DIR=/custom_sysroot/share/cmake/xtl \
-Dnlohmann_json_DIR=/custom_sysroot/lib/cmake/nlohmann_json \
-Dxeus_DIR=/custom_sysroot/lib/cmake/xeus \
-Dxeus-lite_DIR=/custom_sysroot/lib/cmake/xeus-lite \
-DCMAKE_INSTALL_PREFIX=/custom_sysroot
- name: build rendered project
shell: bash -l {0}
run: |
cd bld
emmake make -j2
- name: install rendered project
shell: bash -l {0}
run: |
cd bld
make install
emscripten_wasm:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
emsdk_ver: ["2.0.34", "3.1.3"]
emsdk_ver: ["3.1.45"]

steps:
- uses: actions/checkout@v2
Expand All @@ -224,7 +127,7 @@ jobs:
run: |
emsdk activate ${{matrix.emsdk_ver}}
source $CONDA_EMSDK_DIR/emsdk_env.sh
micromamba create -f environment-wasm-host.yml --platform=emscripten-32
micromamba create -f environment-wasm-host.yml --platform=emscripten-wasm32
mkdir build
pushd build
Expand All @@ -241,4 +144,4 @@ jobs:
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
..
make -j5
EMCC_CFLAGS='-sERROR_ON_UNDEFINED_SYMBOLS=0' emmake make -j5
38 changes: 22 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ OPTION(XEUS_CPP_BUILD_EXECUTABLE "Build the xcpp executable" ON)
OPTION(XEUS_CPP_USE_SHARED_XEUS "Link xcpp with the xeus shared library (instead of the static library)" ON)
OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (instead of the static library)" ON)

OPTION(XEUS_CPP_EMSCRIPTEN_WASM_BUILD "Build for wasm with emscripten" OFF)

if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)

if(EMSCRIPTEN)
add_compile_definitions(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
message("Build with emscripten")
SET(XEUS_CPP_BUILD_STATIC ON)
Expand Down Expand Up @@ -90,7 +90,7 @@ else()
endif ()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
if(NOT XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
if(NOT EMSCRIPTEN)
add_compile_options(-Wunused-parameter -Wextra -Wreorder)
endif()

Expand All @@ -103,7 +103,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU"
endif ()
endif ()

if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
if(EMSCRIPTEN)
# ENV (https://github.com/emscripten-core/emscripten/commit/6d9681ad04f60b41ef6345ab06c29bbc9eeb84e0)
set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"")
endif()
Expand Down Expand Up @@ -131,6 +131,10 @@ set(XEUS_CPP_SRC
src/xparser.cpp
)

if(EMSCRIPTEN)
list(APPEND XEUS_CPP_SRC src/xinterpreter_wasm.cpp)
endif()

set(XEUS_CPP_MAIN_SRC
src/main.cpp
)
Expand Down Expand Up @@ -192,7 +196,7 @@ macro(xeus_cpp_set_kernel_options target_name)
target_link_libraries(${target_name} PRIVATE xeus-cpp-static)
endif()

if(NOT XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
if(NOT EMSCRIPTEN)
find_package(Threads)
target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
endif()
Expand Down Expand Up @@ -238,7 +242,7 @@ macro(xeus_cpp_create_target target_name linkage output_name)
elseif (APPLE)
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
endif ()
if(NOT XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
if(NOT EMSCRIPTEN)
find_package(Threads) # TODO: add Threads as a dependence of xeus-static?
target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
endif()
Expand Down Expand Up @@ -291,14 +295,14 @@ if (XEUS_CPP_BUILD_EXECUTABLE)
target_link_libraries(xcpp PRIVATE xeus-zmq)
endif()

if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
if(EMSCRIPTEN)
include(WasmBuildOptions)
find_package(xeus-lite REQUIRED)
add_executable(xcpp_wasm src/main_emscripten_kernel.cpp )
target_link_libraries(xcpp_wasm PRIVATE xeus-cpp-static xeus-lite)
XEUS_CPP_set_kernel_options(xcpp_wasm)
xeus_wasm_compile_options(xcpp_wasm)
xeus_wasm_link_options(xcpp_wasm "web,worker")
add_executable(xcpp src/main_emscripten_kernel.cpp )
target_link_libraries(xcpp PRIVATE xeus-lite)
xeus_cpp_set_kernel_options(xcpp)
xeus_wasm_compile_options(xcpp)
xeus_wasm_link_options(xcpp "web,worker")
endif()

# Installation
Expand All @@ -325,7 +329,9 @@ endif ()
if (XEUS_CPP_BUILD_EXECUTABLE)
install(TARGETS xcpp
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

if(XEUS_CPP_BUILD_EXECUTABLE OR EMSCRIPTEN)
# Configuration and data directories for jupyter and xeus-cpp
set(XJUPYTER_DATA_DIR "share/jupyter" CACHE STRING "Jupyter data directory")

Expand Down Expand Up @@ -391,13 +397,13 @@ if (XEUS_CPP_BUILD_SHARED)
DESTINATION ${XEUS_CPP_CMAKECONFIG_INSTALL_DIR})
endif ()

if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
install(TARGETS xcpp_wasm
if(EMSCRIPTEN)
install(TARGETS xcpp
ARCHIVE DESTINATION ${XEUS_CPP_CMAKECONFIG_INSTALL_DIR})

install(FILES
"$<TARGET_FILE_DIR:xcpp_wasm>/xcpp_wasm.js"
"$<TARGET_FILE_DIR:xcpp_wasm>/xcpp_wasm.wasm"
"$<TARGET_FILE_DIR:xcpp>/xcpp.js"
"$<TARGET_FILE_DIR:xcpp>/xcpp.wasm"
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()

Expand Down
3 changes: 3 additions & 0 deletions environment-wasm-host.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ dependencies:
- xeus-lite
- xeus >=3.0.5,<4.0
- xtl >=0.7,<0.8
- llvm =16.0.6
- cpp-argparse
- pugixml
29 changes: 29 additions & 0 deletions include/xeus-cpp/xinterpreter_wasm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/************************************************************************************
* Copyright (c) 2023, xeus-cpp contributors *
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/


#ifndef XEUS_CPP_INTERPRETER_WASM_HPP
#define XEUS_CPP_INTERPRETER_WASM_HPP

#include "xinterpreter.hpp"
#include "xeus_cpp_config.hpp"

namespace xcpp
{
class XEUS_CPP_API wasm_interpreter : public interpreter
{
public:

wasm_interpreter();
virtual ~wasm_interpreter() = default;

};
}

#endif
2 changes: 2 additions & 0 deletions include/xeus-cpp/xmagics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ namespace xcpp

struct xmagic_line
{
virtual ~xmagic_line() = default;
virtual void operator()(const std::string& line) = 0;
};

struct xmagic_cell
{
virtual ~xmagic_cell() = default;
virtual void operator()(const std::string& line, const std::string& cell) = 0;
};

Expand Down
4 changes: 2 additions & 2 deletions src/main_emscripten_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

#include <xeus/xembind.hpp>

#include "xeus-cpp/xinterpreter.hpp"
#include "xeus-cpp/xinterpreter_wasm.hpp"

EMSCRIPTEN_BINDINGS(my_module)
{
xeus::export_core();
using interpreter_type = xcpp::interpreter;
using interpreter_type = xcpp::wasm_interpreter;
xeus::export_kernel<interpreter_type>("xkernel");
}
6 changes: 3 additions & 3 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ process_code(clang::Interpreter& Interp, const std::string& code, llvm::raw_stri
auto Addr = Interp.getSymbolAddress(clang::GlobalDecl(VD));
if (!Addr)
{
llvm::logAllUnhandledErrors(std::move(Addr.takeError()), error_stream, "error: ");
llvm::logAllUnhandledErrors(Addr.takeError(), error_stream, "error: ");
return true;
}
}
Expand All @@ -106,7 +106,7 @@ process_code(clang::Interpreter& Interp, const std::string& code, llvm::raw_stri
auto Addr = Interp.getSymbolAddress(clang::GlobalDecl(VD));
if (!Addr)
{
llvm::logAllUnhandledErrors(std::move(Addr.takeError()), error_stream, "error: ");
llvm::logAllUnhandledErrors(Addr.takeError(), error_stream, "error: ");
return true;
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ namespace xcpp
}

interpreter::interpreter(int argc, const char* const* argv)
: m_interpreter(std::move(create_interpreter(Args() /*argv + 1, argv + argc)*/, DiagPrinter.get())))
: m_interpreter(create_interpreter(Args() /*argv + 1, argv + argc)*/, DiagPrinter.get()))
, m_version(get_stdopt(argc, argv))
, // Extract C++ language standard version from command-line option
xmagics()
Expand Down
23 changes: 23 additions & 0 deletions src/xinterpreter_wasm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/************************************************************************************
* Copyright (c) 2023, xeus-cpp contributors *
* Copyright (c) 2023, Martin Vassilev *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/

#include "xeus/xinterpreter.hpp"
#include "xeus/xsystem.hpp"

#include "xeus-cpp/xinterpreter.hpp"
#include "xeus-cpp/xinterpreter_wasm.hpp"

namespace xcpp
{

wasm_interpreter::wasm_interpreter()
: interpreter(0, nullptr)
{
}
}

0 comments on commit 63aabc0

Please sign in to comment.