Specifying a build directory

I tend to build my C/C++ projects on a ram drive (for performance and to avoid unnecessary wear and tear on my persistent storage). I want to keep my sources on persistent storage and only build on the ram drive. I figured one can use --target-dir to tell cargo to build in a separate directory. However, I would like to be able to set this globally, since I'm likely to want to build everything on the ram drive. A partial solution to this is using the environment variable CARGO_TARGET_DIR, but this - as far as I can tell - is only used to set the project's build directory, and thus is not appropriate to set globally in .profile (or similar).

Is there some what to specify cargo variables in CARGO_TARGET_DIR? Like CARGO_TARGET_DIR=$$HOME/tmp/bld/$$CARGO_PROJECT_NAME?

Or rather .. I'm pretty sure there isn't, so what I'm essentially asking is if someone knows of way to accomplish global per-project build directories. (Yes, I realize there are potential issues relating to name collisions).

I saw that one can set the target directory in Cargo.toml, but I don't want to tell others how to build the project -- so I'm looking for a machine-local way to configure build directories. (Plus, my different build systems have their ram drive at different locations).

Hi, I'm not expert with this but I'm using a makefile or shell script's to build my projects with this kind of specific settings. Those makefiles/shell scripts could be excluded from being published into crates.io (with the exclude section in Cargo.toml or to your github (with the .gitignore settings) if do not want to share this kind of information.

1 Like

Alias or function can do the trick; depends on shell your using.
alias cargo="cargo --target-dir $HOME/tmp/bld/$(basename $(pwd))"

1 Like