Skip to content

Commit a5f24fd

Browse files
committed
libosdo-sys: Pre-generate bindings
Fixes: #34 Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent 13536d2 commit a5f24fd

5 files changed

Lines changed: 1296 additions & 22 deletions

File tree

libosdp-sys/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ determine the underlying LibOSDP version.
77
This crate is not intended to be directly consumed. Please take a look at
88
[libosdp][2] (see doc [here][3]) if you intend to use LibOSDP in your project.
99

10+
## Maintainer notes
11+
12+
`src/bindings.rs` is checked into git and used for normal builds.
13+
14+
When bumping `vendor` (LibOSDP submodule), regenerate bindings and include the
15+
updated file in the same commit:
16+
17+
```sh
18+
CCACHE_DISABLE=1 LIBOSDP_SYS_REGENERATE_BINDINGS=1 cargo build -p libosdp-sys
19+
```
20+
1021
[1]: https://github.com/goToMain/libosdp
1122
[2]: https://crates.io/crates/libosdp
12-
[3]: https://docs.rs/libosdp
23+
[3]: https://docs.rs/libosdp

libosdp-sys/build.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Context;
22
use build_target::Os;
33
use std::{
44
borrow::BorrowMut,
5-
path::{Path, PathBuf},
5+
path::Path,
66
process::Command,
77
};
88
type Result<T> = anyhow::Result<T, anyhow::Error>;
@@ -103,6 +103,11 @@ fn generate_osdp_build_headers(out_dir: &str) -> Result<()> {
103103

104104
fn main() -> Result<()> {
105105
let out_dir = std::env::var("OUT_DIR").unwrap();
106+
println!("cargo:rerun-if-env-changed=LIBOSDP_SYS_REGENERATE_BINDINGS");
107+
println!("cargo:rerun-if-changed=vendor/include/osdp.h");
108+
println!("cargo:rerun-if-changed=vendor/src");
109+
println!("cargo:rerun-if-changed=vendor/utils/src");
110+
println!("cargo:rerun-if-changed=vendor/utils/include");
106111

107112
generate_osdp_build_headers(&out_dir)?;
108113

@@ -184,23 +189,31 @@ fn main() -> Result<()> {
184189
}
185190
build.compile("libosdp.a");
186191

187-
/* generate bindings */
188-
189-
let mut args = vec![format!("-I{}", &out_dir)];
190-
if short_enums {
191-
args.push("-fshort-enums".to_owned());
192-
} else {
193-
args.push("-fno-short-enums".to_owned());
192+
/* regenerate bindings only when requested by maintainer */
193+
if std::env::var("LIBOSDP_SYS_REGENERATE_BINDINGS").as_deref() == Ok("1") {
194+
let mut args = vec![format!("-I{}", &out_dir)];
195+
if short_enums {
196+
args.push("-fshort-enums".to_owned());
197+
} else {
198+
args.push("-fno-short-enums".to_owned());
199+
}
200+
let bindings = bindgen::Builder::default()
201+
.use_core()
202+
.header("vendor/include/osdp.h")
203+
.clang_args(args)
204+
.generate()
205+
.context("Unable to generate bindings")?;
206+
207+
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
208+
let bindings_path = Path::new(&manifest_dir).join("src/bindings.rs");
209+
bindings
210+
.write_to_file(&bindings_path)
211+
.context("Couldn't write checked-in bindings")?;
212+
println!(
213+
"cargo:warning=Regenerated checked-in bindings at {}",
214+
bindings_path.display()
215+
);
194216
}
195-
let bindings = bindgen::Builder::default()
196-
.use_core()
197-
.header("vendor/include/osdp.h")
198-
.clang_args(args)
199-
.generate()
200-
.context("Unable to generate bindings")?;
201-
202-
let out_path = PathBuf::from(out_dir);
203-
bindings
204-
.write_to_file(out_path.join("bindings.rs"))
205-
.context("Couldn't write bindings!")
217+
218+
Ok(())
206219
}

0 commit comments

Comments
 (0)