Skip to content

Commit

Permalink
Include package source information on cargo dependencies
Browse files Browse the repository at this point in the history
This information is necessary to perform arbitrary dependency patching
later in the cargo build process.
  • Loading branch information
cottsay committed Dec 23, 2024
1 parent bb6308e commit 9f1da80
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions colcon_cargo/package_augmentation/cargo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

from pathlib import Path

from colcon_cargo.package_identification.cargo import read_cargo_toml
from colcon_core.dependency_descriptor import DependencyDescriptor
from colcon_core.package_augmentation \
Expand Down Expand Up @@ -42,7 +44,7 @@ def _augment_package(
if not metadata.metadata.get('version'):
metadata.metadata['version'] = version

dependencies = extract_dependencies(content)
dependencies = extract_dependencies(content, metadata.path)
for k, v in dependencies.items():
metadata.dependencies[k] |= v

Expand All @@ -52,17 +54,18 @@ def _augment_package(
metadata.metadata['maintainers'] += authors


def extract_dependencies(content):
def extract_dependencies(content, path):
"""
Get the dependencies of a Cargo package.
:param content: The dictionary content of the Cargo.toml file
:param path: The directory where the Cargo.toml resides
:returns: The dependencies
:rtype: dict(string, set(DependencyDescriptor))
"""
name = content.get('name')
depends = {
create_dependency_descriptor(k, v)
create_dependency_descriptor(k, v, path)
for k, v in content.get('dependencies', {}).items()
if k != name
}
Expand All @@ -72,17 +75,30 @@ def extract_dependencies(content):
}


def create_dependency_descriptor(name, constraints):
def create_dependency_descriptor(name, constraints, path):
"""
Create a dependency descriptor from a Cargo dependency specification.
:param name: The name of the dependee
:param constraints: The dependency constraints, either a string or
a dict
:param path: The directory from where relative paths should be
resolved
:rtype: DependencyDescriptor
"""
if isinstance(constraints, dict):
dep_path = constraints.get('path')
if dep_path:
full_dep_path = Path.cwd() / path / dep_path
source = full_dep_path.absolute().as_uri()
else:
source = constraints.get('git') or \
constraints.get('registry')
else:
source = None
metadata = {
'origin': 'cargo',
'cargo_source': source,
}
# TODO: Interpret SemVer constraints and add appropriate constraint
# metadata. Handling arbitrary wildcards will be non-trivial.
Expand Down

0 comments on commit 9f1da80

Please sign in to comment.