Skip to content

Commit 8d5d63c

Browse files
authored
Merge pull request #444 from isuruf/cython3
Cython3 support
2 parents 4fba9a6 + 33ab529 commit 8d5d63c

File tree

8 files changed

+518
-560
lines changed

8 files changed

+518
-560
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ cython_test.cpp
1818
*.vcxproj
1919
*.filters
2020
symengine/lib/symengine_wrapper.cpp
21+
symengine/lib/symengine_wrapper.pyx
22+
symengine/lib/symengine_wrapper.pxd
2123

2224
# Config Files
2325
symengine/lib/config.pxi

cmake/FindCython.cmake

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,13 @@ if(NOT CYTHON_INCLUDE_DIRECTORIES)
6363
endif(NOT CYTHON_INCLUDE_DIRECTORIES)
6464

6565
# Cythonizes the .pyx files into .cpp file (but doesn't compile it)
66-
macro(CYTHON_ADD_MODULE_PYX name)
67-
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd)
68-
set(DEPENDS ${name}.pyx ${name}.pxd)
69-
else(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd)
70-
set(DEPENDS ${name}.pyx)
71-
endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd)
66+
macro(CYTHON_ADD_MODULE_PYX cpp_name pyx_name)
7267
# Allow the user to specify dependencies as optional arguments
7368
set(DEPENDS ${DEPENDS} ${ARGN})
7469
add_custom_command(
75-
OUTPUT ${name}.cpp
70+
OUTPUT ${cpp_name}
7671
COMMAND ${CYTHON_BIN}
77-
ARGS ${CYTHON_FLAGS} -I ${CYTHON_INCLUDE_DIRECTORIES} -o ${name}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pyx
78-
DEPENDS ${DEPENDS}
79-
COMMENT "Cythonizing ${name}.pyx")
72+
ARGS ${CYTHON_FLAGS} -I ${CYTHON_INCLUDE_DIRECTORIES} -o ${cpp_name} ${pyx_name}
73+
DEPENDS ${DEPENDS} ${pyx_name}
74+
COMMENT "Cythonizing ${pyx_name}")
8075
endmacro(CYTHON_ADD_MODULE_PYX)
81-
82-
# Cythonizes and compiles a .pyx file
83-
macro(CYTHON_ADD_MODULE name)
84-
CYTHON_ADD_MODULE_PYX(${name})
85-
# We need Python for this:
86-
find_package(Python REQUIRED)
87-
add_python_library(${name} ${name}.cpp ${ARGN})
88-
endmacro(CYTHON_ADD_MODULE)

cmake/preprocess.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
3+
4+
def main(input_name, output_name, replacements):
5+
replacements = dict((item.split("=")[0], item.split("=")[1] == "True") for item in replacements)
6+
with open(input_name, "r") as inp:
7+
text = inp.readlines()
8+
9+
new_text = []
10+
in_cond = [True]
11+
nspaces = [0]
12+
for i, line in enumerate(text):
13+
if line.strip().startswith("IF"):
14+
s = len(line) - len(line.lstrip())
15+
while s <= nspaces[-1] and len(in_cond) > 1:
16+
in_cond = in_cond[:-1]
17+
nspaces = nspaces[:-1]
18+
19+
cond = line.lstrip()[3:-2]
20+
in_cond.append(replacements[cond])
21+
nspaces.append(s)
22+
elif line.strip().startswith("ELSE"):
23+
in_cond[-1] = not in_cond[-1] and in_cond[-2]
24+
25+
if len(line) > 1 and not line.strip().startswith(("IF", "ELSE")):
26+
while len(in_cond) > 1 and (len(line) <= nspaces[-1] or not line.startswith(" "*nspaces[-1]) or line[nspaces[-1]] != " "):
27+
in_cond = in_cond[:-1]
28+
nspaces = nspaces[:-1]
29+
if len(line) == 1:
30+
new_text.append(line)
31+
elif in_cond[-1] and not line.strip().startswith(("IF", "ELSE")):
32+
new_text.append(line[4*(len(in_cond) - 1):])
33+
34+
with open(output_name, "w") as out:
35+
out.writelines(new_text)
36+
37+
if __name__ == "__main__":
38+
main(sys.argv[1], sys.argv[2], sys.argv[3:])

