configure: Add linker flags for ncursestest to correct position
[netsniff-ng.git] / Makefile
blobee03d86530158784d8bd39b78946c3fca62f0c82
1 # netsniff-ng build system
2 # Copyright 2012 - 2013 Daniel Borkmann <borkmann@gnumaniacs.org>
3 # Subject to the GNU GPL, version 2.
5 include Config
7 VERSION = 0
8 PATCHLEVEL = 5
9 SUBLEVEL = 8
10 EXTRAVERSION = -rc0
11 NAME = Ziggomatic
13 TOOLS ?= netsniff-ng trafgen astraceroute flowtop ifpps bpfc curvetun mausezahn
15 # For packaging purposes, prefix can define a different path.
16 PREFIX ?=
18 # Disable if you don't want it
19 CCACHE ?= ccache
21 # Location of installation paths.
22 SBINDIR = $(PREFIX)/usr/sbin
23 INCDIR = $(PREFIX)/usr/include
24 ETCDIR = $(PREFIX)/etc
25 ETCDIRE = $(ETCDIR)/netsniff-ng
26 MAN8DIR = $(PREFIX)/usr/share/man/man8
28 # Shut up make, helper warnings, parallel compilation!
29 MAKEFLAGS += --no-print-directory
30 MAKEFLAGS += -rR
31 MAKEFLAGS += --warn-undefined-variables
32 MAKEFLAGS += --jobs=$(shell grep "^processor" /proc/cpuinfo | wc -l)
34 # Debugging option
35 ifeq ("$(origin DEBUG)", "command line")
36 DEBUG := 1
37 else
38 DEBUG := 0
39 endif
41 # For packaging purposes, you might want to call your own:
42 # make CFLAGS="<flags>"
43 CFLAGS_DEF = -std=gnu99
44 CFLAGS_DEF += -pipe
46 ifeq ($(DEBUG), 1)
47 CFLAGS_DEF += -O2
48 CFLAGS_DEF += -g
49 else
50 ifeq ($(DISTRO), 1)
51 CFLAGS_DEF += -O2
52 else
53 CFLAGS_DEF += -march=native
54 CFLAGS_DEF += -mtune=native
55 CFLAGS_DEF += -O3
56 endif
57 endif
58 ifeq ($(HARDENING), 1)
59 CFLAGS_DEF += -fPIE -pie
60 CFLAGS_DEF += -Wl,-z,relro,-z,now
61 CFLAGS_DEF += -fstack-protector-all
62 CFLAGS_DEF += -Wstack-protector
63 CFLAGS_DEF += --param=ssp-buffer-size=4
64 CFLAGS_DEF += -ftrapv
65 CFLAGS_DEF += -D_FORTIFY_SOURCE=2
66 CFLAGS_DEF += -fexceptions
67 endif
69 CFLAGS_DEF += -fomit-frame-pointer
70 CFLAGS_DEF += -fno-strict-aliasing
71 CFLAGS_DEF += -fasynchronous-unwind-tables
72 CFLAGS_DEF += -fno-delete-null-pointer-checks
74 CFLAGS_DEF += -D_REENTRANT
75 CFLAGS_DEF += -D_LARGEFILE_SOURCE
76 CFLAGS_DEF += -D_LARGEFILE64_SOURCE
77 CFLAGS_DEF += -D_FILE_OFFSET_BITS=64
79 WFLAGS_DEF = -Wall
80 WFLAGS_DEF += -Wformat=2
81 WFLAGS_DEF += -Wmissing-prototypes
82 WFLAGS_DEF += -Wdeclaration-after-statement
83 WFLAGS_DEF += -Werror-implicit-function-declaration
84 WFLAGS_DEF += -Wstrict-prototypes
85 WFLAGS_DEF += -Wimplicit-int
86 WFLAGS_DEF += -Wundef
88 WFLAGS_EXTRA = -Wno-unused-result
89 WFLAGS_EXTRA += -Wmissing-parameter-type
90 WFLAGS_EXTRA += -Wtype-limits
91 WFLAGS_EXTRA += -Wclobbered
92 WFLAGS_EXTRA += -Wmissing-field-initializers
93 WFLAGS_EXTRA += -Woverride-init
94 WFLAGS_EXTRA += -Wold-style-declaration
95 WFLAGS_EXTRA += -Wignored-qualifiers
96 WFLAGS_EXTRA += -Wempty-body
97 WFLAGS_EXTRA += -Wuninitialized
99 WFLAGS_DEF += $(WFLAGS_EXTRA)
100 CFLAGS_DEF += $(WFLAGS_DEF)
102 CFLAGS ?= $(CFLAGS_DEF)
103 CPPFLAGS ?=
104 LEX_FLAGS =
105 YAAC_FLAGS =
106 LDFLAGS ?=
107 ifeq ("$(origin CROSS_LD_LIBRARY_PATH)", "command line")
108 LDFLAGS += -L$(CROSS_LD_LIBRARY_PATH)
109 endif
111 ALL_LDFLAGS = $(LDFLAGS)
112 ALL_CFLAGS = $(CFLAGS) $(CPPFLAGS) -I.
113 ALL_CFLAGS += -DVERSION_STRING=\"$(VERSION_STRING)\"
114 ALL_CFLAGS += -DVERSION_LONG=\"$(VERSION_LONG)\"
115 ALL_CFLAGS += -DPREFIX_STRING=\"$(PREFIX)\"
116 ifneq ($(wildcard /usr/include/linux/net_tstamp.h),)
117 ALL_CFLAGS += -D__WITH_HARDWARE_TIMESTAMPING
118 endif
120 VERSION_STRING = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
121 VERSION_LONG = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)~$(NAME)
123 # Be quite and do not echo the cmd
124 Q = @
126 # GCC related stuff
127 LD = $(Q)echo -e " LD\t$@" && $(CCACHE) $(CROSS_COMPILE)gcc
128 CCNQ = $(CCACHE) $(CROSS_COMPILE)gcc
129 CC = $(Q)echo -e " CC\t$<" && $(CCNQ)
130 ifeq ($(DEBUG), 1)
131 STRIP = $(Q)true
132 else
133 STRIP = $(Q)echo -e " STRIP\t$@" && $(CROSS_COMPILE)strip
134 endif
136 # Flex/bison related
137 LEX = $(Q)echo -e " LEX\t$<" && flex
138 YAAC = $(Q)echo -e " YAAC\t$<" && bison
140 # Installation related
141 INST = echo -e " INST\t$(1)" && install -d $(2) && \
142 install --mode=644 -DC $(1) $(2)/$(shell basename $(1))
144 ifeq ("$(origin PREFIX)", "command line")
145 INSTX = echo -e " INST\t$(1)" && install -d $(2) && \
146 install -C $(1) $(2)/$(shell basename $(1))
147 else
148 INSTX = echo -e " INST\t$(1)" && install -C $(1) $(2)/$(shell basename $(1))
149 endif
151 RM = echo -e " RM\t$(1)" && rm -rf $(1)
152 RMDIR = echo -e " RM\t$(1)" && rmdir --ignore-fail-on-non-empty $(1) 2> /dev/null || true
154 GZIP = gzip --best -c
156 # Git related
157 GIT_ARCHIVE = git archive --prefix=netsniff-ng-$(VERSION_STRING)/ $(VERSION_STRING) | \
158 $(1) > ../netsniff-ng-$(VERSION_STRING).tar.$(2)
159 GIT_TAG = git tag -a $(VERSION_STRING) -s -m "tools: $(VERSION_STRING) release"
160 GIT_LOG = git shortlog -n --not $(shell git describe --abbrev=0 --tags)
161 GIT_REM = git ls-files -o | xargs rm -rf
162 GIT_PEOPLE = git log --no-merges $(VERSION_STRING)..HEAD | grep Author: | cut -d: -f2 | \
163 cut -d\< -f1 | sort | uniq -c | sort -nr
165 export VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION
166 export CROSS_COMPILE
168 bold = $(shell tput bold)
169 normal = $(shell tput sgr0)
171 ifeq ("$(origin CROSS_COMPILE)", "command line")
172 WHAT := Cross compiling
173 else
174 WHAT := Building
175 endif
177 build_showinfo:
178 $(Q)echo "$(bold)$(WHAT) netsniff-ng toolkit ($(VERSION_STRING)) for" \
179 $(shell $(CCNQ) -dumpmachine)":$(normal)"
180 clean_showinfo:
181 $(Q)echo "$(bold)Cleaning netsniff-ng toolkit ($(VERSION_STRING)):$(normal)"
183 %.yy.o: %.l
184 $(LEX) -P $(shell perl -wlne 'print $$1 if /lex-func-prefix:\s([a-z]+)/' $<) \
185 -o $(BUILD_DIR)/$(shell basename $< .l).yy.c $(LEX_FLAGS) $<
186 %.tab.o: %.y
187 $(YAAC) -p $(shell perl -wlne 'print $$1 if /yaac-func-prefix:\s([a-z]+)/' $<) \
188 -o $(BUILD_DIR)/$(shell basename $< .y).tab.c $(YAAC_FLAGS) -d $<
190 .PHONY: all toolkit $(TOOLS) clean %_prehook %_distclean %_clean %_install tag tags cscope
191 .FORCE:
192 .DEFAULT_GOAL := all
193 .DEFAULT:
194 .IGNORE: %_clean_custom %_install_custom
195 .NOTPARALLEL: $(TOOLS)
197 NCONF_FILES = ether.conf tcp.conf udp.conf oui.conf geoip.conf
199 all: build_showinfo toolkit
200 allbutcurvetun: $(filter-out curvetun,$(TOOLS))
201 allbutmausezahn: $(filter-out mausezahn,$(TOOLS))
202 toolkit: $(TOOLS)
203 install: install_all
204 install_all: $(foreach tool,$(TOOLS),$(tool)_install)
205 install_allbutcurvetun: $(foreach tool,$(filter-out curvetun,$(TOOLS)),$(tool)_install)
206 install_allbutmausezahn: $(foreach tool,$(filter-out mausezahn,$(TOOLS)),$(tool)_install)
207 clean mostlyclean: $(foreach tool,$(TOOLS),$(tool)_clean)
208 $(Q)$(call RM,Config)
209 realclean distclean clobber: $(foreach tool,$(TOOLS),$(tool)_distclean)
210 $(Q)$(call RMDIR,$(ETCDIRE))
211 mrproper: clean distclean
212 $(Q)$(GIT_REM)
214 define TOOL_templ
215 include $(1)/Makefile
216 $(1) $(1)%: BUILD_DIR := $(1)
217 $(1)_prehook:
218 $(Q)echo "$(bold)$(WHAT) $(1):$(normal)"
219 $(1): $(1)_prehook $$($(1)-lex) $$($(1)-yaac) $$(patsubst %.o,$(1)/%.o,$$($(1)-objs))
220 $(1)_clean: $(1)_clean_custom
221 $(Q)$$(call RM,$(1)/*.o $(1)/$(1) $(1)/*.gz)
222 $(1)_install: $(1)_install_custom
223 $(Q)$$(call INSTX,$(1)/$(1),$$(SBINDIR))
224 $(Q)$(GZIP) $(1).8 > $(1)/$(1).8.gz
225 $(Q)$$(call INSTX,$(1)/$(1).8.gz,$$(MAN8DIR))
226 $(1)_distclean: $(1)_distclean_custom
227 $(Q)$$(call RM,$$(SBINDIR)/$(1))
228 $(Q)$$(call RM,$$(MAN8DIR)/$(1).8.gz)
229 $(1)/%.yy.o: $(1)/%.yy.c
230 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
231 $(1)/%.tab.o: $(1)/%.tab.c
232 $$(CC) $$(ALL_CFLAGS) -o $$@ -c $$<
233 $(1)/%.o: %.c
234 $$(CC) $$(ALL_CFLAGS) -o $(1)/$$(shell basename $$@) -c $$<
235 endef
237 $(foreach tool,$(TOOLS),$(eval $(call TOOL_templ,$(tool))))
239 %:: ;
241 netsniff-ng: ALL_CFLAGS += $(shell pkg-config --cflags libnl-3.0) $(shell pkg-config --cflags libnl-genl-3.0) -D__WITH_PROTOS -D__WITH_TCPDUMP_LIKE_FILTER
242 trafgen: ALL_CFLAGS += -I.. $(shell pkg-config --cflags libnl-3.0) $(shell pkg-config --cflags libnl-genl-3.0) -D__WITH_PROTOS
243 ifpps: ALL_CFLAGS += $(shell pkg-config --cflags ncurses)
244 flowtop: ALL_CFLAGS += $(shell pkg-config --cflags ncurses)
245 bpfc: ALL_CFLAGS += -I..
246 curvetun: ALL_CFLAGS += -I ${NACL_INC_DIR}
247 curvetun: ALL_LDFLAGS += -L ${NACL_LIB_DIR}
248 # This gets some extra treatment here until the code looks properly
249 mausezahn: ALL_CFLAGS = -O2 -I. -I.. -DVERSION_STRING=\"$(VERSION_STRING)\" -DPREFIX_STRING=\"$(PREFIX)\" -DVERSION_LONG=\"$(VERSION_LONG)\"
251 bpfc_clean_custom:
252 $(Q)$(call RM,$(BUILD_DIR)/*.h $(BUILD_DIR)/*.c)
253 trafgen_clean_custom:
254 $(Q)$(call RM,$(BUILD_DIR)/*.h $(BUILD_DIR)/*.c)
255 netsniff-ng_distclean_custom flowtop_distclean_custom:
256 $(Q)$(foreach file,$(NCONF_FILES),$(call RM,$(ETCDIRE)/$(file));)
257 $(Q)$(call RMDIR,$(ETCDIRE))
258 trafgen_distclean_custom:
259 $(Q)$(call RM,$(ETCDIRE)/stddef.h)
260 $(Q)$(call RMDIR,$(ETCDIRE))
261 astraceroute_distclean_custom:
262 $(Q)$(call RM,$(ETCDIRE)/geoip.conf)
263 $(Q)$(call RMDIR,$(ETCDIRE))
265 netsniff-ng_install_custom flowtop_install_custom:
266 $(Q)$(foreach file,$(NCONF_FILES),$(call INST,$(file),$(ETCDIRE));)
267 trafgen_install_custom:
268 $(Q)$(call INST,trafgen_stddef.h,$(ETCDIRE))
269 $(Q)ln -fs $(ETCDIRE)/trafgen_stddef.h $(ETCDIRE)/stddef.h
270 astraceroute_install_custom:
271 $(Q)$(call INST,geoip.conf,$(ETCDIRE))
273 $(TOOLS):
274 $(LD) $(ALL_LDFLAGS) -o $@/$@ $@/*.o $($@-libs)
275 $(STRIP) $@/$@
277 nacl:
278 $(Q)echo "$(bold)$(WHAT) $@:$(normal)"
279 $(Q)cd curvetun/ && ./nacl_build.sh ~/nacl
280 $(Q)source ~/.bashrc
282 tarball.gz: ; $(call GIT_ARCHIVE,gzip,gz)
283 tarball.bz2: ; $(call GIT_ARCHIVE,bzip2,bz2)
284 tarball.xz: ; $(call GIT_ARCHIVE,xz,xz)
285 tarball: tarball.gz tarball.bz2 tarball.xz
287 tag:
288 $(GIT_TAG)
290 announcement:
291 $(Q)echo -e "netsniff-ng $(VERSION_STRING) has been released to the public (http://netsniff-ng.org/).\n" > .MAIL_MSG
292 $(Q)echo -e "It can be fetched via Git, through:\n" >> .MAIL_MSG
293 $(Q)echo -e " git clone git://github.com/borkmann/netsniff-ng.git" >> .MAIL_MSG
294 $(Q)echo -e " git checkout $(VERSION_STRING)\n" >> .MAIL_MSG
295 $(Q)echo -e "Or via HTTP, through:\n" >> .MAIL_MSG
296 $(Q)echo -e " wget http://pub.netsniff-ng.org/netsniff-ng/netsniff-ng-$(VERSION_STRING).tar.gz\n" >> .MAIL_MSG
297 $(Q)echo -e "The release be verified via Git, through (see README):\n" >> .MAIL_MSG
298 $(Q)echo -e " git tag -v $(VERSION_STRING)\n" >> .MAIL_MSG
299 $(Q)echo -e "Major high-level changes since the last release are:\n" >> .MAIL_MSG
300 $(Q)echo -e " *** BLURB HERE ***\n" >> .MAIL_MSG
301 $(Q)echo -e "Contributors since last release:\n" >> .MAIL_MSG
302 $(GIT_PEOPLE) >> .MAIL_MSG
303 $(Q)echo -e "Git changelog since the last release:\n" >> .MAIL_MSG
304 $(GIT_LOG) >> .MAIL_MSG
306 release: announcement tag tarball
307 $(Q)echo "Released $(bold)$(VERSION_STRING)$(normal)"
309 FIND_SOURCE_FILES = ( git ls-files '*.[hcS]' 2>/dev/null || \
310 find . \( -name .git -type d -prune \) \
311 -o \( -name '*.[hcS]' -type f -print \) )
313 tags ctags:
314 $(Q)$(call RM,tags)
315 $(FIND_SOURCE_FILES) | xargs ctags -a
317 cscope:
318 $(Q)$(call RM,cscope*)
319 $(FIND_SOURCE_FILES) | xargs cscope -b
321 help:
322 $(Q)echo "$(bold)Available tools from the toolkit:$(normal)"
323 $(Q)echo " <toolnames>:={$(TOOLS)}"
324 $(Q)echo "$(bold)Targets for building the toolkit:$(normal)"
325 $(Q)echo " all|toolkit - Build the whole toolkit"
326 $(Q)echo " allbutcurvetun - Build all except curvetun"
327 $(Q)echo " allbutmausezahn - Build all except mausezahn"
328 $(Q)echo " <toolname> - Build only one of the tools"
329 $(Q)echo "$(bold)Targets for cleaning the toolkit's build files:$(normal)"
330 $(Q)echo " clean|mostlyclean - Remove all build files"
331 $(Q)echo " <toolname>_clean - Remove only one of the tool's files"
332 $(Q)echo "$(bold)Targets for installing the toolkit:$(normal)"
333 $(Q)echo " install - Install the whole toolkit"
334 $(Q)echo " <toolname>_install - Install only one of the tools"
335 $(Q)echo "$(bold)Targets for removing the toolkit:$(normal)"
336 $(Q)echo " realclean|distclean|clobber - Remove the whole toolkit from the system"
337 $(Q)echo " <toolname>_distclean - Remove only one of the tools"
338 $(Q)echo " mrproper - Remove build and install files"
339 $(Q)echo "$(bold)Hacking/development targets:$(normal)"
340 $(Q)echo " tag - Generate Git tag of current version"
341 $(Q)echo " tarball - Generate tarball of latest version"
342 $(Q)echo " release - Generate a new release"
343 $(Q)echo " tags - Generate sparse ctags"
344 $(Q)echo " cscope - Generate cscope files"
345 $(Q)echo "$(bold)Misc targets:$(normal)"
346 $(Q)echo " nacl - Execute the build_nacl script"
347 $(Q)echo " help - Show this help"
348 $(Q)echo "$(bold)Available parameters:$(normal)"
349 $(Q)echo " DEBUG=1 / DISTRO=1 - Enable debugging / Build for distros"
350 $(Q)echo " HARDENING=1 - Enable GCC hardening of executables"
351 $(Q)echo " PREFIX=/path - Install path prefix"
352 $(Q)echo " CROSS_COMPILE=/path-prefix - Kernel-like cross-compiling prefix"
353 $(Q)echo " CROSS_LD_LIBRARY_PATH=/path - Library search path for cross-compiling"
354 $(Q)echo " CC=cgcc - Use sparse compiler wrapper"
355 $(Q)echo " CFLAGS=\"-O2 -Wall ...\" - Overwrite CFLAGS for compilation"
356 $(Q)echo " CPPFLAGS=\"-I <path> ...\" - Additional CFLAGS for compilation"
357 $(Q)echo " LDFLAGS=\"-L <path> ...\" - Additional LDFLAGS for compilation"
358 $(Q)echo " CCACHE= - Do not use ccache for compilation"
359 $(Q)echo " Q= - Show verbose garbage"