autoconf: m4: pass argument multiple times

88 views Asked by At

I have the need to pass an argument multiple times to my ./configure like in this way:

./configure [...] --with-folder=/path/to/folder1 --with-folder=/path/to/folder2

and to store the path(s) in a variable as a list that I will use later in my makefile.

I've tried with this:

AC_DEFUN([ADDITIONAL_FOLDER],[

AC_MSG_CHECKING([for additional folder])
AC_ARG_WITH([folder],
    [AS_HELP_STRING([--with-folder=DIR],
    [specify additional folder])],
    [
        dnl Check if correct path was passed.
        DIR="$(realpath "${withval}")"
        if test ! -d "${DIR}"; then
            AC_MSG_ERROR([incorrect folder])
        fi
    ])

AC_SUBST([FOLDERS],["${FOLDERS} ${DIR}"])
AC_MSG_RESULT([${FOLDERS}])
])

But so far I was unable to tell autoconf to do that! Is it possible and how?

1

There are 1 answers

0
ndim On

I need to correct my comment that this is probably "impossible".

However, while you can implement such option handling, you will need to deal with "$@" in sh code yourself directly after AC_INIT without affecting any of the standard macros emitting shell code after that.

See the last paragraph of https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/html_node/Initializing-configure.html:

If your configure script does its own option processing, it should inspect ‘$@’ or ‘$’ immediately after calling AC_INIT, because other Autoconf macros liberally use the set command to process strings, and this has the side effect of updating ‘$@’ and ‘$’. However, we suggest that you use standard macros like AC_ARG_ENABLE instead of attempting to implement your own option processing. See Site Configuration.

So it might be possible to handle just your --with-folder arguments from "$@" and save the values somewhere, and keep the remaining options in "$@" for the standard Autoconf generated code to deal with. It does not sound easy to implement, though:

  • iterate over $@
  • if the argument is --with-folder=foo, add foo to FOLDERS and remove the argument from $@
  • if the argument is something else, keep the argument in $@

So I would still prefer something like --with-folders=folder1,folder2,folder3 similar to

FOLDERS=""
AC_ARG_WITH([folders],
    [AS_HELP_STRING([--with-folders=DIR,...],
                    [specify additional folders])],
    [
       saved_IFS="$IFS"
       IFS=","
       for dir in ${withval}; do
         IFS="$saved_IFS"
         if test -d "$dir"; then
           AC_MSG_CHECKING([for additional folders])
           absdir="$(cd "$dir" && pwd)"
           AC_MSG_RESULT([$absdir])
           FOLDERS="$FOLDERS $absdir"
         else
           AC_MSG_ERROR([invalid folder: $dir])
         fi
       done
       IFS="$saved_IFS"
    ])
AC_SUBST([FOLDERS])

which (just like the example in the question) only works for folders without spaces anywhere in their path.