Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] supporting cppyy #248

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ set(XEUS_CPP_SRC
src/xoptions.cpp
src/xparser.cpp
src/xutils.cpp

# magics
src/xmagics/pythonexec.cpp
)

if(NOT EMSCRIPTEN)
Expand Down Expand Up @@ -377,6 +380,31 @@ macro(xeus_cpp_create_target target_name linkage output_name)
target_link_libraries(${target_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
endif()

# Python Stuff
if(NOT EMSCRIPTEN)
find_package(Python COMPONENTS Interpreter Development)

target_include_directories(${target_name} PRIVATE ${Python_INCLUDE_DIRS})
target_link_libraries(${target_name} PUBLIC ${Python_LIBRARIES})
target_compile_options(${target_name} PRIVATE ${Python_CFLAGS_OTHER})
else()
set(Python_INCLUDE_DIRS
"${CMAKE_PREFIX_PATH}/include/python3.11")
set(Python_LIBRARIES
"python3.11"
"m"
"bz2"
"libz.a"
"sqlite3"
"ffi"
)
set(Python_CFLAGS_OTHER "-sUSE_ZLIB=1" "-sUSE_BZIP2=1")

target_include_directories(${target_name} PRIVATE ${Python_INCLUDE_DIRS})
target_link_libraries(${target_name} PUBLIC ${Python_LIBRARIES})
target_compile_options(${target_name} PRIVATE ${Python_CFLAGS_OTHER})
endif()

endmacro()

# xeus-cpp-headers
Expand Down Expand Up @@ -437,18 +465,28 @@ endif()
if(EMSCRIPTEN)
include(WasmBuildOptions)
find_package(xeus-lite REQUIRED)
link_directories(BEFORE "${CMAKE_INSTALL_PREFIX}/lib")
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")
# TODO: Remove the exported runtime methods
# after the next xeus release.
# , \"ffi_call\", \"ffi_closure_alloc\", \"ffi_prep_cif\", \"ffi_prep_cif_var\", \"ffi_prep_closure_loc\", \"ffi_type_double\", \"ffi_type_float\", \"ffi_type_longdouble\", \"ffi_type_pointer\", \"ffi_type_sint16\", \"ffi_type_sint32\", \"ffi_type_sint64\", \"ffi_type_sint8\", \"ffi_type_uint16\", \"ffi_type_uint32\", \"ffi_type_uint64\", \"ffi_type_uint8\", \"ffi_type_void\"
target_link_options(xcpp
PUBLIC "SHELL: -s EXPORTED_RUNTIME_METHODS='[\"FS\",\"PATH\",\"LDSO\",\"loadDynamicLibrary\",\"ERRNO_CODES\"]'"
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
PUBLIC "SHELL: --preload-file ${CPyCppyy_DIR}/include/CPyCppyy@/include/CPyCppyy"
PUBLIC "SHELL: --preload-file ${CPyCppyy_BUILD_DIR}/libcppyy.so@/lib/python3.11/site-packages/libcppyy.so"
PUBLIC "SHELL: --preload-file ${Cppyy_Backend_DIR}/python/cppyy_backend@/lib/python3.11/site-packages/cppyy_backend"
PUBLIC "SHELL: --preload-file ${Cppyy_DIR}/python/cppyy@/lib/python3.11/site-packages/cppyy"
PUBLIC "SHELL: --preload-file ${Cppyy_DIR}/python/cppyy_compat@/lib/python3.11/site-packages/cppyy_compat"
PUBLIC "SHELL: --preload-file ${CMAKE_PREFIX_PATH}/lib/python3.11@/lib/python3.11"
PUBLIC "SHELL: --preload-file ${CMAKE_PREFIX_PATH}/include/clang@/include/clang"
PUBLIC "SHELL: --preload-file ${CMAKE_PREFIX_PATH}/include/clang-c@/include/clang-c"
PUBLIC "SHELL: --post-js ${CMAKE_CURRENT_SOURCE_DIR}/wasm_patches/post.js"
)
)
# 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).
Expand Down
5 changes: 5 additions & 0 deletions environment-wasm-host.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ dependencies:
- CppInterOp>=1.5.0
- cpp-argparse
- pugixml
- python=3.11
- bzip2
- zlib
- libffi
- sqlite
3 changes: 2 additions & 1 deletion src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "xmagics/os.hpp"
#include "xmagics/xassist.hpp"
#endif
#include "xmagics/pythonexec.hpp"
#include "xparser.hpp"
#include "xsystem.hpp"

Expand Down Expand Up @@ -371,7 +372,7 @@ __get_cxx_version ()
// executable(m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit",
// timeit(&m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("python", pythonexec());
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("python", pythonexec());
#ifndef EMSCRIPTEN
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("xassist", xassist());
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
Expand Down
86 changes: 86 additions & 0 deletions src/xmagics/pythonexec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/************************************************************************************
* Copyright (c) 2025, xeus-cpp contributors *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/

#include "pythonexec.hpp"
#include "../xparser.hpp"
#include <string>

#include "Python.h"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: included header Python.h is not used directly [misc-include-cleaner]

Suggested change

#include "clang/Interpreter/CppInterOp.h"

namespace xcpp {
bool pythonexec::is_initalized = false;

void pythonexec::operator()([[maybe_unused]] const std::string& line, const std::string& cell) {

if (!is_initalized)
initialize();
if (!is_initalized) {
// initializing failed
std::cout << Cpp::EndStdStreamCapture();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::cout" is directly included [misc-include-cleaner]

src/xmagics/pythonexec.cpp:10:

- #include <string>
+ #include <iostream>
+ #include <string>

std::cerr << Cpp::EndStdStreamCapture();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::cerr" is directly included [misc-include-cleaner]

            std::cerr << Cpp::EndStdStreamCapture();
                 ^


std::cerr << "Failed to Initialize Python\n";
return;
}

std::string code = trim(cell);
if (code.empty())
return;

Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Cpp::BeginStdStreamCapture(Cpp::kStdOut);

PyRun_SimpleString(code.c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "PyRun_SimpleString" is directly included [misc-include-cleaner]

src/xmagics/pythonexec.cpp:13:

+ #include "pythonrun.h"


std::cout << Cpp::EndStdStreamCapture();
std::cerr << Cpp::EndStdStreamCapture();
}

void pythonexec::initialize() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'initialize' can be made static [readability-convert-member-functions-to-static]

src/xmagics/pythonexec.hpp:24:

-         void initialize();
+         static void initialize();

#ifdef EMSCRIPTEN
PyStatus status;

PyConfig config;
PyConfig_InitPythonConfig(&config);
static const std::wstring prefix(L"/");
config.base_prefix = const_cast<wchar_t*>(prefix.c_str());
config.base_exec_prefix = const_cast<wchar_t*>(prefix.c_str());
config.prefix = const_cast<wchar_t*>(prefix.c_str());
config.exec_prefix = const_cast<wchar_t*>(prefix.c_str());

status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
// TODO: initialization failed, propagate error
PyConfig_Clear(&config);
is_initalized = false;
return;
}
PyConfig_Clear(&config);
#else
Py_Initialize();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Py_Initialize" is directly included [misc-include-cleaner]

src/xmagics/pythonexec.cpp:13:

+ #include "pylifecycle.h"

#endif

PyRun_SimpleString("import sys\nsys.path.append('')"); // add current directory to PYTHONPATH

// // Import cppyy module
// PyObject* cppyyModule = PyImport_ImportModule("cppyy");
// if (!cppyyModule) {
// PyErr_Print();
// Py_Finalize();
// return; // Handle import error as needed
// }

// PyObject* mainModule = PyImport_AddModule("__main__");
// PyObject_SetAttrString(mainModule, "cppyy", cppyyModule);
// Py_XDECREF(cppyyModule);

is_initalized = true;
}
} // namespace xcpp
30 changes: 30 additions & 0 deletions src/xmagics/pythonexec.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/************************************************************************************
* Copyright (c) 2025, xeus-cpp contributors *
* *
* 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_PYTHONEXEC_MAGIC_HPP
#define XEUS_CPP_PYTHONEXEC_MAGIC_HPP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: header guard does not follow preferred style [llvm-header-guard]

Suggested change
#define XEUS_CPP_PYTHONEXEC_MAGIC_HPP
#ifndef GITHUB_WORKSPACE_SRC_XMAGICS_PYTHONEXEC_HPP
#define GITHUB_WORKSPACE_SRC_XMAGICS_PYTHONEXEC_HPP

src/xmagics/pythonexec.hpp:29:

- #endif //XEUS_CPP_PYTHONEXEC_MAGIC_HPP
+ #endif // GITHUB_WORKSPACE_SRC_XMAGICS_PYTHONEXEC_HPP


#include <string>

#include "xeus-cpp/xmagics.hpp"

namespace xcpp
{
class pythonexec : public xmagic_cell
{
public:
XEUS_CPP_API
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "XEUS_CPP_API" is directly included [misc-include-cleaner]

src/xmagics/pythonexec.hpp:13:

- #include "xeus-cpp/xmagics.hpp"
+ #include "xeus-cpp/xeus_cpp_config.hpp"
+ #include "xeus-cpp/xmagics.hpp"

void operator()(const std::string& line, const std::string& cell) override;
private:
static bool is_initalized;
void initialize();

};
} // namespace xcpp

#endif //XEUS_CPP_PYTHONEXEC_MAGIC_HPP
Loading