Skip to content

Commit 3ace4a0

Browse files
committed
Adjust NumPy code to work with new directory / namespace structure.
1 parent cbb3851 commit 3ace4a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1200
-485
lines changed

.travis.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ matrix:
2626
- compiler: gcc
2727
env: CXX=g++ PYTHON=python3 CXXFLAGS=-std=c++11
2828
- compiler: clang
29-
env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++98
29+
# clang generates an 'illegal instruction' error in the NumPy check.
30+
# Perhaps we need to upgrade clang to a newer version ?
31+
env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++98 OPTIONS=--no-numpy
3032
- compiler: clang
31-
env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++11
33+
env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++11 OPTIONS=--no-numpy
3234
- env: PYTHON=python DOC=1
3335

3436

@@ -41,8 +43,10 @@ addons:
4143
- gcc-4.8
4244
- g++-4.8
4345
- clang
44-
- python-dev python-pip
46+
- python-numpy
47+
- python-sphinx
4548
- python3-dev
49+
- python3-numpy
4650
- libboost-all-dev
4751
- xsltproc
4852
- docbook-xsl
@@ -58,7 +62,6 @@ before_install:
5862
# conflicting UCS2 and UCS4 unicode. Modify the PATH to skip the TravisCI python.
5963
# See https://github.com/travis-ci/travis-ci/issues/4948 for details.
6064
- export PATH=$(echo $PATH | tr ':' "\n" | sed '/\/opt\/python/d' | tr "\n" ":" | sed "s|::|:|g")
61-
- sudo pip install future
6265

6366
install:
6467
# Install our own version of Boost (the subset we need) as the system version is
@@ -82,7 +85,7 @@ before_script:
8285
- scons --version
8386

8487
script:
85-
- scons config --python=$PYTHON --boost-include=$HOME/Boost
88+
- scons config --python=$PYTHON --boost-include=$HOME/Boost $OPTIONS
8689
- if [ "$DOC" ]; then scons doc; else scons && scons test; fi
8790

8891
after_success:

SConstruct

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ env_vars = {}
4040
if 'CXX' in os.environ: env_vars['CXX'] = os.environ['CXX']
4141
if 'CXXFLAGS' in os.environ: env_vars['CXXFLAGS'] = os.environ['CXXFLAGS'].split()
4242
env = Environment(toolpath=['config/tools'],
43-
tools=['default', 'libs', 'tests', 'doc'],
43+
tools=['default', 'libs', 'tests', 'doc', 'sphinx4scons'],
4444
variables=vars,
4545
TARGET_ARCH=arch,
4646
**env_vars)
@@ -68,7 +68,7 @@ config_log = '{}/config.log'.format(build_dir)
6868
SConsignFile('{}/.sconsign'.format(build_dir))
6969
#env.Decider('MD5-timestamp')
7070
env.Decider('timestamp-newer')
71-
checks = config.get_checks()
71+
checks = config.get_checks(env)
7272
if 'config' in COMMAND_LINE_TARGETS:
7373
conf=env.Configure(custom_tests=checks, log_file=config_log, conf_dir=build_dir)
7474
if False in (getattr(conf, c)() for c in checks):

config/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
from . import ui
1414
from . import cxx
1515
from . import python
16+
from . import numpy
1617
from . import boost
1718

1819
def add_options(vars):
1920
ui.add_option('-V', '--verbose', dest='verbose', action='store_true', help='verbose mode: print full commands.')
21+
ui.add_option('--no-numpy', dest='numpy', action='store_false', help='do not attempt to build NumPy bindings.')
2022
python.add_options(vars)
23+
numpy.add_options(vars)
2124
boost.add_options(vars)
2225

2326
vars.Add('CXX')
@@ -29,8 +32,10 @@ def add_options(vars):
2932
vars.Add('PYTHON')
3033
vars.Add('PYTHONLIBS')
3134
vars.Add('prefix')
32-
vars.Add('boostbook_prefix',
33-
vars.Add('CXX11'))
35+
vars.Add('boostbook_prefix')
36+
vars.Add('CXX11')
37+
vars.Add('NUMPY')
38+
vars.Add('NUMPY_CPPPATH', converter=lambda v:v.split())
3439

3540
ui.add_variable(vars, ("arch", "target architeture", platform.machine()))
3641
ui.add_variable(vars, ("toolchain", "toolchain to use", 'gcc'))
@@ -42,10 +47,14 @@ def add_options(vars):
4247
ui.add_variable(vars, PathVariable("prefix", "Install prefix", "/usr/local", PathVariable.PathAccept))
4348

4449

45-
def get_checks():
50+
def get_checks(env):
4651
checks = OrderedDict()
4752
checks['cxx'] = cxx.check
4853
checks['python'] = python.check
54+
if env.GetOption('numpy') is not False:
55+
checks['numpy'] = numpy.check
56+
else:
57+
env['NUMPY'] = False
4958
checks['boost'] = boost.check
5059
return checks
5160

config/numpy.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

config/tools/doc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def BoostRST(env, target, source, resources=[]):
5858
'rst2html --link-stylesheet --traceback --trim-footnote-reference-space --footnote-references=superscript --stylesheet=rst.css $SOURCE $TARGET')
5959

6060

61+
def BoostSphinx(env, target, source):
62+
env.Sphinx(target, source)
63+
64+
6165
def exists(env):
6266
return True
6367

@@ -68,3 +72,4 @@ def generate(env):
6872
env.AddMethod(BoostBook)
6973
env.AddMethod(BoostHTML)
7074
env.AddMethod(BoostRST)
75+
env.AddMethod(BoostSphinx)

0 commit comments

Comments
 (0)