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-tirx.git


The following commit(s) were added to refs/heads/main by this push:
     new 960a2b2ac [FEAT][RUST] Prepare the tvm-tirx crate and add the ci (#128)
960a2b2ac is described below

commit 960a2b2ac1b0e018b8b11767f67e1556edd6992d
Author: Linzhang Li <[email protected]>
AuthorDate: Thu Oct 29 19:24:14 2026 -0400

    [FEAT][RUST] Prepare the tvm-tirx crate and add the ci (#128)
    
    ## Summary
    
    This PR prepares the `rust/` crate for its first crates.io release as
    `tvm-tirx` 0.1.0-alpha.5,
    the counterpart of `apache-tvm-tirx` 0.1.0a5. The crate now depends on
    the crates.io release of
    tvm-ffi, its build script supports documentation builds on docs.rs, and
    a manual workflow,
    `publish_crate.yml`, verifies the crate on every wheel platform and
    publishes it. The workflow
    follows `publish_wheel.yml` for its inputs and platforms, and tvm-ffi's
    `publish_crates.yml` for
    its dry run and upload.
---
 .github/workflows/publish_crate.yml | 269 ++++++++++++++++++++++++++++++++++++
 AGENTS.md                           |   7 +-
 rust/Cargo.toml                     |  15 +-
 rust/build.rs                       |  61 +++++---
 4 files changed, 327 insertions(+), 25 deletions(-)

diff --git a/.github/workflows/publish_crate.yml 
b/.github/workflows/publish_crate.yml
new file mode 100644
index 000000000..f30368049
--- /dev/null
+++ b/.github/workflows/publish_crate.yml
@@ -0,0 +1,269 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+name: Publish TIRx crate
+
+on:
+  workflow_dispatch:
+    inputs:
+      tag:
+        description: "Tag, branch, or SHA to build"
+        required: true
+        type: string
+      wheel_run_id:
+        description: "ID of the 'Publish TIRx wheels' run to test against 
(empty: the wheels on PyPI)"
+        required: false
+        default: ""
+        type: string
+      publish_repository:
+        description: "Where to publish after the crate is verified"
+        required: true
+        default: "none"
+        type: choice
+        options:
+          - none
+          - crates-io
+
+permissions:
+  contents: read
+
+concurrency:
+  group: publish-crate-${{ inputs.tag }}
+  cancel-in-progress: false
+
+env:
+  CARGO_TERM_COLOR: always
+  PIP_NO_CACHE_DIR: "1"
+  WHEEL_RUN_ID: ${{ inputs.wheel_run_id }}
+
+jobs:
+  # Resolve the source once, so that the commit the build jobs verify is the 
one published even
+  # when the input names a branch.
+  check_release:
+    name: Check release metadata
+    runs-on: ubuntu-latest
+    outputs:
+      sha: ${{ steps.source.outputs.sha }}
+    steps:
+      - name: Checkout source
+        uses: actions/[email protected]
+        with:
+          ref: ${{ inputs.tag }}
+          fetch-depth: 0
+          fetch-tags: true
+
+      - name: Set up release metadata Python
+        uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # 
v10.2.0
+        with:
+          python-version: "3.12"
+          activate-environment: true
+
+      - id: source
+        name: Resolve source commit
+        shell: bash
+        run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
+
+      - name: Prepare matching release metadata
+        shell: bash
+        run: &prepare_release_metadata |
+          uv pip install tomlkit setuptools-scm
+          python ci/wheel/prepare_release_metadata.py
+          python - <<'PY'
+          import os
+          import tomllib
+          from pathlib import Path
+
+          main = 
tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8"))
+          crate = 
tomllib.loads(Path("rust/Cargo.toml").read_text(encoding="utf-8"))
+          tvm_ffi = crate["dependencies"]["tvm-ffi"]
+          tvm_ffi = tvm_ffi["version"] if isinstance(tvm_ffi, dict) else 
tvm_ffi
+          # A caret requirement on a tvm-ffi release such as 0.1.15-post2, a 
SemVer pre-release,
+          # would select an older release such as 0.1.15.
+          if not tvm_ffi.startswith("="):
+              raise SystemExit(f"rust/Cargo.toml must pin tvm-ffi exactly, not 
to {tvm_ffi!r}")
+          with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8", 
newline="\n") as env:
+              env.write(f"RELEASE_VERSION={main['project']['version']}\n")
+              env.write(f"CRATE_VERSION={crate['package']['version']}\n")
+              env.write(f"TVM_FFI_VERSION={tvm_ffi[1:]}\n")
+          PY
+
+      # The crate depends on the crates.io release of the tvm-ffi the wheels 
are built with.
+      - name: Check the tvm-ffi release
+        shell: bash
+        run: |
+          tag="v${TVM_FFI_VERSION}"
+          submodule=$(git ls-tree HEAD 3rdparty/tvm-ffi | awk '{print $3}')
+          # An annotated tag lists the commit it points to as "<tag>^{}", 
after the tag itself.
+          release=$(git ls-remote https://github.com/apache/tvm-ffi 
"refs/tags/${tag}" "refs/tags/${tag}^{}" |
+            sort -k2 | tail -n 1 | cut -f1)
+          if [[ "${submodule}" != "${release}" ]]; then
+            echo "::error::3rdparty/tvm-ffi is at ${submodule}, but the crate 
depends on tvm-ffi ${TVM_FFI_VERSION}, whose tag ${tag} is at ${release:-no 
commit}"
+            exit 1
+          fi
+          echo "Crate tvm-tirx ${CRATE_VERSION} for wheel ${RELEASE_VERSION}, 
with tvm-ffi ${TVM_FFI_VERSION} at ${release}"
+
+  build_crate:
+    name: ${{ matrix.name }}
+    needs: [check_release]
+    runs-on: ${{ matrix.os }}
+    permissions:
+      contents: read
+      # Download the wheels of another workflow run.
+      actions: read
+    strategy:
+      fail-fast: false
+      matrix:
+        # Keep the platforms in sync with the wheels of publish_wheel.yml.
+        include:
+          - name: "Linux x86_64 crate test"
+            os: ubuntu-latest
+            artifact_suffix: linux-x86_64-manylinux_2_28
+          - name: "Linux aarch64 crate test"
+            os: ubuntu-24.04-arm
+            artifact_suffix: linux-aarch64-manylinux_2_28
+          - name: "macOS arm64 crate test"
+            os: macos-14
+            artifact_suffix: macos-arm64
+          - name: "Windows AMD64 crate test"
+            os: windows-2022
+            artifact_suffix: windows-amd64
+          - name: "Windows ARM64 crate test"
+            os: windows-11-arm
+            artifact_suffix: windows-arm64
+    steps:
+      - name: Checkout source
+        uses: actions/[email protected]
+        with:
+          ref: ${{ needs.check_release.outputs.sha }}
+          fetch-depth: 0
+          fetch-tags: true
+
+      - name: Set up release metadata Python
+        uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # 
v10.2.0
+        with:
+          python-version: "3.12"
+          activate-environment: true
+
+      - name: Prepare matching release metadata
+        shell: bash
+        run: *prepare_release_metadata
+
+      - name: Download TIRx wheel
+        if: ${{ inputs.wheel_run_id != '' }}
+        uses: actions/[email protected]
+        with:
+          name: tirx-wheel-${{ matrix.artifact_suffix }}
+          path: wheelhouse
+          run-id: ${{ inputs.wheel_run_id }}
+          github-token: ${{ github.token }}
+
+      # The build scripts of the crate and tvm-ffi-sys find the libraries 
through the
+      # tvm-tirx-config and tvm-ffi-config of these packages.
+      - name: Install TIRx and TVM FFI
+        shell: bash
+        run: |
+          if [[ -n "${WHEEL_RUN_ID}" ]]; then
+            tirx=(wheelhouse/*.whl)
+          else
+            tirx=("apache-tvm-tirx==${RELEASE_VERSION}")
+          fi
+          uv pip install "${tirx[@]}" "apache-tvm-ffi==${TVM_FFI_VERSION}"
+          python - <<'PY'
+          import os
+          from importlib.metadata import version
+
+          from packaging.version import Version
+
+          installed = version("apache-tvm-tirx")
+          if Version(installed) != Version(os.environ["RELEASE_VERSION"]):
+              raise SystemExit(f"installed apache-tvm-tirx {installed}, not 
{os.environ['RELEASE_VERSION']}")
+          PY
+
+      - name: Set up Rust
+        shell: bash
+        run: rustup toolchain install stable --profile minimal
+
+      - name: Test crate
+        working-directory: rust
+        run: cargo +stable test --release
+
+      # Packages and verifies the crate; only the release metadata edits above 
are uncommitted.
+      - name: Dry-run crate publication
+        working-directory: rust
+        run: cargo +stable publish --dry-run --registry crates-io --allow-dirty
+
+      # The dry run keeps its .crate file in a temporary directory; write it 
out again, without
+      # re-verifying, for the artifact upload.
+      - name: Package crate
+        working-directory: rust
+        run: cargo +stable package --registry crates-io --allow-dirty 
--no-verify
+
+      - name: Upload crate artifact
+        uses: actions/[email protected]
+        with:
+          name: tirx-crate-${{ matrix.artifact_suffix }}
+          path: rust/target/package/*.crate
+          if-no-files-found: error
+
+  upload_crate:
+    name: Upload crate to crates.io
+    needs: [check_release, build_crate]
+    if: ${{ inputs.publish_repository == 'crates-io' }}
+    runs-on: ubuntu-latest
+    environment: crates-io
+    steps:
+      - name: Checkout source
+        uses: actions/[email protected]
+        with:
+          ref: ${{ needs.check_release.outputs.sha }}
+          fetch-depth: 0
+          fetch-tags: true
+
+      - name: Set up release metadata Python
+        uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # 
v10.2.0
+        with:
+          python-version: "3.12"
+          activate-environment: true
+
+      - name: Prepare matching release metadata
+        shell: bash
+        run: *prepare_release_metadata
+
+      - name: Set up Rust
+        shell: bash
+        run: rustup toolchain install stable --profile minimal
+
+      # The build jobs verified this commit on every platform; packaging it 
again without
+      # verification needs no TIRx installation. Skipping a version already on 
crates.io lets a
+      # failed run be repeated. Needs a crates.io token with publish access to 
tvm-tirx.
+      - name: Publish crate to crates.io
+        shell: bash
+        env:
+          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
+        run: |
+          # Looked up outside rust/, so that cargo asks the registry rather 
than the local package.
+          if lookup=$(cargo +stable info --registry crates-io 
"tvm-tirx@${CRATE_VERSION}" 2>&1); then
+            echo "tvm-tirx@${CRATE_VERSION} is already on crates.io; skipping."
+            exit 0
+          elif ! grep -q "could not find" <<< "${lookup}"; then
+            # Only a definite "not found" may fall through to publishing.
+            echo "${lookup}"
+            echo "::error::Could not determine whether 
tvm-tirx@${CRATE_VERSION} is on crates.io"
+            exit 1
+          fi
+          echo "Publishing tvm-tirx@${CRATE_VERSION}"
+          cargo +stable publish --manifest-path rust/Cargo.toml --registry 
crates-io --allow-dirty --no-verify
diff --git a/AGENTS.md b/AGENTS.md
index 4c2f3188a..3c16928b0 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -124,9 +124,10 @@ describe missing coverage; they do not demonstrate that a 
backend works.
 
 ### Rust crate
 
-`rust/` is a standalone crate (`tvm-tirx`) over `3rdparty/tvm-ffi/rust`. Its
-IR bindings are generated by `tvm-ffi-stubgen` between `tvm-ffi-stubgen(begin)`
-and `(end)` markers. The crate links `libtvm_tirx_compiler` from the directory
+`rust/` is a standalone crate (`tvm-tirx`) over the crates.io release of
+`3rdparty/tvm-ffi/rust`, pinned with an exact version requirement. Its IR
+bindings are generated by `tvm-ffi-stubgen` between `tvm-ffi-stubgen(begin)` 
and
+`(end)` markers. The crate links `libtvm_tirx_compiler` from the directory
 `tvm-tirx-config --libdir` prints, so build TIRx first. Its build script needs
 `tvm-ffi-config` and `tvm-tirx-config` on `PATH`, which `uv run` provides after
 the editable install:
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 24cdff321..7bdbc5a02 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -17,12 +17,19 @@
 
 [package]
 name = "tvm-tirx"
-version = "0.1.0-alpha.1"
+version = "0.1.0-alpha.5"
 edition = "2021"
 # The oldest toolchain that builds the crate and tvm-ffi; CMake checks cargo 
against it.
 rust-version = "1.84"
+description = "Rust bindings for the TVM TIRx compiler"
 license = "Apache-2.0"
-publish = false
+homepage = "https://tvm.apache.org/tirx/";
+documentation = "https://tvm.apache.org/tirx/reference/rust/";
+repository = "https://github.com/apache/tvm-tirx";
+keywords = ["tvm", "compiler", "tensor", "ir", "ffi"]
+categories = ["compilers", "api-bindings"]
+# The build directories CMake creates here are not part of the crate.
+include = ["/build.rs", "/src", "/examples", "/tests"]
 
 [lib]
 name = "tvm_tirx"
@@ -30,7 +37,9 @@ name = "tvm_tirx"
 crate-type = ["rlib", "cdylib"]
 
 [dependencies]
-tvm-ffi = { path = "../3rdparty/tvm-ffi/rust/tvm-ffi" }
+# The release of 3rdparty/tvm-ffi. An exact requirement, since 0.1.15-post2 is 
a pre-release of
+# 0.1.15 for semver, and a caret requirement would select the older 0.1.15.
+tvm-ffi = "=0.1.15-post2"
 
 # A library of operators and a type defined in Rust, which 
`tests/python/ir/test_op_rust.py` loads.
 [[example]]
diff --git a/rust/build.rs b/rust/build.rs
index c062967e1..b4e6ecbb1 100644
--- a/rust/build.rs
+++ b/rust/build.rs
@@ -20,25 +20,31 @@
 //! `tvm-ffi-sys` links libtvm_ffi from the one `tvm-ffi-config --libdir` 
prints. The loader-path
 //! environment `tvm-ffi-sys` emits only applies to its own package, so this 
script emits it again
 //! for both directories, so that a plain `cargo test` finds the libraries at 
startup.
+//!
+//! A documentation build links nothing, so with `DOCS_RS` set, as docs.rs 
sets it, the script
+//! only warns when it finds no library, as `tvm-ffi-sys` does.
 
 use std::env;
 use std::path::Path;
 use std::process::Command;
 
 /// The library directory a configuration helper prints for `--libdir`.
-fn lib_dir(config: &str) -> String {
-    let output = 
Command::new(config).arg("--libdir").output().unwrap_or_else(|err| {
-        panic!("could not run `{config}`: {err}; put the directory of 
`{config}` on PATH")
-    });
-    assert!(
-        output.status.success(),
-        "`{config} --libdir` failed ({}): {}",
-        output.status,
-        String::from_utf8_lossy(&output.stderr).trim()
-    );
+fn lib_dir(config: &str) -> Result<String, String> {
+    let output = Command::new(config).arg("--libdir").output().map_err(|err| {
+        format!("could not run `{config}`: {err}; put the directory of 
`{config}` on PATH")
+    })?;
+    if !output.status.success() {
+        return Err(format!(
+            "`{config} --libdir` failed ({}): {}",
+            output.status,
+            String::from_utf8_lossy(&output.stderr).trim()
+        ));
+    }
     let lib_dir = String::from_utf8(output.stdout).unwrap().trim().to_string();
-    assert!(!lib_dir.is_empty(), "`{config} --libdir` printed no library 
directory");
-    lib_dir
+    if lib_dir.is_empty() {
+        return Err(format!("`{config} --libdir` printed no library 
directory"));
+    }
+    Ok(lib_dir)
 }
 
 /// Checks that `lib_dir` holds the file the linker looks for under 
`-ltvm_tirx_compiler`.
@@ -46,30 +52,47 @@ fn lib_dir(config: &str) -> String {
 /// `tvm-tirx-config --libdir` falls back to `<prefix>/lib` when no directory 
it knows holds the
 /// libraries, as before the first build. Failing here rather than in the link 
step makes cargo
 /// run the script again on the next build, instead of keeping a search path 
that has no library.
-fn check_compiler_library(lib_dir: &str, target_os: &str) {
+fn check_compiler_library(lib_dir: &str, target_os: &str) -> Result<(), 
String> {
     let names: &[&str] = match target_os {
         "windows" => &["tvm_tirx_compiler.lib", "libtvm_tirx_compiler.dll.a"],
         "macos" | "ios" => &["libtvm_tirx_compiler.dylib"],
         _ => &["libtvm_tirx_compiler.so"],
     };
     let dir = Path::new(lib_dir);
-    assert!(
-        names.iter().any(|name| dir.join(name).is_file()),
+    if names.iter().any(|name| dir.join(name).is_file()) {
+        return Ok(());
+    }
+    Err(format!(
         "`tvm-tirx-config --libdir` printed {lib_dir}, which holds no {}: 
build TIRx first \
          (`uv pip install -e .`), or set TVM_TIRX_LIBRARY_PATH to the 
directory of the library",
         names.join(" or ")
-    );
+    ))
 }
 
 fn main() {
+    // Cargo sets RUSTDOC for every build script, so only DOCS_RS marks a 
documentation build.
+    let docs_only = env::var_os("DOCS_RS").is_some();
+    println!("cargo:rerun-if-env-changed=DOCS_RS");
     println!("cargo:rerun-if-env-changed=PATH");
     // `tvm-tirx-config` puts this directory first when it is set.
     println!("cargo:rerun-if-env-changed=TVM_TIRX_LIBRARY_PATH");
     println!("cargo:rerun-if-changed=build.rs");
     let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
-    let ffi_lib_dir = lib_dir("tvm-ffi-config");
-    let tirx_lib_dir = lib_dir("tvm-tirx-config");
-    check_compiler_library(&tirx_lib_dir, &target_os);
+    let found = lib_dir("tvm-ffi-config").and_then(|ffi_lib_dir| {
+        let tirx_lib_dir = lib_dir("tvm-tirx-config")?;
+        check_compiler_library(&tirx_lib_dir, &target_os)?;
+        Ok((ffi_lib_dir, tirx_lib_dir))
+    });
+    let (ffi_lib_dir, tirx_lib_dir) = match found {
+        Ok(dirs) => dirs,
+        Err(reason) if docs_only => {
+            println!(
+                "cargo:warning={reason}; not linking libtvm_tirx_compiler for 
a documentation build"
+            );
+            return;
+        }
+        Err(reason) => panic!("{reason}. To build documentation only, set 
DOCS_RS=1."),
+    };
 
     // The crate references the library only through its link anchor (see 
`src/lib.rs`).
     println!("cargo:rustc-link-search=native={tirx_lib_dir}");

Reply via email to