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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
This commit is contained in:
Fabio Scotto di Santolo
2026-08-26 15:46:05 +02:00
parent 4e56e3e37c
commit 25b328ba79
2 changed files with 30 additions and 4 deletions

View File

@@ -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)