make the build system even better.
[apps/madtty.git] / am / footer.mk
1 #[ Rules and Depends ]##################################################{{{#
2
3 AUTO_DEPENDS += filter_c
4 filter_c = $(addprefix $2/,$(patsubst %.c,%.o,$(filter %.c,$1)))
5
6 # this is some ad-hoc last resort dependency method
7 %.a: FORCE
8         @$(MAKE) -q -rC $(dir $(@D)) $(_cobjs)/$(@F) || $(MAKE) -rC $(dir $(@D)) $(_cobjs)/$(@F)
9
10 # this implicit rule is used to force directory existence, without depending
11 # on the directory itself, since its mtime changes too often.
12 %/.exists: Makefile $(CONFDIR)/$(wildcard *.mk)
13         @$(RM) -r $*
14         @-mkdir -p $*
15         @touch $@
16 # We don't want make to remove these files because of default rules.
17 .PRECIOUS: %/.exists
18
19 define RULE_C_tpl
20 $(_cobjs)/$1/%.o: %.c $(_cobjs)/$1/.exists
21         $(CC) $$(CFLAGS) $$($1_CPPFLAGS) $2 -MMD -MT "$$(@D)/$$*.d $$(@D)/$$*.o" -MF $$(@D)/$$*.d -g -c -o $$@ $$<
22
23 $(_cobjs)/$1/%.d: %.c $(_cobjs)/$1/.exists
24         @$(CPP) $$(CFLAGS) $$($1_CPPFLAGS) $2 -MM -MT "$$(@D)/$$*.d $$(@D)/$$*.o" -MF $$(@D)/$$*.d $$<
25
26 -include $$(wildcard $(_cobjs)/$1/*.d) /dev/null
27 endef
28
29 ########################################################################}}}#
30 #[ Classes ]############################################################{{{#
31
32 deps_create = $(foreach f,$(AUTO_DEPENDS),$(call $f,$1,$2))
33
34 define CLASS_LIBRARIES_tpl
35 all:: $1.so
36
37 $1.so: $(_cobjs)/$1/.exists $$(call deps_create,$$($1_SOURCES),$(_cobjs)/$1)
38         $(CC) $$(CFLAGS) $$($1_CPPFLAGS) -fPIC -shared -o $$@ \
39             $$(filter %.o,$$^) $$(LDFLAGS) $$($1_LDFLAGS) $$($1_LIBADD)
40
41 $$(eval $$(call RULE_C_tpl,$1,-fPIC $2))
42
43 clean::
44         $(RM) $1.so
45 endef
46 $(foreach l, \
47     $(foreach l,$(filter %_LIBRARIES,$(.VARIABLES)),$($l)), \
48     $(eval $(call CLASS_LIBRARIES_tpl,$l,)))
49
50 #define CLASS_EXE_tpl
51 #all:: $1
52 #$1: $(_cobjs)/$1/.exists $$(call deps_create,$$($1_SOURCES),$(_cobjs)/$1)
53 #       $(CC) -o $$@ $$(CFLAGS) $$($1_CPPFLAGS) $$(filter %.o,$$^) \
54 #                $$(LDFLAGS) $$($1_LDFLAGS) $$(filter %.a,$$^)
55 #
56 #$$(eval $$(call RULE_C_tpl,$1,$2))
57 #
58 #clean::
59 #       $(RM) $1
60 #endef
61 #$(foreach x,$(__EXE__),$(eval $(call CLASS_EXE_tpl,$x,)))
62
63 ########################################################################}}}#
64 #[ Candy ]##############################################################{{{#
65
66 echo-root:
67         @echo $(DEPTH)
68
69 echo-tags:
70         @echo $(DEPTH)/.tags
71
72 ifeq (".","$(DEPTH)")
73 tags:
74         ctags -o .$@ --recurse=yes --exclude=.svn --exclude=\*.mk --totals=yes;
75
76 distclean clober::
77         $(RM) -f .tags
78 else
79 tags:
80         $(MAKE) -C $(DEPTH) $@
81 endif
82
83 .PHONY: tags
84
85 ########################################################################}}}#
86
87 all check clobber distclean::
88         @set -e $(patsubst %,; $(MAKE) -rC % $@,$(SUBDIRS))
89
90 CLEAN_RECURSE=1
91 clean::
92         $(RM) -r $(_cobjs)
93         @$(if $(CLEAN_RECURSE),$(patsubst %,$(MAKE) -rC % $@;,$(SUBDIRS)))
94
95 clobber distclean:: CLEAN_RECURSE=
96 clobber distclean:: clean
97         $(RM) -r .objs* $(patsubst %.in,%,$(wildcard *.in))
98
99 ifeq (".","$(DEPTH)")
100 clobber distclean::
101         $(RM) $(patsubst %.in,%,$(wildcard $(CONFDIR)/*.in))
102 endif
103
104 .PHONY: all check clean clobber distclean FORCE