Skip to content

Commit 537c860

Browse files
committed
Handle compilation with/without numpy
1 parent ca777f6 commit 537c860

File tree

6 files changed

+378
-318
lines changed

6 files changed

+378
-318
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ include_directories(${SYMENGINE_INCLUDE_DIRS})
1313
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
1414
find_package(Python REQUIRED)
1515
find_package(Cython REQUIRED)
16-
1716
include_directories(${PYTHON_INCLUDE_PATH})
1817

1918
if (MINGW AND ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8"))
@@ -59,6 +58,14 @@ else()
5958
set(HAVE_SYMENGINE_LLVM False)
6059
endif()
6160

61+
if(WITH_NUMPY)
62+
find_package(NumPy REQUIRED)
63+
include_directories(${NUMPY_INCLUDE_PATH})
64+
set(HAVE_NUMPY True)
65+
else()
66+
set(HAVE_NUMPY False)
67+
endif()
68+
6269

6370
message("CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")
6471
message("CMAKE_CXX_FLAGS_RELEASE : ${CMAKE_CXX_FLAGS_RELEASE}")
@@ -68,6 +75,7 @@ message("HAVE_SYMENGINE_MPC : ${HAVE_SYMENGINE_MPC}")
6875
message("HAVE_SYMENGINE_PIRANHA : ${HAVE_SYMENGINE_PIRANHA}")
6976
message("HAVE_SYMENGINE_FLINT : ${HAVE_SYMENGINE_FLINT}")
7077
message("HAVE_SYMENGINE_LLVM : ${HAVE_SYMENGINE_LLVM}")
78+
message("HAVE_NUMPY : ${HAVE_NUMPY}")
7179

7280
message("Copying source of python wrappers into: ${CMAKE_CURRENT_BINARY_DIR}")
7381
file(COPY symengine/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/symengine)

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ Additional options to setup.py are
4343
--define="var1=value1;var2=value2" # Give options to CMake
4444
--inplace # Build the extension in source tree
4545

46-
Standard options to setup.py like `--user`, `--prefix` can be used to configure install location
46+
Standard options to setup.py like `--user`, `--prefix` can be used to
47+
configure install location. NumPy is used if found by default, if you wish
48+
to make your choice of NumPy use explicit: then add
49+
e.g. ``WITH_NUMPY=False`` to ``--define``.
4750

4851
Use SymEngine from Python as follows:
4952

cmake/FindNumPy.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
execute_process(
2+
COMMAND ${PYTHON_BIN} -c "import numpy; print(numpy.get_include())"
3+
RESULT_VARIABLE NUMPY_FIND_RESULT
4+
OUTPUT_VARIABLE NUMPY_FIND_OUTPUT
5+
ERROR_VARIABLE NUMPY_FIND_ERROR
6+
OUTPUT_STRIP_TRAILING_WHITESPACE
7+
)
8+
if(NOT NUMPY_FIND_RESULT MATCHES 0)
9+
set(NUMPY_FOUND FALSE)
10+
message(STATUS "NumPy import failure:\n${NUMPY_FIND_ERROR}")
11+
else()
12+
find_path(NUMPY_INCLUDE_DIR numpy/arrayobject.h
13+
HINTS "${__numpy_path}" "${NUMPY_FIND_OUTPUT}" NO_DEFAULT_PATH)
14+
if(NUMPY_INCLUDE_DIR)
15+
set(NUMPY_FOUND TRUE CACHE BOOL INTERNAL "NumPy found")
16+
INCLUDE(FindPackageHandleStandardArgs)
17+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NUMPY DEFAULT_MSG NUMPY_INCLUDE_PATH)
18+
endif()
19+
endif()

setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def finalize_options(self):
162162
# Multiple symbols can be separated with semi-colons.
163163
if self.define:
164164
defines = self.define.split(';')
165+
if not any(define.startswith('WITH_NUMPY') for define in defines):
166+
try:
167+
import numpy as np
168+
except ImportError:
169+
defines.append('WITH_NUMPY=False')
170+
else:
171+
defines.append('WITH_NUMPY=True')
172+
defines.append()
165173
self.define = [(s.strip(), None) if '=' not in s else
166174
tuple(ss.strip() for ss in s.split('='))
167175
for s in defines]

symengine/lib/config.pxi.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ DEF HAVE_SYMENGINE_MPC = ${HAVE_SYMENGINE_MPC}
33
DEF HAVE_SYMENGINE_PIRANHA = ${HAVE_SYMENGINE_PIRANHA}
44
DEF HAVE_SYMENGINE_FLINT = ${HAVE_SYMENGINE_FLINT}
55
DEF HAVE_SYMENGINE_LLVM = ${HAVE_SYMENGINE_LLVM}
6+
DEF HAVE_NUMPY = ${HAVE_NUMPY}

0 commit comments

Comments
 (0)