As the title suggests, I'm trying to retrieve the target folder to hold some intermediate compilation products. I need the actual target folder as I would like to cache some values between compilations to speed them up significantly (otherwise I could just use a temporary directory).
My current approach is to parse the arguments of the rustc invocation (through std::env::args()
) and find the --out-dir, the code looks something like this:
// First we get the arguments for the rustc invocation
let mut args = std::env::args();
// Then we loop through them all, and find the value of "out-dir"
let mut out_dir = None;
while let Some(arg) = args.next() {
if arg == "--out-dir" {
out_dir = args.next();
}
}
// Finally we clean out_dir by removing all trailing directories, until it ends with target
let mut out_dir = PathBuf::from(out_dir.expect("Failed to find out_dir"));
while !out_dir.ends_with("target") {
if !out_dir.pop() {
// We ran out of directories...
panic!("Failed to find out_dir");
}
}
out_dir
Normally I would use something like an environment variable (think of OUT_DIR
), but I couldn't find anything that could help me.
So is there a proper way of doing this? Or should I file an issue to the rust dev team?
OUT_DIR
when running build scripts. I don't know if it is also set when running the compiler though… – Jmb