1 ### -*-makefile-gmake-*-
3 ### Top-level build script for toy project.
5 ### (c) 2019 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
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.
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.
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.
28 ###--------------------------------------------------------------------------
29 ### Configuration things.
31 ## (This is a toy. More stuff is needed in a proper project.)
33 ## Source and build directories.
35 abs_srcdir = @abs_srcdir@
36 abs_builddir = @abs_builddir@
38 ## Installation directories.
40 exec_prefix = @exec_prefix@
52 INCLUDES += -I$(srcdir)/lib/
54 ###--------------------------------------------------------------------------
63 dirvars += TARGETS PROGRAMS LIBS CLEANFILES
64 dirtargets += all clean check install
66 define %descend-subdir
67 %dirstack := $$D $$(%dirstack)
68 D := $$(if $$D,$$D/)$1
70 include $$(srcdir)/$$D/Subdir.mk
71 D := $$(firstword $$(dirstack))
72 %dirstack := $$(wordlist 2,$$(words $$(%dirstack)),$$(%dirstack))
74 descend-subdirs = $(foreach d,$1, \
75 $(eval $(call %descend-subdir,$d)))
78 v-tag = $(call %v-tag.$V,$1)
79 %v-tag.0 = @printf " %-8s %s\n" "$1" "$@";
88 COMPILE = $(call v-tag,CC)$(CC) -c -o$@ -MD \
89 $(DEFS) $(INCLUDES) $(CFLAGS)
93 LINK = $(call v-tag,LD)$(LD) -o$@ \
94 $(CFLAGS) $(LDFLAGS) $(LIBS)
96 objects = $(addsuffix $(if $2,$2,.o), \
97 $(basename $(filter %.c %.s %.S,$1)))
100 notice-objects = $(eval ALL_DEPFILES += $$(patsubst %.o,%.d,$1))
102 ###--------------------------------------------------------------------------
103 ### Descend into subdirectories.
110 $(call descend-subdirs, $(SUBDIRS))
112 ###--------------------------------------------------------------------------
113 ### Maintaining the build system.
115 $(srcdir)/configure: $(srcdir)/configure.ac
116 $(call v-tag,AUTOCONF)cd $(srcdir) && autoconf
118 config.status: $(srcdir)/configure
119 $(call v-tag,CONFIG)./config.status --recheck
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 $$@))
128 ###--------------------------------------------------------------------------
131 all_dirtargets = $(foreach d,$(ALL_SUBDIRS), \
132 $(foreach t,$(dirtargets), $d/$t))
136 ifeq ($(origin SUBDIR),undefined)
137 $(foreach t,$(dirtargets), \
138 $(eval $t:: $(foreach d,$(ALL_SUBDIRS),$d/$t)))
140 $(foreach t,$(dirtargets), \
141 $(eval $t:: $(SUBDIR)/$t))
143 $(foreach d,$(SUBDIRS), \
144 $(foreach t,$(dirtargets), \
146 .PHONY: $(foreach t,$(dirtargets),$t $(foreach d,$(ALL_SUBDIRS),$d/$t))
148 $(foreach d,$(ALL_SUBDIRS), $d/clean):: %/clean:
149 rm -f $*/*.o $*/*.d $($*_CLEANFILES)
152 rm -f config.status config.log
153 rm -f Makefile $(foreach d,$(ALL_SUBDIRS), $d/Makefile)
155 -include $(ALL_DEPFILES)
157 ###----- That's all, folks --------------------------------------------------