Skip to content
Closed
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
14 changes: 8 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ execute_process(
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(_python_platlib)
set(CPPINTEROP_INSTALL_DIR "${_python_platlib}/cppjit_backend")
set(CPPINTEROP_INSTALL_PREFIX "${_python_platlib}")
else()
set(CPPINTEROP_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/cppjit_backend")
set(CPPINTEROP_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
endif()
set(CPPINTEROP_INSTALL_DIR "${CPPINTEROP_INSTALL_PREFIX}/cppjit_backend")

# Include cmake for CppInterOp config and build using ExternalProject.
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/AddCppInterOp.cmake)
Expand All @@ -114,11 +115,12 @@ set(INTEROP_SOURCES
add_library(cppjit SHARED ${CPYRT_SOURCES} ${INTEROP_SOURCES})
add_dependencies(cppjit CppInterOp)

# The exact library file the wrapper dlopens and the include dir the
# interpreter boot requires.
# The wrapper anchors these relative spellings at its own load location,
# falling back to the install prefix (see cppinterop_paths()).
target_compile_definitions(cppjit PRIVATE
CPPINTEROP_LIBRARY="${CPPINTEROP_INSTALL_DIR}/lib/libclangCppInterOp${CMAKE_SHARED_LIBRARY_SUFFIX}"
CPPINTEROP_INCLUDE_DIR="${CPPINTEROP_INSTALL_DIR}/include"
CPPINTEROP_INSTALL_PREFIX="${CPPINTEROP_INSTALL_PREFIX}"
CPPINTEROP_LIBRARY="cppjit_backend/lib/libclangCppInterOp${CMAKE_SHARED_LIBRARY_SUFFIX}"
CPPINTEROP_INCLUDE_DIR="cppjit_backend/include"
)

target_include_directories(cppjit PRIVATE
Expand Down
49 changes: 12 additions & 37 deletions python/cppjit/_cpython_cppjit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""CPython-specific touch-ups"""

import ctypes
import sys
import importlib.util

from . import _stdcpp_fix # noqa: F401

Expand All @@ -17,42 +17,17 @@
"_end_capture_stderr",
]

# the merged libcppjit extension is the backend: importing it loads the
# C++ runtime (no separate loader.load_cpp_backend() step, which would
# initialize the interpreter twice)
import libcppjit as _backend

# explicitly expose APIs from libcppjit
_w = ctypes.CDLL(_backend.__file__, ctypes.RTLD_GLOBAL)


# some beautification for inspect (only on p2)
if sys.hexversion < 0x3000000:
# TODO: this reliese on CPPOverload cooking up a func_code object, which atm
# is simply not implemented for p3 :/

# convince inspect that cppjit method proxies are possible drop-ins for python
# methods and classes for pydoc
import inspect

inspect._old_isfunction = inspect.isfunction

def isfunction(object):
if isinstance(object, _backend.CPPOverload) and not object.im_class:
return True
return inspect._old_isfunction(object)

inspect.isfunction = isfunction

inspect._old_ismethod = inspect.ismethod

def ismethod(object):
if isinstance(object, _backend.CPPOverload):
return True
return inspect._old_ismethod(object)

inspect.ismethod = ismethod
del isfunction, ismethod
# preload the merged extension with ctypes and run LoadCppInterOp() first,
# so the interpreter is ready before the extension module initializes
_spec = importlib.util.find_spec("libcppjit")
if _spec is None or not _spec.origin:
raise ImportError("cannot locate the libcppjit extension module")
_w = ctypes.CDLL(_spec.origin, ctypes.RTLD_GLOBAL)
if not _w.LoadCppInterOp():
raise RuntimeError("failed to load CppInterOp (LoadCppInterOp returned 0)")
del _spec

import libcppjit as _backend # noqa: E402


### template support ---------------------------------------------------------
Expand Down
Loading
Loading