Initial import
[openvswitch] / datapath / linux-2.6 / 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 ifeq (,$(CC))
39   CC := gcc cc
40 endif
41
42 ifeq (,$(KSRC))
43   $(error Linux kernel source not found)
44 else
45 ifeq (/lib/modules/$(shell uname -r)/source, $(KSRC))
46   KOBJ :=  /lib/modules/$(shell uname -r)/build
47 else
48   KOBJ :=  $(KSRC)
49 endif
50 endif
51
52 # version 2.4 or 2.6? (we assume 2.2 isn't in use anymore
53 K_VERSION:=$(shell if grep -q 'PATCHLEVEL = 4' $(KSRC)/Makefile; then echo 2.4; else echo 2.6; fi)
54
55 ifneq ($(K_VERSION),2.6)
56   $(error Linux kernel source not not 2.6)
57 endif
58
59 # check for version.h and autoconf.h for running kernel in /boot (SUSE)
60 ifneq (,$(wildcard /boot/vmlinuz.version.h))
61   VERSION_FILE := /boot/vmlinuz.version.h
62   CONFIG_FILE  := /boot/vmlinuz.autoconf.h
63   KVER := $(shell $(CC) $(EXTRA_CFLAGS) -E -dM $(VERSION_FILE) | \
64           grep UTS_RELEASE | awk '{ print $$3 }' | sed 's/\"//g')
65   ifeq ($(KVER),$(shell uname -r))
66     # set up include path to override headers from kernel source
67     x:=$(shell rm -rf include)
68     x:=$(shell mkdir -p include/linux)
69     x:=$(shell cp /boot/vmlinuz.version.h include/linux/version.h)
70     x:=$(shell cp /boot/vmlinuz.autoconf.h include/linux/autoconf.h)
71     EXTRA_CFLAGS += -I./include
72   else
73     ifneq (,$(wildcard $(KOBJ)/include/linux/utsrelease.h))
74       VERSION_FILE := $(KOBJ)/include/linux/utsrelease.h
75     else
76       VERSION_FILE := $(KOBJ)/include/linux/version.h
77     endif
78     CONFIG_FILE  := $(KSRC)/include/linux/autoconf.h
79   endif
80 else
81   ifneq (,$(wildcard $(KOBJ)/include/linux/utsrelease.h))
82     VERSION_FILE := $(KOBJ)/include/linux/utsrelease.h
83   else
84     VERSION_FILE := $(KOBJ)/include/linux/version.h
85   endif
86   CONFIG_FILE  := $(KSRC)/include/linux/autoconf.h
87 endif
88
89 ifeq (,$(wildcard $(VERSION_FILE)))
90   $(error Linux kernel source not configured - missing version.h)
91 endif
92
93 ifeq (,$(wildcard $(CONFIG_FILE)))
94   $(error Linux kernel source not configured - missing autoconf.h)
95 endif
96
97
98 test_cc = $(shell $(cc) --version > /dev/null 2>&1 && echo $(cc))
99 CC := $(foreach cc, $(CC), $(test_cc))
100 CC := $(firstword $(CC))
101 ifeq (,$(CC))
102   $(error Compiler not found)
103 endif
104
105 # we need to know what platform the driver is being built on
106 # some additional features are only built on Intel platforms
107 ifeq ($(ARCH),)
108   # Set the architecture if it hasn't been already set for cross-compilation
109   ARCH := $(shell uname -m | sed 's/i.86/i386/')
110 endif
111 ifeq ($(ARCH),alpha)
112   EXTRA_CFLAGS += -ffixed-8 -mno-fp-regs
113 endif
114 ifeq ($(ARCH),x86_64)
115   EXTRA_CFLAGS += -mcmodel=kernel -mno-red-zone
116 endif
117 ifeq ($(ARCH),ppc)
118   EXTRA_CFLAGS += -msoft-float
119 endif
120 ifeq ($(ARCH),ppc64)
121   EXTRA_CFLAGS += -m64 -msoft-float
122   LDFLAGS += -melf64ppc
123 endif
124
125 # standard flags for module builds
126 EXTRA_CFLAGS += -DLINUX -D__KERNEL__ -DMODULE -O2 -pipe -Wall
127 NOSTDINC_FLAGS += -I$(srcdir)/compat-2.6 -I$(srcdir)/compat-2.6/include 
128 EXTRA_CFLAGS += $(shell [ -f $(KSRC)/include/linux/modversions.h ] && \
129             echo "-DMODVERSIONS -DEXPORT_SYMTAB \
130                   -include $(KSRC)/include/linux/modversions.h")
131
132 RHC := $(KSRC)/include/linux/rhconfig.h
133 ifneq (,$(wildcard $(RHC)))
134   # 7.3 typo in rhconfig.h
135   ifneq (,$(shell $(CC) $(EXTRA_CFLAGS) -E -dM $(RHC) | grep __module__bigmem))
136         EXTRA_CFLAGS += -D__module_bigmem
137   endif
138 endif
139
140 # get the kernel version - we use this to find the correct install path
141 KVER := $(shell $(CC) $(EXTRA_CFLAGS) -E -dM $(VERSION_FILE) | grep UTS_RELEASE | \
142         awk '{ print $$3 }' | sed 's/\"//g')
143
144 # assume source symlink is the same as build, otherwise adjust KOBJ
145 ifneq (,$(wildcard /lib/modules/$(KVER)/build))
146 ifneq ($(KSRC),$(shell cd /lib/modules/$(KVER)/build ; pwd -P))
147   KOBJ=/lib/modules/$(KVER)/build
148 endif
149 endif
150
151 KKVER := $(shell echo $(KVER) | \
152          awk '{ if ($$0 ~ /2\.[6]\./) print "1"; else print "0"}')
153 ifeq ($(KKVER), 0)
154   $(error *** Aborting the build. \
155           *** This driver is not supported on kernel versions older than 2.6.0)
156 endif
157
158 # look for SMP in config.h
159 SMP := $(shell $(CC) $(EXTRA_CFLAGS) -E -dM $(CONFIG_FILE) | \
160          grep -w CONFIG_SMP | awk '{ print $$3 }')
161 ifneq ($(SMP),1)
162   SMP := 0
163 endif
164
165 #ifneq ($(SMP),$(shell uname -a | grep SMP > /dev/null 2>&1 && echo 1 || echo 0))
166 #  $(warning ***)
167 #  ifeq ($(SMP),1)
168 #    $(warning *** Warning: kernel source configuration (SMP))
169 #    $(warning *** does not match running kernel (UP))
170 #  else
171 #    $(warning *** Warning: kernel source configuration (UP))
172 #    $(warning *** does not match running kernel (SMP))
173 #  endif
174 #  $(warning *** Continuing with build,)
175 #  $(warning *** resulting driver may not be what you want)
176 #  $(warning ***)
177 #endif
178
179 ifeq ($(SMP),1)
180   EXTRA_CFLAGS += -D__SMP__
181 endif
182
183 ###########################################################################
184 # Makefile for 2.6.x kernel
185 all: $(TARGET)
186 TARGET = openflow_mod.ko unit_mod.ko
187
188 $(UNIT_CFILES): 
189         $(foreach UNIT_CFILE, $(UNIT_CFILES), $(shell ln -s $(patsubst %,../t/%,$(UNIT_CFILE)) $(UNIT_CFILE)))
190
191 ifneq ($(PATCHLEVEL),)
192 EXTRA_CFLAGS += $(CFLAGS_EXTRA)
193 obj-m += openflow_mod.o unit_mod.o
194 openflow_mod-objs := $(CFILES:.c=.o)
195 unit_mod-objs := $(UNIT_CFILES:.c=.o)
196 else
197 default:
198 ifeq ($(KOBJ),$(KSRC))
199         $(MAKE) -C $(KSRC) SUBDIRS=$(shell pwd) modules
200 else
201         $(MAKE) -C $(KSRC) O=$(KOBJ) SUBDIRS=$(shell pwd) modules
202 endif
203 endif
204
205 # depmod version for rpm builds
206 DEPVER := $(shell /sbin/depmod -V 2>/dev/null | \
207           awk 'BEGIN {FS="."} NR==1 {print $$2}')
208
209 .PHONY: clean
210
211 clean:
212         rm -rf $(TARGET) $(TARGET:.ko=.o) $(TARGET:.ko=.mod.c) \
213     $(TARGET:.ko=.mod.o) $(CFILES:.c=.o) $(MANFILE).gz .*cmd \
214     .tmp_versions t/ *.o tmp/