I'm embedding a compiled binary from a child crate into a parent library. I need to make the constant FOO
within the child crate configurable from the parent crate during the build process. How can I achieve this?
Here's my project structure:
- child/
- src/
- main.rs
- Cargo.toml
- src/
- lib.rs
- Cargo.toml
- build.rs
Here's the code for child/src/main.rs
:
const FOO: usize = 42; // How can I configure this value from the parent crate?
fn main() {
println!("{FOO}");
}
My build.rs
script looks like this:
fn main() {
const CHILD_CRATE_NAME: &str = "child";
const CHILD_CRATE_DIRNAME: &str = "child";
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let build = Command::new("cargo")
.args([
"build",
"--package",
CHILD_CRATE_NAME,
"--release",
"--target-dir",
out_dir.join(CHILD_CRATE_DIRNAME).to_str().unwrap(),
])
.spawn()
.unwrap();
build.wait().unwrap();
}
src/lib.rs
:
const CHILD: &[u8] = include_bytes!(concat!(
env!("OUT_DIR"),
"/child/release/child",
));
// do something with `CHILD` ...