make: cleanups, added ccache
[netsniff-ng.git] / src / Makefile
blobd753b33a62d6f65a65cb52daa54520c874d788b9
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 # Disable if you don't want it
16 CCACHE = ccache
18 # Location of installation paths.
19 BINDIR = $(PREFIX)/usr/bin
20 SBINDIR = $(PREFIX)/usr/sbin
21 INCDIR = $(PREFIX)/usr/include
22 ETCDIR = $(PREFIX)/etc
24 # Shut up make, helper warnings, parallel compilation!
25 MAKEFLAGS += --no-print-directory
26 MAKEFLAGS += -rR
27 MAKEFLAGS += --warn-undefined-variables
28 MAKEFLAGS += --jobs=$(shell grep "^processor" /proc/cpuinfo | wc -l)
30 # For packaging purposes, you might want to disable O3+arch tuning
31 CFLAGS = -fstack-protector
32 CFLAGS += --param=ssp-buffer-size=4
33 CFLAGS += -fpie
34 CFLAGS += -fno-strict-aliasing
35 CFLAGS += -fexceptions
36 CFLAGS += -fasynchronous-unwind-tables
37 CFLAGS += -fno-delete-null-pointer-checks
38 CFLAGS += -D_FORTIFY_SOURCE=2
39 CFLAGS += -D_REENTRANT
40 CFLAGS += -D_FILE_OFFSET_BITS=64
41 CFLAGS += -D_LARGEFILE_SOURCE
42 CFLAGS += -D_LARGEFILE64_SOURCE
43 CFLAGS += -DVERSION_STRING=\"$(VERSION_STRING)\"
44 CFLAGS += -std=gnu99
45 CFLAGS += -march=native
46 CFLAGS += -mtune=native
47 CFLAGS += -O3
48 CFLAGS += -g
50 # Should be commented in after the mausezahn mess is cleaned
51 WFLAGS = -Wall
52 #WFLAGS += -Wmissing-prototypes
53 #WFLAGS += -Wdeclaration-after-statement
54 #WFLAGS += -Werror-implicit-function-declaration
55 #WFLAGS += -Wundef
56 #WFLAGS += -Wstrict-prototypes
57 #WFLAGS += -Wunused-but-set-variable
58 #WFLAGS += -Werror
59 WFLAGS += -Wformat=2
61 CFLAGS += $(WFLAGS) -I.
62 CPPFLAGS =
63 ifeq ("$(origin CROSS_LD_LIBRARY_PATH)", "command line")
64 LDFLAGS = -L$(CROSS_LD_LIBRARY_PATH)
65 else
66 LDFLAGS =
67 endif
69 ALL_CFLAGS = $(CFLAGS)
70 ALL_LDFLAGS = $(LDFLAGS)
71 TARGET_ARCH =
72 LEX_FLAGS =
73 YAAC_FLAGS =
75 LD = @echo -e " LD\t$@" && $(CCACHE) $(CROSS_COMPILE)gcc
76 CCNQ = $(CCACHE) $(CROSS_COMPILE)gcc
77 CC = @echo -e " CC\t$<" && $(CCNQ)
78 RM = @echo -e " EXEC\t$@" && rm
79 MKDIR = @echo -e " MKDIR\t$@" && mkdir
80 STRIP = @echo -e " STRIP\t$@" && $(CROSS_COMPILE)strip
81 LEX = @echo -e " LEX\t$<" && flex
82 YAAC = @echo -e " YAAC\t$<" && bison
84 export VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION
85 export CROSS_COMPILE LD CC STRIP
87 VERSION_STRING = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
89 bold = $(shell tput bold)
90 normal = $(shell tput sgr0)
92 build_showinfo:
93 @echo "$(bold)Building netsniff-ng toolkit ($(VERSION_STRING)) for" \
94 $(shell $(CCNQ) -dumpmachine)":$(normal)"
95 clean_showinfo:
96 @echo "$(bold)Cleaning netsniff-ng toolkit ($(VERSION_STRING)):$(normal)"
98 %.yy.o: %.l
99 $(LEX) -P $(shell perl -wlne 'print $$1 if /lex-func-prefix:\s([a-z]+)/' $<) \
100 -o $(BUILD_DIR)/$(shell basename $< .l).yy.c $(LEX_FLAGS) $<
101 %.tab.o: %.y
102 $(YAAC) -p $(shell perl -wlne 'print $$1 if /yaac-func-prefix:\s([a-z]+)/' $<) \
103 -o $(BUILD_DIR)/$(shell basename $< .y).tab.c $(YAAC_FLAGS) -d $<
104 %.o: %.c
105 $(CC) $(ALL_CFLAGS) -o $(BUILD_DIR)/$(shell basename $< .c).o -c $<
107 .PHONY: all $(TOOLS) %.o %.c clean %_prehook %_distclean %_clean %_install
108 .DEFAULT_GOAL := all
109 .IGNORE: %_clean_custom
110 .NOTPARALLEL: $(TOOLS)
112 all: build_showinfo toolkit
113 toolkit: $(TOOLS)
114 clean mostlyclean: $(foreach tool,$(TOOLS),$(tool)_clean)
115 realclean distclean clobber: $(foreach tool,$(TOOLS),$(tool)_distclean)
117 # Tool template
118 define TOOL_templ
119 include $(1)/Makefile
120 $(1) $(1)%: BUILD_DIR := $(1)
121 $(1)_prehook:
122 @echo "$(bold)Building $(1):$(normal)"
123 $(1): $(1)_prehook $$($(1)-lex) $$($(1)-yaac) $$($(1)-objs) $$($(1)-libs)
124 $(1)_clean:
125 @rm -rf $$(BUILD_DIR)/*.o $$(BUILD_DIR)/$(1)
126 $(1)_install:
127 @install -D $$(BUILD_DIR)/$(1) $$(SBINDIR)/$(1)
128 $(1)_distclean:
129 @rm $$(SBINDIR)/$(1)
130 endef
132 $(foreach tool,$(TOOLS),$(eval $(call TOOL_templ,$(tool))))
134 # Target specific additions ...
135 netsniff-ng: ALL_CFLAGS += -I$(INCDIR)/libnl3/ \
136 -D__WITH_PROTOS \
137 #-D__WITH_HARDWARE_TIMESTAMPING
138 trafgen: ALL_CFLAGS += -I.. -I$(INCDIR)/libnl3/ \
139 -D__WITH_PROTOS \
140 #-D__WITH_HARDWARE_TIMESTAMPING
141 #trafgen_clean_custom:
142 # @rm $(BUILD_DIR)/*.h $(BUILD_DIR)/*.c
143 bpfc: ALL_CFLAGS += -I..
145 $(TOOLS):
146 $(LD) $(ALL_LDFLAGS) -o $@/$@ $@/*.o $($@-libs)
147 $(STRIP) $@/$@