Makefile: path to root dir
β’ 117 words β’ 1 min β’ updated
β οΈ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.
Problem statement: Given a Makefile within ~/git/scaffolding/Makefile,
and a command that needs to run from the scaffolding/ directory, create an
all target that works from any directory.
Solution:
make
# The directory wherein the Makefile resides.
ROOT_DIR := $(patsubst %/,%,$(dir $(realpath $(lastword $(MAKEFILE_LIST)))))
all:
@echo $(ROOT_DIR)
kickstart $(ROOT_DIR)/app
.PHONY: allExplanation:
- The
echois used only for debugging, therefore it should be removed in prod. - The
kickstartcommand will properly run having~/git/scaffoldingas$PWDwhether you invoke it from~/git/scaffoldingor from~/git(viamake -C). pathsubstis needed to remove the trailing slash (/) from the directory, so that$(ROOT_DIR)/does not yield a double slash, which works but it is ugly.
Source (adapted): https://stackoverflow.com/questions/18136918/how-to-get-current-relative-directory-of-your-makefile