Conditional compilation for different versions of a crate?

I have a project using the clap crate version 2 right now and am now migrating to a distribution using clap version 3. I need to support both over the course of the next year. Is there a way to conditionally compile based on the clap version?

and am now migrating to a distribution using clap version 3.

What do you mean? Do you mean a linux distribution? Cargo will download any version of a crate on that you need independent of your linux distro.


This is probably best served by maintaining multiple project branches and bumping the major version of your own project simultaneously with the clap upgrade.

Yes it's two different distributions: Debian Bullseye and Debian Bookworm variants. I'm using their Rust packaging system that installs the dependencies locally from .deb packages so that they don't hit It also means I'm stuck with the packages for each distribution.

The build infrastructure I'm using doesn't have branch-specific rules like that. I may have found a workaround anyways. Nonetheless, I was asking if there was a way I could deal with this for conditional compilation in case I can't pull off that workaround.

The distribution you're working with should have a migration plan for the binaries they're shipping. Have you checked that?

If you're not actually working closely with the distribution to make something that ships in it, this restriction is not a good idea to put on yourself. If you are, they should already have a plan you can find.

When faced with the same situation I used features: clap2 and clap3. Each version was renamed when used so clap v2 things were presented to my project under the clap2 namespace.

I have a veneer that insulates my project from the differences then presents that under the clap namespace.

I think I ended up not providing a default; the user had to select which to use.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.