Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cython3 support #444

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

Merged
merged 15 commits into from
Aug 16, 2023
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ cython_test.cpp
*.vcxproj
*.filters
symengine/lib/symengine_wrapper.cpp
symengine/lib/symengine_wrapper.pyx
symengine/lib/symengine_wrapper.pxd

# Config Files
symengine/lib/config.pxi
Expand Down
23 changes: 5 additions & 18 deletions cmake/FindCython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,13 @@ if(NOT CYTHON_INCLUDE_DIRECTORIES)
endif(NOT CYTHON_INCLUDE_DIRECTORIES)

# Cythonizes the .pyx files into .cpp file (but doesn't compile it)
macro(CYTHON_ADD_MODULE_PYX name)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd)
set(DEPENDS ${name}.pyx ${name}.pxd)
else(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd)
set(DEPENDS ${name}.pyx)
endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd)
macro(CYTHON_ADD_MODULE_PYX cpp_name pyx_name)
# Allow the user to specify dependencies as optional arguments
set(DEPENDS ${DEPENDS} ${ARGN})
add_custom_command(
OUTPUT ${name}.cpp
OUTPUT ${cpp_name}
COMMAND ${CYTHON_BIN}
ARGS ${CYTHON_FLAGS} -I ${CYTHON_INCLUDE_DIRECTORIES} -o ${name}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pyx
DEPENDS ${DEPENDS}
COMMENT "Cythonizing ${name}.pyx")
ARGS ${CYTHON_FLAGS} -I ${CYTHON_INCLUDE_DIRECTORIES} -o ${cpp_name} ${pyx_name}
DEPENDS ${DEPENDS} ${pyx_name}
COMMENT "Cythonizing ${pyx_name}")
endmacro(CYTHON_ADD_MODULE_PYX)

# Cythonizes and compiles a .pyx file
macro(CYTHON_ADD_MODULE name)
CYTHON_ADD_MODULE_PYX(${name})
# We need Python for this:
find_package(Python REQUIRED)
add_python_library(${name} ${name}.cpp ${ARGN})
endmacro(CYTHON_ADD_MODULE)
38 changes: 38 additions & 0 deletions cmake/preprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys


def main(input_name, output_name, replacements):
replacements = dict((item.split("=")[0], item.split("=")[1] == "True") for item in replacements)
with open(input_name, "r") as inp:
text = inp.readlines()

new_text = []
in_cond = [True]
nspaces = [0]
for i, line in enumerate(text):
if line.strip().startswith("IF"):
s = len(line) - len(line.lstrip())
while s <= nspaces[-1] and len(in_cond) > 1:
in_cond = in_cond[:-1]
nspaces = nspaces[:-1]

cond = line.lstrip()[3:-2]
in_cond.append(replacements[cond])
nspaces.append(s)
elif line.strip().startswith("ELSE"):
in_cond[-1] = not in_cond[-1] and in_cond[-2]

if len(line) > 1 and not line.strip().startswith(("IF", "ELSE")):
while len(in_cond) > 1 and (len(line) <= nspaces[-1] or not line.startswith(" "*nspaces[-1]) or line[nspaces[-1]] != " "):
in_cond = in_cond[:-1]
nspaces = nspaces[:-1]
if len(line) == 1:
new_text.append(line)
elif in_cond[-1] and not line.strip().startswith(("IF", "ELSE")):
new_text.append(line[4*(len(in_cond) - 1):])

with open(output_name, "w") as out:
out.writelines(new_text)

if __name__ == "__main__":
main(sys.argv[1], sys.argv[2], sys.argv[3:])
49 changes: 39 additions & 10 deletions symengine/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
set(SRC
symengine_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.cpp
pywrapper.cpp
)

configure_file(config.pxi.in config.pxi)
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})

include_directories(BEFORE ${python_wrapper_BINARY_DIR}/symengine/lib)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pxd
${PROJECT_SOURCE_DIR}/cmake/preprocess.py
COMMAND ${PYTHON_BIN} ${PROJECT_SOURCE_DIR}/cmake/preprocess.py
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pxd
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
HAVE_SYMENGINE_MPFR=${HAVE_SYMENGINE_MPFR}
HAVE_SYMENGINE_MPC=${HAVE_SYMENGINE_MPC}
HAVE_SYMENGINE_PIRANHA=${HAVE_SYMENGINE_PIRANHA}
HAVE_SYMENGINE_FLINT=${HAVE_SYMENGINE_FLINT}
HAVE_SYMENGINE_LLVM=${HAVE_SYMENGINE_LLVM}
HAVE_SYMENGINE_LLVM_LONG_DOUBLE=${HAVE_SYMENGINE_LLVM_LONG_DOUBLE}
COMMENT "Preprocessing symengine_wrapper.in.pxd"
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pyx
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pyx
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pxd
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
${PROJECT_SOURCE_DIR}/cmake/preprocess.py
COMMAND ${PYTHON_BIN} ${PROJECT_SOURCE_DIR}/cmake/preprocess.py
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pyx
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pyx
HAVE_SYMENGINE_MPFR=${HAVE_SYMENGINE_MPFR}
HAVE_SYMENGINE_MPC=${HAVE_SYMENGINE_MPC}
HAVE_SYMENGINE_PIRANHA=${HAVE_SYMENGINE_PIRANHA}
HAVE_SYMENGINE_FLINT=${HAVE_SYMENGINE_FLINT}
HAVE_SYMENGINE_LLVM=${HAVE_SYMENGINE_LLVM}
HAVE_SYMENGINE_LLVM_LONG_DOUBLE=${HAVE_SYMENGINE_LLVM_LONG_DOUBLE}
COMMENT "Preprocessing symengine_wrapper.in.pyx"
)

cython_add_module_pyx(symengine_wrapper symengine.pxd)
cython_add_module_pyx(symengine_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pyx
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
${CMAKE_CURRENT_SOURCE_DIR}/symengine.pxd)
add_python_library(symengine_wrapper ${SRC})
target_link_libraries(symengine_wrapper ${SYMENGINE_LIBRARIES})
if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
Expand All @@ -18,20 +52,15 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
)
endif()

add_custom_target(cython
COMMAND cython symengine_wrapper.pyx
)

set(PY_PATH ${PYTHON_INSTALL_PATH}/symengine/lib)
install(TARGETS symengine_wrapper
RUNTIME DESTINATION ${PY_PATH}
ARCHIVE DESTINATION ${PY_PATH}
LIBRARY DESTINATION ${PY_PATH}
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/config.pxi
symengine.pxd
symengine_wrapper.pxd
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
pywrapper.h
DESTINATION ${PY_PATH}
)
Expand Down
6 changes: 0 additions & 6 deletions symengine/lib/config.pxi.in

This file was deleted.

Loading