make: add cppcheck analysis
[netsniff-ng.git] / src / Makefile
blobfd8120499aef15db2cd562e1d9ed972faabcd1cf
1 # netsniff-ng build system, part of netsniff-ng.
2 # Copyright 2012 Daniel Borkmann <borkmann@gnumaniacs.org>
3 # Subject to the GNU GPL, version 2.
5 VERSION = 0
6 PATCHLEVEL = 5
7 SUBLEVEL = 8
8 EXTRAVERSION = -rc0
10 TOOLS = netsniff-ng trafgen astraceroute flowtop ifpps mausezahn bpfc curvetun
12 # For packaging purposes, prefix can define a different path.
13 PREFIX ?=
15 # Debugging option
16 ifeq ("$(origin DEBUG)", "command line")
17 DEBUG := 1
18 else
19 DEBUG := 0
20 endif
22 # Disable if you don't want it
23 CCACHE = ccache
25 # Location of installation paths.
26 BINDIR = $(PREFIX)/usr/bin
27 SBINDIR = $(PREFIX)/usr/sbin
28 INCDIR = $(PREFIX)/usr/include
29 ETCDIR = $(PREFIX)/etc
30 ETCDIRE = $(ETCDIR)/netsniff-ng
31 DOCDIR = $(PREFIX)/usr/share/doc
32 DOCDIRE = $(DOCDIR)/netsniff-ng
34 # Shut up make, helper warnings, parallel compilation!
35 MAKEFLAGS += --no-print-directory
36 MAKEFLAGS += -rR
37 MAKEFLAGS += --warn-undefined-variables
38 MAKEFLAGS += --jobs=$(shell grep "^processor" /proc/cpuinfo | wc -l)
40 # For packaging purposes, you might want to disable O3+arch tuning
41 CFLAGS = -fstack-protector
42 ifeq ($(DEBUG), 1)
43 CFLAGS += -g
44 CFLAGS += -O2
45 else
46 CFLAGS += -march=native
47 CFLAGS += -mtune=native
48 CFLAGS += -O3
49 CFLAGS += -fpie
50 CFLAGS += -pipe
51 CFLAGS += -fomit-frame-pointer
52 endif
53 CFLAGS += --param=ssp-buffer-size=4
54 CFLAGS += -fno-strict-aliasing
55 CFLAGS += -fexceptions
56 CFLAGS += -fasynchronous-unwind-tables
57 CFLAGS += -fno-delete-null-pointer-checks
58 CFLAGS += -D_FORTIFY_SOURCE=2
59 CFLAGS += -D_REENTRANT
60 CFLAGS += -D_FILE_OFFSET_BITS=64
61 CFLAGS += -D_LARGEFILE_SOURCE
62 CFLAGS += -D_LARGEFILE64_SOURCE
63 CFLAGS += -D__WITH_HARDWARE_TIMESTAMPING=1
64 CFLAGS += -DVERSION_STRING=\"$(VERSION_STRING)\"
65 CFLAGS += -std=gnu99
67 # Should be commented in after the mausezahn mess is cleaned
68 WFLAGS = -Wall
69 #WFLAGS += -Wmissing-prototypes
70 #WFLAGS += -Wdeclaration-after-statement
71 #WFLAGS += -Werror-implicit-function-declaration
72 #WFLAGS += -Wundef
73 #WFLAGS += -Wstrict-prototypes
74 #WFLAGS += -Wunused-but-set-variable
75 #WFLAGS += -Werror
76 WFLAGS += -Wformat=2
78 CFLAGS += $(WFLAGS) -I.
79 CPPFLAGS =
80 ifeq ("$(origin CROSS_LD_LIBRARY_PATH)", "command line")
81 LDFLAGS = -L$(CROSS_LD_LIBRARY_PATH)
82 else
83 LDFLAGS =
84 endif
86 ALL_CFLAGS = $(CFLAGS)
87 ALL_LDFLAGS = $(LDFLAGS)
88 TARGET_ARCH =
89 LEX_FLAGS =
90 YAAC_FLAGS =
92 Q = @
94 LD = $(Q)echo -e " LD\t$@" && $(CCACHE) $(CROSS_COMPILE)gcc
95 CCNQ = $(CCACHE) $(CROSS_COMPILE)gcc
96 CC = $(Q)echo -e " CC\t$<" && $(CCNQ)
97 CCHK = echo -e " CCHK\t$@" && cppcheck -q --enable=all --std=c99 --includes-file=$(NACL_INC_DIR)
98 MKDIR = $(Q)echo -e " MKDIR\t$@" && mkdir
99 ifeq ($(DEBUG), 1)
100 STRIP = $(Q)true
101 else
102 STRIP = $(Q)echo -e " STRIP\t$@" && $(CROSS_COMPILE)strip
103 endif
104 LEX = $(Q)echo -e " LEX\t$<" && flex
105 YAAC = $(Q)echo -e " YAAC\t$<" && bison
106 INST = echo -e " INST\t$(1)" && install -d $(2) && \
107 install --mode=644 -DC $(1) $(2)/$(shell basename $(1))
108 ifeq ("$(origin PREFIX)", "command line")
109 INSTX = echo -e " INST\t$(1)" && install -d $(2) && \
110 install -C $(1) $(2)/$(shell basename $(1))
111 else
112 INSTX = echo -e " INST\t$(1)" && install -C $(1) $(2)/$(shell basename $(1))
113 endif
114 RM = echo -e " RM\t$(1)" && rm -rf $(1)
115 RMDIR = echo -e " RM\t$(1)" && rmdir --ignore-fail-on-non-empty $(1) 2> /dev/null || true
116 GIT_ARCHIVE = cd .. && git archive --prefix=netsniff-ng-$(VERSION_STRING)/ $(VERSION_STRING) | \
117 $(1) > ../netsniff-ng-$(VERSION_STRING).tar.$(2)
119 export VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION
120 export CROSS_COMPILE
122 VERSION_STRING = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
123 VERSION_SHORT = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
125 bold = $(shell tput bold)
126 normal = $(shell tput sgr0)
128 # Build NaCl if no environment variables are set!
129 ifndef NACL_LIB_DIR
130 ifndef NACL_INC_DIR
131 $(info $(bold)NACL_LIB_DIR/NACL_INC_DIR is undefined, building libnacl with curvetun!$(normal))
132 curvetun: nacl
133 endif
134 endif
136 ifeq ("$(origin CROSS_COMPILE)", "command line")
137 WHAT := Cross compiling
138 else
139 WHAT := Building
140 endif
142 build_showinfo:
143 $(Q)echo "$(bold)$(WHAT) netsniff-ng toolkit ($(VERSION_STRING)) for" \
144 $(shell $(CCNQ) -dumpmachine)":$(normal)"
145 clean_showinfo:
146 $(Q)echo "$(bold)Cleaning netsniff-ng toolkit ($(VERSION_STRING)):$(normal)"
148 %.yy.o: %.l
149 $(LEX) -P $(shell perl -wlne 'print $$1 if /lex-func-prefix:\s([a-z]+)/' $<) \
150 -o $(BUILD_DIR)/$(shell basename $< .l).yy.c $(LEX_FLAGS) $<
151 %.tab.o: %.y
152 $(YAAC) -p $(shell perl -wlne 'print $$1 if /yaac-func-prefix:\s([a-z]+)/' $<) \
153 -o $(BUILD_DIR)/$(shell basename $< .y).tab.c $(YAAC_FLAGS) -d $<
154 %.x:
155 @if [ -a $(shell basename $@ .x).c ]; then $(CCHK) $(shell basename $@ .x).c || true; fi
157 .PHONY: all toolkit $(TOOLS) clean %_prehook %_distclean %_clean %_install
158 .FORCE:
159 .DEFAULT_GOAL := all
160 .DEFAULT:
161 .IGNORE: %_clean_custom %_install_custom
162 .NOTPARALLEL: $(TOOLS)
164 DOC_FILES = Summary RelatedWork Performance KnownIssues IPv6Notes Sponsors \
165 SubmittingPatches CodingStyle logo.png RelNotes/Notes-$(VERSION_SHORT)
167 NCONF_FILES = ether.conf tcp.conf udp.conf oui.conf
169 all: build_showinfo toolkit
170 toolkit: $(TOOLS)
171 allbutcurvetun: $(filter-out curvetun,$(TOOLS))
172 install: $(foreach tool,$(TOOLS),$(tool)_install)
173 $(Q)$(foreach file,$(DOC_FILES),$(call INST,../Documentation/$(file),$(DOCDIRE));)
174 install_allbutcurvetun: $(foreach tool,$(filter-out curvetun,$(TOOLS)),$(tool)_install)
175 $(Q)$(foreach file,$(DOC_FILES),$(call INST,../Documentation/$(file),$(DOCDIRE));)
176 clean mostlyclean: $(foreach tool,$(TOOLS),$(tool)_clean)
177 realclean distclean clobber: $(foreach tool,$(TOOLS),$(tool)_distclean)
178 $(Q)$(foreach file,$(DOC_FILES),$(call RM,$(DOCDIRE)/$(file));)
179 $(Q)$(call RMDIR,$(DOCDIRE))
180 $(Q)$(call RMDIR,$(ETCDIRE))
181 mrproper: clean distclean
182 check: $(foreach tool,$(TOOLS),$(tool)_check)
184 define TOOL_templ
185 include $(1)/Makefile
186 $(1) $(1)%: BUILD_DIR := $(1)
187 $(1)_prehook:
188 $(Q)echo "$(bold)$(WHAT) $(1):$(normal)"
189 $(1): $(1)_prehook $$($(1)-lex) $$($(1)-yaac) $$(patsubst %.o,$(1)/%.o,$$($(1)-objs))
190 $(1)_clean: $(1)_clean_custom
191 $(Q)$$(call RM,$(1)/*.o $(1)/$(1))
192 $(1)_check: $$(patsubst %.o,%.x,$$($(1)-objs))
193 $(1)_install: $(1)_install_custom
194 $(Q)$$(call INSTX,$(1)/$(1),$$(SBINDIR))
195 $(Q)$$(call INST,../Documentation/$$(shell echo $(1) | sed 's/\([a-z]\)\(.*\)/\u\1\2/g'),$$(DOCDIRE))
196 $(1)_distclean: $(1)_distclean_custom
197 $(Q)$$(call RM,$$(SBINDIR)/$(1))
198 $(Q)$$(call RM,$$(DOCDIRE)/$$(shell echo $(1) | sed 's/\([a-z]\)\(.*\)/\u\1\2/g'))
199 $(1)/%.yy.o: $(1)/%.yy.c
200 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
201 $(1)/%.tab.o: $(1)/%.tab.c
202 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
203 $(1)/%.o: %.c
204 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
205 endef
207 $(foreach tool,$(TOOLS),$(eval $(call TOOL_templ,$(tool))))
209 %:: ;
211 netsniff-ng: ALL_CFLAGS += -I$(INCDIR)/libnl3/ -D__WITH_PROTOS
212 trafgen: ALL_CFLAGS += -I.. -I$(INCDIR)/libnl3/ -D__WITH_PROTOS
213 bpfc: ALL_CFLAGS += -I..
214 curvetun: ALL_CFLAGS += -I NACL_INC_DIR
215 curvetun: ALL_LDFLAGS += -L NACL_LIB_DIR
217 bpfc_clean_custom:
218 $(Q)$(call RM,$(BUILD_DIR)/*.h $(BUILD_DIR)/*.c)
219 trafgen_clean_custom:
220 $(Q)$(call RM,$(BUILD_DIR)/*.h $(BUILD_DIR)/*.c)
222 netsniff-ng_distclean_custom flowtop_distclean_custom:
223 $(Q)$(foreach file,$(NCONF_FILES),$(call RM,$(ETCDIRE)/$(file));)
224 $(Q)$(call RMDIR,$(ETCDIRE))
225 astraceroute_distclean_custom:
226 $(Q)$(call RM,$(ETCDIRE)/whois.conf)
227 $(Q)$(call RM,$(SBINDIR)/astraceroute6)
228 $(Q)$(call RMDIR,$(ETCDIRE))
230 netsniff-ng_install_custom flowtop_install_custom:
231 $(Q)$(foreach file,$(NCONF_FILES),$(call INST,conf/$(file),$(ETCDIRE));)
232 astraceroute_install_custom:
233 $(Q)$(call INST,conf/whois.conf,$(ETCDIRE))
234 $(Q)$(call INSTX,../scripts/astraceroute6,$(SBINDIR))
236 $(TOOLS):
237 $(LD) $(ALL_LDFLAGS) -o $@/$@ $@/*.o $($@-libs)
238 $(STRIP) $@/$@
240 update:
241 $(Q)../scripts/geoip-database-update
242 nacl:
243 $(Q)echo "$(bold)$(WHAT) $@:$(normal)"
244 $(Q)cd curvetun/ && ./build_nacl.sh ~/nacl
245 $(Q)source ~/.bashrc
246 geoip:
247 $(Q)echo "$(bold)$(WHAT) $@:$(normal)"
248 $(Q)cd astraceroute/ && ./build_geoip.sh
250 tarball.gz: ; $(call GIT_ARCHIVE,gzip,gz)
251 tarball.bz2: ; $(call GIT_ARCHIVE,bzip2,bz2)
252 tarball.xz: ; $(call GIT_ARCHIVE,xz,xz)
253 tarball: tarball.gz tarball.bz2 tarball.xz
255 help:
256 $(Q)echo "$(bold)Available tools from the toolkit:$(normal)"
257 $(Q)echo " <toolnames>:={$(TOOLS)}"
258 $(Q)echo "$(bold)Targets for building the toolkit:$(normal)"
259 $(Q)echo " all|toolkit - Build the whole toolkit"
260 $(Q)echo " allbutcurvetun - Build all except curvetun"
261 $(Q)echo " <toolname> - Build only one of the tools"
262 $(Q)echo "$(bold)Targets for cleaning the toolkit's build files:$(normal)"
263 $(Q)echo " clean|mostlyclean - Remove all build files"
264 $(Q)echo " <toolname>_clean - Remove only one of the tool's files"
265 $(Q)echo "$(bold)Targets for installing the toolkit:$(normal)"
266 $(Q)echo " install - Install the whole toolkit"
267 $(Q)echo " <toolname>_install - Install only one of the tools"
268 $(Q)echo "$(bold)Targets for removing the toolkit:$(normal)"
269 $(Q)echo " realclean|distclean|clobber - Remove the whole toolkit from the system"
270 $(Q)echo " <toolname>_distclean - Remove only one of the tools"
271 $(Q)echo "$(bold)Targets for checking the toolkit:$(normal)"
272 $(Q)echo " check - Do a static code analysis"
273 $(Q)echo " <toolname>_check - Check only one of the tools"
274 $(Q)echo "$(bold)Misc targets:$(normal)"
275 $(Q)echo " tarball - Generate tarball of latest version"
276 $(Q)echo " mrproper - Remove build and install files"
277 $(Q)echo " update - Update to the latest GeoIP database"
278 $(Q)echo " nacl - Execute the build_nacl script"
279 $(Q)echo " geoip - Execute the build_geoip script"
280 $(Q)echo " help - Show this help"
281 $(Q)echo "$(bold)Available parameters:$(normal)"
282 $(Q)echo " DEBUG=1 - Enable debugging"
283 $(Q)echo " PREFIX=/path - Install path prefix"
284 $(Q)echo " CROSS_COMPILE=/path-prefix - Kernel-like cross-compiling prefix"
285 $(Q)echo " CROSS_LD_LIBRARY_PATH=/path - Library search path for cross-compiling"