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