autoconf
[Top][All Lists]
Advanced

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

Re: adding a shell function


From: Eric Sunshine
Subject: Re: adding a shell function
Date: Tue, 20 Jan 2004 22:03:12 -0500

On Tue, 20 Jan 2004 13:04:39 -0500, Jeff Fulmer wrote:
> I'd like to add a sh function to configure.
> I figured I could add something like this to acinclude.m4:
> foo(){
> echo $1 | /usr/bin/sed 's/[A-Za-z]$/&\//g'
> }
> then in configure.in I could add something like this:
> HAHA=`foo "/home/jeff"`
> AC_SUBST(HAHA)
> autoconf never complains about this, but HAHA is never defined. Could
> anyone tell me what I'm missing?

When aclocal.m4 is being processed output is diverted, so no raw text from  
aclocal.m4 will appear in the generated configure script.  You can, however,  
create a macro in aclocal.m4 which emits this function, and then invoke that  
macro from configure.ac.  For instance:

# aclocal.m4
AC_DEFUN([EMIT_FUNC_FOO],
[foo() {
  echo $[]1 | sed 's/[[A-Za-z]]$/&\//g'
}
])

# confgure.ac
AC_INIT(...)
EMIT_FUNC_FOO
HAHA=`foo "$somevar"`
AC_SUBST([HAHA])
AC_OUTPUT

Of course, caveats apply:

(1) Shell functions are not considered 100% portable, so use at your own risk.

(2) If you feel that you absolutely must use shell functions, then take the  
following messages into account which discuss limitations of shell functions.

http://mail.gnu.org/archive/html/autoconf-patches/2003-12/msg00009.html
http://mail.gnu.org/archive/html/autoconf-patches/2003-12/msg00018.html

(3) There is no guarantee that 'sed' will be in /usr/bin (indeed, it is in  
/bin on the installation I am using presently).  It is better to allow 'sed'  
to be located via PATH (as shown above).

(4) m4 strips away [ and ] characters as part of its normal processing, so  
you need to account for this in your 'sed' expression, as illustrated above  
with [[ and ]].

(5) Positional variables such as $1 have meaning inside m4 macros, so they  
also need to be protected if you want them to be preserved and emitted to the  
shell, as shown above ($[]1 or $[1] or [$]1 or ...).

(6) You can optimize your function a bit by using the shell's 'case'  
statement (as shown below).  This is faster than 'sed' because it does not  
have to run an external program.

For especially simple functions like yours, you can often easily define m4  
macros which get the job done just as well, without worrying about  
portability problems involved with shell functions.  For instance, you can  
define an m4 macro ADD_TRAILING_SLASH which performs the same job as your  
shell function, and you can use it in the same way from configure.ac.  For  
instance:

# aclocal.m4
AC_DEFUN([ADD_TRAILING_SLASH],
[{ case $1 in
  */) echo "$1" ;;
  *) echo "$1/" ;;
esac }
])

# configure.ac
AC_INIT(...)
HAHA=`ADD_TRAILING_SLASH([$somevar])`
AC_SUBST([HAHA])
AC_OUTPUT

Note that, in this case, the $1 is an argument of the m4 macro, and is  
expanded by m4.  The $1 is not intended for the shell.

-- ES




reply via email to

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