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