Skip to content

Commit efca9f1

Browse files
LegNeatoclaude
andcommitted
Fix Bazel and CMake Rust FFI builds with separate env vars
The previous fix used SPIRV_TOOLS_FFI_SKIP_CPP_LINK to determine whether to define SPIRV_RUST_TARGET_ENV, but both Bazel and CMake set this variable. This caused the Bazel build to fail because it tried to include generated SPIRV-Tools headers that don't exist in the sandbox. This fix adds a new environment variable SPIRV_RUST_TARGET_ENV_DEFINE that Bazel sets to explicitly request Rust-only implementations. The logic is now: - Standalone cargo build: Define SPIRV_RUST_TARGET_ENV, use Rust stubs - Bazel build: Define SPIRV_RUST_TARGET_ENV, use Rust stubs - CMake build: Don't define, use C++ implementations with C++ libraries 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7fe4c98 commit efca9f1

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

rust/scripts/build_rust_ffi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def main() -> int:
5353
env["CARGO_TARGET_DIR"] = str(target_dir)
5454
# Skip linking C++ SPIRV-Tools libraries - Bazel will link them at final link time
5555
env["SPIRV_TOOLS_FFI_SKIP_CPP_LINK"] = "1"
56+
# Use Rust-only implementations in context_bridge.cc (no dependency on generated headers)
57+
env["SPIRV_RUST_TARGET_ENV_DEFINE"] = "1"
5658

5759
package = args.package
5860
command = [

rust/spirv-tools-ffi/build.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ fn main() {
2929
.expect("failed to copy context bridge header");
3030

3131
let mut bridge_builder = cxx_build::bridge("src/lib.rs");
32-
// Only define SPIRV_RUST_TARGET_ENV when NOT building via CMake.
33-
// When building via CMake, the FFI library should include its own implementations
34-
// of dispatch_context_message and assemble_text_with_context (from context_bridge.cc)
35-
// to avoid circular dependency issues at link time.
36-
// CMake sets SPIRV_TOOLS_FFI_SKIP_CPP_LINK=1 when invoking cargo.
37-
if env::var("SPIRV_TOOLS_FFI_SKIP_CPP_LINK").is_err() {
32+
// Define SPIRV_RUST_TARGET_ENV to use Rust-only implementations in context_bridge.cc.
33+
// This avoids dependencies on generated SPIRV-Tools headers (like core_tables_header.inc)
34+
// and C++ libraries (libspirv.hpp, reducer.h) that may not be available.
35+
//
36+
// Build scenarios:
37+
// - Standalone cargo build (no env vars): Define SPIRV_RUST_TARGET_ENV, use Rust stubs
38+
// - Bazel build (SPIRV_RUST_TARGET_ENV_DEFINE=1): Define SPIRV_RUST_TARGET_ENV, use Rust stubs
39+
// - CMake build (SPIRV_TOOLS_FFI_SKIP_CPP_LINK=1 only): Don't define, use C++ implementations
40+
//
41+
// Both Bazel and CMake set SPIRV_TOOLS_FFI_SKIP_CPP_LINK=1 to skip link directives,
42+
// but only Bazel sets SPIRV_RUST_TARGET_ENV_DEFINE=1 to use Rust-only implementations.
43+
let use_rust_implementations = env::var("SPIRV_RUST_TARGET_ENV_DEFINE").is_ok()
44+
|| env::var("SPIRV_TOOLS_FFI_SKIP_CPP_LINK").is_err();
45+
if use_rust_implementations {
3846
bridge_builder.define("SPIRV_RUST_TARGET_ENV", None);
3947
}
4048
bridge_builder

0 commit comments

Comments
 (0)