3 # Extract macro arguments from autotools input with GNU M4.
4 # Written by Gary V. Vaughan, 2010
6 # Copyright (C) 2010-2013 Free Software Foundation, Inc.
7 # This is free software; see the source for copying conditions. There is NO
8 # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # Make sure we've evaluated scripts we depend on.
11 test -z "$progpath" && .
`echo "$0" |${SED-sed} 's|[^/]*$||'`/funclib.sh
12 test extract-trace
= "$progname" && .
`echo "$0" |${SED-sed} 's|[^/]*$||'`/options-parser
14 # Set a version string.
15 scriptversion
=2013-08-22.10
; # UTC
17 # This program is free software: you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation, either version 3 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
27 # You should have received a copy of the GNU General Public License
28 # along with this program. If not, see <http://www.gnu.org/licenses/>.
30 # Please report bugs or propose patches to gary@gnu.org.
37 # Run './extract-trace --help' for help with using this script from the
40 # Or source first 'options-parser' and then this file into your own
41 # scripts in order to make use of the function and variable framework
42 # they define, and also to avoid the overhead of forking to run this
43 # script in its own process on every call.
47 ## ------------------##
48 ## Helper functions. ##
49 ## ------------------##
51 # This section contains the helper functions used by the rest of
55 # func_autoconf_configure MAYBE-CONFIGURE-FILE
56 # --------------------------------------------
57 # Ensure that MAYBE-CONFIGURE-FILE is the name of a file in the current
58 # directory that contains an uncommented call to AC_INIT.
59 func_autoconf_configure
()
69 # If we were passed a genuine file, make sure it calls AC_INIT.
71 && _G_ac_init
=`$SED "$_G_sed_no_comment" "$1" |grep AC_INIT`
73 # Otherwise it is not a genuine Autoconf input file.
77 test 0 -ne "$_G_status" \
78 && func_verbose
"'$1' not using Autoconf"
84 # func_find_tool ENVVAR NAMES...
85 # ------------------------------
86 # Search for a required program. Use the value of ENVVAR, if set,
87 # otherwise find the first of the NAMES that can be run (i.e.,
88 # supports --version). If found, set ENVVAR to the program name,
94 _G_find_tool_envvar
=$1
97 eval "_G_find_tool_res=\$$_G_find_tool_envvar"
98 if test -n "$_G_find_tool_res"; then
99 _G_find_tool_error_prefix
="\$$find_tool_envvar: "
103 if func_tool_version_output
$_G_prog >/dev
/null
; then
104 _G_find_tool_res
=$_G_prog
109 if test -n "$_G_find_tool_res"; then
110 func_tool_version_output
>/dev
/null
$_G_find_tool_res "\
111 ${_G_find_tool_error_prefix}Cannot run '$_G_find_tool_res --version'"
113 # Make sure the result is exported to the environment for children
115 eval "$_G_find_tool_envvar=\$_G_find_tool_res"
116 eval "export $_G_find_tool_envvar"
119 One of these is required:
125 # func_tool_version_output CMD [FATAL-ERROR-MSG]
126 # ----------------------------------------------
127 # Attempt to run 'CMD --version', discarding errors. The output can be
128 # ignored by redirecting stdout, and this function used simply to test
129 # whether the command exists and exits normally when passed a
130 # '--version' argument.
131 # When FATAL-ERROR-MSG is given, then this function will display the
132 # message and exit if running 'CMD --version' returns a non-zero exit
134 func_tool_version_output
()
139 _G_fatal_error_msg
=$2
141 # Some tools, like 'git2cl' produce thousands of lines of output
142 # unless stdin is /dev/null - in that case we want to return
143 # successfully without saving all of that output. Other tools,
144 # such as 'help2man' exit with a non-zero status when stdin comes
145 # from /dev/null, so we re-execute without /dev/null if that
146 # happens. This means that occasionally, the output from both calls
147 # ends up in the result, but the alternative would be to discard the
148 # output from one call, and hope the other produces something useful.
149 { $_G_cmd --version </dev
/null ||
$_G_cmd --version; } 2>/dev
/null
152 test 0 -ne "$_G_status" && test -n "$_G_fatal_error_msg" \
153 && func_fatal_error
"$_G_fatal_error_msg"
159 ## -------------------- ##
160 ## Resource management. ##
161 ## -------------------- ##
163 # This section contains definitions for functions that each ensure a
164 # particular resource (a file, or a non-empty configuration variable for
165 # example) is available, and if appropriate to extract default values
166 # from pertinent package files. Where a variable already has a non-
167 # empty value (as set by the package's 'bootstrap.conf'), that value is
168 # used in preference to deriving the default. Call them using their
169 # associated 'require_*' variable to ensure that they are executed, at
172 # It's entirely deliberate that calling these functions can set
173 # variables that don't obey the namespace limitations obeyed by the rest
174 # of this file, in order that that they be as useful as possible to
178 # require_configure_ac
179 # --------------------
180 # Ensure that there is a 'configure.ac' or 'configure.in' file in the
181 # current directory that contains an uncommented call to AC_INIT, and
182 # that '$configure_ac' contains its name.
183 require_configure_ac
=func_require_configure_ac
184 func_require_configure_ac
()
188 test -z "$configure_ac" \
189 && func_autoconf_configure configure.ac
&& configure_ac
=configure.ac
190 test -z "$configure_ac" \
191 && func_autoconf_configure configure.
in && configure_ac
=configure.
in
192 test -z "$configure_ac" \
193 || func_verbose
"found '$configure_ac'"
195 require_configure_ac
=:
201 # Search for GNU M4, and export it in $M4.
202 require_gnu_m4
=func_require_gnu_m4
203 func_require_gnu_m4
()
208 # Find the first m4 binary that responds to --version.
209 func_find_tool M4 gm4 gnum4
m4
212 test -n "$M4" || func_fatal_error
"\
213 Please install GNU M4, or 'export M4=/path/to/gnu/m4'."
215 func_verbose
"export M4='$M4'"
217 # Make sure the search result is visible to subshells
224 ## --------------- ##
225 ## Core functions. ##
226 ## --------------- ##
228 # This section contains the high level functions used when calling this
229 # file as a script. 'func_extract_trace' is probably the only one that you
230 # won't want to replace if you source this file into your own script.
233 # func_extract_trace MACRO_NAMES [FILENAME]...
234 # --------------------------------------------
235 # set '$func_extract_trace_result' to a colon delimited list of arguments
236 # to any of the comma separated list of MACRO_NAMES in FILENAME. If no
237 # FILENAME is given, then '$configure_ac' is assumed.
238 func_extract_trace
()
242 $require_configure_ac
245 _G_m4_traces
=`$bs_echo "--trace=$1" |$SED 's%,% --trace=%g'`
246 _G_re_macros
=`$bs_echo "($1)" |$SED 's%,%|%g'`
247 _G_macros
="$1"; shift
249 set dummy
$configure_ac
253 # Generate an error if the first file is missing
256 # Sadly, we can't use 'autom4te' tracing to extract macro arguments,
257 # because it complains about things we want to ignore at bootstrap
258 # time - like missing m4_include files; AC_PREREQ being newer than
259 # the installed autoconf; and returns nothing when tracing
260 # 'AM_INIT_AUTOMAKE' when aclocal hasn't been generated yet.
262 # The following tries to emulate a less persnickety version of (and
263 # due to not having to wait for Perl startup on every invocation,
264 # it's probably faster too):
266 # autom4te --language=Autoconf --trace=$my_macro:\$% "$@"
268 # First we give a minimal set of macro declarations to M4 to prime
269 # it for reading Autoconf macros, while still providing some of the
270 # functionality generally used at m4-time to supply dynamic
271 # arguments to Autocof functions, but without following
272 # 'm4_s?include' files.
276 m4_define([m4_copy], [m4_define([$2], m4_defn([$1]))])
277 m4_define([m4_rename], [m4_copy([$1], [$2])m4_undefine([$1])])
279 # Disable these macros.
280 m4_undefine([m4_dnl])
281 m4_undefine([m4_include])
282 m4_undefine([m4_m4exit])
283 m4_undefine([m4_m4wrap])
284 m4_undefine([m4_maketemp])
286 # Copy and rename macros not handled by "m4 --prefix".
287 m4_define([dnl], [m4_builtin([dnl])])
288 m4_copy([m4_define], [m4_defun])
289 m4_rename([m4_ifelse], [m4_if])
290 m4_ifdef([m4_mkstemp], [m4_undefine([m4_mkstemp])])
291 m4_rename([m4_patsubst], [m4_bpatsubst])
292 m4_rename([m4_regexp], [m4_bregexp])
294 # "m4sugar.mini" - useful m4-time macros for dynamic arguments.
295 # If we discover packages that need more m4 macros defined in
296 # order to bootstrap correctly, add them here:
297 m4_define([m4_bmatch],
298 [m4_if([$#], 0, [], [$#], 1, [], [$#], 2, [$2],
299 [m4_if(m4_bregexp([$1], [$2]), -1,
300 [$0([$1], m4_shift3($@))], [$3])])])
301 m4_define([m4_ifndef], [m4_ifdef([$1], [$3], [$2])])
302 m4_define([m4_ifset],
303 [m4_ifdef([$1], [m4_ifval(m4_defn([$1]), [$2], [$3])], [$3])])
304 m4_define([m4_require], [$1])
305 m4_define([m4_shift3], [m4_shift(m4shift(m4shift($@)))])
307 # "autoconf.mini" - things from autoconf macros we care about.
308 m4_copy([m4_defun], [AC_DEFUN])
310 # Dummy definitions for the macros we want to trace.
311 # AM_INIT_AUTOMAKE at least produces no trace without this.
316 for _G_macro
in $_G_macros; do
318 func_append _G_mini
"AC_DEFUN([$_G_macro])$nl"
322 # We discard M4's stdout, but the M4 trace output from reading our
323 # "autoconf.mini" followed by any other files passed to this
324 # function is then scanned by sed to transform it into a colon
325 # delimited argument list assigned to a shell variable.
326 _G_transform
='s|#.*$||; s|^dnl .*$||; s| dnl .*$||;'
328 # Unfortunately, alternation in regexp addresses doesn't work in at
329 # least BSD (and hence Mac OS X) sed, so we have to append a capture
330 # and print block for each traced macro to the sed transform script.
333 for _G_macro
in $_G_macros; do
335 func_append _G_transform
'
336 /^m4trace: -1- '"$_G_macro"'/ {
337 s|^m4trace: -1- '"$_G_macro"'[([]*||
346 # Save the command pipeline results for further use by callers of
348 func_extract_trace_result
=`$bs_echo "$_G_mini" \
349 |$M4 -daq --prefix $_G_m4_traces - "$@" 2>&1 1>/dev/null \
350 |$SED -n -e "$_G_transform"`
354 # func_extract_trace_first MACRO_NAMES [FILENAME]...
355 # --------------------------------------------------
356 # Exactly like func_extract_trace, except that only the first argument
357 # to the first invocation of one of the comma separated MACRO_NAMES is
358 # returned in '$func_extract_trace_first_result'.
359 func_extract_trace_first
()
363 func_extract_trace
${1+"$@"}
364 func_extract_trace_first_result
=`$bs_echo "$func_extract_trace_result" \
365 |$SED -e 's|:.*$||g' -e 1q`
376 usage
='$progname MACRO_NAME FILE [...]'
379 The first argument to this program is the name of an autotools macro
380 whose arguments you want to extract by examining the files listed in the
381 remaining arguments using the same tool that Autoconf and Automake use,
384 The arguments are returned separated by colons, with each traced call
389 eval set dummy
"$func_options_result"; shift
391 # Validate remaining non-option arguments.
393 || func_fatal_help
"not enough arguments"
395 # Pass non-option arguments to extraction function.
396 func_extract_trace
"$@"
399 test -n "$func_extract_trace_result" \
400 && $bs_echo "$func_extract_trace_result"
407 ## --------------------------- ##
408 ## Actually perform the trace. ##
409 ## --------------------------- ##
411 # Only call 'func_main' if this script was called directly.
412 test extract-trace
= "$progname" && func_main
"$@"
417 # eval: (add-hook 'write-file-hooks 'time-stamp)
418 # time-stamp-pattern: "10/scriptversion=%:y-%02m-%02d.%02H; # UTC"
419 # time-stamp-time-zone: "UTC"