autotoolsautoconfm4

How to access default value of $prefix (/usr/local) in configure.ac?


Despite the documentation stating that $prefix is defined as /usr/local by default, when trying to expand the $prefix variable in configure.ac, it expands to NONE.

Testing with the following configure.ac:

AC_INIT
AC_PREREQ([2.52])

# Check if $prefix is defined
if test "$prefix" = "NONE"; then
  AC_MSG_ERROR([\$prefix expands to NONE])
else
  AC_MSG_NOTICE([\$prefix expands to $prefix])
fi

AC_OUTPUT

Just running ./configure gives:

configure: error: $prefix expands to NONE

However, running ./configure --prefix=/usr/local gives:

configure: $prefix expands to /usr/local

Given that $prefix defaults to /usr/local, why can't I access that variable unless I set it manually?


Solution

  • How to access default value of $prefix (/usr/local) in configure.ac?

    It depends on what you mean by that, but probably the question is moot.

    If you mean you want to access the path that will be used as the installation prefix in the event that the user doesn't specify one, then use the documented literal value (/usr/local) if you have not used AC_PREFIX_DEFAULT(), or use whatever you gave as the argument to AC_PREFIX_DEFAULT() if you did use that. There is no documented Autoconf variable or macro from which you can read that out.

    In particular, if you mean that you want to cause $prefix to take the value of the default prefix, so that you can read it out from there, then I am not aware of any way to do this other than by explicit assignment (which you should not use).

    But this is moot, because configure should not have any use for the installation prefix specified or defaulted at configuration time. To be robust, and also to comply with GNU coding standards, the build system must allow the installation prefix to be set or overridden at make time, and also, separately, at make install time. (Autoconf and Automake have good support for this.) When overridden at make time, the specified prefix must be incorporated into built files anywhere that the installation path is so encoded. It follows that configure cannot safely rely on the configuration-time prefix to reflect the actual location where files are eventually installed, nor can configure safely use its value to construct files that will be installed, so what use is its value to configure?

    Despite the documentation stating that $prefix is defined as /usr/local by default, when trying to expand the $prefix variable in configure.ac, it expands to NONE.

    I can see some things in the project help text and in the manual that might lead you to think that. However, although they tell you that the default installation prefix is /usr/local (unless you choose a different default), they do not say that that is a default value for variable $prefix, observable by the configure script.

    Given that $prefix defaults to /usr/local,

    But it doesn't.

    why can't I access that variable unless I set it manually?

    You can access the variable. Your example demonstrates that. And you can use the value to discriminate between whether an installation prefix was specified explicitly to configure or not. I imagine that this is useful to configure internally when it configures subprojects, by making it easier on one hand, to pass on the value of any --prefix option that it received, but on the other hand to avoid passing on a prefix if it did not receive one explicitly.

    Again, configure should not have any use for the configure-time installation prefix, but when the user doesn't specify one, you do know what the default is. You just can't read that from $prefix.