Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining directory makefile resides in

What is the correct way to get the directory where the currently executing makefile resides?

I'm currently using export ROOT=$(realpath $(dir $(lastword $(MAKEFILE_LIST)))) and am running into some problems where when running make with the exact same options will result in different values for ROOT. About 90%of the time it has the correct value, but in the remaining 10% there are a number of invalid paths.

like image 513
Nick Avatar asked Dec 29 '10 14:12

Nick


Video Answer


1 Answers

realpath,abspath,lastword and a couple of more functions were only introduced in GNU Make 3.81 [See ref]. Now you can get the current filename in older versions using words and word:

THIS_MAKEFILE:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))

But I am not sure of a workaround for realpath without going to the shell. e.g. this works with make v3.80:

THIS_MAKEFILE_PATH:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
THIS_DIR:=$(shell cd $(dir $(THIS_MAKEFILE_PATH));pwd)
THIS_MAKEFILE:=$(notdir $(THIS_MAKEFILE_PATH))

all:
        @echo "This makefile is $(THIS_MAKEFILE) in the $(THIS_DIR) directory"

Which gives

$ make -f ../M
This makefile is M in the /home/sandipb directory

Ref: http://cvs.savannah.gnu.org/viewvc/make/NEWS?revision=2.93&root=make&view=markup

like image 73
Sandip Bhattacharya Avatar answered Oct 01 '22 01:10

Sandip Bhattacharya