#cargo #shared #clean #directory #temporary #compile-time #crates #multiple #scratch

scratch

Compile-time temporary directory shared by multiple crates and erased by cargo clean

1 stable release

Uses old Rust 2015

1.0.0 Sep 17, 2020

#71 in Build Utils

Download history 2727/week @ 2021-04-02 4062/week @ 2021-04-09 3945/week @ 2021-04-16 3877/week @ 2021-04-23 4597/week @ 2021-04-30 4259/week @ 2021-05-07 4820/week @ 2021-05-14 5505/week @ 2021-05-21 4553/week @ 2021-05-28 6825/week @ 2021-06-04 5047/week @ 2021-06-11 5889/week @ 2021-06-18 6079/week @ 2021-06-25 4961/week @ 2021-07-02 5967/week @ 2021-07-09 6073/week @ 2021-07-16

14,171 downloads per month
Used in 17 crates (2 directly)

MIT/Apache

11KB

Shared scratch for build scripts

github crates.io docs.rs build status

This crate exposes a compile-time temporary directory sharable by multiple crates in a build graph and erased by cargo clean.

The intended usage is from a build.rs Cargo build script, or more likely from a library which is called by other crates' build scripts.

# Cargo.toml

[build-dependencies]
scratch = "1.0"
// build.rs

fn main() {
    let dir = scratch::path("mycrate");
    // ... write or read inside of that path
}

Comparisons

Comparison to std::env::var_os("OUT_DIR"):

  • This functionality is different from OUT_DIR in that the same directory path will be seen by all crates whose build passes a matching suffix argument, and each crate can see content placed into the directory by those other crates' build scripts that have already run.

  • This functionality is similar to OUT_DIR in that both are erased when a cargo clean is executed.

Comparison to std::env::temp_dir():

  • This functionality is similar to temp_dir() in that stuff that goes in is visible to subsequently running build scripts.

  • This functionality is different from temp_dir() in that cargo clean cleans up the contents.


Tips

You'll want to consider what happens when Cargo runs multiple build scripts concurrently that access the same scratch dir. In some use cases you likely want some synchronization over the contents of the scratch directory, such as by an advisory file lock. On Unix-like and Windows host systems the simplest way to sequence the build scripts such that each one gets exclusive access one after the other is something like:

use std::fs::{self, File};
use std::io;

fn main() -> io::Result<()> {
    let dir = scratch::path("demo");
    let flock = File::create(dir.join(".lock"))?;
    fs2::FileExt::lock_exclusive(&flock)?;

    // ... now do work
}

This simplest approach is fine for a cache which is slow to fill (maybe a large download) but fast/almost immediate to use. On the other hand if the build scripts using your cache will take a while to complete even if they only read from the scratch directory, a different approach which allows readers to make progress in parallel would perform better.

use std::fs::{self, File};
use std::io;

fn main() -> io::Result<()> {
    let dir = scratch::path("demo");
    let flock = File::create(dir.join(".lock"))?;
    let sdk = dir.join("thing.sdk");

    if !sdk.exists() {
        fs2::FileExt::lock_exclusive(&flock)?;
        if !sdk.exists() {
            let download_location = sdk.with_file_name("thing.sdk.partial");
            download_sdk_to(&download_location)?;
            fs::rename(&download_location, &sdk)?;
        }
        fs2::FileExt::unlock(&flock)?;
    }

    // ... now use the SDK
}

For use cases that are not just a matter of the first build script writing to the directory and the rest reading, more elaborate schemes involving lock_shared might be something to consider.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps