61b0c2953bc492b684d676e41077eee2184fe22b
[zfs.git] / config / zfs-build.m4
1 AC_DEFUN([ZFS_AC_CONFIG], [
2
3         TOPDIR=`/bin/pwd`
4         BUILDDIR=$ZFS_META_NAME #+$zfsconfig
5         ZFSDIR=$TOPDIR/$BUILDDIR
6         LIBDIR=$ZFSDIR/lib
7         CMDDIR=$ZFSDIR/zcmd
8         UNAME=`uname -r | cut -d- -f1`
9
10         AC_SUBST(UNAME)
11         AC_SUBST(TOPDIR)
12         AC_SUBST(BUILDDIR)
13         AC_SUBST(ZFSDIR)
14         AC_SUBST(LIBDIR)
15         AC_SUBST(CMDDIR)
16         AC_SUBST(UNAME)
17
18         AC_ARG_WITH([zfs-config],
19                 AS_HELP_STRING([--with-config=CONFIG],
20                 [Config file 'kernel|user|lustre']),
21                 [zfsconfig="$withval"])
22
23         AC_MSG_CHECKING([zfs config file])
24
25         [ case "$zfsconfig" in
26                 kernel) ZFS_AC_KERNEL_CONFIG ;;
27                 user)   ZFS_AC_USER_CONFIG ;;
28                 lustre) ZFS_AC_LUSTRE_CONFIG ;;
29                 *)
30                 AC_MSG_RESULT([Error!])
31                 AC_MSG_ERROR([Bad value "$zfsconfig" for --with-config,
32                               user kernel|user|lustre]) ;;
33         esac ]
34
35         AC_MSG_RESULT([$zfsconfig]);
36 ])
37
38 AC_DEFUN([ZFS_AC_KERNEL], [
39         ver=`uname -r`
40
41         AC_ARG_WITH([linux],
42                 AS_HELP_STRING([--with-linux=PATH],
43                 [Path to kernel source]),
44                 [kernelsrc="$withval"; kernelbuild="$withval"])
45
46         AC_ARG_WITH(linux-obj,
47                 AS_HELP_STRING([--with-linux-obj=PATH],
48                 [Path to kernel build objects]),
49                 [kernelbuild="$withval"])
50
51         AC_MSG_CHECKING([kernel source directory])
52         if test -z "$kernelsrc"; then
53                 kernelbuild=
54                 sourcelink=/lib/modules/${ver}/source
55                 buildlink=/lib/modules/${ver}/build
56
57                 if test -e $sourcelink; then
58                         kernelsrc=`(cd $sourcelink; /bin/pwd)`
59                 fi
60                 if test -e $buildlink; then
61                         kernelbuild=`(cd $buildlink; /bin/pwd)`
62                 fi
63                 if test -z "$kernelsrc"; then
64                         kernelsrc=$kernelbuild
65                 fi
66                 if test -z "$kernelsrc" -o -z "$kernelbuild"; then
67                         AC_MSG_RESULT([Not found])
68                         AC_MSG_ERROR([
69                         *** Please specify the location of the kernel source
70                         *** with the '--with-linux=PATH' option])
71                 fi
72         fi
73
74         AC_MSG_RESULT([$kernelsrc])
75         AC_MSG_CHECKING([kernel build directory])
76         AC_MSG_RESULT([$kernelbuild])
77
78         AC_MSG_CHECKING([kernel source version])
79         if test -r $kernelbuild/include/linux/version.h && 
80                 fgrep -q UTS_RELEASE $kernelbuild/include/linux/version.h; then
81
82                 kernsrcver=`(echo "#include <linux/version.h>"; 
83                              echo "kernsrcver=UTS_RELEASE") | 
84                              cpp -I $kernelbuild/include |
85                              grep "^kernsrcver=" | cut -d \" -f 2`
86
87         elif test -r $kernelbuild/include/linux/utsrelease.h && 
88                 fgrep -q UTS_RELEASE $kernelbuild/include/linux/utsrelease.h; then
89
90                 kernsrcver=`(echo "#include <linux/utsrelease.h>"; 
91                              echo "kernsrcver=UTS_RELEASE") | 
92                              cpp -I $kernelbuild/include |
93                              grep "^kernsrcver=" | cut -d \" -f 2`
94         fi
95
96         if test -z "$kernsrcver"; then
97                 AC_MSG_RESULT([Not found])
98                 AC_MSG_ERROR([
99                 *** Cannot determine the version of the linux kernel source.
100                 *** Please prepare the kernel before running this script])
101         fi
102
103         AC_MSG_RESULT([$kernsrcver])
104
105         kmoduledir=${INSTALL_MOD_PATH}/lib/modules/$kernsrcver
106         LINUX=${kernelsrc}
107         LINUX_OBJ=${kernelbuild}
108
109         AC_SUBST(LINUX)
110         AC_SUBST(LINUX_OBJ)
111         AC_SUBST(kmoduledir)
112 ])
113
114 AC_DEFUN([ZFS_AC_SPL], [
115
116         AC_ARG_WITH([spl],
117                 AS_HELP_STRING([--with-spl=PATH],
118                 [Path to spl source]),
119                 [splsrc="$withval"; splbuild="$withval"])
120
121         AC_ARG_WITH([spl-obj],
122                 AS_HELP_STRING([--with-spl-obj=PATH],
123                 [Path to spl build objects]),
124                 [splbuild="$withval"])
125
126
127         AC_MSG_CHECKING([spl source directory])
128         if test -z "$splsrc"; then
129                 splbuild=
130                 sourcelink=/tmp/`whoami`/spl
131                 buildlink=/tmp/`whoami`/spl
132
133                 if test -e $sourcelink; then
134                         splsrc=`(cd $sourcelink; /bin/pwd)`
135                 fi
136                 if test -e $buildlink; then
137                         splbuild=`(cd $buildlink; /bin/pwd)`
138                 fi
139                 if test -z "$splsrc"; then
140                         splsrc=$splbuild
141                 fi
142         fi
143
144         if test -z "$splsrc" -o -z "$splbuild"; then
145                 sourcelink=/lib/modules/${ver}/source
146                 buildlink=/lib/modules/${ver}/build
147
148                 if test -e $sourcelink; then
149                         splsrc=`(cd $sourcelink; /bin/pwd)`
150                 fi
151                 if test -e $buildlink; then
152                         splbuild=`(cd $buildlink; /bin/pwd)`
153                 fi
154                 if test -z "$splsrc"; then
155                         splsrc=$splbuild
156                 fi
157                 if test -z "$splsrc" -o -z "$splbuild"; then
158                         AC_MSG_RESULT([Not found])
159                         AC_MSG_ERROR([
160                         *** Please specify the location of the spl source
161                         *** with the '--with-spl=PATH' option])
162                 fi
163         fi
164
165         AC_MSG_RESULT([$splsrc])
166         AC_MSG_CHECKING([spl build directory])
167         AC_MSG_RESULT([$splbuild])
168
169         AC_MSG_CHECKING([spl source version])
170         if test -r $splbuild/spl_config.h && 
171                 fgrep -q VERSION $splbuild/spl_config.h; then
172
173                 splsrcver=`(echo "#include <spl_config.h>"; 
174                             echo "splsrcver=VERSION") | 
175                             cpp -I $splbuild |
176                             grep "^splsrcver=" | cut -d \" -f 2`
177         fi
178
179         if test -z "$splsrcver"; then
180                 AC_MSG_RESULT([Not found])
181                 AC_MSG_ERROR([
182                 *** Cannot determine the version of the spl source. 
183                 *** Please prepare the spl source before running this script])
184         fi
185
186         AC_MSG_RESULT([$splsrcver])
187
188         AC_MSG_CHECKING([spl Module.symvers])
189         if test -r $splbuild/modules/Module.symvers; then
190                 splsymvers=$splbuild/modules/Module.symvers
191         elif test -r $kernelbuild/Module.symvers; then
192                 splsymvers=$kernelbuild/Module.symvers
193         fi
194
195         if test -z "$splsymvers"; then
196                 AC_MSG_RESULT([Not found])
197                 AC_MSG_ERROR([
198                 *** Cannot find extra Module.symvers in the spl source.
199                 *** Please prepare the spl source before running this script])
200         fi
201
202         AC_MSG_RESULT([$splsymvers])
203         AC_SUBST(splsrc)
204         AC_SUBST(splsymvers)
205 ])
206
207 AC_DEFUN([ZFS_AC_LICENSE], [
208         AC_MSG_CHECKING([license])
209         AC_MSG_RESULT([CDDL])
210 dnl #        AC_DEFINE([HAVE_GPL_ONLY_SYMBOLS], [1],
211 dnl #                [Define to 1 if module is licensed under the GPL])
212 ])
213
214 AC_DEFUN([ZFS_AC_DEBUG], [
215         AC_MSG_CHECKING([whether debugging is enabled])
216         AC_ARG_ENABLE( [debug],
217                 AS_HELP_STRING([--enable-debug],
218                 [Enable generic debug support (default off)]),
219                 [ case "$enableval" in
220                         yes) zfs_ac_debug=yes ;;
221                         no)  zfs_ac_debug=no  ;;
222                         *) AC_MSG_RESULT([Error!])
223                         AC_MSG_ERROR([Bad value "$enableval" for --enable-debug]) ;;
224                 esac ]
225         )
226         if test "$zfs_ac_debug" = yes; then
227                 AC_MSG_RESULT([yes])
228                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DDEBUG "
229                 HOSTCFLAGS="${HOSTCFLAGS} -DDEBUG "
230         else
231                 AC_MSG_RESULT([no])
232                 AC_DEFINE([NDEBUG], [1],
233                 [Define to 1 to disable debug tracing])
234                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DNDEBUG "
235                 HOSTCFLAGS="${HOSTCFLAGS} -DNDEBUG "
236         fi
237 ])
238
239 AC_DEFUN([ZFS_AC_SCRIPT_CONFIG], [
240         SCRIPT_CONFIG=.script-config
241         rm -f ${SCRIPT_CONFIG}
242         echo "KERNELSRC=${LINUX}"         >>${SCRIPT_CONFIG}
243         echo "KERNELBUILD=${LINUX_OBJ}"   >>${SCRIPT_CONFIG}
244         echo "KERNELSRCVER=$kernsrcver"   >>${SCRIPT_CONFIG}
245         echo                              >>${SCRIPT_CONFIG}
246         echo "SPLSRC=$splsrc"             >>${SCRIPT_CONFIG}
247         echo "SPLBUILD=$splbuild"         >>${SCRIPT_CONFIG}
248         echo "SPLSRCVER=$splsrcver"       >>${SCRIPT_CONFIG}
249         echo "SPLSYMVERS=$splsymvers"     >>${SCRIPT_CONFIG}
250         echo                              >>${SCRIPT_CONFIG}
251         echo "ZFSSRC=${TOPDIR}/src"       >>${SCRIPT_CONFIG}
252         echo "ZFSBUILD=${ZFSDIR}"         >>${SCRIPT_CONFIG}
253         echo                              >>${SCRIPT_CONFIG}
254         echo "TOPDIR=${TOPDIR}"           >>${SCRIPT_CONFIG}
255         echo "LIBDIR=${LIBDIR}"           >>${SCRIPT_CONFIG}
256         echo "CMDDIR=${CMDDIR}"           >>${SCRIPT_CONFIG}
257 ])
258
259 dnl #
260 dnl # ZFS_LINUX_CONFTEST
261 dnl #
262 AC_DEFUN([ZFS_LINUX_CONFTEST], [
263 cat >conftest.c <<_ACEOF
264 $1
265 _ACEOF
266 ])
267
268 dnl #
269 dnl # ZFS_LANG_PROGRAM(C)([PROLOGUE], [BODY])
270 dnl #
271 m4_define([ZFS_LANG_PROGRAM], [
272 $1
273 int
274 main (void)
275 {
276 dnl Do *not* indent the following line: there may be CPP directives.
277 dnl Don't move the `;' right after for the same reason.
278 $2
279   ;
280   return 0;
281 }
282 ])
283
284 dnl #
285 dnl # ZFS_LINUX_COMPILE_IFELSE / like AC_COMPILE_IFELSE
286 dnl #
287 AC_DEFUN([ZFS_LINUX_COMPILE_IFELSE], [
288 m4_ifvaln([$1], [ZFS_LINUX_CONFTEST([$1])])dnl
289 rm -f build/conftest.o build/conftest.mod.c build/conftest.ko build/Makefile
290 echo "obj-m := conftest.o" >build/Makefile
291 dnl AS_IF([AC_TRY_COMMAND(cp conftest.c build && make [$2] CC="$CC" -f $PWD/build/Makefile LINUXINCLUDE="-Iinclude -include include/linux/autoconf.h" -o tmp_include_depends -o scripts -o include/config/MARKER -C $LINUX_OBJ EXTRA_CFLAGS="-Werror-implicit-function-declaration $EXTRA_KCFLAGS" $ARCH_UM SUBDIRS=$PWD/build) >/dev/null && AC_TRY_COMMAND([$3])],
292 AS_IF([AC_TRY_COMMAND(cp conftest.c build && make [$2] CC="$CC" LINUXINCLUDE="-Iinclude -include include/linux/autoconf.h" -o tmp_include_depends -o scripts -o include/config/MARKER -C $LINUX_OBJ EXTRA_CFLAGS="-Werror-implicit-function-declaration $EXTRA_KCFLAGS" $ARCH_UM M=$PWD/build) >/dev/null && AC_TRY_COMMAND([$3])],
293         [$4],
294         [_AC_MSG_LOG_CONFTEST
295 m4_ifvaln([$5],[$5])dnl])dnl
296 rm -f build/conftest.o build/conftest.mod.c build/conftest.mod.o build/conftest.ko m4_ifval([$1], [build/conftest.c conftest.c])[]dnl
297 ])
298
299 dnl #
300 dnl # ZFS_LINUX_TRY_COMPILE like AC_TRY_COMPILE
301 dnl #
302 AC_DEFUN([ZFS_LINUX_TRY_COMPILE],
303         [ZFS_LINUX_COMPILE_IFELSE(
304         [AC_LANG_SOURCE([ZFS_LANG_PROGRAM([[$1]], [[$2]])])],
305         [modules],
306         [test -s build/conftest.o],
307         [$3], [$4])
308 ])
309
310 dnl #
311 dnl # ZFS_LINUX_CONFIG
312 dnl #
313 AC_DEFUN([ZFS_LINUX_CONFIG],
314         [AC_MSG_CHECKING([whether Linux was built with CONFIG_$1])
315         ZFS_LINUX_TRY_COMPILE([
316                 #ifndef AUTOCONF_INCLUDED
317                 #include <linux/config.h>
318                 #endif
319         ],[
320                 #ifndef CONFIG_$1
321                 #error CONFIG_$1 not #defined
322                 #endif
323         ],[
324                 AC_MSG_RESULT([yes])
325                 $2
326         ],[
327                 AC_MSG_RESULT([no])
328                 $3
329         ])
330 ])
331
332 dnl #
333 dnl # ZFS_CHECK_SYMBOL_EXPORT
334 dnl # check symbol exported or not
335 dnl #
336 AC_DEFUN([ZFS_CHECK_SYMBOL_EXPORT],
337         [AC_MSG_CHECKING([whether symbol $1 is exported])
338         grep -q -E '[[[:space:]]]$1[[[:space:]]]' $LINUX/Module.symvers 2>/dev/null
339         rc=$?
340         if test $rc -ne 0; then
341                 export=0
342                 for file in $2; do
343                         grep -q -E "EXPORT_SYMBOL.*($1)" "$LINUX/$file" 2>/dev/null
344                         rc=$?
345                         if test $rc -eq 0; then
346                                 export=1
347                                 break;
348                         fi
349                 done
350                 if test $export -eq 0; then
351                         AC_MSG_RESULT([no])
352                         $4
353                 else
354                         AC_MSG_RESULT([yes])
355                         $3
356                 fi
357         else
358                 AC_MSG_RESULT([yes])
359                 $3
360         fi
361 ])
362
363 dnl #
364 dnl # 2.6.x API change
365 dnl # bio_end_io_t uses 2 args (size was dropped from prototype)
366 dnl #
367 AC_DEFUN([ZFS_AC_2ARGS_BIO_END_IO_T],
368         [AC_MSG_CHECKING([whether bio_end_io_t wants 2 args])
369         tmp_flags="$EXTRA_KCFLAGS"
370         EXTRA_KCFLAGS="-Werror"
371         ZFS_LINUX_TRY_COMPILE([
372                 #include <linux/bio.h>
373         ],[
374                 void (*wanted_end_io)(struct bio *, int) = NULL;
375                 bio_end_io_t *local_end_io;
376
377                 local_end_io = wanted_end_io;
378         ],[
379                 AC_MSG_RESULT(yes)
380                 AC_DEFINE(HAVE_2ARGS_BIO_END_IO_T, 1,
381                           [bio_end_io_t wants 2 args])
382         ],[
383                 AC_MSG_RESULT(no)
384         ])
385         EXTRA_KCFLAGS="$tmp_flags"
386 ])