1 m4_divert_push([KILL]) -*- Autoconf -*-
2 # general.m4sh -- general shell script boiler plate
4 # Copyright (C) 2004, 2005, 2007, 2008, 2010 Free Software Foundation, Inc.
5 # Written by Gary V. Vaughan, 2004
7 # This file is part of GNU Libtool.
9 # GNU Libtool is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; either version 2 of
12 # the License, or (at your option) any later version.
14 # As a special exception to the GNU General Public License,
15 # if you distribute this file as part of a program or library that
16 # is built using GNU Libtool, you may include this file under the
17 # same distribution terms that you use for the rest of that program.
19 # GNU Libtool is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNES FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with GNU Libtool; see the file COPYING. If not, a copy
26 # can be downloaded from http://www.gnu.org/licenses/gpl.html,
27 # or obtained by writing to the Free Software Foundation, Inc.,
28 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 m4_define([M4SH_VERBATIM], [$1])
32 m4_ifndef([M4SH_IN_HEADER],
33 [m4_define([M4SH_IN_HEADER],
34 [m4_divert_text([HEADER-COPYRIGHT], [$1])])])
36 m4_divert_pop([KILL])M4SH_IN_HEADER([dnl
37 # Work around backward compatibility issue on IRIX 6.5. On IRIX 6.4+, sh
38 # is ksh but when the shell is invoked as "sh" and the current value of
39 # the _XPG environment variable is not equal to 1 (one), the special
40 # positional parameter $0, within a function call, is the name of the
47 test "${ECHO+set}" = set || ECHO=${as_echo-'printf %s\n'}
57 : ${SHELL="${CONFIG_SHELL-/bin/sh}"}
58 : ${Xsed="$SED -e 1s/^X//"}
63 EXIT_MISMATCH=63 # $? = 63 is used to indicate version mismatch to missing.
64 EXIT_SKIP=77 # $? = 77 is used to indicate a skipped test to automake.
66 exit_status=$EXIT_SUCCESS
68 # Make sure IFS has a sensible default
76 # func_dirname file append nondir_replacement
77 # Compute the dirname of FILE. If nonempty, add APPEND to the result,
78 # otherwise set result to NONDIR_REPLACEMENT.
81 func_dirname_result=`$ECHO "${1}" | $SED "$dirname"`
82 if test "X$func_dirname_result" = "X${1}"; then
83 func_dirname_result="${3}"
85 func_dirname_result="$func_dirname_result${2}"
87 } # func_dirname may be replaced by extended shell implementation
93 func_basename_result=`$ECHO "${1}" | $SED "$basename"`
94 } # func_basename may be replaced by extended shell implementation
97 # func_dirname_and_basename file append nondir_replacement
98 # perform func_basename and func_dirname in a single function
100 # dirname: Compute the dirname of FILE. If nonempty,
101 # add APPEND to the result, otherwise set result
102 # to NONDIR_REPLACEMENT.
103 # value returned in "$func_dirname_result"
104 # basename: Compute filename of FILE.
105 # value retuned in "$func_basename_result"
106 # Implementation must be kept synchronized with func_dirname
107 # and func_basename. For efficiency, we do not delegate to
108 # those functions but instead duplicate the functionality here.
109 func_dirname_and_basename ()
111 # Extract subdirectory from the argument.
112 func_dirname_result=`$ECHO "${1}" | $SED -e "$dirname"`
113 if test "X$func_dirname_result" = "X${1}"; then
114 func_dirname_result="${3}"
116 func_dirname_result="$func_dirname_result${2}"
118 func_basename_result=`$ECHO "${1}" | $SED -e "$basename"`
119 } # func_dirname_and_basename may be replaced by extended shell implementation
122 # func_stripname prefix suffix name
123 # strip PREFIX and SUFFIX off of NAME.
124 # PREFIX and SUFFIX must not contain globbing or regex special
125 # characters, hashes, percent signs, but SUFFIX may contain a leading
126 # dot (in which case that matches only a dot).
127 # func_strip_suffix prefix name
131 .*) func_stripname_result=`$ECHO "${3}" | $SED "s%^${1}%%; s%\\\\${2}\$%%"`;;
132 *) func_stripname_result=`$ECHO "${3}" | $SED "s%^${1}%%; s%${2}\$%%"`;;
134 } # func_stripname may be replaced by extended shell implementation
137 # These SED scripts presuppose an absolute path with a trailing slash.
138 pathcar='s,^/\([^/]*\).*$,\1,'
139 pathcdr='s,^/[^/]*,,'
140 removedotparts=':dotsl
144 collapseslashes='s@/\{1,\}@/@g'
145 finalslash='s,/*$,/,'
147 # func_normal_abspath PATH
148 # Remove doubled-up and trailing slashes, "." path components,
149 # and cancel out any ".." path components in PATH after making
150 # it an absolute path.
151 # value returned in "$func_normal_abspath_result"
152 func_normal_abspath ()
154 # Start from root dir and reassemble the path.
155 func_normal_abspath_result=
156 func_normal_abspath_tpath=$1
157 func_normal_abspath_altnamespace=
158 case $func_normal_abspath_tpath in
160 # Empty path, that just means $cwd.
161 func_stripname '' '/' "`pwd`"
162 func_normal_abspath_result=$func_stripname_result
165 # The next three entries are used to spot a run of precisely
166 # two leading slashes without using negated character classes;
167 # we take advantage of case's first-match behaviour.
169 # Unusual form of absolute path, do nothing.
172 # Not necessarily an ordinary path; POSIX reserves leading '//'
173 # and for example Cygwin uses it to access remote file shares
174 # over CIFS/SMB, so we conserve a leading double slash if found.
175 func_normal_abspath_altnamespace=/
178 # Absolute path, do nothing.
181 # Relative path, prepend $cwd.
182 func_normal_abspath_tpath=`pwd`/$func_normal_abspath_tpath
185 # Cancel out all the simple stuff to save iterations. We also want
186 # the path to end with a slash for ease of parsing, so make sure
187 # there is one (and only one) here.
188 func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \
189 -e "$removedotparts" -e "$collapseslashes" -e "$finalslash"`
191 # Processed it all yet?
192 if test "$func_normal_abspath_tpath" = / ; then
193 # If we ascended to the root using ".." the result may be empty now.
194 if test -z "$func_normal_abspath_result" ; then
195 func_normal_abspath_result=/
199 func_normal_abspath_tcomponent=`$ECHO "$func_normal_abspath_tpath" | $SED \
201 func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \
203 # Figure out what to do with it
204 case $func_normal_abspath_tcomponent in
206 # Trailing empty path component, ignore it.
209 # Parent dir; strip last assembled component from result.
210 func_dirname "$func_normal_abspath_result"
211 func_normal_abspath_result=$func_dirname_result
214 # Actual path component, append it.
215 func_normal_abspath_result=$func_normal_abspath_result/$func_normal_abspath_tcomponent
219 # Restore leading double-slash if one was found on entry.
220 func_normal_abspath_result=$func_normal_abspath_altnamespace$func_normal_abspath_result
223 # func_relative_path SRCDIR DSTDIR
224 # generates a relative path from SRCDIR to DSTDIR, with a trailing
225 # slash if non-empty, suitable for immediately appending a filename
226 # without needing to append a separator.
227 # value returned in "$func_relative_path_result"
228 func_relative_path ()
230 func_relative_path_result=
231 func_normal_abspath "$1"
232 func_relative_path_tlibdir=$func_normal_abspath_result
233 func_normal_abspath "$2"
234 func_relative_path_tbindir=$func_normal_abspath_result
236 # Ascend the tree starting from libdir
238 # check if we have found a prefix of bindir
239 case $func_relative_path_tbindir in
240 $func_relative_path_tlibdir)
241 # found an exact match
242 func_relative_path_tcancelled=
245 $func_relative_path_tlibdir*)
246 # found a matching prefix
247 func_stripname "$func_relative_path_tlibdir" '' "$func_relative_path_tbindir"
248 func_relative_path_tcancelled=$func_stripname_result
249 if test -z "$func_relative_path_result"; then
250 func_relative_path_result=.
255 func_dirname $func_relative_path_tlibdir
256 func_relative_path_tlibdir=${func_dirname_result}
257 if test "x$func_relative_path_tlibdir" = x ; then
258 # Have to descend all the way to the root!
259 func_relative_path_result=../$func_relative_path_result
260 func_relative_path_tcancelled=$func_relative_path_tbindir
263 func_relative_path_result=../$func_relative_path_result
268 # Now calculate path; take care to avoid doubling-up slashes.
269 func_stripname '' '/' "$func_relative_path_result"
270 func_relative_path_result=$func_stripname_result
271 func_stripname '/' '/' "$func_relative_path_tcancelled"
272 if test "x$func_stripname_result" != x ; then
273 func_relative_path_result=${func_relative_path_result}/${func_stripname_result}
276 # Normalisation. If bindir is libdir, return empty string,
277 # else relative path ending with a slash; either way, target
278 # file name can be directly appended.
279 if test ! -z "$func_relative_path_result"; then
280 func_stripname './' '' "$func_relative_path_result/"
281 func_relative_path_result=$func_stripname_result
285 # The name of this program:
286 func_dirname_and_basename "$progpath"
287 progname=$func_basename_result
289 # Make sure we have an absolute path for reexecution:
291 [\\/]*|[A-Za-z]:\\*) ;;
293 progdir=$func_dirname_result
294 progdir=`cd "$progdir" && pwd`
295 progpath="$progdir/$progname"
299 IFS=${PATH_SEPARATOR-:}
300 for progdir in $PATH; do
302 test -x "$progdir/$progname" && break
305 test -n "$progdir" || progdir=`pwd`
306 progpath="$progdir/$progname"
310 # Sed substitution that helps us do robust quoting. It backslashifies
311 # metacharacters that are still active within double-quoted strings.
312 Xsed="${SED}"' -e 1s/^X//'
313 sed_quote_subst='s/\([`"$\\]\)/\\\1/g'
315 # Same as above, but do not quote variable references.
316 double_quote_subst='s/\(["`\\]\)/\\\1/g'
318 # Sed substitution that turns a string into a regex matching for the
320 sed_make_literal_regex='s,[].[^$\\*\/],\\&,g'
322 # Sed substitution that converts a w32 file name or path
323 # which contains forward slashes, into one that contains
324 # (escaped) backslashes. A very naive implementation.
325 lt_sed_naive_backslashify='s|\\\\*|\\|g;s|/|\\|g;s|\\|\\\\|g'
327 # Re-`\' parameter expansions in output of double_quote_subst that were
328 # `\'-ed in input to the same. If an odd number of `\' preceded a '$'
329 # in input to double_quote_subst, that '$' was protected from expansion.
330 # Since each input `\' is now two `\'s, look for any number of runs of
331 # four `\'s followed by two `\'s and then a '$'. `\' that '$'.
336 sed_double_backslash="\
340 s/\\([^$bs]\\)$bs2$dollar/\\1$bs2$bs$dollar/g
351 # Echo program name prefixed message, along with the current mode
352 # name if it has been set yet.
355 $ECHO "$progname: ${opt_mode+$opt_mode: }$*"
358 # func_verbose arg...
359 # Echo program name prefixed message in verbose mode only.
362 $opt_verbose && func_echo ${1+"$@"}
364 # A bug in bash halts the script if the last line of a function
365 # fails when set -e is in force, so we need another command to
370 # func_echo_all arg...
371 # Invoke $ECHO with all args, space-separated.
378 # Echo program name prefixed message to standard error.
381 $ECHO "$progname: ${opt_mode+$opt_mode: }"${1+"$@"} 1>&2
384 # func_warning arg...
385 # Echo program name prefixed warning message to standard error.
388 $opt_warning && $ECHO "$progname: ${opt_mode+$opt_mode: }warning: "${1+"$@"} 1>&2
394 # func_fatal_error arg...
395 # Echo program name prefixed message to standard error, and exit.
402 # func_fatal_help arg...
403 # Echo program name prefixed message to standard error, followed by
404 # a help hint, and exit.
408 func_fatal_error "$help"
410 help="Try \`$progname --help' for more information." ## default
413 # func_grep expression filename
414 # Check whether EXPRESSION matches any line of FILENAME, without output.
417 $GREP "$1" "$2" >/dev/null 2>&1
421 # func_mkdir_p directory-path
422 # Make sure the entire path to DIRECTORY-PATH is available.
425 my_directory_path="$1"
428 if test -n "$my_directory_path" && test "$opt_dry_run" != ":"; then
430 # Protect directory names starting with `-'
431 case $my_directory_path in
432 -*) my_directory_path="./$my_directory_path" ;;
435 # While some portion of DIR does not yet exist...
436 while test ! -d "$my_directory_path"; do
437 # ...make a list in topmost first order. Use a colon delimited
438 # list incase some portion of path contains whitespace.
439 my_dir_list="$my_directory_path:$my_dir_list"
441 # If the last portion added has no slash in it, the list is done
442 case $my_directory_path in */*) ;; *) break ;; esac
444 # ...otherwise throw away the child directory and loop
445 my_directory_path=`$ECHO "$my_directory_path" | $SED -e "$dirname"`
447 my_dir_list=`$ECHO "$my_dir_list" | $SED 's,:*$,,'`
449 save_mkdir_p_IFS="$IFS"; IFS=':'
450 for my_dir in $my_dir_list; do
451 IFS="$save_mkdir_p_IFS"
452 # mkdir can fail with a `File exist' error if two processes
453 # try to create one of the directories concurrently. Don't
455 $MKDIR "$my_dir" 2>/dev/null || :
457 IFS="$save_mkdir_p_IFS"
459 # Bail out if we (or some other process) failed to create a directory.
460 test -d "$my_directory_path" || \
461 func_fatal_error "Failed to create \`$1'"
466 # func_mktempdir [string]
467 # Make a temporary directory that won't clash with other running
468 # libtool processes, and avoids race conditions if possible. If
469 # given, STRING is the basename for that directory.
472 my_template="${TMPDIR-/tmp}/${1-$progname}"
474 if test "$opt_dry_run" = ":"; then
475 # Return a directory name, but don't create it in dry-run mode
476 my_tmpdir="${my_template}-$$"
479 # If mktemp works, use that first and foremost
480 my_tmpdir=`mktemp -d "${my_template}-XXXXXXXX" 2>/dev/null`
482 if test ! -d "$my_tmpdir"; then
483 # Failing that, at least try and use $RANDOM to avoid a race
484 my_tmpdir="${my_template}-${RANDOM-0}$$"
486 save_mktempdir_umask=`umask`
489 umask $save_mktempdir_umask
492 # If we're not in dry-run mode, bomb out on failure
493 test -d "$my_tmpdir" || \
494 func_fatal_error "cannot create temporary directory \`$my_tmpdir'"
501 # func_quote_for_eval arg
502 # Aesthetically quote ARG to be evaled later.
503 # This function returns two values: FUNC_QUOTE_FOR_EVAL_RESULT
504 # is double-quoted, suitable for a subsequent eval, whereas
505 # FUNC_QUOTE_FOR_EVAL_UNQUOTED_RESULT has merely all characters
506 # which are still active within double quotes backslashified.
507 func_quote_for_eval ()
511 func_quote_for_eval_unquoted_result=`$ECHO "$1" | $SED "$sed_quote_subst"` ;;
513 func_quote_for_eval_unquoted_result="$1" ;;
516 case $func_quote_for_eval_unquoted_result in
517 # Double-quote args containing shell metacharacters to delay
518 # word splitting, command substitution and and variable
519 # expansion for a subsequent eval.
520 # Many Bourne shells cannot handle close brackets correctly
521 # in scan sets, so we specify it separately.
522 *[\@<:@\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*@:>@*|"")
523 func_quote_for_eval_result="\"$func_quote_for_eval_unquoted_result\""
526 func_quote_for_eval_result="$func_quote_for_eval_unquoted_result"
531 # func_quote_for_expand arg
532 # Aesthetically quote ARG to be evaled later; same as above,
533 # but do not quote variable references.
534 func_quote_for_expand ()
538 my_arg=`$ECHO "$1" | $SED \
539 -e "$double_quote_subst" -e "$sed_double_backslash"` ;;
545 # Double-quote args containing shell metacharacters to delay
546 # word splitting and command substitution for a subsequent eval.
547 # Many Bourne shells cannot handle close brackets correctly
548 # in scan sets, so we specify it separately.
549 *[\@<:@\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*@:>@*|"")
554 func_quote_for_expand_result="$my_arg"
558 # func_show_eval cmd [fail_exp]
559 # Unless opt_silent is true, then output CMD. Then, if opt_dryrun is
560 # not true, evaluate CMD. If the evaluation of CMD fails, and FAIL_EXP
561 # is given, then evaluate it.
567 ${opt_silent-false} || {
568 func_quote_for_expand "$my_cmd"
569 eval "func_echo $func_quote_for_expand_result"
572 if ${opt_dry_run-false}; then :; else
575 if test "$my_status" -eq 0; then :; else
576 eval "(exit $my_status); $my_fail_exp"
582 # func_show_eval_locale cmd [fail_exp]
583 # Unless opt_silent is true, then output CMD. Then, if opt_dryrun is
584 # not true, evaluate CMD. If the evaluation of CMD fails, and FAIL_EXP
585 # is given, then evaluate it. Use the saved locale for evaluation.
586 func_show_eval_locale ()
591 ${opt_silent-false} || {
592 func_quote_for_expand "$my_cmd"
593 eval "func_echo $func_quote_for_expand_result"
596 if ${opt_dry_run-false}; then :; else
597 eval "$lt_user_locale
600 eval "$lt_safe_locale"
601 if test "$my_status" -eq 0; then :; else
602 eval "(exit $my_status); $my_fail_exp"
608 # Turn $1 into a string suitable for a shell variable name.
609 # Result is stored in $func_tr_sh_result. All characters
610 # not in the set a-zA-Z0-9_ are replaced with '_'. Further,
611 # if $1 begins with a digit, a '_' is prepended as well.
615 [0-9]* | *[!a-zA-Z0-9_]*)
616 func_tr_sh_result=`$ECHO "$1" | $SED 's/^\([0-9]\)/_\1/; s/[^a-zA-Z0-9_]/_/g'`