From b2d3956657038731309cacb09d9a34b0066f4ec2 Mon Sep 17 00:00:00 2001 From: Ciaran Ryan-Anderson Date: Sat, 28 Feb 2026 11:50:22 -0700 Subject: [PATCH] Minimal changes to make compiler available to Rust users --- qis-compiler/Cargo.toml | 4 ++-- qis-compiler/rust/lib.rs | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/qis-compiler/Cargo.toml b/qis-compiler/Cargo.toml index 4113ef34a..484d2e97e 100644 --- a/qis-compiler/Cargo.toml +++ b/qis-compiler/Cargo.toml @@ -11,11 +11,11 @@ edition = "2024" [lib] name = "selene_hugr_qis_compiler" path = "rust/lib.rs" -crate-type = ["cdylib"] +crate-type = ["cdylib", "rlib"] [dependencies] anyhow.workspace = true -pyo3 = { workspace = true, features = ["abi3-py310", "anyhow"] } +pyo3 = { workspace = true, features = ["abi3-py310", "anyhow"], optional = true } serde_json.workspace = true tracing.workspace = true itertools.workspace = true diff --git a/qis-compiler/rust/lib.rs b/qis-compiler/rust/lib.rs index bf6742f5e..366085c58 100644 --- a/qis-compiler/rust/lib.rs +++ b/qis-compiler/rust/lib.rs @@ -17,6 +17,7 @@ use inkwell::targets::{ CodeModel, InitializationConfig, RelocMode, Target, TargetMachine, TargetTriple, }; use itertools::Itertools; +#[cfg(feature = "pyo3")] use pyo3::prelude::*; use tket::hugr::ops::DataflowParent; @@ -45,11 +46,12 @@ use tket_qsystem::llvm::{ random::RandomCodegenExtension, result::ResultsCodegenExtension, utils::UtilsCodegenExtension, }; use tracing::{Level, event, instrument}; +#[cfg(feature = "pyo3")] use utils::read_hugr_envelope; mod gpu; mod selene_specific; -mod utils; +pub mod utils; const LLVM_MAIN: &str = "qmain"; const METADATA: &[(&str, &[&str])] = &[("name", &["mainlib"])]; @@ -278,7 +280,7 @@ fn wrap_main<'c>( } #[derive(Debug)] -struct CompileArgs<'a> { +pub struct CompileArgs<'a> { /// Entry point symbol entry: Option, /// LLVM module name @@ -292,7 +294,7 @@ struct CompileArgs<'a> { } impl<'a> CompileArgs<'a> { - fn new( + pub fn new( name: &impl ToString, target_machine: &'a TargetMachine, opt_level: OptimizationLevel, @@ -310,7 +312,7 @@ impl<'a> CompileArgs<'a> { /// Compile the given HUGR to an LLVM module. /// This function is the primary entry point for the compiler. #[instrument(skip(ctx, hugr),parent = None)] -fn compile<'c, 'hugr: 'c>( +pub fn compile<'c, 'hugr: 'c>( args: &CompileArgs, ctx: &'c Context, hugr: &'hugr mut Hugr, @@ -408,11 +410,13 @@ pub fn get_opt_level(opt_level: u32) -> Result { } // -------------------- Python bindings ----------------------- +#[cfg(feature = "pyo3")] mod exceptions { use pyo3::exceptions::PyException; pyo3::create_exception!(selene_hugr_qis_compiler, HugrReadError, PyException); } +#[cfg(feature = "pyo3")] #[pymodule] mod selene_hugr_qis_compiler { use super::{