chiark / gitweb /
WIP
[subdirmk.git] / Makefile.in
1 ### -*-makefile-gmake-*-
2 ###
3 ### Top-level build script for toy project.
4 ###
5 ### (c) 2019 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This program is free software; you can redistribute it and/or modify
11 ### it under the terms of the GNU Library General Public License as
12 ### published by the Free Software Foundation; either version 2 of the
13 ### License, or (at your option) any later version.
14 ###
15 ### This program is distributed in the hope that it will be useful,
16 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ### GNU Library General Public License for more details.
19 ###
20 ### You should have received a copy of the GNU Library General Public
21 ### License along with this program; if not, write to the Free
22 ### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 ### MA 02111-1307, USA.
24
25 ## Default target.
26 all::
27
28 ###--------------------------------------------------------------------------
29 ### Configuration things.
30
31 ## (This is a toy.  More stuff is needed in a proper project.)
32
33 ## Source and build directories.
34 srcdir                   = @srcdir@
35 abs_srcdir               = @abs_srcdir@
36 abs_builddir             = @abs_builddir@
37
38 ## Installation directories.
39 prefix                   = @prefix@
40 exec_prefix              = @exec_prefix@
41 bindir                   = @bindir@
42
43 ## Build options.
44 CC                       = @CC@
45 CFLAGS                   = @CFLAGS@
46 DEFS                     = @DEFS@
47 INCLUDES                 = @INCLUDES@
48 LD                       = @CC@
49 LDFLAGS                  = @LDFLAGS@
50 LIBS                     = @LIBS@
51
52 INCLUDES                += -I$(srcdir)/lib/
53
54 ###--------------------------------------------------------------------------
55 ### Machinery.
56
57 dirvars                  =
58 dirtargets               =
59 ALL_SUBDIRS             :=
60 D                       :=
61 %dirstack               :=
62
63 dirvars                 += TARGETS PROGRAMS LIBS CLEANFILES
64 dirtargets              += all clean check install
65
66 define %descend-subdir
67   %dirstack             := $$D $$(%dirstack)
68   D                     := $$(if $$D,$$D/)$1
69   ALL_SUBDIRS           += $$D
70   include $$(srcdir)/$$D/Subdir.mk
71   D                     := $$(firstword $$(dirstack))
72   %dirstack             := $$(wordlist 2,$$(words $$(%dirstack)),$$(%dirstack))
73 endef
74 descend-subdirs          = $(foreach d,$1, \
75                                 $(eval $(call %descend-subdir,$d)))
76
77 V                        = 0
78 v-tag                    = $(call %v-tag.$V,$1)
79 %v-tag.0                 = @printf "  %-8s %s\n" "$1" "$@";
80 %v-tag.1                 =
81
82 V_AT                     = $(%V_AT.$V)
83 %V_AT.0                  = @
84 %V_AT.1                  =
85
86 VPATH                    = $(srcdir)
87
88 COMPILE                  = $(call v-tag,CC)$(CC) -c -o$@ -MD \
89                                 $(DEFS) $(INCLUDES) $(CFLAGS)
90 %.o: %.c
91         $(COMPILE) $<
92
93 LINK                     = $(call v-tag,LD)$(LD) -o$@ \
94                                 $(CFLAGS) $(LDFLAGS) $(LIBS)
95
96 objects                  = $(addsuffix $(if $2,$2,.o), \
97                                 $(basename $(filter %.c %.s %.S,$1)))
98
99 ALL_DEPFILES            :=
100 notice-objects           = $(eval ALL_DEPFILES += $$(patsubst %.o,%.d,$1))
101
102 ###--------------------------------------------------------------------------
103 ### Descend into subdirectories.
104
105 SUBDIRS                  =
106
107 SUBDIRS                 += lib
108 SUBDIRS                 += src
109
110 $(call descend-subdirs, $(SUBDIRS))
111
112 ###--------------------------------------------------------------------------
113 ### Maintaining the build system.
114
115 $(srcdir)/configure: $(srcdir)/configure.ac
116         $(call v-tag,AUTOCONF)cd $(srcdir) && autoconf
117
118 config.status: $(srcdir)/configure
119         $(call v-tag,CONFIG)./config.status --recheck
120
121 Makefile: config.status $(srcdir)/Makefile.in \
122                 $(foreach d,$(ALL_SUBDIRS), $d/Makefile)
123         $(call v-tag,SUBST)./config.status Makefile
124 $(foreach d,$(ALL_SUBDIRS), \
125         $(eval $d/Makefile: config.status $$(srcdir)/$d/Makefile.in; \
126                 $$(call v-tag,SUBST)./config.status $$@))
127
128 ###--------------------------------------------------------------------------
129 ### More machinery.
130
131 all_dirtargets           = $(foreach d,$(ALL_SUBDIRS), \
132                                 $(foreach t,$(dirtargets), $d/$t))
133
134 $(all_dirtargets)::
135
136 ifeq ($(origin SUBDIR),undefined)
137 $(foreach t,$(dirtargets), \
138         $(eval $t:: $(foreach d,$(ALL_SUBDIRS),$d/$t)))
139 else
140 $(foreach t,$(dirtargets), \
141         $(eval $t:: $(SUBDIR)/$t))
142 endif
143 $(foreach d,$(SUBDIRS), \
144         $(foreach t,$(dirtargets), \
145                 $(eval $d/$t::)))
146 .PHONY: $(foreach t,$(dirtargets),$t $(foreach d,$(ALL_SUBDIRS),$d/$t))
147
148 $(foreach d,$(ALL_SUBDIRS), $d/clean):: %/clean:
149         rm -f $*/*.o $*/*.d $($*_CLEANFILES)
150
151 realclean:: clean
152         rm -f config.status config.log
153         rm -f Makefile $(foreach d,$(ALL_SUBDIRS), $d/Makefile)
154
155 -include $(ALL_DEPFILES)
156
157 ###----- That's all, folks --------------------------------------------------