* m4/libtool.m4: Quote shrext as it is evaled in ltmain.
[libtool.git] / libtoolize.in
blobff1c792430af2bf656a2ea0fff401ca4b5603483
1 #! /bin/sh
2 # libtoolize (GNU @PACKAGE@@TIMESTAMP@) @VERSION@
3 # Written by Gary V. Vaughan <gary@gnu.org>
5 # Copyright (C) 2003, 2004 Free Software Foundation, Inc.
6 # This is free software; see the source for copying conditions. There is NO
7 # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, a copy can be downloaded from
21 # http://www.gnu.org/copyleft/gpl.html, or by writing to the Free
22 # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 # MA 02111-1307, USA.
25 # As a special exception to the GNU General Public License, if you
26 # distribute this file as part of a program that contains a
27 # configuration script generated by Autoconf, you may include it under
28 # the same distribution terms that you use for the rest of that program.
30 # Usage: $progname [OPTION]...
32 # Prepare a package to use libtool.
34 # -c, --copy copy files rather than symlinking them
35 # --debug enable verbose shell tracing
36 # -n, --dry-run print commands rather than running them
37 # -f, --force replace existing files
38 # --help display this message and exit
39 # -i, --install copy missing auxiliary files
40 # --ltdl install libltdl in a subdirectory
41 # --ltdl-tar install the libltdl tarball
42 # -q, --quiet work silently
43 # -v, --verbose verbosely report processing
44 # --version print version information and exit
46 # You must `cd' to the top directory of your package before you run
47 # `$progname'.
49 # Report bugs to <bug-libtool@gnu.org>.
51 : ${CP="cp -f"}
52 : ${LN_S="@LN_S@"}
53 : ${MKDIR="mkdir"}
54 : ${RM="rm -f"}
56 # The name of this program:
57 progname=`echo "$0" | sed 's%^.*/%%'`
58 PROGRAM=libtoolize
60 # Global variables.
61 dry_run_opt=false
62 force_opt=false
63 install_opt=false
64 link_opt=:
65 ltdl_opt=false
66 ltdl_tar_opt=false
67 quiet_opt=false
68 verbose_opt=false
70 seen_libtool=false
71 seen_ltdl=false
73 exit_status=0
75 # Locations for important files:
76 prefix=@prefix@
77 datadir=@datadir@
78 pkgdatadir=@pkgdatadir@
79 pkgmacrodir="$pkgdatadir/m4"
80 auxdir=
81 m4dir=
82 configure_ac=configure.in
84 sed_dirname='s,/[^/]*$,,'
86 # func_echo arg...
87 # Echo program name prefixed message.
88 func_echo ()
90 echo $progname: ${1+"$@"}
93 # func_verbose arg...
94 # Echo program name prefixed message in verbose mode only.
95 func_verbose ()
97 $verbose_opt && func_echo ${1+"$@"}
100 # func_error arg...
101 # Echo program name prefixed message to standard error.
102 func_error ()
104 echo $progname: ${1+"$@"} 1>&2
107 # func_fatal_error arg...
108 # Echo program name prefixed message to standard error, and exit.
109 func_fatal_error ()
111 func_error ${1+"$@"}
112 exit 1
115 # func_fatal_help arg...
116 # Echo program name prefixed message to standard error, followed by
117 # a help hint, and exit.
118 func_fatal_help ()
120 func_error ${1+"$@"}
121 func_fatal_error "Try \`$progname --help' for more information."
125 # Parse options once, thoroughly. This comes as soon as possible in
126 # the script to make things like `libtoolize --version' happen quickly.
128 # sed scripts:
129 my_sed_single_opt='1s/^\(..\).*$/\1/;q'
130 my_sed_single_rest='1s/^..\(.*\)$/\1/;q'
131 my_sed_help='/^# Usage:/,/# Report bugs to/ {
132 s/^# //; s/^# *$//;
133 s/\$progname/'$progname'/;
135 }; d'
136 my_sed_version='/^# '$PROGRAM' (GNU /,/# warranty; / {
137 s/^# //; s/^# *$//;
138 s/\((C)\)[ 0-9,-]*\( [1-9][0-9]*\)/\1\2/;
140 }; d'
143 while test "$#" -gt 0; do
144 case $1 in
145 --copy | -c) shift; link_opt=false ;;
147 --debug) shift
148 func_echo "enabling shell trace mode"
149 set -x
152 --dry-run | -n) shift
153 if $dry_run_opt; then :; else
154 dry_run_opt=:
155 RM="echo $RM"
156 test -n "$LN_S" && LN_S="echo $LN_S"
157 CP="echo $CP"
158 MKDIR="echo $MKDIR"
162 --force | -f) shift; force_opt=: ;;
163 --install | -i) shift; install_opt=: ;;
164 --ltdl) shift; ltdl_opt=: ;;
165 --ltdl-tar) shift; ltdl_tar_opt=: ;;
166 --quiet | --automake | -q) # --automake is for 1.5 compatibility
167 shift; quiet_opt=: ;;
168 --verbose | -v) shift; verbose_opt=: ;;
170 -c*|-i*|-f*|-n*|-q*|-v*)
171 opt=`echo "$1" |sed "$my_sed_single_opt"`
172 rest=`echo "$1" |sed "$my_sed_single_rest"`
173 shift
174 set -- "$opt" "-$rest" ${1+"$@"}
177 --version) sed "$my_sed_version" < $0; exit 0 ;;
178 --help) sed "$my_sed_help" < $0; exit 0 ;;
179 --) shift; break ;;
180 -*) func_fatal_help "unrecognized option \`$1'" ;;
181 *) func_fatal_help "too many arguments" ;;
182 esac
183 done
186 # func_mkdir_p dir
187 # Make sure the entire path to DIR is available.
188 func_mkdir_p ()
190 my_dir=$1
191 my_dirs=
193 while test ! -d "$my_dir"; do
194 my_dirs="$my_dir $my_dirs"
195 case $my_dir in */*) ;; *) break ;; esac
196 my_dir=`echo "$my_dir" | sed "$sed_dirname"`
197 done
198 test ! -n "$my_dirs" || $MKDIR $my_dirs
201 # func_copy srcfile destfile
202 # If option `--copy' was specified, or soft-linking SRCFILE to DESTFILE fails,
203 # then try to copy SRCFILE to DESTFILE.
204 func_copy ()
206 my_srcfile="$1"
207 my_destfile="$2"
208 my_return_status=1
210 func_mkdir_p `echo "$my_destfile" | sed "$sed_dirname"`
212 $RM "$my_destfile"
213 if $link_opt && $LN_S "$my_srcfile" "$my_destfile"; then
214 $quiet_opt || func_echo "copying file \`$my_destfile'"
215 my_return_status=0
216 elif $CP "$my_srcfile" "$my_destfile"; then
217 $quiet_opt || func_echo "copying file \`$my_destfile'"
218 my_return_status=0
219 else
220 func_error "can not copy \`$my_srcfile' to \`$my_destfile'"
221 exit_status=1
224 return $my_return_status
227 # func_copy_all_files srcdir destdir [glob_exclude] [copy_cb=func_copy]
228 # For each file in SRCDIR, then try to copy the file to DESTDIR by calling
229 # COPY_CB with the src and dest files. If GLOB_EXCLUDE is given, exclude
230 # any matching filenames from the copy. If COPY_CB is passed, then the
231 # check for overwriting without force_opt is the callbacks responsibility:
232 # This allows using callbacks like func_serial_update, which perform their
233 # own checks to decide whether to overwrite the dest file.
234 func_copy_all_files ()
236 my_srcdir="$1"
237 my_destdir="$2"
238 my_glob_exclude="$3"
239 my_copy_cb=${4-func_copy}
241 for my_filename in `cd "$my_srcdir" && ls`; do
243 # ignore excluded filenames
244 if test -n "$my_glob_exclude"; then
245 eval 'case $my_filename in '$my_glob_exclude') continue ;; esac'
248 if test -f "$my_srcdir/$my_filename"; then
249 if test "X$my_copy_cb" = Xfunc_copy; then
250 $force_opt || if test -f "$my_destdir/$my_filename"; then
251 $quiet_opt \
252 || func_error "\`$my_destdir/$my_filename' exists: use \`--force' to overwrite"
253 continue
256 else
257 # Not a regular file
258 continue
261 $my_copy_cb "$my_srcdir/$my_filename" "$my_destdir/$my_filename"
262 done
265 # func_grep expression filename
266 # Check whether EXPRESSION matches any line of FILENAME, without output.
267 func_grep ()
269 grep "$1" "$2" >/dev/null 2>&1
272 # func_scan_files
273 # Scan configure.(ac|in) and aclocal.m4 (if present) for use of libltdl
274 # and libtool. Possibly running some of these tools if necessary.
275 # Libtoolize affects the contents of aclocal.m4, and should be run before
276 # aclocal, so we can't use configure --trace which relies on a consistent
277 # configure.(ac|in) and aclocal.m4.
278 func_scan_files ()
280 # Prefer configure.ac to configure.in
281 test -f configure.ac && configure_ac=configure.ac
282 test -f "$configure_ac" \
283 || func_fatal_help "\`$configure_ac' does not exist"
285 test -n "`cd $pkgdatadir && ls`" \
286 || func_fatal_error "can not list files in \`$pkgdatadir'"
289 # Set local variables to reflect contents of configure.ac
290 my_uses_autoconf=false
291 my_sed_scan_configure_ac='s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
292 /AC_INIT/ { s,^.*$,my_uses_autoconf=:,; p; };
294 eval `sed "$my_sed_scan_configure_ac" "$configure_ac"`
296 $my_uses_autoconf || {
297 func_verbose "$configure_ac: not using Autoconf"
298 return
301 # ---------------------------------------------------- #
302 # Probe macro usage in configure.ac and/or aclocal.m4. #
303 # ---------------------------------------------------- #
305 my_sed_traces='s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
306 /AC_CONFIG_AUX_DIR[^_]/ {
307 s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,auxdir=\1,; p;
309 /AC_CONFIG_MACRO_DIR/ {
310 s,^.*AC_CONFIG_MACRO_DIR([[ ]*\([^])]*\).*$,m4dir=\1,; p;
312 /A[CM]_PROG_LIBTOOL/ { s,^.*$,seen_libtool=:,; p; };
313 /AC_LIB_LTDL/ { s,^.*$,seen_ltdl=:,; p; };
315 eval `cat aclocal.m4 "$configure_ac" | sed "$my_sed_traces" 2>/dev/null`
318 # ---------------- #
319 # Validate auxdir. #
320 # ---------------- #
322 if test -n "$auxdir"; then
323 # If $configure_ac contains AC_CONFIG_AUX_DIR, check that it was
324 # not given in terms of a shell variable!
325 case "$auxdir" in
326 *\$*)
327 func_fatal_error "can not handle variables in AC_CONFIG_AUX_DIR"
329 *);;
330 esac
331 else
332 # Try to discover auxdir the same way it is discovered by configure.
333 # Note that we default to the current directory.
334 for dir in . .. ../..; do
335 if test -f "$dir/install-sh"; then
336 auxdir=$dir
337 break
338 elif test -f "$dir/install.sh"; then
339 auxdir="$dir"
340 break
342 done
345 # Just use the current directory if all else fails.
346 test -n "$auxdir" || auxdir=.
349 # ------------------------------ #
350 # Find local m4 macro directory. #
351 # ------------------------------ #
353 # If AC_CONFIG_MACRO_DIR turned nothing up, we hunt for ACLOCAL_AMFLAGS
354 # in `Makefile.am' for a `-I' argument.
356 my_sed_aclocal_flags='/^[ ]*ACLOCAL_[A-Z_]*FLAGS[ ]*=/ {
357 s,^[^=]*=[ ]*\(.*\), \1,; q; }; d'
358 if test ! -n "$m4dir" && test -f Makefile.am; then
359 my_m4dir_is_next=false
360 for arg in `sed "$my_sed_aclocal_flags" Makefile.am`; do
361 if $my_m4dir_is_next; then
362 m4dir="$arg"
363 break
364 else
365 if test "X$arg" = "X-I"; then
366 my_m4dir_is_next=:
367 else
368 my_m4dir_is_next=false
371 done
374 # Just use the current directory if all else fails.
375 test -n "$m4dir" || m4dir=.
378 # func_included_files searchfile
379 # Output INCLUDEFILE if SEARCHFILE m4_includes it, else output SEARCHFILE.
380 func_included_files ()
382 my_searchfile="$1"
384 my_include_regex=
385 my_sed_include='
386 /^m4_include(\[.*\])$/ { s,^m4_include(\[\(.*\)\])$,\1,; p; };
389 test -f "$my_searchfile" && echo "$my_searchfile"
391 # Only recurse when we don't care if all the variables we use get
392 # trashed, since they are in global scope.
393 for my_filename in `sed "$my_sed_include" "$my_searchfile"`; do
394 func_included_files $my_filename
395 done
398 # func_serial filename [macro_regex]
399 # Output the value of the serial number comment in FILENAME, where the
400 # comment line must also match MACRO_REGEX, if given.
401 func_serial ()
403 my_filename="$1"
404 my_macro_regex="$2"
405 my_sed_serial='/^# serial [1-9][0-9]*[ ]*'"$my_macro_regex"'[ ]*$/ {
406 s,^# serial \([1-9][0-9]*\).*$,\1,; q;
407 }; d'
409 # Search FILENAME and all the files it m4_includes for a serial number
410 # in the file that AC_DEFUNs MACRO_REGEX.
411 my_serial=
412 for my_file in `func_included_files "$my_filename"`; do
413 if func_grep '^AC_DEFUN(\['"$my_macro_regex" "$my_file"; then
414 my_serial=`sed -e "$my_sed_serial" "$my_file"`
415 break
417 done
419 # If the file has no serial number, we assume it's ancient.
420 test -n "$my_serial" || my_serial=0
422 echo $my_serial
425 # func_serial_update srcfile destfile [macro_regex]
426 # Copy SRCFILE to DESTFILE provided SRCFILE has a newer serial number, or
427 # DESTFILE does not yet exist, or the user specified `--force'. If given,
428 # MACRO_REGEX must match any text after "# serial N" in the both files.
429 func_serial_update ()
431 my_srcfile="$1"
432 my_destfile="$2"
433 my_macro_regex="$3"
435 my_return_status=1
436 my_update_p=:
438 if test -f "$my_destfile"; then
439 my_src_serial=`func_serial "$my_srcfile" "$my_macro_regex"`
440 my_dest_serial=`func_serial "$my_destfile" "$my_macro_regex"`
442 test "$my_src_serial" -eq 0 && {
443 func_error "warning: no serial number on \`$my_srcfile', not copying."
444 return
447 # Only perform the file update if the destination has an older serial.
448 test "$my_src_serial" -gt "$my_dest_serial" || my_update_p=false
450 test "$my_src_serial" -gt "$my_dest_serial" \
451 && func_verbose "\`$my_srcfile' is serial $my_srcserial, greater than $my_destserial in \`$my_destfile'"
453 if test "$my_src_serial" -lt "$my_dest_serial"; then
454 func_error "\`$my_srcfile' is serial $my_srcserial, less than $my_destserial in \`$my_destfile'"
455 $force_opt \
456 || func_fatal_error "Use \`--force' to replace newer libtool files with this version."
460 if $my_update_p || $force_opt; then
461 func_copy "$my_srcfile" "$my_destfile"
462 my_return_status=$?
463 else
464 $quiet_opt \
465 || func_echo "\`$my_destfile' is already up to date."
468 # Do this after the copy for hand maintained `aclocal.m4', incase
469 # it has `m4_include([DESTFILE])', so the copy effectively already
470 # updated `aclocal.m4'.
471 $use_aclocal || if test -f aclocal.m4; then
472 test "$my_src_serial" -gt `func_serial aclocal.m4 "$my_macro_regex"` \
473 && func_echo "You should add the contents of \'$my_destfile' to \`aclocal.m4'."
476 return $my_return_status
479 # func_check_macros
480 # Sanity check macros from aclocal.m4 against installed versions.
481 func_check_macros ()
483 # Don't trace for this, we're just checking the user didn't invoke it
484 # directly from configure.ac.
485 sed 's,dnl .*$,,; s,# .*$,,' "$configure_ac" | grep AC_PROG_RANLIB \
486 && func_echo "\`AC_PROG_RANLIB' is rendered obsolete by \`AC_PROG_LIBTOOL'"
488 $seen_libtool \
489 || func_echo "Remember to add \`AC_PROG_LIBTOOL' to \`$configure_ac'."
491 # FIXME: Ensure ltmain.sh, libtool.m4 and ltdl.m4 are from the same release
494 # func_ltmain_update srcfile destfile
495 # Copy SRCFILE to DESTFILE provided SRCFILE has a newer VERSION/TIMESTAMP,
496 # or DESTFILE does not yet exist, or the user specified `--force'.
497 func_ltmain_update ()
499 my_srcfile="$1"
500 my_destfile="$2"
501 my_sed_ltmain='
502 s,^VERSION=[^0-9]*\(.*\)[ ]*$,\1,; t
503 s,^TIMESTAMP=[^0-9]*\([.0-9]*\) .*$,\1,; t
506 if test -f "$my_srcfile"; then :
507 else
508 func_error "\`$my_srcfile' does not exist."
509 return
512 # FIXME: check versions, and only downgrade with --force
513 cmp -s "$my_srcfile" "$my_destfile"
514 if test "$?" -ne 0 || $force_opt; then
515 func_copy "$my_srcfile" "$my_destfile"
516 else
517 $quiet_opt \
518 || func_echo "\`$my_destfile' is already up to date."
522 # func_config_update srcfile destfile
523 # Copy SRCFILE to DESTFILE provided SRCFILE has a newer TIMESTAMP,
524 # or DESTFILE does not yet exist, or the user specified `--force'.
525 func_config_update ()
527 my_srcfile="$1"
528 my_destfile="$2"
529 my_sed_config='s,^timestamp=[^0-9]*\([.0-9-]*\)[^0-9].*$,\1,; t; d'
531 if test -f "$my_srcfile"; then :
532 else
533 func_error "\`$my_srcfile' does not exist."
534 return
537 # FIXME: check versions, and only downgrade with --force
538 cmp -s "$my_srcfile" "$my_destfile"
539 if test "$?" -ne 0 || $force_opt; then
540 func_copy "$my_srcfile" "$my_destfile"
541 else
542 $quiet_opt \
543 || func_echo "\`$my_destfile' is already up to date."
549 ## ----------- ##
550 ## Main. ##
551 ## ----------- ##
554 rerun_aclocal=false
556 re_pkgaux_files='config\.guess|config\.sub|ltmain\.sh'
557 re_pkgmacro_files='libtool\.m4|ltdl\.m4'
559 func_scan_files
560 $quiet_opt || func_check_macros
562 # Copy all the files from installed libltdl to this project, if the
563 # user specified `--with-ltdl'.
564 $ltdl_opt && eval func_copy_all_files "$pkgdatadir/libltdl" libltdl
566 # Copy all the files from installed libltdl to a tarball in this project,
567 # if the user specified `--with-ltdl-tar'.
568 $ltdl_tar_opt && {
569 if test -f libltdl.tar.gz; then
570 if $force_opt; then
571 test -d libltdl && ${RM}r libltdl
572 func_copy_all_files "$pkgdatadir/libltdl" libltdl
573 tar -chof - libltdl | gzip --best > libltdl.tar.gz
574 ${RM}r libltdl
575 else
576 func_error "\`libltdl.tar.gz' exists: use \`--force' to overwrite"
581 # Copy all the installed utility files to the auxiliary directory if
582 # `--install' was passed, or else copy just ltmain.sh.
583 $quiet_opt || if test "$auxdir" != .; then
584 func_echo "putting files in AC_CONFIG_AUX_DIR, \`$auxdir'."
586 if $install_opt || $force_opt; then
587 func_copy_all_files "$pkgdatadir" "$auxdir" "$re_pkgaux_files"
588 func_config_update "$pkgdatadir/config.guess" "$auxdir/config.guess"
589 test -f "$pkgdatadir/config.sub" \
590 && func_config_update "$pkgdatadir/config.sub" "$auxdir/config.sub"
592 func_ltmain_update "$pkgdatadir/ltmain.sh" "$auxdir/ltmain.sh"
594 # Copy libtool's m4 macros to the macro directory, if they are newer.
595 $quiet_opt || if test "$m4dir" != .; then
596 func_echo "putting macros in AC_CONFIG_MACRO_DIR, \`$m4dir'."
599 libtool_m4="$pkgmacrodir/libtool.m4"
600 ltdl_m4="$pkgmacrodir/ltdl.m4"
602 func_copy_all_files "$pkgmacrodir" "$m4dir" \
603 "$re_pkgmacro_files" func_serial_update
604 func_serial_update "$libtool_m4" "$m4dir/libtool.m4" 'A[CM]_PROG_LIBTOOL'
606 if $seen_ltdl; then
607 func_serial_update "$ltdl_m4" "$m4dir/ltdl.m4" 'AC_LIB_LTDL'
608 else
609 func_verbose "Not copying \`$ltdl_m4', AC_LIB_LTDL not used."
613 exit $exit_status
615 # Local Variables:
616 # mode:shell-script
617 # sh-indentation:2
618 # End: