-
Notifications
You must be signed in to change notification settings - Fork 337
/
cmake.py
318 lines (265 loc) · 11 KB
/
cmake.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
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
316
317
318
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project
"""
CMake-based build system
"""
from __future__ import print_function
from rez.build_system import BuildSystem
from rez.build_process import BuildType
from rez.resolved_context import ResolvedContext
from rez.exceptions import BuildSystemError
from rez.utils.execution import create_forwarding_script
from rez.packages import get_developer_package
from rez.utils.platform_ import platform_
from rez.config import config
from rez.utils.which import which
from rez.vendor.schema.schema import Or
from rez.vendor.six import six
from rez.shells import create_shell
import functools
import os.path
import sys
import os
basestring = six.string_types[0]
class RezCMakeError(BuildSystemError):
pass
class CMakeBuildSystem(BuildSystem):
"""The CMake build system.
The 'cmake' executable is run within the build environment. Rez supplies a
library of cmake macros in the 'cmake_files' directory; these are added to
cmake's searchpath and are available to use in your own CMakeLists.txt
file.
The following CMake variables are available:
- REZ_BUILD_TYPE: One of 'local', 'central'. Describes whether an install
is going to the local packages path, or the release packages path.
- REZ_BUILD_INSTALL: One of 0 or 1. If 1, an installation is taking place;
if 0, just a build is occurring.
"""
build_systems = {
'eclipse': "Eclipse CDT4 - Unix Makefiles",
'codeblocks': "CodeBlocks - Unix Makefiles",
'make': "Unix Makefiles",
'nmake': "NMake Makefiles",
'mingw': "MinGW Makefiles",
'xcode': "Xcode",
'ninja': "Ninja",
}
build_targets = ["Debug", "Release", "RelWithDebInfo"]
schema_dict = {
"build_target": Or(*build_targets),
"build_system": Or(*list(build_systems.keys())),
"cmake_args": [basestring],
"cmake_binary": Or(None, basestring),
"make_binary": Or(None, basestring)
}
@classmethod
def name(cls):
return "cmake"
@classmethod
def child_build_system(cls):
return "make"
@classmethod
def is_valid_root(cls, path, package=None):
return os.path.isfile(os.path.join(path, "CMakeLists.txt"))
@classmethod
def bind_cli(cls, parser, group):
settings = config.plugins.build_system.cmake
group.add_argument("--bt", "--build-target", dest="build_target",
type=str, choices=cls.build_targets,
default=settings.build_target,
help="set the build target (default: %(default)s).")
group.add_argument("--bs", "--cmake-build-system",
dest="cmake_build_system",
choices=list(cls.build_systems.keys()),
default=settings.build_system,
help="set the cmake build system (default: %(default)s).")
def __init__(self, working_dir, opts=None, package=None, write_build_scripts=False,
verbose=False, build_args=[], child_build_args=[]):
super(CMakeBuildSystem, self).__init__(
working_dir,
opts=opts,
package=package,
write_build_scripts=write_build_scripts,
verbose=verbose,
build_args=build_args,
child_build_args=child_build_args)
self.settings = self.package.config.plugins.build_system.cmake
self.build_target = getattr(opts, "build_target", self.settings.build_target)
self.cmake_build_system = getattr(opts, "cmake_build_system", self.settings.build_system)
if self.cmake_build_system == 'xcode' and platform_.name != 'osx':
raise RezCMakeError("Generation of Xcode project only available "
"on the OSX platform")
def build(self, context, variant, build_path, install_path, install=False,
build_type=BuildType.local):
def _pr(s):
if self.verbose:
print(s)
# find cmake binary
if self.settings.cmake_binary:
exe = self.settings.cmake_binary
else:
exe = context.which("cmake", fallback=True)
if not exe:
raise RezCMakeError("could not find cmake binary")
found_exe = which(exe)
if not found_exe:
raise RezCMakeError("cmake binary does not exist: %s" % exe)
sh = create_shell()
# assemble cmake command
cmd = [found_exe]
# cmd.append("-d") # see #1055
cmd.append(self.working_dir)
cmd += (self.settings.cmake_args or [])
cmd += (self.build_args or [])
cmd.append("-DCMAKE_INSTALL_PREFIX=%s" % install_path)
cmd.append("-DCMAKE_MODULE_PATH=%s" %
sh.get_key_token("CMAKE_MODULE_PATH").replace('\\', '/'))
cmd.append("-DCMAKE_BUILD_TYPE=%s" % self.build_target)
cmd.append("-DREZ_BUILD_TYPE=%s" % build_type.name)
cmd.append("-DREZ_BUILD_INSTALL=%d" % (1 if install else 0))
cmd.extend(["-G", self.build_systems[self.cmake_build_system]])
if config.rez_1_cmake_variables and \
not config.disable_rez_1_compatibility and \
build_type == BuildType.central:
cmd.append("-DCENTRAL=1")
# execute cmake within the build env
_pr("Executing: %s" % ' '.join(cmd))
if not os.path.abspath(build_path):
build_path = os.path.join(self.working_dir, build_path)
build_path = os.path.realpath(build_path)
actions_callback = functools.partial(
self._add_build_actions,
context=context,
package=self.package,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path
)
post_actions_callback = functools.partial(
self.add_pre_build_commands,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path
)
# run the build command and capture/print stderr at the same time
retcode, _, _ = context.execute_shell(
command=cmd,
block=True,
cwd=build_path,
actions_callback=actions_callback,
post_actions_callback=post_actions_callback
)
ret = {}
if retcode:
ret["success"] = False
return ret
if self.write_build_scripts:
# write out the script that places the user in a build env, where
# they can run make directly themselves.
build_env_script = os.path.join(build_path, "build-env")
create_forwarding_script(build_env_script,
module=("build_system", "cmake"),
func_name="_FWD__spawn_build_shell",
working_dir=self.working_dir,
build_path=build_path,
variant_index=variant.index,
install=install,
install_path=install_path)
ret["success"] = True
ret["build_env_script"] = build_env_script
return ret
# assemble make command
make_binary = self.settings.make_binary
if not make_binary:
if self.cmake_build_system == "mingw":
make_binary = "mingw32-make"
elif self.cmake_build_system == "nmake":
make_binary = "nmake"
elif self.cmake_build_system == "ninja":
make_binary = "ninja"
else:
make_binary = "make"
cmd = [make_binary] + (self.child_build_args or [])
# nmake has no -j
if make_binary != "nmake":
if not any(x.startswith("-j") for x in (self.child_build_args or [])):
n = variant.config.build_thread_count
cmd.append("-j%d" % n)
# execute make within the build env
_pr("\nExecuting: %s" % ' '.join(cmd))
retcode, _, _ = context.execute_shell(
command=cmd,
block=True,
cwd=build_path,
actions_callback=actions_callback,
post_actions_callback=post_actions_callback
)
if not retcode and install and "install" not in cmd:
cmd.append("install")
# execute make install within the build env
_pr("\nExecuting: %s" % ' '.join(cmd))
retcode, _, _ = context.execute_shell(
command=cmd,
block=True,
cwd=build_path,
actions_callback=actions_callback,
post_actions_callback=post_actions_callback
)
ret["success"] = (not retcode)
return ret
@classmethod
def _add_build_actions(cls, executor, context, package, variant,
build_type, install, build_path, install_path=None):
settings = package.config.plugins.build_system.cmake
cmake_path = os.path.join(os.path.dirname(__file__), "cmake_files")
template_path = os.path.join(os.path.dirname(__file__), "template_files")
cls.add_standard_build_actions(
executor=executor,
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path
)
executor.env.CMAKE_MODULE_PATH.append(cmake_path.replace('\\', '/'))
executor.env.REZ_BUILD_DOXYFILE = os.path.join(template_path, 'Doxyfile')
executor.env.REZ_BUILD_INSTALL_PYC = '1' if settings.install_pyc else '0'
def _FWD__spawn_build_shell(working_dir, build_path, variant_index, install,
install_path=None):
# This spawns a shell that the user can run 'make' in directly
context = ResolvedContext.load(os.path.join(build_path, "build.rxt"))
package = get_developer_package(working_dir)
variant = package.get_variant(variant_index)
config.override("prompt", "BUILD>")
actions_callback = functools.partial(
CMakeBuildSystem._add_build_actions,
context=context,
package=package,
variant=variant,
build_type=BuildType.local,
install=install,
build_path=build_path,
install_path=install_path
)
post_actions_callback = functools.partial(
CMakeBuildSystem.add_pre_build_commands,
variant=variant,
build_type=BuildType.local,
install=install,
build_path=build_path,
install_path=install_path
)
retcode, _, _ = context.execute_shell(
block=True,
cwd=build_path,
actions_callback=actions_callback,
post_actions_callback=post_actions_callback
)
sys.exit(retcode)
def register_plugin():
return CMakeBuildSystem