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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# Copyright (c) 2019 Egor Tensin <egor@tensin.name>
# This file is part of the "cmake-common" project.
# For details, see https://github.com/egor-tensin/cmake-common.
# Distributed under the MIT License.
R'''Build a CMake project.
This script works nicely with boost-build from this package - it will set the
correct BOOST_ROOT value automatically.
Also, it facilitates building with different toolsets/for different platforms
with the help from the --toolset and --platform parameters.
Usage example:
$ project-build --configuration Release --install path/to/somewhere -- examples/simple build/
...
$ ./path/to/somewhere/bin/foo
foo
'''
import argparse
from contextlib import contextmanager
import logging
import os
import sys
import tempfile
from project.configuration import Configuration
from project.linkage import Linkage
from project.platform import Platform
from project.toolset import Toolset, ToolsetVersion
from project.utils import normalize_path, mkdir_parent, run, setup_logging
import project.version
DEFAULT_PLATFORM = Platform.AUTO
DEFAULT_CONFIGURATION = Configuration.DEBUG
DEFAULT_TOOLSET_VERSION = ToolsetVersion.default()
# This way of basically passing `-j` to make is more universal compared to
# _guessing_ that the build system is make and passing -j explicitly. Plus it
# works with older CMake versions, which don't support the --parallel flag.
cmake_env = os.environ.copy()
cmake_env['CMAKE_BUILD_PARALLEL_LEVEL'] = str(os.cpu_count())
def run_cmake(cmake_args):
return run(['cmake'] + cmake_args, env=cmake_env)
class GenerationPhase:
def __init__(
self,
src_dir,
build_dir,
platform=None,
configuration=None,
boost_dir=None,
link=None,
runtime_link=None,
install_dir=None,
cmake_args=None,
):
self.src_dir = normalize_path(src_dir)
self.build_dir = normalize_path(build_dir)
self.platform = platform or DEFAULT_PLATFORM
self.configuration = configuration or DEFAULT_CONFIGURATION
self.boost_dir = normalize_path(boost_dir) if boost_dir else None
link = link or Linkage.default_link()
runtime_link = runtime_link or Linkage.default_runtime_link()
self.link, self.runtime_link = Linkage.validate_linkage(link, runtime_link)
self.install_dir = normalize_path(install_dir) if install_dir else None
self.cmake_args = cmake_args or []
def _cmake_args(self, toolset):
result = []
result += toolset.cmake_args(self.build_dir, self.platform)
result += self.configuration.cmake_args()
result += self._cmake_boost_args()
result += self._cmake_extra_args()
result += self.runtime_link.cmake_args_runtime_link()
result += self.cmake_args
result += self._cmake_dir_args()
return result
def _cmake_boost_args(self):
if self.boost_dir is None:
return []
root = self.boost_dir
root = os.path.join(root, self.platform.boost_installdir(self.configuration))
args = [
f'-DBoost_ROOT={root}',
f'-DBOOST_ROOT={root}',
]
args += self.link.cmake_args_link()
return args
@staticmethod
def _cmake_extra_args():
return ['-DCMAKE_EXPORT_COMPILE_COMMANDS=ON']
def _cmake_dir_args(self):
args = []
if self.install_dir is not None:
args += [f'-DCMAKE_INSTALL_PREFIX={self.install_dir}']
# Important! -H must come as the last parameter, older CMake versions
# don't like it when it's not.
args += [f'-B{self.build_dir}', f'-H{self.src_dir}']
return args
def run(self, toolset):
run_cmake(self._cmake_args(toolset))
class BuildPhase:
def __init__(self, build_dir, install_dir=None, configuration=None):
self.build_dir = normalize_path(build_dir)
self.install_dir = install_dir
self.configuration = configuration or DEFAULT_CONFIGURATION
def _cmake_args(self, toolset):
result = ['--build', self.build_dir]
result += ['--config', str(self.configuration)]
if self.install_dir is not None:
result += ['--target', 'install']
result += ['--'] + toolset.build_system_args()
return result
def run(self, toolset):
run_cmake(self._cmake_args(toolset))
class BuildParameters:
def __init__(
self,
src_dir,
build_dir,
install_dir=None,
platform=None,
configuration=None,
boost_dir=None,
link=None,
runtime_link=None,
toolset_version=None,
cmake_args=None,
):
self.src_dir = normalize_path(src_dir)
self.build_dir = normalize_path(build_dir) if build_dir else None
self.install_dir = normalize_path(install_dir) if install_dir else None
self.platform = platform or DEFAULT_PLATFORM
self.configuration = configuration or DEFAULT_CONFIGURATION
self.boost_dir = normalize_path(boost_dir) if boost_dir else None
self.link = link or Linkage.default_link()
self.runtime_link = runtime_link or Linkage.default_runtime_link()
self.toolset_version = toolset_version or DEFAULT_TOOLSET_VERSION
self.cmake_args = cmake_args or []
@staticmethod
def from_args(args):
args = vars(args)
args.pop('help_toolsets', None)
return BuildParameters(**args)
@contextmanager
def create_build_dir(self):
if self.build_dir is not None:
logging.info('Build directory: %s', self.build_dir)
mkdir_parent(self.build_dir)
yield self.build_dir
return
with tempfile.TemporaryDirectory(
dir=os.path.dirname(self.src_dir)
) as build_dir:
logging.info('Build directory: %s', build_dir)
try:
yield build_dir
finally:
logging.info('Removing build directory: %s', build_dir)
def build(params):
with params.create_build_dir() as build_dir:
toolset = Toolset.make(params.toolset_version, params.platform)
gen_phase = GenerationPhase(
params.src_dir,
build_dir,
platform=params.platform,
configuration=params.configuration,
boost_dir=params.boost_dir,
link=params.link,
runtime_link=params.runtime_link,
install_dir=params.install_dir,
cmake_args=params.cmake_args,
)
gen_phase.run(toolset)
build_phase = BuildPhase(
build_dir,
install_dir=params.install_dir,
configuration=params.configuration,
)
build_phase.run(toolset)
def _parse_args(argv=None):
if argv is None:
argv = sys.argv[1:]
if '--help-toolsets' in argv:
sys.stdout.write(ToolsetVersion.help_toolsets())
sys.exit(0)
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'--help-toolsets',
action='store_true',
help='show detailed info about supported toolsets',
)
project.version.add_to_arg_parser(parser)
parser.add_argument(
'--toolset',
metavar='TOOLSET',
dest='toolset_version',
type=ToolsetVersion.parse,
default=DEFAULT_TOOLSET_VERSION,
help=f'toolset to use ({ToolsetVersion.usage()})',
)
platform_options = '/'.join(map(str, Platform.all()))
configuration_options = '/'.join(map(str, Configuration.all()))
parser.add_argument(
'--platform',
metavar='PLATFORM',
type=Platform.parse,
help=f'target platform ({platform_options})',
)
parser.add_argument(
'--configuration',
metavar='CONFIG',
type=Configuration.parse,
default=DEFAULT_CONFIGURATION,
help=f'build configuration ({configuration_options})',
)
parser.add_argument(
'--boost',
metavar='DIR',
dest='boost_dir',
type=normalize_path,
help='set Boost directory path',
)
linkage_options = '/'.join(map(str, Linkage.all()))
parser.add_argument(
'--link',
metavar='LINKAGE',
type=Linkage.parse,
default=Linkage.default_link(),
help=f'how Boost libraries are linked ({linkage_options})',
)
parser.add_argument(
'--runtime-link',
metavar='LINKAGE',
type=Linkage.parse,
default=Linkage.default_runtime_link(),
help=f'which runtime to link to ({linkage_options})',
)
parser.add_argument(
'--install',
metavar='DIR',
dest='install_dir',
type=normalize_path,
help='install directory',
)
parser.add_argument(
'--cmake-arg',
metavar='ARG',
dest='cmake_args',
action='extend',
nargs='*',
help='additional CMake arguments, to be passed verbatim',
)
parser.add_argument('src_dir', type=normalize_path, help='source directory')
parser.add_argument(
'build_dir',
type=normalize_path,
nargs='?',
help='build directory (a temporary directory if omitted)',
)
return parser.parse_args(argv)
def main(argv=None):
args = _parse_args(argv)
with setup_logging():
build(BuildParameters.from_args(args))
if __name__ == '__main__':
main()
|