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