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