Skip to content

Commit 033455f

Browse files
LegNeatoclaude
andcommitted
Fix assemble_text_with_context link errors in CMake builds
Split assemble_text_with_context implementation between two files: - source/text.cpp: Implementation for CMake builds (SPIRV_RUST_TARGET_ENV) using internal AssembleTextWithDiagnostics API - context_bridge.cc: Implementation for standalone Rust builds (Bazel) using public spvTextToBinaryWithOptions C API This avoids circular link dependencies in CMake builds where the Rust FFI library is built before the C++ SPIRV-Tools library. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7d04bd4 commit 033455f

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

rust/spirv-tools-ffi/src/context_bridge.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ void dispatch_context_message(std::uintptr_t context_ptr, std::uint32_t level,
8383
#endif
8484
}
8585

86+
// In CMake builds with SPIRV_RUST_TARGET_ENV, this function is provided by
87+
// source/text.cpp using internal APIs. In standalone Rust builds (Bazel),
88+
// we provide it here using the public C API.
89+
#ifndef SPIRV_RUST_TARGET_ENV
8690
AssembleResult assemble_text_with_context(std::size_t context_ptr,
8791
rust::Slice<const std::uint8_t> text,
8892
std::uint32_t options) {
@@ -113,6 +117,7 @@ AssembleResult assemble_text_with_context(std::size_t context_ptr,
113117

114118
return result;
115119
}
120+
#endif
116121

117122
ValidateResult validate_binary_with_options(
118123
std::uint32_t env, rust::Slice<const std::uint32_t> words,

source/text.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,43 @@ spv_result_t spvTextToBinaryWithOptions(const spv_const_context context,
10711071
sanitized_options, pBinary, pDiagnostic);
10721072
}
10731073

1074+
// FFI bridge for Rust assembler integration. In CMake builds with SPIRV_RUST_TARGET_ENV,
1075+
// we provide this function here using internal C++ APIs. In standalone Rust builds (Bazel),
1076+
// context_bridge.cc provides this using the public C API.
1077+
#if defined(SPIRV_RUST_TARGET_ENV)
1078+
namespace spvtools::ffi {
1079+
AssembleResult assemble_text_with_context(std::size_t context_ptr,
1080+
rust::Slice<const std::uint8_t> text,
1081+
std::uint32_t options) {
1082+
AssembleResult result{false, ::rust::Vec<std::uint32_t>()};
1083+
auto* context = reinterpret_cast<spv_context>(context_ptr);
1084+
if (context == nullptr) {
1085+
return result;
1086+
}
1087+
1088+
spv_binary binary = nullptr;
1089+
spv_diagnostic diagnostic = nullptr;
1090+
const char* text_ptr = reinterpret_cast<const char*>(text.data());
1091+
const spv_result_t status = AssembleTextWithDiagnostics(
1092+
context, text_ptr, text.size(), options, &binary, &diagnostic);
1093+
if (diagnostic) {
1094+
spvDiagnosticDestroy(diagnostic);
1095+
}
1096+
1097+
if (status == SPV_SUCCESS && binary != nullptr) {
1098+
result.success = true;
1099+
result.binary.reserve(binary->wordCount);
1100+
for (size_t i = 0; i < binary->wordCount; ++i) {
1101+
result.binary.push_back(binary->code[i]);
1102+
}
1103+
spvBinaryDestroy(binary);
1104+
}
1105+
1106+
return result;
1107+
}
1108+
} // namespace spvtools::ffi
1109+
#endif
1110+
10741111
void spvTextDestroy(spv_text text) {
10751112
if (text) {
10761113
if (text->str) delete[] text->str;

0 commit comments

Comments
 (0)