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
This commit is contained in:
154
Makefile
154
Makefile
@@ -1,20 +1,77 @@
|
||||
# Not file targets.
|
||||
.PHONY: help install install-scripts install-conf install-systemd uninstall
|
||||
### 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 ###
|
||||
SRCS_SCRIPTS = $(filter-out %cron_mail, $(wildcard usr/local/sbin/*))
|
||||
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 etc/restic/*)))
|
||||
SRCS_SYSTEMD = $(wildcard etc/systemd/system/*)
|
||||
SRCS_CONF = $(sort $(patsubst %.template, %, $(wildcard $(DIR_CONF)/*)))
|
||||
SRCS_SYSTEMD = $(wildcard $(DIR_SYSTEMD)/*)
|
||||
|
||||
# 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
|
||||
DEST_SCRIPTS = $(PREFIX)/usr/local/sbin
|
||||
DEST_CONF = $(PREFIX)/etc/restic
|
||||
DEST_SYSTEMD = $(PREFIX)/etc/systemd/system
|
||||
|
||||
INSTALLED_FILES = $(addprefix $(PREFIX)/, $(SRCS_SCRIPTS) $(SRCS_CONF) $(SRCS_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.
|
||||
@@ -24,33 +81,9 @@ all: install
|
||||
help:
|
||||
@egrep "#\starget:" [Mm]akefile | sed 's/\s-\s/\t\t\t/' | cut -d " " -f3- | sort -d
|
||||
|
||||
# target: install - Install all files
|
||||
install: install-scripts install-conf install-systemd
|
||||
|
||||
|
||||
# target: install-scripts - Install executables.
|
||||
install-scripts:
|
||||
install -d $(DEST_SCRIPTS)
|
||||
install -m 0744 $(filter-out %/resticw, $(SRCS_SCRIPTS)) $(DEST_SCRIPTS)
|
||||
install -m 0755 usr/local/sbin/resticw $(DEST_SCRIPTS)
|
||||
|
||||
# Copy templates to new files with restricted permissions.
|
||||
# Why? Because the non-template files are git-ignored to prevent that someone who clones or forks this repo checks in their sensitive data like the B2 password!
|
||||
etc/restic/_global.env etc/restic/default.env etc/restic/pw.txt:
|
||||
install -m 0600 $@.template $@
|
||||
|
||||
# target: install-conf - Install restic configuration files.
|
||||
# will create these files locally only if they don't already exist
|
||||
# `|` means that dependencies are order-only, i.e. only created if they don't already exist.
|
||||
install-conf: | $(SRCS_CONF)
|
||||
install -d $(DEST_CONF)
|
||||
install -b -m 0600 $(SRCS_CONF) $(DEST_CONF)
|
||||
$(RM) etc/restic/_global.env etc/restic/default.env etc/restic/pw.txt
|
||||
|
||||
# target: install-systemd - Install systemd timer and service files.
|
||||
install-systemd:
|
||||
install -d $(DEST_SYSTEMD)
|
||||
install -m 0644 $(SRCS_SYSTEMD) $(DEST_SYSTEMD)
|
||||
# target: clean - Remove build files.
|
||||
clean:
|
||||
$(RM) -r $(BUILD_DIR)
|
||||
|
||||
# target: uninstall - Uninstall ALL files from the install targets.
|
||||
uninstall:
|
||||
@@ -58,3 +91,46 @@ uninstall:
|
||||
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 $< $@
|
||||
|
||||
Reference in New Issue
Block a user