This is an automated email from the ASF dual-hosted git repository.
Seven-Streams pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/main by this push:
new 5e6b6a03 [FIX][Rust] Let docs.rs build tvm-ffi and correct the Rust
setup docs (#839)
5e6b6a03 is described below
commit 5e6b6a037eff1ce8245391257ed540f2587eb262
Author: Linzhang Li <[email protected]>
AuthorDate: Mon Oct 5 21:46:44 2026 -0400
[FIX][Rust] Let docs.rs build tvm-ffi and correct the Rust setup docs (#839)
The docs.rs build of `tvm-ffi` 0.1.14 failed
(https://docs.rs/crate/tvm-ffi/0.1.14/builds/4724950):
`rust/tvm-ffi/build.rs`
runs `tvm-ffi-config` unconditionally and panics when it is missing,
which is
the case on docs.rs and in `docs/conf.py`, which sets `DOCS_RS` the same
way.
Bring the script in line with `rust/tvm-ffi-sys/build.rs` (#825):
- Under `DOCS_RS`, warn and skip the loader-path setup instead of
panicking.
- Treat a non-zero exit status or empty output of `tvm-ffi-config
--libdir` as
an error, and report the exit status and stderr.
- Fail with the same install guidance as `tvm-ffi-sys` when `DOCS_RS` is
not
set.
- Rerun the script when `DOCS_RS`, `PATH`, the loader-path variable, or
`build.rs` changes, so switching the Python environment updates the
`LD_LIBRARY_PATH` baked into `cargo run` and `cargo test` instead of
keeping
the previous library directory.
Also fix two Rust documentation issues:
- `rust/README.md` claimed the build sets the dynamic loader path for
`cargo run` and `cargo test`. That only holds inside this workspace:
Cargo
applies `rustc-env` from the running package's own build script and
drops
`rustc-link-search` paths outside the target directory, so downstream
crates
must set `LD_LIBRARY_PATH` themselves, including for `cargo run` and
`cargo test`.
- `docs/guides/rust_lang_guide.md` still pointed published users at
`tvm-ffi = "0.1.0-alpha.0"`; use `0.1.14`.
Validation: with no `tvm-ffi-config` on PATH, `DOCS_RS=1 cargo doc
--no-deps
-p tvm-ffi` now succeeds with a warning (it previously panicked at
build.rs:69)
and a plain `cargo build -p tvm-ffi` still fails, now with the install
guidance. With the Python package installed, `cargo test -p tvm-ffi
--test
test_function` and `cargo run --example load_library --features example`
pass;
switching PATH between two environments reruns the build script and
bakes the
new library directory, and an unchanged PATH does not rerun it.
pre-commit
passed on all files.
---------
Signed-off-by: yuchuan <[email protected]>
---
docs/guides/rust_lang_guide.md | 4 +--
rust/README.md | 4 +--
rust/tvm-ffi/build.rs | 62 ++++++++++++++++++++++++++++++++++--------
3 files changed, 54 insertions(+), 16 deletions(-)
diff --git a/docs/guides/rust_lang_guide.md b/docs/guides/rust_lang_guide.md
index 015dea10..9c869121 100644
--- a/docs/guides/rust_lang_guide.md
+++ b/docs/guides/rust_lang_guide.md
@@ -48,11 +48,11 @@ Add to your `Cargo.toml`:
tvm-ffi = { path = "path/to/tvm-ffi/rust/tvm-ffi" }
```
-For published versions (when available):
+For published versions:
```toml
[dependencies]
-tvm-ffi = "0.1.0-alpha.0"
+tvm-ffi = "0.1.14"
```
### Environment Setup
diff --git a/rust/README.md b/rust/README.md
index 31f3fb01..84a584c7 100644
--- a/rust/README.md
+++ b/rust/README.md
@@ -53,9 +53,9 @@ The build will:
- Query `tvm-ffi-config --libdir` to add the appropriate link search path.
- Link against `tvm_ffi`.
-- Update the appropriate dynamic loader path environment variable for `cargo
run` and `cargo test`.
+- Update the appropriate dynamic loader path environment variable for `cargo
run` and `cargo test` within this workspace.
-For running downstream applications, you need to set the `LD_LIBRARY_PATH` so
`libtvm_ffi` is available in the path.
+Downstream crates do not inherit this: set the `LD_LIBRARY_PATH` so
`libtvm_ffi` is available in the path, also for `cargo run` and `cargo test`.
```bash
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:`tvm-ffi-config --libdir`
diff --git a/rust/tvm-ffi/build.rs b/rust/tvm-ffi/build.rs
index 7209745b..4c9e8b48 100644
--- a/rust/tvm-ffi/build.rs
+++ b/rust/tvm-ffi/build.rs
@@ -49,6 +49,7 @@ fn update_ld_library_path(lib_dir: &str) {
return;
}
// Get the current value of the environment variable at build time (if any)
+ println!("cargo:rerun-if-env-changed={}", os_env_var);
let current_val = env::var(os_env_var).unwrap_or_else(|_| String::new());
// Use platform-specific separator
let separator = if os_env_var == "PATH" { ";" } else { ":" };
@@ -62,18 +63,55 @@ fn update_ld_library_path(lib_dir: &str) {
}
fn main() {
- // Run `mylib-config --libdir` to get the library path
- let config_output = Command::new("tvm-ffi-config")
- .arg("--libdir")
- .output()
- .expect("Failed to run tvm-ffi-config");
- let lib_dir = String::from_utf8(config_output.stdout)
- .expect("Invalid UTF-8 output from tvm-ffi-config")
- .trim()
- .to_string();
- // update the LD_LIBRARY_PATH environment variable
- // note that we will also need to update ld_library_path for
- // the cases here besides the tvm-ffi-sys crate so cargo test works out of
the box
+ // docs.rs builds the documentation without tvm-ffi installed, and so does
+ // docs/conf.py, which sets DOCS_RS as docs.rs does. Cargo sets RUSTDOC for
+ // every build script, so it cannot tell a documentation build apart.
+ let docs_only = env::var_os("DOCS_RS").is_some();
+ println!("cargo:rerun-if-env-changed=DOCS_RS");
+ // The library directory comes from the tvm-ffi-config found on PATH.
+ println!("cargo:rerun-if-env-changed=PATH");
+ println!("cargo:rerun-if-changed=build.rs");
+
+ // Run `tvm-ffi-config --libdir` to get the library path
+ let found = match Command::new("tvm-ffi-config").arg("--libdir").output() {
+ Ok(output) if output.status.success() => {
+ let lib_dir = String::from_utf8(output.stdout)
+ .unwrap_or_default()
+ .trim()
+ .to_string();
+ if lib_dir.is_empty() {
+ Err("`tvm-ffi-config --libdir` printed no library
directory".to_string())
+ } else {
+ Ok(lib_dir)
+ }
+ }
+ Ok(output) => Err(format!(
+ "`tvm-ffi-config --libdir` failed ({}): {}",
+ output.status,
+ String::from_utf8_lossy(&output.stderr).trim()
+ )),
+ Err(err) => Err(format!("could not run `tvm-ffi-config`: {err}")),
+ };
+ let lib_dir = match found {
+ Ok(lib_dir) => lib_dir,
+ Err(reason) if docs_only => {
+ println!(
+ "cargo:warning={reason}; not setting the loader path for a
documentation build"
+ );
+ return;
+ }
+ Err(reason) => panic!(
+ "{reason}. tvm-ffi sets the dynamic loader path for `cargo run`
and `cargo test` \
+ from the directory that `tvm-ffi-config --libdir` prints: install
tvm-ffi (e.g. \
+ `pip install apache-tvm-ffi`) and put tvm-ffi-config on PATH. To
build \
+ documentation only, set DOCS_RS=1."
+ ),
+ };
+
+ // Let `cargo run` and `cargo test` in this workspace find libtvm_ffi.
Cargo only
+ // forwards a package's own build-script environment to the binaries it
runs, so
+ // the tvm-ffi-sys build script cannot do this for us, and downstream
crates set
+ // the loader path themselves (see ../README.md).
update_ld_library_path(&lib_dir);
// generate the example library
generate_example_lib();