Skip to content

Commit

Permalink
use bindgen instead of libbindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
flier committed Mar 19, 2017
1 parent d351998 commit ac9fc42
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 79 deletions.
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hyperscan"
version = "0.1.3"
authors = ["Flier Lu <flier.lu@nexusguard.com>"]
version = "0.1.4"
authors = ["Flier Lu <flier.lu@gmail.com>"]
build = "build.rs"
description = "Hyperscan bindings for Rust with Multiple Pattern and Streaming Scan"
homepage = "http://flier.github.io/rust-hyperscan/"
Expand All @@ -12,29 +12,29 @@ readme = "README.md"
keywords = ["regex", "hyperscan", "streaming"]

[features]
bindgen = ["libbindgen"]
gen = ["bindgen"]

[dependencies]
libc = "0.2"
log = "0.3"
regex-syntax = "0.3"
regex-syntax = "0.4"

[build-dependencies]
log = "0.3"
env_logger = "0.3"
env_logger = "0.4"
pkg-config = "0.3"

[build-dependencies.libbindgen]
version = "0.1"
features = ["llvm_stable", "static"]
[build-dependencies.bindgen]
version = "0.22"
optional = true

[dev-dependencies]
env_logger = "0.3"
regex = "0.1"
env_logger = "0.4"
regex = "0.2"
getopts = "0.2"
pcap = "0.5"
pnet = "0.16"
byteorder = "0.5"
byteorder = "1.0"

[lib]
name = "hyperscan"
Expand Down
75 changes: 55 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
#[macro_use]
extern crate log;
#[cfg(feature = "bindgen")]
extern crate libbindgen;
extern crate env_logger;
#[cfg(feature = "gen")]
extern crate bindgen;
extern crate pkg_config;

#[cfg(not(feature = "bindgen"))]
#[cfg(not(feature = "gen"))]
use std::fs;
use std::env;
use std::path::Path;
use std::path::{Path, PathBuf};

