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