tags:

views:

3591

answers:

3

I'm trying to consolidate some build information by using a common makefile. My problem is that I want to use that makefile from different subdirectory levels, which makes the working directory value (pwd) unpredictable. For example:

# Makefile.common
TOP := $(shell pwd)
COMPONENT_DIR := $(TOP)/component
COMPONENT_INC := $(COMPONENT_DIR)/include
COMPONENT_LIB := $(COMPONENT_DIR)/libcomponent.a

If I include Makefile.common from a subdirectory, like so, the $(TOP) directory is incorrect and everything else follows suit:

# other_component/Makefile
include ../Makefile.common
# $(COMPONENT_LIB) is incorrectly other_component/component

What's the best way to get Makefile.common to use its own directory path instead of the more fickle pwd?

+2  A: 

Have you tried doing:

# Makefile.common
TOP ?= $(shell pwd)
COMPONENT_DIR := $(TOP)/component
COMPONENT_INC := $(COMPONENT_DIR)/include
COMPONENT_LIB := $(COMPONENT_DIR)/libcomponent.a

# other_component/Makefile
TOP ?= ..
include ../Makefile.common

Using the ?= construct will keep TOP from being redefined if it is already set. You can set it to the appropriate value based on where you are in the tree when you invoke make. I confess it's been awhile since I've used GNU make so this may not work or may need some tweaks.

tvanfosson
Agreed, that would work. I'm hoping there's a way to do it that doesn't require preparation on the part of the makefile (other than inclusion), but that's certainly a good fallback.
cdleary
+3  A: 

You should be able to use the MAKEFILE_LIST variable, like this:

# This must be the first this in Makefile.common
TOP := $(dir $(lastword $(MAKEFILE_LIST)))

From the documentation:

As make reads various makefiles, including any obtained from the MAKEFILES variable, the command line, the default files, or from include directives, their names will be automatically appended to the MAKEFILE_LIST variable. They are added right before make begins to parse them. This means that if the first thing a makefile does is examine the last word in this variable, it will be the name of the current makefile. Once the current makefile has used include, however, the last word will be the just-included makefile.

JesperE
This worked wonderfully on my dev box, but I realized the target machine was running make 3.79, and the MAKEFILE_LIST feature was added in make 3.80: http://www.mail-archive.com/[email protected]/msg05902.html
cdleary
It seems MAKEFILE_LIST doesn't contain full paths, only relative paths.
kbyrd
+1  A: 

This isn't good style because it adds another dependency (i.e. the realpath binary). Depending on your use case, that may be acceptable.

ROOT_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

If you don't have realpath installed:

$ sudo apt-get install realpath # on debian and derivatives

Bill
I should note that this builds on JesperE's example form above. However, it provides an absolute path to build on top of.
Bill