make: use pathsubst to redirect to correct dir
[netsniff-ng.git] / src / Makefile
blob2606d2bcd0eec05fbf04fd02dceb437506b0c60b
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 $<
105 .PHONY: all toolkit $(TOOLS) clean %_prehook %_distclean %_clean %_install
106 .FORCE:
107 .DEFAULT_GOAL := all
108 .IGNORE: %_clean_custom
109 .NOTPARALLEL: $(TOOLS)
111 all: build_showinfo toolkit
112 toolkit: $(TOOLS)
113 clean mostlyclean: $(foreach tool,$(TOOLS),$(tool)_clean)
114 realclean distclean clobber: $(foreach tool,$(TOOLS),$(tool)_distclean)
116 # Tool template
117 define TOOL_templ
118 include $(1)/Makefile
119 $(1) $(1)%: BUILD_DIR := $(1)
120 $(1)_prehook:
121 @echo "$(bold)Building $(1):$(normal)"
122 $(1): $(1)_prehook $$($(1)-lex) $$($(1)-yaac) $$(patsubst %.o,$(1)/%.o,$$($(1)-objs)) $$($(1)-libs)
123 $(1)_clean:
124 @rm -rf $(1)/*.o $(1)/$(1)
125 $(1)_install:
126 @install -D $(1)/$(1) $$(SBINDIR)/$(1)
127 $(1)_distclean:
128 @rm $$(SBINDIR)/$(1)
129 $(1)/%.o: %.c
130 $$(CC) $$(ALL_CFLAGS) -o $(1)/$$(shell basename $$< .c).o -c $$<
131 endef
133 $(foreach tool,$(TOOLS),$(eval $(call TOOL_templ,$(tool))))
135 # Target specific additions ...
136 netsniff-ng: ALL_CFLAGS += -I$(INCDIR)/libnl3/ \
137 -D__WITH_PROTOS \
138 #-D__WITH_HARDWARE_TIMESTAMPING
139 trafgen: ALL_CFLAGS += -I.. -I$(INCDIR)/libnl3/ \
140 -D__WITH_PROTOS \
141 #-D__WITH_HARDWARE_TIMESTAMPING
142 #trafgen_clean_custom:
143 # @rm $(BUILD_DIR)/*.h $(BUILD_DIR)/*.c
144 bpfc: ALL_CFLAGS += -I..
146 $(TOOLS):
147 $(LD) $(ALL_LDFLAGS) -o $@/$@ $@/*.o $($@-libs)
148 $(STRIP) $@/$@