From 60c35b87a7397554fdbd9460fc40e02df77a7ef0 Mon Sep 17 00:00:00 2001 From: anutosh491 Date: Tue, 28 Nov 2023 15:00:05 +0530 Subject: [PATCH 1/4] Attempt to fix wasm build --- .github/workflows/main.yml | 98 ++------------------------ CMakeLists.txt | 4 +- environment-wasm-host.yml | 3 + include/xeus-cpp/xinterpreter_wasm.hpp | 32 +++++++++ src/main_emscripten_kernel.cpp | 4 +- src/xinterpreter_wasm.cpp | 30 ++++++++ 6 files changed, 73 insertions(+), 98 deletions(-) create mode 100644 include/xeus-cpp/xinterpreter_wasm.hpp create mode 100644 src/xinterpreter_wasm.cpp diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d447edff..6f3a01c4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -106,96 +106,6 @@ jobs: 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 @@ -203,7 +113,7 @@ jobs: strategy: fail-fast: false matrix: - emsdk_ver: ["2.0.34", "3.1.3"] + emsdk_ver: ["3.1.45"] steps: - uses: actions/checkout@v2 @@ -224,7 +134,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 @@ -241,4 +151,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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 216f67b2..dc22bc38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -295,8 +295,8 @@ if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD) 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) + target_link_libraries(xcpp_wasm PRIVATE xeus-lite) + xeus_cpp_set_kernel_options(xcpp_wasm) xeus_wasm_compile_options(xcpp_wasm) xeus_wasm_link_options(xcpp_wasm "web,worker") endif() diff --git a/environment-wasm-host.yml b/environment-wasm-host.yml index d9aa32b1..4bb96d48 100644 --- a/environment-wasm-host.yml +++ b/environment-wasm-host.yml @@ -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 diff --git a/include/xeus-cpp/xinterpreter_wasm.hpp b/include/xeus-cpp/xinterpreter_wasm.hpp new file mode 100644 index 00000000..340c52b3 --- /dev/null +++ b/include/xeus-cpp/xinterpreter_wasm.hpp @@ -0,0 +1,32 @@ +/************************************************************************************ + * 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(); + + private: + + void configure_impl() override; + }; +} + +#endif diff --git a/src/main_emscripten_kernel.cpp b/src/main_emscripten_kernel.cpp index 200a58e3..e25f8960 100644 --- a/src/main_emscripten_kernel.cpp +++ b/src/main_emscripten_kernel.cpp @@ -13,11 +13,11 @@ #include -#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("xkernel"); } diff --git a/src/xinterpreter_wasm.cpp b/src/xinterpreter_wasm.cpp new file mode 100644 index 00000000..6397c713 --- /dev/null +++ b/src/xinterpreter_wasm.cpp @@ -0,0 +1,30 @@ +/************************************************************************************ + * 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) + + wasm_interpreter::~wasm_interpreter() + { + } + + void wasm_interpreter::configure_impl() + { + interpreter::configure_impl(); + } +} From 8c7ad92b1543a11e4747c4cae55df40858b99c22 Mon Sep 17 00:00:00 2001 From: anutosh491 Date: Tue, 2 Jan 2024 15:14:45 +0530 Subject: [PATCH 2/4] Empty commit message From bff4fb1641efcb3ef36603cf90cedf59b9398c41 Mon Sep 17 00:00:00 2001 From: anutosh491 Date: Tue, 2 Jan 2024 16:00:02 +0530 Subject: [PATCH 3/4] fixing windows build --- .github/workflows/main.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6f3a01c4..0d08c58b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -99,13 +99,6 @@ 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: runs-on: ubuntu-latest From 5a5aca548de695ac1bcbdc89a8ab9ededae9fc26 Mon Sep 17 00:00:00 2001 From: Thorsten Beier Date: Wed, 3 Jan 2024 13:22:04 +0100 Subject: [PATCH 4/4] Wasm (#1) * make compatible with most recent lite requirements * use same binary names * added missing destructors * remove superflous moves * actually compiling wasm interpreter.. --- CMakeLists.txt | 38 +++++++++++++++----------- include/xeus-cpp/xinterpreter_wasm.hpp | 5 +--- include/xeus-cpp/xmagics.hpp | 2 ++ src/xinterpreter.cpp | 6 ++-- src/xinterpreter_wasm.cpp | 7 ----- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc22bc38..217d24b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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() @@ -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() @@ -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 ) @@ -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() @@ -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() @@ -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-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 @@ -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") @@ -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 - "$/xcpp_wasm.js" - "$/xcpp_wasm.wasm" + "$/xcpp.js" + "$/xcpp.wasm" DESTINATION ${CMAKE_INSTALL_BINDIR}) endif () diff --git a/include/xeus-cpp/xinterpreter_wasm.hpp b/include/xeus-cpp/xinterpreter_wasm.hpp index 340c52b3..2bb7839c 100644 --- a/include/xeus-cpp/xinterpreter_wasm.hpp +++ b/include/xeus-cpp/xinterpreter_wasm.hpp @@ -21,11 +21,8 @@ namespace xcpp public: wasm_interpreter(); - virtual ~wasm_interpreter(); + virtual ~wasm_interpreter() = default; - private: - - void configure_impl() override; }; } diff --git a/include/xeus-cpp/xmagics.hpp b/include/xeus-cpp/xmagics.hpp index 24ac1891..4be7d4e1 100644 --- a/include/xeus-cpp/xmagics.hpp +++ b/include/xeus-cpp/xmagics.hpp @@ -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; }; diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index bc6587f0..c8364e43 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -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; } } @@ -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; } } @@ -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() diff --git a/src/xinterpreter_wasm.cpp b/src/xinterpreter_wasm.cpp index 6397c713..7746c7b6 100644 --- a/src/xinterpreter_wasm.cpp +++ b/src/xinterpreter_wasm.cpp @@ -18,13 +18,6 @@ namespace xcpp wasm_interpreter::wasm_interpreter() : interpreter(0, nullptr) - - wasm_interpreter::~wasm_interpreter() - { - } - - void wasm_interpreter::configure_impl() { - interpreter::configure_impl(); } }