6ca0562876ab9e315a6c27d8d100cee56783e165
[ipfw-google.git] / kipfw / Makefile
1 # $Id: Makefile 12257 2013-04-26 21:13:24Z luigi $
2 # gnu Makefile to build linux/Windows module for ipfw+dummynet.
3 #
4 # The defaults are set to build without modifications on PlanetLab
5 # and possibly 2.6 versions.
6 # On Windows, we use gnu-make and MSC
7
8 # Some variables need to have specific names, because they are used
9 # by the build infrastructure on Linux and OpenWrt. They are:
10
11 #   ccflags-y   additional $(CC) flags
12 #   M           used by Kbuild, we must set it to `pwd`
13 #   obj-m       list of .o modules to build
14 #   $(MOD)-y    for each $MOD in obj-m, the list of objects
15 #   obj-y       same as above, for openwrt
16 #   O_TARGET    the link target, for openwrt
17 #   EXTRA_CFLAGS as the name says... in openwrt
18 #   EXTRA_CFLAGS is used in 2.6.22 module kernel compilation too
19 #   KERNELPATH  the path to the kernel sources or headers
20 #       (on planetlab it is set already by the build system,
21 #       for other systems we take KSRC which is either guessed
22 #       or taken from the command line.
23 #
24 # Not sure about this (the name might be reserved)
25 #   ipfw-cflags         our flags for building the module
26 #
27 # Other variables are only private and can be renamed. They include:
28 #
29 #   VER         linux version we are building for (2.4 2.6 or openwrt)
30 #
31 #---
32 #
33 # The windows files (passthru etc.) are modified version of the
34 # examples found in the $(DDK)/src/network/ndis/passthru/driver/
35 # They can be re-created using the 'ndis-glue' target in the 
36
37 include $(PWD)/../Makefile.inc
38
39 TARGET = kipfw
40
41 # lets default for 2.6 for planetlab builds
42 VER ?= 2.6
43
44 #--- General values for all types of build ---
45 # obj-m is the target module
46 obj-m := ipfw_mod.o
47
48 #-- the list of source files. IPFW_SRCS is our own name.
49 # Original ipfw and dummynet sources + FreeBSD stuff,
50 IPFW_SRCS := ip_fw2.c ip_fw_pfil.c ip_fw_sockopt.c
51 IPFW_SRCS += ip_fw_dynamic.c ip_fw_table.c ip_fw_log.c
52 IPFW_SRCS += radix.c in_cksum.c
53 IPFW_SRCS += ip_dummynet.c ip_dn_io.c ip_dn_glue.c
54 IPFW_SRCS += dn_heap.c
55 IPFW_SRCS += dn_sched_fifo.c dn_sched_wf2q.c
56 IPFW_SRCS += dn_sched_rr.c dn_sched_qfq.c
57 IPFW_SRCS += dn_sched_prio.c
58 # Module glue and functions missing in linux
59 IPFW_SRCS += ipfw2_mod.c bsd_compat.c
60
61 # generic cflags used on all systems
62 #ipfw-cflags += -DIPFW_HASHTABLES
63 ipfw-cflags += -DIPFIREWALL_DEFAULT_TO_ACCEPT
64 # _BSD_SOURCE enables __FAVOR_BSD (udp/tcp bsd structs instead of posix)
65 ipfw-cflags += -D_BSD_SOURCE
66 ipfw-cflags += -DKERNEL_MODULE  # build linux kernel module
67 # the two header trees for empty and override files
68 ipfw-cflags += -I $(M)/include_e
69 ipfw-cflags += -I $(M)/../sys
70 ipfw-cflags += -include $(M)/../glue.h  # headers
71 ipfw-cflags += -include $(M)/missing.h  # headers
72
73 ifeq ($(OSARCH),Windows)        #--- {  Windows block
74 ifeq ($(VER),win64)
75     $(warning ---- building for 64-bit windows ---)
76     win_arch= -DAMD64=1
77 else
78     win_arch= -Di386=1
79 endif
80     M ?= $(shell pwd)
81     WIN_SRCS += md_win.c
82     WIN_SRCS += miniport.c protocol.c passthru.c debug.c
83     #compiler, linker, target, sources and objects
84     #DDK is exported from the root makefile
85     #DDK = C:/WinDDK/7600.16385.1
86
87     CSOURCES = $(IPFW_SRCS) $(WIN_SRCS)
88
89     COBJS := $(CSOURCES:.c=.obj)
90     COBJS := $(addprefix $(OBJDIR)/,$(COBJS))
91
92     #include paths
93     INCLUDE_PATHS = -Ii386 -I../sys -Iinclude_e -I.
94     # INCLUDE_PATHS += -I$(OBJDIR)
95     INCLUDE_PATHS += -I$(DDK)/inc/api
96     INCLUDE_PATHS += -I$(DDK)/inc/ddk
97     INCLUDE_PATHS += -I$(DDK)/inc/crt
98
99     # #preprocessor MS defines
100     PREPROC  = -D_X86_=1 -Di386=1 -DSTD_CALL -DCONDITION_HANDLING=1
101     PREPROC += -DNT_UP=0 -DNT_INST=0 -DWIN32=100 -D_NT1X_=100 -DWINNT=1
102     PREPROC += -D_WIN32_WINNT=0x0501 -DWINVER=0x0501 -D_WIN32_IE=0x0603
103     PREPROC += -DWIN32_LEAN_AND_MEAN=1 
104     PREPROC += -D__BUILDMACHINE__=WinDDK -DFPO=0 -D_DLL=1
105     PREPROC += -DNDIS_MINIPORT_DRIVER -DNDIS_WDM=1
106     PREPROC += -DNDIS51_MINIPORT=1 -DNDIS51=1
107     PREPROC += -DMSC_NOOPT -DNTDDI_VERSION=0x05010200
108     PREPROC += -DKMDF_MAJOR_VERSION_STRING=01 -DKMDF_MINOR_VERSION_STRING=009
109     #PREPROC += -DDBG=1 #debug
110     PREPROC += -DNDEBUG #always up, seems no effect, possibly no debug?
111     PREPROC += -DDEVL=1 #always up, seems no effect
112     #macroing module name, WARNING: must match the one in .inf files
113     PREPROC += -DMODULENAME=Ipfw 
114
115     #our defines
116     OUR_PREPROC  = -D_KERNEL -DKERNEL_MODULE -DKLD_MODULE
117     OUR_PREPROC += -D__BSD_VISIBLE -DIPFIREWALL_DEFAULT_TO_ACCEPT
118     OUR_PREPROC += -D__LITTLE_ENDIAN -DSYSCTL_NODE -DEMULATE_SYSCTL
119
120 ifeq ($(TCC),)  # Microsoft C compiler
121     CC = $(DDK)/bin/x86/x86/cl.exe
122     LD = $(DDK)/bin/x86/x86/link.exe
123     # #complier options
124     CFLAGS  = -Fo$(OBJDIR)/  -c -FC -Zc:wchar_t-
125     CFLAGS += -Zl -Zp8 -Gy -Gm- -GF -cbstring -Gz -hotpatch -EHs-c-
126     CFLAGS += -W2 # -W3 gives too many conversion errors
127     CFLAGS += -GR- -GF -GS -Zi  # XXX do we need this ?
128     CFLAGS += -Fd$(OBJDIR)/
129     CFLAGS += -wd4603 -wd4627 -typedil-
130     CFLAGS += -FI $(DDK)/inc/api/warning.h
131     CFLAGS += -FI winmissing.h
132     CFLAGS += -FI missing.h     # headers
133     CFLAGS += -FI ../glue.h     # headers
134
135     #optimization options
136     OPTIMIZE = -Od -Oi -Oy-
137
138     #linker options
139     LDFLAGS  = /MERGE:_PAGE=PAGE /MERGE:_TEXT=.text
140     LDFLAGS += /SECTION:INIT,d /OPT:REF /OPT:ICF
141     LDFLAGS += /IGNORE:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221
142     LDFLAGS += /INCREMENTAL:NO /release /NODEFAULTLIB /WX
143     LDFLAGS += /debug /debugtype:cv,fixup,pdata
144     LDFLAGS += /version:6.1 /osversion:6.1 /functionpadmin:5
145     LDFLAGS += /safeseh /pdbcompress
146     LDFLAGS += /STACK:0x40000,0x1000 /driver /base:0x10000 /align:0x80
147     LDFLAGS += /stub:$(DDK)\\lib\\wxp\\stub512.com
148     LDFLAGS += /subsystem:native,5.01 /entry:GsDriverEntry@8
149     LDFLAGS += /out:$(OBJDIR)/ipfw.sys
150
151     #libraries to build against
152     LIBS  = $(DDK)/lib/wxp/i386/BufferOverflowK.lib
153     LIBS += $(DDK)/lib/wxp/i386/ntoskrnl.lib
154     LIBS += $(DDK)/lib/wxp/i386/hal.lib
155     LIBS += $(DDK)/lib/wxp/i386/wmilib.lib
156     LIBS += $(DDK)/lib/wxp/i386/ndis.lib
157     LIBS += $(DDK)/lib/wxp/i386/sehupd.lib
158 else    # use tcc. not working yet for the kernel module.
159     # TCC points to the root of tcc tree
160     CC=$(TCC)/bin/wintcc
161     EXTRA_CFLAGS += -DTCC -I..
162     EXTRA_CFLAGS += -I$(TCC)/include/winapi -I$(TCC)/include
163     EXTRA_CFLAGS += -nostdinc
164
165     CFLAGS += -include winmissing.h -include missing.h -include ../glue.h
166     CFLAGS += -I../../inc/api -I../../inc/ddk -I../../inc/crt
167     CFLAGS += -DRC_INVOKED
168 endif # use tcc
169
170     #empty include directory to be built
171     M ?= $(shell pwd)
172     EFILES_asm += div64.h
173     EFILES_linux += if.h random.h errno.h
174     EFILES_net += if_types.h inet_hashtables.h route.h
175
176     #targets
177 all: $(TARGET)
178
179 $(TARGET): include_e
180         # XXX dangerous rm -rf $(OBJDIR)
181         mkdir -p $(OBJDIR)
182         $(MSG) "  CC [$(CC)] $(CSOURCES)"
183         $(HIDE) $(CC) $(INCLUDE_PATHS) $(PREPROC) $(OUR_PREPROC) $(CFLAGS) $(OPTIMIZE) $(CSOURCES)
184         $(MSG) "  LD [$(LD)] $(COBJS)"
185         $(HIDE) $(LD) $(LDFLAGS) $(COBJS) $(LIBS)
186
187 else # } { linux variables and targets
188
189 # We have three sections: OpenWrt, Linux 2.4 and Linux 2.6
190
191 ifeq ($(VER),openwrt)   #--- { The Makefile section for openwrt ---
192   # We do not include a dependency on include_e as it is called
193   # by Makefile.openwrt in Build/Prepare
194   M=.
195   obj-y := $(IPFW_SRCS:%.c=%.o)
196   O_TARGET := $(obj-m)
197
198   # xcflags-y is a temporary variable where we store build options
199   xcflags-y += -O1 -DLINUX_24
200   xcflags-y += -g
201
202   EXTRA_CFLAGS := $(xcflags-y) $(ipfw-cflags) -DSYSCTL_NODE -DEMULATE_SYSCTL
203
204   # we should not export anything
205   #export-objs := ipfw2_mod.o
206 -include $(TOPDIR)/Rules.make
207 endif # ---- } end openwrt version
208
209
210 ifneq ($(shell echo $(VER)|grep '2.4'),)        #--- {
211   # Makefile section for the linux 2.4 version
212   # tested on linux-2.4.35.4, does not work with 2.4.37
213   #
214   # guess the kernel path -- or is it under /lib/modules ?
215   KERNELPATH ?= $(KSRC)
216
217   # We need to figure out the gcc include directory, if not
218   # set by the user through MYGCC_INCLUDE
219   # Find compiler version (3rd field in last line returned by gcc -v)
220   # e.g.        gcc version 4.3.2 (Debian 4.3.2-1.1)
221   MYGCC_VER ?= $(shell $(CC) -v 2>&1 |tail -n 1 | cut -d " " -f 3)
222   # We don't know the exact directory under /usr/lib/gcc so we guess
223   MYGCC_INCLUDE ?= $(shell echo /usr/lib/gcc/*/$(MYGCC_VER) | cut -d " " -f 1)/include
224   $(warning "---- gcc includes guessed to $(MYGCC_INCLUDE)")
225
226   # additional warning
227   WARN += -Wall -Wundef
228   WARN += -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing
229   WARN += -fno-common -Werror-implicit-function-declaration
230   # WARN += -O2  -fno-stack-protector -m32 -msoft-float -mregparm=3
231   # -mregparm=3 gives a printk error
232   WARN += -m32 -msoft-float # -mregparm=3
233   #WARN += -freg-struct-return -mpreferred-stack-boundary=2
234   WARN += -Wno-sign-compare
235   WARN += -Wdeclaration-after-statement
236   ifneq ($(MYGCC_VER),3.4.6)
237         WARN += -Wno-pointer-sign
238   endif
239
240   ccflags-y += -O1 -DLINUX_24
241   CFLAGS = -DMODULE -D__KERNEL__ -nostdinc \
242         -isystem ${KERNELPATH}/include -isystem $(MYGCC_INCLUDE) \
243         ${ccflags-y}
244   # The Main target
245 all: mod24
246
247 else # --- } {  linux 2.6 and newer
248
249   # This is the Makefile section for Linux 2.6.x including planetlab
250
251 ifeq ($(IPFW_PLANETLAB),1)
252   $(warning "---- Building for PlanetLab")
253   ipfw-cflags += -DIPFW_PLANETLAB        # PlanetLab compilation
254 endif
255   # if not set, use the version from the installed system
256   KERNELPATH ?= $(KSRC)
257 #  $(warning "---- Building Version 2.6 $(VER) in $(KERNELPATH)")
258   WARN := -O1 -Wall -Werror -DDEBUG_SPINLOCK -DDEBUG_MUTEXES
259   # The main target
260
261   # Required by GCC 4.6
262   ccflags-y += -Wno-unused-but-set-variable
263
264   # extract version number (decimal). Newer linuxes have a different dir
265   LINUX_VERSION_CODE := $(shell V=linux/version.h; G=. ; \
266         [ -f $(KERNELPATH)/include/$${V} ] || G=generated/uapi ;\
267         grep LINUX_VERSION_CODE $(KERNELPATH)/include/$${G}/linux/version.h | \
268         awk '{printf "%d", $$3} ')
269
270   #     awk '{printf "%d %03x%02d", $$3, $$3/256, $$3%256} ')
271   # $(warning version $(LINUX_VERSION_CODE))
272
273   ifeq ($(shell if [ -z $(LINUX_VERSION_CODE) ] ; then echo "true"; fi),true)
274     $(warning "---- Perhaps you miss a (cd $(KERNELPATH); make oldconfig; make prepare; make scripts)");
275   endif
276
277   # Required by kernel <= 2.6.22, ccflags-y is used on newer version
278   ifeq ($(shell if [ "$(LINUX_VERSION_CODE)" -le 132630 ] ; then echo "true"; fi),true)
279     EXTRA_CFLAGS += $(ccflags-y)
280   endif
281
282   $(warning $(shell [ "$(LINUX_VERSION_CODE)" -le 132635 ] && \
283         [ `$(MAKE) -version | head -1 | cut -d " " -f 3` != '3.81' ] && \
284         echo "****   need make 3.81 *****") )
285   # $(warning make is $(MAKE) version is $(shell $(MAKE) -version | head -1) )
286
287 all: $(TARGET)
288 $(TARGET):      include_e
289         $(MAKE) -C $(KERNELPATH) V=$(V) M=`pwd` modules
290
291 endif # } --- linux 2.6 and newer
292
293 #-- back to the common section for linux
294
295 # the list of objects used to build the module
296 ipfw_mod-y = $(IPFW_SRCS:%.c=%.o)
297
298 # additional $(CC) flags
299 ccflags-y += $(WARN)
300 ccflags-y += $(ipfw-cflags)
301 # if we really want debug symbols...
302 ccflags-y += -g
303
304 mod24: include_e $(obj-m)
305
306 $(obj-m): $(ipfw_mod-y)
307         $(LD) $(LDFLAGS) -m elf_i386 -r -o $@ $^
308
309 # M is the current directory, used in recursive builds
310 # so we allow it to be overridden
311 M ?= $(shell pwd)
312
313 endif # } ----- end of the non-Windows block
314
315 ifneq ($(OBJDIR),mia)
316     $(error objdir set to $(OBJDIR))
317 endif
318
319 #--- various common targets
320 clean:
321         -@rm -f *.o *.ko Module.symvers *.mod.c
322         -@# rm -rf $(OBJDIR)
323         -@rm -rf include_e
324
325 distclean: clean
326         -@rm -f .*cmd modules.order opt_*
327         -@rm -rf .tmp_versions .*.o.d _CL_*
328
329 # support to create empty dirs and files in include_e/
330 # EFILES_foo/bar is the list of files to be created in foo/bar
331 # (/ and . are allowed in gmake variable names)
332
333 EFILES_. += opt_inet.h opt_inet6.h opt_ipfw.h opt_ipsec.h opt_mpath.h
334 EFILES_. += opt_mbuf_stress_test.h opt_param.h opt_ipdivert.h
335
336 EFILES_altq += if_altq.h
337 EFILES_arpa += inet.h
338 EFILES_machine += in_cksum.h
339 EFILES_net += ethernet.h netisr.h pf_mtag.h bpf.h if_types.h vnet.h
340
341 EFILES_netinet += ether.h icmp6.h if_ether.h in.h in_pcb.h in_var.h
342 EFILES_netinet += in_systm.h ip_carp.h ip_var.h pim.h
343 EFILES_netinet += sctp.h tcp_timer.h tcpip.h udp_var.h
344 EFILES_netinet6 += ip6_var.h
345
346 EFILES_sys += _lock.h _rwlock.h rmlock.h _mutex.h jail.h
347 EFILES_sys += condvar.h eventhandler.h domain.h
348 EFILES_sys += limits.h lock.h mutex.h priv.h
349 EFILES_sys += proc.h rwlock.h socket.h socketvar.h
350 EFILES_sys += sysctl.h time.h ucred.h
351
352 # first make a list of directories from variable names
353 EDIRS= $(subst EFILES_,,$(filter EFILES_%,$(.VARIABLES)))
354 # then prepend the directory name to individual files.
355 #       $(empty) serves to interpret the following space literally,
356 #       and the ":  = " substitution packs spaces into one.
357 EFILES = $(foreach i,$(EDIRS),$(subst $(empty) , $(i)/, $(EFILES_$(i):  = )))
358
359 include_e:
360         -@rm -rf $(M)/include_e opt_*
361         -@mkdir -p $(M)/include_e
362         -@(cd $(M)/include_e; mkdir -p $(EDIRS); touch $(EFILES) )
363
364 #--- some other targets for testing purposes
365 test_radix: test_radix.o radix.o
366 test_lookup: ip_fw_lookup.o
367 test_radix test_lookup: CFLAGS=-Wall -Werror -O1