Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to figure out current Makefile location in gmake at runtime

I would like to tell (g)make to include some common initializations from a separate file knowing the relative location of the included file with respect to the main Makefile.

However in the manuals I cannot find any built-in variable that would, for example, give you the name of the current Makefile.

For example if I want to include the content of a file in the same directory as the current make file, instead of hard-wiring the location of the include:

# MAIN Makefile : ./scripts/make/TaskA.mk

include ./scripts/make/Common.inc
...

I would like to write something like the following assuming that _MAKEFILE_ contains the TaskA.mk location:

# MAIN Makefile : ./scripts/make/TaskA.mk

MAKEFILE_DIR=$(dirname $(_MAKE_FILE_))

include $(MAKEFILE_DIR)/Common.inc
like image 881
Valentin Ruano Avatar asked Oct 12 '12 15:10

Valentin Ruano


People also ask

How do I get the current path in makefile?

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)) .

Where is the makefile located?

The makefile is a text file that contains the recipe for building your program. It usually resides in the same directory as the sources, and it is usually called Makefile .

What is Abspath in makefile?

That's what abspath does. It creates an absolute path. That means it must be anchored at the root.

What is := in makefile?

Expanded assignment = defines a recursively-expanded variable. := defines a simply-expanded variable.


2 Answers

Doesn't the manual give a recipe based on MAKEFILE_LIST?

Basically

this_makefile := $(lastword $(MAKEFILE_LIST))

before any include directives should do the trick.

like image 75
dmckee --- ex-moderator kitten Avatar answered Sep 18 '22 11:09

dmckee --- ex-moderator kitten


Look at GNU make - Other Special Variables. MAKEFILE_LIST includes all Makefiles read. So, if you take the first one and extract the directory, you're done.

MAKEFILE_DIR=$(dir $(firstword $(MAKEFILE_LIST)))

include $(MAKEFILE_DIR)Common.inc
like image 38
Olaf Dietsche Avatar answered Sep 19 '22 11:09

Olaf Dietsche