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 install-LIBRARIES/%::
35         mkdir -p $($*dir)
36         cp $($*_LIBRARIES:=.so) $($*dir)
37
38 uninstall-LIBRARIES/%::
39         rm -f $(patsubst %,$($*dir)/%.so,$($*_LIBRARIES))
40         rmdir -p $($*dir) || true 2> /dev/null
41
42 define CLASS_LIBRARIES_tpl
43 all:: $1.so
44 install-LIBRARIES/$2:: $1.so
45
46 install:: install-LIBRARIES/$2
47 uninstall:: uninstall-LIBRARIES/$2
48
49 $1.so: $(_cobjs)/$1/.exists $$(call deps_create,$$($1_SOURCES),$(_cobjs)/$1)
50         $(CC) $$(CFLAGS) $$($1_CPPFLAGS) -fPIC -shared -o $$@ \
51             $$(filter %.o,$$^) $$(LDFLAGS) $$($1_LDFLAGS) $$($1_LIBADD)
52
53 $$(eval $$(call RULE_C_tpl,$1,-fPIC))
54
55 clean::
56         $(RM) $1.so
57 endef
58 $(foreach v,$(filter %_LIBRARIES,$(.VARIABLES)), \
59             $(foreach x,$($v),$(eval $(call CLASS_LIBRARIES_tpl,$x,$(v:_LIBRARIES=)))))
60
61
62 install-DATA/%::
63         mkdir -p $($*dir)
64         cp $($*_DATA) $($*dir)
65
66 uninstall-DATA/%::
67         rm -f $(patsubst %,$($*dir)/%,$($*_DATA))
68         rmdir -p $($*dir) || true 2> /dev/null
69
70 define CLASS_DATA_tpl
71 install:: install-DATA/$2
72 uninstall:: uninstall-DATA/$2
73 endef
74 $(foreach v,$(filter %_DATA,$(.VARIABLES)), \
75             $(foreach x,$($v),$(eval $(call CLASS_DATA_tpl,$x,$(v:_DATA=)))))
76
77 #define CLASS_EXE_tpl
78 #all:: $1
79 #$1: $(_cobjs)/$1/.exists $$(call deps_create,$$($1_SOURCES),$(_cobjs)/$1)
80 #       $(CC) -o $$@ $$(CFLAGS) $$($1_CPPFLAGS) $$(filter %.o,$$^) \
81 #                $$(LDFLAGS) $$($1_LDFLAGS) $$(filter %.a,$$^)
82 #
83 #$$(eval $$(call RULE_C_tpl,$1,$2))
84 #
85 #clean::
86 #       $(RM) $1
87 #endef
88 #$(foreach x,$(__EXE__),$(eval $(call CLASS_EXE_tpl,$x,)))
89
90 ########################################################################}}}#
91 #[ Candy ]##############################################################{{{#
92
93 echo-root:
94         @echo $(DEPTH)
95
96 echo-tags:
97         @echo $(DEPTH)/.tags
98
99 ifeq (".","$(DEPTH)")
100 tags:
101         ctags -o .$@ --recurse=yes --exclude=.svn --exclude=\*.mk --totals=yes;
102
103 distclean clober::
104         $(RM) -f .tags
105 else
106 tags:
107         $(MAKE) -C $(DEPTH) $@
108 endif
109
110 .PHONY: tags
111
112 ########################################################################}}}#
113
114 all check clobber distclean::
115         @set -e $(patsubst %,; $(MAKE) -rC % $@,$(SUBDIRS))
116
117 CLEAN_RECURSE=1
118 clean::
119         $(RM) -r $(_cobjs) $(CLEANFILES)
120         @$(if $(CLEAN_RECURSE),$(patsubst %,$(MAKE) -rC % $@;,$(SUBDIRS)))
121
122 clobber distclean:: CLEAN_RECURSE=
123 clobber distclean:: clean
124         $(RM) -r .objs* $(DISTCLEANFILES)
125
126 ifeq (".","$(DEPTH)")
127 clobber distclean::
128         $(RM) $(patsubst %.in,%,$(wildcard $(CONFDIR)/*.in))
129 endif
130
131 .PHONY: all check clean clobber distclean install FORCE