1 # ===========================================================================
2 # http://autoconf-archive.cryp.to/ax_enable_builddir.html
3 # ===========================================================================
7 # AX_ENABLE_BUILDDIR [(dirstring-or-command [,Makefile.mk [,-all]])]
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.
18 # $1 = $host (overridden with $HOST)
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
29 # make HOST=i386-mingw-cross
31 # which can of course set at configure time as well using
33 # configure --host=i386-mingw-cross
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 =
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.
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
57 # Copyright (c) 2009 Guido U. Draheim <guidod@gmx.de>
58 # Copyright (c) 2009 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
60 # This program is free software; you can redistribute it and/or modify it
61 # under the terms of the GNU General Public License as published by the
62 # Free Software Foundation; either version 2 of the License, or (at your
63 # option) any later version.
65 # This program is distributed in the hope that it will be useful, but
66 # WITHOUT ANY WARRANTY; without even the implied warranty of
67 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
68 # Public License for more details.
70 # You should have received a copy of the GNU General Public License along
71 # with this program. If not, see <http://www.gnu.org/licenses/>.
73 # As a special exception, the respective Autoconf Macro's copyright owner
74 # gives unlimited permission to copy, distribute and modify the configure
75 # scripts that are the output of Autoconf when processing the Macro. You
76 # need not follow the terms of the GNU General Public License when using
77 # or distributing such scripts, even though portions of the text of the
78 # Macro appear in them. The GNU General Public License (GPL) does govern
79 # all other use of the material that constitutes the Autoconf Macro.
81 # This special exception to the GPL applies to versions of the Autoconf
82 # Macro released by the Autoconf Macro Archive. When you make and
83 # distribute a modified version of the Autoconf Macro, you may extend this
84 # special exception to the GPL to apply to your modified version as well.
86 AC_DEFUN([AX_ENABLE_BUILDDIR],[
87 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
88 AC_REQUIRE([AX_CONFIGURE_ARGS])[]dnl
89 AC_REQUIRE([AM_AUX_DIR_EXPAND])[]dnl
90 AC_BEFORE([$0],[AM_INIT_AUTOMAKE])dnl
91 AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
92 AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
93 AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
95 AC_ARG_ENABLE([builddir], AC_HELP_STRING(
96 [--disable-builddir],[disable automatic build in subdir of sources])
97 ,[SUB="$enableval"], [SUB="auto"])
98 if test ".$ac_srcdir_defaulted" != ".no" ; then
99 if test ".$srcdir" = ".." ; then
100 if test -f config.status ; then
101 AC_MSG_NOTICE(toplevel srcdir already configured... skipping subdir build)
103 test ".$SUB" = "." && SUB="."
104 test ".$SUB" = ".no" && SUB="."
105 test ".$TARGET" = "." && TARGET="$target"
106 test ".$SUB" = ".auto" && SUB="m4_ifval([$1], [$1],[$TARGET])"
107 if test ".$SUB" != ".." ; then # we know where to go and
109 echo __.$SUB.__ > $SUB/conftest.tmp
111 if grep __.$SUB.__ conftest.tmp >/dev/null 2>/dev/null ; then
113 AC_MSG_RESULT([continue configure in default builddir "./$SUB"])
115 AC_MSG_ERROR([could not change to default builddir "./$SUB"])
117 srcdir=`echo "$SUB" |
118 sed -e 's,^\./,,;s,[[^/]]$,&/,;s,[[^/]]*/,../,g;s,[[/]]$,,;'`
119 # going to restart from subdirectory location
120 test -f $srcdir/config.log && mv $srcdir/config.log .
121 test -f $srcdir/confdefs.h && mv $srcdir/confdefs.h .
122 test -f $srcdir/conftest.log && mv $srcdir/conftest.log .
123 test -f $srcdir/$cache_file && mv $srcdir/$cache_file .
124 AC_MSG_RESULT(....exec $SHELL $srcdir/[$]0 "--srcdir=$srcdir" "--enable-builddir=$SUB" ${1+"[$]@"})
125 case "[$]0" in # restart
126 [/\\]*) eval $SHELL "'[$]0'" "'--srcdir=$srcdir'" "'--enable-builddir=$SUB'" $ac_configure_args ;;
127 *) eval $SHELL "'$srcdir/[$]0'" "'--srcdir=$srcdir'" "'--enable-builddir=$SUB'" $ac_configure_args ;;
132 test ".$SUB" = ".auto" && SUB="."
133 dnl ac_path_prog uses "set dummy" to override $@ which would defeat the "exec"
134 AC_PATH_PROG(SED,gsed sed, sed)
136 AS_VAR_POPDEF([SED])dnl
137 AS_VAR_POPDEF([AUX])dnl
138 AS_VAR_POPDEF([SUB])dnl
139 AC_CONFIG_COMMANDS([buildir],[dnl .............. config.status ..............
140 AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
141 AS_VAR_PUSHDEF([TOP],[top_srcdir])dnl
142 AS_VAR_PUSHDEF([SRC],[ac_top_srcdir])dnl
143 AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
144 AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
145 pushdef([END],[Makefile.mk])dnl
146 pushdef([_ALL],[ifelse([$3],,[-all],[$3])])dnl
147 SRC="$ax_enable_builddir_srcdir"
148 if test ".$SUB" = ".." ; then
149 if test -f "$TOP/Makefile" ; then
150 AC_MSG_NOTICE([skipping TOP/Makefile - left untouched])
152 AC_MSG_NOTICE([skipping TOP/Makefile - not created])
155 if test -f "$SRC/Makefile" ; then
156 a=`grep "^VERSION " "$SRC/Makefile"` ; b=`grep "^VERSION " Makefile`
157 test "$a" != "$b" && rm "$SRC/Makefile"
159 if test -f "$SRC/Makefile" ; then
160 echo "$SRC/Makefile : $SRC/Makefile.in" > $tmp/conftemp.mk
161 echo " []@ echo 'REMOVED,,,' >\$[]@" >> $tmp/conftemp.mk
162 eval "${MAKE-make} -f $tmp/conftemp.mk 2>/dev/null >/dev/null"
163 if grep '^REMOVED,,,' "$SRC/Makefile" >/dev/null
164 then rm $SRC/Makefile ; fi
165 cp $tmp/conftemp.mk $SRC/makefiles.mk~ ## DEBUGGING
167 if test ! -f "$SRC/Makefile" ; then
168 AC_MSG_NOTICE([create TOP/Makefile guessed from local Makefile])
169 x='`' ; cat >$tmp/conftemp.sed <<_EOF
186 s/^srcdir *=.*/srcdir = ./
187 s/^top_srcdir *=.*/top_srcdir = ./
190 dnl Now handle rules (i.e. lines containing ":" but not " = ").
196 s/ \\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/ \\1 \\1[]_ALL\\2/g
197 s/^\\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/\\1 \\1[]_ALL\\2/
199 /^all all[]_ALL[[ :]]/i\\
200 all-configured : all[]_ALL
201 dnl dist-all exists... and would make for dist-all-all
202 s/ [[a-zA-Z0-9-]]*[]_ALL [[a-zA-Z0-9-]]*[]_ALL[]_ALL//g
205 @ HOST="\$(HOST)\" \\\\\\
206 ; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
207 ; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
208 ; use=$x basename "\$\@" _ALL $x; n=$x echo \$\$BUILD | wc -w $x \\\\\\
209 ; echo "MAKE \$\$HOST : \$\$n * \$\@"; if test "\$\$n" = "0" ; then : \\\\\\
210 ; BUILD=$x grep "^####.*|" Makefile |tail -1| sed -e 's/.*|//' $x ; fi \\\\\\
211 ; test ".\$\$BUILD" = "." && BUILD="." \\\\\\
212 ; test "\$\$use" = "\$\@" && BUILD=$x echo "\$\$BUILD" | tail -1 $x \\\\\\
213 ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
214 ; (cd "\$\$i" && test ! -f configure && \$(MAKE) \$\$use) || exit; done
215 dnl special rule add-on: "dist" copies the tarball to $(PUB). (source tree)
217 @ HOST="\$(HOST)\" \\\\\\
218 ; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
219 ; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
220 ; found=$x echo \$\$BUILD | wc -w $x \\\\\\
221 ; echo "MAKE \$\$HOST : \$\$found \$(PACKAGE)-\$(VERSION).tar.*" \\\\\\
222 ; if test "\$\$found" = "0" ; then : \\\\\\
223 ; BUILD=$x grep "^#### .*|" Makefile |tail -1| sed -e 's/.*|//' $x \\\\\\
224 ; fi ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
225 ; for f in \$\$i/\$(PACKAGE)-\$(VERSION).tar.* \\\\\\
226 ; do test -f "\$\$f" && mv "\$\$f" \$(PUB). ; done ; break ; done
227 dnl special rule add-on: "dist-foo" copies all the archives to $(PUB). (source tree)
228 /dist-[[a-zA-Z0-9]]*[]_ALL *:/a\\
229 @ HOST="\$(HOST)\" \\\\\\
230 ; test ".\$\$HOST" = "." && HOST=$x sh ./config.guess $x \\\\\\
231 ; BUILD=$x grep "^#### \$\$HOST " Makefile | sed -e 's/.*|//' $x \\\\\\
232 ; found=$x echo \$\$BUILD | wc -w $x \\\\\\
233 ; echo "MAKE \$\$HOST : \$\$found \$(PACKAGE)-\$(VERSION).*" \\\\\\
234 ; if test "\$\$found" = "0" ; then : \\\\\\
235 ; BUILD=$x grep "^#### .*|" Makefile |tail -1| sed -e 's/.*|//' $x \\\\\\
236 ; fi ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
237 ; for f in \$\$i/\$(PACKAGE)-\$(VERSION).* \\\\\\
238 ; do test -f "\$\$f" && mv "\$\$f" \$(PUB). ; done ; break ; done
239 dnl special rule add-on: "distclean" removes all local builddirs completely
240 /distclean[]_ALL *:/a\\
241 @ HOST="\$(HOST)\" \\\\\\
242 ; test ".\$\$HOST" = "." && HOST=$x sh $AUX/config.guess $x \\\\\\
243 ; BUILD=$x grep "^#### .*|" Makefile | sed -e 's/.*|//' $x \\\\\\
244 ; use=$x basename "\$\@" _ALL $x; n=$x echo \$\$BUILD | wc -w $x \\\\\\
245 ; echo "MAKE \$\$HOST : \$\$n * \$\@ (all local builds)" \\\\\\
246 ; test ".\$\$BUILD" = "." && BUILD="." \\\\\\
247 ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
248 ; echo "# rm -r \$\$i"; done ; echo "# (sleep 3)" ; sleep 3 \\\\\\
249 ; for i in \$\$BUILD ; do test ".\$\$i" = "." && continue \\\\\\
250 ; echo "\$\$i" | grep "^/" > /dev/null && continue \\\\\\
251 ; echo "\$\$i" | grep "^../" > /dev/null && continue \\\\\\
252 ; echo "rm -r \$\$i"; (rm -r "\$\$i") ; done ; rm Makefile
254 cp "$tmp/conftemp.sed" "$SRC/makefile.sed~" ## DEBUGGING
255 $SED -f $tmp/conftemp.sed Makefile >$SRC/Makefile
256 if test -f "$SRC/m4_ifval([$2],[$2],[END])" ; then
257 AC_MSG_NOTICE([extend TOP/Makefile with TOP/m4_ifval([$2],[$2],[END])])
258 cat $SRC/END >>$SRC/Makefile
260 echo "$xxxx CONFIGURATIONS FOR TOPLEVEL MAKEFILE: " >>$SRC/Makefile
262 if grep '^; echo "MAKE ' $SRC/Makefile >/dev/null ; then
263 AC_MSG_NOTICE([buggy sed found - it deletes tab in "a" text parts])
264 $SED -e '/^@ HOST=/s/^/ /' -e '/^; /s/^/ /' $SRC/Makefile \
266 (test -s $SRC/Makefile~ && mv $SRC/Makefile~ $SRC/Makefile) 2>/dev/null
270 # echo "/^$xxxx *$ax_enable_builddir_host /d" >$tmp/conftemp.sed
271 echo "s!^$xxxx [[^|]]* | *$SUB *\$!$xxxx ...... $SUB!" >$tmp/conftemp.sed
272 $SED -f "$tmp/conftemp.sed" "$SRC/Makefile" >$tmp/mkfile.tmp
273 cp "$tmp/conftemp.sed" "$SRC/makefiles.sed~" ## DEBUGGING
274 cp "$tmp/mkfile.tmp" "$SRC/makefiles.out~" ## DEBUGGING
275 if cmp -s "$SRC/Makefile" "$tmp/mkfile.tmp" 2>/dev/null ; then
276 AC_MSG_NOTICE([keeping TOP/Makefile from earlier configure])
279 AC_MSG_NOTICE([reusing TOP/Makefile from earlier configure])
280 mv "$tmp/mkfile.tmp" "$SRC/Makefile"
283 AC_MSG_NOTICE([build in $SUB (HOST=$ax_enable_builddir_host)])
285 echo "$xxxx" "$ax_enable_builddir_host" "|$SUB" >>$SRC/Makefile
288 AS_VAR_POPDEF([SED])dnl
289 AS_VAR_POPDEF([AUX])dnl
290 AS_VAR_POPDEF([SRC])dnl
291 AS_VAR_POPDEF([TOP])dnl
292 AS_VAR_POPDEF([SUB])dnl
294 ax_enable_builddir_srcdir="$srcdir" # $srcdir
295 ax_enable_builddir_host="$HOST" # $HOST / $host
296 ax_enable_builddir_version="$VERSION" # $VERSION
297 ax_enable_builddir_package="$PACKAGE" # $PACKAGE
298 ax_enable_builddir_auxdir="$ax_enable_builddir_auxdir" # $AUX
299 ax_enable_builddir_sed="$ax_enable_builddir_sed" # $SED
300 ax_enable_builddir="$ax_enable_builddir" # $SUB