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