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