<p>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 (<code>pwd</code>) unpredictable. For example:</p>

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

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

<pre><code># other_component/Makefile
include ../Makefile.common
# $(COMPONENT_LIB) is incorrectly other_component/component
</code></pre>

<p>What's the best way to get <code>Makefile.common</code> to use <em>its own</em> directory path instead of the more fickle <code>pwd</code>?</p>

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.