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