From d8a3137ce8ff856757647b99d1b9edbb358c8a52 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 1 Aug 2016 19:39:53 +0200 Subject: [PATCH] drying ethcore --- ethcore/build.rs | 44 +++------------------------------------ ethcore/src/client/mod.rs | 4 ++-- ethcore/src/types/mod.rs | 2 +- ipc/codegen/src/lib.rs | 18 ++++++++++++++++ ipc/tests/build.rs | 2 +- 5 files changed, 25 insertions(+), 45 deletions(-) diff --git a/ethcore/build.rs b/ethcore/build.rs index b9b884fac..584e7973e 100644 --- a/ethcore/build.rs +++ b/ethcore/build.rs @@ -14,48 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -extern crate syntex; extern crate ethcore_ipc_codegen as codegen; -use std::env; -use std::path::Path; - fn main() { - let out_dir = env::var_os("OUT_DIR").unwrap(); - // serialization pass - { - let src = Path::new("src/types/mod.rs.in"); - let dst = Path::new(&out_dir).join("types.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).unwrap(); - } - - // blockchain client interface - { - let src = Path::new("src/client/traits.rs"); - let intermediate = Path::new(&out_dir).join("traits.intermediate.rs.in"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &intermediate).unwrap(); - - let dst = Path::new(&out_dir).join("traits.ipc.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &intermediate, &dst).unwrap(); - } - - // chain notify interface - { - let src = Path::new("src/client/chain_notify.rs"); - let intermediate = Path::new(&out_dir).join("chain_notify.intermediate.rs.in"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &intermediate).unwrap(); - - let dst = Path::new(&out_dir).join("chain_notify.ipc.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &intermediate, &dst).unwrap(); - } + codegen::derive_binary("src/types/mod.rs.in"); + codegen::derive_ipc("src/client/traits.rs"); + codegen::derive_ipc("src/client/chain_notify.rs"); } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 4bcec0169..710fb5768 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -40,13 +40,13 @@ pub use self::traits::{BlockChainClient, MiningBlockChainClient, RemoteClient}; mod traits { #![allow(dead_code, unused_assignments, unused_variables, missing_docs)] // codegen issues - include!(concat!(env!("OUT_DIR"), "/traits.ipc.rs")); + include!(concat!(env!("OUT_DIR"), "/traits.rs")); } pub mod chain_notify { //! Chain notify interface #![allow(dead_code, unused_assignments, unused_variables, missing_docs)] // codegen issues - include!(concat!(env!("OUT_DIR"), "/chain_notify.ipc.rs")); + include!(concat!(env!("OUT_DIR"), "/chain_notify.rs")); } diff --git a/ethcore/src/types/mod.rs b/ethcore/src/types/mod.rs index 112f79c32..d01829ea0 100644 --- a/ethcore/src/types/mod.rs +++ b/ethcore/src/types/mod.rs @@ -17,4 +17,4 @@ //! Types used in the public api #![allow(dead_code, unused_assignments, unused_variables)] // codegen issues -include!(concat!(env!("OUT_DIR"), "/types.rs")); +include!(concat!(env!("OUT_DIR"), "/mod.rs.in")); diff --git a/ipc/codegen/src/lib.rs b/ipc/codegen/src/lib.rs index ff8abf515..4198ff568 100644 --- a/ipc/codegen/src/lib.rs +++ b/ipc/codegen/src/lib.rs @@ -132,3 +132,21 @@ pub fn derive_ipc(src_path: &str) -> Result<(), Error> { Ok(()) } + +pub fn derive_binary(src_path: &str) -> Result<(), Error> { + use std::env; + use std::path::{Path, PathBuf}; + + let out_dir = env::var_os("OUT_DIR").unwrap(); + let file_name = try!(PathBuf::from(src_path).file_name().ok_or(Error::InvalidFileName).map(|val| val.to_str().unwrap().to_owned())); + let final_path = Path::new(&out_dir).join(&file_name); + + let mut registry = syntex::Registry::new(); + register(&mut registry); + if let Err(_) = registry.expand("", &Path::new(src_path), &final_path) { + // will be reported by compiler + return Err(Error::ExpandFailure) + } + + Ok(()) +} diff --git a/ipc/tests/build.rs b/ipc/tests/build.rs index 64476a482..688d139be 100644 --- a/ipc/tests/build.rs +++ b/ipc/tests/build.rs @@ -20,5 +20,5 @@ pub fn main() { codegen::derive_ipc("nested.rs.in").unwrap(); codegen::derive_ipc("service.rs.in").unwrap(); codegen::derive_ipc("with_attrs.rs.in").unwrap(); - codegen::derive_ipc("binary.rs.in").unwrap(); + codegen::derive_binary("binary.rs.in").unwrap(); }