Skip to content

Commit

Permalink
Add argument to select .cargo/config.toml path
Browse files Browse the repository at this point in the history
  • Loading branch information
kmn4tor authored and esteve committed Aug 29, 2024
1 parent a1c4900 commit c66185c
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions colcon_ros_cargo/task/ament_cargo/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def add_arguments(self, *, parser): # noqa: D102
'By default, dependencies are looked up only in the installation '
'prefixes. This option is useful for setting up a '
'.cargo/config.toml for subsequent builds with cargo.')
parser.add_argument(
'--config-path',
type=Path,
help='The path to store the .cargo/config.toml '
'By default, the configuration will be stored at the '
'colcon workspace top level directory. Use this option to '
'indicate a path above the resolved path of the package sources '
'(e.g the pointed path if the source is under a symbolic link)')

def _prepare(self, env, additional_hooks):
args = self.context.args
Expand All @@ -66,7 +74,7 @@ def _prepare(self, env, additional_hooks):
# Hence, the installed package paths need to be accumulated.
new_package_paths.update(package_paths)
package_paths = new_package_paths
write_cargo_config_toml(package_paths)
self.write_cargo_config_toml(package_paths)

additional_hooks += create_environment_hook(
'ament_prefix_path',
Expand All @@ -93,19 +101,33 @@ def _build_cmd(self, cargo_args):
def _install_cmd(self, cargo_args): # noqa: D102
pass

def write_cargo_config_toml(self, package_paths):
"""Write the resolved package paths to config.toml.
def write_cargo_config_toml(package_paths):
"""Write the resolved package paths to config.toml.
:param package_paths: A mapping of package names to paths
"""
patches = {pkg: {'path': str(path)} for pkg, path in package_paths.items()}
content = {'patch': {'crates-io': patches}}
config_dir = Path.cwd() / '.cargo'
config_dir.mkdir(exist_ok=True)
cargo_config_toml_out = config_dir / 'config.toml'
with cargo_config_toml_out.open('w') as toml_file:
toml.dump(content, toml_file)
:param package_paths: A mapping of package names to paths
"""
args = self.context.args
src_dir = Path(self.context.pkg.path)
patches = {pkg: {'path': str(path)} for pkg, path in package_paths.items()}
content = {'patch': {'crates-io': patches}}
if args.config_path:
config_dir = args.config_path.resolve() / '.cargo'
else:
config_dir = Path.cwd() / '.cargo'
# The current package directory might be a link to another directory.
# However, cargo only looks for configurations in the package directory
# and in all its parent directories.
# Hence, if the package directory is link, ./cargo/config.toml
# should be installed above the directory pointed to
# in order cargo to necessarily hit it.
if src_dir.absolute() != src_dir.resolve():
logger.warn('The package source path may be under a symbolic link. '
'Use --config-path option to store .cargo/config.toml '
'in a way it will be hit by cargo')
config_dir.mkdir(exist_ok=True)
cargo_config_toml_out = config_dir / 'config.toml'
cargo_config_toml_out.unlink(missing_ok=True)
toml.dump(content, cargo_config_toml_out.open('w'))


def find_installed_cargo_packages(env):
Expand Down

0 comments on commit c66185c

Please sign in to comment.