---
title: "Makefile: path to root dir"
url: https://perrotta.dev/2024/10/makefile-path-to-root-dir/
last_updated: 2025-09-05
---


**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: all
```

**Explanation**:

- The `echo` is used only for debugging, therefore it should be removed in prod.
- The `kickstart` command will properly run having `~/git/scaffolding` as `$PWD`
  whether you invoke it from `~/git/scaffolding` or from `~/git` (via `make
  -C`).
- `pathsubst` is 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

