Skip to content

Commit e3abb2e

Browse files
authored
[BNTL] Add compiler flags processing to BNTL utils (#8270)
Small change to allow adding of clang compiler flags when creating BNTLs.
1 parent e2bcf29 commit e3abb2e

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

plugins/bntl_utils/cli/src/create.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub struct CreateArgs {
1919
pub include_directories: Vec<PathBuf>,
2020
#[clap(long)]
2121
pub dry_run: bool,
22+
/// A list of additional compiler options to pass to the compiler when parsing C header files.
23+
#[arg(last = true, allow_hyphen_values = true, num_args = 0..)]
24+
pub compiler_options: Vec<String>,
2225
}
2326

2427
impl CreateArgs {
@@ -41,7 +44,8 @@ impl CreateArgs {
4144
std::fs::create_dir_all(&output_path).expect("Failed to create output directory");
4245

4346
let processor = TypeLibProcessor::new(&self.name, &self.platform)
44-
.with_include_directories(self.include_directories.clone());
47+
.with_include_directories(self.include_directories.clone())
48+
.with_compiler_options(self.compiler_options.clone());
4549
// TODO: Need progress indicator here, when downloading files.
4650
let resolved_input = self.input.resolve().expect("Failed to resolve input");
4751

plugins/bntl_utils/src/command/create.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ impl PlatformField {
124124
}
125125
}
126126

127+
pub struct CompilerOptionsField;
128+
129+
impl CompilerOptionsField {
130+
pub fn field() -> FormInputField {
131+
FormInputField::MultilineText {
132+
prompt: "Compiler options".to_string(),
133+
default: None,
134+
value: None,
135+
}
136+
}
137+
138+
pub fn from_form(form: &Form) -> Option<String> {
139+
let field = form.get_field_with_name("Compiler options")?;
140+
field.try_value_string()
141+
}
142+
}
143+
127144
pub struct CreateFromDirectory;
128145

129146
impl CreateFromDirectory {
@@ -134,20 +151,24 @@ impl CreateFromDirectory {
134151
form.add_field(PlatformField::field());
135152
form.add_field(NameField::field());
136153
form.add_field(OutputDirectoryField::field());
154+
form.add_field(CompilerOptionsField::field());
155+
137156
if !form.prompt() {
138157
return;
139158
}
140159
let input_dir = InputDirectoryField::from_form(&form).unwrap();
141160
let platform_name = PlatformField::from_form(&form).unwrap();
142161
let default_name = NameField::from_form(&form).unwrap();
143162
let output_dir = OutputDirectoryField::from_form(&form).unwrap();
163+
let flags = CompilerOptionsField::from_form(&form).unwrap();
144164

145165
let Some(default_platform) = Platform::by_name(&platform_name) else {
146166
tracing::error!("Invalid platform name: {}", platform_name);
147167
return;
148168
};
149169

150-
let processor = TypeLibProcessor::new(&default_name, &default_platform.name());
170+
let processor = TypeLibProcessor::new(&default_name, &default_platform.name())
171+
.with_compiler_options(split_args(flags.as_str()));
151172

152173
let background_task = BackgroundTask::new("Processing started...", true);
153174
new_processing_state_background_thread(background_task.clone(), processor.state());
@@ -274,3 +295,33 @@ impl ProjectCommand for CreateFromProject {
274295
true
275296
}
276297
}
298+
299+
fn split_args(input: &str) -> Vec<String> {
300+
let mut args = Vec::new();
301+
let mut cur = String::new();
302+
let mut in_quote = false;
303+
let mut has_token = false;
304+
305+
for c in input.chars() {
306+
match c {
307+
'"' => {
308+
in_quote = !in_quote;
309+
has_token = true;
310+
}
311+
c if c.is_whitespace() && !in_quote => {
312+
if has_token {
313+
args.push(std::mem::take(&mut cur));
314+
has_token = false;
315+
}
316+
}
317+
c => {
318+
cur.push(c);
319+
has_token = true;
320+
}
321+
}
322+
}
323+
if has_token {
324+
args.push(cur);
325+
}
326+
args
327+
}

plugins/bntl_utils/src/process.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ pub struct TypeLibProcessor {
434434
include_directories: Vec<PathBuf>,
435435
/// Whether to process existing type libraries when processing a binary file.
436436
process_existing_type_libraries: bool,
437+
/// The compiler flags to use when processing header files.
438+
compiler_options: Vec<String>,
437439
}
438440

439441
impl TypeLibProcessor {
@@ -448,6 +450,7 @@ impl TypeLibProcessor {
448450
default_platform_name: default_platform_name.to_owned(),
449451
include_directories: Vec::new(),
450452
process_existing_type_libraries: false,
453+
compiler_options: Vec::new(),
451454
}
452455
}
453456

@@ -461,6 +464,11 @@ impl TypeLibProcessor {
461464
self
462465
}
463466

467+
pub fn with_compiler_options(mut self, options: Vec<String>) -> Self {
468+
self.compiler_options = options;
469+
self
470+
}
471+
464472
/// Whether to process existing type libraries when processing a binary file.
465473
///
466474
/// If you open `mymodule.dll` and it imports functions from `kernel32.dll`, any import found
@@ -988,7 +996,7 @@ impl TypeLibProcessor {
988996
&file_name,
989997
&platform,
990998
&platform_type_container,
991-
&[],
999+
&self.compiler_options,
9921000
&include_dirs,
9931001
"",
9941002
)

0 commit comments

Comments
 (0)