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@gmx.de>
54#   Copyright (c) 2009 Alan Jenkins <alan-jenkins@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
84AC_DEFUN([AX_ENABLE_BUILDDIR],[
85AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
86AC_REQUIRE([AX_CONFIGURE_ARGS])[]dnl
87AC_REQUIRE([AM_AUX_DIR_EXPAND])[]dnl
88AC_BEFORE([$0],[AM_INIT_AUTOMAKE])dnl
89AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
90AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
91AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
92SUB="."
93AC_ARG_ENABLE([builddir], AS_HELP_STRING(
94  [--disable-builddir],[disable automatic build in subdir of sources])
95  ,[SUB="$enableval"], [SUB="auto"])
96if test ".$ac_srcdir_defaulted" != ".no" ; then
97if 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       [/\\]*) eval $SHELL "'[$]0'" "'--srcdir=$srcdir'" "'--enable-builddir=$SUB'" $ac_configure_args ;;
125       *) eval $SHELL "'$srcdir/[$]0'" "'--srcdir=$srcdir'" "'--enable-builddir=$SUB'" $ac_configure_args ;;
126      esac ; exit $?
127    fi
128  fi
129fi fi
130test ".$SUB" = ".auto" && SUB="."
131dnl ac_path_prog uses "set dummy" to override $@ which would defeat the "exec"
132AC_PATH_PROG(SED,gsed sed, sed)
133AUX="$am_aux_dir"
134AS_VAR_POPDEF([SED])dnl
135AS_VAR_POPDEF([AUX])dnl
136AS_VAR_POPDEF([SUB])dnl
137AC_CONFIG_COMMANDS([buildir],[dnl .............. config.status ..............
138AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
139AS_VAR_PUSHDEF([TOP],[top_srcdir])dnl
140AS_VAR_PUSHDEF([SRC],[ac_top_srcdir])dnl
141AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
142AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
143pushdef([END],[Makefile.mk])dnl
144pushdef([_ALL],[ifelse([$3],,[-all],[$3])])dnl
145  SRC="$ax_enable_builddir_srcdir"
146  if test ".$SUB" = ".." ; then
147    if test -f "$TOP/Makefile" ; then
148      AC_MSG_NOTICE([skipping TOP/Makefile - left untouched])
149    else
150      AC_MSG_NOTICE([skipping TOP/Makefile - not created])
151    fi
152  else
153    if test -f "$SRC/Makefile" ; then
154      a=`grep "^VERSION " "$SRC/Makefile"` ; b=`grep "^VERSION " Makefile`
155      test "$a" != "$b" && rm "$SRC/Makefile"
156    fi
157    if test -f "$SRC/Makefile" ; then
158	echo "$SRC/Makefile : $SRC/Makefile.in" > $tmp/conftemp.mk
159	echo "	[]@ echo 'REMOVED,,,' >\$[]@" >> $tmp/conftemp.mk
160      eval "${MAKE-make} -f $tmp/conftemp.mk 2>/dev/null >/dev/null"
161      if grep '^REMOVED,,,' "$SRC/Makefile" >/dev/null
162      then rm $SRC/Makefile ; fi
163      cp $tmp/conftemp.mk $SRC/makefiles.mk~      ## DEBUGGING
164    fi
165    if test ! -f "$SRC/Makefile" ; then
166      AC_MSG_NOTICE([create TOP/Makefile guessed from local Makefile])
167      x='`' ; cat >$tmp/conftemp.sed <<_EOF
168/^\$/n
169x
170/^\$/bS
171x
172/\\\\\$/{H;d;}
173{H;s/.*//;x;}
174bM
175:S
176x
177/\\\\\$/{h;d;}
178{h;s/.*//;x;}
179:M
180s/\\(\\n\\)	/\\1 /g
181/^	/d
182/^[[	 ]]*[[\\#]]/d
183/^VPATH *=/d
184s/^srcdir *=.*/srcdir = ./
185s/^top_srcdir *=.*/top_srcdir = ./
186/[[:=]]/!d
187/^\\./d
188dnl Now handle rules (i.e. lines containing ":" but not " = ").
189/ = /b
190/ .= /b
191/:/!b
192s/:.*/:/
193s/ /  /g
194s/ \\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/ \\1 \\1[]_ALL\\2/g
195s/^\\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/\\1 \\1[]_ALL\\2/
196s/  / /g
197/^all all[]_ALL[[ :]]/i\\
198all-configured : all[]_ALL
199dnl dist-all exists... and would make for dist-all-all
200s/ [[a-zA-Z0-9-]]*[]_ALL [[a-zA-Z0-9-]]*[]_ALL[]_ALL//g
201/[]_ALL[]_ALL/d
202a\\
203	@ HOST="\$(HOST)\" \\\\\\
204	; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
205	; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
206	; use=$x basename "\$\@" _ALL $x; n=$x echo \$\$BUILD | wc -w $x \\\\\\
207	; echo "MAKE \$\$HOST : \$\$n * \$\@"; if test "\$\$n" -eq "0" ; then : \\\\\\
208	; BUILD=$x grep "^####.*|" Makefile |tail -1| sed -e 's/.*|//' $x ; fi \\\\\\
209	; test ".\$\$BUILD" = "." && BUILD="." \\\\\\
210	; test "\$\$use" = "\$\@" && BUILD=$x echo "\$\$BUILD" | tail -1 $x \\\\\\
211	; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
212	; (cd "\$\$i" && test ! -f configure && \$(MAKE) \$\$use) || exit; done
213dnl special rule add-on: "dist" copies the tarball to $(PUB). (source tree)
214/dist[]_ALL *:/a\\
215	@ HOST="\$(HOST)\" \\\\\\
216	; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
217	; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
218	; found=$x echo \$\$BUILD | wc -w $x \\\\\\
219	; echo "MAKE \$\$HOST : \$\$found \$(PACKAGE)-\$(VERSION).tar.*" \\\\\\
220	; if test "\$\$found" -eq "0" ; then : \\\\\\
221	; BUILD=$x grep "^#### .*|" Makefile |tail -1| sed -e 's/.*|//' $x \\\\\\
222	; fi ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
223	; for f in \$\$i/\$(PACKAGE)-\$(VERSION).tar.* \\\\\\
224	; do test -f "\$\$f" && mv "\$\$f" \$(PUB). ; done ; break ; done
225dnl special rule add-on: "dist-foo" copies all the archives to $(PUB). (source tree)
226/dist-[[a-zA-Z0-9]]*[]_ALL *:/a\\
227	@ HOST="\$(HOST)\" \\\\\\
228	; test ".\$\$HOST" = "." && HOST=$x sh ./config.guess $x \\\\\\
229	; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
230	; found=$x echo \$\$BUILD | wc -w $x \\\\\\
231	; echo "MAKE \$\$HOST : \$\$found \$(PACKAGE)-\$(VERSION).*" \\\\\\
232	; if test "\$\$found" -eq "0" ; then : \\\\\\
233	; BUILD=$x grep "^#### .*|" Makefile |tail -1| sed -e 's/.*|//' $x \\\\\\
234	; fi ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
235	; for f in \$\$i/\$(PACKAGE)-\$(VERSION).* \\\\\\
236	; do test -f "\$\$f" && mv "\$\$f" \$(PUB). ; done ; break ; done
237dnl special rule add-on: "distclean" removes all local builddirs completely
238/distclean[]_ALL *:/a\\
239	@ HOST="\$(HOST)\" \\\\\\
240	; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
241	; BUILD=$x grep "^#### .*|" Makefile | sed -e 's/.*|//' $x \\\\\\
242	; use=$x basename "\$\@" _ALL $x; n=$x echo \$\$BUILD | wc -w $x \\\\\\
243	; echo "MAKE \$\$HOST : \$\$n * \$\@ (all local builds)" \\\\\\
244	; test ".\$\$BUILD" = "." && BUILD="." \\\\\\
245	; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
246	; echo "# rm -r \$\$i"; done ; echo "# (sleep 3)" ; sleep 3 \\\\\\
247	; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
248	; echo "\$\$i" | grep "^/" > /dev/null && continue \\\\\\
249	; echo "\$\$i" | grep "^../" > /dev/null && continue \\\\\\
250	; echo "rm -r \$\$i"; (rm -r "\$\$i") ; done ; rm Makefile
251_EOF
252      cp "$tmp/conftemp.sed" "$SRC/makefile.sed~"            ## DEBUGGING
253      $SED -f $tmp/conftemp.sed Makefile >$SRC/Makefile
254      if test -f "$SRC/m4_ifval([$2],[$2],[END])" ; then
255        AC_MSG_NOTICE([extend TOP/Makefile with TOP/m4_ifval([$2],[$2],[END])])
256        cat $SRC/END >>$SRC/Makefile
257      fi ; xxxx="####"
258      echo "$xxxx CONFIGURATIONS FOR TOPLEVEL MAKEFILE: " >>$SRC/Makefile
259      # sanity check
260      if grep '^; echo "MAKE ' $SRC/Makefile >/dev/null ; then
261        AC_MSG_NOTICE([buggy sed found - it deletes tab in "a" text parts])
262        $SED -e '/^@ HOST=/s/^/	/' -e '/^; /s/^/	/' $SRC/Makefile \
263          >$SRC/Makefile~
264        (test -s $SRC/Makefile~ && mv $SRC/Makefile~ $SRC/Makefile) 2>/dev/null
265      fi
266    else
267      xxxx="\\#\\#\\#\\#"
268      # echo "/^$xxxx *$ax_enable_builddir_host /d" >$tmp/conftemp.sed
269      echo "s!^$xxxx [[^|]]* | *$SUB *\$!$xxxx ...... $SUB!" >$tmp/conftemp.sed
270      $SED -f "$tmp/conftemp.sed" "$SRC/Makefile" >$tmp/mkfile.tmp
271        cp "$tmp/conftemp.sed" "$SRC/makefiles.sed~"         ## DEBUGGING
272        cp "$tmp/mkfile.tmp"   "$SRC/makefiles.out~"         ## DEBUGGING
273      if cmp -s "$SRC/Makefile" "$tmp/mkfile.tmp" 2>/dev/null ; then
274        AC_MSG_NOTICE([keeping TOP/Makefile from earlier configure])
275        rm "$tmp/mkfile.tmp"
276      else
277        AC_MSG_NOTICE([reusing TOP/Makefile from earlier configure])
278        mv "$tmp/mkfile.tmp" "$SRC/Makefile"
279      fi
280    fi
281    AC_MSG_NOTICE([build in $SUB (HOST=$ax_enable_builddir_host)])
282    xxxx="####"
283    echo "$xxxx" "$ax_enable_builddir_host" "|$SUB" >>$SRC/Makefile
284  fi
285popdef([END])dnl
286AS_VAR_POPDEF([SED])dnl
287AS_VAR_POPDEF([AUX])dnl
288AS_VAR_POPDEF([SRC])dnl
289AS_VAR_POPDEF([TOP])dnl
290AS_VAR_POPDEF([SUB])dnl
291],[dnl
292ax_enable_builddir_srcdir="$srcdir"                    # $srcdir
293ax_enable_builddir_host="$HOST"                        # $HOST / $host
294ax_enable_builddir_version="$VERSION"                  # $VERSION
295ax_enable_builddir_package="$PACKAGE"                  # $PACKAGE
296ax_enable_builddir_auxdir="$ax_enable_builddir_auxdir" # $AUX
297ax_enable_builddir_sed="$ax_enable_builddir_sed"       # $SED
298ax_enable_builddir="$ax_enable_builddir"               # $SUB
299])dnl
300])
301