Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .github/workflows/build-toolchain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: Build Toolchain

on:
workflow_call:
inputs:
runner:
required: true
type: string
workflow_dispatch:
inputs:
runner:
required: true
type: string
default: "ubuntu-22.04"

jobs:
build:
name: Build
runs-on: ${{ inputs.runner }}
timeout-minutes: 180
env:
GITHUB_ACTIONS: "false"
CARGO_TARGET_RISCV64IM_UNKNOWN_OPENVM_ELF_RUSTFLAGS: "-Cpasses=lower-atomic"
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: "recursive"
fetch-depth: 0

- name: Detect host triple
id: host
run: |
# Match rustc's host detection
ARCH=$(uname -m)
OS=$(uname -s)
case "$ARCH" in
x86_64) ARCH_TRIPLE="x86_64" ;;
arm64|aarch64) ARCH_TRIPLE="aarch64" ;;
*) echo "Unsupported arch: $ARCH"; exit 1 ;;
esac
case "$OS" in
Linux) HOST="${ARCH_TRIPLE}-unknown-linux-gnu" ;;
Darwin) HOST="${ARCH_TRIPLE}-apple-darwin" ;;
*) echo "Unsupported OS: $OS"; exit 1 ;;
esac
echo "triple=$HOST" >> "$GITHUB_OUTPUT"
echo "Detected host triple: $HOST"

- name: Free disk space (Linux)
if: runner.os == 'Linux'
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/lib/android
sudo rm -rf /opt/ghc
sudo rm -rf /opt/hostedtoolcache
df -h

- name: Install dependencies (Linux)
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y build-essential python3 curl cmake ninja-build pkg-config libssl-dev

- name: Write bootstrap.toml
run: |
cat > bootstrap.toml << 'EOF'
[build]
extended = true
tools = ["cargo", "cargo-clippy", "clippy", "rustfmt"]
cargo-native-static = true

[rust]
lld = true
llvm-tools = true

[llvm]
download-ci-llvm = false
EOF

- name: Create target sanity check workaround
run: |
mkdir -p /tmp/rustc-targets
cat > /tmp/rustc-targets/riscv64im-unknown-openvm-elf.json << 'EOF'
{"arch": "riscv64", "os": "openvm", "llvm-target": "riscv64"}
EOF

- name: Build toolchain
env:
RUST_TARGET_PATH: /tmp/rustc-targets
run: |
python3 x.py build --stage 2 compiler/rustc library \
--target ${{ steps.host.outputs.triple }},riscv64im-unknown-openvm-elf

- name: Package toolchain
run: |
cd build/${{ steps.host.outputs.triple }}/stage2
tar --exclude lib/rustlib/src \
--exclude lib/rustlib/rustc-src \
-hczvf ../../../rust-toolchain-${{ steps.host.outputs.triple }}.tar.gz .

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: rust-toolchain-${{ steps.host.outputs.triple }}
path: rust-toolchain-${{ steps.host.outputs.triple }}.tar.gz
retention-days: 7
71 changes: 71 additions & 0 deletions .github/workflows/release-toolchain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Release Toolchain

on:
push:
tags:
- "openvm-[0-9]+.[0-9]+.[0-9]+*"
workflow_dispatch:
inputs:
tag:
description: "Tag name for release (e.g., openvm-2.1.0-rv64)"
required: true
type: string

permissions:
contents: write

jobs:
build-x86_64-linux:
uses: ./.github/workflows/build-toolchain.yml
with:
runner: ubuntu-22.04

build-aarch64-darwin:
uses: ./.github/workflows/build-toolchain.yml
with:
runner: macos-14

build-x86_64-darwin:
uses: ./.github/workflows/build-toolchain.yml
with:
runner: macos-13

release:
name: Create Release
needs: [build-x86_64-linux, build-aarch64-darwin, build-x86_64-darwin]
runs-on: ubuntu-latest
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
path: artifacts

- name: Determine tag
id: tag
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "tag=${{ inputs.tag }}" >> "$GITHUB_OUTPUT"
else
echo "tag=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"
fi

- name: List artifacts
run: ls -R artifacts/

- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
mkdir tars
find artifacts -name "*.tar.gz" -exec mv {} tars/ \;
if [ "$(ls -A tars)" ]; then
gh release create "${{ steps.tag.outputs.tag }}" \
--repo "${{ github.repository }}" \
--title "${{ steps.tag.outputs.tag }}" \
--notes "Pre-built Rust toolchain with riscv64im-unknown-openvm-elf target for OpenVM." \
--draft \
tars/*
else
echo "No artifacts found. Exiting."
exit 1
fi
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,7 @@ supported_targets! {
("riscv32gc-unknown-linux-gnu", riscv32gc_unknown_linux_gnu),
("riscv32gc-unknown-linux-musl", riscv32gc_unknown_linux_musl),
("riscv64im-unknown-none-elf", riscv64im_unknown_none_elf),
("riscv64im-unknown-openvm-elf", riscv64im_unknown_openvm_elf),
("riscv64imac-unknown-none-elf", riscv64imac_unknown_none_elf),
("riscv64gc-unknown-none-elf", riscv64gc_unknown_none_elf),
("riscv64gc-unknown-linux-gnu", riscv64gc_unknown_linux_gnu),
Expand Down Expand Up @@ -2007,6 +2008,7 @@ crate::target_spec_enum! {
Nto = "nto",
NuttX = "nuttx",
OpenBsd = "openbsd",
Openvm = "openvm",
Psp = "psp",
Psx = "psx",
Qurt = "qurt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::spec::{
Arch, Cc, LinkerFlavor, Lld, LlvmAbi, Os, PanicStrategy, RelocModel, Target, TargetMetadata,
TargetOptions,
};

pub(crate) fn target() -> Target {
Target {
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128".into(),
llvm_target: "riscv64".into(),
metadata: TargetMetadata {
description: Some("OpenVM zero-knowledge Virtual Machine (RV64IM ISA)".into()),
tier: Some(3),
host_tools: Some(false),
std: Some(true),
},
pointer_width: 64,
arch: Arch::RiscV64,

options: TargetOptions {
os: Os::Openvm,
vendor: "unknown".into(),
linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes),
linker: Some("rust-lld".into()),
cpu: "generic-rv64".into(),

// We set atomic_width to 64 for compatibility with crates such as crossbeam,
// but this should never be triggered since compilation should always lower
// atomics and be single-threaded.
max_atomic_width: Some(64),
atomic_cas: true,

features: "+m".into(),
llvm_abiname: LlvmAbi::Lp64,
panic_strategy: PanicStrategy::Abort,
relocation_model: RelocModel::Static,
emit_debug_gdb_scripts: false,
eh_frame_header: false,
singlethread: true,
..Default::default()
},
}
}
2 changes: 1 addition & 1 deletion library/panic_abort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ core = { path = "../rustc-std-workspace-core", package = "rustc-std-workspace-co
[target.'cfg(target_os = "android")'.dependencies]
libc = { version = "0.2", default-features = false }

[target.'cfg(any(target_os = "android", target_os = "zkvm"))'.dependencies]
[target.'cfg(any(target_os = "android", target_os = "zkvm", target_os = "openvm"))'.dependencies]
alloc = { path = "../alloc" }
7 changes: 7 additions & 0 deletions library/panic_abort/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ mod android;
#[cfg(target_os = "zkvm")]
mod zkvm;

#[cfg(target_os = "openvm")]
mod openvm;

use core::any::Any;
use core::panic::PanicPayload;

Expand All @@ -40,6 +43,10 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
unsafe {
zkvm::zkvm_set_abort_message(_payload);
}
#[cfg(target_os = "openvm")]
unsafe {
openvm::openvm_set_abort_message(_payload);
}

unsafe extern "Rust" {
// This is defined in std::rt.
Expand Down
25 changes: 25 additions & 0 deletions library/panic_abort/src/openvm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use alloc::string::String;
use core::panic::PanicPayload;

// Forward the abort message to OpenVM's sys_panic.
pub(crate) unsafe fn openvm_set_abort_message(payload: &mut dyn PanicPayload) {
let payload = payload.get();
let msg = match payload.downcast_ref::<&'static str>() {
Some(msg) => msg.as_bytes(),
None => match payload.downcast_ref::<String>() {
Some(msg) => msg.as_bytes(),
None => &[],
},
};
if msg.is_empty() {
return;
}

unsafe extern "C" {
fn sys_panic(msg_ptr: *const u8, len: usize) -> !;
}

unsafe {
sys_panic(msg.as_ptr(), msg.len());
}
}
1 change: 1 addition & 0 deletions library/std/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn main() {
|| target_os == "uefi"
|| target_os == "teeos"
|| target_os == "zkvm"
|| target_os == "openvm"
|| target_os == "rtems"
|| target_os == "nuttx"
|| target_os == "cygwin"
Expand Down
1 change: 1 addition & 0 deletions library/std/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::sys::random as sys;
/// VxWorks | `randABytes` after waiting for `randSecure` to become ready
/// WASI | [`random_get`](https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-random_getbuf-pointeru8-buf_len-size---result-errno)
/// ZKVM | `sys_rand`
/// OpenVM | `sys_rand`
///
/// Note that the sources used might change over time.
///
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@ cfg_select! {
target_os = "zkvm" => {
mod zkvm;
}
target_os = "openvm" => {
mod openvm;
}
}
15 changes: 15 additions & 0 deletions library/std/src/sys/alloc/openvm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::alloc::{GlobalAlloc, Layout, System};
use crate::sys::pal::abi;

#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe { abi::sys_alloc_aligned(layout.size(), layout.align()) }
}

#[inline]
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
// this allocator never deallocates memory
}
}
4 changes: 4 additions & 0 deletions library/std/src/sys/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ cfg_select! {
mod zkvm;
pub use zkvm::*;
}
target_os = "openvm" => {
mod openvm;
pub use openvm::*;
}
_ => {
mod unsupported;
pub use unsupported::*;
Expand Down
Loading
Loading