Skip to content Skip to sidebar Skip to footer

Make Include In Makefiles Be Relative To The File's Location

Directly related to this question. How can I make the include directive in makefiles behave relatively to the location of the current script? Assume that the current path is arbitr

Solution 1:

You can get the name of the makefile being currently processed from MAKEFILE_LIST builtin variable.

Given that the current makefile is the last one that has been included (in other words you didn't use another include directive since the beginning of the current script), the path to the script itself would be:

SELF_DIR := $(dir $(lastword$(MAKEFILE_LIST)))

Now you are able to include a script in the same directory as such (note an absence of slash, it has already been added by $(dir ...)):

include$(SELF_DIR)another.mk

Note: In GNU Make 3.80 there was no lastword builtin function. In that case you may implement it as follows replacing $(lastword ...) with $(call lastword,...):

lastword = $(if$(firstword $1),$(word $(words $1),$1))

Solution 2:

Is there a builtin variable with the current makefile's name?

Yes, there is, use ${CURDIR}. This is the directory where top-level Makefile is located, so you don't need to strip anything from it.

http://www.gnu.org/software/make/manual/make.html#Recursion

Solution 3:

I find that relative paths work (GNUMake 3.81), but if they don't for you, try this:

include$(abspath ../whatever)

Post a Comment for "Make Include In Makefiles Be Relative To The File's Location"