diff --git a/src/bin/trixy/cli.rs b/src/bin/trixy/cli.rs index fefce0b..f037d84 100644 --- a/src/bin/trixy/cli.rs +++ b/src/bin/trixy/cli.rs @@ -76,6 +76,13 @@ pub enum GenCommand { /// Generate auxiliary code for a language Auxiliary, + + /// Generate all code for every language + All { + #[arg(short, long, default_value_t=true)] + /// Don't generate vendored files (like c headers) + no_vendored: bool, + }, } #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] diff --git a/src/bin/trixy/generate.rs b/src/bin/trixy/generate.rs index 61424f3..21bc1f0 100644 --- a/src/bin/trixy/generate.rs +++ b/src/bin/trixy/generate.rs @@ -4,17 +4,28 @@ use trixy::macros::config::trixy::TrixyConfig; use crate::cli::{GenCommand, Language}; -pub fn handle(gen_command: GenCommand, language: Language, api_file: &Path) { +pub fn handle(gen_command: GenCommand, _language: Language, api_file: &Path) { let base_config = TrixyConfig::new("callback_function") .trixy_path(api_file.to_owned()) .dist_dir_path("dist") - .out_dir("out/dir"); + .out_dir_path("out/dir"); + let final_config: TrixyConfig; match gen_command { GenCommand::Host => { - let file_tree = base_config.generate(); - println!("{}", file_tree); + final_config = base_config.generate_auxiliary(false).generate_host(true); + } + GenCommand::Auxiliary => { + final_config = base_config.generate_auxiliary(true).generate_host(false); + } + GenCommand::All { no_vendored } => { + final_config = base_config + .generate_auxiliary(true) + .generate_host(true) + .add_c_headers(!no_vendored); } - GenCommand::Auxiliary => todo!(), } + + let file_tree = final_config.generate(); + println!("{}", file_tree); } diff --git a/src/macros/config/trixy.rs b/src/macros/config/trixy.rs index 864c325..99d5708 100644 --- a/src/macros/config/trixy.rs +++ b/src/macros/config/trixy.rs @@ -33,7 +33,7 @@ use std::{env, path::PathBuf}; -#[derive(Debug, Default)] +#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)] pub enum Language { Rust, C, @@ -54,6 +54,9 @@ pub struct TrixyConfig { /// This file is written in $OUT_DIR. pub c_header_name: String, + /// Whether to add the pre-written c headers to the output + pub add_c_headers: bool, + /// The path from the root to the distribution directory. /// Things like the c headers are copied in this dir. /// When this is [None] no dist dir will be generated. @@ -69,7 +72,13 @@ pub struct TrixyConfig { /// This path is used to place the outputted host code files. /// Normally this would be the `$OUT_DIR` environment variable set by cargo at build time. /// You have to set this, if you want to use trixy in an other context. - pub out_dir: PathBuf, + pub out_dir_path: PathBuf, + + /// Generate auxiliary files + pub generate_auxiliary: bool, + + /// Generate host files + pub generate_host: bool, } impl TrixyConfig { @@ -86,7 +95,10 @@ impl TrixyConfig { callback_function: callback_function.into(), host_code_name: "api.rs".into(), c_header_name: "interface.h".into(), - out_dir, + out_dir_path: out_dir, + add_c_headers: true, + generate_auxiliary: true, + generate_host: true, ..Default::default() } } @@ -98,6 +110,26 @@ impl TrixyConfig { } } + pub fn add_c_headers>(self, add_c_headers: T) -> Self { + Self { + add_c_headers: add_c_headers.into(), + ..self + } + } + + pub fn generate_host>(self, host: T) -> Self { + Self { + generate_host: host.into(), + ..self + } + } + pub fn generate_auxiliary>(self, auxiliary: T) -> Self { + Self { + generate_auxiliary: auxiliary.into(), + ..self + } + } + pub fn dist_dir_path>(self, dist_dir_path: T) -> Self { Self { dist_dir_path: Some(dist_dir_path.into()), @@ -112,9 +144,9 @@ impl TrixyConfig { } } - pub fn out_dir>(self, out_dir: T) -> Self { + pub fn out_dir_path>(self, out_dir: T) -> Self { Self { - out_dir: out_dir.into(), + out_dir_path: out_dir.into(), ..self } } diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 9d31784..cc6f9ea 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -20,7 +20,7 @@ */ use std::{ - fs, iter, + fs, path::{Path, PathBuf}, }; @@ -72,52 +72,62 @@ impl TrixyConfig { }); // host code - let host_code = generate::host::generate(&trixy_code, &self); - file_tree.add_host_file(GeneratedFile::new_in_out_dir( - self.host_code_name.clone(), - host_code, - Language::Rust, - &self.out_dir, - )); + if self.generate_host { + let host_code = generate::host::generate(&trixy_code, &self); + file_tree.add_host_file(GeneratedFile::new_in_out_dir( + self.host_code_name.clone(), + host_code, + Language::Rust, + &self.out_dir_path, + )); + } // auxiliary code - let c_header = generate::auxiliary::generate(&trixy_code, &self); + if self.generate_auxiliary { + let c_header = generate::auxiliary::generate(&trixy_code, &self); + if let Some(dist_dir) = &self.dist_dir_path { + let c_header_dist = + PathBuf::from(format!("{}/{}", dist_dir.display(), &self.c_header_name)); + file_tree.add_auxiliary_file(GeneratedFile::new( + c_header_dist, + c_header, + Language::C, + )); - if let Some(dist_dir) = &self.dist_dir_path { - let c_header_dist = - PathBuf::from(format!("{}/{}", dist_dir.display(), &self.c_header_name)); - file_tree.add_auxiliary_file(GeneratedFile::new(c_header_dist, c_header, Language::C)); + // // TODO(@soispha): Is this even necessary? <2024-03-25> + // let (interface_name, interface_content) = { + // let interface_header = format!( + // "\ + // /* This file is automatcially generated by Trixy */ \n\ + // #ifndef TRIXY_INTERFACE_H \n\ + // #define TRIXY_INTERFACE_H \n\ + // #include \"{}\" \n\ + // #endif // TRIXY_INTERFACE_H \n\ + // ", + // &self.c_header_name + // ); + // ("interface.h", interface_header) + // }; - // TODO(@soispha): Is this even necessary? <2024-03-25> - let (interface_name, interface_content) = { - let interface_header = format!( - "\ - /* This file is automatcially generated by Trixy */ \n\ - #ifndef TRIXY_INTERFACE_H \n\ - #define TRIXY_INTERFACE_H \n\ - #include \"{}\" \n\ - #endif // TRIXY_INTERFACE_H \n\ - ", - &self.c_header_name - ); - ("interface.h", interface_header) - }; + if self.add_c_headers { + C_TYPE_HEADER + .iter() + // .chain(iter::once(&(interface_name, &interface_content[..]))) + .for_each(|(name, content)| { + let path: &Path = &Path::new(name); - C_TYPE_HEADER - .iter() - .chain(iter::once(&(interface_name, &interface_content[..]))) - .for_each(|(name, content)| { - let path: &Path = &Path::new(name); - - let header_path = - PathBuf::from(format!("{}/{}", dist_dir.display(), path.display())); - file_tree.add_auxiliary_file(GeneratedFile::new( - header_path, - content.to_string(), - Language::C, - )); - }); + let header_path = + PathBuf::from(format!("{}/{}", dist_dir.display(), path.display())); + file_tree.add_auxiliary_file(GeneratedFile::new( + header_path, + content.to_string(), + Language::C, + )); + }); + } + } } + file_tree } }