autoupdate
[gnulib.git] / build-aux / ar-lib
1 #! /bin/sh
2 # Wrapper for Microsoft lib.exe
3
4 me=ar-lib
5 scriptversion=2010-09-02.19; # UTC
6
7 # Copyright (C) 2010 Free Software
8 # Foundation, Inc.
9 # Written by Peter Rosin <peda@lysator.liu.se>.
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
14 # any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 # As a special exception to the GNU General Public License, if you
25 # distribute this file as part of a program that contains a
26 # configuration script generated by Autoconf, you may include it under
27 # the same distribution terms that you use for the rest of that program.
28
29 # This file is maintained in Automake, please report
30 # bugs to <bug-automake@gnu.org> or send patches to
31 # <automake-patches@gnu.org>.
32
33
34 # func_error message
35 func_error ()
36 {
37   echo "$me: $1" 1>&2
38   exit 1
39 }
40
41 file_conv=
42
43 # func_file_conv build_file
44 # Convert a $build file to $host form and store it in $file
45 # Currently only supports Win32 hosts.
46 func_file_conv ()
47 {
48   file=$1
49   case $file in
50     / | /[!/]*) # absolute file, and not a UNC file
51       if test -z "$file_conv"; then
52         # lazily determine how to convert abs files
53         case `uname -s` in
54           MINGW*)
55             file_conv=mingw
56             ;;
57           CYGWIN*)
58             file_conv=cygwin
59             ;;
60           *)
61             file_conv=wine
62             ;;
63         esac
64       fi
65       case $file_conv in
66         mingw)
67           file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
68           ;;
69         cygwin)
70           file=`cygpath -m "$file" || echo "$file"`
71           ;;
72         wine)
73           file=`winepath -w "$file" || echo "$file"`
74           ;;
75       esac
76       ;;
77   esac
78 }
79
80 # func_at_file at_file operation archive
81 # Iterate over all members in AT_FILE performing OPERATION on ARCHIVE
82 # for each of them.
83 # When interpreting the content of the @FILE, do NOT use func_file_conv,
84 # since the user would need to supply preconverted file names to
85 # binutils ar, at least for MinGW.
86 func_at_file ()
87 {
88   operation=$2
89   archive=$3
90   at_file_contents=`cat "$1"`
91   eval set x "$at_file_contents"
92   shift
93
94   for member
95   do
96     $AR -NOLOGO $operation:"$member" "$archive" || exit $?
97   done
98 }
99
100 case $1 in
101   '')
102      func_error "no command.  Try \`$0 --help' for more information."
103      ;;
104   -h | --h*)
105     cat <<EOF
106 Usage: $me [--help] [--version] PROGRAM ACTION ARCHIVE [MEMBER...]
107
108 Members may be specified in a file named with @FILE.
109 EOF
110     exit $?
111     ;;
112   -v | --v*)
113     echo "$me, version $scriptversion"
114     exit $?
115     ;;
116 esac
117
118 if test $# -lt 3; then
119   func_error "you must specify a program, an action and an archive"
120 fi
121
122 AR=$1
123 shift
124 while :
125 do
126   if test $# -lt 2; then
127     func_error "you must specify a program, an action and an archive"
128   fi
129   case $1 in
130     -lib | -LIB \
131     | -ltcg | -LTCG \
132     | -machine* | -MACHINE* \
133     | -subsystem* | -SUBSYSTEM* \
134     | -verbose | -VERBOSE \
135     | -wx* | -WX* )
136       AR="$AR $1"
137       shift
138       ;;
139     *)
140       action=$1
141       shift
142       break
143       ;;
144   esac
145 done
146 orig_archive=$1
147 shift
148 func_file_conv "$orig_archive"
149 archive=$file
150
151 # strip leading dash in $action
152 action=${action#-}
153
154 delete=
155 extract=
156 list=
157 replace=
158 create=
159
160 while test -n "$action"
161 do
162   case $action in
163     d*) delete=yes  ;;
164     x*) extract=yes ;;
165     t*) list=yes    ;;
166     r*) replace=yes ;;
167     c*) create=yes  ;;
168     u*)             ;; # TODO: don't ignore the update modifier
169     *)
170       func_error "unknown action specified"
171       ;;
172   esac
173   action=${action#?}
174 done
175
176 case $delete$extract$list$replace in
177   yes)
178     ;;
179   yesyes*)
180     func_error "more than one action specified"
181     ;;
182   *)
183     func_error "no action specified"
184     ;;
185 esac
186
187 if test -n "$delete"; then
188   if test ! -f "$orig_archive"; then
189     func_error "archive not found"
190   fi
191   for member
192   do
193     case $1 in
194       @*)
195         func_at_file "${1#@}" -REMOVE "$archive"
196         ;;
197       *)
198         func_file_conv "$1"
199         $AR -NOLOGO -REMOVE:"$file" "$archive" || exit $?
200         ;;
201     esac
202   done
203
204 elif test -n "$extract"; then
205   if test ! -f "$orig_archive"; then
206     func_error "archive not found"
207   fi
208   if test $# -gt 0; then
209     for member
210     do
211       case $1 in
212         @*)
213           func_at_file "${1#@}" -EXTRACT "$archive"
214           ;;
215         *)
216           func_file_conv "$1"
217           $AR -NOLOGO -EXTRACT:"$file" "$archive" || exit $?
218           ;;
219       esac
220     done
221   else
222     $AR -NOLOGO -LIST "$archive" | sed -e 's/\\/\\\\/g' | while read member
223     do
224       $AR -NOLOGO -EXTRACT:"$member" "$archive" || exit $?
225     done
226   fi
227
228 elif test -n "$replace"; then
229   if test ! -f "$orig_archive"; then
230     if test -z "$create"; then
231       echo "$me: creating $orig_archive"
232     fi
233     orig_archive=
234   else
235     orig_archive=$archive
236   fi
237
238   for member
239   do
240     case $1 in
241     @*)
242       func_file_conv "${1#@}"
243       set x "$@" "@$file"
244       ;;
245     *)
246       func_file_conv "$1"
247       set x "$@" "$file"
248       ;;
249     esac
250     shift
251     shift
252   done
253
254   if test -n "$orig_archive"; then
255     $AR -NOLOGO -OUT:"$archive" "$orig_archive" "$@" || exit $?
256   else
257     $AR -NOLOGO -OUT:"$archive" "$@" || exit $?
258   fi
259
260 elif test -n "$list"; then
261   if test ! -f "$orig_archive"; then
262     func_error "archive not found"
263   fi
264   $AR -NOLOGO -LIST "$archive" || exit $?
265 fi