-
Notifications
You must be signed in to change notification settings - Fork 20
/
build.py
105 lines (86 loc) · 3.32 KB
/
build.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
# Copyright 2018 Easymov Robotics
# Licensed under the Apache License, Version 2.0
from pathlib import Path
import shutil
from colcon_cargo.task.cargo import CARGO_EXECUTABLE
from colcon_core.environment import create_environment_scripts
from colcon_core.logging import colcon_logger
from colcon_core.plugin_system import satisfies_version
from colcon_core.shell import create_environment_hook, get_command_environment
from colcon_core.task import run
from colcon_core.task import TaskExtensionPoint
logger = colcon_logger.getChild(__name__)
class CargoBuildTask(TaskExtensionPoint):
"""Build Cargo packages."""
def __init__(self): # noqa: D107
super().__init__()
satisfies_version(TaskExtensionPoint.EXTENSION_POINT_VERSION, '^1.0')
def add_arguments(self, *, parser): # noqa: D102
parser.add_argument(
'--cargo-args',
nargs='*', metavar='*', type=str.lstrip,
help='Pass arguments to Cargo projects. '
'Arguments matching other options must be prefixed by a space,\n'
'e.g. --cargo-args " --help"')
parser.add_argument(
'--clean-build',
action='store_true',
help='Remove old build dir before the build.')
async def build( # noqa: D102
self, *, additional_hooks=None, skip_hook_creation=False
):
if additional_hooks is None:
additional_hooks = []
args = self.context.args
logger.info(
"Building Cargo package in '{args.path}'".format_map(locals()))
try:
env = await get_command_environment(
'build', args.build_base, self.context.dependencies)
except RuntimeError as e:
logger.error(str(e))
return 1
self.progress('prepare')
rc = self._prepare(env, additional_hooks)
if rc:
return rc
# Clean up the build dir
build_dir = Path(args.build_base)
if args.clean_build:
if build_dir.is_symlink():
build_dir.unlink()
elif build_dir.exists():
shutil.rmtree(build_dir)
# Invoke build step
if CARGO_EXECUTABLE is None:
raise RuntimeError("Could not find 'cargo' executable")
cargo_args = args.cargo_args
if cargo_args is None:
cargo_args = []
cmd = self._build_cmd(cargo_args)
self.progress('build')
rc = await run(
self.context, cmd, cwd=self.context.pkg.path, env=env)
if rc and rc.returncode:
return rc.returncode
if not skip_hook_creation:
create_environment_scripts(
self.context.pkg, args, additional_hooks=additional_hooks)
# Overridden by colcon-ros-cargo
def _prepare(self, env, additional_hooks):
pkg = self.context.pkg
additional_hooks += create_environment_hook(
'cargo_{}_path'.format(pkg.name),
Path(self.context.args.install_base), pkg.name,
'PATH', 'bin'
)
# Overridden by colcon-ros-cargo
def _build_cmd(self, cargo_args):
args = self.context.args
return [
CARGO_EXECUTABLE, 'install',
'--force',
'--quiet',
'--path', args.path,
'--root', args.install_base,
] + cargo_args