Additional buidl system cleanup. Starting to move all
[zfs.git] / config / zfs-build.m4
1 AC_DEFUN([ZFS_AC_CONFIG], [
2
3         AC_ARG_WITH([zfs-config],
4                 AS_HELP_STRING([--with-config=CONFIG],
5                 [Config file 'kernel|user|lustre']),
6                 [zfsconfig="$withval"])
7
8         AC_MSG_CHECKING([zfs config file])
9         if test -z "$zfsconfig" || test ! -r config/$zfsconfig; then
10                 AC_MSG_RESULT([no])
11                 AC_MSG_ERROR([
12                 *** Please specify one of the valid config files located
13                 *** in ./config/ with the '--with-zfs-config=CONFIG' option])
14         fi
15
16         TOPDIR=`/bin/pwd`
17         BUILDDIR=$ZFS_META_NAME #+$zfsconfig
18         ZFSDIR=$TOPDIR/$BUILDDIR
19         LIBDIR=$ZFSDIR/lib
20         CMDDIR=$ZFSDIR/zcmd
21         UNAME=`uname -r | cut -d- -f1`
22
23         AC_SUBST(UNAME)
24         AC_SUBST(TOPDIR)
25         AC_SUBST(BUILDDIR)
26         AC_SUBST(ZFSDIR)
27         AC_SUBST(LIBDIR)
28         AC_SUBST(CMDDIR)
29         AC_SUBST(UNAME)
30
31         AC_MSG_RESULT([$zfsconfig]);
32         . ./config/$zfsconfig
33
34         AC_SUBST(KERNELMAKE_PARAMS)
35         AC_SUBST(KERNELCPPFLAGS)
36         AC_SUBST(HOSTCFLAGS)
37 ])
38
39 AC_DEFUN([ZFS_AC_KERNEL], [
40         ver=`uname -r`
41
42         AC_ARG_WITH([linux],
43                 AS_HELP_STRING([--with-linux=PATH],
44                 [Path to kernel source]),
45                 [kernelsrc="$withval"; kernelbuild="$withval"])
46
47         AC_ARG_WITH(linux-obj,
48                 AS_HELP_STRING([--with-linux-obj=PATH],
49                 [Path to kernel build objects]),
50                 [kernelbuild="$withval"])
51
52         AC_MSG_CHECKING([kernel source directory])
53         if test -z "$kernelsrc"; then
54                 kernelbuild=
55                 sourcelink=/lib/modules/${ver}/source
56                 buildlink=/lib/modules/${ver}/build
57
58                 if test -e $sourcelink; then
59                         kernelsrc=`(cd $sourcelink; /bin/pwd)`
60                 fi
61                 if test -e $buildlink; then
62                         kernelbuild=`(cd $buildlink; /bin/pwd)`
63                 fi
64                 if test -z "$kernelsrc"; then
65                         kernelsrc=$kernelbuild
66                 fi
67                 if test -z "$kernelsrc" -o -z "$kernelbuild"; then
68                         AC_MSG_RESULT([Not found])
69                         AC_MSG_ERROR([
70                         *** Please specify the location of the kernel source
71                         *** with the '--with-linux=PATH' option])
72                 fi
73         fi
74
75         AC_MSG_RESULT([$kernelsrc])
76         AC_MSG_CHECKING([kernel build directory])
77         AC_MSG_RESULT([$kernelbuild])
78
79         AC_MSG_CHECKING([kernel source version])
80         if test -r $kernelbuild/include/linux/version.h && 
81                 fgrep -q UTS_RELEASE $kernelbuild/include/linux/version.h; then
82
83                 kernsrcver=`(echo "#include <linux/version.h>"; 
84                              echo "kernsrcver=UTS_RELEASE") | 
85                              cpp -I $kernelbuild/include |
86                              grep "^kernsrcver=" | cut -d \" -f 2`
87
88         elif test -r $kernelbuild/include/linux/utsrelease.h && 
89                 fgrep -q UTS_RELEASE $kernelbuild/include/linux/utsrelease.h; then
90
91                 kernsrcver=`(echo "#include <linux/utsrelease.h>"; 
92                              echo "kernsrcver=UTS_RELEASE") | 
93                              cpp -I $kernelbuild/include |
94                              grep "^kernsrcver=" | cut -d \" -f 2`
95         fi
96
97         if test -z "$kernsrcver"; then
98                 AC_MSG_RESULT([Not found])
99                 AC_MSG_ERROR([
100                 *** Cannot determine the version of the linux kernel source.
101                 *** Please prepare the kernel before running this script])
102         fi
103
104         AC_MSG_RESULT([$kernsrcver])
105
106         kmoduledir=${INSTALL_MOD_PATH}/lib/modules/$kernsrcver
107         LINUX=${kernelsrc}
108         LINUX_OBJ=${kernelbuild}
109
110         AC_SUBST(LINUX)
111         AC_SUBST(LINUX_OBJ)
112         AC_SUBST(kmoduledir)
113 ])
114
115 AC_DEFUN([ZFS_AC_SPL], [
116
117         AC_ARG_WITH([spl],
118                 AS_HELP_STRING([--with-spl=PATH],
119                 [Path to spl source]),
120                 [splsrc="$withval"; splbuild="$withval"])
121
122         AC_ARG_WITH([spl-obj],
123                 AS_HELP_STRING([--with-spl-obj=PATH],
124                 [Path to spl build objects]),
125                 [splbuild="$withval"])
126
127
128         AC_MSG_CHECKING([spl source directory])
129         if test -z "$splsrc"; then
130                 splbuild=
131                 sourcelink=/tmp/`whoami`/spl
132                 buildlink=/tmp/`whoami`/spl
133
134                 if test -e $sourcelink; then
135                         splsrc=`(cd $sourcelink; /bin/pwd)`
136                 fi
137                 if test -e $buildlink; then
138                         splbuild=`(cd $buildlink; /bin/pwd)`
139                 fi
140                 if test -z "$splsrc"; then
141                         splsrc=$splbuild
142                 fi
143         fi
144
145         if test -z "$splsrc" -o -z "$splbuild"; then
146                 sourcelink=/lib/modules/${ver}/source
147                 buildlink=/lib/modules/${ver}/build
148
149                 if test -e $sourcelink; then
150                         splsrc=`(cd $sourcelink; /bin/pwd)`
151                 fi
152                 if test -e $buildlink; then
153                         splbuild=`(cd $buildlink; /bin/pwd)`
154                 fi
155                 if test -z "$splsrc"; then
156                         splsrc=$splbuild
157                 fi
158                 if test -z "$splsrc" -o -z "$splbuild"; then
159                         AC_MSG_RESULT([Not found])
160                         AC_MSG_ERROR([
161                         *** Please specify the location of the spl source
162                         *** with the '--with-spl=PATH' option])
163                 fi
164         fi
165
166         AC_MSG_RESULT([$splsrc])
167         AC_MSG_CHECKING([spl build directory])
168         AC_MSG_RESULT([$splbuild])
169
170         AC_MSG_CHECKING([spl source version])
171         if test -r $splbuild/spl_config.h && 
172                 fgrep -q VERSION $splbuild/spl_config.h; then
173
174                 splsrcver=`(echo "#include <spl_config.h>"; 
175                             echo "splsrcver=VERSION") | 
176                             cpp -I $splbuild |
177                             grep "^splsrcver=" | cut -d \" -f 2`
178         fi
179
180         if test -z "$splsrcver"; then
181                 AC_MSG_RESULT([Not found])
182                 AC_MSG_ERROR([
183                 *** Cannot determine the version of the spl source. 
184                 *** Please prepare the spl source before running this script])
185         fi
186
187         AC_MSG_RESULT([$splsrcver])
188
189         AC_MSG_CHECKING([spl Module.symvers])
190         if test -r $splbuild/modules/Module.symvers; then
191                 splsymvers=$splbuild/modules/Module.symvers
192         elif test -r $kernelbuild/Module.symvers; then
193                 splsymvers=$kernelbuild/Module.symvers
194         fi
195
196         if test -z "$splsymvers"; then
197                 AC_MSG_RESULT([Not found])
198                 AC_MSG_ERROR([
199                 *** Cannot find extra Module.symvers in the spl source.
200                 *** Please prepare the spl source before running this script])
201         fi
202
203         AC_MSG_RESULT([$splsymvers])
204         AC_SUBST(splsrc)
205         AC_SUBST(splsymvers)
206 ])
207
208 AC_DEFUN([ZFS_AC_LICENSE], [
209         AC_MSG_CHECKING([license])
210         AC_MSG_RESULT([CDDL])
211 dnl #        AC_DEFINE([HAVE_GPL_ONLY_SYMBOLS], [1],
212 dnl #                [Define to 1 if module is licensed under the GPL])
213 ])
214
215 AC_DEFUN([ZFS_AC_DEBUG], [
216         AC_MSG_CHECKING([whether debugging is enabled])
217         AC_ARG_ENABLE( [debug],
218                 AS_HELP_STRING([--enable-debug],
219                 [Enable generic debug support (default off)]),
220                 [ case "$enableval" in
221                         yes) zfs_ac_debug=yes ;;
222                         no)  zfs_ac_debug=no  ;;
223                         *) AC_MSG_RESULT([Error!])
224                         AC_MSG_ERROR([Bad value "$enableval" for --enable-debug]) ;;
225                 esac ]
226         )
227         if test "$zfs_ac_debug" = yes; then
228                 AC_MSG_RESULT([yes])
229                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DDEBUG "
230                 HOSTCFLAGS="${HOSTCFLAGS} -DDEBUG "
231         else
232                 AC_MSG_RESULT([no])
233                 AC_DEFINE([NDEBUG], [1],
234                 [Define to 1 to disable debug tracing])
235                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DNDEBUG "
236                 HOSTCFLAGS="${HOSTCFLAGS} -DNDEBUG "
237         fi
238 ])
239
240 AC_DEFUN([ZFS_AC_SCRIPT_CONFIG], [
241         SCRIPT_CONFIG=.script-config
242         rm -f ${SCRIPT_CONFIG}
243         echo "KERNELSRC=${LINUX}"         >>${SCRIPT_CONFIG}
244         echo "KERNELBUILD=${LINUX_OBJ}"   >>${SCRIPT_CONFIG}
245         echo "KERNELSRCVER=$kernsrcver"   >>${SCRIPT_CONFIG}
246         echo                              >>${SCRIPT_CONFIG}
247         echo "SPLSRC=$splsrc"             >>${SCRIPT_CONFIG}
248         echo "SPLBUILD=$splbuild"         >>${SCRIPT_CONFIG}
249         echo "SPLSRCVER=$splsrcver"       >>${SCRIPT_CONFIG}
250         echo "SPLSYMVERS=$splsymvers"     >>${SCRIPT_CONFIG}
251         echo                              >>${SCRIPT_CONFIG}
252         echo "ZFSSRC=${TOPDIR}/src"       >>${SCRIPT_CONFIG}
253         echo "ZFSBUILD=${ZFSDIR}"         >>${SCRIPT_CONFIG}
254         echo                              >>${SCRIPT_CONFIG}
255         echo "TOPDIR=${TOPDIR}"           >>${SCRIPT_CONFIG}
256         echo "LIBDIR=${LIBDIR}"           >>${SCRIPT_CONFIG}
257         echo "CMDDIR=${CMDDIR}"           >>${SCRIPT_CONFIG}
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 ])