Command Helpers
Functions for executing shell commands with different return types and behaviors.
Overview
These functions have PREFIX and BUILD_DIR environment variables set automatically. The shell_in() variants run commands in a specific directory.
shell
Run a shell command. Throws an error if the command fails (non-zero exit).
shell("make -j" + NPROC);
shell("install -Dm755 myapp " + join_path(PREFIX, "bin/myapp"));shell_in
Run a shell command in a specific directory. This is the recommended way to run commands in a source directory instead of changing directories.
// Run configure and make in the source directory
shell_in(ctx.source_dir, "./configure --prefix=" + PREFIX);
shell_in(ctx.source_dir, "make -j" + NPROC);
shell_in(ctx.source_dir, "make install");shell_output
Run a command and return its stdout. Throws if the command fails.
let version = shell_output("git describe --tags");
let arch = trim(shell_output("uname -m"));
let cpus = trim(shell_output("nproc"));shell_output_in
Run a command in a specific directory and return its stdout.
let version = shell_output_in(ctx.source_dir, "git describe --tags");shell_status
Run a command and return its exit code. Does not throw on non-zero exit.
let status = shell_status("command -v clang");
if status == 0 {
set_env("CC", "clang");
}
// Check if a command exists
if shell_status("which rustc") == 0 {
log("Rust is installed");
}shell_status_in
Run a command in a specific directory and return its exit code.
let status = shell_status_in(ctx.source_dir, "test -f Makefile");
if status != 0 {
shell_in(ctx.source_dir, "./configure");
}exec
Execute a command with an array of arguments (no shell interpretation). Returns the exit code. Useful when arguments contain special characters.
let code = exec("git", ["clone", "--depth", "1", url]);
let code = exec("cp", ["-r", src, dest]);exec_output
Execute a command with array arguments and return its stdout. Throws if exit code is non-zero.
let output = exec_output("rustc", ["--version"]);
let files = exec_output("find", [src, "-name", "*.c"]);