diff options
author | Andrew Clayton <a.clayton@nginx.com> | 2023-08-02 17:03:48 +0100 |
---|---|---|
committer | Andrew Clayton <a.clayton@nginx.com> | 2023-08-21 23:24:12 +0100 |
commit | d6ed6a219b31a58526721f96195c80061d41ce54 (patch) | |
tree | 17a1fd6ecf72a327916ff0f8bc7aaf85b981ceff /src/rust/unit-wasm-sys/build.rs | |
download | unit-wasm-0.1.0.tar.gz unit-wasm-0.1.0.tar.bz2 |
Initial commitv0.1.0
libunit-wasm and example C and Rust WebAssembly modules for NGINX Unit.
Co-developed-by: Timo Stark <t.stark@nginx.com>
Co-developed-by: Liam Crilly <liam@nginx.com>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'src/rust/unit-wasm-sys/build.rs')
-rw-r--r-- | src/rust/unit-wasm-sys/build.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/rust/unit-wasm-sys/build.rs b/src/rust/unit-wasm-sys/build.rs new file mode 100644 index 0000000..bd2ebd5 --- /dev/null +++ b/src/rust/unit-wasm-sys/build.rs @@ -0,0 +1,52 @@ +// buildscript for the unit-wasm-sys crate. + +use std::env; +use std::path::{Path, PathBuf}; + +fn main() { + // Tell rustc where to find the libunit-wasm library. + let libunit_wasm_dir = "libunit-wasm"; + + let dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + + // Some generics + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=wrapper.h"); + + // The rustc-link-search tells Cargo to pass the `-L` flag to the + // compiler to add a directory to the library search plugin. The + // `native` keyword means "only looking for `native libraries` in + // this directory". + println!( + "cargo:rustc-link-search=native={}", + Path::new(&dir).join(libunit_wasm_dir).display() + ); + + // The rustc-link-lib tells Cargo to link the given library using + // the compiler's `-l` flag. This is needed to start building our + // FFIs. + println!("cargo:rustc-link-lib=static=unit-wasm"); + + generate_bindings(); +} + +fn generate_bindings() { + let wasi_sysroot = "--sysroot=".to_owned() + &env::var("WASI_SYSROOT").unwrap(); + let bindings = bindgen::Builder::default() + // The input header file. + .header("libunit-wasm/include/unit/unit-wasm.h") + .allowlist_function("^luw_.*") + .allowlist_var("^luw_.*") + .allowlist_type("^luw_.*") + .clang_args(vec![wasi_sysroot]) // Needed for strings.h + .generate() + .expect("Unable to generate bindings"); + + let out_dir_env = + env::var("OUT_DIR").expect("The required environment variable OUT_DIR was not set"); + let out_path = PathBuf::from(out_dir_env); + + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} |