For separate kernel source and build directories, allow the kernel build
[comedi.git] / m4 / as-linux.m4
1 dnl as-linux.m4 0.2.0
2
3 dnl autostars m4 macro for detecting a Linux source tree (or
4 dnl equivalent) for compiling modules.
5
6 dnl David Schleef <ds@schleef.org>
7 dnl Frank Mori Hess <fmhess@users.sourceforge.net>
8 dnl Thomas Vander Stichele <thomas@apestaart.org>
9
10 dnl $Id$
11
12 dnl AS_LINUX()
13 dnl
14 dnl this macro adds the options
15 dnl --with-linuxdir        to specify a kernel build tree location
16 dnl --with-linuxsrcdir     to specify a kernel source tree location
17 dnl --with-linuxconfig     to specify a kernel .config file
18 dnl --with-kernel-release  to specify an alternative uname -r
19 dnl --with-machine         to specify an alternative uname -m
20 dnl --with-rpm-target      to specify to match the given rpm --target option
21 dnl --with-extraversion    to specify an EXTRAVERSION override
22 dnl --with-modulesdir      to specify the base install location of modules
23 dnl --with-modulesdeveldir to specify the base install location of build stuff
24
25 dnl this macro defines:
26 dnl LINUX_DIR
27 dnl   The directory where the Linux build tree resides.
28 dnl LINUX_SRC_DIR
29 dnl   The directory where the Linux source tree resides.
30 dnl CONFIG_FILE
31 dnl   The Linux config file
32 dnl LINUX_ARCH
33 dnl   $(ARCH) in kernel Makefiles
34 dnl LINUX_AFLAGS
35 dnl   $(AFLAGS) in kernel Makefiles
36 dnl LINUX_LDFLAGS
37 dnl   Linker flags used by Linux
38 dnl LINUX_ARFLAGS
39 dnl   Archiver flags used by Linux
40 dnl LINUX_CROSS_COMPILE
41 dnl   Cross-compiler prefix, if any. (example: "powerpc-linux-")
42 dnl LINUX_KERNELRELEASE
43 dnl   Kernel release name (2.4.5-pre4-ac5-rmk), $(KERNELRELEASE) in
44 dnl   Linux Makefiles.
45 dnl LINUX_CFLAGS
46 dnl   CFLAGS used by Linux.  Includes both $CFLAGS and $MODFLAGS from
47 dnl   kernel Makefiles. Also includes warnings and optimization.
48 dnl LINUX_CC
49 dnl   Compiler used by Linux.
50 dnl LINUX_LD
51 dnl   Path to linker (possibly with options) used by Linux.
52 dnl LINUX_AS
53 dnl   Assembler used by Linux.
54 dnl LINUX_MODULE_STYLE
55 dnl   Style of module building (2.4.0, 2.6.0, 2.6.6)
56 dnl LINUX_MODULE_EXT
57 dnl   Module extension (.o or .ko)
58 dnl LINUX_MODPOST
59 dnl   path to modpost script
60 dnl MODULESDIR
61 dnl   base install path for kernel modules
62 dnl MODULESDEVELDIR
63 dnl   base install path for kernel module development files
64 dnl
65 dnl End of search list.
66
67 dnl main entry point
68 dnl checks the version, and figures out all flags to use to make modules.
69 AC_DEFUN([AS_LINUX],
70 [
71         dnl check if user supplied a uname -r, and if not use the running one
72         AS_LINUX_KERNEL_RELEASE()
73         dnl check if user supplied a uname -m, and if not use the running one
74         AS_LINUX_MACHINE()
75         dnl check if the user supplied an rpm target arch
76         dnl override the LINUX_MACHINE value if he did
77         AS_LINUX_RPM_TARGET()
78
79         dnl find the kernel build tree for the given uname -r
80         AS_LINUX_DIR()
81         dnl override kernel release uname -r value with info from build tree
82         AS_LINUX_OVERRIDE_KERNEL_RELEASE($LINUX_DIR)
83         dnl find the kernel source tree from the build tree or --with-linuxsrcdir
84         AS_LINUX_SRC_DIR($LINUX_DIR)
85         dnl check if user supplied an EXTRAVERSION, and if not get from uname -r
86         AS_LINUX_EXTRAVERSION($LINUX_KERNEL_RELEASE)
87         dnl check if user supplied a config file; if not, guess a good one
88         AS_LINUX_CONFIG($LINUX_DIR, $LINUX_KERNEL_RELEASE, $LINUX_MACHINE)
89         dnl check if we're building on pre-FC2 Red Hat/Fedora,
90         dnl and add some flags if we are
91         AS_CHECK_REDHAT_PRE_FC2()
92         dnl check for where to install modules
93         AS_LINUX_MODULESDIR($LINUX_KERNEL_RELEASE)
94         dnl check for where to install module development files
95         AS_LINUX_MODULESDEVELDIR($LINUX_DIR)
96         dnl check for the MAJOR/MINOR version of Linux
97         AS_LINUX_VERSION_MAJOR_MINOR($LINUX_DIR)
98
99         dnl now call the correct macro to get compiler flags
100         dnl the versioned AS_LINUX macros just use the global variables
101         dnl this could be cleaned up later on if we feel like it
102         case $LINUX_VERSION_MAJOR.$LINUX_VERSION_MINOR in
103                 2.6)
104                         AS_LINUX_2_6()
105                         ;;
106                 2.[[01234]])
107                         AS_LINUX_2_4()
108                         ;;
109                 *)
110                         AC_MSG_ERROR([Unknown Linux major.minor $LINUX_VERSION_MAJOR.$LINUX_VERSION_MINOR])
111                         ;;
112         esac
113 ])
114
115
116 dnl check if we can find a build dir for the Linux kernel
117 dnl defines LINUX_DIR to the absolute location of a usable kernel build tree
118 AC_DEFUN([AS_LINUX_DIR],
119 [
120         AC_ARG_WITH([linuxdir],
121                 [AC_HELP_STRING([--with-linuxdir=DIR],
122                         [specify path to Linux build directory])],
123                 [LINUX_DIR="${withval}"],
124                 [LINUX_DIR=default])
125
126         if test "${LINUX_DIR}" != "default" ; then
127                 AS_TRY_LINUX_DIR([${LINUX_DIR}], , AC_MSG_ERROR([Linux build dir not found]) )
128         fi
129
130         if test "${LINUX_DIR}" = "default" ; then
131                 dir="/lib/modules/$LINUX_KERNEL_RELEASE/build";
132                 AS_TRY_LINUX_DIR([${dir}], [LINUX_DIR=${dir}], )
133         fi
134         if test "${LINUX_DIR}" = "default" ; then
135                 dir="../linux";
136                 AS_TRY_LINUX_DIR([${dir}], [LINUX_DIR=${dir}], )
137         fi
138         if test "${LINUX_DIR}" = "default" ; then
139                 dir="/usr/src/linux";
140                 AS_TRY_LINUX_DIR([${dir}], [LINUX_DIR=${dir}], )
141         fi
142
143         if test "${LINUX_DIR}" = "default" ; then
144                 AC_MSG_ERROR([Linux build directory not found])
145         fi
146
147         AC_SUBST(LINUX_DIR)
148 ])
149
150 dnl check if the given candidate path for a linux build tree is usable
151 AC_DEFUN([AS_TRY_LINUX_DIR],
152         [AC_MSG_CHECKING(for Linux build in $1)
153
154         if test -f "$1/Makefile" ; then
155                 result=yes
156                 $2
157         else
158                 result="not found"
159                 $3
160         fi
161
162         AC_MSG_RESULT($result)
163 ])
164
165 dnl get the kernel source directory
166 dnl $1 is the kernel build directory
167 dnl defines LINUX_SRC_DIR to the absolute location of kernel source tree
168 AC_DEFUN([AS_LINUX_SRC_DIR],
169 [
170         AC_ARG_WITH([linuxsrcdir],
171                 [AC_HELP_STRING([--with-linuxsrcdir=DIR],
172                         [specify path to Linux source directory])],
173                 [LINUX_SRC_DIR="${withval}"],
174                 [LINUX_SRC_DIR=default])
175
176         if test "${LINUX_SRC_DIR}" != "default" ; then
177                 AS_TRY_LINUX_SRC_DIR([${LINUX_SRC_DIR}], , AC_MSG_ERROR([Linux source dir not found]) )
178         fi
179
180         if test "${LINUX_SRC_DIR}" = "default" ; then
181                 AC_MSG_CHECKING(for separate Linux source and build directory)
182                 dir=`sed -n -e 's/^KERNELSRC *:= *\(.*\)/\1/p' "$1/Makefile"`
183                 if test -z "$dir"; then
184                         AC_MSG_RESULT([no])
185                         LINUX_SRC_DIR="$1"
186                 else
187                         AC_MSG_RESULT([yes])
188                         case "$dir" in
189                         .*) dir="$1/$dir" ;;
190                         esac
191                         AS_TRY_LINUX_SRC_DIR([${dir}], [LINUX_SRC_DIR=${dir}], )
192                 fi
193         fi
194
195         if test "${LINUX_SRC_DIR}" = "default" ; then
196                 AC_MSG_ERROR([Linux source directory not found])
197         fi
198
199         AC_SUBST(LINUX_SRC_DIR)
200 ])
201
202 dnl check if the given candidate path for a linux source tree is usable
203 AC_DEFUN([AS_TRY_LINUX_SRC_DIR],
204         [AC_MSG_CHECKING(for Linux source in $1)
205
206         if test -f "$1/Makefile" ; then
207                 result=yes
208                 $2
209         else
210                 result="not found"
211                 $3
212         fi
213
214         AC_MSG_RESULT($result)
215 ])
216
217 dnl allow for specifying a kernel release (uname -r) to build for
218 dnl use uname -r of running one if not specified
219 dnl store result in LINUX_KERNEL_RELEASE
220 AC_DEFUN([AS_LINUX_KERNEL_RELEASE],
221 [
222         AC_ARG_WITH([kernel-release],
223                 [AC_HELP_STRING([--with-kernel-release=RELEASE],
224                         [specify the "uname -r"-value to build for])],
225                 [LINUX_KERNEL_RELEASE="${withval}"],
226                 [LINUX_KERNEL_RELEASE=`uname -r`])
227         if test "x$LINUX_KERNEL_RELEASE" = "xyes";
228         then
229                 LINUX_KERNEL_RELEASE=`uname -r`
230         fi
231         AC_MSG_NOTICE([Using $LINUX_KERNEL_RELEASE as the uname -r value])
232 ])
233
234 dnl replaces LINUX_KERNEL_RELEASE once the Linux build directory is known
235 dnl first argument is the Linux build directory
236 AC_DEFUN([AS_LINUX_OVERRIDE_KERNEL_RELEASE],
237 [
238         INCDIR="$1/include"
239         UTSINC="${INCDIR}/linux/utsrelease.h"
240         if ! test -f "${UTSINC}"; then
241                 UTSINC="${INCDIR}/linux/version.h"
242         fi
243         if test -f "${UTSINC}"; then
244                 RELEASE=`echo UTS_RELEASE | cat "${UTSINC}" - |
245                         /lib/cpp -I "${INCDIR}" | tail -n 1 |
246                         sed 's/^"\(.*\)"$/\1/'`
247                 if test "${RELEASE}" != "UTS_RELEASE" -a "${RELEASE}" != "" \
248                         -a "${RELEASE}" != "${LINUX_KERNEL_RELEASE}"; then
249                         AC_MSG_NOTICE([Overriding uname -r value with ${RELEASE}])
250                         LINUX_KERNEL_RELEASE="${RELEASE}"
251                 fi
252
253         fi
254 ])
255
256 dnl allow for specifying a machine (uname -m) to build for
257 dnl use uname -m, of running one if not specified
258 dnl store result in LINUX_MACHINE
259 AC_DEFUN([AS_LINUX_MACHINE],
260 [
261         AC_ARG_WITH([machine],
262                 [AC_HELP_STRING([--with-machine=MACHINE],
263                         [specify the "uname -m"-value to build for])],
264                 [LINUX_MACHINE="${withval}"],
265                 [LINUX_MACHINE=`uname -m`])
266         if test "x$LINUX_MACHINE" = "xyes";
267         then
268                 LINUX_MACHINE=`uname -r`
269         fi
270         AC_MSG_NOTICE([Using $LINUX_MACHINE as the uname -m value])
271 ])
272 dnl allow for specifying an rpm target arch
273 dnl if none specified, try to guess one from running rpm querying for
274 dnl the kernel with the uname -r
275 dnl this is so configure without arguments works out of the box
276 dnl FIXME: investigate if uname -p is a correct guess for this, and if
277 dnl we should have a flag for specifying it instead
278
279 dnl this macro possibly overrides LINUX_MACHINE
280
281 dnl first argument is the kernel release building for
282 AC_DEFUN([AS_LINUX_RPM_TARGET],
283 [
284         RELEASE=$LINUX_KERNEL_RELEASE
285         AC_ARG_WITH([rpm-target],
286                 [AC_HELP_STRING([--with-rpm-target=TARGET],
287                         [specify the target arch to build for])],
288                 [LINUX_RPM_TARGET="${withval}"],
289                 [LINUX_RPM_TARGET=])
290         if test "x$LINUX_RPM_TARGET" = "x"
291         then
292                 dnl if we have rpm, try to guess the target of the kernel
293                 dnl we want to build for using rpm
294                 AC_PATH_PROG([RPM], [rpm], [no])
295                 if test "x$RPM" != "xno" ; then
296                         if $RPM -q kernel-$RELEASE > /dev/null
297                         then
298                           LINUX_RPM_TARGET=`$RPM -q --queryformat %{arch} kernel-$RELEASE`
299                         else
300                           AC_MSG_NOTICE([Cannot guess target arch, consider setting it using --with-rpm-target])
301                         fi
302                 fi
303         fi
304
305         dnl now override LINUX_MACHINE if LINUX_RPM_TARGET is set
306         if test "x$LINUX_RPM_TARGET" != "x"
307         then
308                 dnl override LINUX_MACHINE based on this
309                 dnl FIXME: add other possible Red Hat/Fedora/rpm targets here
310                 LINUX_MACHINE=
311                 case "$LINUX_RPM_TARGET" in
312                         i?86) LINUX_MACHINE=i386;;
313                         athlon) LINUX_MACHINE=i386;;
314                         x86_64) LINUX_MACHINE=x86_64;;
315                 esac
316                 if test "x$LINUX_MACHINE" = "x"
317                 then
318                 AC_MSG_ERROR(Could not guess uname -m value from target $LINUX_RPM_TARGET)
319                 fi
320         fi
321 ])
322
323
324 dnl allow for specifying an override for EXTRAVERSION
325 dnl if none specified, make it from the passed-in KERNEL_RELEASE (uname -r)
326 dnl resulting value is stored in LINUX_EXTRAVERSION
327 dnl first argument is the uname -r string to use as a fallback
328 AC_DEFUN([AS_LINUX_EXTRAVERSION],
329 [
330         KERNEL_RELEASE=[$1]
331         dnl extract the default by getting everything after first -
332         LINUX_EXTRAVERSION="-`echo $KERNEL_RELEASE | cut -d- -f 2-`"
333         AC_ARG_WITH([extraversion],
334                 [AC_HELP_STRING([--with-extraversion=FILE],
335                         [specify override for kernel EXTRAVERSION])],
336                 [LINUX_EXTRAVERSION="${withval}"],)
337 ])
338
339
340 dnl check if we can find a config file for the Linux kernel
341 dnl defines LINUX_CONFIG to the absolute location of a usable
342 dnl kernel source config file
343
344 dnl for rpm distros, it can try and guess the correct config file by
345 dnl checking the global LINUX_RPM_TARGET variable set somewhere else
346 dnl (FIXME: move this to an argument instead ?)
347
348 dnl first argument is LINUX_DIR to check for possible configs
349 dnl second argument is kernel-release (uname -r)
350 dnl third argument is machine (uname -m)
351
352 AC_DEFUN([AS_LINUX_CONFIG],
353 [
354         LINUX_DIR=[$1]
355         KERNEL_RELEASE=[$2]
356         MACHINE=[$3]
357         AC_ARG_WITH([linuxconfig],
358                 [AC_HELP_STRING([--with-linuxconfig=FILE],
359                         [specify path to Linux configuration file])],
360                 [LINUX_CONFIG="${withval}"],
361                 [LINUX_CONFIG=default])
362
363         dnl if a file got specified, try it as a linux config file
364         if test "${LINUX_CONFIG}" != "default" ; then
365                 AS_TRY_LINUX_CONFIG($LINUX_CONFIG, $MACHINE,
366                                     ,, AC_MSG_ERROR([Linux config not found]) )
367         fi
368
369         dnl if no file specified, first check for the regular .config file
370         dnl in LINUX_DIR created by manual configuration
371         if test "${LINUX_CONFIG}" = "default" ; then
372                 file="$LINUX_DIR/.config";
373                 AS_TRY_LINUX_CONFIG($file, $MACHINE,
374                                     [LINUX_CONFIG=${file}], )
375         fi
376         dnl second, try to guess what config file to use for the current kernel
377         dnl FIXME: the possible arch is from rpmbuild --target, and is
378         dnl different from the value of ARCH (Makefile) or MACHINE (uname -m)
379         dnl so we should have a redhat flag to specify the target to find
380         dnl the correct config file
381         if test "${LINUX_CONFIG}" = "default" && test "x$LINUX_RPM_TARGET" != "x"; then
382                 dnl Red Hat stores configuration files for their built kernels
383                 dnl named kernel-(version)-(arch)(extra).config
384                 dnl where arch is athlon, i386, i586, i686, x86_64
385                 dnl and (extra) is empty or -smp, -BOOT, -bigmem
386                 dnl haven't seen combinations of extra yet as of FC1
387                 KVERSION=`echo $KERNEL_RELEASE | cut -d- -f1`
388                 extra=
389                 echo $KERNEL_RELEASE | grep smp && EXTRA="-smp"
390                 echo $KERNEL_RELEASE | grep bigmem && EXTRA="-bigmem"
391                 echo $KERNEL_RELEASE | grep BOOT && EXTRA="-BOOT"
392                 file="$LINUX_DIR/configs/kernel-$KVERSION-$LINUX_RPM_TARGET$EXTRA.config"
393                 AS_TRY_LINUX_CONFIG($file, $MACHINE,
394                                     [LINUX_CONFIG=${file}], )
395         fi
396         if test "${LINUX_CONFIG}" = "default" ; then
397                 AC_MSG_ERROR([
398 The kernel source tree at ${LINUX_DIR} is not configured,
399 and no configuration files in config/ matching your kernel were found.
400 Fix before continuing or specify a config file using --with-configfile.])
401         fi
402
403         AC_SUBST(LINUX_CONFIG)
404 ])
405
406 dnl check if the given candidate config file is usable
407 dnl FIXME: it would be nice if it could check if it matches the
408 dnl given machine (uname -m)
409 AC_DEFUN([AS_TRY_LINUX_CONFIG],
410 [
411         CFG=[$1]
412         MACHINE=[$2]
413         AC_MSG_CHECKING($CFG)
414
415         if test -f "$CFG" ; then
416                 result=yes
417                 ifelse([$3], , :, [$3])
418         else
419                 result="not found"
420                 ifelse([$4], , :, [$4])
421         fi
422         AC_MSG_RESULT($result)
423 ])
424
425 dnl check if RED_HAT_LINUX_KERNEL is defined
426 dnl pre-FC2 RH/Fedora defines this in linux/rhconfig.h
427 dnl included from linux/version.h
428 dnl if this is present, a few extra defines need to be present to make sure
429 dnl symbol versioning is correct
430 dnl uses LINUX_DIR to find rhconfig.h
431 AC_DEFUN([AS_CHECK_REDHAT_PRE_FC2],
432 [
433         AC_MSG_CHECKING(Pre-FC2 Red Hat/Fedora kernel)
434         HAVE_REDHAT_KERNEL=false
435         ac_save_CFLAGS="$CFLAGS"
436         CFLAGS="$CFLAGS -I${LINUX_DIR}/include/linux"
437         AC_COMPILE_IFELSE(AC_LANG_PROGRAM([
438 #include "rhconfig.h"
439 int code = RED_HAT_LINUX_KERNEL;
440         ]),
441         AC_MSG_RESULT(found); HAVE_REDHAT_KERNEL=true,
442         AC_MSG_RESULT(not found))
443         dnl restore CFLAGS
444         CFLAGS="$ac_save_CFLAGS"
445
446         dnl check for Red Hat define flags to use - see /sbin/mkkerneldoth
447
448         dnl initialize the booleans we want to detect
449         ENTERPRISE='0'
450         SMP='0'
451         UP='0'
452         BIGMEM='0'
453         HUGEMEM='0'
454
455         dnl get variables from the currently running kernel as default
456         KERNEL_TYPE=`uname -r | sed 's_^.*\(smp\|enterprise\|bigmem\|hugemem\)$_\1_;t;s_.*__;'`
457         KERNEL_RELEASE=`uname -r | sed 's|smp\|enterprise\|bigmem\|hugemem||g'`
458         KERNEL_ARCH=`uname -m`
459
460         dnl check the config file and override KERNEL_ARCH
461         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_M386, KERNEL_ARCH=i386)
462         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_M586, KERNEL_ARCH=i586)
463         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_M686, KERNEL_ARCH=i686)
464         dnl for some reason the i686 bigmem config file has PENTIUM
465         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_MPENTIUMIII, KERNEL_ARCH=i686)
466         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_MK7, KERNEL_ARCH=athlon)
467
468         dnl check the config file and override KERNEL_TYPE
469         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_SMP, KERNEL_TYPE=smp)
470         dnl bigmem is also smp, so this check is done after smp to override
471         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_HIGHMEM64G, KERNEL_TYPE=bigmem)
472
473         dnl FIXME: need to check hugemem and enterprise config files, which
474         dnl aren't provided in Fedora Core 1 !
475
476         case "$KERNEL_TYPE" in
477                 smp) SMP='1';;
478                 enterprise) ENTERPRISE='1';;
479                 bigmem) BIGMEM='1';;
480                 hugemem) HUGEMEM='1';;
481                 *) UP='1';;
482         esac
483         REDHAT_CFLAGS="-D__MODULE_KERNEL_$KERNEL_ARCH=1"
484         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_ENTERPRISE=$ENTERPRISE"
485         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_UP=$UP"
486         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_SMP=$SMP"
487         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_BIGMEM=$BIGMEM"
488         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_HUGEMEM=$HUGEMEM"
489
490         LINUX_REDHAT_CFLAGS="$REDHAT_CFLAGS"
491
492         AC_SUBST(LINUX_KERNEL_TYPE, "$KERNEL_TYPE")
493 ])
494
495 dnl add an argument to specify/override the module install path
496 dnl if nothing specified, it will be /lib/modules/(kernelrel)
497 AC_DEFUN([AS_LINUX_MODULESDIR],
498 [
499         KERNEL_RELEASE=[$1]
500         AC_ARG_WITH([modulesdir],
501                 [AC_HELP_STRING([--with-modulesdir=DIR],
502                         [specify path to kernel-specific modules install directory])],
503                 [MODULESDIR="${withval}"],
504                 [MODULESDIR=default])
505
506         if test "${MODULESDIR}" = "default" ; then
507                 MODULESDIR="/lib/modules/${KERNEL_RELEASE}"
508         fi
509         dnl make it available to Makefiles so it can be used in ...dir
510         AC_SUBST(modulesdir, $MODULESDIR)
511         AC_MSG_NOTICE([Putting kernel modules under ${MODULESDIR}])
512 ])
513
514 dnl add an argument to specify/override the module devel install path
515 dnl if nothing specified, it will be the passed in LINUXDIR)
516 AC_DEFUN([AS_LINUX_MODULESDEVELDIR],
517 [
518         LINUXDIR=[$1]
519         AC_ARG_WITH([modulesdeveldir],
520                 [AC_HELP_STRING([--with-modulesdeveldir=DIR],
521                         [specify path to kernel-specific module development install directory])],
522                 [MODULESDEVELDIR="${withval}"],
523                 [MODULESDEVELDIR=default])
524
525         if test "${MODULESDEVELDIR}" = "default" ; then
526                 MODULESDEVELDIR="${LINUXDIR}"
527         fi
528         dnl make it available to Makefiles so it can be used in ...dir
529         AC_SUBST(modulesdeveldir, $MODULESDEVELDIR)
530         AC_MSG_NOTICE([Putting kernel module development files under ${MODULESDEVELDIR}])
531 ])
532
533 AC_DEFUN([AS_LINUX_2_6],
534 [
535         AC_MSG_CHECKING(for Linux CFLAGS)
536
537         {
538           tmpdir=`(umask 077 && mktemp -d -q "./confstatXXXXXX") 2>/dev/null` &&
539           test -n "$tmpdir" && test -d "$tmpdir"
540         } ||
541         {
542           tmpdir=./confstat$$-$RANDOM
543           (umask 077 && mkdir $tmpdir)
544         } ||
545         {
546           echo "$me: cannot create a temporary directory in ." >&2
547           { (exit 1); exit 1; }
548         }
549
550         tmpdir=`pwd`/$tmpdir
551
552         cat >${tmpdir}/Makefile <<EOF
553 obj-m += fake.o
554
555 \$(obj)/fake.c: flags
556         touch \$(obj)/fake.c
557
558 .PHONY: flags
559 flags:
560         echo LINUX_ARCH=\"\$(ARCH)\" >>\$(obj)/flags
561         echo LINUX_AFLAGS=\"\$(AFLAGS)\" | sed 's,include,"\$(LINUXDIR)/include",g'>>\$(obj)/flags
562         echo LINUX_LDFLAGS=\"\" >>\$(obj)/flags
563         echo LINUX_ARFLAGS=\"\$(ARFLAGS)\" >>\$(obj)/flags
564         echo LINUX_CROSS_COMPILE=\"\$(CROSS_COMPILE)\" >>\$(obj)/flags
565         echo LINUX_KERNELRELEASE=\"\$(KERNELRELEASE)\" >>\$(obj)/flags
566         echo LINUX_CFLAGS=\"\$(CFLAGS) \$(CPPFLAGS)\" \
567                 | sed -e 's,-Iinclude,-I\$(LINUXDIR)/include,g' -e 's,-include include,-include \$(LINUXDIR)/include,g' >>\$(obj)/flags
568         echo LINUX_CFLAGS_MODULE=\"\$(CFLAGS_MODULE)\" >>\$(obj)/flags
569         echo LINUX_CC=\"\$(CC)\" >>\$(obj)/flags
570         echo LINUX_LD=\"\$(LD) \$(LDFLAGS) \$(LDFLAGS_MODULE)\" >>\$(obj)/flags
571         echo LINUX_AS=\"\$(AS)\" >>\$(obj)/flags
572         echo LINUX_MODLIB=\"\$(MODLIB)\" >>\$(obj)/flags
573 EOF
574
575         echo ${MAKE-make} -C ${LINUX_DIR} V=1 SUBDIRS=${tmpdir} LINUXDIR=${LINUX_DIR} modules >&5 2>&5
576         ${MAKE-make} -C ${LINUX_DIR} V=1 SUBDIRS=${tmpdir} LINUXDIR=${LINUX_DIR} modules >&5 2>&5
577         . ${tmpdir}/flags
578         rm -rf ${tmpdir}
579
580         LINUX_MODULE_EXT=".ko"
581         LINUX_MODULE_STYLE="2.6.6"
582
583         LINUX_CFLAGS="$LINUX_CFLAGS $LINUX_CFLAGS_MODULE"
584
585         AC_SUBST(LINUX_ARCH)
586         AC_SUBST(LINUX_AFLAGS)
587         AC_SUBST(LINUX_LDFLAGS)
588         AC_SUBST(LINUX_ARFLAGS)
589         AC_SUBST(LINUX_CROSS_COMPILE)
590         AC_SUBST(LINUX_KERNELRELEASE)
591         AC_SUBST(LINUX_CFLAGS)
592         AC_SUBST(LINUX_CC)
593         AC_SUBST(LINUX_LD)
594         AC_SUBST(LINUX_AS)
595         AC_SUBST(LINUX_MODULE_EXT)
596         AC_SUBST(LINUX_MODULE_STYLE)
597         AC_SUBST(LINUX_MODPOST)
598         AC_SUBST(LINUX_MODLIB)
599
600         AC_MSG_RESULT([$LINUX_CFLAGS])
601
602         AC_PATH_PROG([LINUX_MODPOST], ["modpost"], ["no"], ["$LINUX_DIR/scripts:$LINUX_DIR/scripts/mod"])
603 ])
604
605
606 AC_DEFUN([AS_LINUX_2_4],
607 [
608         AC_MSG_CHECKING(for Linux 2.4 make flags)
609         dnl we try to figure out the CFLAGS by invoking the Makefile on
610         dnl a test dir
611         dnl we use the correct config file by substituting the MAKEFILES
612         dnl Makefile variable, which originally points to .config
613
614         if [[ ! -e "${LINUX_DIR}/.hdepend" ]];then
615                 AC_MSG_WARN([
616 No .hdepend file found, you may need to run 'make dep' on the kernel source before continuing.])
617         fi
618
619         {
620           tmpdir=`(umask 077 && mktemp -d -q "./confstatXXXXXX") 2>/dev/null` &&
621           test -n "$tmpdir" && test -d "$tmpdir"
622         } ||
623         {
624           tmpdir=./confstat$$-$RANDOM
625           (umask 077 && mkdir $tmpdir)
626         } ||
627         {
628           echo "$me: cannot create a temporary directory in ." >&2
629           { (exit 1); exit 1; }
630         }
631         tmpdir=`pwd`/$tmpdir
632
633         #sed "s|\.config|${CONFIG_FILE}|g" ${LINUX_DIR}/Makefile >$tmpdir/Makefile
634         cat >$tmpdir/Makefile <<EOF
635 modules:
636         @echo LINUX_ARCH=\"\$(ARCH)\"
637         @echo LINUX_AFLAGS=\"\$(AFLAGS)\" | sed 's_Iinclude_I\"\$(LINUXDIR)/include\"_g'
638         @echo LINUX_LDFLAGS=\"\"
639         @echo LINUX_ARFLAGS=\"\$(ARFLAGS)\"
640         @echo LINUX_CROSS_COMPILE=\"\$(CROSS_COMPILE)\"
641         @echo LINUX_KERNELRELEASE=\"\$(KERNELRELEASE)\"
642         @echo LINUX_CFLAGS=\"\$(CFLAGS)\" | sed 's_Iinclude_I\"\$(LINUXDIR)/include\"_g'
643         @echo LINUX_MODFLAGS=\"\$(MODFLAGS)\"
644         @echo LINUX_CC=\"\$(CC)\"
645         @echo LINUX_LD=\"\$(LD) \$(LDFLAGS)\"
646         @echo LINUX_AS=\"\$(AS)\"
647 EOF
648         make -C ${LINUX_DIR} SUBDIRS=${tmpdir} modules | grep ^LINUX_ >${tmpdir}/ack
649         if (($?)); then
650                 echo make modules with LINUX_DIR=$(LINUX_DIR) failed.
651                 exit 1
652         fi
653         . ${tmpdir}/ack
654         rm -rf ${tmpdir}
655
656         LINUX_MODULE_EXT=".o"
657         LINUX_MODULE_STYLE="2.4.0"
658
659         LINUX_CFLAGS="$LINUX_CFLAGS $LINUX_MODFLAGS"
660
661         dnl if we have REDHAT_CFLAGS, put them in LINUX_CFLAGS
662         if test "x$REDHAT_CFLAGS" != "x";
663         then
664                 LINUX_CFLAGS="$LINUX_CFLAGS $REDHAT_CFLAGS"
665         fi
666         AC_SUBST(LINUX_ARCH)
667         AC_SUBST(LINUX_AFLAGS)
668         AC_SUBST(LINUX_LDFLAGS)
669         AC_SUBST(LINUX_ARFLAGS)
670         AC_SUBST(LINUX_CROSS_COMPILE)
671         AC_SUBST(LINUX_KERNELRELEASE)
672         AC_SUBST(LINUX_CFLAGS)
673         AC_SUBST(LINUX_CC)
674         AC_SUBST(LINUX_LD)
675         AC_SUBST(LINUX_AS)
676         AC_SUBST(LINUX_MODULE_EXT)
677         AC_SUBST(LINUX_MODULE_STYLE)
678
679         AC_MSG_RESULT([ok])
680
681         AC_MSG_CHECKING(for Linux 2.4 CFLAGS)
682         AC_MSG_RESULT($LINUX_CFLAGS)
683         AC_MSG_CHECKING(for Linux 2.4 LDFLAGS)
684         AC_MSG_RESULT($LINUX_LDFLAGS)
685 ])
686
687 AC_DEFUN([AS_CHECK_LINUX_CONFIG_OPTION],
688 [
689         AC_MSG_CHECKING([Linux config option $1])
690
691         if grep '^$1=y$' ${LINUX_CONFIG} >/dev/null 2>/dev/null; then
692                 result=yes
693                 $2
694         else if grep '^$1=m$' ${LINUX_CONFIG} >/dev/null 2>/dev/null; then
695                 result=module
696                 $3
697         else
698                 result=no
699                 $4
700         fi
701         fi
702
703         AC_MSG_RESULT([$result])
704 ])
705
706 AC_DEFUN([AS_LINUX_CONFIG_OPTION],
707 [
708         AS_CHECK_LINUX_CONFIG_OPTION([$1],
709                 [$1=yes],
710                 [$1=module],
711                 [$1=no])
712
713         AM_CONDITIONAL([$1],[test "${$1}" = yes])
714 ])
715
716 AC_DEFUN([AS_LINUX_CONFIG_OPTION_MODULE],
717 [
718         AS_CHECK_LINUX_CONFIG_OPTION([$1],
719                 [$1=yes],
720                 [$1=module],
721                 [$1=no])
722
723         AM_CONDITIONAL([$1],[test "${$1}" = yes -o "${$1}" = module])
724 ])
725
726 dnl check for the major/minor version of the Linux source by checking
727 dnl the Makefile
728 dnl first argument is the linux directory
729 dnl sets LINUX_VERSION_MAJOR and LINUX_VERSION_MINOR
730 AC_DEFUN([AS_LINUX_VERSION_MAJOR_MINOR],
731 [
732         LINUX_DIR=[$1]
733         AC_MSG_CHECKING([Linux major/minor version])
734
735         if [[ ! -f "${LINUX_DIR}/Makefile" ]];then
736                 AC_MSG_ERROR([The Linux kernel Makefile does not exist.])
737         fi
738         dnl the next set of tests is for figuring out version major/minor
739         dnl use VERSION and PATCHLEVEL in the kernel Makefile
740         LINUX_VERSION_MAJOR=`sed -n 's/^VERSION = \([[0-9]]*\)/\1/p' "${LINUX_DIR}/Makefile"`
741         LINUX_VERSION_MINOR=`sed -n 's/^PATCHLEVEL = \([[0-9]]*\)/\1/p' "${LINUX_DIR}/Makefile"`
742         if [[ -z "$LINUX_VERSION_MAJOR" -o -z "$LINUX_VERSION_MINOR" ]]; then
743                 AC_MSG_ERROR([No major/minor version information found in Linux kernel Makefile.])
744         fi
745         AC_MSG_RESULT($LINUX_VERSION_MAJOR.$LINUX_VERSION_MINOR)
746 ])
747
748 # COMEDI_CHECK_LINUX_KBUILD([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
749 # -------------------------------------------------------------
750 #
751 # Check if kernel source tree is recent enough to support "Kbuild" files.
752 AC_DEFUN([COMEDI_CHECK_LINUX_KBUILD],
753 [
754         AC_MSG_CHECKING([for Kbuild support in $1])
755         dnl If $1/scripts/Makefile.build refers to $(<something>)/Kbuild
756         dnl then we support Kbuild (2.6.10 onwards).
757         if grep -q '/Kbuild' "$1/scripts/Makefile.build" 2>/dev/null; then
758                 AC_MSG_RESULT([yes])
759                 $2
760         else
761                 AC_MSG_RESULT([no])
762                 $3
763         fi
764 ])
765
766 # COMEDI_CHECK_PCMCIA_PROBE([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
767 # -------------------------------------------------------------
768 #
769 # Check if kernel pcmcia support is new enough to have a probe member in the pcmcia_driver
770 # struct.
771 AC_DEFUN([COMEDI_CHECK_PCMCIA_PROBE],
772 [
773         AC_REQUIRE([AC_PROG_EGREP])
774         AC_MSG_CHECKING([$1 for probe in pcmcia_driver struct])
775         cat "$1/include/pcmcia/ds.h" | tr \\n ' ' | [$EGREP "struct[[:space:]]+pcmcia_driver[[:space:]]*[{][^}]*probe"] > /dev/null
776         if (($?)); then
777                 AC_MSG_RESULT([no])
778                 $3
779         else
780                 AC_MSG_RESULT([yes])
781                 $2
782         fi
783 ])
784
785 # COMEDI_CHECK_HAVE_MUTEX_H([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
786 # -------------------------------------------------------------
787 #
788 # Check if kernel has <linux/mutex.h> file.
789 AC_DEFUN([COMEDI_CHECK_HAVE_MUTEX_H],
790 [
791         AC_MSG_CHECKING([$1 for include/linux/mutex.h])
792         if test -f "$1/include/linux/mutex.h"; then
793                 AC_MSG_RESULT([yes])
794                 $2
795         else
796                 AC_MSG_RESULT([no])
797                 $3
798         fi
799 ])