Autoconf Macro Archive ac-archive.sf.net: - Project CVS - Download Macro Index - AM Support - C++ Support - C Support - Fortran Support - Java Support - Cross Compilation - Installed Packages - Miscellaneous - LaTeX Support - Uncategorized - archive macros - adl's macros - bkorb's macros - guidod's macros - latex's macros - other's macros - rleigh's macros - obsoleted macros - released macros - search index Documentation - Contribute! - History - acincludedir m4 - acinclude (tool) - macro howto - ax tricks - maintainers - License - Topics generated... (C) 2007 guidod |
This is a step-by-step description howto turn your own configure check into an autoconf macro - this text tells about the ways to polish the script into a shiny piece that you will really feel like showing them on the Macro Archive. We suppose you have written a small check already used in your "configure" script - which is actually a bourne shell script that calls other tools and possibly sets an AC_SUBST variable to be replaced in your Makefile.in. It may look like the following check for a "gnu make" in the build environment: make_command="" for a in "$MAKE" make gmake gnumake do test -z "$a" && continue if ( sh -c "$a --version" 2>/dev/null | grep GNU >/dev/null ) then make_command=$a ; break; fi done if test -z $make_command then ifGNUmake="" else ifGNUmake="#" fi AC_SUBST(ifGNUmake) In the first step, one would extract that part from the "configure" script and put it into its own file. For a first test, please use the "acinclude.m4" file. The next call of `aclocal && autoconf` will copy the new macro to "aclocal.m4" and from there to "configure". So, now you have an extra file "acinclude.m4" reading: AC_DEFUN(CHECK_GNU,[ make_command="" for a in "$MAKE" make gmake gnumake do test -z "$a" && continue if ( sh -c "$a --version" 2>/dev/null | grep GNU >/dev/null ) then make_command=$a ; break; fi done if test -n $make_command then ifGNUmake="" else ifGNUmake="#" fi AC_SUBST(ifGNUmake) ])and in its place in the original "configure.in" you write that name as: .... CHECK_GNU ... Now you are already done with having your own autoconf macro. And you can be quite sure that it will work in your project - and probably you have tested it on the platforms you do usually build for. Believe it or not, this is a rather "controlled" environment - if you push the macro to the AC Macro Archive then your macro might make it into other projects where they will meet lots of other macros, lots of other shell code, lots of different platforms. And possibly, it will fail over there - however there are some steps that can be checked before.
After applying all these guideline, the macro might now look like this: AC_DEFUN([AX_CHECK_GNU_MAKE],[ AC_CACHE_CHECK([for GNU make],ax_cv_gnu_make_command,[ AS_VAR_PUSHEDEF([_make_command],ax_cv_gnu_make_command)dnl _make_command="" for a in "$MAKE" make gmake gnumake do test "x$a" = "x" && continue if ( sh -c "$a --version" 2>/dev/null | grep GNU >/dev/null ) then _make_command="$a" ; break; fi done if test "x$_make_command" != "x" then ifGNUmake="" AC_MSG_RESULT([$_make_command]) else ifGNUmake="#" AC_MSG_RESULT([nothing found]) fi AC_SUBST([ifGNUmake]) AS_VAR_POPDEF([_make_command])dnl ]]) Now the macro is clean - just add a description block as required in the contribution format and send it to ac-archive-maintainers@gnu.org However, we can even get to the next level of autoconf macro coding.
dnl AX_CHECK_GNU_MAKE([make names],[ACTION-IF-FOUND],[ACTION-IF-NOT]) AC_DEFUN([AX_CHECK_GNU_MAKE],[ AC_CACHE_CHECK([for GNU make],ax_cv_gnu_make_command,[ AS_VAR_PUSHEDEF([_make_command],ax_cv_gnu_make_command)dnl _make_command="" for a in "$MAKE" $1 make gmake gnumake do test "x$a" = "x" && continue if ( sh -c "$a --version" 2>/dev/null | grep GNU >/dev/null ) then _make_command="$a" ; break; fi done if test "x$_make_command" != "x" then ifGNUmake="" AC_MSG_RESULT([$_make_command]) $2 else ifGNUmake="#" AC_MSG_RESULT([nothing found]) $3 fi AC_SUBST([ifGNUmake]) AS_VAR_POPDEF([_make_command]) ]])
Among the possible usages - one can avoid the extra space in the example if there are no optional $2 or $3 argument. We use the m4sugar m4_ifvaln for that, i.e. then ifGNUmake="" AC_MSG_RESULT([$_make_command]) m4_ifvaln([$2],[$2])dnl else ifGNUmake="#" AC_MSG_RESULT([nothing found]) m4_ifvaln([$3],[$3])dnl fi In this example it would only be one line saved - but one can save a lot more in other places. Let's consider the case that the check-value is only used to execute an ACTION whereas both action-defs are optional as arguments. The shell does have a default "no-operation" statement named ":", so the first attempt might read as if test "x$testvalue" = "xyes" then $2 : else $3 : fi That one is simply needed since the shell might call for a "syntax error" if there is nothing after "then" and before "else". And in fact, the $2 and $3 ACTION might be empty. A more interesting approach would be to use some m4sugar as if test "x$testvalue" = "xyes" ; then m4_ifvaln([$2],[$2],[:])dnl m4_ifvaln([$3],[else $3])dnl fi |