hypervectorscan-sys 0.1.12

Raw FFI bindings to [Hyperscan](https://github.com/intel/hyperscan) / [Vectorscan](https://github.com/VectorCamp/vectorscan).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! hypervectorscan-sys 构建脚本
//!
//! # 架构概述
//!
//! 本 build.rs 支持五种构建模式,通过 Cargo feature 切换:
//!
//! ## autobuild 模式(默认,feature = "autobuild")
//!
//! 自动选择最优构建方式,优先级从高到低:
//! 1. conan2   — conan CLI 包管理器
//! 2. vcpkg    — vcpkg 包管理器
//! 3. system   — 系统已安装的库(pkg-config,如 apt install libhyperscan-dev)
//! 4. vendored — bundled 源码 cmake 编译
//!
//! ## 显式指定模式
//! - conan2:   强制 Conan
//! - vcpkg:    强制 vcpkg
//! - system:   强制使用系统已安装的库
//! - vendored: 强制 bundled 源码编译(需 cmake、ragel、boost-dev)
//!
//! # 平台与后端选择
//!
//! | 架构            | conan2/vcpkg/system 后端 | vendored 后端   |
//! |-----------------|--------------------------|-----------------|
//! | x86_64          | Hyperscan                | hyperscan-src   |
//! | aarch64/arm64   | Vectorscan               | vectorscan-src  |
//! | ppc64/riscv/其他| ❌                       | vectorscan-src  |
//!
//! # 关键设计
//!
//! vendored 构建的 cmake 调用在 *-src/src/lib.rs 中(非 build.rs)。
//! cargo:rustc-link-* 指令在**本进程内**发出 → 正确传递到最终产物。

#[cfg(any(
    feature = "autobuild",
    feature = "conan2",
    feature = "vcpkg",
    feature = "system",
    feature = "vendored",
))]
use std::path::Path;

// ───────────── 入口分发 ─────────────

#[cfg(feature = "autobuild")]
fn main() -> anyhow::Result<()> {
    autobuild()
}

#[cfg(all(feature = "conan2", not(feature = "autobuild")))]
fn main() -> anyhow::Result<()> {
    conan2_build()
}

#[cfg(all(feature = "vcpkg", not(feature = "autobuild")))]
fn main() -> anyhow::Result<()> {
    vcpkg_build()
}

#[cfg(all(feature = "system", not(feature = "autobuild")))]
fn main() -> anyhow::Result<()> {
    system_build()
}

#[cfg(all(feature = "vendored", not(feature = "autobuild")))]
fn main() -> anyhow::Result<()> {
    vendored_build()
}

#[cfg(not(any(
    feature = "autobuild",
    feature = "conan2",
    feature = "vcpkg",
    feature = "system",
    feature = "vendored",
)))]
fn main() -> anyhow::Result<()> {
    anyhow::bail!(
        "hypervectorscan-sys: 未启用构建模式 feature。\n\
         请启用以下之一: autobuild, conan2, vcpkg, system, vendored\n\
         \n\
         推荐用法 (通过 hypervectorscan 顶层 crate, 默认 autobuild):\n\
           hypervectorscan = \"0.1\"\n\
         \n\
         或显式指定 vendored 模式:\n\
           hypervectorscan = {{ default-features = false, features = [\"vendored\"] }}"
    );
}

// ── autobuild:四级自动回退 ────────────────────────────────────────────

#[cfg(feature = "autobuild")]
fn autobuild() -> anyhow::Result<()> {
    if conan_available() {
        println!("cargo:warning=autobuild: 检测到 conan,使用 conan2 构建");
        return conan2_build();
    }
    if vcpkg_available() {
        println!("cargo:warning=autobuild: 检测到 vcpkg,使用 vcpkg 构建");
        return vcpkg_build();
    }
    if system_available() {
        println!("cargo:warning=autobuild: 检测到系统已安装库,使用 system 构建");
        return system_build();
    }
    println!("cargo:warning=autobuild: 回退到 vendored 构建(bundled 源码 cmake 编译)");
    vendored_build()
}

// ── 工具检测 ──────────────────────────────────────────────────────────

