1 #---------------------------------------------------------------------------
5 # Copyright (c) 2013 Daniel Stenberg <daniel@haxx.se>
7 # Permission to use, copy, modify, and distribute this software for any
8 # purpose with or without fee is hereby granted, provided that the above
9 # copyright notice and this permission notice appear in all copies.
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #---------------------------------------------------------------------------
25 dnl -------------------------------------------------
28 dnl This macro performs embedding of automake initialization
29 dnl code into configure script. When automake version 1.14 or
30 dnl newer is used at configure script generation time, this
31 dnl results in 'subdir-objects' automake option being used.
32 dnl When using automake versions older than 1.14 this option
33 dnl is not used when generating configure script.
35 dnl Existence of automake _AM_PROG_CC_C_O m4 private macro
36 dnl is used to differentiate automake version 1.14 from older
37 dnl ones which lack this macro.
39 m4_define([_XC_AUTOMAKE_BODY],
41 ## --------------------------------------- ##
42 ## Start of automake initialization code ##
43 ## --------------------------------------- ##
44 m4_ifdef([_AM_PROG_CC_C_O],
46 AM_INIT_AUTOMAKE([subdir-objects])
50 ## ------------------------------------- ##
51 ## End of automake initialization code ##
52 ## ------------------------------------- ##
54 m4_define([$0], [])[]dnl
59 dnl -------------------------------------------------
62 dnl This macro embeds automake machinery into configure
63 dnl script regardless of automake version used in order
64 dnl to generate configure script.
66 dnl When using automake version 1.14 or newer, automake
67 dnl initialization option 'subdir-objects' is used to
68 dnl generate the configure script, otherwise this option
71 AC_DEFUN([XC_AUTOMAKE],
75 AC_BEFORE([$0],[AM_INIT_AUTOMAKE])dnl
79 m4_ifdef([AM_INIT_AUTOMAKE],
80 [m4_undefine([AM_INIT_AUTOMAKE])])dnl
82 m4_define([$0], [])[]dnl
86 dnl _XC_AMEND_DISTCLEAN_BODY ([LIST-OF-SUBDIRS])
87 dnl -------------------------------------------------
90 dnl This macro performs shell code embedding into
91 dnl configure script in order to modify distclean
92 dnl and maintainer-clean targets of makefiles which
93 dnl are located in given list of subdirs.
95 dnl See XC_AMEND_DISTCLEAN comments for details.
97 m4_define([_XC_AMEND_DISTCLEAN_BODY],
99 ## ---------------------------------- ##
100 ## Start of distclean amending code ##
101 ## ---------------------------------- ##
103 for xc_subdir in [$1]
106 if test ! -f "$xc_subdir/Makefile"; then
107 echo "$xc_msg_err $xc_subdir/Makefile file not found. $xc_msg_abrt" >&2
111 # Fetch dependency tracking file list from Makefile include lines.
113 xc_inc_lines=`grep '^include .*(DEPDIR)' "$xc_subdir/Makefile" 2>/dev/null`
114 xc_cnt_words=`echo "$xc_inc_lines" | wc -w | tr -d "$xc_space$xc_tab"`
116 # --disable-dependency-tracking might have been used, consequently
117 # there is nothing to amend without a dependency tracking file list.
119 if test $xc_cnt_words -gt 0; then
121 AC_MSG_NOTICE([amending $xc_subdir/Makefile])
123 # Build Makefile specific patch hunk.
125 xc_p="$xc_subdir/xc_patch.tmp"
127 xc_rm_depfiles=`echo "$xc_inc_lines" \
128 | $SED 's%include% -rm -f%' 2>/dev/null`
130 xc_dep_subdirs=`echo "$xc_inc_lines" \
131 | $SED 's%include[[ ]][[ ]]*%%' 2>/dev/null \
132 | $SED 's%(DEPDIR)/.*%(DEPDIR)%' 2>/dev/null \
135 echo "$xc_rm_depfiles" >$xc_p
137 for xc_dep_dir in $xc_dep_subdirs; do
138 echo "${xc_tab}@xm_dep_cnt=\`ls $xc_dep_dir | wc -l 2>/dev/null\`; \\" >>$xc_p
139 echo "${xc_tab}if test \$\$xm_dep_cnt -eq 0 && test -d $xc_dep_dir; then \\" >>$xc_p
140 echo "${xc_tab} rm -rf $xc_dep_dir; \\" >>$xc_p
141 echo "${xc_tab}fi" >>$xc_p
144 # Build Makefile patching sed scripts.
146 xc_s1="$xc_subdir/xc_script_1.tmp"
147 xc_s2="$xc_subdir/xc_script_2.tmp"
148 xc_s3="$xc_subdir/xc_script_3.tmp"
151 /^distclean[[ ]]*:/,/^[[^ ]][[^ ]]*:/{
152 s/^.*(DEPDIR)/___xc_depdir_line___/
154 /^maintainer-clean[[ ]]*:/,/^[[^ ]][[^ ]]*:/{
155 s/^.*(DEPDIR)/___xc_depdir_line___/
160 /___xc_depdir_line___$/{
162 /___xc_depdir_line___$/D
167 /^___xc_depdir_line___/{
173 # Apply patch to Makefile and cleanup.
175 $SED -f "$xc_s1" "$xc_subdir/Makefile" >"$xc_subdir/Makefile.tmp1"
176 $SED -f "$xc_s2" "$xc_subdir/Makefile.tmp1" >"$xc_subdir/Makefile.tmp2"
177 $SED -f "$xc_s3" "$xc_subdir/Makefile.tmp2" >"$xc_subdir/Makefile.tmp3"
179 if test -f "$xc_subdir/Makefile.tmp3"; then
180 mv -f "$xc_subdir/Makefile.tmp3" "$xc_subdir/Makefile"
183 test -f "$xc_subdir/Makefile.tmp1" && rm -f "$xc_subdir/Makefile.tmp1"
184 test -f "$xc_subdir/Makefile.tmp2" && rm -f "$xc_subdir/Makefile.tmp2"
185 test -f "$xc_subdir/Makefile.tmp3" && rm -f "$xc_subdir/Makefile.tmp3"
187 test -f "$xc_p" && rm -f "$xc_p"
188 test -f "$xc_s1" && rm -f "$xc_s1"
189 test -f "$xc_s2" && rm -f "$xc_s2"
190 test -f "$xc_s3" && rm -f "$xc_s3"
196 ## -------------------------------- ##
197 ## End of distclean amending code ##
198 ## -------------------------------- ##
200 m4_define([$0], [])[]dnl
204 dnl XC_AMEND_DISTCLEAN ([LIST-OF-SUBDIRS])
205 dnl -------------------------------------------------
208 dnl This macro embeds shell code into configure script
209 dnl that amends, at configure runtime, the distclean
210 dnl and maintainer-clean targets of Makefiles located
211 dnl in all subdirs given in the mandatory white-space
212 dnl separated list argument.
214 dnl Embedding only takes place when using automake 1.14
215 dnl or newer, otherwise amending code is not included
216 dnl in generated configure script.
218 dnl distclean and maintainer-clean targets are modified
219 dnl to avoid unconditional removal of dependency subdirs
220 dnl which triggers distclean and maintainer-clean errors
221 dnl when using automake 'subdir-objects' option along
222 dnl with per-target objects and source files existing in
223 dnl multiple subdirs used for different build targets.
225 dnl New behavior first removes each dependency tracking
226 dnl file independently, and only removes each dependency
227 dnl subdir when it finds out that it no longer holds any
228 dnl dependency tracking file.
230 dnl When configure option --disable-dependency-tracking
231 dnl is used no amending takes place given that there are
232 dnl no dependency tracking files.
234 AC_DEFUN([XC_AMEND_DISTCLEAN],
238 m4_ifdef([_AC_OUTPUT_MAIN_LOOP],
239 [m4_provide_if([_AC_OUTPUT_MAIN_LOOP], [],
240 [m4_fatal([call to AC_OUTPUT needed before $0])])])dnl
242 m4_if([$#], [1], [], [m4_fatal([$0: wrong number of arguments])])dnl
243 m4_if([$1], [], [m4_fatal([$0: missing argument])])dnl
245 AC_REQUIRE([XC_CONFIGURE_PREAMBLE])dnl
247 m4_ifdef([_AM_PROG_CC_C_O],
249 _XC_AMEND_DISTCLEAN_BODY([$1])
251 m4_define([$0], [])[]dnl