chiark / gitweb /
11e8a32f8e725547f58d6c183839320f53f04d03
[subdirmk.git] / regen.mk.in
1 # subdirmk - rules for regenerating makefiles etc.
2 #  Copyright 2019 Mark Wooding
3 #  Copyright 2019 Ian Jackson
4 # SPDX-License-Identifier: LGPL-2.0-or-later
5
6 # Usage:
7 #   include subdirmk/regen.mk
8 # (probably in toplevel Subdir.sd.mk)
9 #
10 # Arranges that config.status is automatically rerun to update
11 # makefiles from templates, whenever a template *.sd.mk or *.mk.in is
12 # edited; and that autoconf is rerun if configure's inputs are edited.
13 #
14 # If you add includes to configure.ac, add them to CONFIGURE_ACS.
15 #
16 # Also provides a `realclean::' target at the toplevel which deletes
17 # the autoconf output.  This may be made into a recursive target
18 # by mentioning &TARGETS_realclean in appropriate .sd.mk.
19
20 CONFIGURE       ?= configure
21 CONFIGURE_AC    ?= $(CONFIGURE).ac
22 CONFIG_STATUS   ?= config.status
23
24 CONFIGURE_ACS   += $(CONFIGURE_AC)
25 CONFIGURE_ACS   += subdirmk/subdirmk.ac
26
27 # To turn on debugging here, export SUBDIRMK_REGEN_NDEBUG=''
28 SUBDIRMK_REGEN_NDEBUG ?= @
29
30 $(top_srcdir)/$(CONFIGURE): $(addprefix $(top_srcdir)/,$(CONFIGURE_ACS))
31         cd $(top_srcdir) && autoconf
32
33 $(CONFIG_STATUS): $(top_srcdir)/$(CONFIGURE)
34         ./$(CONFIG_STATUS) --recheck
35
36 # generate will add all its own inputs and outputs to these variables
37 SUBDIRMK_MAKEFILES += @_SUBDIRMK_MAKEFILES@
38 MAKEFILE_TEMPLATES += $(addprefix $(top_srcdir)/, $(addsuffix .in, \
39         @_SUBDIRMK_MAKEFILES@ \
40         ))
41
42 main.mk $(SUBDIRMK_MAKEFILES) $(CONFIG_STATUS_OUTPUTS): .makefiles.stamp
43         $(SUBDIRMK_REGEN_NDEBUG): REGEN STAMP CAUSES TARGET=$@
44
45 .makefiles.stamp:                                               \
46                 $(top_srcdir)/subdirmk/generate                 \
47                 $(CONFIG_STATUS)                                \
48                 $(MAKEFILE_TEMPLATES)
49 # This filtering arranges that we can often run config.status to
50 # generate only particular output files.  We look for *inputs* that
51 # have changed.  If the only inputs that have changed are ones that we
52 # know affect only one output (Subdir.sd.mk and *.mk.in), we pass
53 # config.status the corresponding output file names.  Otherwise we
54 # pass nothing and config.status does them all.  We need to mention
55 # Subdir.sd.mk twice because if $(top_srcdir) is `.', make elides the
56 # directory part from $?.
57         $(SUBDIRMK_REGEN_NDEBUG): REGEN STAMP WANTS DEPS=$?
58         ./$(CONFIG_STATUS) $(if                                 \
59                 $(filter-out Subdir.sd.mk %/Subdir.sd.mk        \
60                              %.mk.in                            \
61                         , $?),,                                 \
62                 $(patsubst $(top_srcdir)/%,%, $(sort            \
63                         $(patsubst %.sd.mk,%.mk,$(filter %.sd.mk,$?)) \
64                         $(patsubst %.mk.in,%.mk,$(filter %.mk.in,$?)))))
65         touch $@
66
67 realclean:: clean
68         $(RM) config.status config.log
69         $(RM) main.mk $(SUBDIRMK_MAKEFILES) @_SUBDIRMK_MAKEFILES@
70         $(RM) $(addsuffix Makefile,$(dir $(SUBDIRMK_MAKEFILES)))
71
72 -include $(ALL_DEPFILES)