use codegen, instead of build.rs, to build header file

This commit is contained in:
Dustin J. Mitchell
2022-02-16 00:28:07 +00:00
parent 02055b122e
commit b1d537ac87
8 changed files with 76 additions and 35 deletions

47
xtask/src/main.rs Normal file
View File

@@ -0,0 +1,47 @@
//! This executable defines the `cargo xtask` subcommands.
//!
//! At the moment it is very simple, but if this grows more subcommands then
//! it will be sensible to use `clap` or another similar library.
use cbindgen::*;
use std::env;
use std::path::PathBuf;
pub fn main() -> anyhow::Result<()> {
let arg = env::args().nth(1);
match arg.as_ref().map(|arg| arg.as_str()) {
Some("codegen") => codegen(),
Some(arg) => anyhow::bail!("unknown xtask {}", arg),
_ => anyhow::bail!("unknown xtask"),
}
}
/// `cargo xtask codegen`
///
/// This uses cbindgen to generate `lib/taskchampion.h`.
fn codegen() -> anyhow::Result<()> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let workspace_dir = manifest_dir.parent().unwrap();
let lib_crate_dir = workspace_dir.join("lib");
Builder::new()
.with_crate(&lib_crate_dir)
.with_config(Config {
language: Language::C,
cpp_compat: true,
sys_includes: vec!["stdbool.h".into(), "stdint.h".into(), "time.h".into()],
usize_is_size_t: true,
no_includes: true,
enumeration: EnumConfig {
// this appears to still default to true for C
enum_class: false,
..Default::default()
},
..Default::default()
})
.generate()
.expect("Unable to generate bindings")
.write_to_file(lib_crate_dir.join("taskchampion.h"));
Ok(())
}