1# ===========================================================================
2#    https://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-detection 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 overridden 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 <https://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 30
83
84AC_DEFUN([AX_ENABLE_BUILDDIR],[
85AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
86AC_REQUIRE([AC_CANONICAL_TARGET])[]dnl
87AC_REQUIRE([AX_CONFIGURE_ARGS])[]dnl
88AC_REQUIRE([AM_AUX_DIR_EXPAND])[]dnl
89AC_BEFORE([$0],[AM_INIT_AUTOMAKE])dnl
90AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
91AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
92AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
93SUB="."
94AC_ARG_ENABLE([builddir], AS_HELP_STRING(
95  [--disable-builddir],[disable automatic build in subdir of sources])
96  ,[SUB="$enableval"], [SUB="auto"])
97if test ".$ac_srcdir_defaulted" != ".no" ; then
98if test ".$srcdir" = ".." ; then
99  if test -f config.status ; then
100    AC_MSG_NOTICE(toplevel srcdir already configured... skipping subdir build)
101  else
102    test ".$SUB" = "."  && SUB="."
103    test ".$SUB" = ".no"  && SUB="."
104    test ".$TARGET" = "." && TARGET="$target"
105    test ".$SUB" = ".auto" && SUB="m4_ifval([$1], [$1],[$TARGET])"
106    if test ".$SUB" != ".." ; then    # we know where to go and
107      AS_MKDIR_P([$SUB])
108      echo __.$SUB.__ > $SUB/conftest.tmp
109      cd $SUB
110      if grep __.$SUB.__ conftest.tmp >/dev/null 2>/dev/null ; then
111        rm conftest.tmp
112        AC_MSG_RESULT([continue configure in default builddir "./$SUB"])
113      else
114        AC_MSG_ERROR([could not change to default builddir "./$SUB"])
115      fi
116      srcdir=`echo "$SUB" |
117              sed -e 's,^\./,,;s,[[^/]]$,&/,;s,[[^/]]*/,../,g;s,[[/]]$,,;'`
118      # going to restart from subdirectory location
119      test -f $srcdir/config.log   && mv $srcdir/config.log   .
120      test -f $srcdir/confdefs.h   && mv $srcdir/confdefs.h   .
121      test -f $srcdir/conftest.log && mv $srcdir/conftest.log .
122      test -f $srcdir/$cache_file  && mv $srcdir/$cache_file  .
123      AC_MSG_RESULT(....exec $SHELL $srcdir/[$]0 "--srcdir=$srcdir" "--enable-builddir=$SUB" ${1+"[$]@"})
124      case "[$]0" in # restart
125       [[\\/]]* | ?:[[\\/]]*) # Absolute name
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 ;;
128      esac ; exit $?
129    fi
130  fi
131fi fi
132test ".$SUB" = ".auto" && SUB="."
133dnl ac_path_prog uses "set dummy" to override $@ which would defeat the "exec"
134AC_PATH_PROG(SED,gsed sed, sed)
135AUX="$am_aux_dir"
136AS_VAR_POPDEF([SED])dnl
137AS_VAR_POPDEF([AUX])dnl
138AS_VAR_POPDEF([SUB])dnl
139AC_CONFIG_COMMANDS([buildir],[dnl .............. config.status ..............
140AS_VAR_PUSHDEF([SUB],[ax_enable_builddir])dnl
141AS_VAR_PUSHDEF([TOP],[top_srcdir])dnl
142AS_VAR_PUSHDEF([SRC],[ac_top_srcdir])dnl
143AS_VAR_PUSHDEF([AUX],[ax_enable_builddir_auxdir])dnl
144AS_VAR_PUSHDEF([SED],[ax_enable_builddir_sed])dnl
145pushdef([END],[Makefile.mk])dnl
146pushdef([_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])
151    else
152      AC_MSG_NOTICE([skipping TOP/Makefile - not created])
153    fi
154  else
155    if test -f "$SRC/Makefile" ; then
156      a=`grep "^VERSION " "$SRC/Makefile"` ; b=`grep "^VERSION " Makefile`
157      test "$a" != "$b" && rm "$SRC/Makefile"
158    fi
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
166    fi
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
170/^\$/n
171x
172/^\$/bS
173x
174/\\\\\$/{H;d;}
175{H;s/.*//;x;}
176bM
177:S
178x
179/\\\\\$/{h;d;}
180{h;s/.*//;x;}
181:M
182s/\\(\\n\\)	/\\1 /g
183/^	/d
184/^[[	 ]]*[[\\#]]/d
185/^VPATH *=/d
186s/^srcdir *=.*/srcdir = ./
187s/^top_srcdir *=.*/top_srcdir = ./
188/[[:=]]/!d
189/^\\./d
190dnl Now handle rules (i.e. lines containing ":" but not " = ").
191/ = /b
192/ .= /b
193/:/!b
194s/:.*/:/
195s/ /  /g
196s/ \\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/ \\1 \\1[]_ALL\\2/g
197s/^\\([[a-z]][[a-z-]]*[[a-zA-Z0-9]]\\)\\([[ :]]\\)/\\1 \\1[]_ALL\\2/
198s/  / /g
199/^all all[]_ALL[[ :]]/i\\
200all-configured : all[]_ALL
201dnl dist-all exists... and would make for dist-all-all
202s/ [[a-zA-Z0-9-]]*[]_ALL [[a-zA-Z0-9-]]*[]_ALL[]_ALL//g
203/[]_ALL[]_ALL/d
204a\\
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" -eq "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
215dnl special rule add-on: "dist" copies the tarball to $(PUB). (source tree)
216/dist[]_ALL *:/a\\
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" -eq "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
227dnl 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" -eq "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
239dnl 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
253_EOF
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
259      fi ; xxxx="####"
260      echo "$xxxx CONFIGURATIONS FOR TOPLEVEL MAKEFILE: " >>$SRC/Makefile
261      # sanity check
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 \
265          >$SRC/Makefile~
266        (test -s $SRC/Makefile~ && mv $SRC/Makefile~ $SRC/Makefile) 2>/dev/null
267      fi
268    else
269      xxxx="\\#\\#\\#\\#"
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])
277        rm "$tmp/mkfile.tmp"
278      else
279        AC_MSG_NOTICE([reusing TOP/Makefile from earlier configure])
280        mv "$tmp/mkfile.tmp" "$SRC/Makefile"
281      fi
282    fi
283    AC_MSG_NOTICE([build in $SUB (HOST=$ax_enable_builddir_host)])
284    xxxx="####"
285    echo "$xxxx" "$ax_enable_builddir_host" "|$SUB" >>$SRC/Makefile
286  fi
287popdef([END])dnl
288AS_VAR_POPDEF([SED])dnl
289AS_VAR_POPDEF([AUX])dnl
290AS_VAR_POPDEF([SRC])dnl
291AS_VAR_POPDEF([TOP])dnl
292AS_VAR_POPDEF([SUB])dnl
293],[dnl
294ax_enable_builddir_srcdir="$srcdir"                    # $srcdir
295ax_enable_builddir_host="$HOST"                        # $HOST / $host
296ax_enable_builddir_version="$VERSION"                  # $VERSION
297ax_enable_builddir_package="$PACKAGE"                  # $PACKAGE
298ax_enable_builddir_auxdir="$ax_enable_builddir_auxdir" # $AUX
299ax_enable_builddir_sed="$ax_enable_builddir_sed"       # $SED
300ax_enable_builddir="$ax_enable_builddir"               # $SUB
301])dnl
302])
303