symengine/lib/CMakeLists.txt

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
set(SRC
2-
symengine_wrapper.cpp
2+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.cpp
33
pywrapper.cpp
44
)
55

6-
configure_file(config.pxi.in config.pxi)
6+
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
77

8-
include_directories(BEFORE ${python_wrapper_BINARY_DIR}/symengine/lib)
8+
add_custom_command(
9+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
10+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pxd
11+
${PROJECT_SOURCE_DIR}/cmake/preprocess.py
12+
COMMAND ${PYTHON_BIN} ${PROJECT_SOURCE_DIR}/cmake/preprocess.py
13+
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pxd
14+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
15+
HAVE_SYMENGINE_MPFR=${HAVE_SYMENGINE_MPFR}
16+
HAVE_SYMENGINE_MPC=${HAVE_SYMENGINE_MPC}
17+
HAVE_SYMENGINE_PIRANHA=${HAVE_SYMENGINE_PIRANHA}
18+
HAVE_SYMENGINE_FLINT=${HAVE_SYMENGINE_FLINT}
19+
HAVE_SYMENGINE_LLVM=${HAVE_SYMENGINE_LLVM}
20+
HAVE_SYMENGINE_LLVM_LONG_DOUBLE=${HAVE_SYMENGINE_LLVM_LONG_DOUBLE}
21+
COMMENT "Preprocessing symengine_wrapper.in.pxd"
22+
)
23+
add_custom_command(
24+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pyx
25+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pyx
26+
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pxd
27+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
28+
${PROJECT_SOURCE_DIR}/cmake/preprocess.py
29+
COMMAND ${PYTHON_BIN} ${PROJECT_SOURCE_DIR}/cmake/preprocess.py
30+
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pyx
31+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pyx
32+
HAVE_SYMENGINE_MPFR=${HAVE_SYMENGINE_MPFR}
33+
HAVE_SYMENGINE_MPC=${HAVE_SYMENGINE_MPC}
34+
HAVE_SYMENGINE_PIRANHA=${HAVE_SYMENGINE_PIRANHA}
35+
HAVE_SYMENGINE_FLINT=${HAVE_SYMENGINE_FLINT}
36+
HAVE_SYMENGINE_LLVM=${HAVE_SYMENGINE_LLVM}
37+
HAVE_SYMENGINE_LLVM_LONG_DOUBLE=${HAVE_SYMENGINE_LLVM_LONG_DOUBLE}
38+
COMMENT "Preprocessing symengine_wrapper.in.pyx"
39+
)
940

10-
cython_add_module_pyx(symengine_wrapper symengine.pxd)
41+
cython_add_module_pyx(symengine_wrapper.cpp
42+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pyx
43+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
44+
${CMAKE_CURRENT_SOURCE_DIR}/symengine.pxd)
1145
add_python_library(symengine_wrapper ${SRC})
1246
target_link_libraries(symengine_wrapper ${SYMENGINE_LIBRARIES})
1347
if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
@@ -18,20 +52,15 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
1852
)
1953
endif()
2054

21-
add_custom_target(cython
22-
COMMAND cython symengine_wrapper.pyx
23-
)
24-
2555
set(PY_PATH ${PYTHON_INSTALL_PATH}/symengine/lib)
2656
install(TARGETS symengine_wrapper
2757
RUNTIME DESTINATION ${PY_PATH}
2858
ARCHIVE DESTINATION ${PY_PATH}
2959
LIBRARY DESTINATION ${PY_PATH}
3060
)
3161
install(FILES
32-
${CMAKE_CURRENT_BINARY_DIR}/config.pxi
3362
symengine.pxd
34-
symengine_wrapper.pxd
63+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
3564
pywrapper.h
3665
DESTINATION ${PY_PATH}
3766
)

symengine/lib/config.pxi.in

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)