resiliparse 0.16.0

A collection of robust and fast processing tools for parsing and analyzing (not only) web archive data.
// Copyright 2023 Janek Bevendorff
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::env;
use std::env::consts;
use std::fmt::format;
use std::fs;
use std::path::PathBuf;
use std::process::Command;

extern crate bindgen;

fn main() {
    let out_dir = env::var("OUT_DIR").unwrap_or(".".to_string());

    let arch = consts::ARCH.replace("x86_64", "x64").replace("aarch64", "arm64");
    let os = consts::OS.replace("macos", "osx");
    let triplet = env::var("VCPKG_DEFAULT_TRIPLET").unwrap_or_else(|_| format!("{}-{}", arch, os));

    let vcpkg_output = Command::new("vcpkg")
        .args([
            "install",
            "--triplet",
            &triplet,
            "--x-install-root",
            format!("{}/vcpkg_installed", out_dir).as_str(),
        ])
        .output()
        .expect("Failed to run vcpkg.");
    if !vcpkg_output.status.success() {
        panic!("Failed to install vcpkg dependencies:\n{}", String::from_utf8_lossy(&vcpkg_output.stdout));
    }

    let mut vcpkg_dir = PathBuf::from(format!("vcpkg_installed/{}", triplet));
    if !vcpkg_dir.exists() {
        vcpkg_dir = PathBuf::from(format!("../vcpkg_installed/{}", triplet));
    }
    vcpkg_dir = fs::canonicalize(vcpkg_dir.clone()).expect(format!("{} not found", vcpkg_dir.display()).as_str());

    // let cwd = env::current_dir().expect("Failed to get current directory");
    // panic!("Current working directory: {}, {}", cwd.display(), vcpkg_dir.display());

    println!("cargo:rustc-link-search=native={}/lib", vcpkg_dir.to_str().unwrap());
    println!("cargo:rustc-link-lib=lexbor");
    println!("cargo:rerun-if-changed=src/third_party/lexbor.h");

    bindgen::Builder::default()
        .header("src/third_party/lexbor.h")
        .clang_arg(format!("-I{}/include", vcpkg_dir.to_str().unwrap()))
        .allowlist_function("(lexbor|lxb)_.*")
        .allowlist_type("(LEXBOR|lexbor|lxb)_.*")
        .allowlist_var("(LEXBOR|LXB)_.*")
        .default_enum_style(bindgen::EnumVariation::ModuleConsts)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .generate()
        .expect("Error generating Lexbor binding")
        .write_to_file(PathBuf::from(out_dir).join("lexbor.rs"))
        .unwrap();
}