-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,7 @@ dart/pubspec.lock | |
dart/.dart_tool/ | ||
dart/build/ | ||
dart/doc/api/ | ||
!rust/flatc | ||
Cargo.lock | ||
.corpus** | ||
.seed** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[workspace] | ||
members = [ | ||
"rust/flatbuffers", | ||
"rust/flatc", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "flatc" | ||
version = "0.1.0" | ||
authors = ["Richard Berry <[email protected]>", "FlatBuffers Maintainers"] | ||
license = "Apache-2.0" | ||
edition = "2018" | ||
build = "build.rs" | ||
description = "flatc Rust build script wrapper" | ||
homepage = "https://google.github.io/flatbuffers/" | ||
repository = "https://github.com/google/flatbuffers" | ||
keywords = ["flatbuffers", "serialization", "zero-copy"] | ||
categories = ["encoding", "data-structures", "memory-management"] | ||
|
||
[build-dependencies] | ||
cmake = "0.1" | ||
|
||
[dev-dependencies] | ||
syn = { version = "0.15", features = ["full", "parsing"] } | ||
tempfile = "3.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::{env, path::Path}; | ||
|
||
fn main() { | ||
let flatc_root = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()) | ||
.join("..") | ||
.join(".."); | ||
|
||
::cmake::Config::new(flatc_root) | ||
.define("CMAKE_BUILD_TYPE", "Release") | ||
.define("CMAKE_INSTALL_BINDIR", "bin") | ||
.define("FLATBUFFERS_BUILD_TESTS", "OFF") | ||
.define("FLATBUFFERS_BUILD_FLATLIB", "OFF") | ||
.define("FLATBUFFERS_BUILD_FLATHASH", "OFF") | ||
.build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,316 @@ | ||
/* | ||
* Copyright 2018 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
//! # flatc | ||
//! | ||
//! A library for build scripts to compile FlatBuffer schemas. | ||
//! | ||
//! This library is intended to be used as a `build-dependencies` entry in | ||
//! `Cargo.toml`: | ||
//! | ||
//! ```toml | ||
//! [dependencies] | ||
//! flatbuffers = "0.5" | ||
//! | ||
//! [build-dependencies] | ||
//! flatc = "0.1" | ||
//! ``` | ||
//! | ||
//! # Examples | ||
//! | ||
//! Use the `Build` structure to compile schema files: | ||
//! | ||
//! ```no_run | ||
//! extern crate flatc; | ||
//! | ||
//! fn main() { | ||
//! flatc::Build::new() | ||
//! .schema("schema/foo.fbs") | ||
//! .compile(); | ||
//! } | ||
//! ``` | ||
//! | ||
//! Include the generated Rust code: | ||
//! | ||
//! ```ignore | ||
//! mod foo_generated { | ||
//! include!(concat!(env!("OUT_DIR"), "/foo_generated.rs")); | ||
//! } | ||
//! ``` | ||
#![allow(clippy::new_without_default_derive)] | ||
|
||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
use std::{env, fmt, io}; | ||
|
||
/// An internal error that occured. | ||
struct Error { | ||
/// The kind of error. | ||
kind: ErrorKind, | ||
/// A helpful message. | ||
message: String, | ||
} | ||
|
||
impl Error { | ||
/// Create a new instance of an `Error`. | ||
fn new<S: AsRef<str>>(kind: ErrorKind, message: S) -> Self { | ||
Self { | ||
kind, | ||
message: message.as_ref().to_owned(), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"An {} error occurred during FlatBuffer compilation: {}", | ||
self.kind, self.message | ||
) | ||
} | ||
} | ||
|
||
impl From<io::Error> for Error { | ||
fn from(error: io::Error) -> Self { | ||
Self::new(ErrorKind::IO, format!("{}", error)) | ||
} | ||
} | ||
|
||
/// The type of errors that can occur when using `flatc`. | ||
enum ErrorKind { | ||
/// An I/O error occured. | ||
IO, | ||
/// An error occurred while using the schema compiler. | ||
FlatC, | ||
} | ||
|
||
impl fmt::Display for ErrorKind { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match *self { | ||
ErrorKind::IO => write!(f, "I/O"), | ||
ErrorKind::FlatC => write!(f, "flatc internal"), | ||
} | ||
} | ||
} | ||
|
||
/// A builder for compilation of one or multiple FlatBuffer schemas. | ||
/// | ||
/// This is the core type of `flatc`. For further documentation, see the | ||
/// individual methods for this structure. | ||
pub struct Build { | ||
/// The path that compiled schema files will be written into. | ||
output_path: Option<PathBuf>, | ||
/// The paths that will be tried in `include` statements in schemas. | ||
include_paths: Vec<PathBuf>, | ||
/// The schema files that will be compiled. | ||
schema: Vec<PathBuf>, | ||
} | ||
|
||
impl Build { | ||
/// Create a new instance of a `flatc` compilation with no configuration. | ||
/// | ||
/// This is finished with the [`compile`](struct.Build.html#method.compile) | ||
/// function. | ||
pub fn new() -> Self { | ||
Self { | ||
output_path: None, | ||
include_paths: Vec::new(), | ||
schema: Vec::new(), | ||
} | ||
} | ||
|
||
/// Add a schema file to the files which will be compiled. | ||
/// | ||
/// ## Shell Command | ||
/// | ||
/// ```text | ||
/// $ flatc -r foo.fbs bar.fbs baz.fbs | ||
/// ``` | ||
/// | ||
/// ## Rust Equivalent | ||
/// | ||
/// ```no_run | ||
/// extern crate flatc; | ||
/// | ||
/// fn main() { | ||
/// flatc::Build::new() | ||
/// .schema("foo.fbs") | ||
/// .schema("bar.fbs") | ||
/// .schema("baz.fbs") | ||
/// .compile(); | ||
/// } | ||
/// ``` | ||
pub fn schema<P: AsRef<Path>>(&mut self, file: P) -> &mut Self { | ||
self.schema.push(file.as_ref().to_path_buf()); | ||
self | ||
} | ||
|
||
/// Add include directories to the flatc compilation. | ||
/// | ||
/// The order in which directories are added will be preserved. | ||
/// | ||
/// ## Shell Command | ||
/// | ||
/// ```text | ||
/// $ flatc -r -I dir1 -I dir2 foo.fbs | ||
/// ``` | ||
/// | ||
/// ## Rust equivalent | ||
/// | ||
/// ```no_run | ||
/// extern crate flatc; | ||
/// | ||
/// fn main() { | ||
/// flatc::Build::new() | ||
/// .schema("foo.fbs") | ||
/// .include("dir1") | ||
/// .include("dir2") | ||
/// .compile(); | ||
/// } | ||
/// ``` | ||
pub fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self { | ||
self.include_paths.push(dir.as_ref().to_path_buf()); | ||
self | ||
} | ||
|
||
/// Run the FlatBuffer schema compiler, generating one file for each of the | ||
/// schemas added to the `Build` with the | ||
/// [`schema`](struct.Build.html#method.schema) method. | ||
/// | ||
/// Output filenames are postfixed with "*_generated*", and the extension is | ||
/// set to "*.rs*". These files are placed in `OUT_DIR`. They can be | ||
/// included in your crate using compile time macros. | ||
/// | ||
/// # Example | ||
/// | ||
/// For a compilation of `foo.fbs`, you would include it in your main crate | ||
/// with: | ||
/// | ||
/// ```ignore | ||
/// mod foo_generated { | ||
/// include!(concat!(env!("OUT_DIR"), "/foo_generated.rs")); | ||
/// } | ||
/// ``` | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function will panic if it encounters any error. This will be | ||
/// emitted as a compilation error when compiling your crate. | ||
pub fn compile(&self) { | ||
if let Err(e) = self.try_compile() { | ||
panic!("\n\n{}\n\n", e); | ||
} | ||
} | ||
} | ||
|
||
impl Build { | ||
fn try_compile(&self) -> Result<(), Error> { | ||
let output_path = self.output_path.as_ref().map_or_else( | ||
|| env::var("OUT_DIR").unwrap(), | ||
|path| format!("{}", path.display()), | ||
); | ||
|
||
let include_paths = self | ||
.include_paths | ||
.iter() | ||
.map(|path| format!("-I {}", path.display())); | ||
|
||
let fbs_files = self.schema.iter(); | ||
|
||
Command::new(concat!(env!("OUT_DIR"), "/bin/flatc")) | ||
.current_dir(env::var("CARGO_MANIFEST_DIR").unwrap()) | ||
.arg("-r") | ||
.args(&["-o", &output_path]) | ||
.args(include_paths) | ||
.args(fbs_files) | ||
.output() | ||
.map_err(Error::from) | ||
.and_then(|output| { | ||
if output.status.success() { | ||
Ok(()) | ||
} else { | ||
Err(Error::new( | ||
ErrorKind::FlatC, | ||
String::from_utf8(output.stdout).unwrap(), | ||
)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
use std::fs; | ||
use tempfile::TempDir; | ||
|
||
const MONSTER_FBS: &str = r#" | ||
// Example IDL file for our monster's schema. | ||
namespace MyGame.Sample; | ||
enum Color:byte { Red = 0, Green, Blue = 2 } | ||
union Equipment { Weapon } // Optionally add more tables. | ||
struct Vec3 { | ||
x:float; | ||
y:float; | ||
z:float; | ||
} | ||
table Monster { | ||
pos:Vec3; | ||
mana:short = 150; | ||
hp:short = 100; | ||
name:string; | ||
friendly:bool = false (deprecated); | ||
inventory:[ubyte]; | ||
color:Color = Blue; | ||
weapons:[Weapon]; | ||
equipped:Equipment; | ||
} | ||
table Weapon { | ||
name:string; | ||
damage:short; | ||
} | ||
root_type Monster;"#; | ||
|
||
impl Build { | ||
fn output<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self { | ||
self.output_path = Some(dir.as_ref().to_path_buf()); | ||
self | ||
} | ||
} | ||
|
||
#[test] | ||
fn flatc() { | ||
let tempdir = TempDir::new().expect("Failed to create temporary directory"); | ||
let input = tempdir.path().join("monster.fbs"); | ||
let output = tempdir.path().join("monster_generated.rs"); | ||
fs::write(&input, MONSTER_FBS).expect("Failed to write monster.fbs"); | ||
|
||
Build::new().schema(&input).output(&tempdir).compile(); | ||
|
||
let src = fs::read_to_string(&output).expect("Failed to read generated code"); | ||
let _ = ::syn::parse_file(&src).expect("Invalid output generated"); | ||
} | ||
} |