Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / libltdl / config / general.m4sh
1 m4_divert_push([KILL])                                  -*- Autoconf -*-
2 # general.m4sh -- general shell script boiler plate
3 #
4 #   Copyright (C) 2004, 2005, 2007, 2008, 2010 Free Software Foundation, Inc.
5 #   Written by Gary V. Vaughan, 2004
6 #
7 #   This file is part of GNU Libtool.
8 #
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.
13 #
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.
18 #
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.
23 #
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.
29
30 m4_define([M4SH_VERBATIM], [$1])
31
32 m4_ifndef([M4SH_IN_HEADER],
33 [m4_define([M4SH_IN_HEADER],
34 [m4_divert_text([HEADER-COPYRIGHT], [$1])])])
35
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
41 # function.
42 progpath="$0"
43 ])
44
45 M4SH_VERBATIM([[
46 : ${CP="cp -f"}
47 test "${ECHO+set}" = set || ECHO=${as_echo-'printf %s\n'}
48 : ${EGREP="@EGREP@"}
49 : ${FGREP="@FGREP@"}
50 : ${GREP="@GREP@"}
51 : ${LN_S="@LN_S@"}
52 : ${MAKE="make"}
53 : ${MKDIR="mkdir"}
54 : ${MV="mv -f"}
55 : ${RM="rm -f"}
56 : ${SED="@SED@"}
57 : ${SHELL="${CONFIG_SHELL-/bin/sh}"}
58 : ${Xsed="$SED -e 1s/^X//"}
59
60 # Global variables:
61 EXIT_SUCCESS=0
62 EXIT_FAILURE=1
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.
65
66 exit_status=$EXIT_SUCCESS
67
68 # Make sure IFS has a sensible default
69 lt_nl='
70 '
71 IFS="   $lt_nl"
72
73 dirname="s,/[^/]*$,,"
74 basename="s,^.*/,,"
75
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.
79 func_dirname ()
80 {
81     func_dirname_result=`$ECHO "${1}" | $SED "$dirname"`
82     if test "X$func_dirname_result" = "X${1}"; then
83       func_dirname_result="${3}"
84     else
85       func_dirname_result="$func_dirname_result${2}"
86     fi
87 } # func_dirname may be replaced by extended shell implementation
88
89
90 # func_basename file
91 func_basename ()
92 {
93     func_basename_result=`$ECHO "${1}" | $SED "$basename"`
94 } # func_basename may be replaced by extended shell implementation
95
96
97 # func_dirname_and_basename file append nondir_replacement
98 # perform func_basename and func_dirname in a single function
99 # call:
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 ()
110 {
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}"
115     else
116       func_dirname_result="$func_dirname_result${2}"
117     fi
118     func_basename_result=`$ECHO "${1}" | $SED -e "$basename"`
119 } # func_dirname_and_basename may be replaced by extended shell implementation
120
121
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
128 func_stripname ()
129 {
130     case ${2} in
131       .*) func_stripname_result=`$ECHO "${3}" | $SED "s%^${1}%%; s%\\\\${2}\$%%"`;;
132       *)  func_stripname_result=`$ECHO "${3}" | $SED "s%^${1}%%; s%${2}\$%%"`;;
133     esac
134 } # func_stripname may be replaced by extended shell implementation
135
136
137 # These SED scripts presuppose an absolute path with a trailing slash.
138 pathcar='s,^/\([^/]*\).*$,\1,'
139 pathcdr='s,^/[^/]*,,'
140 removedotparts=':dotsl
141                 s@/\./@/@g
142                 t dotsl
143                 s,/\.$,/,'
144 collapseslashes='s@/\{1,\}@/@g'
145 finalslash='s,/*$,/,'
146
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 ()
153 {
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
159     "")
160       # Empty path, that just means $cwd.
161       func_stripname '' '/' "`pwd`"
162       func_normal_abspath_result=$func_stripname_result
163       return
164     ;;
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.
168     ///*)
169       # Unusual form of absolute path, do nothing.
170     ;;
171     //*)
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=/
176     ;;
177     /*)
178       # Absolute path, do nothing.
179     ;;
180     *)
181       # Relative path, prepend $cwd.
182       func_normal_abspath_tpath=`pwd`/$func_normal_abspath_tpath
183     ;;
184   esac
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"`
190   while :; do
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=/
196       fi
197       break
198     fi
199     func_normal_abspath_tcomponent=`$ECHO "$func_normal_abspath_tpath" | $SED \
200         -e "$pathcar"`
201     func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \
202         -e "$pathcdr"`
203     # Figure out what to do with it
204     case $func_normal_abspath_tcomponent in
205       "")
206         # Trailing empty path component, ignore it.
207       ;;
208       ..)
209         # Parent dir; strip last assembled component from result.
210         func_dirname "$func_normal_abspath_result"
211         func_normal_abspath_result=$func_dirname_result
212       ;;
213       *)
214         # Actual path component, append it.
215         func_normal_abspath_result=$func_normal_abspath_result/$func_normal_abspath_tcomponent
216       ;;
217     esac
218   done
219   # Restore leading double-slash if one was found on entry.
220   func_normal_abspath_result=$func_normal_abspath_altnamespace$func_normal_abspath_result
221 }
222
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 ()
229 {
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
235
236   # Ascend the tree starting from libdir
237   while :; do
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=
243         break
244         ;;
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=.
251         fi
252         break
253         ;;
254       *)
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
261           break
262         fi
263         func_relative_path_result=../$func_relative_path_result
264         ;;
265     esac
266   done
267
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}
274   fi
275
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
282   fi
283 }
284
285 # The name of this program:
286 func_dirname_and_basename "$progpath"
287 progname=$func_basename_result
288
289 # Make sure we have an absolute path for reexecution:
290 case $progpath in
291   [\\/]*|[A-Za-z]:\\*) ;;
292   *[\\/]*)
293      progdir=$func_dirname_result
294      progdir=`cd "$progdir" && pwd`
295      progpath="$progdir/$progname"
296      ;;
297   *)
298      save_IFS="$IFS"
299      IFS=${PATH_SEPARATOR-:}
300      for progdir in $PATH; do
301        IFS="$save_IFS"
302        test -x "$progdir/$progname" && break
303      done
304      IFS="$save_IFS"
305      test -n "$progdir" || progdir=`pwd`
306      progpath="$progdir/$progname"
307      ;;
308 esac
309
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'
314
315 # Same as above, but do not quote variable references.
316 double_quote_subst='s/\(["`\\]\)/\\\1/g'
317
318 # Sed substitution that turns a string into a regex matching for the
319 # string literally.
320 sed_make_literal_regex='s,[].[^$\\*\/],\\&,g'
321
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'
326
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 '$'.
332 bs='\\'
333 bs2='\\\\'
334 bs4='\\\\\\\\'
335 dollar='\$'
336 sed_double_backslash="\
337   s/$bs4/&\\
338 /g
339   s/^$bs2$dollar/$bs&/
340   s/\\([^$bs]\\)$bs2$dollar/\\1$bs2$bs$dollar/g
341   s/\n//g"
342
343 # Standard options:
344 opt_dry_run=false
345 opt_help=false
346 opt_quiet=false
347 opt_verbose=false
348 opt_warning=:
349
350 # func_echo arg...
351 # Echo program name prefixed message, along with the current mode
352 # name if it has been set yet.
353 func_echo ()
354 {
355     $ECHO "$progname: ${opt_mode+$opt_mode: }$*"
356 }
357
358 # func_verbose arg...
359 # Echo program name prefixed message in verbose mode only.
360 func_verbose ()
361 {
362     $opt_verbose && func_echo ${1+"$@"}
363
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
366     # work around that:
367     :
368 }
369
370 # func_echo_all arg...
371 # Invoke $ECHO with all args, space-separated.
372 func_echo_all ()
373 {
374     $ECHO "$*"
375 }
376
377 # func_error arg...
378 # Echo program name prefixed message to standard error.
379 func_error ()
380 {
381     $ECHO "$progname: ${opt_mode+$opt_mode: }"${1+"$@"} 1>&2
382 }
383
384 # func_warning arg...
385 # Echo program name prefixed warning message to standard error.
386 func_warning ()
387 {
388     $opt_warning && $ECHO "$progname: ${opt_mode+$opt_mode: }warning: "${1+"$@"} 1>&2
389
390     # bash bug again:
391     :
392 }
393
394 # func_fatal_error arg...
395 # Echo program name prefixed message to standard error, and exit.
396 func_fatal_error ()
397 {
398     func_error ${1+"$@"}
399     exit $EXIT_FAILURE
400 }
401
402 # func_fatal_help arg...
403 # Echo program name prefixed message to standard error, followed by
404 # a help hint, and exit.
405 func_fatal_help ()
406 {
407     func_error ${1+"$@"}
408     func_fatal_error "$help"
409 }
410 help="Try \`$progname --help' for more information."  ## default
411
412
413 # func_grep expression filename
414 # Check whether EXPRESSION matches any line of FILENAME, without output.
415 func_grep ()
416 {
417     $GREP "$1" "$2" >/dev/null 2>&1
418 }
419
420
421 # func_mkdir_p directory-path
422 # Make sure the entire path to DIRECTORY-PATH is available.
423 func_mkdir_p ()
424 {
425     my_directory_path="$1"
426     my_dir_list=
427
428     if test -n "$my_directory_path" && test "$opt_dry_run" != ":"; then
429
430       # Protect directory names starting with `-'
431       case $my_directory_path in
432         -*) my_directory_path="./$my_directory_path" ;;
433       esac
434
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"
440
441         # If the last portion added has no slash in it, the list is done
442         case $my_directory_path in */*) ;; *) break ;; esac
443
444         # ...otherwise throw away the child directory and loop
445         my_directory_path=`$ECHO "$my_directory_path" | $SED -e "$dirname"`
446       done
447       my_dir_list=`$ECHO "$my_dir_list" | $SED 's,:*$,,'`
448
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
454         # stop in that case!
455         $MKDIR "$my_dir" 2>/dev/null || :
456       done
457       IFS="$save_mkdir_p_IFS"
458
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'"
462     fi
463 }
464
465
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.
470 func_mktempdir ()
471 {
472     my_template="${TMPDIR-/tmp}/${1-$progname}"
473
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}-$$"
477     else
478
479       # If mktemp works, use that first and foremost
480       my_tmpdir=`mktemp -d "${my_template}-XXXXXXXX" 2>/dev/null`
481
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}$$"
485
486         save_mktempdir_umask=`umask`
487         umask 0077
488         $MKDIR "$my_tmpdir"
489         umask $save_mktempdir_umask
490       fi
491
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'"
495     fi
496
497     $ECHO "$my_tmpdir"
498 }
499
500
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 ()
508 {
509     case $1 in
510       *[\\\`\"\$]*)
511         func_quote_for_eval_unquoted_result=`$ECHO "$1" | $SED "$sed_quote_subst"` ;;
512       *)
513         func_quote_for_eval_unquoted_result="$1" ;;
514     esac
515
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\""
524         ;;
525       *)
526         func_quote_for_eval_result="$func_quote_for_eval_unquoted_result"
527     esac
528 }
529
530
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 ()
535 {
536     case $1 in
537       *[\\\`\"]*)
538         my_arg=`$ECHO "$1" | $SED \
539             -e "$double_quote_subst" -e "$sed_double_backslash"` ;;
540       *)
541         my_arg="$1" ;;
542     esac
543
544     case $my_arg in
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       *[\@<:@\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \  ]*|*@:>@*|"")
550         my_arg="\"$my_arg\""
551         ;;
552     esac
553
554     func_quote_for_expand_result="$my_arg"
555 }
556
557
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.
562 func_show_eval ()
563 {
564     my_cmd="$1"
565     my_fail_exp="${2-:}"
566
567     ${opt_silent-false} || {
568       func_quote_for_expand "$my_cmd"
569       eval "func_echo $func_quote_for_expand_result"
570     }
571
572     if ${opt_dry_run-false}; then :; else
573       eval "$my_cmd"
574       my_status=$?
575       if test "$my_status" -eq 0; then :; else
576         eval "(exit $my_status); $my_fail_exp"
577       fi
578     fi
579 }
580
581
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 ()
587 {
588     my_cmd="$1"
589     my_fail_exp="${2-:}"
590
591     ${opt_silent-false} || {
592       func_quote_for_expand "$my_cmd"
593       eval "func_echo $func_quote_for_expand_result"
594     }
595
596     if ${opt_dry_run-false}; then :; else
597       eval "$lt_user_locale
598             $my_cmd"
599       my_status=$?
600       eval "$lt_safe_locale"
601       if test "$my_status" -eq 0; then :; else
602         eval "(exit $my_status); $my_fail_exp"
603       fi
604     fi
605 }
606
607 # func_tr_sh
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.
612 func_tr_sh ()
613 {
614   case $1 in
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'`
617     ;;
618   * )
619     func_tr_sh_result=$1
620     ;;
621   esac
622 }
623 ]])