Home | History | Annotate | Download | only in m4
      1 # ===========================================================================
      2 #    http://www.gnu.org/software/autoconf-archive/ax_enable_builddir.html
      3 # ===========================================================================
      4 #
      5 # SYNOPSIS
      6 #
      7 #   AX_ENABLE_BUILDDIR [(dirstring-or-command [,Makefile.mk [,-all]])]
      8 #
      9 # DESCRIPTION
     10 #
     11 #   If the current configure was run within the srcdir then we move all
     12 #   configure-files into a subdir and let the configure steps continue
     13 #   there. We provide an option --disable-builddir to suppress the move into
     14 #   a separate builddir.
     15 #
     16 #   Defaults:
     17 #
     18 #     $1 = $host (overridden with $HOST)
     19 #     $2 = Makefile.mk
     20 #     $3 = -all
     21 #
     22 #   This macro must be called before AM_INIT_AUTOMAKE. It creates a default
     23 #   toplevel srcdir Makefile from the information found in the created
     24 #   toplevel builddir Makefile. It just copies the variables and
     25 #   rule-targets, each extended with a default rule-execution that recurses
     26 #   into the build directory of the current "HOST". You can override the
     27 #   auto-dection through `config.guess` and build-time of course, as in
     28 #
     29 #     make HOST=i386-mingw-cross
     30 #
     31 #   which can of course set at configure time as well using
     32 #
     33 #     configure --host=i386-mingw-cross
     34 #
     35 #   After the default has been created, additional rules can be appended
     36 #   that will not just recurse into the subdirectories and only ever exist
     37 #   in the srcdir toplevel makefile - these parts are read from the $2 =
     38 #   Makefile.mk file
     39 #
     40 #   The automatic rules are usually scanning the toplevel Makefile for lines
     41 #   like '#### $host |$builddir' to recognize the place where to recurse
     42 #   into. Usually, the last one is the only one used. However, almost all
     43 #   targets have an additional "*-all" rule which makes the script to
     44 #   recurse into _all_ variants of the current HOST (!!) setting. The "-all"
     45 #   suffix can be overriden for the macro as well.
     46 #
     47 #   a special rule is only given for things like "dist" that will copy the
     48 #   tarball from the builddir to the sourcedir (or $(PUB)) for reason of
     49 #   convenience.
     50 #
     51 # LICENSE
     52 #
     53 #   Copyright (c) 2009 Guido U. Draheim <guidod (a] gmx.de>
     54 #   Copyright (c) 2009 Alan Jenkins <alan-jenkins (a] tuffmail.co.uk>
     55 #
     56 #   This program is free software; you can redistribute it and/or modify it
     57 #   under the terms of the GNU General Public License as published by the
     58 #   Free Software Foundation; either version 3 of the License, or (at your
     59 #   option) any later version.
     60 #
     61 #   This program is distributed in the hope that it will be useful, but
     62 #   WITHOUT ANY WARRANTY; without even the implied warranty of
     63 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
     64 #   Public License for more details.
     65 #
     66 #   You should have received a copy of the GNU General Public License along
     67 #   with this program. If not, see <http://www.gnu.org/licenses/>.
     68 #
     69 #   As a special exception, the respective Autoconf Macro's copyright owner
     70 #   gives unlimited permission to copy, distribute and modify the configure
     71 #   scripts that are the output of Autoconf when processing the Macro. You
     72 #   need not follow the terms of the GNU General Public License when using
     73 #   or distributing such scripts, even though portions of the text of the
     74 #   Macro appear in them. The GNU General Public License (GPL) does govern
     75 #   all other use of the material that constitutes the Autoconf Macro.
     76 #
     77 #   This special exception to the GPL applies to versions of the Autoconf
     78 #   Macro released by the Autoconf Archive. When you make and distribute a
     79 #   modified version of the Autoconf Macro, you may extend this special
     80 #   exception to the GPL to apply to your modified version as well.
     81 
     82 #serial 23
     83 
     84 AC_DEFUN([AX_ENABLE_BUILDDIR],[
     85 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
     86 AC_REQUIRE([AX_CONFIGURE_ARGS])[]dnl
     87 AC_REQUIRE([AM_AUX_DIR_EXPAND])[]dnl
     88 AC_BEFORE([$0],[AM_INIT_AUTOMAKE])dnl
     89 AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
     90 AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
     91 AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
     92 SUB="."
     93 AC_ARG_ENABLE([builddir], AS_HELP_STRING(
     94   [--disable-builddir],[disable automatic build in subdir of sources])
     95   ,[SUB="$enableval"], [SUB="auto"])
     96 if test ".$ac_srcdir_defaulted" != ".no" ; then
     97 if test ".$srcdir" = ".." ; then
     98   if test -f config.status ; then
     99     AC_MSG_NOTICE(toplevel srcdir already configured... skipping subdir build)
    100   else
    101     test ".$SUB" = "."  && SUB="."
    102     test ".$SUB" = ".no"  && SUB="."
    103     test ".$TARGET" = "." && TARGET="$target"
    104     test ".$SUB" = ".auto" && SUB="m4_ifval([$1], [$1],[$TARGET])"
    105     if test ".$SUB" != ".." ; then    # we know where to go and
    106       AS_MKDIR_P([$SUB])
    107       echo __.$SUB.__ > $SUB/conftest.tmp
    108       cd $SUB
    109       if grep __.$SUB.__ conftest.tmp >/dev/null 2>/dev/null ; then
    110         rm conftest.tmp
    111         AC_MSG_RESULT([continue configure in default builddir "./$SUB"])
    112       else
    113         AC_MSG_ERROR([could not change to default builddir "./$SUB"])
    114       fi
    115       srcdir=`echo "$SUB" |
    116               sed -e 's,^\./,,;s,[[^/]]$,&/,;s,[[^/]]*/,../,g;s,[[/]]$,,;'`
    117       # going to restart from subdirectory location
    118       test -f $srcdir/config.log   && mv $srcdir/config.log   .
    119       test -f $srcdir/confdefs.h   && mv $srcdir/confdefs.h   .
    120       test -f $srcdir/conftest.log && mv $srcdir/conftest.log .
    121       test -f $srcdir/$cache_file  && mv $srcdir/$cache_file  .
    122       AC_MSG_RESULT(....exec $SHELL $srcdir/[$]0 "--srcdir=$srcdir" "--enable-builddir=$SUB" ${1+"[$]@"})
    123       case "[$]0" in # restart
    124        [[\\/]]* | ?:[[\\/]]*) # Asbolute name
    125          eval $SHELL "'[$]0'" "'--srcdir=$srcdir'" "'--enable-builddir=$SUB'" $ac_configure_args ;;
    126        *) eval $SHELL "'$srcdir/[$]0'" "'--srcdir=$srcdir'" "'--enable-builddir=$SUB'" $ac_configure_args ;;
    127       esac ; exit $?
    128     fi
    129   fi
    130 fi fi
    131 test ".$SUB" = ".auto" && SUB="."
    132 dnl ac_path_prog uses "set dummy" to override $@ which would defeat the "exec"
    133 AC_PATH_PROG(SED,gsed sed, sed)
    134 AUX="$am_aux_dir"
    135 AS_VAR_POPDEF([SED])dnl
    136 AS_VAR_POPDEF([AUX])dnl
    137 AS_VAR_POPDEF([SUB])dnl
    138 AC_CONFIG_COMMANDS([buildir],[dnl .............. config.status ..............
    139 AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
    140 AS_VAR_PUSHDEF([TOP],[top_srcdir])dnl
    141 AS_VAR_PUSHDEF([SRC],[ac_top_srcdir])dnl
    142 AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
    143 AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
    144 pushdef([END],[Makefile.mk])dnl
    145 pushdef([_ALL],[ifelse([$3],,[-all],[$3])])dnl
    146   SRC="$ax_enable_builddir_srcdir"
    147   if test ".$SUB" = ".." ; then
    148     if test -f "$TOP/Makefile" ; then
    149       AC_MSG_NOTICE([skipping TOP/Makefile - left untouched])
    150     else
    151       AC_MSG_NOTICE([skipping TOP/Makefile - not created])
    152     fi
    153   else
    154     if test -f "$SRC/Makefile" ; then
    155       a=`grep "^VERSION " "$SRC/Makefile"` ; b=`grep "^VERSION " Makefile`
    156       test "$a" != "$b" && rm "$SRC/Makefile"
    157     fi
    158     if test -f "$SRC/Makefile" ; then
    159 	echo "$SRC/Makefile : $SRC/Makefile.in" > $tmp/conftemp.mk
    160 	echo "	[]@ echo 'REMOVED,,,' >\$[]@" >> $tmp/conftemp.mk
    161       eval "${MAKE-make} -f $tmp/conftemp.mk 2>/dev/null >/dev/null"
    162       if grep '^REMOVED,,,' "$SRC/Makefile" >/dev/null
    163       then rm $SRC/Makefile ; fi
    164       cp $tmp/conftemp.mk $SRC/makefiles.mk~      ## DEBUGGING
    165     fi
    166     if test ! -f "$SRC/Makefile" ; then
    167       AC_MSG_NOTICE([create TOP/Makefile guessed from local Makefile])
    168       x='`' ; cat >$tmp/conftemp.sed <<_EOF
    169 /^\$/n
    170 x
    171 /^\$/bS
    172 x
    173 /\\\\\$/{H;d;}
    174 {H;s/.*//;x;}
    175 bM
    176 :S
    177 x
    178 /\\\\\$/{h;d;}
    179 {h;s/.*//;x;}
    180 :M
    181 s/\\(\\n\\)	/\\1 /g
    182 /^	/d
    183 /^[[	 ]]*[[\\#]]/d
    184 /^VPATH *=/d
    185 s/^srcdir *=.*/srcdir = ./
    186 s/^top_srcdir *=.*/top_srcdir = ./
    187 /[[:=]]/!d
    188 /^\\./d
    189 dnl Now handle rules (i.e. lines containing ":" but not " = ").
    190 / = /b
    191 / .= /b
    192 /:/!b
    193 s/:.*/:/
    194 s/ /  /g
    195 s/ \\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/ \\1 \\1[]_ALL\\2/g
    196 s/^\\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/\\1 \\1[]_ALL\\2/
    197 s/  / /g
    198 /^all all[]_ALL[[ :]]/i\\
    199 all-configured : all[]_ALL
    200 dnl dist-all exists... and would make for dist-all-all
    201 s/ [[a-zA-Z0-9-]]*[]_ALL [[a-zA-Z0-9-]]*[]_ALL[]_ALL//g
    202 /[]_ALL[]_ALL/d
    203 a\\
    204 	@ HOST="\$(HOST)\" \\\\\\
    205 	; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
    206 	; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
    207 	; use=$x basename "\$\@" _ALL $x; n=$x echo \$\$BUILD | wc -w $x \\\\\\
    208 	; echo "MAKE \$\$HOST : \$\$n * \$\@"; if test "\$\$n" -eq "0" ; then : \\\\\\
    209 	; BUILD=$x grep "^####.*|" Makefile |tail -1| sed -e 's/.*|//' $x ; fi \\\\\\
    210 	; test ".\$\$BUILD" = "." && BUILD="." \\\\\\
    211 	; test "\$\$use" = "\$\@" && BUILD=$x echo "\$\$BUILD" | tail -1 $x \\\\\\
    212 	; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
    213 	; (cd "\$\$i" && test ! -f configure && \$(MAKE) \$\$use) || exit; done
    214 dnl special rule add-on: "dist" copies the tarball to $(PUB). (source tree)
    215 /dist[]_ALL *:/a\\
    216 	@ HOST="\$(HOST)\" \\\\\\
    217 	; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
    218 	; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
    219 	; found=$x echo \$\$BUILD | wc -w $x \\\\\\
    220 	; echo "MAKE \$\$HOST : \$\$found \$(PACKAGE)-\$(VERSION).tar.*" \\\\\\
    221 	; if test "\$\$found" -eq "0" ; then : \\\\\\
    222 	; BUILD=$x grep "^#### .*|" Makefile |tail -1| sed -e 's/.*|//' $x \\\\\\
    223 	; fi ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
    224 	; for f in \$\$i/\$(PACKAGE)-\$(VERSION).tar.* \\\\\\
    225 	; do test -f "\$\$f" && mv "\$\$f" \$(PUB). ; done ; break ; done
    226 dnl special rule add-on: "dist-foo" copies all the archives to $(PUB). (source tree)
    227 /dist-[[a-zA-Z0-9]]*[]_ALL *:/a\\
    228 	@ HOST="\$(HOST)\" \\\\\\
    229 	; test ".\$\$HOST" = "." && HOST=$x sh ./config.guess $x \\\\\\
    230 	; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
    231 	; found=$x echo \$\$BUILD | wc -w $x \\\\\\
    232 	; echo "MAKE \$\$HOST : \$\$found \$(PACKAGE)-\$(VERSION).*" \\\\\\
    233 	; if test "\$\$found" -eq "0" ; then : \\\\\\
    234 	; BUILD=$x grep "^#### .*|" Makefile |tail -1| sed -e 's/.*|//' $x \\\\\\
    235 	; fi ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
    236 	; for f in \$\$i/\$(PACKAGE)-\$(VERSION).* \\\\\\
    237 	; do test -f "\$\$f" && mv "\$\$f" \$(PUB). ; done ; break ; done
    238 dnl special rule add-on: "distclean" removes all local builddirs completely
    239 /distclean[]_ALL *:/a\\
    240 	@ HOST="\$(HOST)\" \\\\\\
    241 	; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
    242 	; BUILD=$x grep "^#### .*|" Makefile | sed -e 's/.*|//' $x \\\\\\
    243 	; use=$x basename "\$\@" _ALL $x; n=$x echo \$\$BUILD | wc -w $x \\\\\\
    244 	; echo "MAKE \$\$HOST : \$\$n * \$\@ (all local builds)" \\\\\\
    245 	; test ".\$\$BUILD" = "." && BUILD="." \\\\\\
    246 	; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
    247 	; echo "# rm -r \$\$i"; done ; echo "# (sleep 3)" ; sleep 3 \\\\\\
    248 	; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
    249 	; echo "\$\$i" | grep "^/" > /dev/null && continue \\\\\\
    250 	; echo "\$\$i" | grep "^../" > /dev/null && continue \\\\\\
    251 	; echo "rm -r \$\$i"; (rm -r "\$\$i") ; done ; rm Makefile
    252 _EOF
    253       cp "$tmp/conftemp.sed" "$SRC/makefile.sed~"            ## DEBUGGING
    254       $SED -f $tmp/conftemp.sed Makefile >$SRC/Makefile
    255       if test -f "$SRC/m4_ifval([$2],[$2],[END])" ; then
    256         AC_MSG_NOTICE([extend TOP/Makefile with TOP/m4_ifval([$2],[$2],[END])])
    257         cat $SRC/END >>$SRC/Makefile
    258       fi ; xxxx="####"
    259       echo "$xxxx CONFIGURATIONS FOR TOPLEVEL MAKEFILE: " >>$SRC/Makefile
    260       # sanity check
    261       if grep '^; echo "MAKE ' $SRC/Makefile >/dev/null ; then
    262         AC_MSG_NOTICE([buggy sed found - it deletes tab in "a" text parts])
    263         $SED -e '/^@ HOST=/s/^/	/' -e '/^; /s/^/	/' $SRC/Makefile \
    264           >$SRC/Makefile~
    265         (test -s $SRC/Makefile~ && mv $SRC/Makefile~ $SRC/Makefile) 2>/dev/null
    266       fi
    267     else
    268       xxxx="\\#\\#\\#\\#"
    269       # echo "/^$xxxx *$ax_enable_builddir_host /d" >$tmp/conftemp.sed
    270       echo "s!^$xxxx [[^|]]* | *$SUB *\$!$xxxx ...... $SUB!" >$tmp/conftemp.sed
    271       $SED -f "$tmp/conftemp.sed" "$SRC/Makefile" >$tmp/mkfile.tmp
    272         cp "$tmp/conftemp.sed" "$SRC/makefiles.sed~"         ## DEBUGGING
    273         cp "$tmp/mkfile.tmp"   "$SRC/makefiles.out~"         ## DEBUGGING
    274       if cmp -s "$SRC/Makefile" "$tmp/mkfile.tmp" 2>/dev/null ; then
    275         AC_MSG_NOTICE([keeping TOP/Makefile from earlier configure])
    276         rm "$tmp/mkfile.tmp"
    277       else
    278         AC_MSG_NOTICE([reusing TOP/Makefile from earlier configure])
    279         mv "$tmp/mkfile.tmp" "$SRC/Makefile"
    280       fi
    281     fi
    282     AC_MSG_NOTICE([build in $SUB (HOST=$ax_enable_builddir_host)])
    283     xxxx="####"
    284     echo "$xxxx" "$ax_enable_builddir_host" "|$SUB" >>$SRC/Makefile
    285   fi
    286 popdef([END])dnl
    287 AS_VAR_POPDEF([SED])dnl
    288 AS_VAR_POPDEF([AUX])dnl
    289 AS_VAR_POPDEF([SRC])dnl
    290 AS_VAR_POPDEF([TOP])dnl
    291 AS_VAR_POPDEF([SUB])dnl
    292 ],[dnl
    293 ax_enable_builddir_srcdir="$srcdir"                    # $srcdir
    294 ax_enable_builddir_host="$HOST"                        # $HOST / $host
    295 ax_enable_builddir_version="$VERSION"                  # $VERSION
    296 ax_enable_builddir_package="$PACKAGE"                  # $PACKAGE
    297 ax_enable_builddir_auxdir="$ax_enable_builddir_auxdir" # $AUX
    298 ax_enable_builddir_sed="$ax_enable_builddir_sed"       # $SED
    299 ax_enable_builddir="$ax_enable_builddir"               # $SUB
    300 ])dnl
    301 ])
    302