openvm_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Initial cargo build commands taken from risc0 under Apache 2.0 license

#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use std::{
    env, fs,
    io::{BufRead, BufReader, Write},
    path::{Path, PathBuf},
    process::{Command, Stdio},
};

use cargo_metadata::{MetadataCommand, Package};
use openvm_platform::memory;

pub use self::config::GuestOptions;

mod config;

const RUSTUP_TOOLCHAIN_NAME: &str = "nightly-2024-10-30";
const BUILD_LOCKED_ENV: &str = "OPENVM_BUILD_LOCKED";
const BUILD_DEBUG_ENV: &str = "OPENVM_BUILD_DEBUG";
const SKIP_BUILD_ENV: &str = "OPENVM_SKIP_BUILD";
const GUEST_LOGFILE_ENV: &str = "OPENVM_GUEST_LOGFILE";

/// Returns the given cargo Package from the metadata in the Cargo.toml manifest
/// within the provided `manifest_dir`.
pub fn get_package(manifest_dir: impl AsRef<Path>) -> Package {
    let manifest_path = fs::canonicalize(manifest_dir.as_ref().join("Cargo.toml")).unwrap();
    let manifest_meta = MetadataCommand::new()
        .manifest_path(&manifest_path)
        .no_deps()
        .exec()
        .expect("cargo metadata command failed");
    let mut matching: Vec<Package> = manifest_meta
        .packages
        .into_iter()
        .filter(|pkg| {
            let std_path: &Path = pkg.manifest_path.as_ref();
            std_path == manifest_path
        })
        .collect();
    if matching.is_empty() {
        eprintln!(
            "ERROR: No package found in {}",
            manifest_dir.as_ref().display()
        );
        std::process::exit(-1);
    }
    if matching.len() > 1 {
        eprintln!(
            "ERROR: Multiple packages found in {}",
            manifest_dir.as_ref().display()
        );
        std::process::exit(-1);
    }
    matching.pop().unwrap()
}

/// Determines and returns the build target directory from the Cargo manifest at
/// the given `manifest_path`.
pub fn get_target_dir(manifest_path: impl AsRef<Path>) -> PathBuf {
    MetadataCommand::new()
        .manifest_path(manifest_path.as_ref())
        .no_deps()
        .exec()
        .expect("cargo metadata command failed")
        .target_directory
        .into()
}

/// Returns the target executable directory given `target_dir` and `profile`.
pub fn get_dir_with_profile(
    target_dir: impl AsRef<Path>,
    profile: &str,
    examples: bool,
) -> PathBuf {
    let res = target_dir
        .as_ref()
        .join("riscv32im-risc0-zkvm-elf")
        .join(profile);
    if examples {
        res.join("examples")
    } else {
        res
    }
}

/// When called from a build.rs, returns the current package being built.
pub fn current_package() -> Package {
    get_package(env::var("CARGO_MANIFEST_DIR").unwrap())
}

/// Reads the value of the environment variable `OPENVM_BUILD_DEBUG` and returns true if it is set to 1.
pub fn is_debug() -> bool {
    get_env_var(BUILD_DEBUG_ENV) == "1"
}

/// Reads the value of the environment variable `OPENVM_SKIP_BUILD` and returns true if it is set to 1.
pub fn is_skip_build() -> bool {
    !get_env_var(SKIP_BUILD_ENV).is_empty()
}

fn get_env_var(name: &str) -> String {
    println!("cargo:rerun-if-env-changed={name}");
    env::var(name).unwrap_or_default()
}

/// Returns all target ELF paths associated with the given guest crate.
pub fn guest_methods(
    pkg: &Package,
    target_dir: impl AsRef<Path>,
    guest_features: &[String],
) -> Vec<PathBuf> {
    let profile = if is_debug() { "debug" } else { "release" };
    pkg.targets
        .iter()
        .filter(|target| {
            target
                .kind
                .iter()
                .any(|kind| kind == "bin" || kind == "example")
        })
        .filter(|target| {
            target
                .required_features
                .iter()
                .all(|required_feature| guest_features.contains(required_feature))
        })
        .map(|target| {
            target_dir
                .as_ref()
                .join("riscv32im-risc0-zkvm-elf")
                .join(profile)
                .join(&target.name)
                .to_path_buf()
        })
        .collect()
}

