10

I am working on a project which requires a subproject which has its own makefile and configure.ac. The subproject is a program which is used to generate source files for the main project. There is an option to disable the building of this project and attempt to use an installed version instead.

In either case I create a variable which is used in my Makefile.am containing the path to the program.

My question is, When using the local version of the project, how can I get its location for use in my Makefile.am?

I have tried

FOOPLACE=${abs_top_builddir}/bar/foo

But this always makes FOOPLACE contain only /bar/foo. I'm also not even sure this will be the right way to do this though even in principle.

crobar
  • 2,810
  • 4
  • 28
  • 46
  • 1
    Shouldn't this be `$(abs_top_builddir)` in a `Makefile.am` file? – Brett Hale Feb 23 '13 at 18:26
  • @BrettHale, I _believe_ I can't do this in the Makefile as if I am using the installed version of the program, instead of the one that would be built in my source directory, then I need to use the path to this instead. Is there a way around this? If there was a way to modify my `FOOPLACE` variable in the makefile I could put this in a conditional in the Makefile, but this didn't seem to work. – crobar Feb 23 '13 at 19:46
  • @BrettHale: In `Makefile.am` you can actually use either but the `$()` form is conventional. – Jack Kelly Feb 23 '13 at 20:44

2 Answers2

8

I just did some testing with a simple configure.ac.

I couldn't get a sensible value for $abs_top_builddir unless it was through a substitution (i.e., AC_CONFIG_FILES). According to Preset Output Variables(autoconf), they should at least be available during config.status (i.e., AC_CONFIG_COMMANDS). They are not.

Diving into config.status, I found that @abs_top_builddir@ was set to the value of $ac_abs_top_builddir. This seems strange, and I think it may be a bug. I have sent it to bug-autoconf to see what they think.

Moral of the story: It should work anywhere substitutions are done. You may be able to use $ac_abs_top_builddir within config.status, but I wouldn't rely on it.

Here is the test I used:

configure.ac:

AC_PREREQ([2.67])
AC_INIT([], [0], [foo@example.com])
AC_MSG_NOTICE([notice: ${abs_top_builddir}])
AC_MSG_NOTICE([notice+ac: ${abs_top_builddir}])
AC_CONFIG_COMMANDS_PRE([echo "pre+ac: ${ac_abs_top_builddir}"])
AC_CONFIG_COMMANDS_PRE([echo "pre: ${abs_top_builddir}"])
AC_CONFIG_COMMANDS_POST([echo "post+ac: ${ac_abs_top_builddir}"])
AC_CONFIG_COMMANDS_POST([echo "post: ${abs_top_builddir}"])
AC_CONFIG_COMMANDS([echo],
[echo "config.status+ac: ${ac_abs_top_builddir}"
echo "config.status: ${abs_top_builddir}"])
AC_CONFIG_FILES([test], [chmod +x test])
AC_OUTPUT

test.in:

#!/bin/sh
# -*- sh -*-
# @configure_input@
echo "test: @abs_top_builddir@"
echo "test+ac: @ac_abs_top_builddir@"

Run it with autoconf && ./configure && ./test.

Jack Kelly
  • 18,264
  • 2
  • 56
  • 81
0

Looking inside a configure script, I see the absolute path to where the configure script is being run from is set by using

ac_abs_confdir=`(
    cd "$srcdir" && test -r "./$ac_unique_file" || as_fn_error $? "$ac_msg"
    pwd)`

I've tested it by using $ac_abs_confdir in configure.ac and it works.

Andy Alt
  • 297
  • 2
  • 12
  • 1
    Ok. Note that this is the top source directory, not the top build directory as was asked by the OP. But, along these lines you could use `$ac_pwd` to get the top build directory. – Carlo Wood Dec 18 '19 at 00:18