I was writing a lib calling a 3rd party binary file, but I don't want the user to download it manually, thus I modified the build script to download it automatically. This lib is basically some functions to call the downloaded binary, in the code, CARGO_MANIFEST_DIR is used to find correct binary file inside of this lib. The problem is that if I have another project using the lib, CARGO_MANIFEST_DIR will becomes the current project not the one downloading the binary file, thus I can't find it anymore. What should I do to cope with this?
You should download to the build output directory (
std::env::var("OUT_DIR").unwrap()) and not the source directory. The source directory should be considered immutable. If it isn't, cargo will even refuse to upload your crate to crates.io.
But the downloaded binary file is still can't be found by other project using the lib. The
OUT_DIRis not defined for the project without build script.
Note that the user may transfer the compiled binary into another machine and run it there.
build.rs is useful to find compile time dependencies, but not much with runtime dependencies.
You are right. To make it portable, it's better to use a environment variable to find the required binary. It should be users' responsibility to download the binary and specify the correct path to it.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.