Cached Prerequisites from Multiple Locations – First Silicon Designs

Cached Prerequisites from Multiple Locations

Using GNU Make, combine a local cache for prerequisites with multiple source directories.

Multiple Prerequisite Locations

Using multiple source locations for target prerequisites may not be that interesting or difficult for you, but when you want or need to cache these files locally, Make requires a bit more cleverness. We’ll build our example starting with the simple case of two different source locations for prerequisite files. In fact, it’s a common requirement for included header files, libraries, and other similar situations. We can simply use different variables to hold the different paths. The choice of variable names in the following Make file snippet hints at where we’re leading you:

# Define 2 places where prerequisite files may be found
DEFAULT_PATH  := ./srcloc1
OVERRIDE_PATH := ./srcloc2

Now let’s suppose we want a single set of files, selected from OVERRIDE_PATH if present there but otherwise from DEFAULT_PATH. Let’s further suppose we want all the TCL files existing in either location. Make’s built-in file functions are quite adequate for the task:

# 'wildcard' is basically a directory list function
# 'notdir' strips all the text up to and including the rightmost '/'
srcfiles_override = $(notdir $(wildcard $(OVERRIDE_PATH)/*.tcl))
srcfiles_default  = $(filter-out $(srcfiles_override),\
                      $(notdir $(wildcard $(DEFAULT_PATH)/*.tcl)))

Using both sets of files as prerequisites requires reconstructing the full paths for the two file-only list variables. Again, this is rather straightforward Make coding:

src_prereqs  = $(addprefix $(DEFAULT_PATH)/,$(srcfiles_default))
src_prereqs += $(addprefix $(OVERRIDE_PATH)/,$(srcfiles_override))

maintarget : $(src_prereqs)
	.
	.
	.