autoconf
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: macros which define macros


From: Eric Blake
Subject: Re: macros which define macros
Date: Thu, 29 May 2008 17:24:04 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Sam Steingold <sds <at> gnu.org> writes:

> I use two almost identical macros:

In general, you suffer from insufficient quoting.  Also, m4 interprets $$2 the 
same as [$]$2, so you also had some unnecessary quoting.  Also, I would use 
m4sugar rather than raw m4 macros.

> 
> AC_DEFUN([CL_COMPILE_CHECK],
> [AC_MSG_CHECKING(for $1)
> AC_CACHE_VAL($2,[
> AC_TRY_COMPILE([$3],[$4], $2=yes, $2=no)
> ])
> AC_MSG_RESULT([$]$2)
> if test [$]$2 = yes; then
> ~  ifelse([$5], , :, [$5])
> ifelse([$6], , , [else
> ~  $6
> ])dnl
> fi
> ])

That should be:

AC_DEFUN([CL_COMPILE_CHECK],
  [AC_MSG_CHECKING([for $1])
  AC_CACHE_VAL([$2],[
    AC_TRY_COMPILE([$3],[$4], [$2=yes], [$2=no])
  ])
  AC_MSG_RESULT($[$2])
  if test $$2 = yes; then
    m4_if([$5], , :, [$5])
    m4_if([$6], , , [else
    $6
  ])dnl
  fi
])

I noticed you are already trying to collapse the AC_MSG* by using 
AC_CACHE_CHECK, and replacing the obsolete AC_TRY_COMPILE with 
AC_COMPILE_IFELSE - good.  You can also collapse that if/fi pair by using:
AS_IF([test $$2 = yes], [$5], [$6])

> 
> and I though that I could replace them with this:
> 
> AC_DEFUN([CL_CHECK],[dnl
> AC_CACHE_CHECK(for $2,$3,$1(AC_LANG_PROGRAM([$4],[$5]),[$3=yes], [$3=no]))
> if test [$]$3 = yes; then
> ~  ifelse([$6], , :, [$6])
> ifelse([$7], , , [else
> ~  $7
> ])dnl
> fi
> ])
> 
> AC_DEFUN([CL_COMPILE_CHECK],
> [CL_CHECK(AC_COMPILE_IFELSE,[$1],[$2],[$3],[$4],[$5],[$6])])
> AC_DEFUN([CL_LINK_CHECK],
> [CL_CHECK(AC_LINK_IFELSE,[$1],[$2],[$3],[$4],[$5],[$6])])

And it was the underquoting that bit you - when you expand CL_COMPILE_CHECK, 
you are then asking m4 to expand CL_CHECK with the expansion of 
AC_COMPILE_IFELSE given no arguments, rather than the literal string 
[AC_COMPILE_IFELSE] to be used within the expansion of CL_CHECK to expand 
AC_COMPILE_IFELSE with the desired arguments.  Try this (I haven't tested it 
myself, but it should do the trick):

AC_DEFUN([CL_CHECK],[dnl
  AC_CACHE_CHECK([for $2],[$3],
    [$1([AC_LANG_PROGRAM([$4],[$5])],[$3=yes], [$3=no])])
  AS_IF([test $$3 = yes], [$6], [$7])
])

AC_DEFUN([CL_COMPILE_CHECK], [CL_CHECK([AC_COMPILE_IFELSE],$@)])
AC_DEFUN([CL_LINK_CHECK], [CL_CHECK([AC_LINK_IFELSE],$@)])


Note that, contrary to your subject line, this was not an example of a macro 
defining a macro; rather it is an example of adding another layer of macro 
expansion to share common aspects of macros.

Macros to define macros are also possible in m4, albeit somewhat more difficult 
when it comes to generating macros with $1 in them.  This (untested) snippet 
should create CL_COMPILE_CHECK and CL_LINK_CHECK by way of defining a macro 
(CL_DEFUN_CHECKER) to define macros:

dnl CL_DEFUN_CHECKER(TYPE) - TYPE is COMPILE or LINK
dnl defines the macro CL_<TYPE>_CHECK(description, cacheval, headers, body,
dnl                                   if-true, if-false)
m4_define([CL_DEFUN_CHECKER],
  [_$0([$]1, [$]2, [$]3, [$]4, [$]5, [$]6, [$1])])

dnl _CL_DEFUN_CHECKER($1,$2,$3,$4,$5,$6,TYPE) - the first six arguments
dnl are literal strings to work around over-eager m4 parameter expansion,
dnl and TYPE is as in CL_DEFUN_CHECKER
m4_define([_CL_DEFUN_CHECKER],
[AC_DEFUN([CL_$7_CHECK],
  [AC_CACHE_CHECK([for $1],[$2],
    [AC_$7_IFELSE([AC_LANG_PROGRAM([$3],[$4])],[$2=yes], [$2=no])])
  AS_IF([test $$2 = yes], [$5], [$6])
])])

CL_DEFUN_CHECKER([COMPILE])
CL_DEFUN_CHECKER([LINK])



> 
> Another question - is there a way to reuse the results of AC_LANG_PROGRAM?

If you don't mind the duplication in the configure file, you could create an m4 
macro which expands to the same text:

m4_define([CL_PROGRAM_XCHECK], [AC_LANG_PROGRAM([#include <sys/socket.h>
#include <X11/Xlib.h>
#include <X11/Xauth.h>],
[XauGetAuthByAddr(AF_INET,0,"",13,"localhost:0.0",13,"localhost:0.0");])])

AC_LINK_IFELSE([CL_PROGRAM_XCHECK], ...


Or, if you want a smaller configure file, you could try assigning the expansion 
of AC_LANG_PROGRAM to a shell variable, then use that shell variable instead of 
re-expanding AC_LANG_PROGRAM:

cl_program_xcheck=`cat <<\EOF
AC_LANG_PROGRAM([#include <sys/socket.h>
#include <X11/Xlib.h>
#include <X11/Xauth.h>],
[XauGetAuthByAddr(AF_INET,0,"",13,"localhost:0.0",13,"localhost:0.0");])
EOF`

AC_LINK_IFELSE([$cl_program], ...

-- 
Eric Blake






reply via email to

[Prev in Thread] Current Thread [Next in Thread]