Support custom build directories and move includes
[zfs.git] / config / kernel.m4
1 dnl #
2 dnl # Default ZFS kernel configuration 
3 dnl #
4 AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
5         ZFS_AC_KERNEL
6         ZFS_AC_SPL
7         ZFS_AC_KERNEL_CONFIG
8         ZFS_AC_KERNEL_BDEV_BLOCK_DEVICE_OPERATIONS
9         ZFS_AC_KERNEL_TYPE_FMODE_T
10         ZFS_AC_KERNEL_KOBJ_NAME_LEN
11         ZFS_AC_KERNEL_OPEN_BDEV_EXCLUSIVE
12         ZFS_AC_KERNEL_INVALIDATE_BDEV_ARGS
13         ZFS_AC_KERNEL_BDEV_LOGICAL_BLOCK_SIZE
14         ZFS_AC_KERNEL_BIO_EMPTY_BARRIER
15         ZFS_AC_KERNEL_BIO_END_IO_T_ARGS
16         ZFS_AC_KERNEL_BIO_RW_SYNCIO
17         ZFS_AC_KERNEL_BLK_END_REQUEST
18         ZFS_AC_KERNEL_BLK_FETCH_REQUEST
19         ZFS_AC_KERNEL_BLK_REQUEUE_REQUEST
20         ZFS_AC_KERNEL_BLK_RQ_BYTES
21         ZFS_AC_KERNEL_BLK_RQ_POS
22         ZFS_AC_KERNEL_BLK_RQ_SECTORS
23         ZFS_AC_KERNEL_GET_DISK_RO
24         ZFS_AC_KERNEL_RQ_IS_SYNC
25         ZFS_AC_KERNEL_RQ_FOR_EACH_SEGMENT
26
27         if test "$LINUX_OBJ" != "$LINUX"; then
28                 KERNELMAKE_PARAMS="$KERNELMAKE_PARAMS O=$LINUX_OBJ"
29         fi
30         AC_SUBST(KERNELMAKE_PARAMS)
31
32
33         dnl # -Wall -fno-strict-aliasing -Wstrict-prototypes and other
34         dnl # compiler options are added by the kernel build system.
35         KERNELCPPFLAGS="$KERNELCPPFLAGS -DHAVE_SPL -D_KERNEL"
36         KERNELCPPFLAGS="$KERNELCPPFLAGS -DTEXT_DOMAIN=\\\"zfs-linux-kernel\\\""
37
38         AC_SUBST(KERNELCPPFLAGS)
39 ])
40
41 dnl #
42 dnl # Detect name used for Module.symvers file in kernel
43 dnl #
44 AC_DEFUN([ZFS_AC_MODULE_SYMVERS], [
45         modpost=$LINUX/scripts/Makefile.modpost
46         AC_MSG_CHECKING([kernel file name for module symbols])
47         if test -f "$modpost"; then
48                 if grep -q Modules.symvers $modpost; then
49                         LINUX_SYMBOLS=Modules.symvers
50                 else
51                         LINUX_SYMBOLS=Module.symvers
52                 fi
53         else
54                 LINUX_SYMBOLS=NONE
55         fi
56         AC_MSG_RESULT($LINUX_SYMBOLS)
57         AC_SUBST(LINUX_SYMBOLS)
58 ])
59
60 dnl #
61 dnl # Detect the kernel to be built against
62 dnl #
63 AC_DEFUN([ZFS_AC_KERNEL], [
64         AC_ARG_WITH([linux],
65                 AS_HELP_STRING([--with-linux=PATH],
66                 [Path to kernel source]),
67                 [kernelsrc="$withval"])
68
69         AC_ARG_WITH(linux-obj,
70                 AS_HELP_STRING([--with-linux-obj=PATH],
71                 [Path to kernel build objects]),
72                 [kernelbuild="$withval"])
73
74         AC_MSG_CHECKING([kernel source directory])
75         if test -z "$kernelsrc"; then
76                 headersdir="/lib/modules/$(uname -r)/build"
77                 if test -e "$headersdir"; then
78                         sourcelink=$(readlink -f "$headersdir")
79                 else
80                         sourcelink=$(ls -1d /usr/src/kernels/* \
81                                      /usr/src/linux-* \
82                                      2>/dev/null | grep -v obj | tail -1)
83                 fi
84
85                 if test -n "$sourcelink" && test -e ${sourcelink}; then
86                         kernelsrc=`readlink -f ${sourcelink}`
87                 else
88                         AC_MSG_RESULT([Not found])
89                         AC_MSG_ERROR([
90         *** Please make sure the kernel devel package for your distribution
91         *** is installed then try again.  If that fails you can specify the
92         *** location of the kernel source with the '--with-linux=PATH' option.])
93                 fi
94         else
95                 if test "$kernelsrc" = "NONE"; then
96                         kernsrcver=NONE
97                 fi
98         fi
99
100         AC_MSG_RESULT([$kernelsrc])
101         AC_MSG_CHECKING([kernel build directory])
102         if test -z "$kernelbuild"; then
103                 if test -d ${kernelsrc}-obj/${target_cpu}/${target_cpu}; then
104                         kernelbuild=${kernelsrc}-obj/${target_cpu}/${target_cpu}
105                 elif test -d ${kernelsrc}-obj/${target_cpu}/default; then
106                         kernelbuild=${kernelsrc}-obj/${target_cpu}/default
107                 elif test -d `dirname ${kernelsrc}`/build-${target_cpu}; then
108                         kernelbuild=`dirname ${kernelsrc}`/build-${target_cpu}
109                 else
110                         kernelbuild=${kernelsrc}
111                 fi
112         fi
113         AC_MSG_RESULT([$kernelbuild])
114
115         AC_MSG_CHECKING([kernel source version])
116         utsrelease1=$kernelbuild/include/linux/version.h
117         utsrelease2=$kernelbuild/include/linux/utsrelease.h
118         utsrelease3=$kernelbuild/include/generated/utsrelease.h
119         if test -r $utsrelease1 && fgrep -q UTS_RELEASE $utsrelease1; then
120                 utsrelease=linux/version.h
121         elif test -r $utsrelease2 && fgrep -q UTS_RELEASE $utsrelease2; then
122                 utsrelease=linux/utsrelease.h
123         elif test -r $utsrelease3 && fgrep -q UTS_RELEASE $utsrelease3; then
124                 utsrelease=generated/utsrelease.h
125         fi
126
127         if test "$utsrelease"; then
128                 kernsrcver=`(echo "#include <$utsrelease>";
129                              echo "kernsrcver=UTS_RELEASE") |
130                              cpp -I $kernelbuild/include |
131                              grep "^kernsrcver=" | cut -d \" -f 2`
132
133                 if test -z "$kernsrcver"; then
134                         AC_MSG_RESULT([Not found])
135                         AC_MSG_ERROR([*** Cannot determine kernel version.])
136                 fi
137         else
138                 AC_MSG_RESULT([Not found])
139                 AC_MSG_ERROR([*** Cannot find UTS_RELEASE definition.])
140         fi
141
142         AC_MSG_RESULT([$kernsrcver])
143
144         LINUX=${kernelsrc}
145         LINUX_OBJ=${kernelbuild}
146         LINUX_VERSION=${kernsrcver}
147
148         AC_SUBST(LINUX)
149         AC_SUBST(LINUX_OBJ)
150         AC_SUBST(LINUX_VERSION)
151
152         ZFS_AC_MODULE_SYMVERS
153 ])
154
155 dnl #
156 dnl # Detect name used for the additional SPL Module.symvers file.  If one
157 dnl # does not exist this is likely because the SPL has been configured
158 dnl # but not built.  To allow recursive builds a good guess is made as to
159 dnl # what this file will be named based on what it is named in the kernel
160 dnl # build products.  This file will first be used at link time so if
161 dnl # the guess is wrong the build will fail then.  This unfortunately
162 dnl # means the ZFS package does not contain a reliable mechanism to
163 dnl # detect symbols exported by the SPL at configure time.
164 dnl #
165 AC_DEFUN([ZFS_AC_SPL_MODULE_SYMVERS], [
166         AC_MSG_CHECKING([spl file name for module symbols])
167         if test -r $SPL_OBJ/Module.symvers; then
168                 SPL_SYMBOLS=Module.symvers
169         elif test -r $SPL_OBJ/Modules.symvers; then
170                 SPL_SYMBOLS=Modules.symvers
171         elif test -r $SPL_OBJ/module/Module.symvers; then
172                 SPL_SYMBOLS=Module.symvers
173         elif test -r $SPL_OBJ/module/Modules.symvers; then
174                 SPL_SYMBOLS=Modules.symvers
175         else
176                 SPL_SYMBOLS=$LINUX_SYMBOLS
177         fi
178
179         AC_MSG_RESULT([$SPL_SYMBOLS])
180         AC_SUBST(SPL_SYMBOLS)
181 ])
182
183 dnl #
184 dnl # Detect the SPL module to be built against
185 dnl #
186 AC_DEFUN([ZFS_AC_SPL], [
187         AC_ARG_WITH([spl],
188                 AS_HELP_STRING([--with-spl=PATH],
189                 [Path to spl source]),
190                 [splsrc="$withval"])
191
192         AC_ARG_WITH([spl-obj],
193                 AS_HELP_STRING([--with-spl-obj=PATH],
194                 [Path to spl build objects]),
195                 [splbuild="$withval"])
196
197
198         AC_MSG_CHECKING([spl source directory])
199         if test -z "$splsrc"; then
200                 sourcelink=`ls -1d /usr/src/spl-*/${LINUX_VERSION} \
201                             2>/dev/null | tail -1`
202
203                 if test -z "$sourcelink" || test ! -e $sourcelink; then
204                         sourcelink=../spl
205                 fi
206
207                 if test -e $sourcelink; then
208                         splsrc=`readlink -f ${sourcelink}`
209                 else
210                         AC_MSG_RESULT([Not found])
211                         AC_MSG_ERROR([
212         *** Please make sure the spl devel package for your distribution
213         *** is installed then try again.  If that fails you can specify the
214         *** location of the spl source with the '--with-spl=PATH' option.])
215                 fi
216         else
217                 if test "$splsrc" = "NONE"; then
218                         splbuild=NONE
219                         splsrcver=NONE
220                 fi
221         fi
222
223         AC_MSG_RESULT([$splsrc])
224         AC_MSG_CHECKING([spl build directory])
225         if test -z "$splbuild"; then
226                 splbuild=${splsrc}
227         fi
228         AC_MSG_RESULT([$splbuild])
229
230         AC_MSG_CHECKING([spl source version])
231         if test -r $splbuild/spl_config.h &&
232                 fgrep -q SPL_META_VERSION $splbuild/spl_config.h; then
233
234                 splsrcver=`(echo "#include <spl_config.h>";
235                             echo "splsrcver=SPL_META_VERSION") |
236                             cpp -I $splbuild |
237                             grep "^splsrcver=" | cut -d \" -f 2`
238         fi
239
240         if test -z "$splsrcver"; then
241                 AC_MSG_RESULT([Not found])
242                 AC_MSG_ERROR([
243                 *** Cannot determine the version of the spl source.
244                 *** Please prepare the spl source before running this script])
245         fi
246
247         AC_MSG_RESULT([$splsrcver])
248
249         SPL=${splsrc}
250         SPL_OBJ=${splbuild}
251         SPL_VERSION=${splsrcver}
252
253         AC_SUBST(SPL)
254         AC_SUBST(SPL_OBJ)
255         AC_SUBST(SPL_VERSION)
256
257         ZFS_AC_SPL_MODULE_SYMVERS
258 ])
259
260 dnl #
261 dnl # There are certain kernel build options which when enabled are
262 dnl # completely incompatible with non GPL kernel modules.  It is best
263 dnl # to detect these at configure time and fail with a clear error
264 dnl # rather than build everything and fail during linking.
265 dnl #
266 dnl # CONFIG_DEBUG_LOCK_ALLOC - Maps mutex_lock() to mutex_lock_nested()
267 dnl #
268 AC_DEFUN([ZFS_AC_KERNEL_CONFIG], [
269
270         if test "$ZFS_META_LICENSE" = CDDL; then
271                 ZFS_LINUX_CONFIG([DEBUG_LOCK_ALLOC],
272                 AC_MSG_ERROR([
273                 *** Kernel built with CONFIG_DEBUG_LOCK_ALLOC which is
274                 *** incompatible with the CDDL license.  You must rebuild
275                 *** your kernel without this option.]), [])
276         fi
277
278         if test "$ZFS_META_LICENSE" = GPL; then
279                 AC_DEFINE([HAVE_GPL_ONLY_SYMBOLS], [1],
280                         [Define to 1 if licensed under the GPL])
281         fi
282 ])
283
284 dnl #
285 dnl # ZFS_LINUX_CONFTEST
286 dnl #
287 AC_DEFUN([ZFS_LINUX_CONFTEST], [
288 cat confdefs.h - <<_ACEOF >conftest.c
289 $1
290 _ACEOF
291 ])
292
293 dnl #
294 dnl # ZFS_LANG_PROGRAM(C)([PROLOGUE], [BODY])
295 dnl #
296 m4_define([ZFS_LANG_PROGRAM], [
297 $1
298 int
299 main (void)
300 {
301 dnl Do *not* indent the following line: there may be CPP directives.
302 dnl Don't move the `;' right after for the same reason.
303 $2
304   ;
305   return 0;
306 }
307 ])
308
309 dnl #
310 dnl # ZFS_LINUX_COMPILE_IFELSE / like AC_COMPILE_IFELSE
311 dnl #
312 AC_DEFUN([ZFS_LINUX_COMPILE_IFELSE], [
313         m4_ifvaln([$1], [ZFS_LINUX_CONFTEST([$1])])
314         rm -Rf build && mkdir -p build
315         echo "obj-m := conftest.o" >build/Makefile
316         AS_IF(
317                 [AC_TRY_COMMAND(cp conftest.c build && make [$2] -C $LINUX_OBJ EXTRA_CFLAGS="-Werror-implicit-function-declaration $EXTRA_KCFLAGS" $ARCH_UM M=$PWD/build) >/dev/null && AC_TRY_COMMAND([$3])],
318                 [$4],
319                 [_AC_MSG_LOG_CONFTEST m4_ifvaln([$5],[$5])]
320         )
321         rm -Rf build
322 ])
323
324 dnl #
325 dnl # ZFS_LINUX_TRY_COMPILE like AC_TRY_COMPILE
326 dnl #
327 AC_DEFUN([ZFS_LINUX_TRY_COMPILE],
328         [ZFS_LINUX_COMPILE_IFELSE(
329         [AC_LANG_SOURCE([ZFS_LANG_PROGRAM([[$1]], [[$2]])])],
330         [modules],
331         [test -s build/conftest.o],
332         [$3], [$4])
333 ])
334
335 dnl #
336 dnl # ZFS_LINUX_CONFIG
337 dnl #
338 AC_DEFUN([ZFS_LINUX_CONFIG],
339         [AC_MSG_CHECKING([whether Linux was built with CONFIG_$1])
340         ZFS_LINUX_TRY_COMPILE([
341                 #ifndef AUTOCONF_INCLUDED
342                 #include <linux/config.h>
343                 #endif
344         ],[
345                 #ifndef CONFIG_$1
346                 #error CONFIG_$1 not #defined
347                 #endif
348         ],[
349                 AC_MSG_RESULT([yes])
350                 $2
351         ],[
352                 AC_MSG_RESULT([no])
353                 $3
354         ])
355 ])
356
357 dnl #
358 dnl # ZFS_CHECK_SYMBOL_EXPORT
359 dnl # check symbol exported or not
360 dnl #
361 AC_DEFUN([ZFS_CHECK_SYMBOL_EXPORT],
362         [AC_MSG_CHECKING([whether symbol $1 is exported])
363         grep -q -E '[[[:space:]]]$1[[[:space:]]]' \
364                 $LINUX_OBJ/$LINUX_SYMBOLS 2>/dev/null
365         rc=$?
366         if test $rc -ne 0; then
367                 export=0
368                 for file in $2; do
369                         grep -q -E "EXPORT_SYMBOL.*($1)" "$LINUX/$file" 2>/dev/null
370                         rc=$?
371                         if test $rc -eq 0; then
372                                 export=1
373                                 break;
374                         fi
375                 done
376                 if test $export -eq 0; then
377                         AC_MSG_RESULT([no])
378                         $4
379                 else
380                         AC_MSG_RESULT([yes])
381                         $3
382                 fi
383         else
384                 AC_MSG_RESULT([yes])
385                 $3
386         fi
387 ])