Initial import
[openvswitch] / datapath / linux-2.4 / kbuild.inc
1 # The remainder of this file is from Intel's e1000 distribution,
2 # with the following license:
3
4 ################################################################################
5 #
6 # Intel PRO/1000 Linux driver
7 # Copyright(c) 1999 - 2007 Intel Corporation.
8 #
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms and conditions of the GNU General Public License,
11 # version 2, as published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16 # more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 #
22 # The full GNU General Public License is included in this distribution in
23 # the file called "COPYING".
24 #
25 # Contact Information:
26 # Linux NICS <linux.nics@intel.com>
27 # e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
28 # Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
29 #
30 ################################################################################
31 ifeq (,$(BUILD_KERNEL))
32 BUILD_KERNEL=$(shell uname -r)
33 endif
34
35 ###########################################################################
36 # Environment tests
37
38 # Kernel Search Path
39 # All the places we look for kernel source
40 KSP :=  /lib/modules/$(BUILD_KERNEL)/build \
41         /lib/modules/$(BUILD_KERNEL)/source \
42         /usr/src/linux-$(BUILD_KERNEL) \
43         /usr/src/linux-$($(BUILD_KERNEL) | sed 's/-.*//') \
44         /usr/src/kernel-headers-$(BUILD_KERNEL) \
45         /usr/src/kernel-source-$(BUILD_KERNEL) \
46         /usr/src/linux-$($(BUILD_KERNEL) | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/') \
47         /usr/src/linux
48
49 # prune the list down to only values that exist
50 # and have an include/linux sub-directory
51 test_dir = $(shell [ -e $(dir)/include/linux ] && echo $(dir))
52 KSP := $(foreach dir, $(KSP), $(test_dir))
53
54 # we will use this first valid entry in the search path
55 ifeq (,$(KSRC))
56   KSRC := $(firstword $(KSP))
57 endif
58
59 CFLAGS += $(CFLAGS_EXTRA)
60
61 ifeq (,$(KSRC))
62   $(error Linux kernel source not found)
63 else
64 ifeq (/lib/modules/$(shell uname -r)/source, $(KSRC))
65   KOBJ :=  /lib/modules/$(shell uname -r)/build
66 else
67   KOBJ :=  $(KSRC)
68 endif
69 endif
70
71 # version 2.4 or 2.6?
72 K_VERSION:=$(shell if grep -q 'PATCHLEVEL = 4' $(KSRC)/Makefile; then echo 2.4; else echo 2.6; fi)
73
74 ifneq ($(K_VERSION),2.4)
75   $(error Linux kernel source in $(KSRC) not 2.4)
76 endif
77
78 # check for version.h and autoconf.h for running kernel in /boot (SUSE)
79 ifneq (,$(wildcard /boot/vmlinuz.version.h))
80   VERSION_FILE := /boot/vmlinuz.version.h
81   CONFIG_FILE  := /boot/vmlinuz.autoconf.h
82   KVER := $(shell $(CC) $(CFLAGS) -E -dM $(VERSION_FILE) | \
83           grep UTS_RELEASE | awk '{ print $$3 }' | sed 's/\"//g')
84   ifeq ($(KVER),$(shell uname -r))
85     # set up include path to override headers from kernel source
86     x:=$(shell rm -rf include)
87     x:=$(shell mkdir -p include/linux)
88     x:=$(shell cp /boot/vmlinuz.version.h include/linux/version.h)
89     x:=$(shell cp /boot/vmlinuz.autoconf.h include/linux/autoconf.h)
90     CFLAGS += -I./include
91   else
92     ifneq (,$(wildcard $(KOBJ)/include/linux/utsrelease.h))
93       VERSION_FILE := $(KOBJ)/include/linux/utsrelease.h
94     else
95       VERSION_FILE := $(KOBJ)/include/linux/version.h
96     endif
97     CONFIG_FILE  := $(KSRC)/include/linux/autoconf.h
98   endif
99 else
100   ifneq (,$(wildcard $(KOBJ)/include/linux/utsrelease.h))
101     VERSION_FILE := $(KOBJ)/include/linux/utsrelease.h
102   else
103     VERSION_FILE := $(KOBJ)/include/linux/version.h
104   endif
105   CONFIG_FILE  := $(KSRC)/include/linux/autoconf.h
106 endif
107
108 ifeq (,$(wildcard $(VERSION_FILE)))
109   $(error Linux kernel source not configured - missing version.h)
110 endif
111
112 ifeq (,$(wildcard $(CONFIG_FILE)))
113   $(error Linux kernel source not configured - missing autoconf.h)
114 endif
115
116 # pick a compiler
117 ifeq ($(K_VERSION),2.6)
118   CC := gcc cc
119 else
120   # Older kernels require GCC 2.95
121   K_SUBLEVEL:=$(shell sed -n 's/SUBLEVEL = // p' $(KSRC)/Makefile)
122   ifeq ($(K_SUBLEVEL),20)
123     CC := gcc-2.95
124   else
125     CC := gcc-3.4 gcc-3.3 gcc-2.95
126   endif
127 endif
128 test_cc = $(shell $(cc) --version > /dev/null 2>&1 && echo $(cc))
129 CC := $(foreach cc, $(CC), $(test_cc))
130 CC := $(firstword $(CC))
131 ifeq (,$(CC))
132   $(error Compiler not found)
133 endif
134
135 # we need to know what platform the driver is being built on
136 # some additional features are only built on Intel platforms
137 ifeq ($(ARCH),)
138   # Set the architecture if it hasn't been already set for cross-compilation
139   ARCH := $(shell uname -m | sed 's/i.86/i386/')
140 endif
141 ifeq ($(ARCH),alpha)
142   CFLAGS += -ffixed-8 -mno-fp-regs
143 endif
144 ifeq ($(ARCH),x86_64)
145   CFLAGS += -mcmodel=kernel -mno-red-zone
146 endif
147 ifeq ($(ARCH),ppc)
148   CFLAGS += -msoft-float
149 endif
150 ifeq ($(ARCH),ppc64)
151   CFLAGS += -m64 -msoft-float
152   LDFLAGS += -melf64ppc
153 endif
154
155 # standard flags for module builds
156 CFLAGS += -DLINUX -D__KERNEL__ -DMODULE -O2 -pipe -Wall
157 CFLAGS += -I$(KSRC)/include -I.
158 CFLAGS += -I$(srcdir)/compat-2.4 -I$(srcdir)/compat-2.4/include
159 CFLAGS += $(shell [ -f $(KSRC)/include/linux/modversions.h ] && \
160             echo "-DMODVERSIONS -DEXPORT_SYMTAB \
161                   -include $(KSRC)/include/linux/modversions.h")
162
163 RHC := $(KSRC)/include/linux/rhconfig.h
164 ifneq (,$(wildcard $(RHC)))
165   # 7.3 typo in rhconfig.h
166   ifneq (,$(shell $(CC) $(CFLAGS) -E -dM $(RHC) | grep __module__bigmem))
167         CFLAGS += -D__module_bigmem
168   endif
169 endif
170
171 # get the kernel version - we use this to find the correct install path
172 KVER := $(shell $(CC) $(CFLAGS) -E -dM $(VERSION_FILE) | grep UTS_RELEASE | \
173         awk '{ print $$3 }' | sed 's/\"//g')
174
175 # assume source symlink is the same as build, otherwise adjust KOBJ
176 ifneq (,$(wildcard /lib/modules/$(KVER)/build))
177 ifneq ($(KSRC),$(shell cd /lib/modules/$(KVER)/build ; pwd -P))
178   KOBJ=/lib/modules/$(KVER)/build
179 endif
180 endif
181
182 KKVER := $(shell echo $(KVER) | \
183          awk '{ if ($$0 ~ /2\.[4-9]\./) print "1"; else print "0"}')
184 ifeq ($(KKVER), 0)
185   $(error *** Aborting the build. \
186           *** This driver is not supported on kernel versions older than 2.4.0)
187 endif
188
189 # look for SMP in config.h
190 SMP := $(shell $(CC) $(CFLAGS) -E -dM $(CONFIG_FILE) | \
191          grep -w CONFIG_SMP | awk '{ print $$3 }')
192 ifneq ($(SMP),1)
193   SMP := 0
194 endif
195
196 #ifneq ($(SMP),$(shell uname -a | grep SMP > /dev/null 2>&1 && echo 1 || echo 0))
197 #  $(warning ***)
198 #  ifeq ($(SMP),1)
199 #    $(warning *** Warning: kernel source configuration (SMP))
200 #    $(warning *** does not match running kernel (UP))
201 #  else
202 #    $(warning *** Warning: kernel source configuration (UP))
203 #    $(warning *** does not match running kernel (SMP))
204 #  endif
205 #  $(warning *** Continuing with build,)
206 #  $(warning *** resulting driver may not be what you want)
207 #  $(warning ***)
208 #endif
209
210 ifeq ($(SMP),1)
211   CFLAGS += -D__SMP__
212 endif
213
214 ###########################################################################
215 # 2.4.x 
216
217 # Makefile for 2.4.x kernel
218 TARGET = openflow_mod.o unit_mod.o compat24_mod.o
219
220 CFLAGS += -Wno-sign-compare -fno-strict-aliasing
221 CFLAGS := -I $(srcdir)/compat-2.4/include $(CFLAGS)
222 CFLAGS := -I $(srcdir)/compat-2.4/include-$(ARCH) $(CFLAGS)
223
224 default: $(TARGET)
225
226 openflow_mod.o: $(filter-out $(TARGET), $(CFILES:.c=.o))
227         $(LD) $(LDFLAGS) -r $^ -o $@
228 unit_mod.o: $(UNIT_CFILES:.c=.o)
229         $(LD) $(LDFLAGS) -r $^ -o $@
230 compat24_mod.o: $(COMPAT24_CFILES:.c=.o)
231         $(LD) $(LDFLAGS) -r $^ -o $@
232
233 ALL_CFILES = $(FILES) $(UNIT_CFILES) $(COMPAT24_CFILES)
234 $(ALL_CFILES:.c=.o): $(HFILES) Makefile
235
236 # depmod version for rpm builds
237 DEPVER := $(shell /sbin/depmod -V 2>/dev/null | \
238           awk 'BEGIN {FS="."} NR==1 {print $$2}')
239
240 .PHONY: clean
241
242 clean:
243         rm -rf $(TARGET) $(TARGET:.ko=.o) $(TARGET:.ko=.mod.c)\
244     $(TARGET:.ko=.mod.o) $(CFILES:.c=.o) $(COMPAT24_CFILES:.c=.o)\
245     $(UNIT_CFILES:.c=.o) $(MANFILE).gz .*cmd .tmp_versions\
246     compat24_mod.o unit_mod.o tmp/ \