Skip to content

Commit 1d63ae4

Browse files
committed
Replace the use of conditional statements by a preprocessing program
1 parent d1d4de8 commit 1d63ae4

File tree

8 files changed

+85
-33
lines changed

8 files changed

+85
-33
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 name filename)
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 ${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 ${name} ${filename}
73+
DEPENDS ${DEPENDS} ${filename}
74+
COMMENT "Cythonizing ${filename}")
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: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
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_SOURCE_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+
${PROJECT_SOURCE_DIR}/cmake/preprocess.py
28+
COMMAND ${PYTHON_BIN} ${PROJECT_SOURCE_DIR}/cmake/preprocess.py
29+
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.in.pyx
30+
${CMAKE_CURRENT_SOURCE_DIR}/symengine_wrapper.pyx
31+
HAVE_SYMENGINE_MPFR=${HAVE_SYMENGINE_MPFR}
32+
HAVE_SYMENGINE_MPC=${HAVE_SYMENGINE_MPC}
33+
HAVE_SYMENGINE_PIRANHA=${HAVE_SYMENGINE_PIRANHA}
34+
HAVE_SYMENGINE_FLINT=${HAVE_SYMENGINE_FLINT}
35+
HAVE_SYMENGINE_LLVM=${HAVE_SYMENGINE_LLVM}
36+
HAVE_SYMENGINE_LLVM_LONG_DOUBLE=${HAVE_SYMENGINE_LLVM_LONG_DOUBLE}
37+
COMMENT "Preprocessing symengine_wrapper.in.pyx"
38+
)
939

10-
cython_add_module_pyx(symengine_wrapper symengine.pxd)
40+
cython_add_module_pyx(symengine_wrapper.cpp
41+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pyx
42+
${CMAKE_CURRENT_BINARY_DIR}/symengine_wrapper.pxd
43+
${CMAKE_CURRENT_SOURCE_DIR}/symengine.pxd)
1144
add_python_library(symengine_wrapper ${SRC})
1245
target_link_libraries(symengine_wrapper ${SYMENGINE_LIBRARIES})
1346
if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
@@ -18,18 +51,13 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
1851
)
1952
endif()
2053

21-
add_custom_target(cython
22-
COMMAND cython symengine_wrapper.pyx
23-
)
24-
2554
set(PY_PATH ${PYTHON_INSTALL_PATH}/symengine/lib)
2655
install(TARGETS symengine_wrapper
2756
RUNTIME DESTINATION ${PY_PATH}
2857
ARCHIVE DESTINATION ${PY_PATH}
2958
LIBRARY DESTINATION ${PY_PATH}
3059
)
3160
install(FILES
32-
${CMAKE_CURRENT_BINARY_DIR}/config.pxi
3361
symengine.pxd
3462
symengine_wrapper.pxd
3563
pywrapper.h

symengine/lib/config.pxi.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ DEF HAVE_SYMENGINE_PIRANHA = ${HAVE_SYMENGINE_PIRANHA}
44
DEF HAVE_SYMENGINE_FLINT = ${HAVE_SYMENGINE_FLINT}
55
DEF HAVE_SYMENGINE_LLVM = ${HAVE_SYMENGINE_LLVM}
66
DEF HAVE_SYMENGINE_LLVM_LONG_DOUBLE = ${HAVE_SYMENGINE_LLVM_LONG_DOUBLE}
7+
DEF ENDIF = 1

symengine/lib/symengine.pxd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ from libcpp.pair cimport pair
77
from libcpp.set cimport set
88
from libcpp.unordered_map cimport unordered_map
99

10-
include "config.pxi"
11-
1210
cdef extern from "<set>" namespace "std":
1311
# Cython's libcpp.set does not support multiset in 0.29.x
1412
cdef cppclass multiset[T]:
@@ -394,7 +392,7 @@ cdef extern from "<symengine/mul.h>" namespace "SymEngine":
394392
void as_two_terms(const Ptr[RCP[Basic]] &a, const Ptr[RCP[Basic]] &b)
395393
RCP[const Number] get_coef()
396394
const map_basic_basic &get_dict()
397-
cdef RCP[const Mul] mul_from_dict "SymEngine::Mul::from_dict"(RCP[const Number] coef, map_basic_basic &&d) nogil
395+
cdef RCP[const Mul] mul_from_dict "SymEngine::Mul::from_dict"(RCP[const Number] coef, map_basic_basic &d) nogil
398396

399397
cdef extern from "<symengine/pow.h>" namespace "SymEngine":
400398
cdef rcp_const_basic pow(rcp_const_basic &a, rcp_const_basic &b) except+ nogil

symengine/lib/symengine_wrapper.pxd renamed to symengine/lib/symengine_wrapper.in.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ from libcpp.vector cimport vector
66
from libcpp.string cimport string
77
from libcpp cimport bool as cppbool
88

9-
include "config.pxi"
10-
119
cdef class Basic(object):
1210
cdef rcp_const_basic thisptr
1311

symengine/lib/symengine_wrapper.in.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ try:
3030
except ImportError:
3131
have_numpy = False
3232

33-
include "config.pxi"
33+
# include "config.pxi"
3434

3535
class SympifyError(Exception):
3636
pass

0 commit comments

Comments
 (0)