Download prebuilt in build.rs

in my library's build.rs, i need to download and cache prebuilt library from github release. my problem is where's the best directory for doing this, how to avoid race, and if there are some helping tools.

existing crates:
ort-sys donwloads to user cache dir. seems no race protection.
tensorflow-sys downloads to OUT_DIR. no race protection. but cargo build already has lock, so it's fine?

my prebulit library is about 20M for each arch.

update: found cached-path useful for download, unzip and cache in one call.

Please be sure to document that your package does this, and provide an alternative (perhaps configurable via feature or environment variable). Many users care about offline, fully vendored, from-source-only, or reproducible builds. In the future, build scripts might be outright prohibited from network access or have to declare it.

downloads to OUT_DIR. no race protection. but cargo build already has lock, so it's fine?

Yes, that should be fine. Lots of things would break if build scripts could be run overlapped like that. You get the unique OUT_DIR and you get assurance only one build script run will be accessing it.

3 Likes