Skip to content

Add support for UTF-32 matching, and other fixes #38

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 10 commits into
base: master
Choose a base branch
from
Open
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
- name: Build
run: cargo build --verbose --all
run: cargo build --verbose --all --features utf32
- name: Build docs
run: cargo doc --verbose --all
run: cargo doc --verbose --all --features utf32
- name: Run tests
run: cargo test --verbose --all
run: cargo test --verbose --all --features utf32
- name: Run tests with static build
shell: bash
run: PCRE2_SYS_STATIC=1 cargo test --verbose --all
run: PCRE2_SYS_STATIC=1 cargo test --verbose --all --features utf32

rustfmt:
name: rustfmt
Expand Down
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ members = ["pcre2-sys"]
libc = "0.2.146"
log = "0.4.19"
pcre2-sys = { version = "0.2.7", path = "pcre2-sys" }

[features]
default = ["jit"]

# Enable matching on UTF-32 strings
utf32 = ["pcre2-sys/utf32"]

# Enable the PCRE2 JIT
jit = ["pcre2-sys/jit"]
4 changes: 4 additions & 0 deletions pcre2-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ libc = "0.2.146"
[build-dependencies]
cc = { version = "1.0.73", features = ["parallel"] }
pkg-config = "0.3.27"

[features]
utf32 = []
jit = []
51 changes: 36 additions & 15 deletions pcre2-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,29 @@

use std::path::PathBuf;

fn main() {
println!("cargo:rerun-if-env-changed=PCRE2_SYS_STATIC");

// Build and link against a PCRE2 library with the given code unit width,
// which should be "8" or "32".
fn build_1_pcre2_lib(code_unit_width: &str) {
let target = std::env::var("TARGET").unwrap();
// let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
let upstream = PathBuf::from("upstream");

// Don't link to a system library if we want a static build.
let want_static = pcre2_sys_static().unwrap_or(target.contains("musl"));
if !want_static && pkg_config::probe_library("libpcre2-8").is_ok() {
return;
}

// Set some config options. We mostly just use the default values. We do
// this in lieu of patching config.h since it's easier.
let mut builder = cc::Build::new();
builder
.define("PCRE2_CODE_UNIT_WIDTH", "8")
.define("PCRE2_CODE_UNIT_WIDTH", code_unit_width)
.define("HAVE_STDLIB_H", "1")
.define("HAVE_MEMMOVE", "1")
.define("HAVE_CONFIG_H", "1")
.define("PCRE2_STATIC", "1")
.define("STDC_HEADERS", "1")
.define("SUPPORT_PCRE2_8", "1")
.define(&format!("SUPPORT_PCRE2_{}", code_unit_width), "1")
.define("SUPPORT_UNICODE", "1");
if target.contains("windows") {
builder.define("HAVE_WINDOWS_H", "1");
}
enable_jit(&target, &mut builder);
if feature_enabled("JIT") {
enable_jit(&target, &mut builder);
}

builder.include(upstream.join("src")).include(upstream.join("include"));
for result in std::fs::read_dir(upstream.join("src")).unwrap() {
Expand Down Expand Up @@ -78,7 +72,34 @@ fn main() {
{
builder.debug(true);
}
builder.compile("libpcre2.a");
builder.compile(&format!("libpcre2-{}.a", code_unit_width));
}

fn main() {
println!("cargo:rerun-if-env-changed=PCRE2_SYS_STATIC");

let target = std::env::var("TARGET").unwrap();
let do_utf32 = feature_enabled("UTF32");

// Don't link to a system library if we want a static build.
let want_static = pcre2_sys_static().unwrap_or(target.contains("musl"));
if want_static || pkg_config::probe_library("libpcre2-8").is_err() {
build_1_pcre2_lib("8");
}
if do_utf32
&& (want_static || pkg_config::probe_library("libpcre2-32").is_err())
{
build_1_pcre2_lib("32");
}
}

// Return whether a given feature is enabled.
fn feature_enabled(feature: &str) -> bool {
let env_var_name = format!("CARGO_FEATURE_{}", feature);
match std::env::var(&env_var_name) {
Ok(s) => s == "1",
Err(_) => false,
}
}

fn pcre2_sys_static() -> Option<bool> {
Expand Down
10 changes: 8 additions & 2 deletions pcre2-sys/generate-bindings
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

if ! command -V bindgen > /dev/null 2>&1; then
echo "bindgen must be installed" >&2
echo "to install: cargo install bindgen" >&2
echo "to install: cargo install bindgen-cli" >&2
exit 1
fi
if ! [ -f "$PCRE2SYS_HEADER" ]; then
Expand All @@ -14,6 +14,12 @@ if ! [ -f "$PCRE2SYS_HEADER" ]; then
exit 1
fi

if [ -z "$PCRE2_CODE_UNIT_WIDTH" ]; then
echo "The PCRE2_CODE_UNIT_WIDTH environment variable must be set" >&2
echo "Valid values are 8, 16, and 32" >&2
exit 1
fi

bindgen \
"$PCRE2SYS_HEADER" \
--ctypes-prefix '::libc' \
Expand All @@ -22,4 +28,4 @@ bindgen \
--allowlist-var '^PCRE2_.*' \
--blocklist-function '^.*_callout_.*' \
--blocklist-type '^.*_callout_.*' \
-- -DPCRE2_CODE_UNIT_WIDTH=8 > "$PCRE2SYS_BINDINGS"
-- -DPCRE2_CODE_UNIT_WIDTH=${PCRE2_CODE_UNIT_WIDTH} > "$PCRE2SYS_BINDINGS"
Loading