-
Notifications
You must be signed in to change notification settings - Fork 87
/
boost.py
218 lines (174 loc) · 8.26 KB
/
boost.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
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
214
215
216
217
218
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=invalid-name, too-few-public-methods
# pylint: disable=too-many-instance-attributes
"""Boost building block"""
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
import re
import posixpath
import hpccm.config
import hpccm.templates.envvars
import hpccm.templates.ldconfig
import hpccm.templates.rm
import hpccm.templates.tar
import hpccm.templates.wget
from hpccm.building_blocks.base import bb_base
from hpccm.building_blocks.packages import packages
from hpccm.common import linux_distro
from hpccm.primitives.comment import comment
from hpccm.primitives.copy import copy
from hpccm.primitives.environment import environment
from hpccm.primitives.shell import shell
class boost(bb_base, hpccm.templates.envvars, hpccm.templates.ldconfig,
hpccm.templates.rm, hpccm.templates.tar, hpccm.templates.wget):
"""The `boost` building block downloads and installs the
[Boost](https://www.boost.org) component.
# Parameters
bootstrap_opts: List of options to pass to `bootstrap.sh`. The
default is an empty list.
environment: Boolean flag to specify whether the environment
(`LD_LIBRARY_PATH`) should be modified to include Boost. The
default is True.
ldconfig: Boolean flag to specify whether the Boost library
directory should be added dynamic linker cache. If False, then
`LD_LIBRARY_PATH` is modified to include the Boost library
directory. The default value is False.
ospackages: List of OS packages to install prior to building. For
Ubuntu, the default values are `bzip2`, `libbz2-dev`, `tar`,
`wget`, and `zlib1g-dev`. For RHEL-based Linux distributions the
default values are `bzip2`, `bzip2-devel`, `tar`, `wget`, `which`,
and `zlib-devel`.
prefix: The top level installation location. The default value
is `/usr/local/boost`.
python: Boolean flag to specify whether Boost should be built with
Python support. If enabled, the Python C headers need to be
installed (typically this can be done by adding `python-dev` or
`python-devel` to the list of OS packages). The default is False.
sourceforge: Boolean flag to specify whether Boost should be
downloaded from SourceForge rather than the current Boost
repository. For versions of Boost older than 1.63.0, the
SourceForge repository should be used. The default is False.
version: The version of Boost source to download. The default
value is `1.72.0`.
# Examples
```python
boost(prefix='/opt/boost/1.67.0', version='1.67.0')
```
```python
boost(sourceforge=True, version='1.57.0')
```
"""
def __init__(self, **kwargs):
"""Initialize building block"""
super(boost, self).__init__(**kwargs)
self.__baseurl = kwargs.get('baseurl',
'https://dl.bintray.com/boostorg/release/__version__/source')
self.__bootstrap_opts = kwargs.get('bootstrap_opts', [])
self.__ospackages = kwargs.get('ospackages', [])
self.__parallel = kwargs.get('parallel', '$(nproc)')
self.__prefix = kwargs.get('prefix', '/usr/local/boost')
self.__python = kwargs.get('python', False)
self.__sourceforge = kwargs.get('sourceforge', False)
self.__version = kwargs.get('version', '1.72.0')
self.__commands = [] # Filled in by __setup()
self.__wd = '/var/tmp' # working directory
if self.__sourceforge:
self.__baseurl = 'https://sourceforge.net/projects/boost/files/boost/__version__'
# Set the Linux distribution specific parameters
self.__distro()
# Construct the series of steps to execute
self.__setup()
# Fill in container instructions
self.__instructions()
def __instructions(self):
"""Fill in container instructions"""
self += comment('Boost version {}'.format(self.__version))
self += packages(ospackages=self.__ospackages)
self += shell(commands=self.__commands)
self += environment(variables=self.environment_step())
def __distro(self):
"""Based on the Linux distribution, set values accordingly. A user
specified value overrides any defaults."""
if hpccm.config.g_linux_distro == linux_distro.UBUNTU:
if not self.__ospackages:
self.__ospackages = ['bzip2', 'libbz2-dev', 'tar', 'wget',
'zlib1g-dev']
elif hpccm.config.g_linux_distro == linux_distro.CENTOS:
if not self.__ospackages:
self.__ospackages = ['bzip2', 'bzip2-devel', 'tar', 'wget',
'which', 'zlib-devel']
else: # pragma: no cover
raise RuntimeError('Unknown Linux distribution')
def __setup(self):
"""Construct the series of shell commands, i.e., fill in
self.__commands"""
# The download URL has the version format with underscores so
# pull apart the full version to get the individual
# components.
match = re.match(r'(?P<major>\d+)\.(?P<minor>\d+)\.(?P<revision>\d+)',
self.__version)
v_underscore = '{0}_{1}_{2}'.format(match.groupdict()['major'],
match.groupdict()['minor'],
match.groupdict()['revision'])
tarball = 'boost_{}.tar.bz2'.format(v_underscore)
url = '{0}/{1}'.format(self.__baseurl, tarball)
url = url.replace('__version__', self.__version)
# Python support requires pyconfig.h which is not part of the
# standard Python install. It requires the development
# package, python-dev or python-devel. So skip Python unless
# it's specifically enabled.
if not self.__python:
self.__bootstrap_opts.append('--without-libraries=python')
# Download source from web
self.__commands.append(self.download_step(url=url, directory=self.__wd))
self.__commands.append(self.untar_step(
tarball=posixpath.join(self.__wd, tarball), directory=self.__wd))
# Configure
self.__commands.append(
'cd {} && ./bootstrap.sh --prefix={} {}'.format(
posixpath.join(self.__wd, 'boost_{}'.format(v_underscore)),
self.__prefix,
' '.join(self.__bootstrap_opts)))
# Build and install
self.__commands.append('./b2 -j{} -q install'.format(self.__parallel))
# Set library path
libpath = posixpath.join(self.__prefix, 'lib')
if self.ldconfig:
self.__commands.append(self.ldcache_step(directory=libpath))
else:
self.environment_variables['LD_LIBRARY_PATH'] = '{}:$LD_LIBRARY_PATH'.format(libpath)
# Cleanup tarball and directory
self.__commands.append(self.cleanup_step(
items=[posixpath.join(self.__wd, tarball),
posixpath.join(self.__wd,
'boost_{}'.format(v_underscore))]))
def runtime(self, _from='0'):
"""Generate the set of instructions to install the runtime specific
components from a build in a previous stage.
# Examples
```python
b = boost(...)
Stage0 += b
Stage1 += b.runtime()
```
"""
self.rt += comment('Boost')
self.rt += copy(_from=_from, src=self.__prefix, dest=self.__prefix)
if self.ldconfig:
self.rt += shell(commands=[self.ldcache_step(
directory=posixpath.join(self.__prefix, 'lib'))])
self.rt += environment(variables=self.environment_step())
return str(self.rt)