52

I'd like to get the current directory during a GNUmake file run put into a make variable.

What is the syntax to do this? Something like this?

DIR := $(PWD)
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
WilliamKF
  • 41,123
  • 68
  • 193
  • 295

2 Answers2

102

Um, no, $PWD is sometimes defined in your environment and thus inherited by make, but oftentimes not. You need $CURDIR.

DIR := ${CURDIR}
bobbogo
  • 1,832
  • 1
  • 16
  • 5
  • 3
    Doc: https://www.gnu.org/software/make/manual/html_node/Recursion.html#Recursion Note: not POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html In particular, `PWD` does not get updated on recursive invocations of `make -C`. – Ciro Santilli OurBigBook.com Aug 07 '16 at 09:16
  • 3
    For reference: ***this is Windows-friendly*** and works in cmd, git bash, powershell, conemu... Compared to `$$PWD` which did not work for me anywhere (Context: our makefile was doing `docker run -v $$PWD:/opt ...` and docker was complaining with a non-descriptive `Mount denied: The source path "C:/Users/.../somedir;C" doesn't exist and is not known to Docker`) – jakub.g Aug 01 '18 at 17:25
  • 1
    Note: this does not work for included makefiles. It will report the path of the top-level makefile instead. – Xeverous May 29 '19 at 09:59
7

If you have one makefile including another in a different directory, PWD and CURDIR aren't updated for the child makefile. If that second makefile needs to know where it is, the following will tell you.

$(dir $(realpath $(lastword $(MAKEFILE_LIST))))
Swiss Frank
  • 1,985
  • 15
  • 33