forked from classner/SConsChecks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.py
159 lines (155 loc) · 5.4 KB
/
python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- python -*-
#
# These SCons tests are based on the ones of boost numpy
# (https://github.com/ndarray/Boost.NumPy/blob/master/SConscript),
# but have been altered and substantially extended.
#
# Copyright Christoph Lassner 2014.
# For the python, numpy and boost python checks, _setupPaths and _checkLibs:
# Copyright Jim Bosch 2010-2012.
#
# Distributed under the Boost Software License, Version 1.0.
# (See http://www.boost.org/LICENSE_1_0.txt)
from __future__ import print_function
import os
import sys
import platform
import subprocess
from ._tools import _checkLibs, _setupPaths
from SCons.SConf import CheckContext
CheckContext.checkLibs = _checkLibs
_check_dict = {}
def CheckPython(context):
python_source_file = r"""
// If defined, enforces linking againg PythonXXd.lib, which
// is usually not included in Python environments.
#undef _DEBUG
#include "Python.h"
int main()
{
Py_Initialize();
Py_Finalize();
return 0;
}
"""
context.Message('Check building against Python... ')
try:
import distutils.sysconfig
except ImportError:
context.Result(0)
print('Failed to import distutils.sysconfig.')
return False
context.env.AppendUnique(CPPPATH=[distutils.sysconfig.get_python_inc()])
if platform.system() == 'Windows':
libfile = os.path.join(os.path.dirname(sys.executable),
'libs',
'python27.lib')
context.env.AppendUnique(LIBS=[libfile])
else:
libDir = distutils.sysconfig.get_config_var("LIBDIR")
context.env.AppendUnique(LIBPATH=[libDir])
libfile = distutils.sysconfig.get_config_var("LIBRARY")
import re
match = re.search("(python.*)\.(a|so|dylib)", libfile)
if match:
context.env.AppendUnique(LIBS=[match.group(1)])
if match.group(2) == 'a':
flags = distutils.sysconfig.get_config_var('LINKFORSHARED')
if flags is not None:
context.env.AppendUnique(LINKFLAGS=flags.split())
flags = [f for f in " ".join(distutils.sysconfig.get_config_vars("MODLIBS", "SHLIBS")).split()
if f != "-L"]
context.env.MergeFlags(" ".join(flags))
result, output = context.TryRun(python_source_file,'.cpp')
if not result and context.env["PLATFORM"] == 'darwin':
# Sometimes we need some extra stuff on Mac OS
frameworkDir = libDir # search up the libDir tree for the proper home for frameworks
while frameworkDir and frameworkDir != "/":
frameworkDir, d2 = os.path.split(frameworkDir)
if d2 == "Python.framework":
if not "Python" in os.listdir(os.path.join(frameworkDir, d2)):
context.Result(0)
print((
"Expected to find Python in framework directory %s, but it isn't there"
% frameworkDir))
return False
break
context.env.AppendUnique(LINKFLAGS="-F%s" % frameworkDir)
result, output = context.TryRun(python_source_file,'.cpp')
if not result:
context.Result(0)
print("Cannot run program built with Python.")
return False
if context.env["PLATFORM"] == "darwin":
context.env["LDMODULESUFFIX"] = ".so"
context.Result(1)
return True
_check_dict['python'] = {'options': {},
'checks': [CheckPython]}
def CheckNumPy(context):
numpy_source_file = r"""
// If defined, enforces linking againg PythonXXd.lib, which
// is usually not included in readymade Python environments.
#undef _DEBUG
#include "Python.h"
#include "numpy/arrayobject.h"
#if PY_MAJOR_VERSION == 2
void doImport() {
import_array();
}
#else
void * doImport() {
import_array();
return nullptr;
}
#endif
int main()
{
int result = 0;
Py_Initialize();
doImport();
if (PyErr_Occurred()) {
result = 1;
} else {
npy_intp dims = 2;
PyObject * a = PyArray_SimpleNew(1, &dims, NPY_INT);
if (!a) result = 1;
Py_DECREF(a);
}
Py_Finalize();
return result;
}
"""
context.Message('Check building against NumPy... ')
try:
import numpy
except ImportError:
context.Result(0)
print('Failed to import numpy.')
print('Things to try:')
print('1) Check that the command line python (with which you probably installed numpy):')
print(' ', end=' ')
sys.stdout.flush()
subprocess.call('which python',shell=True)
print(' is the same as the one used by SCons:')
print(' ', sys.executable)
print(' If not, then you probably need to reinstall numpy with %s.' % sys.executable)
print(' Alternatively, you can reinstall SCons with your preferred python.')
print('2) Check that if you open a python session from the command line,')
print(' import numpy is successful there.')
return False
context.env.Append(CPPPATH=numpy.get_include())
result = context.checkLibs([''],numpy_source_file)
if not result:
context.Result(0)
print("Cannot build against NumPy.")
return False
result, output = context.TryRun(numpy_source_file,'.cpp')
if not result:
context.Result(0)
print("Cannot run program built with NumPy.")
return False
context.Result(1)
return True
_check_dict['numpy'] = {'options': {},
'checks': [CheckPython, CheckNumPy]}