How to get current relative directory of your Makefile?

I have a several Makefiles in app specific directories like this:


Each Makefile includes a .inc file in this path one level up:


Inside I'm setting the destination of where I want the binaries to be placed when built. I want all binaries to be in their respective app_type path:


I tried using $(CURDIR), like this:

OUTPUT_PATH = /project1/bin/$(CURDIR)

but instead I got the binaries buried in the entire path name like this: (notice the redundancy)


What can I do to get the "current directory" of execution so that I can know just the app_typeX in order to put the binaries in their respective types folder?


  • The shell function.

    You can use shell function: current_dir = $(shell pwd). Or shell in combination with notdir, if you need not absolute path: current_dir = $(notdir $(shell pwd)).


    Given solution only works when you are running make from the Makefile's current directory.
    As @Flimm noted:

    Note that this returns the current working directory, not the parent directory of the Makefile.
    For example, if you run cd /; make -f /home/username/project/Makefile, the current_dir variable will be /, not /home/username/project/.

    Code below will work for Makefiles invoked from any directory:

    mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
    current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))