diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..0dab1ed1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +build/**/* +build diff --git a/CMakeLists.txt b/CMakeLists.txt index c53e0d7b..8397ab5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,10 +35,50 @@ if (NOT DEFINED XEUS_CPP_KERNELSPEC_PATH) set(XEUS_CPP_KERNELSPEC_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/") endif () -configure_file ( - "${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp/kernel.json.in" - "${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp/kernel.json" -) +set(CMAKE_CPLUS_INCLUDE_PATH "$ENV{CPLUS_INCLUDE_PATH}") +set(CMAKE_PATH "$ENV{PATH}") +set(CMAKE_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}") +set(CMAKE_PYTHONPATH "$ENV{PYTHONPATH}") +set(CMAKE_VENV_PATH "$ENV{VENV}") + +message(STATUS "Debug: Replace in kernels") +message(STATUS "Debug: CMAKE_CPLUS_INCLUDE_PATH=${CMAKE_CPLUS_INCLUDE_PATH}") +message(STATUS "Debug: CMAKE_PATH=${CMAKE_PATH}") +message(STATUS "Debug: CMAKE_LD_LIBRARY_PATH=${CMAKE_LD_LIBRARY_PATH}") +message(STATUS "Debug: CMAKE_PYTHONPATH=${CMAKE_PYTHONPATH}") +message(STATUS "Debug: CMAKE_VENV_PATH=${CMAKE_VENV_PATH}") +message(STATUS "Debug: CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}") +message(STATUS "Debug: CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}") + + +function(configure_kernel kernel) + configure_file ( + "${CMAKE_CURRENT_SOURCE_DIR}/${kernel}/kernel.json.in" + "${CMAKE_CURRENT_BINARY_DIR}/${kernel}/kernel.json" + ) + file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/${kernel}/*.png") + foreach(file ${files}) + configure_file( + "${file}" + "${CMAKE_CURRENT_BINARY_DIR}/${kernel}/" + COPYONLY + ) + endforeach() + file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/${kernel}/*.svg") + foreach(file ${files}) + configure_file( + "${file}" + "${CMAKE_CURRENT_BINARY_DIR}/${kernel}/" + COPYONLY + ) + endforeach() +endfunction() + +file(GLOB _kernels LIST_DIRECTORIES true RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "share/jupyter/kernels/*") +foreach(_kernel IN LISTS _kernels) + message("Configure kernel: ${_kernel}") + configure_kernel("${_kernel}") +endforeach() # Build options # ============= @@ -90,7 +130,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" if(NOT XEUS_CPP_EMSCRIPTEN_WASM_BUILD) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused-parameter -Wextra -Wreorder") endif() - + CHECK_CXX_COMPILER_FLAG("-std=c++17" HAS_CPP_17_FLAG) if (HAS_CPP_17_FLAG) @@ -105,9 +145,12 @@ if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD) set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"") endif() -find_package(Clang REQUIRED) +#find_package(Clang REQUIRED) +find_package(CppInterOp REQUIRED CONFIG PATHS "${CPPINTEROP_DIR}" "${CPPINTEROP_DIR}/lib") find_package(argparse REQUIRED) find_package(pugixml REQUIRED) +##set(Python_FIND_VIRTUALENV ONLY) +##find_package(Python COMPONENTS Interpreter Development) # Source files # ============ @@ -190,7 +233,6 @@ macro(xeus_cpp_set_kernel_options target_name) find_package(Threads) target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT}) endif() - endmacro() # Common macro for shared and static library @@ -226,7 +268,8 @@ macro(xeus_cpp_create_target target_name linkage output_name) set(XEUS_CPP_XEUS_TARGET xeus-static) endif () - target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangInterpreter pugixml argparse::argparse xtl) +## target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangInterpreter pugixml argparse::argparse xtl) + target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl) if (WIN32 OR CYGWIN) # elseif (APPLE) @@ -236,7 +279,7 @@ macro(xeus_cpp_create_target target_name linkage output_name) find_package(Threads) # TODO: add Threads as a dependence of xeus-static? target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT}) endif() - + endmacro() # xeus-cpp-headers @@ -283,6 +326,23 @@ if (XEUS_CPP_BUILD_EXECUTABLE) xeus_cpp_set_common_options(xcpp) xeus_cpp_set_kernel_options(xcpp) target_link_libraries(xcpp PRIVATE xeus-zmq) + +##set_target_properties(xcpp PROPERTIES +## ENABLE_EXPORTS 1 +## CXX_STANDARD ${CMAKE_CXX_STANDARD} +##) +##target_link_libraries(xcpp PUBLIC xeus-cpp pthread Python::Python) +## +###TODO: We may be need sse RPATH +###set_target_properties(xcpp clangCppInterOp PROPERTIES +### INSTALL_RPATH_USE_LINK_PATH TRUE +###) +##if(APPLE) +## target_link_libraries(xcpp PUBLIC -Wl,-w -Wl,-bind_at_load -Wl,-undefined,dynamic_lookup) +##elseif(NOT MSVC) +## target_link_libraries(xcpp PUBLIC -Wl,--unresolved-symbols=ignore-in-object-files) +##endif() + endif() if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD) @@ -318,18 +378,18 @@ endif () # Install xcpp if (XEUS_CPP_BUILD_EXECUTABLE) install(TARGETS xcpp - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +# RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + RUNTIME DESTINATION ${CMAKE_VENV_PATH}/bin) # Configuration and data directories for jupyter and xeus-cpp set(XJUPYTER_DATA_DIR "share/jupyter" CACHE STRING "Jupyter data directory") - # Install xcpp Jupyter kernelspec - set(KERNELSPEC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels) + set(KERNELSPEC_DIR ${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels) install(DIRECTORY ${KERNELSPEC_DIR} DESTINATION ${XJUPYTER_DATA_DIR} +# DESTINATION ${CMAKE_INSTALL_PREFIX}/${XJUPYTER_DATA_DIR} PATTERN "*.in" EXCLUDE) - # Extra path for installing Jupyter kernelspec if (XEXTRA_JUPYTER_DATA_DIR) install(DIRECTORY ${KERNELSPEC_DIR} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..bf8833a7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,269 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +# https://hub.docker.com/r/jupyter/base-notebook/tags +ARG BASE_CONTAINER=jupyter/base-notebook +ARG BASE_TAG=latest +ARG BUILD_TYPE=Release + +FROM $BASE_CONTAINER:$BASE_TAG + +LABEL maintainer="Xeus-cpp Project" + +SHELL ["/bin/bash", "--login", "-o", "pipefail", "-c"] + +USER root + +ENV TAG="$BASE_TAG" + +ENV LC_ALL=en_US.UTF-8 \ + LANG=en_US.UTF-8 \ + LANGUAGE=en_US.UTF-8 + +# Install all OS dependencies for notebook server that starts but lacks all +# features (e.g., download as all possible file formats) +RUN \ + set -x && \ + apt-get update --yes && \ + apt-get install --yes --no-install-recommends pciutils && \ + export _CUDA_="$(lspci -nn | grep '\[03' | grep NVIDIA)" && \ + apt-get install --yes --no-install-recommends \ + #fonts-liberation, pandoc, run-one are inherited from base-notebook container image + # Other "our" apt installs + unzip \ + curl \ + jq \ + ###libomp-dev \ + # Other "our" apt installs (development and testing) + build-essential \ + git \ + nano-tiny \ + less \ + gdb valgrind \ + emacs \ + # CUDA + #cuda \ + $([ -n "$_CUDA_" ] && echo nvidia-cuda-toolkit) \ + && \ + apt-get clean && rm -rf /var/lib/apt/lists/* && \ + echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \ + locale-gen + +ENV LC_ALL=en_US.UTF-8 \ + LANG=en_US.UTF-8 \ + LANGUAGE=en_US.UTF-8 + +# Create alternative for nano -> nano-tiny +#RUN update-alternatives --install /usr/bin/nano nano /bin/nano-tiny 10 + +USER ${NB_UID} + +# Copy git repository to home directory of container +COPY --chown=${NB_UID}:${NB_GID} . "${HOME}"/ + +EXPOSE 8888 + +# Configure container startup +CMD ["start-notebook.sh", "--debug", "&>/home/jovyan/log.txt"] + +USER root + +# Fix start-notebook.sh +RUN sed -i '2 i source /home/jovyan/.conda.init && conda activate .venv' /usr/local/bin/start-notebook.sh + +### Make /home/runner directory and fix permisions +##RUN mkdir /home/runner && fix-permissions /home/runner + +# Switch back to jovyan to avoid accidental container runs as root +USER ${NB_UID} + +ENV NB_PYTHON_PREFIX=${CONDA_DIR} \ + KERNEL_PYTHON_PREFIX=${CONDA_DIR} \ +# CPLUS_INCLUDE_PATH="${CONDA_DIR}/include:/home/${NB_USER}/include:/home/runner/work/xeus-clang-repl/xeus-clang-repl/clang-dev/clang/include:/home/jovyan/clad/include:/home/jovyan/CppInterOp/include" + CPLUS_INCLUDE_PATH="${CONDA_DIR}/include:/home/${NB_USER}/include:/home/jovyan/clad/include:/home/jovyan/CppInterOp/include" + +WORKDIR "${HOME}" + +# CUDA +ENV NVIDIA_VISIBLE_DEVICES=all +ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility +ENV NVIDIA_REQUIRE_CUDA "cuda>=12.1.1 driver>=530" + +# VENV + +# Jupyter Notebook, Lab, and Hub are installed in base image +# ReGenerate a notebook server config +# Cleanup temporary files +# Correct permissions +# Do all this in a single RUN command to avoid duplicating all of the +# files across image layers when the permissions change +#RUN mamba update --all --quiet --yes -c conda-forge && \ +RUN \ + set -x && \ + # setup virtual environment + mamba create -y -n .venv python=3.10.6 && \ + # + #echo "echo \"@ @ @ PROFILE @ @ @ \"" >> ~/.profile && \ + #echo "echo \"@ @ @ BASHRC @ @ @ \"" >> /home/jovyan/.bashrc && \ + mv /home/jovyan/.bashrc /home/jovyan/.bashrc.tmp && \ + touch /home/jovyan/.bashrc && \ + conda init bash && \ + mv /home/jovyan/.bashrc /home/jovyan/.conda.init && \ + mv /home/jovyan/.bashrc.tmp /home/jovyan/.bashrc && \ + conda init bash && \ + echo "source /home/jovyan/.conda.init && conda activate .venv" >> /home/jovyan/.bashrc && \ + # + source /home/jovyan/.conda.init && \ + conda activate .venv && \ + fix-permissions "${CONDA_DIR}" && \ + # + mamba install --quiet --yes -c conda-forge \ + # notebook, jpyterhub, jupyterlab are inherited from base-notebook container image + # Other "our" conda installs + # + # Build dependencies + make \ + cmake \ + cxx-compiler \ + # Host dependencies + 'xeus-zmq>=1.0.2,<2.0' \ + nlohmann_json \ + cppzmq \ + xtl \ + 'clangdev>=17' \ + pugixml \ + cpp-argparse \ + zlib \ + # + ipykernel \ + # Test dependencies + pytest \ + 'jupyter_kernel_test>=0.4.3' \ + nbval \ + pytest-rerunfailures \ + && \ + hash -r && \ + pip install ipython && \ + #rm /home/jovyan/.jupyter/jupyter_notebook_config.py && \ + jupyter notebook --generate-config -y && \ + mamba clean --all -f -y && \ + npm cache clean --force && \ + jupyter lab clean && \ + rm -rf "/home/${NB_USER}/.cache/yarn" && \ + fix-permissions "${CONDA_DIR}" && \ + fix-permissions "/home/${NB_USER}" + +### Post Build +RUN \ + set -x && \ + source /home/jovyan/.conda.init && \ + conda activate .venv && \ + source /home/jovyan/.conda.init && \ + conda activate .venv && \ + # + export VENV=${CONDA_DIR}/envs/.venv && \ + export PATH_TO_LLVM_BUILD=${VENV} && \ + #export PATH=${VENV}/bin:${CONDA_DIR}/bin:$PATH_TO_LLVM_BUILD/bin:$PATH && \ + export PATH=${VENV}/bin:${CONDA_DIR}/bin:$PATH && \ + export LD_LIBRARY_PATH=$PATH_TO_LLVM_BUILD/lib:$LD_LIBRARY_PATH && \ + echo "export VENV=$VENV" >> ~/.profile && \ + echo "export PATH=$PATH" >> ~/.profile && \ + echo "export EDITOR=emacs" >> ~/.profile && \ + # + # Build CppInterOp + # + sys_incs=$(LC_ALL=C c++ -xc++ -E -v /dev/null 2>&1 | LC_ALL=C sed -ne '/starts here/,/End of/p' | LC_ALL=C sed '/^ /!d' | cut -c2- | tr '\n' ':') && \ + export CPLUS_INCLUDE_PATH="${PATH_TO_LLVM_BUILD}/include/llvm:${PATH_TO_LLVM_BUILD}/include/clange:$CPLUS_INCLUDE_PATH:${sys_incs%:}" && \ + git clone https://github.com/compiler-research/CppInterOp.git && \ + export CB_PYTHON_DIR="$PWD/cppyy-backend/python" && \ + export CPPINTEROP_DIR="$CB_PYTHON_DIR/cppyy_backend" && \ + cd CppInterOp && \ + mkdir build && \ + cd build && \ + export CPPINTEROP_BUILD_DIR=$PWD && \ + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DUSE_CLING=OFF -DUSE_REPL=ON -DLLVM_DIR=$PATH_TO_LLVM_BUILD -DLLVM_CONFIG_EXTRA_PATH_HINTS=${PATH_TO_LLVM_BUILD}/lib -DLLVM_USE_LINKER=gold -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=$CPPINTEROP_DIR .. && \ + cmake --build . --parallel $(nproc --all) && \ + #make install -j$(nproc --all) + export CPLUS_INCLUDE_PATH="$CPPINTEROP_DIR/include:$CPLUS_INCLUDE_PATH" && \ + export LD_LIBRARY_PATH="${VENV}/lib:${CONDA_DIR}/lib:$CPPINTEROP_DIR/lib:$LD_LIBRARY_PATH" && \ + echo "export LD_LIBRARY_PATH=$CPPINTEROP_DIR/lib:$LD_LIBRARY_PATH" >> ~/.profile && \ + cd ../.. && \ + # + # Build and Install cppyy-backend + # + git clone https://github.com/compiler-research/cppyy-backend.git && \ + cd cppyy-backend && \ + mkdir -p $CPPINTEROP_DIR/lib build && cd build && \ + # Install CppInterOp + (cd $CPPINTEROP_BUILD_DIR && cmake --build . --target install --parallel $(nproc --all)) && \ + # Build and Install cppyy-backend + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCppInterOp_DIR=$CPPINTEROP_DIR .. && \ + cmake --build . --parallel $(nproc --all) && \ + cp libcppyy-backend.so $CPPINTEROP_DIR/lib/ && \ + cd ../.. && \ + # + # Build and Install CPyCppyy + # + # Install CPyCppyy + git clone https://github.com/compiler-research/CPyCppyy.git && \ + cd CPyCppyy && \ + mkdir build && cd build && \ + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE .. && \ + cmake --build . --parallel $(nproc --all) && \ + export CPYCPPYY_DIR=$PWD && \ + cd ../.. && \ + # + # Build and Install cppyy + # + # Install cppyy + git clone https://github.com/compiler-research/cppyy.git && \ + cd cppyy && \ + python -m pip install --upgrade . --no-deps && \ + cd .. && \ + # Run cppyy + #TODO: Fix cppyy path (/home/jovyan) to path to installed module + export PYTHONPATH=$PYTHONPATH:$CPYCPPYY_DIR:$CB_PYTHON_DIR:/home/jovyan && \ + echo "export PYTHONPATH=$PYTHONPATH" >> ~/.profile && \ + export CPLUS_INCLUDE_PATH="/home/jovyan/CPyCppyy/include/:$CPLUS_INCLUDE_PATH" && \ + # FIXME: Remove the hardcoded version of python here. + export CPLUS_INCLUDE_PATH="/home/jovyan/clad/include:$CPLUS_INCLUDE_PATH" && \ + export CPLUS_INCLUDE_PATH="${VENV}/include:${VENV}/include/python3.10:$CPLUS_INCLUDE_PATH" && \ + python -c "import cppyy" && \ + # + # Build and Install xeus-cpp + # + mkdir build && \ + cd build && \ + echo "export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH" >> ~/.profile && \ + ##echo "conda activate .venv" >> ~/.profile + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_PREFIX_PATH=$KERNEL_PYTHON_PREFIX -DCMAKE_INSTALL_PREFIX=$KERNEL_PYTHON_PREFIX -DCMAKE_INSTALL_LIBDIR=lib -DCPPINTEROP_DIR=$CPPINTEROP_BUILD_DIR .. && \ + make install -j$(nproc --all) && \ + cd .. && \ + # + # Build and Install Clad + # + git clone --depth=1 https://github.com/vgvassilev/clad.git && \ + cd clad && \ + mkdir build && \ + cd build && \ + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE .. -DClang_DIR=${PATH_TO_LLVM_BUILD}/lib/cmake/clang/ -DLLVM_DIR=${PATH_TO_LLVM_BUILD}/lib/cmake/llvm/ -DCMAKE_INSTALL_PREFIX=${CONDA_DIR} -DLLVM_EXTERNAL_LIT="$(which lit)" && \ + #make -j$(nproc --all) && \ + make && \ + make install && \ + # + # xtensor + # + git clone https://github.com/xtensor-stack/xtensor.git && \ + cd xtensor && \ + mkdir build && cd build && \ + cmake -DCMAKE_INSTALL_PREFIX=$KERNEL_PYTHON_PREFIX .. && \ + make install && \ + # + # Fixes and patches + # + # Web password and token set to "" + echo "c.NotebookApp.token = ''" >> /home/jovyan/.jupyter/jupyter_notebook_config.py && \ + echo "c.NotebookApp.password = ''" >> /home/jovyan/.jupyter/jupyter_notebook_config.py && \ + # Patch /opt/conda/share/jupyter/kernels/python3/kernel.json to use .venv + k="/opt/conda/share/jupyter/kernels/python3/kernel.json" && \ + jq ".argv[0] = \"${VENV}/bin/python\"" $k > $k.$$.tmp && mv $k.$$.tmp $k diff --git a/bash-docker.sh b/bash-docker.sh new file mode 100755 index 00000000..52d413d2 --- /dev/null +++ b/bash-docker.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +if [ "$#" -eq 0 ]; then + user="root" +else + user="$1" +fi + +docker exec -u "$user" -t -i xeus-cpp-c /bin/bash --login diff --git a/include/xcpp/xdisplay.hpp b/include/xcpp/xdisplay.hpp index 1cb1d443..05acdccf 100644 --- a/include/xcpp/xdisplay.hpp +++ b/include/xcpp/xdisplay.hpp @@ -14,6 +14,8 @@ #include "xcpp/xmime.hpp" +#include "xeus/xinterpreter.hpp" + namespace nl = nlohmann; namespace xcpp diff --git a/include/xeus-cpp/xinterpreter.hpp b/include/xeus-cpp/xinterpreter.hpp index bec47613..833e9f3e 100644 --- a/include/xeus-cpp/xinterpreter.hpp +++ b/include/xeus-cpp/xinterpreter.hpp @@ -16,7 +16,8 @@ #include #include -#include +//#include +#include "clang/Interpreter/CppInterOp.h" #include @@ -78,7 +79,7 @@ namespace xcpp std::string get_stdopt(int argc, const char* const* argv); - std::unique_ptr m_interpreter; +// std::unique_ptr m_interpreter; std::string m_version; diff --git a/remove-docker.sh b/remove-docker.sh new file mode 100755 index 00000000..15a5b419 --- /dev/null +++ b/remove-docker.sh @@ -0,0 +1,3 @@ +#!/bin/bash +docker rm xeus-cpp-c -f +docker rmi xeus-cpp diff --git a/run-docker.sh b/run-docker.sh new file mode 100755 index 00000000..e64d7116 --- /dev/null +++ b/run-docker.sh @@ -0,0 +1,15 @@ +#!/bin/bash +docker container run --rm -i hadolint/hadolint hadolint - < Dockerfile + +jupyter-repo2docker \ + --no-run \ + --user-name=jovyan \ + --image-name xeus-cpp \ + . + +#docker run --gpus all --publish 8888:8888 --name xeus-cpp-c -i -t xeus-cpp "start-notebook.sh" +docker run --rm --runtime=nvidia --gpus all --publish 8888:8888 --name xeus-cpp-c -i -t xeus-cpp "start-notebook.sh" + +# --editable \ +# --ref InterOpIntegration \ +# https://github.com/alexander-penev/xeus-cpp.git \ diff --git a/share/jupyter/kernels/clad-xcpp17/kernel.json.in b/share/jupyter/kernels/clad-xcpp17/kernel.json.in new file mode 100644 index 00000000..bd8836ad --- /dev/null +++ b/share/jupyter/kernels/clad-xcpp17/kernel.json.in @@ -0,0 +1,18 @@ +{ + "display_name": "C++17 (+Clad)", + "env": { + "CPLUS_INCLUDE_PATH":"@CMAKE_CPLUS_INCLUDE_PATH@", + "PATH":"@CMAKE_PATH@", + "LD_LIBRARY_PATH":"@CMAKE_LD_LIBRARY_PATH@", + "PYTHONPATH":"@CMAKE_PYTHONPATH@" + }, + "argv": [ + "@CMAKE_VENV_PATH@/bin/xcpp", + "-f", + "{connection_file}", + "-fplugin=/opt/conda/lib/clad.so", + "-std=c++17"@CMAKE_OMP@ + ], + "language": "C++17", + "metadata": {"debugger": false} +} diff --git a/share/jupyter/kernels/clad-xcpp17/logo-32x32.png b/share/jupyter/kernels/clad-xcpp17/logo-32x32.png new file mode 100644 index 00000000..c09c4585 Binary files /dev/null and b/share/jupyter/kernels/clad-xcpp17/logo-32x32.png differ diff --git a/share/jupyter/kernels/clad-xcpp17/logo-64x64.png b/share/jupyter/kernels/clad-xcpp17/logo-64x64.png new file mode 100644 index 00000000..396c2446 Binary files /dev/null and b/share/jupyter/kernels/clad-xcpp17/logo-64x64.png differ diff --git a/share/jupyter/kernels/clad-xcpp17/logo-svg.svg b/share/jupyter/kernels/clad-xcpp17/logo-svg.svg new file mode 100644 index 00000000..5e117077 --- /dev/null +++ b/share/jupyter/kernels/clad-xcpp17/logo-svg.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + diff --git a/share/jupyter/kernels/cppinterop-xcpp17/kernel.json.in b/share/jupyter/kernels/cppinterop-xcpp17/kernel.json.in new file mode 100644 index 00000000..904a375c --- /dev/null +++ b/share/jupyter/kernels/cppinterop-xcpp17/kernel.json.in @@ -0,0 +1,18 @@ +{ + "display_name": "CppInterOp (C++17)", + "env": { + "CPLUS_INCLUDE_PATH":"@CMAKE_CPLUS_INCLUDE_PATH@", + "PATH":"@CMAKE_PATH@", + "LD_LIBRARY_PATH":"@CMAKE_LD_LIBRARY_PATH@", + "PYTHONPATH":"@CMAKE_PYTHONPATH@" + }, + "argv": [ + "@CMAKE_VENV_PATH@/bin/xcpp", + "-f", + "{connection_file}", + "-cuda", + "-std=c++17"@CMAKE_OMP@ + ], + "language": "CUDA", + "metadata": {"debugger": false} +} diff --git a/share/jupyter/kernels/omp-xcpp17/kernel.json.in b/share/jupyter/kernels/omp-xcpp17/kernel.json.in new file mode 100644 index 00000000..3583b275 --- /dev/null +++ b/share/jupyter/kernels/omp-xcpp17/kernel.json.in @@ -0,0 +1,18 @@ +{ + "display_name": "C++17 (+OpenMP)", + "env": { + "CPLUS_INCLUDE_PATH":"@CMAKE_CPLUS_INCLUDE_PATH@", + "PATH":"@CMAKE_PATH@", + "LD_LIBRARY_PATH":"@CMAKE_LD_LIBRARY_PATH@", + "PYTHONPATH":"@CMAKE_PYTHONPATH@" + }, + "argv": [ + "@CMAKE_VENV_PATH@/bin/xcpp", + "-f", + "{connection_file}", + "-fopenmp", + "-std=c++17" + ], + "language": "C++17", + "metadata": {"debugger": false} +} diff --git a/share/jupyter/kernels/omp-xcpp17/logo-32x32.png b/share/jupyter/kernels/omp-xcpp17/logo-32x32.png new file mode 100644 index 00000000..c09c4585 Binary files /dev/null and b/share/jupyter/kernels/omp-xcpp17/logo-32x32.png differ diff --git a/share/jupyter/kernels/omp-xcpp17/logo-64x64.png b/share/jupyter/kernels/omp-xcpp17/logo-64x64.png new file mode 100644 index 00000000..396c2446 Binary files /dev/null and b/share/jupyter/kernels/omp-xcpp17/logo-64x64.png differ diff --git a/share/jupyter/kernels/omp-xcpp17/logo-svg.svg b/share/jupyter/kernels/omp-xcpp17/logo-svg.svg new file mode 100644 index 00000000..5e117077 --- /dev/null +++ b/share/jupyter/kernels/omp-xcpp17/logo-svg.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + diff --git a/share/jupyter/kernels/xcpp/kernel.json.in b/share/jupyter/kernels/xcpp/kernel.json.in index f053f27c..556bd429 100644 --- a/share/jupyter/kernels/xcpp/kernel.json.in +++ b/share/jupyter/kernels/xcpp/kernel.json.in @@ -1,11 +1,17 @@ { - "display_name": "cpp 14 (xcpp)", + "display_name": "C++17 (xcpp)", + "env": { + "CPLUS_INCLUDE_PATH":"@CMAKE_CPLUS_INCLUDE_PATH@", + "PATH":"@CMAKE_PATH@", + "LD_LIBRARY_PATH":"@CMAKE_LD_LIBRARY_PATH@", + "PYTHONPATH":"@CMAKE_PYTHONPATH@" + }, "argv": [ - "@XEUS_CPP_KERNELSPEC_PATH@xcpp", + "@CMAKE_VENV_PATH@/bin/xcpp", "-f", - "{connection_file}" + "{connection_file}", + "-std=c++17"@CMAKE_OMP@ ], "language": "cpp", - "metadata": {"debugger": false - } + "metadata": {"debugger": false} } diff --git a/share/jupyter/kernels/xcpp11/kernel.json.in b/share/jupyter/kernels/xcpp11/kernel.json.in new file mode 100644 index 00000000..c7440e47 --- /dev/null +++ b/share/jupyter/kernels/xcpp11/kernel.json.in @@ -0,0 +1,17 @@ +{ + "display_name": "C++11", + "env": { + "CPLUS_INCLUDE_PATH":"@CMAKE_CPLUS_INCLUDE_PATH@", + "PATH":"@CMAKE_PATH@", + "LD_LIBRARY_PATH":"@CMAKE_LD_LIBRARY_PATH@", + "PYTHONPATH":"@CMAKE_PYTHONPATH@" + }, + "argv": [ + "@CMAKE_VENV_PATH@/bin/xcpp", + "-f", + "{connection_file}", + "-std=c++11"@CMAKE_OMP@ + ], + "language": "C++11", + "metadata": {"debugger": false} +} diff --git a/share/jupyter/kernels/xcpp11/logo-32x32.png b/share/jupyter/kernels/xcpp11/logo-32x32.png new file mode 100644 index 00000000..c09c4585 Binary files /dev/null and b/share/jupyter/kernels/xcpp11/logo-32x32.png differ diff --git a/share/jupyter/kernels/xcpp11/logo-64x64.png b/share/jupyter/kernels/xcpp11/logo-64x64.png new file mode 100644 index 00000000..396c2446 Binary files /dev/null and b/share/jupyter/kernels/xcpp11/logo-64x64.png differ diff --git a/share/jupyter/kernels/xcpp11/logo-svg.svg b/share/jupyter/kernels/xcpp11/logo-svg.svg new file mode 100644 index 00000000..5e117077 --- /dev/null +++ b/share/jupyter/kernels/xcpp11/logo-svg.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + diff --git a/share/jupyter/kernels/xcpp14/kernel.json.in b/share/jupyter/kernels/xcpp14/kernel.json.in new file mode 100644 index 00000000..3c9a47f4 --- /dev/null +++ b/share/jupyter/kernels/xcpp14/kernel.json.in @@ -0,0 +1,20 @@ +{ + "display_name": "C++14", + "env": { + "CPLUS_INCLUDE_PATH":"@CMAKE_CPLUS_INCLUDE_PATH@", + "PATH":"@CMAKE_PATH@", + "LD_LIBRARY_PATH":"@CMAKE_LD_LIBRARY_PATH@", + "PYTHONPATH":"@CMAKE_PYTHONPATH@" + }, + "argv": [ + "@CMAKE_VENV_PATH@/bin/xcpp", + "-f", + "{connection_file}", + "-std=c++14", + "-fno-exceptions", + "-O2", + "-v"@CMAKE_OMP@ + ], + "language": "C++14", + "metadata": {"debugger": false} +} diff --git a/share/jupyter/kernels/xcpp14/logo-32x32.png b/share/jupyter/kernels/xcpp14/logo-32x32.png new file mode 100644 index 00000000..c09c4585 Binary files /dev/null and b/share/jupyter/kernels/xcpp14/logo-32x32.png differ diff --git a/share/jupyter/kernels/xcpp14/logo-64x64.png b/share/jupyter/kernels/xcpp14/logo-64x64.png new file mode 100644 index 00000000..396c2446 Binary files /dev/null and b/share/jupyter/kernels/xcpp14/logo-64x64.png differ diff --git a/share/jupyter/kernels/xcpp14/logo-svg.svg b/share/jupyter/kernels/xcpp14/logo-svg.svg new file mode 100644 index 00000000..5e117077 --- /dev/null +++ b/share/jupyter/kernels/xcpp14/logo-svg.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + diff --git a/share/jupyter/kernels/xcpp17/kernel.json.in b/share/jupyter/kernels/xcpp17/kernel.json.in new file mode 100644 index 00000000..ee116fc5 --- /dev/null +++ b/share/jupyter/kernels/xcpp17/kernel.json.in @@ -0,0 +1,17 @@ +{ + "display_name": "C++17", + "env": { + "CPLUS_INCLUDE_PATH":"@CMAKE_CPLUS_INCLUDE_PATH@", + "PATH":"@CMAKE_PATH@", + "LD_LIBRARY_PATH":"@CMAKE_LD_LIBRARY_PATH@", + "PYTHONPATH":"@CMAKE_PYTHONPATH@" + }, + "argv": [ + "@CMAKE_VENV_PATH@/bin/xcpp", + "-f", + "{connection_file}", + "-std=c++17"@CMAKE_OMP@ + ], + "language": "C++17", + "metadata": {"debugger": false} +} diff --git a/share/jupyter/kernels/xcpp17/logo-32x32.png b/share/jupyter/kernels/xcpp17/logo-32x32.png new file mode 100644 index 00000000..c09c4585 Binary files /dev/null and b/share/jupyter/kernels/xcpp17/logo-32x32.png differ diff --git a/share/jupyter/kernels/xcpp17/logo-64x64.png b/share/jupyter/kernels/xcpp17/logo-64x64.png new file mode 100644 index 00000000..396c2446 Binary files /dev/null and b/share/jupyter/kernels/xcpp17/logo-64x64.png differ diff --git a/share/jupyter/kernels/xcpp17/logo-svg.svg b/share/jupyter/kernels/xcpp17/logo-svg.svg new file mode 100644 index 00000000..5e117077 --- /dev/null +++ b/share/jupyter/kernels/xcpp17/logo-svg.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main.cpp b/src/main.cpp index 8ebd0611..55906b34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,7 +60,7 @@ bool should_print_version(int argc, char* argv[]) return false; } -std::string extract_filename(int argc, char* argv[]) +std::string extract_filename(int& argc, char* argv[]) { std::string res = ""; for (int i = 0; i < argc; ++i) @@ -83,20 +83,16 @@ using interpreter_ptr = std::unique_ptr; interpreter_ptr build_interpreter(int argc, char** argv) { - int interpreter_argc = argc; // + 1; // ... - const char** interpreter_argv = new const char*[interpreter_argc]; - interpreter_argv[0] = "xeus-cpp"; - // Copy all arguments in the new array excepting the process name. - for (int i = 1; i < argc; i++) - { - interpreter_argv[i] = argv[i]; + std::vector interpreter_args; + for (int i = 1; i < argc; i++) { + if (argv[i] == "-f") { + i++; // skip the value of -f which is a json file. + continue; } - // std::string include_dir = std::string(LLVM_DIR) + std::string("/include"); // ... - // interpreter_argv[interpreter_argc - 1] = include_dir.c_str(); // ... - - interpreter_ptr interp_ptr = interpreter_ptr(new xcpp::interpreter(interpreter_argc, interpreter_argv)); - delete[] interpreter_argv; - return interp_ptr; + interpreter_args.push_back(argv[i]); + } + interpreter_ptr interp_ptr = interpreter_ptr(new xcpp::interpreter(interpreter_args.size(), interpreter_args.data())); + return interp_ptr; } int main(int argc, char* argv[]) @@ -167,8 +163,8 @@ int main(int argc, char* argv[]) xeus::make_xserver_zmq, xeus::make_in_memory_history_manager(), xeus::make_console_logger( - xeus::xlogger::msg_type, - xeus::make_file_logger(xeus::xlogger::content, "xeus.log") + xeus::xlogger::msg_type, + xeus::make_file_logger(xeus::xlogger::content, "xeus.log") ) ); diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 563dc539..e1a1e365 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -16,16 +16,16 @@ #include #include -#include -#include -#include -#include +//#include +//#include +//#include +//#include -#include -#include -#include -#include -#include +//#include +//#include +//#include +//#include +//#include #include @@ -45,6 +45,7 @@ #include "xsystem.hpp" +/* std::string DiagnosticOutput; llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); auto DiagPrinter = std::make_unique(DiagnosticsOS, new clang::DiagnosticOptions()); @@ -104,9 +105,11 @@ process_code(clang::Interpreter& Interp, const std::string& code, llvm::raw_stri } return false; } +*/ using Args = std::vector; +/* static std::unique_ptr create_interpreter(const Args& ExtraArgs = {}, clang::DiagnosticConsumer* Client = nullptr) { @@ -115,21 +118,30 @@ create_interpreter(const Args& ExtraArgs = {}, clang::DiagnosticConsumer* Client Args ClangArgs = {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"}; ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); - auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs)); + auto CI = llvm::cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs)); if (Client) { - CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false); + CI->getDiagnostics().setClient(Client, / *ShouldOwnClient=* /false); } - return cantFail(clang::Interpreter::create(std::move(CI))); + return llvm::cantFail(clang::Interpreter::create(std::move(CI))); +} +*/ +void* createInterpreter(const Args &ExtraArgs = {}) { + Args ClangArgs = {/*"-xc++"*/}; + ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); + // FIXME: We should process the kernel input options and conditionally pass + // the gpu args here. + return Cpp::CreateInterpreter(ClangArgs, {"-cuda"}); } +/* static void inject_symbol(llvm::StringRef LinkerMangledName, llvm::JITTargetAddress KnownAddr, clang::Interpreter& Interp) { using namespace llvm; using namespace llvm::orc; - auto Symbol = Interp.getSymbolAddress(LinkerMangledName); //, /*IncludeFromHost=*/true); + auto Symbol = Interp.getSymbolAddress(LinkerMangledName); //, / *IncludeFromHost=* /true); if (Error Err = Symbol.takeError()) { @@ -173,7 +185,8 @@ inject_symbol(llvm::StringRef LinkerMangledName, llvm::JITTargetAddress KnownAdd logAllUnhandledErrors(std::move(Err), errs(), "[IncrementalJIT] define() failed3: "); } } - +*/ +/* namespace utils { void AddIncludePath(llvm::StringRef Path, clang::HeaderSearchOptions& HOpts) @@ -191,7 +204,7 @@ namespace utils return; } - HOpts.AddPath(Path, clang::frontend::Angled, false /* IsFramework */, true /* IsSysRootRelative */); + HOpts.AddPath(Path, clang::frontend::Angled, false / * IsFramework * /, true / * IsSysRootRelative * /); if (HOpts.Verbose) { @@ -228,13 +241,14 @@ void AddIncludePath(clang::Interpreter& Interp, llvm::StringRef Path) } } } - +*/ using namespace std::placeholders; namespace xcpp { void interpreter::configure_impl() { +/* // todo: why is error_stream necessary std::string error_message; llvm::raw_string_ostream error_stream(error_message); @@ -243,11 +257,12 @@ namespace xcpp std::string code = "xeus::register_interpreter(static_cast((void*)" + std::to_string(intptr_t(this)) + "));"; process_code(*m_interpreter, code.c_str(), error_stream); +*/ } interpreter::interpreter(int argc, const char* const* argv) - : m_interpreter(std::move(create_interpreter(Args() /*argv + 1, argv + argc)*/, DiagPrinter.get()))) - , m_version(get_stdopt(argc, argv)) +// : m_interpreter(std::move(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() , p_cout_strbuf(nullptr) @@ -255,7 +270,9 @@ namespace xcpp , m_cout_buffer(std::bind(&interpreter::publish_stdout, this, _1)) , m_cerr_buffer(std::bind(&interpreter::publish_stderr, this, _1)) { + createInterpreter(Args(argv, argv + argc)); redirect_output(); + // Bootstrap the execution engine init_includes(); init_preamble(); init_magic(); @@ -308,12 +325,21 @@ namespace xcpp // Scope guard performing the temporary redirection of input requests. auto input_guard = input_redirection(allow_stdin); - std::string error_message; - llvm::raw_string_ostream error_stream(error_message); +// std::string error_message; +// llvm::raw_string_ostream error_stream(error_message); + std::string err; + std::string out; // Attempt normal evaluation try { - compilation_result = process_code(*m_interpreter, code, error_stream); +// compilation_result = process_code(*m_interpreter, code, error_stream); + Cpp::BeginStdStreamCapture(Cpp::kStdErr); + Cpp::BeginStdStreamCapture(Cpp::kStdOut); + //compilation_result = Cpp::Process(block.c_str()); + compilation_result = Cpp::Process(code.c_str()); + out = Cpp::EndStdStreamCapture(); + err = Cpp::EndStdStreamCapture(); + std::cout << out; } catch (std::exception& e) { @@ -331,11 +357,12 @@ namespace xcpp { errorlevel = 1; ename = "Error"; - evalue = error_stream.str(); +// evalue = error_stream.str(); + std::cerr << err; } - error_stream.str().clear(); - DiagnosticsOS.str().clear(); +// error_stream.str().clear(); +// DiagnosticsOS.str().clear(); // Flush streams std::cout << std::flush; @@ -536,8 +563,8 @@ namespace xcpp // Inject versions of printf and fprintf that output to std::cout // and std::cerr (see implementation above). - inject_symbol("printf", llvm::pointerToJITTargetAddress(printf_jit), *m_interpreter); - inject_symbol("fprintf", llvm::pointerToJITTargetAddress(fprintf_jit), *m_interpreter); +// inject_symbol("printf", llvm::pointerToJITTargetAddress(printf_jit), *m_interpreter); +// inject_symbol("fprintf", llvm::pointerToJITTargetAddress(fprintf_jit), *m_interpreter); } void interpreter::restore_output() @@ -561,7 +588,7 @@ namespace xcpp void interpreter::init_includes() { - AddIncludePath(*m_interpreter, xtl::prefix_path() + "/include/"); +// AddIncludePath(*m_interpreter, xtl::prefix_path() + "/include/"); } void interpreter::init_preamble()