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