Skip to content

Commit 7eaa25f

Browse files
committed
add Nix package with C header generation
1 parent 781fb29 commit 7eaa25f

5 files changed

Lines changed: 108 additions & 1 deletion

File tree

PR_DESCRIPTION.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Add static and shared library support
2+
3+
Adds support for building `tree_magic_mini` as static and shared libraries with C FFI bindings.
4+
5+
- Added C FFI wrappers for all public APIs
6+
- Updated `Cargo.toml` to build `cdylib` and `staticlib` crate types
7+
- Added Nix package with C header generation via `cbindgen` and pkg-config support

cbindgen.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language = "C"
2+
3+
[struct]
4+
derive_eq = true
5+
6+
[enum]
7+
add_sentinel = true
8+
prefix_with_name = true

flake.nix

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
};
99

1010
outputs =
11-
{ nixpkgs, rust-overlay, ... }:
11+
{
12+
self,
13+
nixpkgs,
14+
rust-overlay,
15+
}:
1216
let
1317
systems = [
1418
"x86_64-linux"
@@ -41,5 +45,10 @@
4145
RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";
4246
};
4347
});
48+
49+
packages = forAllSystems (pkgs: {
50+
tree_magic_mini = pkgs.callPackage ./nix/package.nix { };
51+
default = self.packages.${pkgs.stdenv.hostPlatform.system}.tree_magic_mini;
52+
});
4453
};
4554
}

nix/package.nix

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
rustPlatform,
3+
lib,
4+
gcc,
5+
stdenv,
6+
rust-cbindgen,
7+
}:
8+
let
9+
cargoToml = fromTOML (builtins.readFile ../Cargo.toml);
10+
in
11+
rustPlatform.buildRustPackage (finalAttrs: {
12+
pname = "tree_magic_mini";
13+
inherit (cargoToml.package) version;
14+
15+
cargoLock = {
16+
lockFile = ../Cargo.lock;
17+
};
18+
19+
src = lib.cleanSourceWith {
20+
src = ../.;
21+
filter =
22+
p: type:
23+
let
24+
relPath = lib.removePrefix (toString ../. + "/") (toString p);
25+
in
26+
lib.any (p: lib.hasPrefix p relPath) [
27+
"src"
28+
"tests"
29+
"magic_db"
30+
"benches"
31+
"cbindgen.toml"
32+
"Cargo.toml"
33+
"Cargo.lock"
34+
"tree_magic_mini.pc.in"
35+
];
36+
};
37+
38+
nativeBuildInputs = [
39+
rustPlatform.bindgenHook
40+
rust-cbindgen
41+
];
42+
43+
buildInputs = [
44+
gcc
45+
];
46+
47+
postInstall = ''
48+
mkdir -p $out/include
49+
cbindgen --config cbindgen.toml --crate tree_magic_mini --output $out/include/tree_magic_mini.h --lang C
50+
51+
mkdir -p $out/lib
52+
cp target/${stdenv.hostPlatform.rust.rustcTarget}/release/libtree_magic_mini${stdenv.hostPlatform.extensions.sharedLibrary} $out/lib
53+
54+
mkdir -p $out/lib/pkgconfig
55+
56+
sed \
57+
-e "s|@PREFIX@|$out|g" \
58+
-e "s|@INCLUDE@|$out/include|g" \
59+
-e "s|@LIBDIR@|$out/lib|g" \
60+
-e "s|@VERSION@|${finalAttrs.version}|g" \
61+
< ./tree_magic_mini.pc.in > $out/lib/pkgconfig/tree_magic_mini.pc
62+
'';
63+
64+
doCheck = false;
65+
66+
meta = {
67+
description = "Determines the MIME type of a file by traversing a filetype tree.";
68+
homepage = "https://github.com/mbrubeck/tree_magic";
69+
license = lib.licenses.gpl3;
70+
maintainers = builtins.attrValues { inherit (lib.maintainers) r0chd; };
71+
platforms = lib.platforms.all;
72+
};
73+
})

tree_magic_mini.pc.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@PREFIX@
2+
includedir=@INCLUDE@
3+
libdir=@LIBDIR@
4+
5+
Name: tree_magic_mini
6+
URL: https://github.com/mbrubeck/tree_magic
7+
Description: Determines the MIME type of a file by traversing a filetype tree.
8+
Version: @VERSION@
9+
Cflags: -I${includedir}
10+
Libs: -L${libdir} -ltree_magic_mini

0 commit comments

Comments
 (0)