Search code examples
makefilegnu-make

Absolute path of current Makefile


In GNU Make, given a makefile in some absolute path /path/Makefile, if Makefile is the current makefile being processed, how can I recover the absolute path of Makefile (e.g., /path) regardless of whether such Makefile is being processed and/or included?

The most similar makefile variable I have found is CURDIR, but this is like the PWD of the current makefile invocation as far as I understand.


Solution

  • Expanding the comment by G.M., I would suggest that you combine $(firstword) or $(lastword) with either $(abspath) or $(realpath), e.g.:

    all:
            @echo "$(abspath $(lastword $(MAKEFILE_LIST)))"
            @echo "$(realpath $(firstword $(MAKEFILE_LIST)))"