/// Build a [Command] with CARGO and RUSTUP_TOOLCHAIN environment variables
/// removed.
fn sanitized_cmd(tool: &str) -> Command {
    let mut cmd = Command::new(tool);
    for (key, _val) in env::vars().filter(|x| x.0.starts_with("CARGO")) {
        cmd.env_remove(key);
    }
    cmd.env_remove("RUSTUP_TOOLCHAIN");
    cmd
}

/// Creates a std::process::Command to execute the given cargo
/// command in an environment suitable for targeting the zkvm guest.
pub fn cargo_command(subcmd: &str, rust_flags: &[&str]) -> Command {
    let toolchain = format!("+{RUSTUP_TOOLCHAIN_NAME}");

    let rustc = sanitized_cmd("rustup")
        .args([&toolchain, "which", "rustc"])
        .output()
        .expect("rustup failed to find nightly toolchain")
        .stdout;

    let rustc = String::from_utf8(rustc).unwrap();
    let rustc = rustc.trim();
    println!("Using rustc: {rustc}");

    let mut cmd = sanitized_cmd("cargo");
    let mut args = vec![&toolchain, subcmd, "--target", "riscv32im-risc0-zkvm-elf"];

    if std::env::var(BUILD_LOCKED_ENV).is_ok() {
        args.push("--locked");
    }

    // let rust_src = get_env_var("OPENVM_RUST_SRC");
    // if !rust_src.is_empty() {
    // TODO[jpw]: only do this for custom src once we make openvm toolchain
    args.extend_from_slice(&[
        "-Z",
        "build-std=alloc,core,proc_macro,panic_abort,std",
        "-Z",
        "build-std-features=compiler-builtins-mem",
    ]);
    // cmd.env("__CARGO_TESTS_ONLY_SRC_ROOT", rust_src);
    // }

    println!("Building guest package: cargo {}", args.join(" "));

    let encoded_rust_flags = encode_rust_flags(rust_flags);

    cmd.env("RUSTC", rustc)
        .env("CARGO_ENCODED_RUSTFLAGS", encoded_rust_flags)
        .args(args);
    cmd
}

/// Returns a string that can be set as the value of CARGO_ENCODED_RUSTFLAGS when compiling guests
pub(crate) fn encode_rust_flags(rustc_flags: &[&str]) -> String {
    [
        // Append other rust flags
        rustc_flags,
        &[
            // Replace atomic ops with nonatomic versions since the guest is single threaded.
            "-C",
            "passes=lower-atomic",
            // Specify where to start loading the program in
            // memory.  The clang linker understands the same
            // command line arguments as the GNU linker does; see
            // https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_mono/ld.html#SEC3
            // for details.
            "-C",
            &format!("link-arg=-Ttext=0x{:08X}", memory::TEXT_START),
            // Apparently not having an entry point is only a linker warning(!), so
            // error out in this case.
            "-C",
            "link-arg=--fatal-warnings",
            "-C",
            "panic=abort",
        ],
    ]
    .concat()
    .join("\x1f")
}

// HACK: Attempt to bypass the parent cargo output capture and
// send directly to the tty, if available.  This way we get
// progress messages from the inner cargo so the user doesn't
// think it's just hanging.
fn tty_println(msg: &str) {
    let tty_file = env::var(GUEST_LOGFILE_ENV).unwrap_or_else(|_| "/dev/tty".to_string());

    let mut tty = fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(tty_file)
        .ok();

    if let Some(tty) = &mut tty {
        writeln!(tty, "{msg}").unwrap();
    } else {
        eprintln!("{msg}");
    }
}

