mod build;
mod command;
mod utils;
use std::env;
use std::path::{Path, PathBuf};
use build::build_program_internal;
pub use build::{execute_build_program, generate_elf_paths};
pub use command::TOOLCHAIN_NAME;
pub use sp1_primitives::types::Elf;
use clap::{Parser, ValueEnum};
const DEFAULT_DOCKER_TAG: &str = concat!("v", env!("CARGO_PKG_VERSION"));
pub const DEFAULT_TARGET: &str = "riscv64im-succinct-zkvm-elf";
const HELPER_TARGET_SUBDIR: &str = "elf-compilation";
pub const CLANG_FLAGS: &[&str] = &[
"--target=riscv64-unknown-none-elf",
"-march=rv64im",
"-mabi=lp64",
"-ffreestanding",
"-fno-builtin",
"-fno-stack-protector",
"-nostdlibinc",
];
pub fn find_lld() -> Option<PathBuf> {
use std::process::Command;
if Command::new("ld.lld").arg("--version").output().is_ok_and(|o| o.status.success()) {
return Some(PathBuf::from("ld.lld"));
}
let home = std::env::var_os("HOME")?;
let toolchains = Path::new(&home).join(".sp1/toolchains");
for entry in std::fs::read_dir(&toolchains).ok()?.flatten() {
let candidate = entry.path().join("lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld/ld.lld");
if candidate.exists() {
return Some(candidate);
}
}
None
}
pub fn build_program_staticlib(path: &str) -> PathBuf {
let manifest = Path::new(path).join("Cargo.toml");
let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
let metadata = metadata_cmd.manifest_path(&manifest).exec().unwrap_or_else(|e| {
panic!("failed to read cargo metadata from {}: {e}", manifest.display())
});
let root_package = metadata
.root_package()
.unwrap_or_else(|| panic!("no root package at {}", manifest.display()));
let lib_target = root_package
.targets
.iter()
.find(|t| t.kind.iter().any(|k| k == "staticlib"))
.unwrap_or_else(|| panic!("crate {} has no `staticlib` target", root_package.name));
build_program(path);
let staticlib = metadata
.target_directory
.join(HELPER_TARGET_SUBDIR)
.join(DEFAULT_TARGET)
.join("release")
.join(format!("lib{}.a", lib_target.name));
if !staticlib.as_std_path().exists() {
panic!(
"expected staticlib at {} after `build_program` — did the build fail silently?",
staticlib
);
}
staticlib.into_std_path_buf()
}
#[derive(Clone, Copy, ValueEnum, Debug, Default)]
pub enum WarningLevel {
#[default]
All,
Minimal,
}
#[derive(Clone, Parser, Debug)]
pub struct BuildArgs {
#[arg(
long,
action,
help = "Run compilation using a Docker container for reproducible builds."
)]
pub docker: bool,
#[arg(
long,
help = "The ghcr.io/succinctlabs/sp1 image tag to use when building with Docker.",
default_value = DEFAULT_DOCKER_TAG
)]
pub tag: String,
#[arg(
long,
action,
value_delimiter = ',',
help = "Space or comma separated list of features to activate"
)]
pub features: Vec<String>,
#[arg(
long,
action,
value_delimiter = ',',
help = "Space or comma separated list of extra flags to invokes `rustc` with"
)]
pub rustflags: Vec<String>,
#[arg(long, action, help = "Do not activate the `default` feature")]
pub no_default_features: bool,
#[arg(long, action, help = "Ignore `rust-version` specification in packages")]
pub ignore_rust_version: bool,
#[arg(long, action, help = "Assert that `Cargo.lock` will remain unchanged")]
pub locked: bool,
#[arg(
short,
long,
action,
help = "Build only the specified packages",
num_args = 1..
)]
pub packages: Vec<String>,
#[arg(
alias = "bin",
long,
action,
help = "Build only the specified binaries",
num_args = 1..
)]
pub binaries: Vec<String>,
#[arg(long, action, requires = "output_directory", help = "ELF binary name")]
pub elf_name: Option<String>,
#[arg(alias = "out-dir", long, action, help = "Copy the compiled ELF to this directory")]
pub output_directory: Option<String>,
#[arg(
alias = "workspace-dir",
long,
action,
help = "The top level directory to be used in the docker invocation."
)]
pub workspace_directory: Option<String>,
#[arg(long, value_enum, default_value = "all", help = "Control warning message verbosity")]
pub warning_level: WarningLevel,
#[arg(
long,
action,
help = "Disable Docker volume caching for cargo registry and git dependencies."
)]
pub no_docker_cache: bool,
}
impl Default for BuildArgs {
#[allow(clippy::uninlined_format_args)]
fn default() -> Self {
Self {
docker: false,
tag: DEFAULT_DOCKER_TAG.to_string(),
features: vec![],
rustflags: vec![],
ignore_rust_version: false,
packages: vec![],
binaries: vec![],
elf_name: None,
output_directory: None,
locked: false,
no_default_features: false,
workspace_directory: None,
warning_level: WarningLevel::All,
no_docker_cache: false,
}
}
}
pub fn build_program(path: &str) {
build_program_internal(path, None)
}
pub fn build_program_with_args(path: &str, args: BuildArgs) {
build_program_internal(path, Some(args))
}
#[macro_export]
macro_rules! include_elf {
($arg:tt) => {{
$crate::Elf::Static(include_bytes!(env!(concat!("SP1_ELF_", $arg))))
}};
}