Skip to content

Commit

Permalink
Merge 669d1cc into c9b6123
Browse files Browse the repository at this point in the history
  • Loading branch information
otavio committed Feb 28, 2021
2 parents c9b6123 + 669d1cc commit 2b98087
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 85 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/windows.yml
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- master
- feature-*
- issue-*
pull_request:

jobs:
Expand Down
75 changes: 0 additions & 75 deletions binding_script.rs

This file was deleted.

68 changes: 68 additions & 0 deletions scripts/generate-ffi
@@ -0,0 +1,68 @@
#!/bin/sh

die() {
echo "ERROR: $*"
exit 1
}

is_installed() {
type $1 > /dev/null 2>&1 || die "'$1' tool has not been found. Install it and try again"
}

is_installed bindgen

basedir=$(dirname $(readlink -f $0))/..

# Generate temporary wrapper, so we can include the required headers
cat <<EOF > $basedir/wrapper.h
#include <archive.h>
#include <archive_entry.h>
EOF

bindgen \
\
--size_t-is-usize \
\
--raw-line "#![allow(non_camel_case_types)]" \
--raw-line "pub const ARCHIVE_EOF: i32 = 1;" \
--raw-line "pub const ARCHIVE_OK: i32 = 0;" \
\
--whitelist-var "ARCHIVE_EXTRACT_TIME" \
--whitelist-var "ARCHIVE_EXTRACT_PERM" \
--whitelist-var "ARCHIVE_EXTRACT_ACL" \
--whitelist-var "ARCHIVE_EXTRACT_FFLAGS" \
--whitelist-var "ARCHIVE_EXTRACT_OWNER" \
--whitelist-var "ARCHIVE_EXTRACT_FFLAGS" \
--whitelist-var "ARCHIVE_EXTRACT_XATTR" \
--whitelist-function "archive_read_new" \
--whitelist-function "archive_read_set_seek_callback" \
--whitelist-function "archive_read_support_filter_all" \
--whitelist-function "archive_read_support_format_all" \
--whitelist-function "archive_read_support_format_raw" \
--whitelist-function "archive_read_close" \
--whitelist-function "archive_read_free" \
--whitelist-function "archive_read_data_block" \
--whitelist-function "archive_read_next_header" \
--whitelist-function "archive_read_open" \
--whitelist-function "archive_write_disk_new" \
--whitelist-function "archive_write_disk_set_options" \
--whitelist-function "archive_write_disk_set_standard_lookup" \
--whitelist-function "archive_write_header" \
--whitelist-function "archive_write_finish_entry" \
--whitelist-function "archive_write_data_block" \
--whitelist-function "archive_write_close" \
--whitelist-function "archive_write_free" \
--whitelist-function "archive_entry_pathname" \
--whitelist-function "archive_entry_free" \
--whitelist-function "archive_entry_set_pathname" \
--whitelist-function "archive_entry_set_hardlink" \
--whitelist-function "archive_entry_hardlink" \
--whitelist-function "archive_set_error" \
--whitelist-function "archive_error_string" \
--whitelist-function "archive_errno" \
\
--output $basedir/src/ffi.rs \
\
$basedir/wrapper.h

rm $basedir/wrapper.h
1 change: 1 addition & 0 deletions shell.nix
Expand Up @@ -5,6 +5,7 @@ with pkgs;
stdenv.mkDerivation {
name = "compress-tools";
buildInputs = [
rust-bindgen
pkg-config
libarchive
clang
Expand Down
85 changes: 77 additions & 8 deletions src/build.rs
Expand Up @@ -2,21 +2,90 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

fn main() {
find_libarchive()
fn main() -> Result<(), Box<dyn std::error::Error>> {
find_libarchive()?;

Ok(())
}

#[cfg(not(target_env = "msvc"))]
fn find_libarchive() {
fn find_libarchive() -> Result<pkg_config::Library, pkg_config::Error> {
pkg_config::Config::new()
.atleast_version("3.2.0")
.probe("libarchive")
.expect("Unable to find libarchive");
}

#[cfg(target_env = "msvc")]
fn find_libarchive() {
vcpkg::Config::new()
.find_package("libarchive")
.expect("Unable to find libarchive");
fn find_libarchive() -> Result<vcpkg::Library, vcpkg::Error> {
vcpkg::Config::new().find_package("libarchive")
}

#[cfg(feature = "generate_ffi")]
fn update_bindings() -> Result<(), Box<dyn std::error::Error>> {
use std::path::PathBuf;

let mut lib = find_libarchive()?;

let include_path = lib
.include_paths
.pop()
.unwrap_or(PathBuf::from("usr/include"));

let bindings = bindgen::Builder::default()
// Set rustfmt setting
.rustfmt_configuration_file(Some(".rustfmt.toml".into()))

// Set include path
.header(format!("{}/archive.h", include_path.display()))
.header(format!("{}/archive_entry.h", include_path.display()))

// We need to add this as raw_line to pass cargo clippy warning about
// convert to upper camel case
.raw_line("#![allow(non_camel_case_types)]\n")

// We need to add this as raw_line otherwise bindgen generates this as
// u32, causing type mismatch
.raw_line("pub const ARCHIVE_EOF: i32 = 1;")
.raw_line("pub const ARCHIVE_OK: i32 = 0;")

// Binding whitelist
.whitelist_var("ARCHIVE_EXTRACT_TIME")
.whitelist_var("ARCHIVE_EXTRACT_PERM")
.whitelist_var("ARCHIVE_EXTRACT_ACL")
.whitelist_var("ARCHIVE_EXTRACT_FFLAGS")
.whitelist_var("ARCHIVE_EXTRACT_OWNER")
.whitelist_var("ARCHIVE_EXTRACT_FFLAGS")
.whitelist_var("ARCHIVE_EXTRACT_XATTR")
.whitelist_function("archive_read_new")
.whitelist_function("archive_read_set_seek_callback")
.whitelist_function("archive_read_support_filter_all")
.whitelist_function("archive_read_support_format_all")
.whitelist_function("archive_read_support_format_raw")
.whitelist_function("archive_read_close")
.whitelist_function("archive_read_free")
.whitelist_function("archive_read_data_block")
.whitelist_function("archive_read_next_header")
.whitelist_function("archive_read_open")
.whitelist_function("archive_write_disk_new")
.whitelist_function("archive_write_disk_set_options")
.whitelist_function("archive_write_disk_set_standard_lookup")
.whitelist_function("archive_write_header")
.whitelist_function("archive_write_finish_entry")
.whitelist_function("archive_write_data_block")
.whitelist_function("archive_write_close")
.whitelist_function("archive_write_free")
.whitelist_function("archive_entry_pathname")
.whitelist_function("archive_entry_free")
.whitelist_function("archive_entry_set_pathname")
.whitelist_function("archive_entry_set_hardlink")
.whitelist_function("archive_entry_hardlink")
.whitelist_function("archive_set_error")
.whitelist_function("archive_error_string")
.whitelist_function("archive_errno")
.generate()
.expect("Unable to generate bindings");

bindings.write_to_file(PathBuf::from("src/ffi.rs"))?;

Ok(())
}
3 changes: 1 addition & 2 deletions src/ffi.rs
@@ -1,7 +1,6 @@
/* automatically generated by rust-bindgen */
/* automatically generated by rust-bindgen 0.55.1 */

#![allow(non_camel_case_types)]

pub const ARCHIVE_EOF: i32 = 1;
pub const ARCHIVE_OK: i32 = 0;

Expand Down

0 comments on commit 2b98087

Please sign in to comment.