Imported Upstream version 3.1.9
[platform/upstream/Imath.git] / src / python / config / NumPyLocate.cmake
1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright Contributors to the OpenEXR Project.
3
4 if(TARGET Python2::Interpreter)
5   execute_process(
6     COMMAND ${Python2_EXECUTABLE} -c
7     "from __future__ import print_function\ntry: import numpy; print(numpy.get_include(), end='')\nexcept: import sys; sys.exit(1)\n"
8     RESULT_VARIABLE _NUMPY2_RESULT
9     OUTPUT_VARIABLE py_imath_numpy2_path
10     OUTPUT_STRIP_TRAILING_WHITESPACE)
11
12   if(NOT _NUMPY2_RESULT MATCHES 0)
13     set(NumPy_Py2_FOUND FALSE CACHE INTERNAL "Python2 numpy libraries not found")
14     message(WARNING "Unable to import numpy using python ${Python2_VERSION}")
15   else()
16     execute_process(
17       COMMAND ${Python2_EXECUTABLE} -c
18       "from __future__ import print_function\ntry: import numpy; print(numpy.__version__, end='')\nexcept: import sys; sys.exit(1)\n"
19       RESULT_VARIABLE _NUMPY2_RESULT
20       OUTPUT_VARIABLE py_imath_numpy2_version
21       OUTPUT_STRIP_TRAILING_WHITESPACE)
22
23     if(NOT _NUMPY2_RESULT MATCHES 0)
24       set(NumPy_Py2_FOUND FALSE CACHE INTERNAL "Python2 numpy libraries not found")
25       message(WARNING "Found numpy module in python ${Python2_VERSION}, but no version information")
26     else()
27       find_path(NumPy_Py2_INCLUDE_DIRS numpy/arrayobject.h
28         HINTS "${py_imath_numpy2_path}" "${Python2_INCLUDE_DIRS}"
29         NO_DEFAULT_PATH
30       )
31       if(NumPy_Py2_INCLUDE_DIRS)
32         set(NumPy_Py2_FOUND TRUE CACHE INTERNAL "Python2 numpy found")
33         set(NumPy_Py2_VERSION ${py_imath_numpy2_version})
34         add_library(NumPy_Py2 INTERFACE IMPORTED GLOBAL)
35         target_include_directories(NumPy_Py2 INTERFACE ${NumPy_Py2_INCLUDE_DIRS})
36         add_library(Python2::ImathNumPy ALIAS NumPy_Py2)
37         message(STATUS "Found NumPy ${NumPy_Py2_VERSION} for Python ${Python2_VERSION}: ${NumPy_Py2_INCLUDE_DIRS}")
38       else()
39         set(NumPy_Py2_FOUND FALSE CACHE INTERNAL "Python2 numpy libraries not found")
40         message(WARNING "Found numpy version ${py_imath_numpy2_version} in python ${Python2_VERSION}, but unable to locate header files")
41       endif()
42     endif()
43   endif()
44 endif()
45
46 if(TARGET Python3::Interpreter)
47   execute_process(
48     COMMAND ${Python3_EXECUTABLE} -c
49     "from __future__ import print_function\ntry: import numpy; print(numpy.get_include(), end='')\nexcept: import sys; sys.exit(1)\n"
50     RESULT_VARIABLE _NUMPY3_RESULT
51     OUTPUT_VARIABLE py_imath_numpy3_path
52     OUTPUT_STRIP_TRAILING_WHITESPACE)
53
54   if(NOT _NUMPY3_RESULT MATCHES 0)
55     set(NumPy_Py3_FOUND FALSE CACHE INTERNAL "Python3 numpy libraries not found")
56     message(WARNING "Unable to import numpy using python ${Python3_VERSION}")
57   else()
58     execute_process(
59       COMMAND ${Python3_EXECUTABLE} -c
60       "from __future__ import print_function\ntry: import numpy; print(numpy.__version__, end='')\nexcept: import sys; sys.exit(1)\n"
61       RESULT_VARIABLE _NUMPY3_RESULT
62       OUTPUT_VARIABLE py_imath_numpy3_version
63       OUTPUT_STRIP_TRAILING_WHITESPACE)
64
65     if(NOT _NUMPY3_RESULT MATCHES 0)
66       set(NumPy_Py3_FOUND FALSE CACHE INTERNAL "Python3 numpy libraries not found")
67       message(WARNING "Found numpy module in python ${Python3_VERSION}, but no version information")
68     else()
69       find_path(NumPy_Py3_INCLUDE_DIRS numpy/arrayobject.h
70         HINTS "${py_imath_numpy3_path}" "${Python3_INCLUDE_DIRS}"
71         NO_DEFAULT_PATH
72       )
73       if(NumPy_Py3_INCLUDE_DIRS)
74         set(NumPy_Py3_FOUND TRUE CACHE INTERNAL "Python3 numpy found")
75         set(NumPy_Py3_VERSION ${py_imath_numpy3_version})
76         add_library(NumPy_Py3 INTERFACE IMPORTED GLOBAL)
77         target_include_directories(NumPy_Py3 INTERFACE ${NumPy_Py3_INCLUDE_DIRS})
78         add_library(Python3::ImathNumPy ALIAS NumPy_Py3)
79         message(STATUS "Found NumPy ${NumPy_Py3_VERSION} for Python ${Python3_VERSION}: ${NumPy_Py3_INCLUDE_DIRS}")
80       else()
81         set(NumPy_Py3_FOUND FALSE CACHE INTERNAL "Python3 numpy libraries not found")
82         message(WARNING "Found numpy version ${py_imath_numpy3_version} in python ${Python3_VERSION}, but unable to locate header files")
83       endif()
84     endif()
85   endif()
86 endif()