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