Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine build process #10

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!");
}