Cargo env file?
from japps13@lemmy.physfluids.fr to rust@programming.dev on 18 Mar 2024 17:40
https://lemmy.physfluids.fr/post/351875

Hello,

I have a project for which some machines have only a subset of features set. Currently, I write

cargo run --no-default-features --features "toto,titi" --bin my_bin

This is a bit cumbersome and I sometimes forget the –no-default-features --features part. I was wondering if there is a local config file that I could put in the directory to instruct cargo to use this particular subset of features. I guess this should be in the documentation, but I didn’t find it.

#rust

threaded - newest

doot@social.bug.expert on 18 Mar 2024 17:53 next collapse

doc.rust-lang.org/cargo/reference/features.html

?

fnmain@programming.dev on 18 Mar 2024 19:43 next collapse

The way I can think of to do this is to make a program called cargo-build-env which reads a file of your choosing with the features and executes cargo, then place that binary in your ~/.cargo/bin directory. Then you can run cargo build-env to build your program

The program can be in any language as long as it’s executable, including shell script

sugar_in_your_tea@sh.itjust.works on 18 Mar 2024 20:22 collapse

Yeah, a simple bash script should do. Such as:

#!/usr/bin/env bash

BUILD_FEATURES=${BUILD_FEATURES:=default}

cargo run --no-default-features --features "${BUILD_FEATURES}" --bin my_bin

Then just set a BUILD_FEATURES env var.

TehPers@beehaw.org on 18 Mar 2024 21:15 next collapse

A couple options come to mind to me:

  1. Only have default features that every platform will support. This way you don’t need to opt-out, instead you always opt-in per platform.
  2. Use other conditions in cfg more. If your features are conditional based on OS, architecture, CPU features, etc then you probably are better off using conditions other than feature. See the reference for more information.
  3. Use a separate build script. Cargo doesn’t scale well by itself for larger projects, so separate scripts which run cargo are common. You could use Makefile, shell scripts, or even look into tools like cargo-make and just. From there, you can do platform-specific build logic or even read variables from a .env like you mentioned you wanted.
japps13@lemmy.physfluids.fr on 21 Mar 2024 10:31 collapse

Going opt-in instead of opt-out does not change the fact that I would still have to toggle the features manually. To be more specific, my use case is that I have a program to control cameras in my lab. But not all computers have the libraries for all cameras. So, every supported library can be enable/disable using a feature But the program being still in active development, I am frequently using cargo run, cargo check, cargo install, on different computers with different libraries installed. What would be convenient would be to have a configuration file on each computer, specifying that we will build only for PCO camera on this computer, only for Photometrics camera on this one, only Ximea and PCO on this one, instead of having to remember to toggle the relevant features every time. A shell script is not very convenient because I use different commands, run, check, install etc.

TehPers@beehaw.org on 21 Mar 2024 18:50 collapse

You should look at the tools I linked. cargo-make would just change your flow to cargo make run, cargo make check, etc. and just has similar benefits. You’d handle the computer-specific logic in there, using a .env per computer if you want.

japps13@lemmy.physfluids.fr on 22 Mar 2024 14:25 collapse

Ok, I’ll look into that then. Thanks.

rutrum@lm.paradisus.day on 18 Mar 2024 22:50 next collapse

Not cargo, but I use justfiles in all my projects: github.com/casey/just Its great for aliasing project-specific commands like what you have.

owsei@programming.dev on 20 Mar 2024 10:24 collapse

You can setup a cargo configuration file, in it you put a manifest and define some profiles, and in them define what features you want compiled

japps13@lemmy.physfluids.fr on 21 Mar 2024 10:27 collapse

This would look like it would be what I am looking for, but the documentation of the configuration file does not mention features.

owsei@programming.dev on 21 Mar 2024 10:54 collapse

doc.rust-lang.org/cargo/reference/manifest.html

it’s the sixth from the bottom in the table of contents