#[cfg(any(feature = "autobuild", feature = "conan2"))]
fn conan_available() -> bool {
    std::process::Command::new("conan")
        .arg("--version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

#[cfg(any(feature = "autobuild", feature = "vcpkg"))]
fn vcpkg_available() -> bool {
    std::process::Command::new("vcpkg")
        .arg("version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

/// 检测系统是否通过包管理器安装了 hyperscan/vectorscan 及其开发头文件
/// 通过 pkg-config 检查。Windows 上 pkg-config 不存在,直接返回 false。
#[cfg(any(feature = "autobuild", feature = "system"))]
fn system_available() -> bool {
    // Windows 不支持 pkg-config(除 MSYS2 外),交给 vcpkg 处理
    if cfg!(target_os = "windows") {
        return false;
    }
    let target = std::env::var("TARGET").unwrap_or_default();
    let pc = if is_x86_64(&target) {
        "libhs"
    } else {
        "vectorscan"
    };
    std::process::Command::new("pkg-config")
        .args(["--exists", pc])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

// ── system 构建:使用系统已安装的库(pkg-config 检测)──────────────────

/// 通过 pkg-config 查找系统已安装的 hyperscan/vectorscan
/// 适用场景:用户已通过 apt/yum/brew 安装好 dev 包
#[cfg(any(feature = "system", feature = "autobuild"))]
fn system_build() -> anyhow::Result<()> {
    let target = std::env::var("TARGET").unwrap_or_default();
    let package = if is_x86_64(&target) {
        "libhs"
    } else {
        "vectorscan"
    };
    println!("cargo:warning=system: 通过 pkg-config 查找 {package}");

    // 优先链接静态库
    let lib = pkg_config::Config::new()
        .statik(true)
        .probe(package)
        .map_err(|_| anyhow::anyhow!("pkg-config 未找到 {package},请安装对应的 dev 包"))?;

    let include_paths: Vec<&Path> = lib.include_paths.iter().map(|p| p.as_path()).collect();
    generate_bindings(&include_paths, "wrapper.h")?;

    link_stdcpp();
    Ok(())
}

// ── vendored 构建:bundled 源码 cmake 编译 ─────────────────────────────

#[cfg(any(feature = "vendored", feature = "autobuild"))]
fn vendored_build() -> anyhow::Result<()> {
    println!("cargo:warning=vendored: 从 bundled 源码编译(无需外部包管理器)");

    let target = std::env::var("TARGET").unwrap_or_default();

    if is_x86_64(&target) {
        println!("cargo:warning=vendored 后端: hyperscan-src (x86_64)");
        let mut build = hyperscan_src::Build::new();
        #[cfg(feature = "avx512")]
        build.avx512(true);
        #[cfg(feature = "avxvnni")]
        build.avxvnni(true);
        let artifacts = build.build();
        generate_bindings(&[&artifacts.include_dir], "wrapper.h")?;
    } else {
        println!("cargo:warning=vendored 后端: vectorscan-src (非 x86_64)");
        let artifacts = vectorscan_src::Build::new().build();
        generate_bindings(&[&artifacts.include_dir], "wrapper.h")?;
    }

    link_stdcpp();
    Ok(())
}

#[cfg(any(
    feature = "autobuild",
    feature = "conan2",
    feature = "vcpkg",
    feature = "system",
    feature = "vendored",
))]
fn is_x86_64(target: &str) -> bool {
    target.contains("x86_64")
}

// ── vcpkg 构建 ─────────────────────────────────────────────────────────

#[cfg(any(feature = "vcpkg", feature = "autobuild"))]
fn vcpkg_build() -> anyhow::Result<()> {
    println!("cargo:warning=vcpkg 构建模式");

    let target = std::env::var("TARGET").unwrap_or_default();
    let package = if is_x86_64(&target) {
        "hyperscan"
    } else {
        "vectorscan"
    };
    println!("cargo:warning=vcpkg: 查找包 {package}");

    let lib = match vcpkg::find_package(package) {
        Ok(lib) => lib,
        Err(_) => {
            println!("cargo:warning=vcpkg: 未找到 {package},执行 vcpkg install...");
            vcpkg_install(package)?;
            vcpkg::find_package(package)?
        }
    };

    let include_paths: Vec<&Path> = lib.include_paths.iter().map(|p| p.as_path()).collect();
    generate_bindings(&include_paths, "wrapper.h")?;

    link_stdcpp();
    Ok(())
}

#[cfg(any(feature = "vcpkg", feature = "autobuild"))]
fn vcpkg_install(package: &str) -> anyhow::Result<()> {
    let status = std::process::Command::new("vcpkg")
        .args(["install", package])
        .status()?;
    anyhow::ensure!(status.success(), "vcpkg install {package} 失败");
    Ok(())
}

// ── conan2 构建 ────────────────────────────────────────────────────────

#[cfg(any(feature = "conan2", feature = "autobuild"))]
use conan2_rs::command::BuildPolicy;
#[cfg(any(feature = "conan2", feature = "autobuild"))]
use conan2_rs::command::install::ConanInstall;

#[cfg(any(feature = "conan2", feature = "autobuild"))]
struct Backend {
    conanfile: &'static str,
    cpp_info_deps: &'static [&'static str],
    options: &'static [&'static str],
    force_build: Option<&'static str>,
}

