Skip to content

add pkgconfig feature - make static builds possible #2

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tidy-sys/tidy-html5"]
path = tidy-sys/tidy-html5
url = https://github.com/htacg/tidy-html5
5 changes: 4 additions & 1 deletion tidy-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ tidy-sys = { path = "../tidy-sys" }

[build-dependencies]
regex = "1"
bindgen = "0.55"
bindgen = "0.62.0"
cc = { version = "1.0", features = ["parallel"] }
pkg-config = "0.3"
glob = "0.3.0"

[features]
default = []
pkg-config = ["tidy-sys/pkg-config"]
8 changes: 6 additions & 2 deletions tidy-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ libc = "0.2"

[build-dependencies]
regex = "1"
bindgen = "0.55"
bindgen = "0.62.0"
cc = { version = "1.0", features = ["parallel"] }
pkg-config = "0.3"
glob = "0.3.0"
cmake = "0.1"

[build-dependencies.pkg-config]
optional = true
version = "0.3"
58 changes: 44 additions & 14 deletions tidy-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::io::prelude::*;
use std::iter::Iterator;
use std::path;

#[cfg(feature = "pkg-config")]
extern crate pkg_config;

fn strip_to_include(mut paths: Paths, prefix: &str) -> Option<String> {
Expand All @@ -22,31 +23,62 @@ fn strip_to_include(mut paths: Paths, prefix: &str) -> Option<String> {
r = &r[1..]
}
println!("Entry: {} {}", p, r);
return Some(r.to_string());
Some(r.to_string())
}
_ => None,
}
}

fn main() -> Result<(), Box<dyn Error>> {
let out_fn = "src/bindings.rs";

#[cfg(feature = "pkg-config")]
fn pkg_config() -> Vec<path::PathBuf> {
let lib = pkg_config::Config::new()
.atleast_version("5.2.0")
.probe("tidy")
.unwrap();

let out_dir = std::env::var("OUT_DIR").unwrap();

if lib.include_paths.len() == 0 {
panic!("No include dir found, can't find tidy.h/buffio.h")
}

println!("cargo:rustc-link-lib=tidy");
lib.include_paths
}

#[cfg(not(feature = "pkg-config"))]
fn pkg_config() -> Vec<path::PathBuf> {
unimplemented!()
}

fn main() -> Result<(), Box<dyn Error>> {
let out_fn = "src/bindings.rs";
let out_dir = std::env::var("OUT_DIR").unwrap();
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
let wrapper_path = path::Path::new(&out_dir).join("wrapper.h");

let includes_path = if cfg!(feature = "pkg-config") {
pkg_config()
} else {
let dst = cmake::Config::new("tidy-html5")
.define("TIDY_COMPAT_HEADERS", "ON")
.build();
println!("cargo:rustc-link-search=native={}/lib", dst.display());
if target_os == "windows" {
println!("cargo:rustc-link-lib=static=tidy_static");
} else {
println!("cargo:rustc-link-lib=static=tidy");
}
let mut include_path = path::PathBuf::new();
include_path.push(dst);
include_path.push("include");

vec![include_path]
};

let h_files: [&str; 2] = ["tidy.h", "buffio.h"];
let mut includes: [Option<String>; 2] = Default::default();

for (i, find) in h_files.iter().enumerate() {
for dir in &lib.include_paths {
for dir in &includes_path {
let fileglob = dir.join("**").join(find);
let mut i1 = strip_to_include(
glob(fileglob.to_str().unwrap()).unwrap(),
Expand All @@ -63,7 +95,6 @@ fn main() -> Result<(), Box<dyn Error>> {
panic!("Required include files tidy.h/buffio.h not found")
}

let wrapper_path = path::Path::new(&out_dir).join("wrapper.h");
let mut file_w = OpenOptions::new()
.create(true)
.write(true)
Expand All @@ -76,16 +107,17 @@ fn main() -> Result<(), Box<dyn Error>> {
includes[1].as_ref().unwrap()
);

file_w.write(h_text.as_bytes())?;
file_w.write_all(h_text.as_bytes())?;
drop(file_w);

let bindings = bindgen::Builder::default()
.header(wrapper_path.to_path_buf().to_str().unwrap())
.header(wrapper_path.to_str().unwrap())
.rustified_enum("^Tidy.*")
.whitelist_function("^tidy.*")
.whitelist_var("^tidy.*")
.layout_tests(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_arg(format!("-I{}", includes_path[0].display()))
.generate()
.expect("Unable to generate bindings");

Expand All @@ -109,12 +141,10 @@ fn main() -> Result<(), Box<dyn Error>> {
}";
let replaced = re.replace(&contents, new_val);
let mut file_w = OpenOptions::new().write(true).truncate(true).open(out_fn)?;
file_w.write(replaced.as_bytes())?;
file_w.write_all(replaced.as_bytes())?;
drop(file_w);

println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=path/to/Cargo.lock");
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rustc-link-lib=tidy");

Ok(())
}
Loading