blob: 621453d4e69aeaf8243a0902c7e371c900e2184f [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001#---------------------------------------------------------------------------
2#
3# xc-am-iface.m4
4#
5# Copyright (c) 2013 - 2022 Daniel Stenberg <daniel@haxx.se>
6#
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.
10#
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.
18#
19# SPDX-License-Identifier: ISC
20#
21#---------------------------------------------------------------------------
22
23# serial 1
24
25
26dnl _XC_AUTOMAKE_BODY
27dnl -------------------------------------------------
28dnl Private macro.
29dnl
30dnl This macro performs embedding of automake initialization
31dnl code into configure script. When automake version 1.14 or
32dnl newer is used at configure script generation time, this
33dnl results in 'subdir-objects' automake option being used.
34dnl When using automake versions older than 1.14 this option
35dnl is not used when generating configure script.
36dnl
37dnl Existence of automake _AM_PROG_CC_C_O m4 private macro
38dnl is used to differentiate automake version 1.14 from older
39dnl ones which lack this macro.
40
41m4_define([_XC_AUTOMAKE_BODY],
42[dnl
43## --------------------------------------- ##
44## Start of automake initialization code ##
45## --------------------------------------- ##
46m4_ifdef([_AM_PROG_CC_C_O],
47[
48AM_INIT_AUTOMAKE([subdir-objects])
49],[
50AM_INIT_AUTOMAKE
51])dnl
52## ------------------------------------- ##
53## End of automake initialization code ##
54## ------------------------------------- ##
55dnl
56m4_define([$0], [])[]dnl
57])
58
59
60dnl XC_AUTOMAKE
61dnl -------------------------------------------------
62dnl Public macro.
63dnl
64dnl This macro embeds automake machinery into configure
65dnl script regardless of automake version used in order
66dnl to generate configure script.
67dnl
68dnl When using automake version 1.14 or newer, automake
69dnl initialization option 'subdir-objects' is used to
70dnl generate the configure script, otherwise this option
71dnl is not used.
72
73AC_DEFUN([XC_AUTOMAKE],
74[dnl
75AC_PREREQ([2.50])dnl
76dnl
77AC_BEFORE([$0],[AM_INIT_AUTOMAKE])dnl
78dnl
79_XC_AUTOMAKE_BODY
80dnl
81m4_ifdef([AM_INIT_AUTOMAKE],
82 [m4_undefine([AM_INIT_AUTOMAKE])])dnl
83dnl
84m4_define([$0], [])[]dnl
85])
86
87
88dnl _XC_AMEND_DISTCLEAN_BODY ([LIST-OF-SUBDIRS])
89dnl -------------------------------------------------
90dnl Private macro.
91dnl
92dnl This macro performs shell code embedding into
93dnl configure script in order to modify distclean
94dnl and maintainer-clean targets of makefiles which
95dnl are located in given list of subdirs.
96dnl
97dnl See XC_AMEND_DISTCLEAN comments for details.
98
99m4_define([_XC_AMEND_DISTCLEAN_BODY],
100[dnl
101## ---------------------------------- ##
102## Start of distclean amending code ##
103## ---------------------------------- ##
104
105for xc_subdir in [$1]
106do
107
108if test ! -f "$xc_subdir/Makefile"; then
109 echo "$xc_msg_err $xc_subdir/Makefile file not found. $xc_msg_abrt" >&2
110 exit 1
111fi
112
113# Fetch dependency tracking file list from Makefile include lines.
114
115xc_inc_lines=`grep '^include .*(DEPDIR)' "$xc_subdir/Makefile" 2>/dev/null`
116xc_cnt_words=`echo "$xc_inc_lines" | wc -w | tr -d "$xc_space$xc_tab"`
117
118# --disable-dependency-tracking might have been used, consequently
119# there is nothing to amend without a dependency tracking file list.
120
121if test $xc_cnt_words -gt 0; then
122
123AC_MSG_NOTICE([amending $xc_subdir/Makefile])
124
125# Build Makefile specific patch hunk.
126
127xc_p="$xc_subdir/xc_patch.tmp"
128
129xc_rm_depfiles=`echo "$xc_inc_lines" \
130 | $SED 's%include% -rm -f%' 2>/dev/null`
131
132xc_dep_subdirs=`echo "$xc_inc_lines" \
133 | $SED 's%include[[ ]][[ ]]*%%' 2>/dev/null \
134 | $SED 's%(DEPDIR)/.*%(DEPDIR)%' 2>/dev/null \
135 | sort | uniq`
136
137echo "$xc_rm_depfiles" >$xc_p
138
139for xc_dep_dir in $xc_dep_subdirs; do
140 echo "${xc_tab}@xm_dep_cnt=\`ls $xc_dep_dir | wc -l 2>/dev/null\`; \\" >>$xc_p
141 echo "${xc_tab}if test \$\$xm_dep_cnt -eq 0 && test -d $xc_dep_dir; then \\" >>$xc_p
142 echo "${xc_tab} rm -rf $xc_dep_dir; \\" >>$xc_p
143 echo "${xc_tab}fi" >>$xc_p
144done
145
146# Build Makefile patching sed scripts.
147
148xc_s1="$xc_subdir/xc_script_1.tmp"
149xc_s2="$xc_subdir/xc_script_2.tmp"
150xc_s3="$xc_subdir/xc_script_3.tmp"
151
152cat >$xc_s1 <<\_EOT
153/^distclean[[ ]]*:/,/^[[^ ]][[^ ]]*:/{
154 s/^.*(DEPDIR)/___xc_depdir_line___/
155}
156/^maintainer-clean[[ ]]*:/,/^[[^ ]][[^ ]]*:/{
157 s/^.*(DEPDIR)/___xc_depdir_line___/
158}
159_EOT
160
161cat >$xc_s2 <<\_EOT
162/___xc_depdir_line___$/{
163 N
164 /___xc_depdir_line___$/D
165}
166_EOT
167
168cat >$xc_s3 <<_EOT
169/^___xc_depdir_line___/{
170 r $xc_p
171 d
172}
173_EOT
174
175# Apply patch to Makefile and cleanup.
176
177$SED -f "$xc_s1" "$xc_subdir/Makefile" >"$xc_subdir/Makefile.tmp1"
178$SED -f "$xc_s2" "$xc_subdir/Makefile.tmp1" >"$xc_subdir/Makefile.tmp2"
179$SED -f "$xc_s3" "$xc_subdir/Makefile.tmp2" >"$xc_subdir/Makefile.tmp3"
180
181if test -f "$xc_subdir/Makefile.tmp3"; then
182 mv -f "$xc_subdir/Makefile.tmp3" "$xc_subdir/Makefile"
183fi
184
185test -f "$xc_subdir/Makefile.tmp1" && rm -f "$xc_subdir/Makefile.tmp1"
186test -f "$xc_subdir/Makefile.tmp2" && rm -f "$xc_subdir/Makefile.tmp2"
187test -f "$xc_subdir/Makefile.tmp3" && rm -f "$xc_subdir/Makefile.tmp3"
188
189test -f "$xc_p" && rm -f "$xc_p"
190test -f "$xc_s1" && rm -f "$xc_s1"
191test -f "$xc_s2" && rm -f "$xc_s2"
192test -f "$xc_s3" && rm -f "$xc_s3"
193
194fi
195
196done
197
198## -------------------------------- ##
199## End of distclean amending code ##
200## -------------------------------- ##
201dnl
202m4_define([$0], [])[]dnl
203])
204
205
206dnl XC_AMEND_DISTCLEAN ([LIST-OF-SUBDIRS])
207dnl -------------------------------------------------
208dnl Public macro.
209dnl
210dnl This macro embeds shell code into configure script
211dnl that amends, at configure runtime, the distclean
212dnl and maintainer-clean targets of Makefiles located
213dnl in all subdirs given in the mandatory white-space
214dnl separated list argument.
215dnl
216dnl Embedding only takes place when using automake 1.14
217dnl or newer, otherwise amending code is not included
218dnl in generated configure script.
219dnl
220dnl distclean and maintainer-clean targets are modified
221dnl to avoid unconditional removal of dependency subdirs
222dnl which triggers distclean and maintainer-clean errors
223dnl when using automake 'subdir-objects' option along
224dnl with per-target objects and source files existing in
225dnl multiple subdirs used for different build targets.
226dnl
227dnl New behavior first removes each dependency tracking
228dnl file independently, and only removes each dependency
229dnl subdir when it finds out that it no longer holds any
230dnl dependency tracking file.
231dnl
232dnl When configure option --disable-dependency-tracking
233dnl is used no amending takes place given that there are
234dnl no dependency tracking files.
235
236AC_DEFUN([XC_AMEND_DISTCLEAN],
237[dnl
238AC_PREREQ([2.50])dnl
239dnl
240m4_ifdef([_AC_OUTPUT_MAIN_LOOP],
241 [m4_provide_if([_AC_OUTPUT_MAIN_LOOP], [],
242 [m4_fatal([call to AC_OUTPUT needed before $0])])])dnl
243dnl
244m4_if([$#], [1], [], [m4_fatal([$0: wrong number of arguments])])dnl
245m4_if([$1], [], [m4_fatal([$0: missing argument])])dnl
246dnl
247AC_REQUIRE([XC_CONFIGURE_PREAMBLE])dnl
248dnl
249m4_ifdef([_AM_PROG_CC_C_O],
250[
251_XC_AMEND_DISTCLEAN_BODY([$1])
252])dnl
253m4_define([$0], [])[]dnl
254])