From bedde0e19f8a6970e289c142688c1aee9b3c0156 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Wed, 1 Dec 2021 17:12:57 +0100 Subject: [PATCH] Allow static generation of schema code --- api/Cargo.toml | 6 +++++- api/build.rs | 30 ++++++++++++++++++++++++++++++ api/src/lib.rs | 2 +- api/src/schema.rs | 34 +++++++++++++++++++++++++--------- api/src/schema/.gitkeep | 0 5 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 api/src/schema/.gitkeep diff --git a/api/Cargo.toml b/api/Cargo.toml index 9a8ff7f..dc0fe93 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -6,6 +6,10 @@ build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +generated = [] +gen_static = [] + [dependencies] capnp = "0.14.3" capnpc = "0.14.4" @@ -13,4 +17,4 @@ capnpc = "0.14.4" [build-dependencies] capnpc = "0.14.4" # Used in build.rs to iterate over all files in schema/ -walkdir = "2.3.2" +walkdir = "2.3.2" \ No newline at end of file diff --git a/api/build.rs b/api/build.rs index e3de006..6238b59 100644 --- a/api/build.rs +++ b/api/build.rs @@ -7,6 +7,7 @@ fn is_hidden(entry: &DirEntry) -> bool { .unwrap_or(false) } +#[cfg(not(feature = "gen_static"))] fn main() { let mut compile_command = ::capnpc::CompilerCommand::new(); compile_command @@ -34,3 +35,32 @@ fn main() { println!("Compiling schemas..."); compile_command.run().expect("Failed to generate API code"); } + +#[cfg(feature = "gen_static")] +fn main() { + let mut compile_command = ::capnpc::CompilerCommand::new(); + compile_command + .src_prefix("schema") + .output_path("src/schema") + .default_parent_module(vec!["schema".to_string()]); + + for entry in WalkDir::new("schema") + .max_depth(2) + .into_iter() + .filter_entry(|e| !is_hidden(e)) + .filter_map(Result::ok) // Filter all entries that access failed on + .filter(|e| !e.file_type().is_dir()) // Filter directories + // Filter non-schema files + .filter(|e| e.file_name() + .to_str() + .map(|s| s.ends_with(".capnp")) + .unwrap_or(false) + ) + { + println!("Collecting schema file {}", entry.path().display()); + compile_command + .file(entry.path()); + } + + compile_command.run().expect("Failed to generate extra API code"); +} \ No newline at end of file diff --git a/api/src/lib.rs b/api/src/lib.rs index a33af5a..3e08d04 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -3,7 +3,7 @@ //! //! This crate contains slightly nicer and better documented bindings for the FabAccess API. - +#[allow(dead_code)] pub mod schema; /// Authentication subsystem diff --git a/api/src/schema.rs b/api/src/schema.rs index 8ed5d10..21124ca 100644 --- a/api/src/schema.rs +++ b/api/src/schema.rs @@ -1,41 +1,57 @@ pub use capnpc::schema_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod auth_capnp { include!(concat!(env!("OUT_DIR"), "/auth_capnp.rs")); } +#[cfg(not(feature = "generated"))] +pub mod auth_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod main_capnp { include!(concat!(env!("OUT_DIR"), "/main_capnp.rs")); } +#[cfg(not(feature = "generated"))] +pub mod main_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod utils_capnp { include!(concat!(env!("OUT_DIR"), "/utils_capnp.rs")); } +#[cfg(not(feature = "generated"))] +pub mod utils_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod resource_capnp { include!(concat!(env!("OUT_DIR"), "/resource_capnp.rs")); } +#[cfg(not(feature = "generated"))] +pub mod resource_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod resources_capnp { include!(concat!(env!("OUT_DIR"), "/resources_capnp.rs")); } +#[cfg(not(feature = "generated"))] +pub mod resources_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod role_capnp { include!(concat!(env!("OUT_DIR"), "/role_capnp.rs")); } +#[cfg(not(feature = "generated"))] +pub mod role_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod user_capnp { include!(concat!(env!("OUT_DIR"), "/user_capnp.rs")); } +#[cfg(not(feature = "generated"))] +pub mod user_capnp; -#[allow(dead_code)] +#[cfg(feature = "generated")] pub mod users_capnp { include!(concat!(env!("OUT_DIR"), "/users_capnp.rs")); -} \ No newline at end of file +} +#[cfg(not(feature = "generated"))] +pub mod users_capnp; \ No newline at end of file diff --git a/api/src/schema/.gitkeep b/api/src/schema/.gitkeep new file mode 100644 index 0000000..e69de29