chiark / gitweb /
Use trusty
[nlopt.git] / cmake / FindNumPy.cmake
1 # - Find the NumPy libraries
2 # This module finds if NumPy is installed, and sets the following variables
3 # indicating where it is.
4 #
5 # TODO: Update to provide the libraries and paths for linking npymath lib.
6 #
7 #  NUMPY_FOUND               - was NumPy found
8 #  NUMPY_VERSION             - the version of NumPy found as a string
9 #  NUMPY_VERSION_MAJOR       - the major version number of NumPy
10 #  NUMPY_VERSION_MINOR       - the minor version number of NumPy
11 #  NUMPY_VERSION_PATCH       - the patch version number of NumPy
12 #  NUMPY_VERSION_DECIMAL     - e.g. version 1.6.1 is 10601
13 #  NUMPY_INCLUDE_DIRS        - path to the NumPy include files
14
15 #============================================================================
16 # Copyright 2012 Continuum Analytics, Inc.
17 #
18 # MIT License
19 #
20 # Permission is hereby granted, free of charge, to any person obtaining
21 # a copy of this software and associated documentation files
22 # (the "Software"), to deal in the Software without restriction, including
23 # without limitation the rights to use, copy, modify, merge, publish,
24 # distribute, sublicense, and/or sell copies of the Software, and to permit
25 # persons to whom the Software is furnished to do so, subject to
26 # the following conditions:
27 #
28 # The above copyright notice and this permission notice shall be included
29 # in all copies or substantial portions of the Software.
30 #
31 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
35 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
37 # OTHER DEALINGS IN THE SOFTWARE.
38 #
39 #============================================================================
40
41 # Finding NumPy involves calling the Python interpreter
42 if(NumPy_FIND_REQUIRED)
43     find_package(PythonInterp REQUIRED)
44 else()
45     find_package(PythonInterp)
46 endif()
47
48 if(NOT PYTHONINTERP_FOUND)
49     set(NUMPY_FOUND FALSE)
50     return()
51 endif()
52
53 execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
54     "import numpy as n; print(n.__version__); print(n.get_include());"
55     RESULT_VARIABLE _NUMPY_SEARCH_SUCCESS
56     OUTPUT_VARIABLE _NUMPY_VALUES_OUTPUT
57     ERROR_VARIABLE _NUMPY_ERROR_VALUE
58     OUTPUT_STRIP_TRAILING_WHITESPACE)
59
60 if(NOT _NUMPY_SEARCH_SUCCESS MATCHES 0)
61     if(NumPy_FIND_REQUIRED)
62         message(FATAL_ERROR
63             "NumPy import failure:\n${_NUMPY_ERROR_VALUE}")
64     endif()
65     set(NUMPY_FOUND FALSE)
66     return()
67 endif()
68
69 # Convert the process output into a list
70 string(REGEX REPLACE ";" "\\\\;" _NUMPY_VALUES ${_NUMPY_VALUES_OUTPUT})
71 string(REGEX REPLACE "\n" ";" _NUMPY_VALUES ${_NUMPY_VALUES})
72 list(GET _NUMPY_VALUES 0 NUMPY_VERSION)
73 list(GET _NUMPY_VALUES 1 NUMPY_INCLUDE_DIRS)
74
75 string(REGEX MATCH "^[0-9]+\\.[0-9]+\\.[0-9]+" _VER_CHECK "${NUMPY_VERSION}")
76 if("${_VER_CHECK}" STREQUAL "")
77     # The output from Python was unexpected. Raise an error always
78     # here, because we found NumPy, but it appears to be corrupted somehow.
79     message(FATAL_ERROR
80         "Requested version and include path from NumPy, got instead:\n${_NUMPY_VALUES_OUTPUT}\n")
81     return()
82 endif()
83
84 # Make sure all directory separators are '/'
85 string(REGEX REPLACE "\\\\" "/" NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS})
86
87 # Get the major and minor version numbers
88 string(REGEX REPLACE "\\." ";" _NUMPY_VERSION_LIST ${NUMPY_VERSION})
89 list(GET _NUMPY_VERSION_LIST 0 NUMPY_VERSION_MAJOR)
90 list(GET _NUMPY_VERSION_LIST 1 NUMPY_VERSION_MINOR)
91 list(GET _NUMPY_VERSION_LIST 2 NUMPY_VERSION_PATCH)
92 string(REGEX MATCH "[0-9]*" NUMPY_VERSION_PATCH ${NUMPY_VERSION_PATCH})
93 math(EXPR NUMPY_VERSION_DECIMAL
94     "(${NUMPY_VERSION_MAJOR} * 10000) + (${NUMPY_VERSION_MINOR} * 100) + ${NUMPY_VERSION_PATCH}")
95
96 find_package_message(NUMPY
97     "Found NumPy: version \"${NUMPY_VERSION}\" ${NUMPY_INCLUDE_DIRS}"
98     "${NUMPY_INCLUDE_DIRS}${NUMPY_VERSION}")
99
100 set(NUMPY_FOUND TRUE)