diff mbox

configure: make source tree build more robust

Message ID 1398687662-27080-1-git-send-email-mst@redhat.com
State New
Headers show

Commit Message

Michael S. Tsirkin April 28, 2014, 12:21 p.m. UTC
When source directory can be arrived at by two paths,
configure might misdetect an out of tree build.
The simplest way to trigger the problem is running
configure using a full path. E.g. (<firstpath> refers to qemu source
tree):
    ln -s <firstpath> <secondpath>
    cd <firstpath>
    <secondpath>/configure

A more practical way is when make runs configure automatically:

1. cd <firstpath>/; ./configure
    SRC_PATH=<firstpath>/ is written into config_host.mak
2. cd <secondpath>/; touch configure; make
    make now runs <firstpath>/configure, so configure
    assumes it's an out of tree build

When this happens configure overwrites parts of
the current tree with symlinks.

Make the test more robust: create a canary file under
the build tree, then look for it in the source tree.
If there - we know it's a source build!

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 configure | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

Comments

Eric Blake April 28, 2014, 2:52 p.m. UTC | #1
On 04/28/2014 06:21 AM, Michael S. Tsirkin wrote:
> When source directory can be arrived at by two paths,
> configure might misdetect an out of tree build.
> The simplest way to trigger the problem is running
> configure using a full path. E.g. (<firstpath> refers to qemu source
> tree):
>     ln -s <firstpath> <secondpath>
>     cd <firstpath>
>     <secondpath>/configure
> 
> A more practical way is when make runs configure automatically:

> +# running configure in the source tree? create a temporary file
> +# under pwd, check for it in source tree. Use .o suffix so that
> +# make clean will blow it away if
> +canary_path=`pwd`
> +canary_fullpath=`mktemp "$canary_path/canary.XXXXXXXXXX.o"`

mktemp is non-POSIX, and not necessarily portable.  In particular, on
FreeBSD, 'mktemp a.XXXX.b' generates the file 'a.XXXX.b', with no
randomness introduced, which therefore makes it a predictable file name
and defeats the purpose of using mktemp.

Paolo's suggestion of using ./configure as the canary seems more reliable.
Markus Armbruster April 28, 2014, 3:38 p.m. UTC | #2
Eric Blake <eblake@redhat.com> writes:

> On 04/28/2014 06:21 AM, Michael S. Tsirkin wrote:
>> When source directory can be arrived at by two paths,
>> configure might misdetect an out of tree build.
>> The simplest way to trigger the problem is running
>> configure using a full path. E.g. (<firstpath> refers to qemu source
>> tree):
>>     ln -s <firstpath> <secondpath>
>>     cd <firstpath>
>>     <secondpath>/configure
>> 
>> A more practical way is when make runs configure automatically:
>
>> +# running configure in the source tree? create a temporary file
>> +# under pwd, check for it in source tree. Use .o suffix so that
>> +# make clean will blow it away if
>> +canary_path=`pwd`
>> +canary_fullpath=`mktemp "$canary_path/canary.XXXXXXXXXX.o"`
>
> mktemp is non-POSIX, and not necessarily portable.  In particular, on
> FreeBSD, 'mktemp a.XXXX.b' generates the file 'a.XXXX.b', with no
> randomness introduced, which therefore makes it a predictable file name
> and defeats the purpose of using mktemp.
>
> Paolo's suggestion of using ./configure as the canary seems more reliable.

For what it's worth, recommended autoconf practice is to use a source
file that's probably unique to the package:

 -- Macro: AC_CONFIG_SRCDIR (UNIQUE-FILE-IN-SOURCE-DIR)
     UNIQUE-FILE-IN-SOURCE-DIR is some file that is in the package's
     source directory; `configure' checks for this file's existence to
     make sure that the directory that it is told contains the source
     code in fact does.  Occasionally people accidentally specify the
     wrong directory with `--srcdir'; this is a safety check.  *Note
     configure Invocation::, for more information.

We got a bunch starting with qemu-; suggest to pick one you like.
diff mbox

Patch

diff --git a/configure b/configure
index b08afc3..4a59cb7 100755
--- a/configure
+++ b/configure
@@ -404,6 +404,22 @@  fi
 # make source path absolute
 source_path=`cd "$source_path"; pwd`
 
+# running configure in the source tree? create a temporary file
+# under pwd, check for it in source tree. Use .o suffix so that
+# make clean will blow it away if
+canary_path=`pwd`
+canary_fullpath=`mktemp "$canary_path/canary.XXXXXXXXXX.o"`
+if test -z "$canary_fullpath"; then
+    error_exit "Current directory $canary_path/ does not appear to be writeable"
+fi
+canary_basename=`basename "$canary_fullpath"`
+if test -e "$source_path/$canary_basename"; then
+    pwd_is_source_path="y"
+else
+    pwd_is_source_path="n"
+fi
+rm -f -- "$canary_fullpath"
+
 check_define() {
 cat > $TMPC <<EOF
 #if !defined($1)
@@ -2940,7 +2956,7 @@  EOF
     fdt=yes
     dtc_internal="yes"
     mkdir -p dtc
-    if [ "$source_path" != `pwd` ] ; then
+    if [ "$pwd_is_source_path" != "y" ] ; then
        symlink "$source_path/dtc/Makefile" "dtc/Makefile"
        symlink "$source_path/dtc/scripts" "dtc/scripts"
     fi
@@ -5178,7 +5194,7 @@  do
 done
 mkdir -p $DIRS
 for f in $FILES ; do
-    if [ -e "$source_path/$f" ] && [ "$source_path" != `pwd` ]; then
+    if [ -e "$source_path/$f" ] && [ "$pwd_is_source_path" != "y" ]; then
         symlink "$source_path/$f" "$f"
     fi
 done