sp1_build/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
mod build;
mod command;
mod utils;
use build::build_program_internal;
pub use build::execute_build_program;
use clap::Parser;
const BUILD_TARGET: &str = "riscv32im-succinct-zkvm-elf";
const DEFAULT_TAG: &str = "latest";
const DEFAULT_OUTPUT_DIR: &str = "elf";
const HELPER_TARGET_SUBDIR: &str = "elf-compilation";
/// Compile an SP1 program.
///
/// Additional arguments are useful for configuring the build process, including options for using
/// Docker, specifying binary and ELF names, ignoring Rust version checks, and enabling specific
/// features.
#[derive(Clone, Parser, Debug)]
pub struct BuildArgs {
#[clap(
long,
action,
help = "Run compilation using a Docker container for reproducible builds."
)]
pub docker: bool,
#[clap(
long,
help = "The ghcr.io/succinctlabs/sp1 image tag to use when building with Docker.",
default_value = DEFAULT_TAG
)]
pub tag: String,
#[clap(
long,
action,
value_delimiter = ',',
help = "Space or comma separated list of features to activate"
)]
pub features: Vec<String>,
#[clap(
long,
action,
value_delimiter = ',',
help = "Space or comma separated list of extra flags to invokes `rustc` with"
)]
pub rustflags: Vec<String>,
#[clap(long, action, help = "Do not activate the `default` feature")]
pub no_default_features: bool,
#[clap(long, action, help = "Ignore `rust-version` specification in packages")]
pub ignore_rust_version: bool,
#[clap(long, action, help = "Assert that `Cargo.lock` will remain unchanged")]
pub locked: bool,
#[clap(
alias = "bin",
long,
action,
help = "Build only the specified binary",
default_value = ""
)]
pub binary: String,
#[clap(long, action, help = "ELF binary name", default_value = "")]
pub elf_name: String,
#[clap(
alias = "out-dir",
long,
action,
help = "Copy the compiled ELF to this directory",
default_value = DEFAULT_OUTPUT_DIR
)]
pub output_directory: String,
}
// Implement default args to match clap defaults.
impl Default for BuildArgs {
fn default() -> Self {
Self {
docker: false,
tag: DEFAULT_TAG.to_string(),
features: vec![],
rustflags: vec![],
ignore_rust_version: false,
binary: "".to_string(),
elf_name: "".to_string(),
output_directory: DEFAULT_OUTPUT_DIR.to_string(),
locked: false,
no_default_features: false,
}
}
}
/// Builds the program if the program at the specified path, or one of its dependencies, changes.
///
/// This function monitors the program and its dependencies for changes. If any changes are
/// detected, it triggers a rebuild of the program.
///
/// # Arguments
///
/// * `path` - A string slice that holds the path to the program directory.
///
/// This function is useful for automatically rebuilding the program during development
/// when changes are made to the source code or its dependencies.
///
/// Set the `SP1_SKIP_PROGRAM_BUILD` environment variable to `true` to skip building the program.
pub fn build_program(path: &str) {
build_program_internal(path, None)
}
/// Builds the program with the given arguments if the program at path, or one of its dependencies,
/// changes.
///
/// # Arguments
///
/// * `path` - A string slice that holds the path to the program directory.
/// * `args` - A [`BuildArgs`] struct that contains various build configuration options.
///
/// Set the `SP1_SKIP_PROGRAM_BUILD` environment variable to `true` to skip building the program.
pub fn build_program_with_args(path: &str, args: BuildArgs) {
build_program_internal(path, Some(args))
}