--- /dev/null
+# subdirmk - subdirmk/.gitignore
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+#----- subdirmk-generated -----
+/regen.mk
+/usual.mk
--- /dev/null
+Developer Certificate of Origin
+Version 1.1
+
+Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
+1 Letterman Drive
+Suite D4700
+San Francisco, CA, 94129
+
+Everyone is permitted to copy and distribute verbatim copies of this
+license document, but changing it is not allowed.
+
+
+Developer's Certificate of Origin 1.1
+
+By making a contribution to this project, I certify that:
+
+(a) The contribution was created in whole or in part by me and I
+ have the right to submit it under the open source license
+ indicated in the file; or
+
+(b) The contribution is based upon previous work that, to the best
+ of my knowledge, is covered under an appropriate open source
+ license and I have the right under that license to submit that
+ work with modifications, whether created in whole or in part
+ by me, under the same open source license (unless I am
+ permitted to submit under a different license), as indicated
+ in the file; or
+
+(c) The contribution was provided directly to me by some other
+ person who certified (a), (b) or (c) and I have not modified
+ it.
+
+(d) I understand and agree that this project and the contribution
+ are public and that a record of the contribution (including all
+ personal information I submit with it, including my sign-off) is
+ maintained indefinitely and may be redistributed consistent with
+ this project or the open source license(s) involved.
+
--- /dev/null
+ GNU LIBRARY GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1991 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the library GPL. It is
+ numbered 2 because it goes with version 2 of the ordinary GPL.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Library General Public License, applies to some
+specially designated Free Software Foundation software, and to any
+other libraries whose authors decide to use it. You can use it for
+your libraries, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if
+you distribute copies of the library, or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link a program with the library, you must provide
+complete object files to the recipients so that they can relink them
+with the library, after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ Our method of protecting your rights has two steps: (1) copyright
+the library, and (2) offer you this license which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ Also, for each distributor's protection, we want to make certain
+that everyone understands that there is no warranty for this free
+library. If the library is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original
+version, so that any problems introduced by others will not reflect on
+the original authors' reputations.
+\f
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that companies distributing free
+software will individually obtain patent licenses, thus in effect
+transforming the program into proprietary software. To prevent this,
+we have made it clear that any patent must be licensed for everyone's
+free use or not licensed at all.
+
+ Most GNU software, including some libraries, is covered by the ordinary
+GNU General Public License, which was designed for utility programs. This
+license, the GNU Library General Public License, applies to certain
+designated libraries. This license is quite different from the ordinary
+one; be sure to read it in full, and don't assume that anything in it is
+the same as in the ordinary license.
+
+ The reason we have a separate public license for some libraries is that
+they blur the distinction we usually make between modifying or adding to a
+program and simply using it. Linking a program with a library, without
+changing the library, is in some sense simply using the library, and is
+analogous to running a utility program or application program. However, in
+a textual and legal sense, the linked executable is a combined work, a
+derivative of the original library, and the ordinary General Public License
+treats it as such.
+
+ Because of this blurred distinction, using the ordinary General
+Public License for libraries did not effectively promote software
+sharing, because most developers did not use the libraries. We
+concluded that weaker conditions might promote sharing better.
+
+ However, unrestricted linking of non-free programs would deprive the
+users of those programs of all benefit from the free status of the
+libraries themselves. This Library General Public License is intended to
+permit developers of non-free programs to use free libraries, while
+preserving your freedom as a user of such programs to change the free
+libraries that are incorporated in them. (We have not seen how to achieve
+this as regards changes in header files, but we have achieved it as regards
+changes in the actual functions of the Library.) The hope is that this
+will lead to faster development of free libraries.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, while the latter only
+works together with the library.
+
+ Note that it is possible for a library to be covered by the ordinary
+General Public License rather than by this special one.
+\f
+ GNU LIBRARY GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library which
+contains a notice placed by the copyright holder or other authorized
+party saying it may be distributed under the terms of this Library
+General Public License (also called "this License"). Each licensee is
+addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+\f
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+\f
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+\f
+ 6. As an exception to the Sections above, you may also compile or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ c) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ d) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the source code distributed need not include anything that is normally
+distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+\f
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+\f
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded. In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Library General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+\f
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+\f
+ How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library. It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
--- /dev/null
+subdirmk - assistance for non-recursive use of make
+===================================================
+
+Introduction
+------------
+
+Peter Miller's 1997 essay _Recursive Make Considered Harmful_
+persuasively argues that it is better to arrange to have a single
+make invocation with the project's complete dependency tree, rather
+than the currently conventional `$(MAKE) -C subdirectory' approach.
+
+However, actually writing a project's build system in a non-recursive
+style is not very ergonomic. The main difficulties are:
+ - constantly having to write out long file and directory names
+ - the lack of a per-directory make variable namespace means
+ long make variables (or namespace clashes)
+ - it is difficult to arrange that one can cd to a subdirectory
+ and say `make all' and have something reasonable happen
+ (to wit, build an appropriate subset)
+
+`subdirmk' is an attempt to solve these problems (and it also slightly
+alleviates some of the boilerplate needed to support out-of-tree
+builds well).
+
+Basic approach
+--------------
+
+The developer is expected to write a makefile fragment, in each
+relevant subdirectory, called `Subdir.sd.mk'.
+
+These fragments may contain ordinary make language.
+
+However, the sigil & is treated specially. By and large, it refers to
+`the current directory'. There are a variety of convenient
+constructions.
+
+The result is that to a large extent, the Subdir.sd.mk has an easy way
+to namespace its "local" make variables, and an easy way to refer to
+its "local" filenames.
+
+The Subdir.sd.mk's are filtered, fed through autoconf in the usual way
+(for @..@-substitutions) and included by one autogenerated toplevel
+makefile.
+
+So all of the input is combined and passed to one make invocation.
+(A corollary is that there is no enforcement of the namespacing:
+discipline is required to prefix relevant variable names with &, etc.)
+
+Each subdirectory is also provided with an autogenerated `Makefile'
+which exists purely to capture ordinary make invocations and arrange
+for something suitable to happen.
+
+Where there are dependencies between subdirectories, each Subdir.sd.mk
+can simply refer to files in other subdirectories directly.
+
+Invocation, "recursive" per-directory targets
+---------------------------------------------
+
+Arrangements are made so that when you run `make foo' in a
+subdirectory, it is like running the whole toplevel makefile, from the
+toplevel, as `make subdir/foo'. If `subdir/foo' is a file that might
+be built, that builds it.
+
+But `foo' can also be a conventional target like `all'.
+
+Each subdirectory has its own `all' target. For example a
+subdirectory `src' has a target `src/all'. The rules for these are
+automatically generated from the settings of the per-directory
+&TARGETS variables. &TARGETS is magic in this way. (In
+src/Subdir.sd.mk, &TARGETS of course refers to a make variable called
+src_TARGETS.)
+
+The `all' target in a parent directory is taken to imply the `all'
+targets in all of its subdirectories, recursively. And in the
+autogenerated stub Makefiles, `all' is the default target. So if you
+just type `make' in the toplevel, you are asking for `&all'
+(<subdir>/all) for every directory in the project.
+
+In a parallel build, the rules for all these various subdirectory
+targets may be in run in parallel: there is only one `make' invocation
+at a time. There is no sequencing between subdirectories, only been
+individual targets (as specified according to their dependencies).
+
+You can define other per-directory recursive targets too: simply
+mention (usually, by setting) the variable &TARGETS_zonk, or whatever.
+This will create a src/zonk target (for appropriate value of src/).
+Unlike `all', these other targets only exist in areas of the project
+where at least something mentions them. So for example, if
+&TARGETS_zonk is mentioned in src but not lib, `make zonk' in
+lib will fail. If you want to make a target exist everywhere,
+mention its name in Perdir.sd.mk (see below).
+
+Perdir.sd.mk, inclusion
+-----------------------
+
+The file Perdir.sd.mk in the toplevel of the source is automatically
+processed after each individual directory's Subdir.sd.mk, and the
+&-substituted contents therefore appear once for each subdirectory.
+
+This lets you do per-directory boilerplate. Some useful boilerplate
+is already provided in subdirmk, for you to reference like this:
+ &:include subdirmk/cdeps.sd.mk
+ &:include subdirmk/clean.sd.mk
+For example you could put that in Perdir.sd.mk.
+
+Global definitions
+------------------
+
+If want to set global variables, such as CC, that should only be done
+once. You can put them in your top-level Subdir.sd.mk, or a separate
+file you `include' and declare using SUBDIRMK_MAKEFILES.
+
+If you need different settings of variables like CC for different
+subdirectories, you should probably do that with target-specific
+variable settings. See the info node `(make) Target-specific'.
+
+Subdirectory templates `.sd.mk' vs plain autoconf templates `.mk.in'
+--------------------------------------------------------------------
+
+There are two kinds of template files.
+
+ Filename .sd.mk .mk.in
+
+ Processed by &-substitution, autoconf only
+ then autoconf
+
+ Instantiated Usu. once per subdir Once only
+
+ Need to be mentioned No, but Subdir.sd.mk All not in subdirmk/
+ in configure.ac? via SUBDIRMK_SUBDIRS via SUBDIRMK_MAKEFILES
+
+ How to include `&:include foo.sd.mk' `include foo.mk'
+ in all relevant .sd.mk in only one
+ (but not needed for Subdir.sd.mk
+ Subdir and Perdir)
+
+If you `include subdirmk/regen.mk', dependency management and
+automatic regeneration for all of this template substitution, and for
+config.status etc. is done for you.
+
+Substitution syntax
+-------------------
+
+In general & expands to the subdirectory name when used for a
+filename, and to the subdirectory name with / replaced with _ for
+variable names.
+
+Note that & is processed *even in makefile comments*. The substitutor
+does not understand make syntax, or shell syntax, at all. However,
+the substitution rules are chosen to work well with constructs which
+are common in makefiles.
+
+In the notation below, we suppose that the substitution is being in
+done in a subdirectory sub/dir of the source tree. In the RH column
+we describe the expansion at the top level, which is often a special
+case (in general in variable names we call that TOP rather than the
+empty string).
+
+&CAPS => sub_dir_CAPS or TOP_CAPS
+&lc => sub/dir/lc or lc
+ Here CAPS is any ASCII letter A-Z and lc is a-z.
+ The assumption is that filenames are usually lowercase and
+ variables usually uppercase. Otherwise, use another syntax:
+
+&_ => sub_dir_ or TOP_
+&/ => sub/dir/ or nothing
+&=_ => sub_dir or TOP
+&=/ => sub/dir or .
+&^ => $(top_srcdir)/sub/dir or $(top_srcdir)
+&~ => $(abs_top_srcdir)/sub/dir or $(abs_top_srcdir)
+
+&& => && for convenience in shell runes
+\& => & general escaping mechanism
+
+& thing thing... &
+& ^ thing thing... &
+& ~ thing thing... &
+ Convenience syntax for prefixing multiple filenames.
+ Introduced by & followed by lwsp (space or tab).
+ Each lwsp-separated non-ws word is prefixed by &/ &^/ &~/
+ respectively. No other & escapes are recognised.
+ This processing continues until & preceded by lwsp,
+ or until EOL (the end of the line), or \ then EOL.
+
+&:<directive> <args>....
+ recognised at start of line only (possibly after lwsp)
+ args are processed for &
+
+&:include filename filename should usually be foo.sd.mk
+&:-include filename tolerate nonexistent file
+ filenames are relative to $(top_srcdir)
+
+&!<lwsp> disables & until EOL (and then disappears)
+
+&# delete everything to end of line
+ (useful if the RHS contains unrecognised & constructions)
+
+&!STUFF
+ changes the escape sequence from & to literally STUFF
+ STUFF may be any series of of non-whitespace characters,
+ and is terminated by EOL or lwsp. &!STUFF and the lwsp
+ are discarded.
+
+ After this, write STUFF instead of &, everywhere.
+ The effect is global and lasts until the next setting.
+ It takes effect on &:include'd files too, so maybe set
+ it back before using &:include.
+
+ Notably
+ STUFFSTUFF => STUFFSTUFF
+ \STUFF => STUFF
+ STUFF!& set escape back to &
+
+&TARGETS_things
+ Handled specially. If mentioned, declares that this
+ subdir ought to have a target `things'. The rule will be
+ &/things:: $(&TARGETS_things)
+
+ You may extend it by adding more :: rules for the target,
+ but the preferred style is to do things like this:
+ &TARGETS_check += & test-passed.stamp
+
+ It is important to mention &TARGETS_things at least once in
+ the context of each applicable directory, because doing so
+ arranges that the *parent* will also have a `things' target
+ which recursively implies this directory's `things'.
+
+ Must be spelled exactly &TARGETS_things. &_TARGETS_things,
+ for example, is not magic. But mentioning &TARGETS_things in
+ a #-comment *does* work because the & filter does not care
+ about comments.
+
+ `all' is extra special: every directory has an `all'
+ target, which corresponds to &TARGETS.
+
+Subdirectory and variable naming
+--------------------------------
+
+The simple variable decoration scheme does not enforce a strict
+namespace distinction between parts of variable names which come from
+subdirectory names, and parts that mean something else.
+
+So it is a good idea to be a bit careful with your directory naming.
+`TOP', names that contain `_', and names that are similar to parts of
+make variables (whether conventional ones, or ones used in your
+project) are best avoided.
+
+If you name your variables in ALL CAPS and your subdirectories in
+lower case with `-' rather than `_', there will be no confusion.
+
+Incorporating this into your project
+------------------------------------
+
+Use `git-subtree' to merge the subdirmk/ directory. You may find it
+useful to symlink the DEVELOPER-CERTIFICATE file (git can store
+symlinks as symlinks - just `git add' the link). And you probably
+want to mention the situation in your top-level COPYING.
+
+Symlink autogen.sh into your project toplevel.
+
+In your configure.ac, say
+
+ m4_include([subdirmk/subdirmk.ac])
+ SUBDIRMK_SUBDIRS([...list of subdirectories in relative syntax...])
+
+Write a Subdir.sd.mk in each directory. The toplevel one should
+probably contain:
+
+ include subdirmk/usual.mk
+ include subdirmk/regen.mk
+
+Write a Perdir.sd.mk in the toplevel, if you want. It should probably
+have:
+
+ &:include subdirmk/cdeps.sd.mk
+ &:include subdirmk/clean.sd.mk
+
+
+Legal information
+-----------------
+
+subdirmk is
+ Copyright 2019 Mark Wooding
+ Copyright 2019 Ian Jackson
+
+ subdirmk and its example is free software; you can redistribute it
+ and/or modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with this library as the file LGPL-2.
+ If not, see https://www.gnu.org/.
+
+Individual files generally contain the following tag in the copyright
+notice, instead of the full licence grant text:
+ SPDX-License-Identifier: LGPL-2.0-or-later
+As is conventional, this should be read as a licence grant.
+
+Contributions are accepted based on the git commit Signed-off-by
+convention, by which the contributors' certify their contributions
+according to the Developer Certificate of Origin version 1.1 - see
+the file DEVELOPER-CERTIFICATE.
+
+Where subdirmk is used by and incorporated into another project (eg
+via git subtree), the directory subdirmk/ is under GNU LGPL-2.0+, and
+the rest of the project are under that other project's licence(s).
+(The project's overall licence must be compatible with LGPL-2.0+.)
--- /dev/null
+#!/bin/sh
+# subdirmk, autogen.sh (conventional autoconf invocation script)
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+set -e
+cd ${0%/*}
+autoconf
--- /dev/null
+# subdirmk - useful rules for making and using cpp .*.d files
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+&# Usage:
+&# &:include subdirmk/cdeps.sd.mk
+&# (probably in Perdir.sd.mk)
+&#
+&# Arranges for automatic #include dependency tracking for
+&# C compilation. The compiler is asked to write the dependencies to
+&# .*.d and these are automatically included.
+&#
+&# There is a bug: if a #included file is deleted and all references
+&# in .c files to it removed, `make' will complain that it is needed
+&# and can't be built. `make clean' will fix this.
+
+CDEPS_CFLAGS ?= -MD -MF $(*D)/.$(*F).d
+
+&DEPFILES += $(foreach b,$(patsubst %.o,%,$(&OBJECTS)), \
+ $(dir $b).$(notdir $b).d)
+-include $(&DEPFILES)
+
+&CLEAN += $(&DEPFILES)
--- /dev/null
+# subdirmk - useful rules for clean target
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+&# Usage:
+&# &:include subdirmk/clean.sd.mk
+&# (probably in Perdir.sd.mk)
+&#
+&# Provides a per-directory `clean' target, which deletes all the files
+&# in &CLEAN. &OBJECTS, &DEPFILES and &TARGETS are automatically deleted.
+&#
+&# If you want to delete a directory, extend the target with
+&# &/clean::
+&# $(RM) -r somethingn
+&# ($(RM) conventionally contains `-f'.)
+
+&CLEAN += & *~ *.tmp
+&CLEAN += $(&OBJECTS)
+&CLEAN += $(&TARGETS)
+
+# &TARGETS_clean
+
+&/clean::
+ $(RM) $(&CLEAN)
--- /dev/null
+# subdirmk example - .gitignore
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+#----- Build artifacts -----
+*.a
+.*.d
+*.o
+*.stamp
+/lib/t/toytest
+/src/toy
+
+#----- Autoconf cruft -----
+
+/autom4te.cache/
+/config.log
+/config.status
+/configure
+
+.makefiles.stamp
+/main.mk
+Makefile
+Subdir.mk
+*.tmp
+
+#----- For our tests -----
+/build
+/src/for-test.sd.mk
+/lib/for-test.mk.in
--- /dev/null
+subdirmk/DEVELOPER-CERTIFICATE
\ No newline at end of file
--- /dev/null
+subdirmk/LGPL-2
\ No newline at end of file
--- /dev/null
+# subdirmk example - general replicated per-directory template
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+&:include subdirmk/cdeps.sd.mk
+&:include subdirmk/clean.sd.mk
--- /dev/null
+# subdirmk example - top-level Subdir.sd.mk
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+INCLUDES += -I&^/lib/
+
+include subdirmk/usual.mk
+include subdirmk/regen.mk
--- /dev/null
+subdirmk/autogen.sh
\ No newline at end of file
--- /dev/null
+dnl -*-autoconf-*-
+dnl subdirmk example - configuration script
+dnl Copyright 2019 Mark Wooding
+dnl Copyright 2019 Ian Jackson
+dnl SPDX-License-Identifier: LGPL-2.0-or-later
+
+AC_INIT([mktoy], [0.9.0], [mdw@distorted.org.uk])
+AC_CONFIG_SRCDIR([src/toy.c])
+
+AC_PROG_CC
+INCLUDES=
+AC_SUBST(INCLUDES)
+
+m4_include([subdirmk/subdirmk.ac])
+
+SUBDIRMK_SUBDIRS([lib])
+SUBDIRMK_SUBDIRS([lib/t src])
+
+if test -f $srcdir/lib/for-test.mk.in; then
+ SUBDIRMK_MAKEFILES([lib/for-test.mk])
+fi
+
+AC_OUTPUT
+
+dnl----- That's all, folks --------------------------------------------------
--- /dev/null
+# subdirmk example - subdirectory rules
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+&TARGETS += & libtoy.a
+
+&OBJECTS += & toylib.o
+
+&libtoy.a: $(&OBJECTS)
+ $(AR) rc $@ $^
+
+-include &^/lib/for-test.mk
--- /dev/null
+# subdirmk example - subdirectory rules
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+&TARGETS_check += & toytest.stamp
+
+&OBJECTS += & toytest.o
+&LIBS += lib/libtoy.a
+
+&CLEAN += & toytest toytest.stamp
+
+&toytest: $(&OBJECTS) $(&LIBS)
+ $(LINK) $^
+
+&toytest.stamp: & toytest
+ $<
+ touch $@
--- /dev/null
+/*
+ * subdirmk - example code
+ * Copyright 2019 Mark Wooding
+ * SPDX-License-Identifier: LGPL-2.0-or-later
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "toylib.h"
+
+int main(void)
+{
+ const char *p;
+
+ p = greeting();
+ if (STRNCMP(p, !=, "Hello", 5)) {
+ fprintf(stderr, "greeting `%s' has bad salutation\n", p);
+ exit(1);
+ }
+ printf("all ok\n");
+ return (0);
+}
--- /dev/null
+/*
+ * subdirmk - example code
+ * Copyright 2019 Mark Wooding
+ * SPDX-License-Identifier: LGPL-2.0-or-later
+ */
+
+#include "toylib.h"
+
+const char *greeting(void)
+ { return "Hello, world!\n"; }
--- /dev/null
+/*
+ * subdirmk - example code
+ * Copyright 2019 Mark Wooding
+ * SPDX-License-Identifier: LGPL-2.0-or-later
+ */
+
+#ifndef LIBTOY_H
+#define LIBTOY_H
+
+#include <string.h>
+
+#define STRCMP(x, op, y) (strcmp((x), (y)) op 0)
+#define STRNCMP(x, op, y, n) (strncmp((x), (y), (n)) op 0)
+#define MEMCMP(x, op, y, n) (memcmp((x), (y), (n)) op 0)
+
+extern const char *greeting(void);
+
+#endif
--- /dev/null
+# subdirmk example - subdirectory rules
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+&TARGETS += & toy
+
+&OBJECTS += & toy.o
+&LIBS += lib/libtoy.a
+
+&toy: $(&OBJECTS) $(&LIBS)
+ $(LINK) $^
+
+&:-include src/for-test.sd.mk
--- /dev/null
+/*
+ * subdirmk - example code
+ * Copyright 2019 Mark Wooding
+ * SPDX-License-Identifier: LGPL-2.0-or-later
+ */
+
+#include <stdio.h>
+#include "toylib.h"
+
+int main(void)
+ { fputs(greeting(), stdout); return (0); }
--- /dev/null
+..
\ No newline at end of file
--- /dev/null
+#!/usr/bin/perl -w
+#
+# subdirmk - &-filter (makefile generation program)
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+#
+# $(srcdir)/subdirmk/generate [--srcdir=SRCDIR] [--] SUBDIR...
+#
+# generates in each subdirectory from in each subdirectory
+# Subdir.mk.tmp Subdir.sd.mk
+# Makefile and included files
+# and in toplevel and in toplevel
+# main.mk.tmp Perdir.sd.mk
+
+use strict;
+use POSIX;
+
+print "$0 @ARGV\n" or die $!;
+
+our $srcdir='.';
+
+while (@ARGV && $ARGV[0] =~ m/^-/) {
+ $_ = shift @ARGV;
+ last if $_ eq '--';
+ if (s/^--srcdir=//) {
+ $srcdir=$';
+ } else {
+ die "$0: unknown option \`$_'\n";
+ }
+}
+our @subdirs = @ARGV;
+
+s{/+$}{} foreach @subdirs;
+
+our $root = [ '.', [ ] ];
+# each node is [ 'relative subdir name', \@children ]
+
+sub build_tree () {
+ foreach my $subdir (@subdirs) {
+ my @path = $subdir eq '.' ? () : split m{/+}, $subdir;
+ my $node = $root;
+ foreach my $d (@path) {
+ my ($c,) = grep { $_->[0] eq $d } @{ $node->[1] };
+ if (!$c) {
+ $c = [ $d, [ ] ];
+ push @{ $node->[1] }, $c;
+ }
+ $node = $c;
+ }
+ }
+}
+
+sub target_varname ($$) {
+ my ($var_prefix, $target) = @_;
+ return $var_prefix.'TARGETS'.($target eq 'all' ? '' : "_$target");
+}
+
+our $writing_output;
+our $buffering_output;
+our %output_files;
+our %input_files;
+
+sub close_any_output_file() {
+ return unless defined $writing_output;
+ O->error and die "error writing $writing_output.tmp: $! (?)\n";
+ close O or die "error closing $writing_output.tmp: $!\n";
+ $writing_output = undef;
+}
+
+sub o {
+ if (defined $buffering_output) {
+ $buffering_output .= $_ foreach @_;
+ return;
+ }
+ die unless defined $writing_output;
+ print O @_ or die "error writing $writing_output.tmp: $!\n";
+}
+
+sub start_output_file ($) {
+ close_any_output_file();
+ ($writing_output) = @_;
+ die "$writing_output ?" if $output_files{$writing_output}++;
+ my $tmp = "$writing_output.tmp";
+ open O, ">", $tmp or die "create $tmp: $!\n";
+ o "# autogenerated - do not edit\n";
+}
+
+sub install_output_files () {
+ close_any_output_file();
+ foreach my $f (sort keys %output_files) {
+ rename "$f.tmp", $f or die "install new $f: $!\n";
+ }
+}
+
+sub write_makefile ($$) {
+ my ($dir_prefix,$depth) = @_;
+ #print STDERR "write_makefile @_\n";
+ start_output_file("${dir_prefix}Makefile");
+ my $cd = $depth ? join('/', ('..',) x $depth) : '.';
+ o <<END;
+default: all
+%: FORCE-ALWAYS-RUN
+ \@:
+Makefile FORCE-ALWAYS-RUN:
+ \$(MAKE) -C $cd -f main.mk \$(addprefix ${dir_prefix},\$(or \$(MAKECMDGOALS),all))
+.SUFFIXES:
+.PHONY: FORCE-ALWAYS-RUN
+END
+}
+
+sub process_input_mk ($$$$$$$$);
+sub process_input_mk ($$$$$$$$) {
+ my ($dir_prefix, $dir_suffix, $dir_name,
+ $var_prefix, $targets,
+ $f, $esclitr, $enoent_ok) = @_;
+
+ my $caps_re = qr{[A-Z]};
+ my $lc_re = qr{[a-z]};
+
+ my $esc;
+ my $set_esc = sub {
+ $esc = $$esclitr;
+ $esc =~ s/\W/\\$&/g;
+ };
+ $set_esc->();
+
+ my $input = new IO::File $f, '<';
+ if (!$input) {
+ die "open $f: $!\n" unless $!==ENOENT && $enoent_ok;
+ return;
+ }
+ $input_files{$f}++;
+
+ while (<$input>) {
+ if (s#^\s*$esc\:##) {
+ $buffering_output='';
+ }
+ for (;;) {
+ unless (s{^(.*?)(\\)?(?=$esc)}{}) { o $_; last; }
+ o $1;
+ if ($2) { o $$esclitr; next; }
+ s{^$esc}{} or die "$_ ?";
+ if (s{^$esc}{}) { o "$$esclitr$$esclitr" }
+ elsif (s{^TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) {
+ my $t = $1 // 'all';
+ o target_varname($var_prefix, $t);
+ $targets->{$t} //= [ ];
+ }
+ elsif (m{^(?=$caps_re)}) { o $var_prefix }
+ elsif (m{^(?=$lc_re)}) { o $dir_prefix }
+ elsif (s{^_}{}) { o $var_prefix }
+ elsif (s{^/}{}) { o $dir_prefix }
+ elsif (s{^=_}{}) { o $var_prefix }
+ elsif (s{^=/}{}) { o $dir_name }
+ elsif (s{^\^}{}) { o "\$(top_srcdir)${dir_suffix}" }
+ elsif (s{^\}}{}) { o "\$(abs_top_srcdir)${dir_suffix}" }
+ elsif (s{^(?:[ \t]+([~^]))?(?=[ \t])}{}) {
+ my $prefix =
+ !$1 ? $dir_prefix :
+ $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
+ $1 eq '~' ? '$(abs_top_srcdir)'.$dir_suffix :
+ die;
+ my $after='';
+ if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); }
+ s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g;
+ o $_;
+ $_ = $after;
+ } elsif (s{^\#}{}) {
+ $_ = '';
+ } elsif (s{^![ \t]+}{}) {
+ o $_;
+ $_ = '';
+ } elsif (s{^!(\S+)(?:[ \t]+|$)}{}) {
+ $$esclitr = $1;
+ $set_esc->();
+ } else {
+ die "bad escape $$esclitr$_ ";
+ }
+ }
+ if (defined $buffering_output) {
+ $_=$buffering_output;
+ $buffering_output=undef;
+ if (m#^(-?)include\s+(\S+)\s+$#) {
+ my $subf = "$srcdir/$2";
+ process_input_mk($dir_prefix, $dir_suffix, $dir_name,
+ $var_prefix, $targets,
+ $subf, $esclitr, $1);
+ o "\n";
+ } else {
+ die "unknown directive $_ ";
+ }
+ }
+ }
+ $input->error and die "read $f: $!\n";
+ close $input or die "close $f: $!\n";
+}
+
+sub filter_subdir_mk ($$$$$) {
+ my ($dir_prefix, $dir_suffix, $dir_name,
+ $var_prefix, $targets) = @_;
+
+ #use Data::Dumper;
+ #print STDERR "filter @_\n";
+
+ my $esclit = '&';
+
+ my $pi = sub {
+ my ($f, $enoentok) = @_;
+ process_input_mk($dir_prefix, $dir_suffix, $dir_name,
+ $var_prefix, $targets,
+ "${srcdir}/$f", \$esclit, $enoentok);
+ };
+ $pi->("${dir_prefix}Subdir.sd.mk", 0);
+ $pi->("Perdir.sd.mk", 1);
+}
+
+sub process_subtree ($$);
+sub process_subtree ($$) {
+ # => list of descendants (in form SUBDIR/)
+ # recursive, children first
+ my ($node, $path) = @_;
+
+ #use Data::Dumper;
+ #print STDERR Dumper(\@_);
+
+ my $dir_prefix = join '', map { "$_/" } @$path;
+ my $dir_suffix = join '', map { "/$_" } @$path;
+ my $dir_name = join '/', @$path ? @$path : '.';
+ my $var_prefix = join '', map { "${_}_" } @$path ? @$path : qw(TOP);
+
+ write_makefile($dir_prefix, scalar @$path);
+
+ my %targets = (all => []);
+ foreach my $child (@{ $node->[1] }) {
+ my @childpath = (@$path, $child->[0]);
+ my $child_subdir = join '/', @childpath;
+ mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!";
+ push @{ $targets{$_} }, $child_subdir foreach
+ process_subtree($child, \@childpath);
+ }
+ start_output_file("${dir_prefix}Subdir.mk.tmp");
+
+ filter_subdir_mk($dir_prefix, $dir_suffix, $dir_name,
+ $var_prefix, \%targets);
+
+ o "\n";
+
+ my @targets = sort keys %targets;
+ foreach my $target (@targets) {
+ my $target_varname = target_varname($var_prefix, $target);
+ print O "${dir_prefix}${target}:: \$($target_varname)";
+ foreach my $child_subdir (@{ $targets{$target} }) {
+ print O " $child_subdir/$target";
+ }
+ print O "\n";
+ }
+
+ return @targets;
+}
+
+sub process_tree() {
+ process_subtree($root, [ ]);
+ start_output_file("main.mk.tmp");
+ foreach my $v (qw(top_srcdir abs_top_srcdir)) {
+ o "$v=\@$v@\n";
+ }
+ o "MAKEFILES += Subdir.mk\n";
+ foreach my $subdir (@subdirs) {
+ o "MAKEFILES += $subdir/Subdir.mk\n";
+ }
+ foreach my $input (sort keys %input_files) {
+ o "MAKEFILE_TEMPLATES += $input\n";
+ }
+ o "include \$(MAKEFILES)";
+}
+
+build_tree();
+process_tree();
+install_output_files();
--- /dev/null
+# subdirmk - rules for regenerating makefiles etc.
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+# Usage:
+# include subdirmk/regen.mk
+# (probably in toplevel Subdir.sd.mk)
+#
+# Arranges that config.status is automatically rerun to update
+# makefiles from templates, whenever a template *.sd.mk or *.mk.in is
+# edited; and that autoconf is rerun if configure's inputs are edited.
+#
+# If you add includes to configure.ac, add them to CONFIGURE_ACS.
+#
+# Also provides a `realclean::' target at the toplevel which deletes
+# the autoconf output. This may be made into a recursive target
+# by mentioning &TARGETS_realclean in appropriate .sd.mk.
+
+CONFIGURE ?= configure
+CONFIGURE_AC ?= $(CONFIGURE).ac
+CONFIG_STATUS ?= config.status
+
+CONFIGURE_ACS += $(CONFIGURE_AC)
+CONFIGURE_ACS += subdirmk/subdirmk.ac
+
+$(top_srcdir)/$(CONFIGURE): $(addprefix $(top_srcdir)/,$(CONFIGURE_ACS))
+ cd $(top_srcdir) && autoconf
+
+$(CONFIG_STATUS): $(top_srcdir)/$(CONFIGURE)
+ ./$(CONFIG_STATUS) --recheck
+
+# Normally, generate will add all the inputs to MAKEFILE_TEMPLATES.
+MAKEFILE_TEMPLATES += $(addprefix $(top_srcdir)/, $(addsuffix .in, \
+ @_SUBDIRMK_MAKEFILES@ \
+ ))
+
+main.mk $(MAKEFILES): .makefiles.stamp ;
+.makefiles.stamp: \
+ $(top_srcdir)/subdirmk/generate \
+ $(CONFIG_STATUS) \
+ $(MAKEFILE_TEMPLATES)
+# This filtering arranges that we can often run config.status to
+# generate only particular output files. We look for *inputs* that
+# have changed. If the only inputs that have changed are ones that we
+# know affect only one output (Subdir.mk.in and regen.mk.in), we pass
+# config.status the corresponding output file names. Otherwise we
+# pass nothing and config.status does them all. We need to mention
+# regen.mk.in twice because if $(top_srcdir) is `.', make elides the
+# directory part from $?.
+ ./$(CONFIG_STATUS) $(if \
+ $(filter-out Subdir.sd.mk %/Subdir.sd.mk \
+ %.mk.in \
+ , $?),, \
+ $(patsubst $(top_srcdir)/%,%, $(sort \
+ $(patsubst %.sd.mk,%.mk,$(filter %.sd.mk,$?)) \
+ $(patsubst %.mk.in,%.mk,$(filter %.mk.in,$?)))))
+ touch $@
+
+realclean:: clean
+ $(RM) config.status config.log
+ $(RM) main.mk $(MAKEFILES) @_SUBDIRMK_MAKEFILES@
+ $(RM) $(addsuffix Makefile,$(dir $(MAKEFILES)))
+
+-include $(ALL_DEPFILES)
--- /dev/null
+dnl -*-autoconf-*-
+dnl subdirmk - autoconf macros
+dnl Copyright 2019 Mark Wooding
+dnl Copyright 2019 Ian Jackson
+dnl SPDX-License-Identifier: LGPL-2.0-or-later
+
+_SUBDIRMK_MAKEFILES=""
+AC_SUBST([_SUBDIRMK_MAKEFILES])
+
+AC_DEFUN([SUBDIRMK_SUBDIRS],
+[_SUBDIRMK_INIT
+m4_map_args_w([$1],[_SUBDIRMK_SUBDIR(],[/)])])dnl
+
+AC_DEFUN_ONCE([_SUBDIRMK_INIT],[
+ AC_CONFIG_FILES([
+ main.mk:main.mk.tmp
+ Subdir.mk:Subdir.mk.tmp
+ ],[],[
+ '$srcdir'/subdirmk/generate --srcdir='$srcdir' $subdirmk_subdirs
+ ])
+ SUBDIRMK_MAKEFILES([subdirmk/regen.mk subdirmk/usual.mk])
+])
+
+AC_DEFUN([_SUBDIRMK_SUBDIR],[
+ subdirmk_subdirs="$subdirmk_subdirs '$1'"
+ AC_CONFIG_FILES([$1Subdir.mk:$1Subdir.mk.tmp])
+])
+
+AC_DEFUN([SUBDIRMK_MAKEFILES],
+[_SUBDIRMK_INIT
+m4_map_args_w([$1],[_SUBDIRMK_MAKEFILE(],[)])])dnl
+
+AC_DEFUN([_SUBDIRMK_MAKEFILE],[
+ _SUBDIRMK_MAKEFILES="$_SUBDIRMK_MAKEFILES $1"
+ AC_CONFIG_FILES([$1:$1.in])
+])
--- /dev/null
+#!/bin/sh
+set -ex
+
+cd example
+git clean -xdff
+./autogen.sh && ./configure
+make -j4 all check
+make -j4 clean
+make -j4 all check
+
+git clean -xdff
+mkdir build
+cd build
+>>../src/for-test.sd.mk
+>>../lib/for-test.mk.in
+.././autogen.sh && ../configure
+make -j4 all check
+
+echo '# for-check 1' >>../src/for-test.sd.mk
+make -j4
+grep '^# for-check 1' src/Subdir.mk || false
+
+echo '# for-check 2' >>../lib/for-test.mk.in
+make -j4
+grep '^# for-check 2' lib/for-test.mk || false
+
+echo ok.
--- /dev/null
+# subdirmk - usual variable settings
+# Copyright 2019 Mark Wooding
+# Copyright 2019 Ian Jackson
+# SPDX-License-Identifier: LGPL-2.0-or-later
+
+# Usage:
+# include subdirmk/usual.mk
+# (probably in toplevel Subdir.sd.mk)
+#
+# Provides various conventional `make' variables, and a
+# rule for compiling C programs.
+
+VPATH = $(top_srcdir)
+
+prefix = @prefix@
+exec_prefix = @exec_prefix@
+bindir = @bindir@
+
+CC ?= @CC@
+CFLAGS ?= @CFLAGS@
+DEFS ?= @DEFS@
+INCLUDES ?= @INCLUDES@
+LD ?= @CC@
+LDFLAGS ?= @LDFLAGS@
+LIBS ?= @LIBS@
+
+LINK ?= $(CC) -o$@ $(CFLAGS) $(LDFLAGS)
+AR ?= ar
+COMPILE ?= $(CC) -c -o$@ $(CDEPS_CFLAGS) $(DEFS) $(INCLUDES) $(CFLAGS)
+
+%.o: %.c
+ $(COMPILE) $<