Amit Pundir | 33109f7 | 2020-02-07 22:26:08 +0530 | [diff] [blame] | 1 | proj := qrtr |
| 2 | proj-major := 1 |
| 3 | proj-minor := 0 |
| 4 | proj-version := $(proj-major).$(proj-minor) |
| 5 | |
| 6 | CFLAGS := -Wall -g |
| 7 | LDFLAGS := |
| 8 | |
| 9 | prefix := /usr/local |
| 10 | bindir := $(prefix)/bin |
| 11 | libdir := $(prefix)/lib |
| 12 | includedir := $(prefix)/include |
| 13 | servicedir := $(prefix)/lib/systemd/system |
| 14 | |
| 15 | ifneq ($(CROSS_COMPILE),) |
| 16 | CC := $(CROSS_COMPILE)gcc |
| 17 | endif |
| 18 | SFLAGS := -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 | |
| 45 | lib$(proj).so-srcs := \ |
| 46 | lib/logging.c \ |
| 47 | lib/qrtr.c \ |
| 48 | lib/qmi.c |
| 49 | |
| 50 | lib$(proj).so-cflags := -fPIC -Isrc |
| 51 | |
| 52 | targets := $(proj)-ns $(proj)-cfg $(proj)-lookup lib$(proj).so |
| 53 | |
| 54 | out := out |
| 55 | src_to_obj = $(patsubst %.c,$(out)/obj/%.o,$(1)) |
| 56 | src_to_dep = $(patsubst %.c,$(out)/dep/%.d,$(1)) |
| 57 | |
| 58 | all-srcs := |
| 59 | all-objs := |
| 60 | all-deps := |
| 61 | all-clean := $(out) |
| 62 | all-install := |
| 63 | |
| 64 | all: $(targets) |
| 65 | |
| 66 | $(out)/obj/%.o: %.c |
| 67 | ifneq ($C,) |
| 68 | @echo "CHECK $<" |
| 69 | @sparse $< $(patsubst -iquote=%,-I%,$(CFLAGS)) $(SFLAGS) |
| 70 | endif |
| 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 | |
| 75 | define add-inc-target |
| 76 | $(DESTDIR)$(includedir)/$2: $1/$2 |
| 77 | @echo "INSTALL $$<" |
| 78 | @install -D -m 755 $$< $$@ |
| 79 | |
| 80 | all-install += $(DESTDIR)$(includedir)/$2 |
| 81 | endef |
| 82 | |
| 83 | define add-target-deps |
| 84 | all-srcs += $($1-srcs) |
| 85 | all-objs += $(call src_to_obj,$($1-srcs)) |
| 86 | all-deps += $(call src_to_dep,$($1-srcs)) |
| 87 | all-clean += $1 |
| 88 | $(call src_to_obj,$($1-srcs)): _CFLAGS := $($1-cflags) |
| 89 | endef |
| 90 | |
| 91 | define 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 | |
| 103 | all-install += $(DESTDIR)$(bindir)/$1 |
| 104 | endef |
| 105 | |
| 106 | define 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 | |
| 120 | all-install += $(DESTDIR)$(libdir)/$1.$(proj-version) |
| 121 | endef |
| 122 | |
| 123 | define 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 | |
| 131 | all-install += $(DESTDIR)$(servicedir)/$1 |
| 132 | endef |
| 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 | |
| 139 | install: $(all-install) |
| 140 | |
| 141 | clean: |
| 142 | @echo CLEAN |
| 143 | @$(RM) -r $(all-clean) |
| 144 | |
| 145 | $(call src_to_obj,$(all-srcs)): Makefile |
| 146 | |
| 147 | ifneq ("$(MAKECMDGOALS)","clean") |
| 148 | cmd-goal-1 := $(shell mkdir -p $(sort $(dir $(all-objs) $(all-deps)))) |
| 149 | -include $(all-deps) |
| 150 | endif |