diff --git a/Makefile b/Makefile index 00c5c076c..9f9ced2af 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,10 @@ PLENARY_DIR ?= ../plenary.nvim MINI_DIR ?= ../mini.nvim PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin LIBDIR ?= $(PREFIX)/lib INCLUDEDIR ?= $(PREFIX)/include +PKGCONFIGDIR ?= $(LIBDIR)/pkgconfig # Compile-time cfg that gates the watcher + git-status fuzz stress test. STRESS_RUSTFLAGS := --cfg stress @@ -50,38 +52,25 @@ build: build-e2e: cargo build --release -p fff-nvim -p fff-c --no-default-features --features zlob +# Build the C library, header, and pkg-config file via cargo-c. build-c-lib: - cargo build --release -p fff-c --no-default-features --features zlob + @command -v cargo-cbuild >/dev/null 2>&1 || { echo "error: cargo-c is required to build the C library. Install it with: cargo install cargo-c" >&2; exit 1; } + cargo cbuild -p fff-c --release --no-default-features --features zlob --prefix=$(PREFIX) --bindir=$(BINDIR) --libdir=$(LIBDIR) --includedir=$(INCLUDEDIR) --pkgconfigdir=$(PKGCONFIGDIR) header: cbindgen --config crates/fff-c/cbindgen.toml --crate fff-c --output crates/fff-c/include/fff.h -# Install the C library and header under $(PREFIX) (default /usr/local). -# Override PREFIX for user-local installs, e.g. `make install PREFIX=$$HOME/.local`. -# DESTDIR is honoured for packagers. +# Install the C library, header, and pkg-config file via cargo-c. install: build-c-lib - install -d $(DESTDIR)$(LIBDIR) - install -d $(DESTDIR)$(INCLUDEDIR) - install -m 0644 crates/fff-c/include/fff.h $(DESTDIR)$(INCLUDEDIR)/fff.h - @if [ -f target/release/libfff_c.dylib ]; then \ - install -m 0755 target/release/libfff_c.dylib $(DESTDIR)$(LIBDIR)/libfff_c.dylib; \ - echo "Installed $(DESTDIR)$(LIBDIR)/libfff_c.dylib"; \ - fi - @if [ -f target/release/libfff_c.so ]; then \ - install -m 0755 target/release/libfff_c.so $(DESTDIR)$(LIBDIR)/libfff_c.so; \ - echo "Installed $(DESTDIR)$(LIBDIR)/libfff_c.so"; \ - fi - @if [ -f target/release/fff_c.dll ]; then \ - install -m 0755 target/release/fff_c.dll $(DESTDIR)$(LIBDIR)/fff_c.dll; \ - echo "Installed $(DESTDIR)$(LIBDIR)/fff_c.dll"; \ - fi - @echo "Installed header $(DESTDIR)$(INCLUDEDIR)/fff.h" + @command -v cargo-cinstall >/dev/null 2>&1 || { echo "error: cargo-c is required to install the C library. Install it with: cargo install cargo-c" >&2; exit 1; } + cargo cinstall -p fff-c --release --no-default-features --features zlob --prefix=$(PREFIX) --bindir=$(BINDIR) --libdir=$(LIBDIR) --includedir=$(INCLUDEDIR) --pkgconfigdir=$(PKGCONFIGDIR) $(if $(DESTDIR),--destdir=$(DESTDIR),) uninstall: - rm -f $(DESTDIR)$(LIBDIR)/libfff_c.dylib - rm -f $(DESTDIR)$(LIBDIR)/libfff_c.so - rm -f $(DESTDIR)$(LIBDIR)/fff_c.dll + rm -f $(DESTDIR)$(LIBDIR)/libfff_c.* + rm -f $(DESTDIR)$(LIBDIR)/fff_c.* + rm -f $(DESTDIR)$(BINDIR)/fff_c.dll rm -f $(DESTDIR)$(INCLUDEDIR)/fff.h + rm -f $(DESTDIR)$(PKGCONFIGDIR)/fff_c.pc @echo "Removed fff-c from $(DESTDIR)$(PREFIX)" test-setup: diff --git a/README.md b/README.md index 96f0a70d8..d367ae766 100644 --- a/README.md +++ b/README.md @@ -664,12 +664,18 @@ Native rust crate that is performing all the search. Stable and well documented. ### Build +Building and installing the C library requires [cargo-c](https://crates.io/crates/cargo-c): + ```bash -# Builds only the C cdylib (fastest): +cargo install cargo-c +``` + +```bash +# Builds the C library, header, and pkg-config file: make build-c-lib -# or directly with cargo: -cargo build --release -p fff-c --features zlob +# or directly with cargo-c: +cargo cbuild -p fff-c --release --no-default-features --features zlob ``` > The `zlob` feature (requires the [Zig](https://ziglang.org) toolchain) switches both @@ -677,12 +683,14 @@ cargo build --release -p fff-c --features zlob > native parallel walker. Without it, the default build uses the pure-Rust > [`ignore`](https://crates.io/crates/ignore) (ripgrep) walker and `globset`. -The output is a `cdylib` (`libfff_c.so` / `libfff_c.dylib` / `fff_c.dll`). The header lives at [`crates/fff-c/include/fff.h`](./crates/fff-c/include/fff.h). +The output includes dynamic libraries (`libfff_c.so` / `libfff_c.dylib` / `fff_c.dll`) and a static library (`libfff_c.a` / `fff_c.lib`). The header lives at [`crates/fff-c/include/fff.h`](./crates/fff-c/include/fff.h). Prebuilt binaries for every version, including every commit on main, are on the [releases page](https://github.com/dmtrKovalenko/fff/releases). The same binaries also ship inside the `@ff-labs/fff-bin-*` npm packages. ### Install +Then install via `make install`: + ```bash # System-wide (needs sudo): sudo make install @@ -694,11 +702,21 @@ make install PREFIX=$HOME/.local make install DESTDIR=/tmp/pkgroot PREFIX=/usr ``` -Drops `libfff_c.{so,dylib,dll}` into `$(PREFIX)/lib` and the header into `$(PREFIX)/include/fff.h`. Remove with `make uninstall`, which honours the same `PREFIX` and `DESTDIR`. +Or directly with `cargo-c`: + +```bash +cargo cinstall -p fff-c --release --no-default-features --features zlob --prefix=/usr/local +``` + +Installs `libfff_c.{so,dylib}` and `libfff_c.a` into `$(PREFIX)/lib` on Unix. On Windows, it installs `fff_c.dll` into `$(BINDIR)` and `fff_c.lib` into `$(PREFIX)/lib`. It also installs the header into `$(PREFIX)/include/fff.h` and `fff_c.pc` into `$(PKGCONFIGDIR)`. Remove with `make uninstall`, which honours the same `PREFIX` and `DESTDIR`. Link against it after install: ```bash +# Using pkg-config: +cc my_app.c $(pkg-config --cflags --libs fff_c) -o my_app + +# Or directly: cc my_app.c -lfff_c -o my_app ``` diff --git a/crates/fff-c/Cargo.toml b/crates/fff-c/Cargo.toml index 75e9e853c..5b3ce5c50 100644 --- a/crates/fff-c/Cargo.toml +++ b/crates/fff-c/Cargo.toml @@ -9,10 +9,15 @@ license = "MIT" workspace = true [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "staticlib"] + +[package.metadata.capi.header] +name = "fff" +subdirectory = false [features] default = ["ripgrep"] # use ripgrep base crates to avoid requiring zig for rust crate +capi = [] ripgrep = ["fff/ripgrep", "fff-query-parser/ripgrep"] zlob = ["fff/zlob", "fff-query-parser/zlob"]