Skip to content

Commit

Permalink
Merge pull request #10 from kulp/build-refinements
Browse files Browse the repository at this point in the history
Refine build process
  • Loading branch information
petelliott authored May 9, 2020
2 parents d5b5645 + e9301f4 commit 9587845
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 68 deletions.
22 changes: 0 additions & 22 deletions C/Makefile

This file was deleted.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ num-traits = "0.1"
libc = "0.2"

[build-dependencies]
bindgen = "0.50"
bindgen = "0.53"
cc = "1.0"
65 changes: 20 additions & 45 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,17 @@ extern crate bindgen;
use std::env;
use std::path::{PathBuf, Path};
use std::process::Command;
use std::fs;
use std::io::Result;

fn build_lightning(prefix: &str) {
Command::new("./build-lightning.sh")
.arg(prefix)
.output().unwrap();
}

fn build_c(prefix: &str) {
Command::new("make")
.env("PREFIX", prefix)
.arg("-C")
.arg("C/")
.output().unwrap();
}

fn lightning_built(prefix: &Path) -> bool {
prefix.exists()
}

fn _need_bindings_built_res(prefix: &PathBuf) -> Result<bool> {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

let mt1 = fs::metadata(prefix.join("include").join("lightning.h").to_str().unwrap())?.modified()?;
let mt2 = fs::metadata("C/lightning-sys.h")?.modified()?;

let targett = fs::metadata(out_path.join("bindings.rs").to_str().unwrap())?.modified()?;

Ok(targett < mt1 || targett < mt2)
}

fn need_bindings_built(prefix: &PathBuf) -> bool {
match _need_bindings_built_res(prefix) {
Ok(b) => b,
Err(_) => true,
}
}

fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let prefix = out_path.join("lightning-prefix");
Expand All @@ -51,24 +23,27 @@ fn main() {
if !lightning_built(&prefix) {
build_lightning(prefix.to_str().unwrap());
}
build_c(prefix.to_str().unwrap());
cc::Build::new()
.include(incdir.clone())
.file("C/register.c")
.compile("lightningsys");

println!("cargo:rustc-link-search=native={}", libdir.to_str().unwrap());

println!("cargo:rustc-link-lib=static=lightning");
println!("cargo:rustc-link-lib=static=lightningsys");

if need_bindings_built(&prefix) {
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.rustfmt_bindings(true)
.clang_arg(format!("-I{}", incdir.to_str().unwrap()))
.generate()
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
println!("cargo:rustc-link-lib=dylib=lightning");

let bindings = bindgen::Builder::default()
.header("wrapper.h")
// Tell bindgen to regenerate bindings if the wrapper.h's contents or transitively
// included files change.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.rustfmt_bindings(true)
.clang_arg(format!("-I{}", incdir.to_str().unwrap()))
.generate()
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

0 comments on commit 9587845

Please sign in to comment.