Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate proto-compiler logic into main.rs #317

Merged
merged 7 commits into from
Oct 19, 2020
Merged
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
16 changes: 5 additions & 11 deletions proto-compiler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
[package]
name = "ibc-proto-compiler"
name = "ibc-proto-compiler"
version = "0.1.0"
authors = ["Greg Szabo <greg@philosobear.com>"]
edition = "2018"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
walkdir = { version = "2.3" }

[build-dependencies]
prost-build = { version = "0.6" }
walkdir = { version = "2.3" }
git2 = { version = "0.13" }

[workspace]
git2 = "0.13"
prost-build = "0.6"
tempdir = "0.3.7"
walkdir = "2.3"
23 changes: 23 additions & 0 deletions proto-compiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ibc-proto-compiler

The `ibc-proto-compiler` is a simple command-line tool to automate the compilation of [Protocol Buffers](https://developers.google.com/protocol-buffers) message definitions from the [Cosmos SDK](https://github.com/cosmos/cosmos-sdk) to Rust source code with [Prost](https://lib.rs/crates/prost), for use in the [`ibc-proto` crate](https://lib.rs/crates/ibc-proto) in the [`ibc-rs` project](https://github.com/informalsystems/ibc-rs/).

## Usage

From within the `proto-compiler` directory, run the following command to clone the Cosmos SDK repository, generate the Rust sources from the Protobuf definitions, and copy them to the `ibc-proto` crate within the `ibc-rs` project:

```bash
$ cargo run
```

The path to the Cosmos SDK checkout can be specified with the `SDK_DIR` environment variable: (default: `target/cosmos-sdk/`)

```bash
$ SDK_DIR=$HOME/Code/cosmos-sdk cargo run
```

The directory to which the Rust sources should be generated in before being copied to the appropriate location can be specified with the `OUT_DIR` environment variable: (default: temporary directory via the `tempdir` crate)

```bash
$ OUT_DIR=/tmp/rust cargo run
```
50 changes: 0 additions & 50 deletions proto-compiler/build.rs

This file was deleted.

119 changes: 106 additions & 13 deletions proto-compiler/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,129 @@
use std::env::var;
use std::fs::remove_dir_all;
use std::fs::{copy, create_dir_all};
use std::path::{Path, PathBuf};

use git2::Repository;
use tempdir::TempDir;
use walkdir::WalkDir;

pub(crate) fn main() {
let ibc_proto_path = "../proto/src/prost";
fn main() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

let target_dir = root.join("../proto/src/prost");
let out_dir = var("OUT_DIR")
.map(PathBuf::from)
.or_else(|_| TempDir::new("ibc_proto_out").map(|d| d.into_path()))
.unwrap();

let sdk_dir = clone_cosmos_sdk();
compile_protos(&sdk_dir, &out_dir);
copy_generated_files(&out_dir, &target_dir);
}

fn clone_cosmos_sdk() -> PathBuf {
let sdk_dir = var("SDK_DIR").unwrap_or_else(|_| "target/cosmos-sdk".to_string());

if Path::new(&sdk_dir).exists() {
println!("[info ] Found Cosmos SDK source at '{}'", sdk_dir);
} else {
println!("[info ] Cloning cosmos/cosmos-sdk repository...");

let url = "https://github.com/cosmos/cosmos-sdk";
Repository::clone(url, &sdk_dir).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a more robust solution to this in the tendermint-proto crate. Later, for a next update.


println!("[info ] => Cloned at '{}'", sdk_dir);
}

PathBuf::from(sdk_dir)
}

fn compile_protos(sdk_dir: impl AsRef<Path>, out_dir: impl AsRef<Path>) {
println!(
"[info ] Compiling .proto files to Rust into '{}'...",
out_dir.as_ref().display()
);

let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let sdk_dir = sdk_dir.as_ref().to_owned();

// Paths
let proto_paths = [
root.join("../proto/definitions/mock"),
sdk_dir.join("proto/ibc"),
sdk_dir.join("proto/cosmos/tx"),
sdk_dir.join("proto/cosmos/base"),
];

let proto_includes_paths = [
root.join("../proto"),
sdk_dir.join("proto"),
sdk_dir.join("third_party/proto"),
];

// List available proto files
let mut protos: Vec<PathBuf> = vec![];
for proto_path in &proto_paths {
protos.append(
&mut WalkDir::new(proto_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type().is_file()
&& e.path().extension().is_some()
&& e.path().extension().unwrap() == "proto"
})
.map(|e| e.into_path())
.collect(),
);
}

// List available paths for dependencies
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();

// Compile all proto files
let mut config = prost_build::Config::default();
config.out_dir(out_dir.as_ref());
config.extern_path(".tendermint", "::tendermint_proto");
config.compile_protos(&protos, &includes).unwrap();

println!("[info ] => Done!");
}

fn copy_generated_files(from_dir: impl AsRef<Path>, to_dir: impl AsRef<Path>) {
println!(
"[info ] Copying generated Rust sources into '{}'...",
to_dir.as_ref().display()
);

// Remove old compiled files
remove_dir_all(ibc_proto_path).unwrap_or_default();
create_dir_all(ibc_proto_path).unwrap();
remove_dir_all(&to_dir).unwrap_or_default();
create_dir_all(&to_dir).unwrap();

// Copy new compiled files (prost does not use folder structures)
let err: Vec<std::io::Error> = WalkDir::new(env!("OUT_DIR"))
let errors = WalkDir::new(from_dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.map(|e| {
copy(
e.path(),
std::path::Path::new(&format!(
format!(
"{}/{}",
ibc_proto_path,
to_dir.as_ref().display(),
&e.file_name().to_os_string().to_str().unwrap()
)),
),
)
})
.filter_map(|e| e.err())
.collect();
.collect::<Vec<_>>();

if !err.is_empty() {
for e in err {
dbg!(e);
if !errors.is_empty() {
for e in errors {
println!("[error] Error while copying compiled file: {}", e);
}
panic!("error while copying compiled files")

panic!("[error] Aborted.");
}

println!("[info ] => Done!");
}