1 subdirmk - assistance for non-recursive use of make
2 ===================================================
7 Peter Miller's 1997 essay _Recursive Make Considered Harmful_
8 persuasively argues that it is better to arrange to have a single
9 make invocation with the project's complete dependency tree, rather
10 than the currently conventional `$(MAKE) -C subdirectory' approach.
12 However, actually writing a project's build system in a non-recursive
13 style is not very ergonomic. The main difficulties are:
14 - constantly having to write out long file and directory names
15 - the lack of a per-directory make variable namespace means
16 long make variables (or namespace clashes)
17 - it is difficult to arrange that one can cd to a subdirectory
18 and say `make all' and have something reasonable happen
19 (to wit, build an appropriate subset)
21 `subdirmk' is an attempt to solve these problems (and it also slightly
22 alleviates some of the boilerplate needed to support out-of-tree
28 The developer is expected to write a makefile fragment, in each
29 relevant subdirectory, called `Subdir.sd.mk'.
31 These fragments may contain ordinary make language. Unqualified
32 filenames are relative to the build toplevel, and all commands all run
35 However, the sigil & is treated specially. By and large, it refers to
36 `the build directory corresponding to this .sd.mk file', etc.
37 There are a variety of convenient constructions.
39 The result is that to a large extent, the Subdir.sd.mk has an easy way
40 to namespace its "local" make variables, and an easy way to refer to
41 its "local" filenames (and filenames in general).
43 The Subdir.sd.mk's are filtered, fed through autoconf in the usual way
44 (for @..@-substitutions) and included by one autogenerated toplevel
47 So all of the input is combined and passed to one make invocation.
48 (A corollary is that there is no enforcement of the namespacing:
49 discipline is required to prefix relevant variable names with &, etc.)
51 Each subdirectory is also provided with an autogenerated `Makefile'
52 which exists purely to capture ordinary make invocations and arrange
53 for something suitable to happen.
55 Where there are dependencies between subdirectories, each Subdir.sd.mk
56 can simply refer to files in other subdirectories directly.
58 Invocation, "recursive" per-directory targets
59 ---------------------------------------------
61 Arrangements are made so that when you run `make foo' in a
62 subdirectory, it is like running the whole toplevel makefile, from the
63 toplevel, as `make subdir/foo'. If `subdir/foo' is a file that might
64 be built, that builds it.
66 But `foo' can also be a conventional target like `all'.
68 Each subdirectory has its own `all' target. For example a
69 subdirectory `src' has a target `src/all'. The rules for these are
70 automatically generated from the settings of the per-directory
71 &TARGETS variables. &TARGETS is magic in this way. (In
72 src/Subdir.sd.mk, &TARGETS of course refers to a make variable called
75 The `all' target in a parent directory is taken to imply the `all'
76 targets in all of its subdirectories, recursively. And in the
77 autogenerated stub Makefiles, `all' is the default target. So if you
78 just type `make' in the toplevel, you are asking for `&all'
79 (<subdir>/all) for every directory in the project.
81 In a parallel build, the rules for all these various subdirectory
82 targets may be in run in parallel: there is only one `make' invocation
83 at a time. There is no sequencing between subdirectories, only been
84 individual targets (as specified according to their dependencies).
86 You can define other per-directory recursive targets too: simply
87 mention (usually, by setting) the variable &TARGETS_zonk, or whatever.
88 This will create a src/zonk target (for appropriate value of src/).
89 Unlike `all', these other targets only exist in areas of the project
90 where at least something mentions them. So for example, if
91 &TARGETS_zonk is mentioned in src but not lib, `make zonk' in
92 lib will fail. If you want to make a target exist everywhere,
93 mention its name in Perdir.sd.mk (see below).
95 Perdir.sd.mk, inclusion
96 -----------------------
98 The file Perdir.sd.mk in the toplevel of the source is automatically
99 processed after each individual directory's Subdir.sd.mk, and the
100 &-substituted contents therefore appear once for each subdirectory.
102 This lets you do per-directory boilerplate. Some useful boilerplate
103 is already provided in subdirmk, for you to reference like this:
104 &:include subdirmk/cdeps.sd.mk
105 &:include subdirmk/clean.sd.mk
106 For example you could put that in Perdir.sd.mk.
111 If want to set global variables, such as CC, that should only be done
112 once. You can put them in your top-level Subdir.sd.mk, or a separate
113 file you `include' and declare using SUBDIRMK_MAKEFILES.
115 If you need different settings of variables like CC for different
116 subdirectories, you should probably do that with target-specific
117 variable settings. See the info node `(make) Target-specific'.
119 Subdirectory templates `.sd.mk' vs plain autoconf templates `.mk.in'
120 --------------------------------------------------------------------
122 There are two kinds of template files.
124 Filename .sd.mk .mk.in
126 Processed by &-substitution, autoconf only
129 Instantiated Usu. once per subdir Once only
131 Need to be mentioned No, but Subdir.sd.mk All not in subdirmk/
132 in configure.ac? via SUBDIRMK_SUBDIRS via SUBDIRMK_MAKEFILES
134 How to include `&:include foo.sd.mk' `include foo.mk'
135 in all relevant .sd.mk in only one
136 (but not needed for Subdir.sd.mk
139 If you `include subdirmk/regen.mk', dependency management and
140 automatic regeneration for all of this template substitution, and for
141 config.status etc. is done for you.
143 Tables of file reference syntaxes
144 ---------------------------------
146 In a nonrecursive makefile supporting out of tree builds there are
147 three separate important distinctions between different file
150 (i) In the build tree, or in the source tree ?
152 (ii) In (or relative to) the subdirectory to which this Subdir.sd.mk
153 relates, or relative to the project's top level ?
155 (iii) Absolute or relative pathname ?
156 (Usually relative pathnames suffice.)
158 Path construction &-expansions are built from the following:
160 Relative paths in... Absolute paths in...
161 build source build source
163 This directory & &, &@ &@,
164 Top level . &; &@. &@;
166 In more detail, with the various options for what comes next:
168 Recommended Relative paths in... Absolute paths in...
169 for build source build source
171 This lc &file &,file &@file &@,file
172 directory any &/file &,/file &@/file &@,/file
173 several & f g h &, f g h &@ f g h &@, f g h
175 Top lc file &;file &@.file &@;file
176 level any file &;/file &@./file &@;/file
177 several f g h &; f g h &@. f g h &@; f g h
178 .mk.in file $(src)/file $(abs)/file $(abs_src)/file
183 In general & expands to the subdirectory name when used for a
184 filename, and to the subdirectory name with / replaced with _ for
187 Note that & is processed *even in makefile comments*. The substitutor
188 does not understand make syntax, or shell syntax, at all. However,
189 the substitution rules are chosen to work well with constructs which
190 are common in makefiles.
192 In the notation below, we suppose that the substitution is being in
193 done in a subdirectory sub/dir of the source tree. In the RH column
194 we describe the expansion at the top level, which is often a special
195 case (in general in variable names we call that TOP rather than the
198 &CAPS => sub_dir_CAPS or TOP_CAPS
199 &lc => sub/dir/lc or lc
200 Here CAPS is any ASCII letter A-Z and lc is a-z.
201 The assumption is that filenames are usually lowercase and
202 variables usually uppercase. Otherwise, use another syntax:
204 &_ => sub_dir_ or TOP_
205 &=_ => sub_dir or TOP
207 &/ => sub/dir/ or nothing
210 &,lc => $(top_srcdir)/sub/dir/lc &,/ => $(top_srcdir)/sub/dir/
211 &;lc => $(top_srcdir)/lc &;/ => $(top_srcdir)/
213 &@lc => $(PWD)/sub/dir/lc &@/ => $(PWD)/sub/dir/
214 &@.lc => $(PWD)/lc &@./ => $(PWD)/
215 &@,lc => $(abs_top_srcdir)/sub/dir/lc &@,/ => $(abs_top_srcdir)/sub/dir/
216 &@;lc => $(abs_top_srcdir)/lc &@;/ => $(abs_top_srcdir)/
219 = return subdir without delimiter (not allowed with `,' `;' `@')
220 , pathname of this subdirectory in source tree
221 ; pathname of top level of source tree
222 . pathname of this directory in build tree, `@' must be specified
224 / terminates the escape (needed if next is not lwsp or space)
225 lwsp starts multi-word processing (see below)
227 So pathname syntax is a subset of:
228 '&' [ '@' ] [ ',' | ';' | '.' ] [ lc | '/' ]
230 To avoid incomprehensible .sd.mk files, some combinations are not
231 allowed. For example `&=./' would mean `.' and `&./' would be the
232 empty string. Variations with `=' and one of `@' `,' `;' are
233 uncommon and must be written using make variables instead.
235 && => && for convenience in shell runes
236 \& => & general escaping mechanism
238 & thing thing... & &@ thing thing... &
240 &, thing thing... & &@, thing thing... &
241 &; thing thing... & &@; thing thing... &
242 Convenience syntax for prefixing multiple filenames.
243 Introduced by & followed by lwsp where lc could go.
244 Each lwsp-separated non-ws word is prefixed by &/ etc.
245 etc. respectively. No other & escapes are recognised.
246 This processing continues until & preceded by lwsp,
247 or until EOL (the end of the line), or \ then EOL.
249 &:<directive> <args>....
250 recognised at start of line only (possibly after lwsp)
251 args are processed for &
253 &:include filename filename should usually be foo.sd.mk
254 &:-include filename tolerate nonexistent file
255 filenames are relative to $(top_srcdir)
257 &!<lwsp> disables & until EOL (and then disappears)
259 &# delete everything to end of line
260 (useful if the RHS contains unrecognised & constructions)
263 changes the escape sequence from & to literally STUFF
264 STUFF may be any series of of non-whitespace characters,
265 and is terminated by EOL or lwsp. &!STUFF and the lwsp
268 After this, write STUFF instead of &, everywhere.
269 The effect is global and lasts until the next setting.
270 It takes effect on &:include'd files too, so maybe set
271 it back before using &:include.
274 STUFFSTUFF => STUFFSTUFF
276 STUFF!& set escape back to &
279 Handled specially. If mentioned, declares that this
280 subdir ought to have a target `things'. The rule will be
281 &/things:: $(&TARGETS_things)
283 You may extend it by adding more :: rules for the target,
284 but the preferred style is to do things like this:
285 &TARGETS_check += & test-passed.stamp
287 It is important to mention &TARGETS_things at least once in
288 the context of each applicable directory, because doing so
289 arranges that the *parent* will also have a `things' target
290 which recursively implies this directory's `things'.
292 Must be spelled exactly &TARGETS_things. &_TARGETS_things,
293 for example, is not magic. But mentioning &TARGETS_things in
294 a #-comment *does* work because the & filter does not care
297 `all' is extra special: every directory has an `all'
298 target, which corresponds to &TARGETS.
300 Subdirectory and variable naming
301 --------------------------------
303 The simple variable decoration scheme does not enforce a strict
304 namespace distinction between parts of variable names which come from
305 subdirectory names, and parts that mean something else.
307 So it is a good idea to be a bit careful with your directory naming.
308 `TOP', names that contain `_', and names that are similar to parts of
309 make variables (whether conventional ones, or ones used in your
310 project) are best avoided.
312 If you name your variables in ALL CAPS and your subdirectories in
313 lower case with `-' rather than `_', there will be no confusion.
315 Incorporating this into your project
316 ------------------------------------
318 Use `git-subtree' to merge the subdirmk/ directory. You may find it
319 useful to symlink the DEVELOPER-CERTIFICATE file (git can store
320 symlinks as symlinks - just `git add' the link). And you probably
321 want to mention the situation in your top-level COPYING.
323 Symlink autogen.sh into your project toplevel.
325 In your configure.ac, say
327 m4_include([subdirmk/subdirmk.ac])
328 SUBDIRMK_SUBDIRS([...list of subdirectories in relative syntax...])
330 Write a Subdir.sd.mk in each directory. The toplevel one should
333 include subdirmk/usual.mk
334 include subdirmk/regen.mk
336 Write a Perdir.sd.mk in the toplevel, if you want. It should probably
339 &:include subdirmk/cdeps.sd.mk
340 &:include subdirmk/clean.sd.mk
345 You can convert your project incrementally. Start with the top-level
346 Makefile.in and rename it to Subdir.sd.mk, and add the appropriate
347 stuff to configure.ac, and fix everything up. Leave the existing
348 $(MAKE) -C for your existing subdirectories alone. Then you can
349 convert individual subdirectories, or classes of subdirectories, at
350 your leisure. (You must be /sure/ that each subdirectory will be
351 entered only once at a time, but your existing recursive make descent
352 system should already do that or you already have concurrency bugs.)
354 Aside from this, be very wary of any invocation of $(MAKE) anywhere.
355 This is a frequent source of concurrency bugs in recursive make build
356 systems. When combined with nonrecursive make it's all in the same
357 directory and there is nothing stopping the different invocations
358 ending up trying to make the same targets at the same time. That
359 causes hideous racy lossage. There are ways to get this to work
360 reliably but it is advanced stuff.
362 If you make syntax errors, or certain kinds of other errors, in your
363 makefiles, you may find that just `make' is broken now and cannot get
364 far enough to regenerate a working set of makefiles. If this happens
365 just rerun ./config.status by hand.
372 Copyright 2019 Mark Wooding
373 Copyright 2019 Ian Jackson
375 subdirmk and its example is free software; you can redistribute it
376 and/or modify it under the terms of the GNU Library General Public
377 License as published by the Free Software Foundation; either
378 version 2 of the License, or (at your option) any later version.
380 This is distributed in the hope that it will be useful, but
381 WITHOUT ANY WARRANTY; without even the implied warranty of
382 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
383 Library General Public License for more details.
385 You should have received a copy of the GNU Library General Public
386 License along with this library as the file LGPL-2.
387 If not, see https://www.gnu.org/.
389 Individual files generally contain the following tag in the copyright
390 notice, instead of the full licence grant text:
391 SPDX-License-Identifier: LGPL-2.0-or-later
392 As is conventional, this should be read as a licence grant.
394 Contributions are accepted based on the git commit Signed-off-by
395 convention, by which the contributors' certify their contributions
396 according to the Developer Certificate of Origin version 1.1 - see
397 the file DEVELOPER-CERTIFICATE.
399 Where subdirmk is used by and incorporated into another project (eg
400 via git subtree), the directory subdirmk/ is under GNU LGPL-2.0+, and
401 the rest of the project are under that other project's licence(s).
402 (The project's overall licence must be compatible with LGPL-2.0+.)