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