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