]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/config/numpy.py
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / python / config / numpy.py
1 #
2 # Copyright (c) 2016 Stefan Seefeld
3 # All rights reserved.
4 #
5 # Distributed under the Boost Software License, Version 1.0.
6 # (See accompanying file LICENSE_1_0.txt or copy at
7 # http://www.boost.org/LICENSE_1_0.txt)
8
9 from . import ui
10 from contextlib import contextmanager
11
12 @contextmanager
13 def saved(context):
14 save_cpppath = context.env.get('CPPPATH', [])
15 save_libs = context.env.get('LIBS', [])
16 yield context
17 context.env.Replace(LIBS=save_libs)
18 context.env.Replace(CPPPATH=save_cpppath)
19
20
21 def add_options(vars):
22
23 pass
24
25
26 def check(context):
27
28 numpy_source_file = r"""
29 // If defined, enforces linking againg PythonXXd.lib, which
30 // is usually not included in Python environments.
31 #undef _DEBUG
32 #include "Python.h"
33 #include "numpy/arrayobject.h"
34
35 #if PY_VERSION_HEX >= 0x03000000
36 void *initialize() { import_array();}
37 #else
38 void initialize() { import_array();}
39 #endif
40
41 int main()
42 {
43 int result = 0;
44 Py_Initialize();
45 initialize();
46 if (PyErr_Occurred())
47 {
48 result = 1;
49 }
50 else
51 {
52 npy_intp dims = 2;
53 PyObject * a = PyArray_SimpleNew(1, &dims, NPY_INT);
54 if (!a) result = 1;
55 Py_DECREF(a);
56 }
57 Py_Finalize();
58 return result;
59 }
60 """
61
62 import platform
63 import subprocess
64 import re, os
65
66 def check_python(cmd):
67 try:
68 return True, subprocess.check_output([python, '-c', cmd]).strip()
69 except subprocess.CalledProcessError as e:
70 return False, e
71
72 context.Message('Checking for NumPy...')
73 with saved(context):
74 python = context.env['PYTHON']
75 result, numpy_incpath = check_python('import numpy; print(numpy.get_include())')
76 if result:
77 context.env.AppendUnique(CPPPATH=numpy_incpath)
78 context.env.AppendUnique(LIBS=context.env['PYTHONLIBS'])
79 result, output = context.TryRun(numpy_source_file,'.cpp')
80 if not result:
81 context.Result(0)
82 return False
83 context.env['NUMPY'] = True
84 context.env['NUMPY_CPPPATH'] = numpy_incpath
85 context.Result(1)
86 return True