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