Merge branch 'master' of github.com:borkmann/netsniff-ng
[netsniff-ng.git] / Makefile
blobf570a8aade09eea703480f88ce1802c414c1079d
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 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 WFLAGS = -Wall
68 WFLAGS += -Wformat=2
69 WFLAGS += -Wmissing-prototypes
70 WFLAGS += -Wdeclaration-after-statement
71 WFLAGS += -Werror-implicit-function-declaration
72 WFLAGS += -Wstrict-prototypes
73 WFLAGS += -Wundef
74 WFLAGS += -Wimplicit-int
76 WFLAGS_EXTRA = -Wno-unused-result
77 WFLAGS_EXTRA += -Wmissing-parameter-type
78 WFLAGS_EXTRA += -Wtype-limits
79 WFLAGS_EXTRA += -Wclobbered
80 WFLAGS_EXTRA += -Wmissing-field-initializers
81 WFLAGS_EXTRA += -Woverride-init
82 WFLAGS_EXTRA += -Wold-style-declaration
83 WFLAGS_EXTRA += -Wignored-qualifiers
84 WFLAGS_EXTRA += -Wempty-body
85 WFLAGS_EXTRA += -Wuninitialized
87 CFLAGS += $(WFLAGS) -I.
88 CPPFLAGS =
89 ifeq ("$(origin CROSS_LD_LIBRARY_PATH)", "command line")
90 LDFLAGS = -L$(CROSS_LD_LIBRARY_PATH)
91 else
92 LDFLAGS =
93 endif
95 ALL_CFLAGS = $(CFLAGS)
96 ALL_LDFLAGS = $(LDFLAGS)
97 TARGET_ARCH =
98 LEX_FLAGS =
99 YAAC_FLAGS =
101 Q = @
103 LD = $(Q)echo -e " LD\t$@" && $(CCACHE) $(CROSS_COMPILE)gcc
104 CCNQ = $(CCACHE) $(CROSS_COMPILE)gcc
105 CC = $(Q)echo -e " CC\t$<" && $(CCNQ)
106 CCHK = echo -e " CCHK\t$@" && cppcheck -q --std=c99 \
107 --enable=style,performance,portability,unusedFunction \
108 --includes-file=${NACL_INC_DIR}
109 MKDIR = $(Q)echo -e " MKDIR\t$@" && mkdir
110 ifeq ($(DEBUG), 1)
111 STRIP = $(Q)true
112 else
113 STRIP = $(Q)echo -e " STRIP\t$@" && $(CROSS_COMPILE)strip
114 endif
115 LEX = $(Q)echo -e " LEX\t$<" && flex
116 YAAC = $(Q)echo -e " YAAC\t$<" && bison
117 INST = echo -e " INST\t$(1)" && install -d $(2) && \
118 install --mode=644 -DC $(1) $(2)/$(shell basename $(1))
119 ifeq ("$(origin PREFIX)", "command line")
120 INSTX = echo -e " INST\t$(1)" && install -d $(2) && \
121 install -C $(1) $(2)/$(shell basename $(1))
122 else
123 INSTX = echo -e " INST\t$(1)" && install -C $(1) $(2)/$(shell basename $(1))
124 endif
125 RM = echo -e " RM\t$(1)" && rm -rf $(1)
126 RMDIR = echo -e " RM\t$(1)" && rmdir --ignore-fail-on-non-empty $(1) 2> /dev/null || true
127 GIT_ARCHIVE = cd .. && git archive --prefix=netsniff-ng-$(VERSION_STRING)/ $(VERSION_STRING) | \
128 $(1) > ../netsniff-ng-$(VERSION_STRING).tar.$(2)
129 GIT_TAG = git tag -a $(VERSION_STRING) -m "$(VERSION_STRING) release"
131 export VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION
132 export CROSS_COMPILE
134 VERSION_STRING = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
135 VERSION_SHORT = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
137 bold = $(shell tput bold)
138 normal = $(shell tput sgr0)
140 $(shell . ~/.bashrc)
141 # Build NaCl if no environment variables are set!
142 ifndef NACL_LIB_DIR
143 ifndef NACL_INC_DIR
144 $(info $(bold)NACL_LIB_DIR/NACL_INC_DIR is undefined, building libnacl with curvetun!$(normal))
145 curvetun: nacl
146 endif
147 endif
149 ifeq ("$(origin CROSS_COMPILE)", "command line")
150 WHAT := Cross compiling
151 else
152 WHAT := Building
153 endif
155 build_showinfo:
156 $(Q)echo "$(bold)$(WHAT) netsniff-ng toolkit ($(VERSION_STRING)) for" \
157 $(shell $(CCNQ) -dumpmachine)":$(normal)"
158 clean_showinfo:
159 $(Q)echo "$(bold)Cleaning netsniff-ng toolkit ($(VERSION_STRING)):$(normal)"
161 %.yy.o: %.l
162 $(LEX) -P $(shell perl -wlne 'print $$1 if /lex-func-prefix:\s([a-z]+)/' $<) \
163 -o $(BUILD_DIR)/$(shell basename $< .l).yy.c $(LEX_FLAGS) $<
164 %.tab.o: %.y
165 $(YAAC) -p $(shell perl -wlne 'print $$1 if /yaac-func-prefix:\s([a-z]+)/' $<) \
166 -o $(BUILD_DIR)/$(shell basename $< .y).tab.c $(YAAC_FLAGS) -d $<
167 %.x:
168 @if [ -a $(shell basename $@ .x).c ]; then $(CCHK) $(shell basename $@ .x).c || true; fi
170 .PHONY: all toolkit $(TOOLS) clean %_prehook %_distclean %_clean %_install
171 .FORCE:
172 .DEFAULT_GOAL := all
173 .DEFAULT:
174 .IGNORE: %_clean_custom %_install_custom
175 .NOTPARALLEL: $(TOOLS)
177 DOC_FILES = Summary RelatedWork Performance KnownIssues IPv6Notes Sponsors \
178 SubmittingPatches CodingStyle logo.png RelNotes/Notes-$(VERSION_SHORT)
180 NCONF_FILES = ether.conf tcp.conf udp.conf oui.conf
182 all: build_showinfo toolkit
183 allbutcurvetun: $(filter-out curvetun,$(TOOLS))
184 allbutmausezahn: $(filter-out mausezahn,$(TOOLS))
185 toolkit: $(TOOLS)
186 install: install_all
187 install_all: $(foreach tool,$(TOOLS),$(tool)_install)
188 $(Q)$(foreach file,$(DOC_FILES),$(call INST,../Documentation/$(file),$(DOCDIRE));)
189 install_allbutcurvetun: $(foreach tool,$(filter-out curvetun,$(TOOLS)),$(tool)_install)
190 $(Q)$(foreach file,$(DOC_FILES),$(call INST,../Documentation/$(file),$(DOCDIRE));)
191 install_allbutmausezahn: $(foreach tool,$(filter-out mausezahn,$(TOOLS)),$(tool)_install)
192 $(Q)$(foreach file,$(DOC_FILES),$(call INST,../Documentation/$(file),$(DOCDIRE));)
193 clean mostlyclean: $(foreach tool,$(TOOLS),$(tool)_clean)
194 realclean distclean clobber: $(foreach tool,$(TOOLS),$(tool)_distclean)
195 $(Q)$(foreach file,$(DOC_FILES),$(call RM,$(DOCDIRE)/$(file));)
196 $(Q)$(call RMDIR,$(DOCDIRE))
197 $(Q)$(call RMDIR,$(ETCDIRE))
198 mrproper: clean distclean
199 check: $(foreach tool,$(TOOLS),$(tool)_check)
201 define TOOL_templ
202 include $(1)/Makefile
203 $(1) $(1)%: BUILD_DIR := $(1)
204 $(1)_prehook:
205 $(Q)echo "$(bold)$(WHAT) $(1):$(normal)"
206 $(1)_prehook_check:
207 $(Q)echo "$(bold)Checking $(1):$(normal)"
208 $(1): $(1)_prehook $$($(1)-lex) $$($(1)-yaac) $$(patsubst %.o,$(1)/%.o,$$($(1)-objs))
209 $(1)_clean: $(1)_clean_custom
210 $(Q)$$(call RM,$(1)/*.o $(1)/$(1))
211 $(1)_check: $(1)_prehook_check $$(patsubst %.o,%.x,$$($(1)-objs))
212 $(1)_install: $(1)_install_custom
213 $(Q)$$(call INSTX,$(1)/$(1),$$(SBINDIR))
214 $(Q)$$(call INST,../Documentation/$$(shell echo $(1) | sed 's/\([a-z]\)\(.*\)/\u\1\2/g'),$$(DOCDIRE))
215 $(1)_distclean: $(1)_distclean_custom
216 $(Q)$$(call RM,$$(SBINDIR)/$(1))
217 $(Q)$$(call RM,$$(DOCDIRE)/$$(shell echo $(1) | sed 's/\([a-z]\)\(.*\)/\u\1\2/g'))
218 $(1)/%.yy.o: $(1)/%.yy.c
219 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
220 $(1)/%.tab.o: $(1)/%.tab.c
221 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
222 $(1)/%.o: %.c
223 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
224 endef
226 $(foreach tool,$(TOOLS),$(eval $(call TOOL_templ,$(tool))))
228 %:: ;
230 netsniff-ng: ALL_CFLAGS += -I$(INCDIR)/libnl3/ -D__WITH_PROTOS -D__WITH_TCPDUMP_LIKE_FILTER
231 trafgen: ALL_CFLAGS += -I.. -I$(INCDIR)/libnl3/ -D__WITH_PROTOS
232 bpfc: ALL_CFLAGS += -I..
233 curvetun: ALL_CFLAGS += -I ${NACL_INC_DIR}
234 curvetun: ALL_LDFLAGS += -L ${NACL_LIB_DIR}
236 bpfc_clean_custom:
237 $(Q)$(call RM,$(BUILD_DIR)/*.h $(BUILD_DIR)/*.c)
238 trafgen_clean_custom:
239 $(Q)$(call RM,$(BUILD_DIR)/*.h $(BUILD_DIR)/*.c)
241 netsniff-ng_distclean_custom flowtop_distclean_custom:
242 $(Q)$(foreach file,$(NCONF_FILES),$(call RM,$(ETCDIRE)/$(file));)
243 $(Q)$(call RMDIR,$(ETCDIRE))
244 astraceroute_distclean_custom:
245 $(Q)$(call RM,$(ETCDIRE)/whois.conf)
246 $(Q)$(call RM,$(SBINDIR)/astraceroute6)
247 $(Q)$(call RMDIR,$(ETCDIRE))
249 netsniff-ng_install_custom flowtop_install_custom:
250 $(Q)$(foreach file,$(NCONF_FILES),$(call INST,configs/$(file),$(ETCDIRE));)
251 astraceroute_install_custom:
252 $(Q)$(call INST,configs/whois.conf,$(ETCDIRE))
254 $(TOOLS): WFLAGS += $(WFLAGS_EXTRA)
255 $(TOOLS):
256 $(LD) $(ALL_LDFLAGS) -o $@/$@ $@/*.o $($@-libs)
257 $(STRIP) $@/$@
259 update:
260 $(Q)./update-geoip.sh
261 nacl:
262 $(Q)echo "$(bold)$(WHAT) $@:$(normal)"
263 $(Q)cd curvetun/ && ./build_nacl.sh ~/nacl
264 $(Q)source ~/.bashrc
265 geoip:
266 $(Q)echo "$(bold)$(WHAT) $@:$(normal)"
267 $(Q)cd astraceroute/ && ./build_geoip.sh
269 tarball.gz: ; $(call GIT_ARCHIVE,gzip,gz)
270 tarball.bz2: ; $(call GIT_ARCHIVE,bzip2,bz2)
271 tarball.xz: ; $(call GIT_ARCHIVE,xz,xz)
272 tarball: tarball.gz tarball.bz2 tarball.xz
274 tag:
275 $(GIT_TAG)
277 help:
278 $(Q)echo "$(bold)Available tools from the toolkit:$(normal)"
279 $(Q)echo " <toolnames>:={$(TOOLS)}"
280 $(Q)echo "$(bold)Targets for building the toolkit:$(normal)"
281 $(Q)echo " all|toolkit - Build the whole toolkit"
282 $(Q)echo " allbutcurvetun - Build all except curvetun"
283 $(Q)echo " <toolname> - Build only one of the tools"
284 $(Q)echo "$(bold)Targets for cleaning the toolkit's build files:$(normal)"
285 $(Q)echo " clean|mostlyclean - Remove all build files"
286 $(Q)echo " <toolname>_clean - Remove only one of the tool's files"
287 $(Q)echo "$(bold)Targets for installing the toolkit:$(normal)"
288 $(Q)echo " install - Install the whole toolkit"
289 $(Q)echo " <toolname>_install - Install only one of the tools"
290 $(Q)echo "$(bold)Targets for removing the toolkit:$(normal)"
291 $(Q)echo " realclean|distclean|clobber - Remove the whole toolkit from the system"
292 $(Q)echo " <toolname>_distclean - Remove only one of the tools"
293 $(Q)echo "$(bold)Targets for checking the toolkit:$(normal)"
294 $(Q)echo " check - Do a static code analysis"
295 $(Q)echo " <toolname>_check - Check only one of the tools"
296 $(Q)echo "$(bold)Misc targets:$(normal)"
297 $(Q)echo " tarball - Generate tarball of latest version"
298 $(Q)echo " tag - Generate Git tag of current version"
299 $(Q)echo " mrproper - Remove build and install files"
300 $(Q)echo " update - Update to the latest GeoIP database"
301 $(Q)echo " nacl - Execute the build_nacl script"
302 $(Q)echo " geoip - Execute the build_geoip script"
303 $(Q)echo " help - Show this help"
304 $(Q)echo "$(bold)Available parameters:$(normal)"
305 $(Q)echo " DEBUG=1 - Enable debugging"
306 $(Q)echo " PREFIX=/path - Install path prefix"
307 $(Q)echo " CROSS_COMPILE=/path-prefix - Kernel-like cross-compiling prefix"
308 $(Q)echo " CROSS_LD_LIBRARY_PATH=/path - Library search path for cross-compiling"
309 $(Q)echo " Q= - Show verbose garbage"