chiark / gitweb /
README: fix TARGETS text for new tidier semantics
[subdirmk.git] / README
1 subdirmk - assistance for non-recursive use of make
2 ===================================================
3
4 Introduction
5 ------------
6
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.
11
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)
20
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
23 builds well).
24
25 Basic approach
26 --------------
27
28 The developer is expected to write a makefile fragment, in each
29 relevant subdirectory, called `Subdir.sd.mk'.
30
31 These fragments may contain ordinary make language.  Unqualified
32 filenames are relative to the build toplevel, and all commands all run
33 there.
34
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.
38
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).
42
43 The Subdir.sd.mk's are filtered, fed through autoconf in the usual way
44 (for @..@-substitutions) and included by one autogenerated toplevel
45 makefile.
46
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.)
50
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.
54
55 Where there are dependencies between subdirectories, each Subdir.sd.mk
56 can simply refer to files in other subdirectories directly.
57
58 Invocation, "recursive" per-directory targets
59 ---------------------------------------------
60
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.
65
66 But `foo' can also be a conventional target like `all'.
67
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
73 src_TARGETS.)
74
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.
80
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).
85
86 You can define other per-directory recursive targets too: set the
87 variable &TARGETS_zonk, or whatever (being sure to write &TARGETS_zonk
88 at the start of a line).  This will create a src/zonk target (for
89 appropriate value of src/).  Unlike `all', these other targets only
90 exist in areas of the project where at least something mentions them.
91 So for example, if &TARGETS_zonk is set in src but not lib, `make
92 zonk' in lib will fail.  If you want to make a target exist
93 everywhere, += it with nothing in Prefix.sd.mk or Suffix.sd.mk (see
94 below).
95
96 Prefix.sd.mk, Suffix.sd.mk, inclusion
97 -------------------------------------
98
99 The files Prefix.sd.mk and Suffix.sd.mk in the toplevel of the source
100 are automatically processed before and after each individual
101 directory's Subdir.sd.mk, and the &-substituted contents therefore
102 appear once for each subdirectory.
103
104 This lets you do per-directory boilerplate.  Some useful boilerplate
105 is already provided in subdirmk, for you to reference like this:
106   &:include subdirmk/cdeps.sd.mk
107   &:include subdirmk/clean.sd.mk
108 For example you could put that in Suffix.sd.mk.
109
110 The top-level Subdir.sd.mk is the first makefile included after the
111 autogenerated `main.mk' which merely has some basic settings and
112 includes.  So if you want to get in early and set global variables,
113 put them near the top of Subdir.sd.mk.
114
115 subdirmk's filter script itself sets (only) these variables:
116   top_srcdir
117   abs_top_srcdir
118   SUBDIRMK_MAKEFILES
119   MAKEFILE_TEMPLATES
120 You are likely to want to define $(PWD), and shorter names for
121 top_srdir and abs_top_srcdir (we suggest $(src) and $(abs_src)).
122
123 Global definitions
124 ------------------
125
126 If want to set global variables, such as CC, that should only be done
127 once.  You can put them in your top-level Subdir.sd.mk, or a separate
128 file you `include' and declare using SUBDIRMK_MAKEFILES.
129
130 If you need different settings of variables like CC for different
131 subdirectories, you should probably do that with target-specific
132 variable settings.  See the info node `(make) Target-specific'.
133
134 Subdirectory templates `.sd.mk' vs plain autoconf templates `.mk.in'
135 --------------------------------------------------------------------
136
137 There are two kinds of template files.
138
139  Filename                 .sd.mk                  .mk.in
140
141  Processed by             &-substitution,         autoconf only
142                           then autoconf
143
144  Instantiated             Usu. once per subdir    Once only
145
146  Need to be mentioned     No, but Subdir.sd.mk    All not in subdirmk/
147  in configure.ac?         via SUBDIRMK_SUBDIRS    via SUBDIRMK_MAKEFILES
148
149  How to include           `&:include foo.sd.mk'   `include foo.mk'
150                           in all relevant .sd.mk  in only one
151                           (but not needed for     Subdir.sd.mk
152                            Prefix, Suffix)
153
154 If you `include subdirmk/regen.mk', dependency management and
155 automatic regeneration for all of this template substitution, and for
156 config.status etc. is done for you.
157
158 Tables of file reference syntaxes
159 ---------------------------------
160
161 In a nonrecursive makefile supporting out of tree builds there are
162 three separate important distinctions between different file
163 locations:
164
165  (i) In the build tree, or in the source tree ?
166
167  (ii) In (or relative to) the subdirectory to which this Subdir.sd.mk
168      relates, or relative to the project's top level ?
169
170  (iii) Absolute or relative pathname ?  Usually relative pathnames
171      suffice.  Where an absolute pathname is needed, it can be built
172      out of &/ and an appropriate make variable such as $(PWD).
173
174 Path construction &-expansions are built from the following:
175
176                       Relative paths in...
177                       build     source
178                                                        
179   This directory      &         &^
180   Top level           .         &~
181
182 In more detail, with all the various options laid out:
183
184       Recommended     Relative paths in...   Absolute paths in...
185              for      build     source       build         source
186                                                        
187   This       lc       &file     &^file       $(PWD)/&file  $(abs_src)/&file
188   directory  any      &/file    &^/file      $(PWD)/&/file $(abs_src)/&/file
189              several  & f g h   &^ f g h     $(addprefix...)
190                                              
191   Top        lc       file      &~file
192   level      any      file      &~/file      $(PWD)/file   $(abs_src)/file
193              .mk.in   file      $(src)/file  $(PWD)/file   $(abs_src)/file
194              several  f g h     &~ f g h     $(addprefix...)
195
196 (This assumes you have appropriate make variables src, PWD and
197 abs_src.)
198
199 Substitution syntax
200 -------------------
201
202 In general & expands to the subdirectory name when used for a
203 filename, and to the subdirectory name with / replaced with _ for
204 variable names.
205
206 Note that & is processed *even in makefile comments*.  The substitutor
207 does not understand make syntax, or shell syntax, at all.  However,
208 the substitution rules are chosen to work well with constructs which
209 are common in makefiles.
210
211 In the notation below, we suppose that the substitution is being in
212 done in a subdirectory sub/dir of the source tree.  In the RH column
213 we describe the expansion at the top level, which is often a special
214 case (in general in variable names we call that TOP rather than the
215 empty string).
216
217 &CAPS           =>      sub_dir_CAPS                    or TOP_CAPS
218 &lc             =>      sub/dir/lc                      or lc
219         Here CAPS is any ASCII letter A-Z and lc is a-z.
220         The assumption is that filenames are usually lowercase and
221         variables usually uppercase.  Otherwise, use another syntax:
222
223 &/              =>      sub/dir/                        or nothing
224 &_              =>      sub_dir_                        or TOP_
225 &.              =>      sub/dir                         or .
226         (This implies that `&./' works roughly like `&/', although
227         it can produce a needless `./')
228
229 &=              =>      sub_dir                         or TOP
230
231 &^lc            =>      $(top_srcdir)/sub/dir/lc
232 &^/             =>      $(top_srcdir)/sub/dir/
233 &^.             =>      $(top_srcdir)/sub/dir
234
235 &~lc            =>      $(top_srcdir)/lc
236 &~/             =>      $(top_srcdir)/
237 &~.             =>      $(top_srcdir)
238
239 In general:
240     ^   pathname of this subdirectory in source tree
241     ~   pathname of top level of source tree
242     /   terminates the path escape } needed if next is
243     _   terminates the var escape  } not lwsp or space)
244     .   terminates path escape giving dir name (excluding /)
245     =   terminates var escape giving only prefix part (rarely needed)
246   lwsp  starts multi-word processing (see below)
247
248 So pathname syntax is a subset of:
249     '&' [ '^' | '~' ] [ lc | '/' | '.' | '=' ]
250
251 &&              =>      &&              for convenience in shell runes
252
253 &\&             =>      &               general escaping mechanism
254 &\$             =>      $
255 &\NEWLINE                               eats the newline and vanishes
256
257 &$VARIABLE      =>      $(sub_dir_VARIABLE)     or $(TOP_VARIABLE)
258         VARIABLE is ASCII starting with a letter and matching \w+
259
260 & thing thing... &
261 &^ thing thing... &
262 &~ thing thing... &
263         Convenience syntax for prefixing multiple filenames.
264         Introduced by & followed by lwsp where lc could go.
265         Each lwsp-separated non-ws word is prefixed by &/ etc.
266         etc. respectively.  No other & escapes are recognised.
267         This processing continues until & preceded by lwsp,
268         or until EOL (the end of the line), or \ then EOL.
269
270 &:<directive> <args>....
271         recognised at start of line only (possibly after lwsp)
272
273 &:include filename              filename should usually be [&]foo.sd.mk
274 &:-include filename             tolerate nonexistent file
275         filenames are relative to $(top_srcdir)
276         RHS is &-expanded
277
278 &!<lwsp>        disables & until EOL (and then disappears)
279
280 &#      delete everything to end of line
281         (useful if the RHS contains unrecognised & constructions)
282
283 &:changequote NEWQUOTE
284         changes the escape sequence from & to literally NEWQUOTE
285         NEWQUOTE may be any series of of non-whitespace characters,
286         and is terminated by EOL or lwsp.  The whole line is
287         discarded.
288
289         After this, write NEWQUOTE instead of &, everywhere.
290         The effect is global and lasts until the next setting.
291         It takes effect on &:include'd files too, so maybe set
292         it back before using &:include.
293
294         Notably
295                 NEWQUOTENEWQUOTE        => NEWQUOTENEWQUOTE
296                 NEWQUOTE\NEWQUOTE       => NEWQUOTE
297                 NEWQUOTE\$              => $
298                 NEWQUOTE:changequote &  set escape back to &
299
300 &TARGETS_things
301         Handled specially.  If mentioned at the start of a line
302         (possibly following whitespace), declares that this
303         subdir ought to have a target `things'.  The rule will be
304                 &/things:: $(&TARGETS_things)
305
306         You may extend it by adding more :: rules for the target,
307         but the preferred style is to do things like this:
308                 &TARGETS_check += & test-passed.stamp
309
310         It is important to mention &TARGETS_things at least once in
311         the context of each applicable directory, because doing so
312         arranges that the *parent* will also have a `things' target
313         which recursively implies this directory's `things'.
314
315         Must be spelled exactly &TARGETS_things.  &_TARGETS_things,
316         for example, is not magic.  To make the target exist
317         without providing any prerequisites for it, write a line
318         containing just `&TARGETS_things +='.
319
320         `all' is extra special: every directory has an `all'
321         target, which corresponds to &TARGETS.
322
323 Subdirectory and variable naming
324 --------------------------------
325
326 The simple variable decoration scheme does not enforce a strict
327 namespace distinction between parts of variable names which come from
328 subdirectory names, and parts that mean something else.
329
330 So it is a good idea to be a bit careful with your directory naming.
331 `TOP', names that contain `_', and names that are similar to parts of
332 make variables (whether conventional ones, or ones used in your
333 project) are best avoided.
334
335 If you name your variables in ALL CAPS and your subdirectories in
336 lower case with `-' rather than `_', there will be no confusion.
337
338 Incorporating this into your project
339 ------------------------------------
340
341 Use `git-subtree' to merge the subdirmk/ directory.  You may find it
342 useful to symlink the DEVELOPER-CERTIFICATE file (git can store
343 symlinks as symlinks - just `git add' the link).  And you probably
344 want to mention the situation in your top-level COPYING.
345
346 Symlink autogen.sh into your project toplevel.
347
348 In your configure.ac, say
349
350   m4_include([subdirmk/subdirmk.ac])
351   SUBDIRMK_SUBDIRS([...list of subdirectories in relative syntax...])
352
353 Write a Subdir.sd.mk in each directory.  The toplevel one should
354 probably contain:
355
356   include subdirmk/usual.mk
357   include subdirmk/regen.mk
358
359 Write a Suffix.sd.mk in the toplevel, if you want.  It should probably
360 have:
361
362   &:include subdirmk/cdeps.sd.mk
363   &:include subdirmk/clean.sd.mk
364
365 Hints
366 -----
367
368 You can convert your project incrementally.  Start with the top-level
369 Makefile.in and rename it to Subdir.sd.mk, and add the appropriate
370 stuff to configure.ac, and fix everything up.  Leave the existing
371 $(MAKE) -C for your existing subdirectories alone.  Then you can
372 convert individual subdirectories, or classes of subdirectories, at
373 your leisure.  (You must be /sure/ that each subdirectory will be
374 entered only once at a time, but your existing recursive make descent
375 system should already do that or you already have concurrency bugs.)
376
377 Aside from this, be very wary of any invocation of $(MAKE) anywhere.
378 This is a frequent source of concurrency bugs in recursive make build
379 systems.  When combined with nonrecursive make it's all in the same
380 directory and there is nothing stopping the different invocations
381 ending up trying to make the same targets at the same time. That
382 causes hideous racy lossage.  There are ways to get this to work
383 reliably but it is advanced stuff.
384
385 If you make syntax errors, or certain kinds of other errors, in your
386 makefiles, you may find that just `make' is broken now and cannot get
387 far enough to regenerate a working set of makefiles.  If this happens
388 just rerun ./config.status by hand.
389
390 If you go back and forth between different versions of your code you
391 can sometimes find that `make' complains that one of your Subdir.sd.mk
392 files is missing: typically, if iot was used and therefore a
393 dependency in some other version of your code.  If you run `make
394 clean' (or `make realclean') these dependencies are suppressed, which
395 will clear up the problem.
396
397
398 Legal information
399 -----------------
400
401 subdirmk is
402  Copyright 2019 Mark Wooding
403  Copyright 2019 Ian Jackson
404
405     subdirmk and its example is free software; you can redistribute it
406     and/or modify it under the terms of the GNU Library General Public
407     License as published by the Free Software Foundation; either
408     version 2 of the License, or (at your option) any later version.
409
410     This is distributed in the hope that it will be useful, but
411     WITHOUT ANY WARRANTY; without even the implied warranty of
412     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
413     Library General Public License for more details.
414
415     You should have received a copy of the GNU Library General Public
416     License along with this library as the file LGPL-2.
417     If not, see https://www.gnu.org/.
418
419 Individual files generally contain the following tag in the copyright
420 notice, instead of the full licence grant text:
421   SPDX-License-Identifier: LGPL-2.0-or-later
422 As is conventional, this should be read as a licence grant.
423
424 Contributions are accepted based on the git commit Signed-off-by
425 convention, by which the contributors' certify their contributions
426 according to the Developer Certificate of Origin version 1.1 - see
427 the file DEVELOPER-CERTIFICATE.
428
429 Where subdirmk is used by and incorporated into another project (eg
430 via git subtree), the directory subdirmk/ is under GNU LGPL-2.0+, and
431 the rest of the project are under that other project's licence(s).
432 (The project's overall licence must be compatible with LGPL-2.0+.)