Skip to content
Open
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
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ links = "protosol"

[build-dependencies]
prost-build = "0.11.9"
flatc-rust = "0.2.0"

[dependencies]
prost = "0.11.9"
flatbuffers = "25.9.23"
64 changes: 54 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{env, fs, path::{Path, PathBuf}};
extern crate flatc_rust;
use std::{
env, fs,
path::{Path, PathBuf},
};

/// Normalize path only on Windows, else just return unchanged.
fn maybe_normalize_windows_path(path: &Path) -> PathBuf {
Expand All @@ -18,34 +22,74 @@ fn maybe_normalize_windows_path(path: &Path) -> PathBuf {
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let proto_dir = PathBuf::from("proto");
fn monitor_and_get_files(
dir: &PathBuf,
env_var: &str,
extension: &str,
) -> Result<(Vec<PathBuf>, PathBuf), Box<dyn std::error::Error>> {
let abs = env::current_dir()
.expect("cwd")
.join(&proto_dir)
.join(dir)
.canonicalize()
.map(|p| maybe_normalize_windows_path(&p))
.expect("canonicalize proto dir");
.expect("canonicalize dir");

println!("cargo:rerun-if-changed={}", abs.display());
println!("cargo:PROTO_DIR={}", abs.display());
println!("cargo:{}={}", env_var, abs.display());

let mut proto_files = vec![];
let mut files = vec![];
for entry in fs::read_dir(&abs)? {
let path = entry?.path();
if path.extension().and_then(|e| e.to_str()) == Some("proto") {
if path.extension().and_then(|e| e.to_str()) == Some(extension) {
println!("cargo:rerun-if-changed={}", path.display());
proto_files.push(path);
files.push(path);
}
}

Ok((files, abs))
}

fn compile_protos() -> Result<(), Box<dyn std::error::Error>> {
let proto_dir = PathBuf::from("proto");
let (proto_files, abs) = monitor_and_get_files(&proto_dir, "PROTO_DIR", "proto")?;

let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let mut config = prost_build::Config::new();
config.out_dir(&out_dir);
config.compile_protos(
&proto_files.iter().map(|p| p.display().to_string()).collect::<Vec<_>>(),
&proto_files
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>(),
&[abs],
)?;

Ok(())
}

fn compile_flatbuffers() -> Result<(), Box<dyn std::error::Error>> {
let flatbuffers_dir = PathBuf::from("flatbuffers");
let (flatbuffers_files, abs) =
monitor_and_get_files(&flatbuffers_dir, "FLATBUFFERS_DIR", "fbs")?;

let out_dir = PathBuf::from(env::var("OUT_DIR")?);
flatc_rust::run(flatc_rust::Args {
lang: "rust",
inputs: flatbuffers_files
.iter()
.map(|p| p.as_path())
.collect::<Vec<_>>()
.as_slice(),
out_dir: out_dir.as_path(),
includes: &[abs.as_path()],
..Default::default()
})?;

Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
compile_protos()?;
compile_flatbuffers()?;
Ok(())
}
36 changes: 36 additions & 0 deletions flatbuffers/context.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Generated from context.proto

namespace org.solana.sealevel.v2;

struct Pubkey {
address:[ubyte:32];
}

struct Signature {
signature:[ubyte:64];
}

struct Hash {
hash:[ubyte:32];
}

struct LtHash {
hash:[ushort:1024];
}

table FeatureSet {
features:[ulong];
}

table Account {
address:Pubkey (required);
lamports:ulong;
data:[ubyte] (required);
executable:bool;
owner:Pubkey (required);
}

table VoteAccount {
vote_account:Account (required);
stake:ulong;
}
28 changes: 28 additions & 0 deletions flatbuffers/elf.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Generated from elf.proto

include "metadata.fbs";
include "context.fbs";

namespace org.solana.sealevel.v2;

table ELFLoaderCtx {
elf_data:[ubyte] (required);
features:FeatureSet (required);
deploy_checks:bool;
}

table ELFLoaderEffects {
err_code:ubyte;
rodata_hash:ulong;
text_cnt:ulong;
text_off:ulong;
entry_pc:ulong;
calldests_hash:ulong;
}

table ELFLoaderFixture {
metadata:FixtureMetadata (required);
input:ELFLoaderCtx (required);
output:ELFLoaderEffects (required);
}

8 changes: 8 additions & 0 deletions flatbuffers/metadata.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Generated from metadata.proto

namespace org.solana.sealevel.v2;

table FixtureMetadata {
fn_entrypoint:string (required);
}