Search code examples
dependenciescommand-line-argumentsconfigureautotoolsautoconf

How to let the user provide an installation prefix for a dependency in AutoConf?


Some time ago I saw a configure script somewhere that had the possibility to specify the installation prefix of a dependency, but I don't know where that was, so I ask you to tell me how to implement this. Example:

I have a program foo that depends on a library bar. But bar's installation prefix is /home/user/.local instead of /usr. For that reason, configure will fail to find bar, though it is installed. How can I let the user provide an argument to configure containing the installation prefix of a dependency ? The command line would maybe look like the following:

~/foo$ ./configure --bar-dir=/home/user/.local

EDIT: I found following paragraph in the docs

--with-package

The package package will be installed, so configure this package to work with package.

[...]

Do not use a ‘--with’ option to specify the file name to use to find certain files. That is outside the scope of what ‘--with’ options are for.

Okay, but when what option should I use "to specify the file name to use to find certain files" ?


Solution

  • Notwithstanding the manual's objection to the practice, it is relatively common for configure scripts to support this via a --with option, so that the appropriate command line takes the form:

        ~/foo$ ./configure --with-bar-dir=/home/user/.local
    

    That difference follows in large part from the fact that there are only two built-in mechanisms for making configure recognize additional command-line arguments: AC_ARG_ENABLE and AC_ARG_WITH, and these provide for arguments with corresponding idiosyncratic forms. The --with-xyzzy=... form provided by the latter makes more sense for specifying a library directory than the --enable-xyzzy=... form provided by the former.

    For example, you might use this in your configure.ac:

    AC_ARG_WITH(
      [bar-dir],
      [AS_HELP_STRING(
        [--with-bar-dir=@<:@directory@:>@],
        [Specify directory as the location of libbar]
       )],
      [bar_ldflags="-L${withval}"]
    )
    

    Do read the documentation (linked above), as I've given only a taste of what AC_ARG_WITH does.

    How you use the data received this way depends on what else you want to do. You might put it straight into an output variable and let configure otherwise ignore it, but if you're using AC_CHECK_LIB or AC_SEARCH_LIBS to check for the library then you will need to put it in LDFLAGS for those tests. LDFLAGS is a user variable, however, so you should not make permanent changes to it:

    LDFLAGS_save=$LDFLAGS
    LDFLAGS="${LDFLAGS} ${bar_ldflags}"
    AC_CHECK_LIB([bar], [bar_init])
    LDFLAGS=$LDFLAGS_save
    

    That would be combined with putting the flags from bar_ldflags into an output variable and writing Makefile.in or Makefile.am such that it is included among the link flags.


    You also write:

    Okay, but when what option should I use "to specify the file name to use to find certain files" [if I don't use a --with argument] ?

    Supposing that we also reject --enable arguments, the only other option provided by Autoconf is environment variables. If you plan to rely on an environment variable, then you probably want to use AC_ARG_VAR() to tell Autoconf about it. That will enable you to get it documented in configure's help text, will automatically make it an output variable (without an explicit AC_SUBST) and it will make the variable "precious", such that configure will memorialize its value for re-use when the build system automatically reconfigures.