Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support emitting Makefile-syntax depfiles like gcc/clang/rustc.
Needed to auto-bindgen with a ninja build without the build graph going stale.
- Loading branch information
Showing
9 changed files
with
362 additions
and
1 deletion.
There are no files selected for viewing
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,175 @@ | ||
// Generating build depfiles from parsed bindings. | ||
|
||
use crate::{ | ||
callbacks::{ | ||
DeriveTrait, EnumVariantCustomBehavior, EnumVariantValue, | ||
ImplementsTrait, IntKind, MacroParsingBehavior, ParseCallbacks, | ||
}, | ||
BindgenOptions, | ||
}; | ||
use std::{ | ||
collections::BTreeSet, | ||
path::PathBuf, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct DepfileSpec { | ||
pub output_module: String, | ||
pub depfile_path: PathBuf, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct DepCollector { | ||
inner: Arc<Mutex<InnerCollector>>, | ||
} | ||
|
||
impl DepCollector { | ||
pub(crate) fn install(options: &mut BindgenOptions) { | ||
let collector = DepCollector { | ||
inner: Arc::new(Mutex::new(InnerCollector { | ||
files: Default::default(), | ||
prev_cb: None, | ||
})), | ||
}; | ||
collector.inner.lock().unwrap().prev_cb = std::mem::replace( | ||
&mut options.parse_callbacks, | ||
Some(Box::new(collector.clone())), | ||
); | ||
} | ||
|
||
pub(crate) fn retrieve(options: &BindgenOptions) -> Option<&Self> { | ||
options | ||
.parse_callbacks | ||
.as_ref() | ||
.and_then(|cb| cb.as_any()) | ||
.and_then(|cb| cb.downcast_ref()) | ||
} | ||
|
||
pub(crate) fn write_depfile( | ||
&self, | ||
spec: &DepfileSpec, | ||
) -> std::io::Result<()> { | ||
let inner = self.inner.lock().unwrap(); | ||
|
||
let mut buf = format!("{}:", spec.output_module); | ||
|
||
for file in &inner.files { | ||
buf = format!("{} {}", buf, file); | ||
} | ||
|
||
std::fs::write(&spec.depfile_path, &buf) | ||
} | ||
} | ||
|
||
impl ParseCallbacks for DepCollector { | ||
fn include_file(&self, filename: &str) { | ||
let mut inner = self.inner.lock().unwrap(); | ||
inner.files.insert(filename.to_owned()); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.include_file(filename); | ||
} | ||
} | ||
|
||
// all trait methods below here just forward to the wrapped callbacks | ||
|
||
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.will_parse_macro(name) | ||
} else { | ||
Default::default() | ||
} | ||
} | ||
|
||
fn int_macro(&self, name: &str, value: i64) -> Option<IntKind> { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.int_macro(name, value) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn str_macro(&self, name: &str, value: &[u8]) { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.str_macro(name, value); | ||
} | ||
} | ||
|
||
fn func_macro(&self, name: &str, value: &[&[u8]]) { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.func_macro(name, value); | ||
} | ||
} | ||
|
||
fn enum_variant_behavior( | ||
&self, | ||
enum_name: Option<&str>, | ||
original_variant_name: &str, | ||
variant_value: EnumVariantValue, | ||
) -> Option<EnumVariantCustomBehavior> { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.enum_variant_behavior( | ||
enum_name, | ||
original_variant_name, | ||
variant_value, | ||
) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn enum_variant_name( | ||
&self, | ||
enum_name: Option<&str>, | ||
original_variant_name: &str, | ||
variant_value: EnumVariantValue, | ||
) -> Option<String> { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.enum_variant_name( | ||
enum_name, | ||
original_variant_name, | ||
variant_value, | ||
) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn item_name(&self, original_item_name: &str) -> Option<String> { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.item_name(original_item_name) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn blocklisted_type_implements_trait( | ||
&self, | ||
name: &str, | ||
derive_trait: DeriveTrait, | ||
) -> Option<ImplementsTrait> { | ||
let inner = self.inner.lock().unwrap(); | ||
if let Some(prev_cb) = &inner.prev_cb { | ||
prev_cb.blocklisted_type_implements_trait(name, derive_trait) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn as_any(&self) -> Option<&dyn std::any::Any> { | ||
Some(self) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct InnerCollector { | ||
files: BTreeSet<String>, | ||
prev_cb: Option<Box<dyn ParseCallbacks>>, | ||
} |
Oops, something went wrong.