Files
restic-automatic-backup-sch…/Makefile
Erik Westrup 9760cd05ec Truly support custom PREFIX= install
* Before, doing `$ PREFIX=/usr/local make install` would install files to`/usr/local/usr/local..` which is wrong
* With this PR, files will be installed to the expected location e.g. `/usr/local/etc/restic`
* `Makefile` almost completely rewritten
   * As e.g. `default.env` would source `_global.env`, `default.env` must be edited to find the right location of `_global.env` depending on what `$PREFIX` was set to.
   * see documented build stages in the `Makefile` itself.
   *  Made sure that the rules are correct so that only modifed files are installed, not all at once unnecessarily like before.
* A sub-goal was that the [PKGBUILD](https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=restic-systemd-automatic-backup#n20) for Arch should not need to do any custom install configuration, to keep everything easier to maintain. `$ make install` should work out of the box for Arch.
* Additionally added the `-b` flag to `install(1)` that makes a backup of existing `etc/restic/*` files before installing a newer version.

Fixes #49
2022-02-03 20:52:00 +01:00

137 lines
4.6 KiB
Makefile

### Notes ###
# This build process is done in three stages:
# 1. copy source files to the local build directory.
# 2. replace the string "$INSTALL_PREFIX" with the value of $PREFIX
# 3. copy files from the build directory to the target directory.
#
# Why this dance?
# * To fully support that a user can install this project to a custom path e.g. $(PREFIX=/usr/local make install),
# we need to modify the files that refer to other files on disk. We do this by having a placeholder "$INSTALL_PREFIX"
# that is substituted with the value of $PREFIX when installed
# * We don't want to modify the files that are controlled by git, thus let's copy them to a build directory and then modify.
### Non-file targets ###
.PHONY: help install install-scripts install-conf install-systemd clean uninstall
### Macros ###
NOW := $(shell date +%Y-%m-%d_%H:%M:%S)
# Source: https://stackoverflow.com/a/14777895/265508
ifeq ($(OS),Windows_NT)
CUR_OS := Windows
else
CUR_OS := $(shell uname)
endif
# GNU install and macOS install have incompatible command line arguments.
ifeq ($(CUR_OS),Darwin)
BAK_SUFFIX = -B .$(NOW).bak
else
BAK_SUFFIX = --suffix=.$(NOW).bak
endif
# Create parent directories of a file, if not existing.
# Reference: https://stackoverflow.com/a/25574592/265508
MKDIR_PARENTS=sh -c '\
dir=$$(dirname $$1); \
test -d $$dir || mkdir -p $$dir \
' MKDIR_PARENTS
# Source directories.
DIR_SCRIPTS = sbin
DIR_CONF = etc/restic
DIR_SYSTEMD = usr/lib/systemd/system
# Source files.
SRCS_SCRIPTS = $(filter-out %cron_mail, $(wildcard $(DIR_SCRIPTS)/*))
# $(sort) remove duplicates that comes from running make install >1 times.
SRCS_CONF = $(sort $(patsubst %.template, %, $(wildcard $(DIR_CONF)/*)))
SRCS_SYSTEMD = $(wildcard $(DIR_SYSTEMD)/*)
# Local build directory. Sources will be copied here, modified and then installed from this directory.
BUILD_DIR := build
BUILD_DIR_SCRIPTS = $(BUILD_DIR)/$(DIR_SCRIPTS)
BUILD_DIR_CONF = $(BUILD_DIR)/$(DIR_CONF)
BUILD_DIR_SYSTEMD = $(BUILD_DIR)/$(DIR_SYSTEMD)
# Sources copied to build directory.
BUILD_SRCS_SCRIPTS = $(addprefix $(BUILD_DIR)/, $(SRCS_SCRIPTS))
BUILD_SRCS_CONF = $(addprefix $(BUILD_DIR)/, $(SRCS_CONF))
BUILD_SRCS_SYSTEMD = $(addprefix $(BUILD_DIR)/, $(SRCS_SYSTEMD))
# Destination directories
DEST_DIR_SCRIPTS = $(PREFIX)/$(DIR_SCRIPTS)
DEST_DIR_CONF = $(PREFIX)/$(DIR_CONF)
DEST_DIR_SYSTEMD = $(PREFIX)/$(DIR_SYSTEMD)
# Destination files.
DEST_SCRIPTS = $(addprefix $(PREFIX)/, $(SRCS_SCRIPTS))
DEST_CONF = $(addprefix $(PREFIX)/, $(SRCS_CONF))
DEST_SYSTEMD = $(addprefix $(PREFIX)/, $(SRCS_SYSTEMD))
INSTALLED_FILES = $(DEST_SCRIPTS) $(DEST_CONF) $(DEST_SYSTEMD)
### Targets ###
# target: all - Default target.
all: install
# target: help - Display all targets.
help:
@egrep "#\starget:" [Mm]akefile | sed 's/\s-\s/\t\t\t/' | cut -d " " -f3- | sort -d
# target: clean - Remove build files.
clean:
$(RM) -r $(BUILD_DIR)
# target: uninstall - Uninstall ALL files from the install targets.
uninstall:
@for file in $(INSTALLED_FILES); do \
echo $(RM) $$file; \
$(RM) $$file; \
done
# To change the installation root path, set the PREFIX variable in your shell's environment, like:
# $ PREFIX=/usr/local make install
# $ PREFIX=/tmp/test make install
# target: install - Install all files
install: install-scripts install-conf install-systemd
# Install targets - add build sources to prereqa as well, so that build dir is re-created if deleted (expected behaviour).
# target: install-scripts - Install executables.
install-scripts: $(DEST_SCRIPTS)
# target: install-conf - Install restic configuration files.
install-conf: $(DEST_CONF) $(BUILD_SRCS_CONF)
# target: install-systemd - Install systemd timer and service files.
install-systemd: $(DEST_SYSTEMD)
# Copies sources to build directory & replace "$INSTALL_PREFIX"
# dir= line needs to be in the same subshell to use shared envvars. Reference: https://stackoverflow.com/a/36419671/265508
$(BUILD_DIR)/% : %
${MKDIR_PARENTS} $@
cp $< $@
sed -i.bak -e "s|\$$INSTALL_PREFIX|$$PREFIX|g" $@; rm $@.bak
# For the destination files to be built, build-files must exist.
#$(DEST_SCRIPTS): $(BUILD_SRCS_SCRIPTS)
#$(DEST_CONF): $(BUILD_SRCS_CONF)
#$(DEST_SYSTEMD): $(BUILD_SRCS_SYSTEMD)
# Install destination script files.
$(DEST_DIR_SCRIPTS)/%: $(BUILD_DIR_SCRIPTS)/%
${MKDIR_PARENTS} $@
install -m 0744 $< $@
# Install destination conf files. Additionally backup existing files.
$(DEST_DIR_CONF)/%: $(BUILD_DIR_CONF)/%
${MKDIR_PARENTS} $@
install -m 0600 -b $(BAK_SUFFIX) $< $@
# Install destination script files.
$(DEST_DIR_SYSTEMD)/%: $(BUILD_DIR_SYSTEMD)/%
${MKDIR_PARENTS} $@
install -m 0644 $< $@