Skip to content

Commit 5ae3fa3

Browse files
committed
Allow sharing of cargo target directory between builds
This flag will cause all colcon-cargo invocations of the 'cargo' executable to use a shared target directory, thereby sharing build artifacts between builds. I don't yet understand all of the nuances of this change and what dragons be yet lurking, so for now the change is hidden behind the colcon feature flag 'shared_cargo_target_dir'.
1 parent 518a4f9 commit 5ae3fa3

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

colcon_cargo/task/cargo/build.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from colcon_cargo.task.cargo import CARGO_EXECUTABLE
99
from colcon_core.environment import create_environment_scripts
10+
from colcon_core.feature_flags import is_feature_flag_set
1011
from colcon_core.logging import colcon_logger
1112
from colcon_core.plugin_system import satisfies_version
1213
from colcon_core.shell import create_environment_hook, get_command_environment
@@ -58,12 +59,18 @@ async def build( # noqa: D102
5859
return rc
5960

6061
# Clean up the build dir
61-
build_dir = Path(args.build_base)
62-
if args.clean_build:
63-
if build_dir.is_symlink():
64-
build_dir.unlink()
65-
elif build_dir.exists():
66-
shutil.rmtree(build_dir)
62+
self._build_dir = Path(args.build_base)
63+
if is_feature_flag_set('shared_cargo_target_dir'):
64+
self._build_dir = self._build_dir.parent / '.cargo_target'
65+
if args.clean_build:
66+
logger.warning(
67+
"The '--clean-build' argument has no effect when using "
68+
"'shared_cargo_target_dir'")
69+
elif args.clean_build:
70+
if self._build_dir.is_symlink():
71+
self._build_dir.unlink()
72+
elif self._build_dir.exists():
73+
shutil.rmtree(self._build_dir)
6774

6875
if CARGO_EXECUTABLE is None:
6976
raise RuntimeError("Could not find 'cargo' executable")
@@ -111,14 +118,13 @@ def _prepare(self, env, additional_hooks):
111118

112119
# Overridden by colcon-ros-cargo
113120
def _build_cmd(self, cargo_args):
114-
args = self.context.args
115121
pkg = self.context.pkg
116122
cmd = [
117123
CARGO_EXECUTABLE,
118124
'build',
119125
'--quiet',
120126
'--package', pkg.name,
121-
'--target-dir', args.build_base,
127+
'--target-dir', str(self._build_dir),
122128
]
123129
if not any(
124130
arg == '--profile' or arg.startswith('--profile=')
@@ -138,7 +144,7 @@ def _install_cmd(self, cargo_args):
138144
'--locked',
139145
'--path', '.',
140146
'--root', args.install_base,
141-
'--target-dir', args.build_base,
147+
'--target-dir', str(self._build_dir),
142148
'--no-track',
143149
]
144150
if not any(

0 commit comments

Comments
 (0)