Skip to content
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
8 changes: 6 additions & 2 deletions tidy-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tidy"
version = "0.1.7"
version = "0.1.6"
authors = ["Wolfgang Grimm <grimm@mondial.at>"]
edition = "2018"
publish = false
Expand All @@ -13,7 +13,11 @@ tidy-sys = { path = "../tidy-sys" }

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

[features]
default = []
pkg-config = ["tidy-sys/pkg-config"]
10 changes: 7 additions & 3 deletions tidy-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tidy-sys"
version = "0.1.7"
version = "0.1.6"
authors = ["Wolfgang Grimm <grimm@mondial.at>"]
edition = "2018"
links = "tidy"
Expand All @@ -14,7 +14,11 @@ libc = "0.2"

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

[build-dependencies.pkg-config]
optional = true
version = "0.3"
77 changes: 54 additions & 23 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,63 @@ 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 or tidybuffio.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_dir = path::PathBuf::from(std::env::var("OUT_DIR")?);
let out_fn = out_dir.join("bindings.rs");
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
let wrapper_path = out_dir.join("wrapper.h");

let includes_path = if cfg!(feature = "pkg-config") {
pkg_config()
} else {
let dst = cmake::Config::new("tidy-html5")
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
.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", "tidybuffio.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 +96,6 @@ fn main() -> Result<(), Box<dyn Error>> {
panic!("Required include files tidy.h or tidybuffio.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,30 +108,28 @@ 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())
.clang_arg(format!("{}{}", "-I", lib.include_paths[0]
.clone()
.into_os_string()
.to_str()
.unwrap()))
.header(wrapper_path.to_str().unwrap())
.rustified_enum("^Tidy.*")
.allowlist_function("^tidy.*")
.allowlist_var("^tidy.*")
.allowlist_type("^Tidy.*")
.allowlist_type("^_Tidy.*")
.layout_tests(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.clang_arg(format!("-I{}", includes_path[0].display()))
.generate()
.expect("Unable to generate bindings");

bindings
.write_to_file(out_fn)
.write_to_file(&out_fn)
.expect("Couldn't write bindings!");

let re = Regex::new(r"(?s)pub struct _TidyOption \{.+?\}").unwrap();
let mut file_r = OpenOptions::new().read(true).open(out_fn)?;
let mut file_r = OpenOptions::new().read(true).open(&out_fn)?;

let mut contents = String::new();
file_r.read_to_string(&mut contents)?;
Expand All @@ -113,13 +143,14 @@ fn main() -> Result<(), Box<dyn Error>> {
pub name: ctmbstr,
}";
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())?;
let mut file_w = OpenOptions::new()
.write(true)
.truncate(true)
.open(&out_fn)?;
file_w.write_all(replaced.as_bytes())?;
drop(file_w);

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

Ok(())
}
Loading