#[cfg(feature = "bindgen")]
fn generate_binding(hyperscan_root: &str, out_file: &Path) {
struct Library {
pub libs: Vec<String>,
pub link_paths: Vec<PathBuf>,
pub include_paths: Vec<PathBuf>,
}

fn find_hyperscan() -> Library {
if let Ok(prefix) = env::var("HYPERSCAN_ROOT") {
debug!("building with Hyperscan @ HYPERSCAN_ROOT={}", prefix);

Library {
libs: vec![From::from("hs")],
link_paths: vec![From::from(format!("{}/lib", prefix))],
include_paths: vec![From::from(format!("{}/include", prefix))],
}
} else if let Ok(pkg_config::Library { libs, link_paths, include_paths, .. }) =
pkg_config::Config::new().statik(true).probe("libhs") {
debug!("building with Hyperscan @ libs={:?}, link_paths={:?}, include_paths={:?}",
libs,
link_paths,
include_paths);

Library {
libs: libs,
link_paths: link_paths,
include_paths: include_paths,
}
} else {
panic!("please install hyperscan from https://github.com/01org/hyperscan")
}
}

#[cfg(feature = "gen")]
fn generate_binding(hyperscan_include_path: &str, out_file: &Path) {
info!("generating raw Hyperscan wrapper @ {}", out_file.display());

libbindgen::builder()
.header(format!("{}/include/hs/hs.h", hyperscan_root))
bindgen::builder()
.header(format!("{}/hs.h", hyperscan_include_path))
.clang_arg("-xc++")
.clang_arg("-std=c++11")
.no_unstable_rust()
Expand All @@ -24,29 +57,23 @@ fn generate_binding(hyperscan_root: &str, out_file: &Path) {
.write_to_file(out_file)
.expect("Fail to write raw wrapper");

println!("cargo:rerun-if-changed={}/include/hs/hs.h", hyperscan_root);

println!("cargo:rerun-if-changed={}/hs.h", hyperscan_include_path);
}

#[cfg(not(feature = "bindgen"))]
#[cfg(not(feature = "gen"))]
fn generate_binding(_: &str, out_file: &Path) {
fs::copy("src/raw_bindgen.rs", out_file).expect("fail to copy bindings");
}

fn main() {
env_logger::init().unwrap();

let hyperscan_root = match env::var("HYPERSCAN_ROOT") {
Ok(prefix) => prefix,
Err(_) => String::from("/usr/local"),
};

debug!("building with Hyperscan @ {}", hyperscan_root);
let libhs = find_hyperscan();

let out_dir = env::var("OUT_DIR").unwrap();
let out_file = Path::new(&out_dir).join("raw_bindgen.rs");

generate_binding(&hyperscan_root, &out_file);
generate_binding(libhs.include_paths[0].to_str().unwrap(), &out_file);

if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=dylib=c++");
Expand All @@ -55,6 +82,14 @@ fn main() {
println!("cargo:rustc-link-lib=dylib=gcc");
}

println!("cargo:rustc-link-lib=static=hs");
println!("cargo:rustc-link-search=native={}/lib", hyperscan_root);
for lib in libhs.libs {
if lib.contains("hs") {
println!("cargo:rustc-link-lib=static={}", lib);
}
}

for link_path in libhs.link_paths {
println!("cargo:rustc-link-search=native={}",
link_path.to_str().unwrap());
}
}
20 changes: 6 additions & 14 deletions examples/pcapscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ extern crate getopts;
extern crate pcap;
extern crate pnet;
extern crate byteorder;
#[macro_use]
extern crate log;
extern crate env_logger;
#[macro_use]
extern crate hyperscan;

use std::fmt;
Expand All @@ -51,9 +49,8 @@ use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::udp::UdpPacket;
use byteorder::{BigEndian, ReadBytesExt};

use hyperscan::{Pattern, Patterns, Database, DatabaseBuilder, StreamingDatabase, BlockDatabase,
RawScratch, Scratch, ScratchAllocator, BlockScanner, StreamingScanner, Stream,
RawStream};
use hyperscan::{Pattern, Patterns, Database, DatabaseBuilder, StreamingDatabase, BlockDatabase, RawScratch, Scratch,
ScratchAllocator, BlockScanner, StreamingScanner, Stream, RawStream};

#[derive(Debug)]
enum Error {
Expand Down Expand Up @@ -97,8 +94,7 @@ trait Milliseconds {

impl Milliseconds for Duration {
fn ms(&self) -> usize {
(self.as_secs() * MILLIS_PER_SEC) as usize +
(self.subsec_nanos() / NANOS_PER_MILLI) as usize
(self.as_secs() * MILLIS_PER_SEC) as usize + (self.subsec_nanos() / NANOS_PER_MILLI) as usize
}
}

Expand Down Expand Up @@ -204,9 +200,7 @@ struct Benchmark {
}

impl Benchmark {
fn new(db_streaming: StreamingDatabase,
db_block: BlockDatabase)
-> Result<Benchmark, hyperscan::Error> {
fn new(db_streaming: StreamingDatabase, db_block: BlockDatabase) -> Result<Benchmark, hyperscan::Error> {
let mut s = try!(db_streaming.alloc());

try!(s.realloc(&db_block));
Expand Down Expand Up @@ -315,8 +309,7 @@ impl Benchmark {
// Close all open Hyperscan streams (potentially generating any end-anchored matches)
fn close_streams(&mut self) {
for ref stream in &self.streams {
if let Err(err) =
stream.close(&self.scratch, Some(Self::on_match), Some(&self.match_count)) {
if let Err(err) = stream.close(&self.scratch, Some(Self::on_match), Some(&self.match_count)) {
println!("ERROR: Unable to close stream. Exiting. {}", err);
}
}
Expand Down Expand Up @@ -523,8 +516,7 @@ fn main() {
let bytes = bench.bytes();
let total_bytes = (bytes * 8 * repeat_count) as f64;
let tput_stream_scanning = total_bytes * 1000.0 / streaming_scan.ms() as f64;
let tput_stream_overhead = total_bytes * 1000.0 /
(streaming_scan + streaming_open_close).ms() as f64;
let tput_stream_overhead = total_bytes * 1000.0 / (streaming_scan + streaming_open_close).ms() as f64;
let matches_stream = bench.matches();
let match_rate_stream = (matches_stream as f64) / ((bytes * repeat_count) as f64 / 1024.0);

Expand Down
2 changes: 1 addition & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl fmt::Display for Pattern {
write!(f,
"{}:/{}/{}",
self.id,
regex_syntax::quote(self.expression.as_str()),
regex_syntax::escape(self.expression.as_str()),
self.flags)
}
}
Expand Down
Loading

0 comments on commit ac9fc42

Please sign in to comment.