4e4be6fd414b229e2aad4cbb43f7a68b6e30e684
[apps/pfixtools.git] / mk / common.mk
1 include ../mk/cflags.mk
2
3 prefix ?= /usr/local
4 LDFLAGS += -L/opt/local/lib
5 CFLAGS  += --std=gnu99 -I../ -I../common -I/opt/local/include
6
7 INSTALL_PROGS = $(addprefix install-,$(PROGRAMS))
8
9 all: $(GENERATED) $(LIBS) $(PROGRAMS) | $(GENERATED)
10
11 install: all $(INSTALL_PROGS)
12
13 $(INSTALL_PROGS): install-%:
14         install $* $(DESTDIR)$(prefix)/sbin
15
16 clean:
17         $(RM) $(LIBS:=.a) $(PROGRAMS) $(TESTS) .*.o .*.dep
18
19 distclean: clean
20         $(RM) $(GENERATED)
21
22 tags: .tags
23 .tags: $(shell git ls-files | egrep '\.[hc]$$')
24         ctags -o $@ $^
25
26 headers: HEADACHEOPTS=-c mk/headache.cfg -h mk/COPYING
27 headers:
28         @which headache > /dev/null || \
29                 ( echo "package headache not installed" ; exit 1 )
30         @git ls-files | egrep '(\.h|\.c|Makefile|*\.mk)$$' | xargs -t headache $(HEADACHEOPTS)
31
32 %.h: %.sh
33         ./$< $@ || ($(RM) $@; exit 1)
34
35 %.c: %.sh
36         ./$< $@ || ($(RM) $@; exit 1)
37
38 .%.o: %.c Makefile
39         $(shell test -d $(@D) || mkdir -p $(@D))
40         $(CC) $(CFLAGS) -MMD -MT ".$*.dep $@" -MF .$*.dep -g -c -o $@ $<
41
42 .%.dep: .%.o
43
44 $(LIBS): %: %.a
45
46 .SECONDEXPANSION:
47
48 $(LIBS:=.a): $$(patsubst %.c,.%.o,$$($$(patsubst %.a,%,$$@)_SOURCES)) Makefile
49         $(RM) $@
50         $(AR) rcs $@ $(filter %.o,$^)
51
52 $(PROGRAMS) $(TESTS): $$(patsubst %.c,.%.o,$$($$@_SOURCES)) Makefile
53         $(CC) -o $@ $(filter %.o,$^) $(LDFLAGS) $($@_LIBADD) $(filter %.a,$^)
54
55 -include $(foreach p,$(PROGRAMS) $(TESTS),$(patsubst %.c,.%.dep,$(filter %.c,$($p_SOURCES))))
56
57 .PHONY: install-dir $(INSTALL_PROGS)