\section{Introduction and usage}% ===> this file was generated automatically by noweave --- better not edit it

Sometimes we want to recursively descend into subdirectories making a specific 
target in each subdirectory.
The subdirectories must be listed in the variable {\Tt{}SUBDIR\nwendquote}, which holds 
a space-separated list of directory names.
Then each subdirectory may in turn hold a new set of subdirectories to descend 
into.
We note that the subdirectories will be built in depth-first search order 
(unless we use parallel execution).

By default, we will recurse into the subdirectories in {\Tt{}SUBDIR\nwendquote} for any goals 
passed on the command line.
This behaviour can be overridden by changing the value of {\Tt{}SUBDIR{\_}GOALS\nwendquote} from 
its default:
\nwfilename{subdir.mk.nw}\nwbegincode{1}\sublabel{NW1r6b01-29jjeg-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1r6b01-29jjeg-1}}}\moddef{variables~{\nwtagstyle{}\subpageref{NW1r6b01-29jjeg-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1r6b01-2mZUpH-1}}\nwprevnextdefs{\relax}{NW1r6b01-29jjeg-2}\nwenddeflinemarkup
SUBDIR_GOALS?=$\{MAKECMDGOALS\}
\nwalsodefined{\\{NW1r6b01-29jjeg-2}}\nwused{\\{NW1r6b01-2mZUpH-1}}\nwendcode{}\nwbegindocs{2}Like this we can ensure that we only recurse into the subdirectories only for 
a subset of desired targets.

Any variables that should be passed down to sub-makes should be exported using 
the {\Tt{}export\nwendquote} directive of make.

\section{Implementation}

The structure of the file is that of most include files.
We want to ensure that it is not included more than once.
Furthermore, we do not want to do anything unless the {\Tt{}SUBDIR\nwendquote} variable, 
containing the space-separated list of subdirectories, exists.
\nwenddocs{}\nwbegincode{3}\sublabel{NW1r6b01-2mZUpH-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1r6b01-2mZUpH-1}}}\moddef{subdir.mk~{\nwtagstyle{}\subpageref{NW1r6b01-2mZUpH-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
ifndef SUBDIR_MK
SUBDIR_MK=true

\LA{}variables~{\nwtagstyle{}\subpageref{NW1r6b01-29jjeg-1}}\RA{}

ifdef SUBDIR
\LA{}let the recipe for each subdirectory recurse into it~{\nwtagstyle{}\subpageref{NW1r6b01-3ZL9cr-1}}\RA{}
endif

ifneq ($\{SUBDIR_GOALS\},)
\LA{}add subdirectories as prerequisites for the goals~{\nwtagstyle{}\subpageref{NW1r6b01-16y7WG-1}}\RA{}
endif

endif
\nwnotused{subdir.mk}\nwendcode{}\nwbegindocs{4}\nwdocspar

The thing we want to do is to build the given goals ({\Tt{}SUBDIR{\_}GOALS\nwendquote}), \ie the 
targets specified on the command-line by default, in all subdirectories listed 
in {\Tt{}SUBDIR\nwendquote}.
For each directory, we specify a recipe which runs make in the subdirectory 
with the appropriate goals.
\nwenddocs{}\nwbegincode{5}\sublabel{NW1r6b01-3ZL9cr-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1r6b01-3ZL9cr-1}}}\moddef{let the recipe for each subdirectory recurse into it~{\nwtagstyle{}\subpageref{NW1r6b01-3ZL9cr-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1r6b01-2mZUpH-1}}\nwenddeflinemarkup
$\{SUBDIR\}::
  $\{MAKE\} -C $@ $\{actual_goals\}
\nwused{\\{NW1r6b01-2mZUpH-1}}\nwendcode{}\nwbegindocs{6}\nwdocspar

Those {\Tt{}actual{\_}goals\nwendquote} is the intersection of the goals specified on the 
command-line and those specified in {\Tt{}SUBDIR{\_}GOALS\nwendquote}.
\nwenddocs{}\nwbegincode{7}\sublabel{NW1r6b01-29jjeg-2}\nwmargintag{{\nwtagstyle{}\subpageref{NW1r6b01-29jjeg-2}}}\moddef{variables~{\nwtagstyle{}\subpageref{NW1r6b01-29jjeg-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1r6b01-2mZUpH-1}}\nwprevnextdefs{NW1r6b01-29jjeg-1}{\relax}\nwenddeflinemarkup
actual_goals=$(sort $(filter $\{SUBDIR_GOALS\},$\{MAKECMDGOALS\}))
\nwused{\\{NW1r6b01-2mZUpH-1}}\nwendcode{}\nwbegindocs{8}To ensure these recipes are run we need to ensure that they are prerequisites
to those goals.
\nwenddocs{}\nwbegincode{9}\sublabel{NW1r6b01-16y7WG-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1r6b01-16y7WG-1}}}\moddef{add subdirectories as prerequisites for the goals~{\nwtagstyle{}\subpageref{NW1r6b01-16y7WG-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1r6b01-2mZUpH-1}}\nwprevnextdefs{\relax}{NW1r6b01-16y7WG-2}\nwenddeflinemarkup
ifneq ($\{actual_goals\},)
.PHONY: $\{actual_goals\}
$\{actual_goals\}: $\{SUBDIR\}
endif
\nwalsodefined{\\{NW1r6b01-16y7WG-2}}\nwused{\\{NW1r6b01-2mZUpH-1}}\nwendcode{}\nwbegindocs{10}\nwdocspar

Finally, we must consider the case of the default goal, \ie no goal is 
specified on the command line.
In this case we simply check if the default goal ({\Tt{}.DEFAULT{\_}GOAL\nwendquote}) is among 
the desired goals ({\Tt{}SUBDIR{\_}GOALS\nwendquote}).
\nwenddocs{}\nwbegincode{11}\sublabel{NW1r6b01-16y7WG-2}\nwmargintag{{\nwtagstyle{}\subpageref{NW1r6b01-16y7WG-2}}}\moddef{add subdirectories as prerequisites for the goals~{\nwtagstyle{}\subpageref{NW1r6b01-16y7WG-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1r6b01-2mZUpH-1}}\nwprevnextdefs{NW1r6b01-16y7WG-1}{\relax}\nwenddeflinemarkup
ifneq ($(filter $\{.DEFAULT_GOAL\},$\{SUBDIR_GOALS\}),)
.PHONY: $\{.DEFAULT_GOAL\}
$\{.DEFAULT_GOAL\}: $\{SUBDIR\}
endif
\nwused{\\{NW1r6b01-2mZUpH-1}}\nwendcode{}

\nwixlogsorted{c}{{add subdirectories as prerequisites for the goals}{NW1r6b01-16y7WG-1}{\nwixu{NW1r6b01-2mZUpH-1}\nwixd{NW1r6b01-16y7WG-1}\nwixd{NW1r6b01-16y7WG-2}}}%
\nwixlogsorted{c}{{let the recipe for each subdirectory recurse into it}{NW1r6b01-3ZL9cr-1}{\nwixu{NW1r6b01-2mZUpH-1}\nwixd{NW1r6b01-3ZL9cr-1}}}%
\nwixlogsorted{c}{{subdir.mk}{NW1r6b01-2mZUpH-1}{\nwixd{NW1r6b01-2mZUpH-1}}}%
\nwixlogsorted{c}{{variables}{NW1r6b01-29jjeg-1}{\nwixd{NW1r6b01-29jjeg-1}\nwixu{NW1r6b01-2mZUpH-1}\nwixd{NW1r6b01-29jjeg-2}}}%
\nwbegindocs{12}We note that if we specify no goals on the command line, then 
{\Tt{}actual{\_}goals\nwendquote} will be the empty string.
This means that we execute the default goal in each subdirectory recursively: 
\eg if the current default goal is {\Tt{}all\nwendquote}, but its {\Tt{}clean\nwendquote} in a 
subdirectory, then we will execute {\Tt{}all\nwendquote} here but {\Tt{}clean\nwendquote} there --- not 
{\Tt{}all\nwendquote} in both places.
\nwenddocs{}
