How to determine the current location of a makefile in gmake at run time

I would say (g) to include some common initializations from a separate file, knowing the relative location of the included file in relation to the main Makefile.

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

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

# MAIN Makefile : ./scripts/make/TaskA.mk include ./scripts/make/Common.inc ... 

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

 # MAIN Makefile : ./scripts/make/TaskA.mk MAKEFILE_DIR=$(dirname $(_MAKE_FILE_)) include $(MAKEFILE_DIR)/Common.inc 
+4
source share
2 answers

Does the manual contain a recipe based on MAKEFILE_LIST ?

Mostly

 this_makefile := $(lastword $(MAKEFILE_LIST)) 

before any include directives should do the trick.

+6
source

See GNU make - Other Special Variables . MAKEFILE_LIST includes all read Makefiles. 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 
+4
source

Source: https://habr.com/ru/post/1439416/


All Articles