Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target
graph.dot
graph.svg

.direnv
result
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ exclude = ["tests/*", "benches/*/"]
edition = "2024"
rust-version = "1.85"

[lib]
name = "tree_magic_mini"
crate-type = ["cdylib", "staticlib"]

[dependencies]
petgraph = { version = "0.8.0", default-features = false }
nom = "8.0"
Expand Down
7 changes: 7 additions & 0 deletions PR_DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Add static and shared library support

Adds support for building `tree_magic_mini` as static and shared libraries with C FFI bindings.

- Added C FFI wrappers for all public APIs
- Updated `Cargo.toml` to build `cdylib` and `staticlib` crate types
- Added Nix package with C header generation via `cbindgen` and pkg-config support
8 changes: 8 additions & 0 deletions cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language = "C"

[struct]
derive_eq = true

[enum]
add_sentinel = true
prefix_with_name = true
48 changes: 48 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs =
{
self,
nixpkgs,
rust-overlay,
}:
let
systems = [
"x86_64-linux"
"aarch64-linux"
];
overlays = [
(import rust-overlay)
(self: super: {
rustToolchain = super.rust-bin.stable.latest.default.override {
extensions = [
"rust-src"
"rustfmt"
];
};
})
];
forAllSystems =
function:
nixpkgs.lib.genAttrs systems (system: function (import nixpkgs { inherit system overlays; }));
in
{
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
buildInputs = builtins.attrValues {
inherit (pkgs)
rustToolchain
rust-analyzer-unwrapped
;
};
RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";
};
});

packages = forAllSystems (pkgs: {
tree_magic_mini = pkgs.callPackage ./nix/package.nix { };
default = self.packages.${pkgs.stdenv.hostPlatform.system}.tree_magic_mini;
});
};
}
73 changes: 73 additions & 0 deletions nix/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
rustPlatform,
lib,
gcc,
stdenv,
rust-cbindgen,
}:
let
cargoToml = fromTOML (builtins.readFile ../Cargo.toml);
in
rustPlatform.buildRustPackage (finalAttrs: {
pname = "tree_magic_mini";
inherit (cargoToml.package) version;

cargoLock = {
lockFile = ../Cargo.lock;
};

src = lib.cleanSourceWith {
src = ../.;
filter =
p: type:
let
relPath = lib.removePrefix (toString ../. + "/") (toString p);
in
lib.any (p: lib.hasPrefix p relPath) [
"src"
"tests"
"magic_db"
"benches"
"cbindgen.toml"
"Cargo.toml"
"Cargo.lock"
"tree_magic_mini.pc.in"
];
};

nativeBuildInputs = [
rustPlatform.bindgenHook
rust-cbindgen
];

buildInputs = [
gcc
];

postInstall = ''
mkdir -p $out/include
cbindgen --config cbindgen.toml --crate tree_magic_mini --output $out/include/tree_magic_mini.h --lang C

mkdir -p $out/lib
cp target/${stdenv.hostPlatform.rust.rustcTarget}/release/libtree_magic_mini${stdenv.hostPlatform.extensions.sharedLibrary} $out/lib

mkdir -p $out/lib/pkgconfig

sed \
-e "s|@PREFIX@|$out|g" \
-e "s|@INCLUDE@|$out/include|g" \
-e "s|@LIBDIR@|$out/lib|g" \
-e "s|@VERSION@|${finalAttrs.version}|g" \
< ./tree_magic_mini.pc.in > $out/lib/pkgconfig/tree_magic_mini.pc
'';

doCheck = false;

meta = {
description = "Determines the MIME type of a file by traversing a filetype tree.";
homepage = "https://github.com/mbrubeck/tree_magic";
license = lib.licenses.gpl3;
maintainers = builtins.attrValues { inherit (lib.maintainers) r0chd; };
platforms = lib.platforms.all;
};
})
Loading