From 9c3ab097f949f4457a5a84b6adf1bf2190e07bb4 Mon Sep 17 00:00:00 2001 From: roblabla Date: Thu, 25 Jan 2024 15:49:21 +0100 Subject: [PATCH] Add bundled feature --- .gitmodules | 3 +++ Cargo.lock | 12 ++++++++++++ Cargo.toml | 4 +++- README.md | 5 +++++ build.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ file | 1 + 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1651f66 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "file"] + path = file + url = https://github.com/file/file diff --git a/Cargo.lock b/Cargo.lock index 92f55f7..ff5b9ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,16 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + [[package]] name = "libc" version = "0.2.104" @@ -10,6 +21,7 @@ checksum = "7b2f96d100e1cf1929e7719b7edb3b90ab5298072638fccd77be9ce942ecdfce" name = "magic-sys" version = "0.3.0" dependencies = [ + "cc", "libc", "pkg-config", "vcpkg", diff --git a/Cargo.toml b/Cargo.toml index a8a54c5..d611e5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,13 +32,15 @@ rust-version = "1.54" maintenance = { status = "passively-maintained" } [features] -default = ["pkg-config", "vcpkg"] +default = ["pkg-config", "vcpkg", "bundled"] +bundled = ["cc", "v5-40"] # the "default" version feature would be v5-39, but that's API-wise the same as v5-38 v5-40 = [] [build-dependencies] pkg-config = { version = "0.3.27", optional = true } vcpkg = { version = "0.2.15", optional = true } +cc = { version = "1.0.79", optional = true } [package.metadata.vcpkg] git = "https://github.com/microsoft/vcpkg" diff --git a/README.md b/README.md index 9299294..bdeb5a7 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,11 @@ If you do _not_ use `cargo vcpkg build`, you will have to either - `vcpkg install libmagic` and set the `VCPKG_ROOT` environment variable for your `vcpkg` root directory - `vcpkg integrate install` your `vcpkg` root user-wide +## vendor + +The `vendor` feature uses the [`cc` crate](https:/docs.rs/cc) to compile and +static link a vendored version of libmagic, currently based on 5.45. + ## Override If you disable or skip both `pkg-config` and `vcpkg` the `magic-sys` build script will fail.\ diff --git a/build.rs b/build.rs index cd77902..9c844d6 100644 --- a/build.rs +++ b/build.rs @@ -39,7 +39,54 @@ fn try_vcpkg() -> LibraryResult { } } +#[cfg(feature = "bundled")] +fn try_bundled() { + let out_dir = std::env::var("OUT_DIR").unwrap(); + let out_dir = std::path::Path::new(&out_dir); + let include_dir = out_dir.join("include"); + + // First, copy magic.h.in into out_dir/include/magic.h, replacing the X.YY + // string with the actual versionversion. + std::fs::create_dir_all(&include_dir).unwrap(); + let mut data = std::fs::read_to_string("file/src/magic.h.in").unwrap(); + data = data.replace("X.YY", "5.45"); + std::fs::write(include_dir.join("magic.h"), &data).unwrap(); + + cc::Build::new() + .include("file/src") + .include(include_dir) + .define("HAVE_UNISTD_H", "1") + .define("HAVE_INTTYPES_H", "1") + .define("VERSION", "5.45") + .file("file/src/buffer.c") + .file("file/src/magic.c") + .file("file/src/apprentice.c") + .file("file/src/softmagic.c") + .file("file/src/ascmagic.c") + .file("file/src/encoding.c") + .file("file/src/compress.c") + .file("file/src/is_csv.c") + .file("file/src/is_json.c") + .file("file/src/is_simh.c") + .file("file/src/is_tar.c") + .file("file/src/readelf.c") + .file("file/src/print.c") + .file("file/src/fsmagic.c") + .file("file/src/funcs.c") + .file("file/src/apptype.c") + .file("file/src/der.c") + .file("file/src/cdf.c") + .file("file/src/cdf_time.c") + .file("file/src/readcdf.c") + .compile("magic"); +} + fn main() { + #[cfg(feature = "bundled")] + { + let lib = try_bundled(); + return; + } #[cfg(feature = "pkg-config")] { let lib = try_pkgconfig(); diff --git a/file b/file new file mode 160000 index 0000000..4cbd5c8 --- /dev/null +++ b/file @@ -0,0 +1 @@ +Subproject commit 4cbd5c8f0851201d203755b76cb66ba991ffd8be