Search code examples
autotoolsautoconf

How to add/use a --with-package arg in an autotools project?


I'm trying to add a mandatory path to look for headers in my autotools project. The user would configure as follows.

$ configure --with-newproj=/path/to/newproj

And then I would expect to somehow be able to use the given path in src/Makefile.am like such.

$ newproj_CFLAGS = -I${NEWPROJ_HOME}/include @CHECK_CFLAGS@

So far I have the following and this seems to output the path on the command line.

AC_ARG_WITH([newproj],
    AS_HELP_STRING([--with-newproj],
        [The top-level directory to the newproj installation.]),
    [with_newproj=yes],
    [with_newproj=no])

echo "==== with-newproj := \"$with_newproj\""

But I'm not sure how to use it in the CFLAGS line or make it mandatory.


Solution

  • The with_newproj=yes argument seems odd. In the event that the user in fact provides a path, you are replacing it with the string yes. In the event that they specify --with-newproj without specifying the path, you are being redundant with what Autoconf will do automatically.

    Inasmuch as you say the path is mandatory, the with_newproj=no argument also seems odd. In the event that the user does not specify a path, you provide no as the wanted path.

    For the AC_ARG_WITH, then, something like this would be more appropriate:

    AC_ARG_WITH([newproj],
        [AS_HELP_STRING([--with-newproj=<path>],
            [The top-level directory to the newproj installation.])],
        [AS_IF([test -d "$with_newproj"],
            [AC_MSG_NOTICE([Using newproj from "$with_newproj"])],
            [AC_MSG_ERROR([Specify an existing directory for newproj, not "$with_newproj"])])],
        [AC_MSG_ERROR([Use --with-newproj=<path> to specify the path to newproj])])
    

    If the user provides the --with (or --without) argument then Autoconf will automatically set shell variable with_newproj to the value specified by the user, or to yes or no, as appropriate. If they don't, then Autoconf will not automatically set it, and since the argument is mandatory for you, that is an error case.

    But I think your main question was about how to transfer that information to the Makefile. The central concept here is the "output variable". This is a shell variable in configure whose value is substituted into the templates processed by AC_OUTPUT. Moreover, Automake automatically provides a make variable for each of these (and it is idiomatic for Makefile.am to use these instead of using Autoconf substitutions directly). The main Autoconf macro for setting this up is AC_SUBST:

    NEWPROJ_HOME="$with_newproj"
    AC_SUBST([NEWPROJ_HOME])