-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: dynamically generate flatc rust at compile time
- Loading branch information
1 parent
4bd6c6e
commit d348870
Showing
21 changed files
with
160 additions
and
3,638 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
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
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
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 @@ | ||
ignore = ["**/*_generated.rs"] |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,60 @@ | ||
use std::env; | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
fn main() { | ||
// Define schema directory and target directory for generated Rust code. | ||
let schema_dir = Path::new("../schemas"); | ||
let generated_src = | ||
PathBuf::from(env::var("GENERATED_CODE_DIR").unwrap_or_else(|_| "src".to_string())); | ||
|
||
// Collect all .fbs files in the schema directory. | ||
let file_list: Vec<_> = fs::read_dir(&schema_dir) | ||
.expect("Schema directory not found") | ||
.filter_map(|entry| { | ||
entry.ok().and_then(|e| { | ||
let path = e.path(); | ||
if path.extension().and_then(|ext| ext.to_str()) == Some("fbs") { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
}) | ||
}) | ||
.collect(); | ||
|
||
// Build flatc arguments. | ||
let mut args = vec![ | ||
"--gen-mutable", | ||
"--gen-object-api", | ||
"--reflect-names", | ||
"--rust", | ||
"-o", | ||
generated_src | ||
.to_str() | ||
.expect("Invalid path for generated source"), | ||
]; | ||
|
||
// Add schema file paths to the arguments. | ||
args.extend(file_list.iter().map(|path| path.to_str().unwrap())); | ||
|
||
// Execute flatc. | ||
let compile_status = Command::new("flatc") | ||
.args(&args) | ||
.status() | ||
.expect("Failed to execute flatc command"); | ||
|
||
assert!( | ||
compile_status.success(), | ||
"flatc failed to compile schema files: {:?}", | ||
file_list | ||
); | ||
|
||
// Set an environment variable with the generated source path, stripping "src/" if present. | ||
let generated_path = generated_src.strip_prefix("src").unwrap_or(&generated_src); | ||
println!("cargo:rustc-env=GENERATED_SRC={}", generated_path.display()); | ||
|
||
// Instruct Cargo to re-run this script if schema files change. | ||
println!("cargo:rerun-if-changed={}", schema_dir.display()); | ||
} |
Oops, something went wrong.