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