-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathboost.py
More file actions
213 lines (158 loc) · 6.73 KB
/
Copy pathboost.py
File metadata and controls
213 lines (158 loc) · 6.73 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# encoding: utf-8
# Andre Anjos <andre.anjos@idiap.ch>
# Thu Mar 20 12:38:14 CET 2014
"""Helps looking for Boost on stock file-system locations"""
import os
import re
import sys
import glob
from distutils.version import LooseVersion
from .utils import uniq, egrep, find_header, find_library
def boost_version(version_hpp):
matches = egrep(version_hpp, r"^#\s*define\s+BOOST_VERSION\s+(\d+)\s*$")
if not len(matches): return None
# we have a match, produce a string version of the version number
version_int = int(matches[0].group(1))
version_tuple = (
version_int // 100000,
(version_int // 100) % 1000,
version_int % 100,
)
return '.'.join([str(k) for k in version_tuple])
class boost:
"""A class for capturing configuration information from boost
Example usage:
.. doctest::
:options: +NORMALIZE_WHITESPACE +ELLIPSIS
>>> from bob.extension import boost
>>> pkg = boost('>= 1.35')
>>> pkg.include_directory
'...'
>>> pkg.version
'...'
You can also use this class to retrieve information about installed Boost
libraries and link information:
.. doctest::
:options: +NORMALIZE_WHITESPACE +ELLIPSIS
>>> from bob.extension import boost
>>> pkg = boost('>= 1.35')
>>> pkg.libconfig(['python', 'system'])
(...)
"""
def __init__ (self, requirement=''):
"""
Searches for the Boost library in stock locations. Allows user to override.
If the user sets the environment variable BOB_PREFIX_PATH, that prefixes
the standard path locations.
"""
candidates = find_header('version.hpp', subpaths=['boost', 'boost?*'])
if not candidates:
raise RuntimeError("could not find boost's `version.hpp' - have you installed Boost on this machine?")
found = False
if not requirement:
# since we use boost headers **including the boost/ directory**, we need to go one level lower
self.include_directory = os.path.dirname(os.path.dirname(candidates[0]))
self.version = boost_version(candidates[0])
found = True
else:
# requirement is 'operator' 'version'
operator, required = [k.strip() for k in requirement.split(' ', 1)]
# now check for user requirements
for path in candidates:
version = boost_version(path)
available = LooseVersion(version)
if (operator == '<' and available < required) or \
(operator == '<=' and available <= required) or \
(operator == '>' and available > required) or \
(operator == '>=' and available >= required) or \
(operator == '==' and available == required):
self.include_directory = path
self.version = version
found = True
break
if not found:
raise RuntimeError("could not find the required (%s) version of boost on the file system (looked at: %s)" % (requirement, ', '.join(candidates)))
# normalize
self.include_directory = os.path.normpath(self.include_directory)
def libconfig(self, modules, only_static=False,
templates=['boost_%(name)s-mt-%(py)s', 'boost_%(name)s-%(py)s', 'boost_%(name)s-mt', 'boost_%(name)s', 'boost_%(name)s-vc140-mt-1_65_1']):
"""Returns a tuple containing the library configuration for requested
modules.
This function respects the path location where the include files for Boost
are installed.
Parameters:
modules (list of strings)
A list of string specifying the requested libraries to search for. For
example, to search for `libboost_mpi.so`, pass only ``mpi``.
static (bool)
A boolean, indicating if we should try only to search for static versions
of the libraries. If not set, any would do.
templates (list of template strings)
A list that defines in which order to search for libraries on the default
search path, defined by ``self.include_directory``. Tune this list if you
have compiled specific versions of Boost with support to multi-threading
(``-mt``), debug (``-g``), STLPORT (``-p``) or required to insert
compiler, the underlying thread API used or your own namespace.
Here are the keywords you can use:
%(name)s
resolves to the module name you are searching for
%(ver)s
resolves to the current boost version string (e.g. ``'1.50.0'``)
%(py)s
resolves to the string ``'pyXY'`` where ``XY`` represent the major and
minor versions of the current python interpreter.
Example templates:
* ``'boost_%(name)s-mt'``
* ``'boost_%(name)s'``
* ``'boost_%(name)s-gcc43-%(ver)s'``
Returns:
directories (list of strings)
A list of directories indicating where the libraries are installed
libs (list of strings)
A list of strings indicating the names of the libraries you can use
"""
# make the include header prefix preferential
prefix = os.path.dirname(self.include_directory)
py = 'py%d%d' % sys.version_info[:2]
filenames = []
for module in modules:
candidates = []
modnames = [k % dict(name=module, ver=self.version, py=py) for k in
templates]
for modname in modnames:
candidates += find_library(modname, version=self.version,
prefixes=[prefix], only_static=only_static)
if not candidates:
raise RuntimeError("cannot find required boost module `%s' - make sure boost is installed on `%s' and that this module is named %s on the filesystem" % (module, prefix, ' or '.join(modnames)))
# take the first choice that includes the prefix (or the absolute first choice otherwise)
index = 0
for i, candidate in enumerate(candidates):
if candidate.find(prefix) == 0:
index = i
break
filenames.append(candidates[index])
# libraries
libraries = []
for f in filenames:
name, ext = os.path.splitext(os.path.basename(f))
if ext in ['.so', '.a', '.dylib', '.dll', '.lib']:
libraries.append(name[3:]) #strip 'lib' from the name
else: #link against the whole thing
libraries.append(':' + os.path.basename(f))
# library paths
libpaths = [os.path.dirname(k) for k in filenames]
return uniq(libpaths), uniq(libraries)
def macros(self):
"""Returns package availability and version number macros
This method returns a python list with 2 macros indicating package
availability and a version number, using standard GNU compatible names.
Example:
.. doctest::
:options: +NORMALIZE_WHITESPACE +ELLIPSIS
>>> from bob.extension import boost
>>> pkg = boost('>= 1.34')
>>> pkg.macros()
[('HAVE_BOOST', '1'), ('BOOST_VERSION', '"..."')]
"""
return [('HAVE_BOOST', '1'), ('BOOST_VERSION', '"%s"' % self.version)]