3

As a Rust driver crate developer, I would like to perform below steps during my crate installation/download when used by any other Rust program:

  1. Check the platform i.e. Windows or UNIX or macOS.
  2. Download the corresponding platform-specific binary from an external website.
  3. Set an environment variable pointing to the download location.

I know this is possible in Node or Python or R but not sure if this is possible in Rust.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • `cargo install` expects to build you crate and should not download binaries. And if you don't want to depend on `cargo` or force your users to have a Rust toolchain installed, use whatever install/packaging system your target platform prefers. – mcarton Dec 15 '20 at 10:15

1 Answers1

3

You can use Build script to achieve that (but it is not what you should do, please see note below).

The script will be compiled and executed before cargo start building your library.

  1. Inside the script you can use cfg attribute to check platform.
  2. There is a bunch of libraries to download something via HTTP, for example reqwest
  3. You can set environment variable via cargo:rustc-env=VAR=VALUE

IMPORTANT NOTE

Most of Rust users doesn't expect that kind of behavior from build script. There may be dozen of problems with the approach. Just few of them from the top of my head:

  • First of all there may be security issues.
  • The approach will break builds at client side.

I believe it's better to upload all binaries you need as a part of the crate. You can use include_bytes! for that.

MaxV
  • 2,601
  • 3
  • 18
  • 25
  • 1
    A build script should not be downloading precompiled target-specific binaries! And step 3 only works during the build, which doesn't seem to be what the OP wants. – mcarton Dec 15 '20 at 10:17
  • Hi @macarton. Thanks for your comment. `step 3 only works during the build, which doesn't seem to be what the OP wants.` -- it's hard to say but if OP would like to relay on the value of variable in sources then it may be achieved by providing the variable to build phase and then use it via `env!`. – MaxV Dec 15 '20 at 10:27
  • Regarding `A build script should not be downloading precompiled target-specific binaries!`. I agree with you, it'll be a bad behavior. But I'm not sure if there are any mechanism in place that prevents it. Could you please help me to make my note `There is a couple of problems` to looks better? It seems like include_bytes! should work for OP. – MaxV Dec 15 '20 at 10:29
  • If you would like to add `-1` then please provide a short comment to help me to improve my answer. I know that kind of behavior is not expected. I know build scripts was created to solve different kind of problem. But it looks like the solution should work. If we, as a Rust community, are not happy with that ability we should introduce something to prevent it to happens. I don't believe keeping it in secret will improve the situation. – MaxV Dec 15 '20 at 10:40
  • I agree with John and hence was trying to write a separate rust file to do the same. But currently I am stuck with the download part where I am using this link example "https://rust-lang-nursery.github.io/rust-cookbook/web/clients/download.html" but it doesn't seem to be working and is really complex inciting the use of async and all. Is there any other simple way to download a file using RUST? i.e. I want to download a binary from a URL and store in local directory. – BINIT KUMAR Dec 16 '20 at 12:23