#[cfg(any(feature = "conan2", feature = "autobuild"))]
fn detect_backend() -> anyhow::Result<Backend> {
    let target = std::env::var("TARGET").unwrap_or_default();

    let os = if target.contains("linux") {
        "linux"
    } else if target.contains("windows") {
        "windows"
    } else if target.contains("apple-darwin") {
        "macos"
    } else {
        anyhow::bail!("conan2 不支持的目标平台: {target}")
    };

    let arch = if target.contains("aarch64") || target.contains("arm64") {
        "arm64"
    } else if target.contains("x86_64") {
        "x86_64"
    } else {
        anyhow::bail!("conan2 不支持的目标架构: {target}")
    };

    match (os, arch) {
        ("linux", "x86_64") => Ok(Backend {
            conanfile: "conanfile-hyperscan.txt",
            cpp_info_deps: &["hyperscan::hs", "hyperscan::hs_runtime"],
            options: &["hyperscan/*:fat_runtime=True"],
            force_build: Some("hyperscan*"),
        }),
        ("windows", "x86_64") | ("macos", "x86_64") => Ok(Backend {
            conanfile: "conanfile-hyperscan.txt",
            cpp_info_deps: &["hyperscan::hs", "hyperscan::hs_runtime"],
            options: &[],
            force_build: None,
        }),
        ("linux", "arm64") | ("macos", "arm64") => Ok(Backend {
            conanfile: "conanfile-vectorscan.txt",
            cpp_info_deps: &["vectorscan::hs", "vectorscan::hs_runtime"],
            options: &[
                "vectorscan/*:with_fat_runtime=True",
                "vectorscan/*:with_cpu_native=False",
                "vectorscan/*:shared=False",
            ],
            force_build: Some("vectorscan*"),
        }),
        _ => anyhow::bail!("conan2 不支持的目标: {os}/{arch}"),
    }
}

#[cfg(any(feature = "conan2", feature = "autobuild"))]
fn conan2_build() -> anyhow::Result<()> {
    let backend = detect_backend()?;
    println!(
        "cargo:warning=conan2 backend: conanfile={}, force_build={:?}",
        backend.conanfile, backend.force_build
    );
    println!("cargo:rerun-if-env-changed=TARGET");

    let build_policy = match backend.force_build {
        Some(pattern) => BuildPolicy::Pattern(pattern.to_string()),
        None => BuildPolicy::Missing(None),
    };

    let mut builder = ConanInstall::builder()?
        .profile("default")
        .build(build_policy)
        .path(backend.conanfile);

    for opt in backend.options {
        builder = builder.options(*opt);
    }

    let command = builder.into_command();
    let root = command.run()?;

    let cpp_info = root.get_cpp_info(backend.cpp_info_deps)?;

    for libdir in cpp_info.lib_dirs.iter() {
        println!("cargo:rustc-link-search={}", libdir);
    }
    for lib in cpp_info.libs.iter() {
        println!("cargo:rustc-link-lib=static={}", lib);
    }

    link_stdcpp();

    let include_dirs: Vec<String> = cpp_info.include_dirs.iter().cloned().collect();
    let include_paths: Vec<&Path> = include_dirs.iter().map(|d| Path::new(d)).collect();
    generate_bindings(&include_paths, "wrapper.h")?;

    Ok(())
}

// ── 共享 ──────────────────────────────────────────────────────────────

/// Linux release 下静态链接 libstdc++
#[cfg(any(
    feature = "autobuild",
    feature = "conan2",
    feature = "vcpkg",
    feature = "system",
    feature = "vendored",
))]
fn link_stdcpp() {
    #[cfg(target_os = "linux")]
    println!("cargo:rustc-link-lib=stdc++");
    #[cfg(all(target_os = "linux", not(debug_assertions)))]
    {
        println!("cargo:rustc-target-feature=+crt-static");
        println!("cargo:rustc-link-arg=-static-libstdc++");
    }
}

/// bindgen 从 hs/hs.h 生成 Rust FFI 绑定 → OUT_DIR/hyperscan.rs
#[cfg(any(
    feature = "autobuild",
    feature = "conan2",
    feature = "vcpkg",
    feature = "system",
    feature = "vendored",
))]
fn generate_bindings(include_dirs: &[&Path], header_file: &str) -> anyhow::Result<()> {
    let out_dir = std::env::var("OUT_DIR")?;
    let out_path = format!("{out_dir}/hyperscan.rs");
    println!("cargo:rerun-if-changed={header_file}");

    let mut bindgen = bindgen::builder();
    for include in include_dirs {
        bindgen = bindgen.clang_arg(format!("-I{}", include.display()));
    }
    let bindgen = bindgen.header(header_file);

    let bindgen = bindgen
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .derive_partialeq(true)
        .allowlist_function("hs_.*")
        .allowlist_type("hs_.*")
        .allowlist_var("HS_.*");
    let bindings = bindgen.generate()?;
    bindings.write_to_file(out_path)?;

    Ok(())
}