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