summaryrefslogtreecommitdiff
path: root/mk/c.mk
blob: a98a5c2ebea74420e1953e5a4df363b89b63d205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
.DEFAULT_GOAL=all
_DEPSDIR = .deps
_DEPS=$(addprefix $(_DEPSDIR)/, $(OBJ:.o=.d))

_LIBSRCHPATHS=$(addprefix -L, $(dir $(DEPS)))
_LIBINCPATHS=$(addprefix -I, $(dir $(DEPS)))
_LIBPATHS=$(addprefix -l, $(patsubst lib%.a,%,$(notdir $(DEPS))))

CFLAGS += -Wall -Werror -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
CFLAGS += -g
CFLAGS += -MMD -MP -MF ${_DEPSDIR}/$(subst /,-,$*).d

.PHONY: clean clean-gen clean-bin clean-obj clean-misc clean-backups
.PHONY: all

all: subdirs $(BIN) $(LIB) $(EXTRA)
install: subdirs-install install-bin install-lib install-hdr install-pc

$(LIB): $(OBJ) $(DEPS)
	$(AR) -rcs $@ $(OBJ)

$(BIN): $(OBJ) $(EXTRADEP) $(DEPS)
	$(CC) -o $@ $(OBJ) $(_LIBSRCHPATHS) $(_LIBPATHS)

$(DEPS):
	@cd $(dir $@) && $(MAKE)

subdirs:
	@for i in $(SUB); do (\
	    cd $$i && \
	    $(MAKE) || \
	    exit 1 \
	) || exit 1; done

subdirs-clean:
	@for i in $(SUB); do (\
	    cd $$i && \
	    $(MAKE) clean|| \
	    exit 1 \
	); done

subdirs-install:
	@for i in $(SUB); do (\
	    cd $$i && \
	    $(MAKE) install|| \
	    exit 1 \
	); done


clean: subdirs-clean 
	rm -f ${BIN} ${OBJ} ${CLEAN}


install-bin: $(INSTBIN)
	@if [ ! -z "$(INSTBIN)" ]; then \
		echo install $(INSTBIN) $(INST_ROOT)/bin; \
		mkdir -p $(INST_ROOT)/bin; \
		install $(INSTBIN) $(INST_ROOT)/bin; \
	fi

install-lib: $(INSTLIB)
	@if [ ! -z "$(INSTLIB)" ]; then \
		echo install -m 644 $(INSTLIB) $(INST_ROOT)/lib; \
		mkdir -p $(INST_ROOT)/lib; \
		install -m 644 $(INSTLIB) $(INST_ROOT)/lib; \
	fi

install-hdr: $(INSTHDR)
	@if [ ! -z "$(INSTHDR)" ]; then \
		echo install $(INSTHDR) $(INST_ROOT)/include; \
		mkdir -p $(INST_ROOT)/include; \
		install $(INSTHDR) $(INST_ROOT)/include; \
	fi

install-pc: $(INSTPKG)
	@if [ ! -z "$(INSTPKG)" ]; then \
		echo install $(INSTPKG) $(INST_ROOT)/lib/pkgconfig; \
		mkdir -p $(INST_ROOT)/lib/pkgconfig; \
		install $(INSTPKG) $(INST_ROOT)/lib/pkgconfig; \
	fi

clean-backups:
	find ./ -name .*.sw* -exec rm -f {} \;
	find ./ -name *.bak -exec rm -f {} \;

%.o: %.c .deps
	$(CC) -c $(CFLAGS) $(_LIBINCPATHS) $<

.deps: 
	mkdir -p $(_DEPSDIR)

config.mk: configure
	./configure

-include $(_DEPS)