colcon / colcon-cargo

An extension for colcon-core to support Rust projects built with Cargo
http://colcon.readthedocs.io
Apache License 2.0
30 stars 20 forks source link

Support for Rust libraries in colcon workspace #17

Open mxgrey opened 2 years ago

mxgrey commented 2 years ago

Firstly, much thanks to the developers and maintainers for getting this plugin started! I'm very excited by the prospect of using Rust in ROS2, and more generally having a nice automated workspace overlay tool for Rust.

After trying out the tool, I noticed that Rust library packages seem to explicitly not be supported. Trying to include a library package, I get the following error message:

error: no packages found with binaries or examples

I assume this is an intentional limitation, since it currently seems quite difficult to have Cargo redirect to a local dependency without modifying the Cargo.toml of the package that needs to use the local dependency.

While researching how we might be able to make libraries and dependency overlays work I learned about Cargo config files which (will) offer a patch-in-config feature. This allows a [patch] section to be put into a .cargo/config.toml file, and those dependency patches will be applied to every package that belongs to the workspace that the config file is provided for. This feature is currently in the beta channel (version 1.56), which looks like it will move to stable on Oct 21.

I just did a quick manually set-up test of this with the following structure (relative to the root of a hypothetical colcon workspace):

.cargo/config.toml:

[patch.crates-io]
hello_lib = {path = "src/hello_lib"}

src/hello_lib/Cargo.toml:

[package]
name = "hello_lib"
version = "0.1.0"
edition = "2018"

src/hello_lib/src/lib.rs:

pub fn say_hello() {
    println!("Hello, library!");
}

src/hello_app/Cargo.toml:

[package]
name = "hello_app"
version = "0.1.0"
edition = "2018"

[dependencies]
hello_lib = "*"

src/hello_app/main.rs:

use hello_lib;

fn main() {
    hello_lib::say_hello();
}

Then run the following command from src/hello_app:

cargo +beta run

You should find that the hello_app successfully builds, runs, and prints out Hello, library!. (Make sure you have run rustup install beta and rustup update before running the command.)

I think this would be a solid strategy for fully integrating Cargo into colcon. The documentation for this feature explicitly mentions that the feature is intended for external build tools (such as colcon):

Patching through cargo configuration files is generally only appropriate when the patch section is automatically generated by an external build tool.

Does anyone have thoughts on this? It's exciting to think that we might be able to have complete colcon+cargo integration in just a few weeks when this feature goes stable.

allsey87 commented 2 years ago

Hi @mxgrey, I need Cargo library support and will look into this/work towards creating a PR this week. @esteve @bergercookie do you have time at the moment to review/merge a PR? I only ask since this thread was left unanswered and there hasn't been much activity on this repository.

esteve commented 2 years ago

@allsey87 that'd be great, thank you so much! Yeah, I can review your PR and merge it.

mxgrey commented 2 years ago

We may want to coordinate effort with @nnmm . I believe he was thinking about incorporating these concepts into his PRs.

nnmm commented 2 years ago

See the colcon/colcon-ros-cargo repository (currently still in a PR).