1proj := qrtr
2proj-major := 1
3proj-minor := 0
4proj-version := $(proj-major).$(proj-minor)
5
6CFLAGS := -Wall -g
7LDFLAGS :=
8
9prefix := /usr/local
10bindir := $(prefix)/bin
11libdir := $(prefix)/lib
12includedir := $(prefix)/include
13servicedir := $(prefix)/lib/systemd/system
14
15ifneq ($(CROSS_COMPILE),)
16CC := $(CROSS_COMPILE)gcc
17endif
18SFLAGS := -I$(shell $(CC) -print-file-name=include) -Wno-non-pointer-null
19
20$(proj)-cfg-srcs := \
21	lib/logging.c \
22	src/addr.c \
23	src/cfg.c \
24
25$(proj)-cfg-cflags := -Ilib
26
27$(proj)-ns-srcs := \
28	lib/logging.c \
29	src/addr.c \
30	src/ns.c \
31	src/map.c \
32	src/hash.c \
33	src/waiter.c \
34	src/util.c \
35
36$(proj)-ns-cflags := -Ilib
37
38$(proj)-lookup-srcs := \
39	lib/logging.c \
40	src/lookup.c \
41	src/util.c \
42
43$(proj)-lookup-cflags := -Ilib
44
45lib$(proj).so-srcs := \
46	lib/logging.c \
47	lib/qrtr.c \
48	lib/qmi.c
49
50lib$(proj).so-cflags := -fPIC -Isrc
51
52targets := $(proj)-ns $(proj)-cfg $(proj)-lookup lib$(proj).so
53
54out := out
55src_to_obj = $(patsubst %.c,$(out)/obj/%.o,$(1))
56src_to_dep = $(patsubst %.c,$(out)/dep/%.d,$(1))
57
58all-srcs :=
59all-objs :=
60all-deps :=
61all-clean := $(out)
62all-install :=
63
64all: $(targets)
65
66$(out)/obj/%.o: %.c
67ifneq ($C,)
68	@echo "CHECK	$<"
69	@sparse $< $(patsubst -iquote=%,-I%,$(CFLAGS)) $(SFLAGS)
70endif
71	@echo "CC	$<"
72	@$(CC) -MM -MF $(call src_to_dep,$<) -MP -MT "$@ $(call src_to_dep,$<)" $(CFLAGS) $(_CFLAGS) $<
73	@$(CC) -o $@ -c $< $(CFLAGS) $(_CFLAGS)
74
75define add-inc-target
76$(DESTDIR)$(includedir)/$2: $1/$2
77	@echo "INSTALL	$$<"
78	@install -D -m 755 $$< $$@
79
80all-install += $(DESTDIR)$(includedir)/$2
81endef
82
83define add-target-deps
84all-srcs += $($1-srcs)
85all-objs += $(call src_to_obj,$($1-srcs))
86all-deps += $(call src_to_dep,$($1-srcs))
87all-clean += $1
88$(call src_to_obj,$($1-srcs)): _CFLAGS := $($1-cflags)
89endef
90
91define add-bin-target
92
93$(call add-target-deps,$1)
94
95$1: $(call src_to_obj,$($1-srcs))
96	@echo "LD	$$@"
97	$$(CC) -o $$@ $$(filter %.o,$$^) $(LDFLAGS)
98
99$(DESTDIR)$(bindir)/$1: $1
100	@echo "INSTALL	$$<"
101	@install -D -m 755 $$< $$@
102
103all-install += $(DESTDIR)$(bindir)/$1
104endef
105
106define add-lib-target
107
108$(call add-target-deps,$1)
109
110$1: $(call src_to_obj,$($1-srcs))
111	@echo "LD	$$@"
112	$$(CC) -o $$@ $$(filter %.o,$$^) $(LDFLAGS) -shared -Wl,-soname,$1.$(proj-major)
113
114$(DESTDIR)$(libdir)/$1.$(proj-version): $1
115	@echo "INSTALL	$$<"
116	@install -D -m 755 $$< $$@
117	@ln -sf $1.$(proj-version) $(DESTDIR)$(libdir)/$1.$(proj-major)
118	@ln -sf $1.$(proj-major) $(DESTDIR)$(libdir)/$1
119
120all-install += $(DESTDIR)$(libdir)/$1.$(proj-version)
121endef
122
123define add-systemd-service-target
124$1: $1.in
125	sed 's+QRTR_NS_PATH+$(bindir)+g' $$< > $$@
126
127$(DESTDIR)$(servicedir)/$1: $1
128	@echo "INSTALL	$$<"
129	@install -D -m 755 $$< $$@
130
131all-install += $(DESTDIR)$(servicedir)/$1
132endef
133
134$(foreach v,$(filter-out %.so,$(targets)),$(eval $(call add-bin-target,$v)))
135$(foreach v,$(filter %.so,$(targets)),$(eval $(call add-lib-target,$v)))
136$(eval $(call add-inc-target,lib,libqrtr.h))
137$(eval $(call add-systemd-service-target,qrtr-ns.service))
138
139install: $(all-install)
140
141clean:
142	@echo CLEAN
143	@$(RM) -r $(all-clean)
144
145$(call src_to_obj,$(all-srcs)): Makefile
146
147ifneq ("$(MAKECMDGOALS)","clean")
148cmd-goal-1 := $(shell mkdir -p $(sort $(dir $(all-objs) $(all-deps))))
149-include $(all-deps)
150endif
151