Get path to file that is included into a Makefile

Viewed 1052

I have the following situation: I want several independent Makefiles to include a single file with all the rules (via include ... directive). So far, so good. However, that central rules file does need to use some other files next to it (shell-scripts and such). To name these other files, it needs to know its own path relative to the make invocation.

What would work is this:

# in the Makefiles
toolsPath := ../../generalStuff
include $(toolsPath)/generalRules.mk

# in the generalRules.mk
foo: ./$(toolsPath)/script
    ./$(toolsPath)/script foo

Yet, while this would work, it relies on the including Makefiles to honestly tell the general file its own path. And I'm wondering whether there is a way to avoid setting a toolsPath variable.

Is it possible to replace the $(toolsPath) in the included file with something that relies on make itself, rather than the including Makefiles?

I would like to be able to simply write this into the Makefiles:

include ../../generalStuff/generalRules.mk
1 Answers

The MAKEFILE_LIST variable (see the example in the link) can be used to find this information. Just be sure you obtain it before another include hides it.

Related