'Use build.rs to setup enviroment

I have a project where a dependency requires libclang on the system to build, I want to avoid needing to manually install this on the system and instead use build.rs to automatically download this. Is there a way to run build.rs before dependencies to effectively setup the required environment?

My attempt project:


Cargo.toml:

[package]
name = "testing-lib"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
userfaultfd-sys = "0.4.1"

lib.rs:

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}

build.rs

fn main() {
    std::process::Command::new("sudo").args(["apt","install","clang"]).output().unwrap();
}

cargo build leads to:

error: failed to run custom build command for `userfaultfd-sys v0.4.1`

Caused by:
  process didn't exit successfully: `/home/admin/testing-lib/target/debug/build/userfaultfd-sys-dd02a368283723d2/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at 'Unable to find libclang: "couldn't find any valid shared libraries matching: ['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"', /home/admin/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.59.2/src/lib.rs:2144:31
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed

Is this something that would need to be addressed within the dependency crate? (requiring a change to userfaultfd-sys where if libclang is not found on a system it builds via another approach)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source