From 25b328ba79de4ddb3f89f76c29463deb990f92f5 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 26 Aug 2026 15:46:05 +0200 Subject: [PATCH] Add install and uninstall targets install copies the binary to $(BINDIR), defaulting to /usr/local/bin. PREFIX, BINDIR and INSTALL are overridable, and DESTDIR is honoured so a packager can stage into a temporary root; it is deliberately left undefaulted rather than set to empty. uninstall removes what install placed, and is idempotent. Both use install(1) rather than cp, so the mode is set explicitly and the target directory is created in the same step. The flags used, -d and -m, are common to GNU and BSD install, which SPEC.md section 19 asks for. Verified: staged install under DESTDIR, PREFIX and BINDIR overrides, uninstall in both plain and staged form, uninstall with nothing to remove, and install from a clean tree rebuilding first. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx --- Makefile | 19 +++++++++++++++++-- README.md | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 8e365b9..bdc29bf 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,15 @@ LDFLAGS := LDLIBS := BUILD_DIR := build -TARGET := $(BUILD_DIR)/fsnap +NAME := fsnap +TARGET := $(BUILD_DIR)/$(NAME) + +# ?= so a caller can override from the environment or the command line. +# DESTDIR is deliberately not defaulted: it is empty for a real install and set +# by a packager staging into a temporary root. +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin +INSTALL ?= install SOURCES := \ src/main.c \ @@ -35,7 +43,7 @@ SOURCES := \ OBJECTS := $(SOURCES:%.c=$(BUILD_DIR)/%.o) DEPENDENCIES := $(OBJECTS:.o=.d) -.PHONY: all clean run +.PHONY: all clean run install uninstall all: $(TARGET) @@ -49,6 +57,13 @@ $(BUILD_DIR)/%.o: %.c run: $(TARGET) ./$(TARGET) +install: $(TARGET) + $(INSTALL) -d $(DESTDIR)$(BINDIR) + $(INSTALL) -m 755 $(TARGET) $(DESTDIR)$(BINDIR)/$(NAME) + +uninstall: + rm -f $(DESTDIR)$(BINDIR)/$(NAME) + clean: rm -rf $(BUILD_DIR) diff --git a/README.md b/README.md index 44be750..90ff924 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,18 @@ make # produces build/fsnap make clean ``` +`make install` copies the binary to `$(BINDIR)`, which defaults to +`/usr/local/bin`; `make uninstall` removes it again. Both honour `DESTDIR` for +staged installs: + +```sh +make install PREFIX=~/.local # ~/.local/bin/fsnap +make install DESTDIR=/tmp/stage # /tmp/stage/usr/local/bin/fsnap +sudo make install # /usr/local/bin/fsnap +``` + +`PREFIX`, `BINDIR` and `INSTALL` can all be overridden. + The sources declare no feature test macros of their own; the `Makefile` sets `-D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE` for the whole build. Everything used behind them — `getline`, `realpath(path, NULL)`, `mkstemp`, `readlink`, @@ -48,7 +60,7 @@ used behind them — `getline`, `realpath(path, NULL)`, `mkstemp`, `readlink`, `dirent.d_type`, used as a fast path only, with an `lstat` fallback for `DT_UNKNOWN` as the spec requires. -Only Linux/glibc has been tested. There is no `install` target. +Only Linux/glibc has been tested. ## Usage @@ -206,7 +218,6 @@ Rough order of intent, no timeline: rather than collapsing them ([SPEC.md](SPEC.md) §18). - [ ] Honour `TMPDIR` instead of hardcoding `/tmp`. - [ ] Add a test suite and wire it into the `Makefile`. -- [ ] Add an `install` target. ## License