/// Builds a package that targets the riscv guest into the specified target
/// directory.
pub fn build_guest_package(
    pkg: &Package,
    guest_opts: &GuestOptions,
    runtime_lib: Option<&str>,
    target_filter: &Option<TargetFilter>,
) -> Result<PathBuf, Option<i32>> {
    if is_skip_build() {
        return Err(None);
    }

    let target_dir = guest_opts
        .target_dir
        .clone()
        .unwrap_or_else(|| get_target_dir(pkg.manifest_path.clone()));

    fs::create_dir_all(&target_dir).unwrap();

    let runtime_rust_flags = runtime_lib
        .map(|lib| vec![String::from("-C"), format!("link_arg={}", lib)])
        .unwrap_or_default();
    let rust_flags: Vec<_> = [
        runtime_rust_flags
            .iter()
            .map(|s| s.as_str())
            .collect::<Vec<_>>(),
        guest_opts.rustc_flags.iter().map(|s| s.as_str()).collect(),
    ]
    .concat();

    let mut cmd = cargo_command("build", &rust_flags);

    let features_str = guest_opts.features.join(",");
    if !features_str.is_empty() {
        cmd.args(["--features", &features_str]);
    }

    cmd.args([
        "--manifest-path",
        pkg.manifest_path.as_str(),
        "--target-dir",
        target_dir.to_str().unwrap(),
    ]);

    if let Some(target_filter) = target_filter {
        cmd.args([
            format!("--{}", target_filter.kind).as_str(),
            target_filter.name.as_str(),
        ]);
    }

    let profile = if let Some(profile) = &guest_opts.profile {
        profile
    } else if is_debug() {
        "dev"
    } else {
        "release"
    };
    cmd.args(["--profile", profile]);

    cmd.args(&guest_opts.options);

    let command_string = format!(
        "{} {}",
        cmd.get_program().to_string_lossy(),
        cmd.get_args()
            .map(|arg| arg.to_string_lossy())
            .collect::<Vec<_>>()
            .join(" ")
    );
    tty_println(&format!("cargo command: {command_string}"));

    let mut child = cmd
        .stderr(Stdio::piped())
        .env("CARGO_TERM_COLOR", "always")
        .spawn()
        .expect("cargo build failed");
    let stderr = child.stderr.take().unwrap();

    tty_println(&format!(
        "{}: Starting build for riscv32im-risc0-zkvm-elf",
        pkg.name
    ));

    for line in BufReader::new(stderr).lines() {
        tty_println(&format!("{}: {}", pkg.name, line.unwrap()));
    }

    let res = child.wait().expect("Guest 'cargo build' failed");
    if !res.success() {
        Err(res.code())
    } else {
        Ok(get_dir_with_profile(
            &target_dir,
            profile,
            target_filter
                .as_ref()
                .map(|t| t.kind == "example")
                .unwrap_or(false),
        ))
    }
}

/// A filter for selecting a target from a package.
#[derive(Default)]
pub struct TargetFilter {
    /// The target name to match.
    pub name: String,
    /// The kind of target to match.
    pub kind: String,
}

/// Finds the unique executable target in the given package and target directory,
/// using the given target filter.
pub fn find_unique_executable<P: AsRef<Path>, Q: AsRef<Path>>(
    pkg_dir: P,
    target_dir: Q,
    target_filter: &Option<TargetFilter>,
) -> eyre::Result<PathBuf> {
    let pkg = get_package(pkg_dir.as_ref());
    let elf_paths = pkg
        .targets
        .into_iter()
        .filter(move |target| {
            if let Some(target_filter) = target_filter {
                return target.kind.iter().any(|k| k == &target_filter.kind)
                    && target.name == target_filter.name;
            }
            true
        })
        .collect::<Vec<_>>();
    if elf_paths.len() != 1 {
        Err(eyre::eyre!(
            "Expected 1 target, got {}: {:#?}",
            elf_paths.len(),
            elf_paths
        ))
    } else {
        Ok(target_dir.as_ref().join(&elf_paths[0].name))
    }
}

/// Detect rust toolchain of given name
pub fn detect_toolchain(name: &str) {
    let result = Command::new("rustup")
        .args(["toolchain", "list", "--verbose"])
        .stderr(Stdio::inherit())
        .output()
        .unwrap();
    if !result.status.success() {
        eprintln!("Failed to run: 'rustup toolchain list --verbose'");
        std::process::exit(result.status.code().unwrap());
    }

    let stdout = String::from_utf8(result.stdout).unwrap();
    if !stdout.lines().any(|line| line.trim().starts_with(name)) {
        eprintln!("The '{name}' toolchain could not be found.");
        // eprintln!("To install the risc0 toolchain, use rzup.");
        // eprintln!("For example:");
        // eprintln!("  curl -L https://risczero.com/install | bash");
        // eprintln!("  rzup install");
        std::process::exit(-1);
    }
}