nexus_sdk/compile/
mod.rs

1#![allow(deprecated)]
2
3use crypto_common::generic_array::ArrayLength;
4use std::fs;
5use std::io::Write;
6use std::marker::PhantomData;
7use std::path::PathBuf;
8use std::str::FromStr;
9
10use crate::error::BuildError;
11
12/// Compilation and packaging for Rust guests via Cargo.
13pub mod cargo;
14
15/// A guest program packager.
16pub trait Packager {
17    type DigestSize: ArrayLength<u8>;
18
19    /// Return the digest length the packager uses.
20    fn digest_len() -> usize;
21}
22
23/// Dynamic compilation of guest programs.
24///
25/// By default, compilation occurs within `/tmp`. However, the implementation does respect the [`OUT_DIR`](https://doc.rust-lang.org/cargo/reference/environment-variables.html) environment variable.
26#[derive(Clone)]
27pub struct Compiler<P: Packager> {
28    /// The (in-workspace) package to build.
29    pub package: String,
30    /// The binary produced by the build that should be loaded into the zkVM after successful compilation.
31    pub binary: String,
32    debug: bool,
33    native: bool,
34    unique: bool,
35    _packager: PhantomData<P>,
36}
37
38/// An interface for dynamic compilation of guest programs.
39pub trait Compile {
40    /// Setup dynamic compilation.
41    fn new(package: &str) -> Self;
42
43    /// Setup dynamic compilation, using non-default binary name.
44    fn new_with_custom_binary(package: &str, binary: &str) -> Self;
45
46    /// Set dynamic compilation to build the guest program in a debug profile.
47    fn set_debug_build(&mut self, debug: bool);
48
49    /// Set dynamic compilation to build for the native (host machine) target, rather than for the zkVM.
50    fn set_native_build(&mut self, native: bool);
51
52    /// Set dynamic compilation to run a unique build that neither overwrites prior builds nor will be overwritten by future builds. May be used to concurrently build different versions of the same binary.
53    ///
54    /// Note: the SDK does not automatically clean or otherwise manage the resultant builds in the output directory.
55    fn set_unique_build(&mut self, unique: bool);
56
57    /// Set the linker script to use when building the guest binary.
58    fn set_linker() -> Result<PathBuf, BuildError> {
59        let linker_script = include_str!("./linker-scripts/default.x");
60
61        let linker_path = PathBuf::from_str("/tmp/nexus-guest-linkers/default.ld").unwrap();
62
63        if let Some(parent) = linker_path.parent() {
64            fs::create_dir_all(parent)?;
65        }
66
67        let mut file = fs::File::create(linker_path.clone())?;
68        file.write_all(linker_script.as_bytes())?;
69
70        Ok(linker_path)
71    }
72
73    /// Compile and build the guest binary.
74    fn build(&mut self) -> Result<PathBuf, BuildError>;
75}