configure: Recognize kernel 3.x
[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 | 3.[[0-9]]*)
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                         # 2.6.25
185                         dir=`sed -n -e 's/^MAKEARGS *:= *-C *\([[^[:space:]]]*\).*/\1/p' "$1/Makefile"`
186                 fi
187                 if test -z "$dir"; then
188                         AC_MSG_RESULT([no])
189                         LINUX_SRC_DIR="$1"
190                 else
191                         AC_MSG_RESULT([yes])
192                         case "$dir" in
193                         .*) dir="$1/$dir" ;;
194                         esac
195                         AS_TRY_LINUX_SRC_DIR([${dir}], [LINUX_SRC_DIR=${dir}], )
196                 fi
197         fi
198
199         if test "${LINUX_SRC_DIR}" = "default" ; then
200                 AC_MSG_ERROR([Linux source directory not found])
201         fi
202
203         AC_SUBST(LINUX_SRC_DIR)
204 ])
205
206 dnl check if the given candidate path for a linux source tree is usable
207 AC_DEFUN([AS_TRY_LINUX_SRC_DIR],
208         [AC_MSG_CHECKING(for Linux source in $1)
209
210         if test -f "$1/Makefile" ; then
211                 result=yes
212                 $2
213         else
214                 result="not found"
215                 $3
216         fi
217
218         AC_MSG_RESULT($result)
219 ])
220
221 dnl allow for specifying a kernel release (uname -r) to build for
222 dnl use uname -r of running one if not specified
223 dnl store result in LINUX_KERNEL_RELEASE
224 AC_DEFUN([AS_LINUX_KERNEL_RELEASE],
225 [
226         AC_ARG_WITH([kernel-release],
227                 [AC_HELP_STRING([--with-kernel-release=RELEASE],
228                         [specify the "uname -r"-value to build for])],
229                 [LINUX_KERNEL_RELEASE="${withval}"],
230                 [LINUX_KERNEL_RELEASE=`uname -r`])
231         if test "x$LINUX_KERNEL_RELEASE" = "xyes";
232         then
233                 LINUX_KERNEL_RELEASE=`uname -r`
234         fi
235         AC_MSG_NOTICE([Using $LINUX_KERNEL_RELEASE as the uname -r value])
236 ])
237
238 dnl replaces LINUX_KERNEL_RELEASE once the Linux build directory is known
239 dnl first argument is the Linux build directory
240 AC_DEFUN([AS_LINUX_OVERRIDE_KERNEL_RELEASE],
241 [
242         INCDIR="$1/include"
243         UTSINC="${INCDIR}/linux/utsrelease.h"
244         if ! test -f "${UTSINC}"; then
245                 UTSINC="${INCDIR}/linux/version.h"
246         fi
247         if test -f "${UTSINC}"; then
248                 RELEASE=`echo UTS_RELEASE | cat "${UTSINC}" - |
249                         /lib/cpp -I "${INCDIR}" | tail -n 1 |
250                         sed 's/^"\(.*\)"$/\1/'`
251                 if test "${RELEASE}" != "UTS_RELEASE" -a "${RELEASE}" != "" \
252                         -a "${RELEASE}" != "${LINUX_KERNEL_RELEASE}"; then
253                         AC_MSG_NOTICE([Overriding uname -r value with ${RELEASE}])
254                         LINUX_KERNEL_RELEASE="${RELEASE}"
255                 fi
256
257         fi
258 ])
259
260 dnl allow for specifying a machine (uname -m) to build for
261 dnl use uname -m, of running one if not specified
262 dnl store result in LINUX_MACHINE
263 AC_DEFUN([AS_LINUX_MACHINE],
264 [
265         AC_ARG_WITH([machine],
266                 [AC_HELP_STRING([--with-machine=MACHINE],
267                         [specify the "uname -m"-value to build for])],
268                 [LINUX_MACHINE="${withval}"],
269                 [LINUX_MACHINE=`uname -m`])
270         if test "x$LINUX_MACHINE" = "xyes";
271         then
272                 LINUX_MACHINE=`uname -m`
273         fi
274         AC_MSG_NOTICE([Using $LINUX_MACHINE as the uname -m value])
275 ])
276 dnl allow for specifying an rpm target arch
277 dnl if none specified, try to guess one from running rpm querying for
278 dnl the kernel with the uname -r
279 dnl this is so configure without arguments works out of the box
280 dnl FIXME: investigate if uname -p is a correct guess for this, and if
281 dnl we should have a flag for specifying it instead
282
283 dnl this macro possibly overrides LINUX_MACHINE
284
285 dnl first argument is the kernel release building for
286 AC_DEFUN([AS_LINUX_RPM_TARGET],
287 [
288         RELEASE=$LINUX_KERNEL_RELEASE
289         AC_ARG_WITH([rpm-target],
290                 [AC_HELP_STRING([--with-rpm-target=TARGET],
291                         [specify the target arch to build for])],
292                 [LINUX_RPM_TARGET="${withval}"],
293                 [LINUX_RPM_TARGET=])
294         if test "x$LINUX_RPM_TARGET" = "x"
295         then
296                 dnl if we have rpm, try to guess the target of the kernel
297                 dnl we want to build for using rpm
298                 AC_PATH_PROG([RPM], [rpm], [no])
299                 if test "x$RPM" != "xno" ; then
300                         if $RPM -q kernel-$RELEASE > /dev/null
301                         then
302                           LINUX_RPM_TARGET=`$RPM -q --queryformat %{arch} kernel-$RELEASE`
303                         else
304                           AC_MSG_NOTICE([Cannot guess target arch, consider setting it using --with-rpm-target])
305                         fi
306                 fi
307         fi
308
309         dnl now override LINUX_MACHINE if LINUX_RPM_TARGET is set
310         if test "x$LINUX_RPM_TARGET" != "x"
311         then
312                 dnl override LINUX_MACHINE based on this
313                 dnl FIXME: add other possible Red Hat/Fedora/rpm targets here
314                 LINUX_MACHINE=
315                 case "$LINUX_RPM_TARGET" in
316                         i?86) LINUX_MACHINE=i386;;
317                         athlon) LINUX_MACHINE=i386;;
318                         x86_64) LINUX_MACHINE=x86_64;;
319                 esac
320                 if test "x$LINUX_MACHINE" = "x"
321                 then
322                 AC_MSG_ERROR(Could not guess uname -m value from target $LINUX_RPM_TARGET)
323                 fi
324         fi
325 ])
326
327
328 dnl allow for specifying an override for EXTRAVERSION
329 dnl if none specified, make it from the passed-in KERNEL_RELEASE (uname -r)
330 dnl resulting value is stored in LINUX_EXTRAVERSION
331 dnl first argument is the uname -r string to use as a fallback
332 AC_DEFUN([AS_LINUX_EXTRAVERSION],
333 [
334         KERNEL_RELEASE=[$1]
335         dnl extract the default by getting everything after first -
336         LINUX_EXTRAVERSION="-`echo $KERNEL_RELEASE | cut -d- -f 2-`"
337         AC_ARG_WITH([extraversion],
338                 [AC_HELP_STRING([--with-extraversion=FILE],
339                         [specify override for kernel EXTRAVERSION])],
340                 [LINUX_EXTRAVERSION="${withval}"],)
341 ])
342
343
344 dnl check if we can find a config file for the Linux kernel
345 dnl defines LINUX_CONFIG to the absolute location of a usable
346 dnl kernel source config file
347
348 dnl for rpm distros, it can try and guess the correct config file by
349 dnl checking the global LINUX_RPM_TARGET variable set somewhere else
350 dnl (FIXME: move this to an argument instead ?)
351
352 dnl first argument is LINUX_DIR to check for possible configs
353 dnl second argument is kernel-release (uname -r)
354 dnl third argument is machine (uname -m)
355
356 AC_DEFUN([AS_LINUX_CONFIG],
357 [
358         LINUX_DIR=[$1]
359         KERNEL_RELEASE=[$2]
360         MACHINE=[$3]
361         AC_ARG_WITH([linuxconfig],
362                 [AC_HELP_STRING([--with-linuxconfig=FILE],
363                         [specify path to Linux configuration file])],
364                 [LINUX_CONFIG="${withval}"],
365                 [LINUX_CONFIG=default])
366
367         dnl if a file got specified, try it as a linux config file
368         if test "${LINUX_CONFIG}" != "default" ; then
369                 AS_TRY_LINUX_CONFIG($LINUX_CONFIG, $MACHINE,
370                                     ,, AC_MSG_ERROR([Linux config not found]) )
371         fi
372
373         dnl if no file specified, first check for the regular .config file
374         dnl in LINUX_DIR created by manual configuration
375         if test "${LINUX_CONFIG}" = "default" ; then
376                 file="$LINUX_DIR/.config";
377                 AS_TRY_LINUX_CONFIG($file, $MACHINE,
378                                     [LINUX_CONFIG=${file}], )
379         fi
380         dnl second, try to guess what config file to use for the current kernel
381         dnl FIXME: the possible arch is from rpmbuild --target, and is
382         dnl different from the value of ARCH (Makefile) or MACHINE (uname -m)
383         dnl so we should have a redhat flag to specify the target to find
384         dnl the correct config file
385         if test "${LINUX_CONFIG}" = "default" && test "x$LINUX_RPM_TARGET" != "x"; then
386                 dnl Red Hat stores configuration files for their built kernels
387                 dnl named kernel-(version)-(arch)(extra).config
388                 dnl where arch is athlon, i386, i586, i686, x86_64
389                 dnl and (extra) is empty or -smp, -BOOT, -bigmem
390                 dnl haven't seen combinations of extra yet as of FC1
391                 KVERSION=`echo $KERNEL_RELEASE | cut -d- -f1`
392                 extra=
393                 echo $KERNEL_RELEASE | grep smp && EXTRA="-smp"
394                 echo $KERNEL_RELEASE | grep bigmem && EXTRA="-bigmem"
395                 echo $KERNEL_RELEASE | grep BOOT && EXTRA="-BOOT"
396                 file="$LINUX_DIR/configs/kernel-$KVERSION-$LINUX_RPM_TARGET$EXTRA.config"
397                 AS_TRY_LINUX_CONFIG($file, $MACHINE,
398                                     [LINUX_CONFIG=${file}], )
399         fi
400         if test "${LINUX_CONFIG}" = "default" ; then
401                 AC_MSG_ERROR([
402 The kernel source tree at ${LINUX_DIR} is not configured,
403 and no configuration files in config/ matching your kernel were found.
404 Fix before continuing or specify a config file using --with-configfile.])
405         fi
406
407         AC_SUBST(LINUX_CONFIG)
408 ])
409
410 dnl check if the given candidate config file is usable
411 dnl FIXME: it would be nice if it could check if it matches the
412 dnl given machine (uname -m)
413 AC_DEFUN([AS_TRY_LINUX_CONFIG],
414 [
415         CFG=[$1]
416         MACHINE=[$2]
417         AC_MSG_CHECKING($CFG)
418
419         if test -f "$CFG" ; then
420                 result=yes
421                 ifelse([$3], , :, [$3])
422         else
423                 result="not found"
424                 ifelse([$4], , :, [$4])
425         fi
426         AC_MSG_RESULT($result)
427 ])
428
429 dnl check if RED_HAT_LINUX_KERNEL is defined
430 dnl pre-FC2 RH/Fedora defines this in linux/rhconfig.h
431 dnl included from linux/version.h
432 dnl if this is present, a few extra defines need to be present to make sure
433 dnl symbol versioning is correct
434 dnl uses LINUX_DIR to find rhconfig.h
435 AC_DEFUN([AS_CHECK_REDHAT_PRE_FC2],
436 [
437         AC_MSG_CHECKING(Pre-FC2 Red Hat/Fedora kernel)
438         HAVE_REDHAT_KERNEL=false
439         ac_save_CFLAGS="$CFLAGS"
440         CFLAGS="$CFLAGS -I${LINUX_DIR}/include/linux"
441         AC_COMPILE_IFELSE(AC_LANG_PROGRAM([
442 #include "rhconfig.h"
443 int code = RED_HAT_LINUX_KERNEL;
444         ]),
445         AC_MSG_RESULT(found); HAVE_REDHAT_KERNEL=true,
446         AC_MSG_RESULT(not found))
447         dnl restore CFLAGS
448         CFLAGS="$ac_save_CFLAGS"
449
450         dnl check for Red Hat define flags to use - see /sbin/mkkerneldoth
451
452         dnl initialize the booleans we want to detect
453         ENTERPRISE='0'
454         SMP='0'
455         UP='0'
456         BIGMEM='0'
457         HUGEMEM='0'
458
459         dnl get variables from the currently running kernel as default
460         KERNEL_TYPE=`uname -r | sed 's_^.*\(smp\|enterprise\|bigmem\|hugemem\)$_\1_;t;s_.*__;'`
461         KERNEL_RELEASE=`uname -r | sed 's|smp\|enterprise\|bigmem\|hugemem||g'`
462         KERNEL_ARCH=`uname -m`
463
464         dnl check the config file and override KERNEL_ARCH
465         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_M386, KERNEL_ARCH=i386)
466         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_M586, KERNEL_ARCH=i586)
467         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_M686, KERNEL_ARCH=i686)
468         dnl for some reason the i686 bigmem config file has PENTIUM
469         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_MPENTIUMIII, KERNEL_ARCH=i686)
470         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_MK7, KERNEL_ARCH=athlon)
471
472         dnl check the config file and override KERNEL_TYPE
473         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_SMP, KERNEL_TYPE=smp)
474         dnl bigmem is also smp, so this check is done after smp to override
475         AS_CHECK_LINUX_CONFIG_OPTION(CONFIG_HIGHMEM64G, KERNEL_TYPE=bigmem)
476
477         dnl FIXME: need to check hugemem and enterprise config files, which
478         dnl aren't provided in Fedora Core 1 !
479
480         case "$KERNEL_TYPE" in
481                 smp) SMP='1';;
482                 enterprise) ENTERPRISE='1';;
483                 bigmem) BIGMEM='1';;
484                 hugemem) HUGEMEM='1';;
485                 *) UP='1';;
486         esac
487         REDHAT_CFLAGS="-D__MODULE_KERNEL_$KERNEL_ARCH=1"
488         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_ENTERPRISE=$ENTERPRISE"
489         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_UP=$UP"
490         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_SMP=$SMP"
491         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_BIGMEM=$BIGMEM"
492         REDHAT_CFLAGS="$REDHAT_CFLAGS -D__BOOT_KERNEL_HUGEMEM=$HUGEMEM"
493
494         LINUX_REDHAT_CFLAGS="$REDHAT_CFLAGS"
495
496         AC_SUBST(LINUX_KERNEL_TYPE, "$KERNEL_TYPE")
497 ])
498
499 dnl add an argument to specify/override the module install path
500 dnl if nothing specified, it will be /lib/modules/(kernelrel)
501 AC_DEFUN([AS_LINUX_MODULESDIR],
502 [
503         KERNEL_RELEASE=[$1]
504         AC_ARG_WITH([modulesdir],
505                 [AC_HELP_STRING([--with-modulesdir=DIR],
506                         [specify path to kernel-specific modules install directory])],
507                 [MODULESDIR="${withval}"],
508                 [MODULESDIR=default])
509
510         if test "${MODULESDIR}" = "default" ; then
511                 MODULESDIR="/lib/modules/${KERNEL_RELEASE}"
512         fi
513         dnl make it available to Makefiles so it can be used in ...dir
514         AC_SUBST(modulesdir, $MODULESDIR)
515         AC_MSG_NOTICE([Putting kernel modules under ${MODULESDIR}])
516 ])
517
518 dnl add an argument to specify/override the module devel install path
519 dnl if nothing specified, it will be the passed in LINUXDIR)
520 AC_DEFUN([AS_LINUX_MODULESDEVELDIR],
521 [
522         LINUXDIR=[$1]
523         AC_ARG_WITH([modulesdeveldir],
524                 [AC_HELP_STRING([--with-modulesdeveldir=DIR],
525                         [specify path to kernel-specific module development install directory])],
526                 [MODULESDEVELDIR="${withval}"],
527                 [MODULESDEVELDIR=default])
528
529         if test "${MODULESDEVELDIR}" = "default" ; then
530                 MODULESDEVELDIR="${LINUXDIR}"
531         fi
532         dnl make it available to Makefiles so it can be used in ...dir
533         AC_SUBST(modulesdeveldir, $MODULESDEVELDIR)
534         AC_MSG_NOTICE([Putting kernel module development files under ${MODULESDEVELDIR}])
535 ])
536
537 AC_DEFUN([AS_LINUX_2_6],
538 [
539         AC_MSG_CHECKING(for Linux CFLAGS)
540
541         {
542           tmpdir=`(umask 077 && mktemp -d -q "./confstatXXXXXX") 2>/dev/null` &&
543           test -n "$tmpdir" && test -d "$tmpdir"
544         } ||
545         {
546           tmpdir=./confstat$$-$RANDOM
547           (umask 077 && mkdir $tmpdir)
548         } ||
549         {
550           echo "$me: cannot create a temporary directory in ." >&2
551           { (exit 1); exit 1; }
552         }
553
554         tmpdir=`pwd`/$tmpdir
555
556         cat >${tmpdir}/Makefile <<EOF
557 obj-m += fake.o
558
559 \$(obj)/fake.c: flags
560         touch \$(obj)/fake.c
561
562 .PHONY: flags
563 flags:
564         echo LINUX_ARCH=\"\$(ARCH)\" >>\$(obj)/flags
565         echo LINUX_AFLAGS=\"\$(AFLAGS)\" | sed 's,include,"\$(LINUXDIR)/include",g'>>\$(obj)/flags
566         echo LINUX_LDFLAGS=\"\" >>\$(obj)/flags
567         echo LINUX_ARFLAGS=\"\$(ARFLAGS)\" >>\$(obj)/flags
568         echo LINUX_CROSS_COMPILE=\"\$(CROSS_COMPILE)\" >>\$(obj)/flags
569         echo LINUX_KERNELRELEASE=\"\$(KERNELRELEASE)\" >>\$(obj)/flags
570         echo LINUX_CFLAGS=\"\$(CFLAGS) \$(CPPFLAGS)\" \
571                 | sed -e 's,-Iinclude,-I\$(LINUXDIR)/include,g' -e 's,-include include,-include \$(LINUXDIR)/include,g' >>\$(obj)/flags
572         echo LINUX_CFLAGS_MODULE=\"\$(CFLAGS_MODULE)\" >>\$(obj)/flags
573         echo LINUX_CC=\"\$(CC)\" >>\$(obj)/flags
574         echo LINUX_LD=\"\$(LD) \$(LDFLAGS) \$(LDFLAGS_MODULE)\" >>\$(obj)/flags
575         echo LINUX_AS=\"\$(AS)\" >>\$(obj)/flags
576         echo LINUX_MODLIB=\"\$(MODLIB)\" >>\$(obj)/flags
577 EOF
578
579         echo ${MAKE-make} -C ${LINUX_DIR} V=1 SUBDIRS=${tmpdir} LINUXDIR=${LINUX_DIR} modules >&5 2>&5
580         ${MAKE-make} -C ${LINUX_DIR} V=1 SUBDIRS=${tmpdir} LINUXDIR=${LINUX_DIR} modules >&5 2>&5
581         . ${tmpdir}/flags
582         rm -rf ${tmpdir}
583
584         LINUX_MODULE_EXT=".ko"
585         LINUX_MODULE_STYLE="2.6.6"
586
587         LINUX_CFLAGS="$LINUX_CFLAGS $LINUX_CFLAGS_MODULE"
588
589         AC_SUBST(LINUX_ARCH)
590         AC_SUBST(LINUX_AFLAGS)
591         AC_SUBST(LINUX_LDFLAGS)
592         AC_SUBST(LINUX_ARFLAGS)
593         AC_SUBST(LINUX_CROSS_COMPILE)
594         AC_SUBST(LINUX_KERNELRELEASE)
595         AC_SUBST(LINUX_CFLAGS)
596         AC_SUBST(LINUX_CC)
597         AC_SUBST(LINUX_LD)
598         AC_SUBST(LINUX_AS)
599         AC_SUBST(LINUX_MODULE_EXT)
600         AC_SUBST(LINUX_MODULE_STYLE)
601         AC_SUBST(LINUX_MODPOST)
602         AC_SUBST(LINUX_MODLIB)
603
604         AC_MSG_RESULT([$LINUX_CFLAGS])
605
606         AC_PATH_PROG([LINUX_MODPOST], ["modpost"], ["no"], ["$LINUX_DIR/scripts:$LINUX_DIR/scripts/mod"])
607 ])
608
609
610 AC_DEFUN([AS_LINUX_2_4],
611 [
612         AC_MSG_CHECKING(for Linux 2.4 make flags)
613         dnl we try to figure out the CFLAGS by invoking the Makefile on
614         dnl a test dir
615         dnl we use the correct config file by substituting the MAKEFILES
616         dnl Makefile variable, which originally points to .config
617
618         if [[ ! -e "${LINUX_DIR}/.hdepend" ]];then
619                 AC_MSG_WARN([
620 No .hdepend file found, you may need to run 'make dep' on the kernel source before continuing.])
621         fi
622
623         {
624           tmpdir=`(umask 077 && mktemp -d -q "./confstatXXXXXX") 2>/dev/null` &&
625           test -n "$tmpdir" && test -d "$tmpdir"
626         } ||
627         {
628           tmpdir=./confstat$$-$RANDOM
629           (umask 077 && mkdir $tmpdir)
630         } ||
631         {
632           echo "$me: cannot create a temporary directory in ." >&2
633           { (exit 1); exit 1; }
634         }
635         tmpdir=`pwd`/$tmpdir
636
637         #sed "s|\.config|${CONFIG_FILE}|g" ${LINUX_DIR}/Makefile >$tmpdir/Makefile
638         cat >$tmpdir/Makefile <<EOF
639 modules:
640         @echo LINUX_ARCH=\"\$(ARCH)\"
641         @echo LINUX_AFLAGS=\"\$(AFLAGS)\" | sed 's_Iinclude_I\"\$(LINUXDIR)/include\"_g'
642         @echo LINUX_LDFLAGS=\"\"
643         @echo LINUX_ARFLAGS=\"\$(ARFLAGS)\"
644         @echo LINUX_CROSS_COMPILE=\"\$(CROSS_COMPILE)\"
645         @echo LINUX_KERNELRELEASE=\"\$(KERNELRELEASE)\"
646         @echo LINUX_CFLAGS=\"\$(CFLAGS)\" | sed 's_Iinclude_I\"\$(LINUXDIR)/include\"_g'
647         @echo LINUX_MODFLAGS=\"\$(MODFLAGS)\"
648         @echo LINUX_CC=\"\$(CC)\"
649         @echo LINUX_LD=\"\$(LD) \$(LDFLAGS)\"
650         @echo LINUX_AS=\"\$(AS)\"
651 EOF
652         make -C ${LINUX_DIR} SUBDIRS=${tmpdir} modules | grep ^LINUX_ >${tmpdir}/ack
653         if (($?)); then
654                 echo make modules with LINUX_DIR=$(LINUX_DIR) failed.
655                 exit 1
656         fi
657         . ${tmpdir}/ack
658         rm -rf ${tmpdir}
659
660         LINUX_MODULE_EXT=".o"
661         LINUX_MODULE_STYLE="2.4.0"
662
663         LINUX_CFLAGS="$LINUX_CFLAGS $LINUX_MODFLAGS"
664
665         dnl if we have REDHAT_CFLAGS, put them in LINUX_CFLAGS
666         if test "x$REDHAT_CFLAGS" != "x";
667         then
668                 LINUX_CFLAGS="$LINUX_CFLAGS $REDHAT_CFLAGS"
669         fi
670         AC_SUBST(LINUX_ARCH)
671         AC_SUBST(LINUX_AFLAGS)
672         AC_SUBST(LINUX_LDFLAGS)
673         AC_SUBST(LINUX_ARFLAGS)
674         AC_SUBST(LINUX_CROSS_COMPILE)
675         AC_SUBST(LINUX_KERNELRELEASE)
676         AC_SUBST(LINUX_CFLAGS)
677         AC_SUBST(LINUX_CC)
678         AC_SUBST(LINUX_LD)
679         AC_SUBST(LINUX_AS)
680         AC_SUBST(LINUX_MODULE_EXT)
681         AC_SUBST(LINUX_MODULE_STYLE)
682
683         AC_MSG_RESULT([ok])
684
685         AC_MSG_CHECKING(for Linux 2.4 CFLAGS)
686         AC_MSG_RESULT($LINUX_CFLAGS)
687         AC_MSG_CHECKING(for Linux 2.4 LDFLAGS)
688         AC_MSG_RESULT($LINUX_LDFLAGS)
689 ])
690
691 AC_DEFUN([AS_CHECK_LINUX_CONFIG_OPTION],
692 [
693         AC_MSG_CHECKING([Linux config option $1])
694
695         if grep '^$1=y$' ${LINUX_CONFIG} >/dev/null 2>/dev/null; then
696                 result=yes
697                 $2
698         else if grep '^$1=m$' ${LINUX_CONFIG} >/dev/null 2>/dev/null; then
699                 result=module
700                 $3
701         else
702                 result=no
703                 $4
704         fi
705         fi
706
707         AC_MSG_RESULT([$result])
708 ])
709
710 AC_DEFUN([AS_LINUX_CONFIG_OPTION],
711 [
712         AS_CHECK_LINUX_CONFIG_OPTION([$1],
713                 [$1=yes],
714                 [$1=module],
715                 [$1=no])
716
717         AM_CONDITIONAL([$1],[test "${$1}" = yes])
718 ])
719
720 AC_DEFUN([AS_LINUX_CONFIG_OPTION_MODULE],
721 [
722         AS_CHECK_LINUX_CONFIG_OPTION([$1],
723                 [$1=yes],
724                 [$1=module],
725                 [$1=no])
726
727         AM_CONDITIONAL([$1],[test "${$1}" = yes -o "${$1}" = module])
728 ])
729
730 dnl check for the major/minor version of the Linux source by checking
731 dnl the Makefile
732 dnl first argument is the linux directory
733 dnl sets LINUX_VERSION_MAJOR and LINUX_VERSION_MINOR
734 AC_DEFUN([AS_LINUX_VERSION_MAJOR_MINOR],
735 [
736         LINUX_DIR=[$1]
737         AC_MSG_CHECKING([Linux major/minor version])
738
739         if [[ ! -f "${LINUX_DIR}/Makefile" ]];then
740                 AC_MSG_ERROR([The Linux kernel Makefile does not exist.])
741         fi
742         dnl the next set of tests is for figuring out version major/minor
743         dnl use VERSION and PATCHLEVEL in the kernel Makefile
744         LINUX_VERSION_MAJOR=`sed -n 's/^VERSION = \([[0-9]]*\)/\1/p' "${LINUX_DIR}/Makefile"`
745         LINUX_VERSION_MINOR=`sed -n 's/^PATCHLEVEL = \([[0-9]]*\)/\1/p' "${LINUX_DIR}/Makefile"`
746         if [[ -z "$LINUX_VERSION_MAJOR" -o -z "$LINUX_VERSION_MINOR" ]]; then
747                 AC_MSG_ERROR([No major/minor version information found in Linux kernel Makefile.])
748         fi
749         AC_MSG_RESULT($LINUX_VERSION_MAJOR.$LINUX_VERSION_MINOR)
750 ])
751
752 # COMEDI_CHECK_LINUX_KBUILD([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
753 # -------------------------------------------------------------
754 #
755 # Check if kernel source tree is recent enough to support "Kbuild" files.
756 AC_DEFUN([COMEDI_CHECK_LINUX_KBUILD],
757 [
758         AC_MSG_CHECKING([for Kbuild support in $1])
759         dnl If $1/scripts/Makefile.build refers to $(<something>)/Kbuild
760         dnl then we support Kbuild (2.6.10 onwards).
761         if grep -q '/Kbuild' "$1/scripts/Makefile.build" 2>/dev/null; then
762                 AC_MSG_RESULT([yes])
763                 $2
764         else
765                 AC_MSG_RESULT([no])
766                 $3
767         fi
768 ])
769
770 # COMEDI_CHECK_PCMCIA_PROBE([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
771 # -------------------------------------------------------------
772 #
773 # Check if kernel pcmcia support is new enough to have a probe member in the pcmcia_driver
774 # struct.
775 AC_DEFUN([COMEDI_CHECK_PCMCIA_DRIVER_PROBE],
776 [
777         AC_REQUIRE([AC_PROG_EGREP])
778         AC_MSG_CHECKING([$1 for probe in pcmcia_driver struct])
779         cat "$1/include/pcmcia/ds.h" | tr \\n ' ' | [$EGREP "struct[[:space:]]+pcmcia_driver[[:space:]]*[{][^}]*probe"] > /dev/null
780         if (($?)); then
781                 AC_MSG_RESULT([no])
782                 $3
783         else
784                 AC_MSG_RESULT([yes])
785                 $2
786         fi
787 ])
788
789 # COMEDI_CHECK_PCMCIA_DRIVER_NAME([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
790 # -------------------------------------------------------------
791 #
792 # Check if kernel pcmcia support is new enough to have a name member in the pcmcia_driver
793 # struct.
794 AC_DEFUN([COMEDI_CHECK_PCMCIA_DRIVER_NAME],
795 [
796         AC_REQUIRE([AC_PROG_EGREP])
797         AC_MSG_CHECKING([$1 for name in pcmcia_driver struct])
798         cat "$1/include/pcmcia/ds.h" | tr \\n ' ' | [$EGREP "struct[[:space:]]+pcmcia_driver[[:space:]]*[{][^}]*name"] > /dev/null
799         if (($?)); then
800                 AC_MSG_RESULT([no])
801                 $3
802         else
803                 AC_MSG_RESULT([yes])
804                 $2
805         fi
806 ])
807
808 # COMEDI_CHECK_PCMCIA_LOOP_TUPLE([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
809 # -------------------------------------------------------------
810 #
811 # Check if kernel pcmcia support is new enough to have the pcmcia_loop_tuple
812 # function.
813 AC_DEFUN([COMEDI_CHECK_PCMCIA_LOOP_TUPLE],
814 [
815         AC_MSG_CHECKING([$1 for pcmcia_loop_tuple function])
816         if [grep -q 'int[[:space:]]\+pcmcia_loop_tuple[[:space:]]*(' "$1/include/pcmcia/ds.h"] 2>/dev/null ; then
817                 AC_MSG_RESULT([yes])
818                 $2
819         else
820                 AC_MSG_RESULT([no])
821                 $3
822         fi
823 ])
824
825 # COMEDI_CHECK_DS_DEV_NODE_T([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
826 # -------------------------------------------------------------
827 #
828 # Check if kernel pcmcia support is old enough to have the dev_node_t type
829 # in pcmcia/ds.h.
830 AC_DEFUN([COMEDI_CHECK_DS_DEV_NODE_T],
831 [
832         AC_MSG_CHECKING([$1 for dev_node_t in pcmcia/ds.h (removed in 2.6.35)])
833         if [grep -q dev_node_t "$1/include/pcmcia/ds.h"] 2>/dev/null ; then
834                 AC_MSG_RESULT([yes])
835                 $2
836         else
837                 AC_MSG_RESULT([no])
838                 $3
839         fi
840 ])
841
842 # COMEDI_CHECK_CS_IRQ_REQ_T([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
843 # -------------------------------------------------------------
844 #
845 # Check if kernel pcmcia support is old enough to have the irq_req_t type
846 # in pcmcia/cs.h.
847 AC_DEFUN([COMEDI_CHECK_CS_IRQ_REQ_T],
848 [
849         AC_MSG_CHECKING([$1 for irq_req_t in pcmcia/cs.h (removed in 2.6.35)])
850         if [grep -q irq_req_t "$1/include/pcmcia/cs.h"] 2>/dev/null ; then
851                 AC_MSG_RESULT([yes])
852                 $2
853         else
854                 AC_MSG_RESULT([no])
855                 $3
856         fi
857 ])
858
859 # COMEDI_CHECK_CS_IO_REQ_T([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
860 # -------------------------------------------------------------
861 #
862 # Check if kernel pcmcia support is old enough to have the io_req_t type
863 # in pcmcia/cs.h.
864 AC_DEFUN([COMEDI_CHECK_CS_IO_REQ_T],
865 [
866         AC_MSG_CHECKING([$1 for io_req_t in pcmcia/cs.h (removed in 2.6.36)])
867         if [grep -q io_req_t "$1/include/pcmcia/cs.h"] 2>/dev/null ; then
868                 AC_MSG_RESULT([yes])
869                 $2
870         else
871                 AC_MSG_RESULT([no])
872                 $3
873         fi
874 ])
875
876 # COMEDI_CHECK_CS_MEMREQ_T([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
877 # -------------------------------------------------------------
878 #
879 # Check if kernel pcmcia support is old enough to have the memreq_t type
880 # in pcmcia/cs.h.
881 AC_DEFUN([COMEDI_CHECK_CS_MEMREQ_T],
882 [
883         AC_MSG_CHECKING([$1 for memreq_t in pcmcia/cs.h (removed in 2.6.36)])
884         if [grep -q memreq_t "$1/include/pcmcia/cs.h"] 2>/dev/null ; then
885                 AC_MSG_RESULT([yes])
886                 $2
887         else
888                 AC_MSG_RESULT([no])
889                 $3
890         fi
891 ])
892
893 # COMEDI_CHECK_HAVE_CS_TYPES_H([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
894 # -------------------------------------------------------------
895 #
896 # Check if kernel has <pcmcia/cs_types.h> file.
897 AC_DEFUN([COMEDI_CHECK_HAVE_CS_TYPES_H],
898 [
899         AC_MSG_CHECKING([$1 for include/pcmcia/cs_types.h (removed in 2.6.36)])
900         if test -f "$1/include/pcmcia/cs_types.h"; then
901                 AC_MSG_RESULT([yes])
902                 $2
903         else
904                 AC_MSG_RESULT([no])
905                 $3
906         fi
907 ])
908
909 # COMEDI_CHECK_HAVE_CS_H([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
910 # -------------------------------------------------------------
911 #
912 # Check if kernel has <pcmcia/cs.h> file.
913 AC_DEFUN([COMEDI_CHECK_HAVE_CS_H],
914 [
915         AC_MSG_CHECKING([$1 for include/pcmcia/cs.h (removed in 2.6.37)])
916         if test -f "$1/include/pcmcia/cs.h"; then
917                 AC_MSG_RESULT([yes])
918                 $2
919         else
920                 AC_MSG_RESULT([no])
921                 $3
922         fi
923 ])
924
925 # COMEDI_CHECK_HAVE_MUTEX_H([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
926 # -------------------------------------------------------------
927 #
928 # Check if kernel has <linux/mutex.h> file.
929 AC_DEFUN([COMEDI_CHECK_HAVE_MUTEX_H],
930 [
931         AC_MSG_CHECKING([$1 for include/linux/mutex.h])
932         if test -f "$1/include/linux/mutex.h"; then
933                 AC_MSG_RESULT([yes])
934                 $2
935         else
936                 AC_MSG_RESULT([no])
937                 $3
938         fi
939 ])
940
941 # COMEDI_CHECK_HAVE_LINUX_SEMAPHORE_H([LINUX_SOURCE_PATH], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
942 # -------------------------------------------------------------
943 #
944 # Check if kernel has <linux/semaphore.h> file.
945 AC_DEFUN([COMEDI_CHECK_HAVE_LINUX_SEMAPHORE_H],
946 [
947         AC_MSG_CHECKING([$1 for include/linux/semaphore.h])
948         if test -f "$1/include/linux/semaphore.h"; then
949                 AC_MSG_RESULT([yes])
950                 $2
951         else
952                 AC_MSG_RESULT([no])
953                 $3
954         fi
955 ])