aa4acbdaa0f1ad3debe0f2118719c24df8bea62a
[gnulib.git] / build-aux / ar-lib
1 #! /bin/sh
2 # Wrapper for Microsoft lib.exe
3
4 me=ar-lib
5 scriptversion=2010-08-08.07; # 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 action=$1
125 shift
126 orig_archive=$1
127 shift
128 func_file_conv "$orig_archive"
129 archive=$file
130
131 # strip leading dash in $action
132 action=${action#-}
133
134 delete=
135 extract=
136 list=
137 replace=
138 create=
139
140 while test -n "$action"
141 do
142   case $action in
143     d*) delete=yes  ;;
144     x*) extract=yes ;;
145     t*) list=yes    ;;
146     r*) replace=yes ;;
147     c*) create=yes  ;;
148     u*)             ;; # TODO: don't ignore the update modifier
149     *)
150       func_error "unknown action specified"
151       ;;
152   esac
153   action=${action#?}
154 done
155
156 case $delete$extract$list$replace in
157   yes)
158     ;;
159   yesyes*)
160     func_error "more than one action specified"
161     ;;
162   *)
163     func_error "no action specified"
164     ;;
165 esac
166
167 if test -n "$delete"; then
168   if test ! -f "$orig_archive"; then
169     func_error "archive not found"
170   fi
171   for member
172   do
173     case $1 in
174       @*)
175         func_at_file "${1#@}" -REMOVE "$archive"
176         ;;
177       *)
178         func_file_conv "$1"
179         $AR -NOLOGO -REMOVE:"$file" "$archive" || exit $?
180         ;;
181     esac
182   done
183
184 elif test -n "$extract"; then
185   if test ! -f "$orig_archive"; then
186     func_error "archive not found"
187   fi
188   if test $# -gt 0; then
189     for member
190     do
191       case $1 in
192         @*)
193           func_at_file "${1#@}" -EXTRACT "$archive"
194           ;;
195         *)
196           func_file_conv "$1"
197           $AR -NOLOGO -EXTRACT:"$file" "$archive" || exit $?
198           ;;
199       esac
200     done
201   else
202     $AR -NOLOGO -LIST "$archive" | while read member
203     do
204       $AR -NOLOGO -EXTRACT:"$member" "$archive" || exit $?
205     done
206   fi
207
208 elif test -n "$replace"; then
209   if test ! -f "$orig_archive"; then
210     if test -z "$create"; then
211       echo "$me: creating $orig_archive"
212     fi
213     orig_archive=
214   else
215     orig_archive=$archive
216   fi
217
218   for member
219   do
220     case $1 in
221     @*)
222       func_file_conv "${1#@}"
223       set x "$@" "@$file"
224       ;;
225     *)
226       func_file_conv "$1"
227       set x "$@" "$file"
228       ;;
229     esac
230     shift
231     shift
232   done
233
234   if test -n "$orig_archive"; then
235     $AR -NOLOGO -OUT:"$archive" "$orig_archive" "$@" || exit $?
236   else
237     $AR -NOLOGO -OUT:"$archive" "$@" || exit $?
238   fi
239
240 elif test -n "$list"; then
241   if test ! -f "$orig_archive"; then
242     func_error "archive not found"
243   fi
244   $AR -NOLOGO -LIST "$archive" || exit $?
245 fi