Home | History | Annotate | Line # | Download | only in dist
      1 #! /bin/sh
      2 # Wrapper for Microsoft lib.exe
      3 
      4 me=ar-lib
      5 scriptversion=2025-02-03.05; # UTC
      6 
      7 # Copyright (C) 2010-2025 Free Software Foundation, Inc.
      8 # Written by Peter Rosin <peda (at] 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 (at] gnu.org> or send patches to
     30 # <automake-patches (at] gnu.org>.
     31 
     32 
     33 # func_error message
     34 func_error ()
     35 {
     36   echo "$me: $1" 1>&2
     37   exit 1
     38 }
     39 
     40 file_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.
     45 func_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 	    if test -n "$MSYSTEM" && (cygpath --version) >/dev/null 2>&1; then
     55 	      # MSYS2 environment.
     56 	      file_conv=cygwin
     57 	    else
     58 	      # Original MinGW environment.
     59 	      file_conv=mingw
     60 	    fi
     61 	    ;;
     62 	  MSYS*)
     63 	    # Old MSYS environment, or MSYS2 with 32-bit MSYS2 shell.
     64 	    file_conv=cygwin
     65 	    ;;
     66 	  CYGWIN*)
     67 	    # Cygwin environment.
     68 	    file_conv=cygwin
     69 	    ;;
     70 	  *)
     71 	    file_conv=wine
     72 	    ;;
     73 	esac
     74       fi
     75       case $file_conv in
     76 	mingw)
     77 	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
     78 	  ;;
     79 	cygwin)
     80 	  file=`cygpath -w "$file" || echo "$file"`
     81 	  ;;
     82 	wine)
     83 	  file=`winepath -w "$file" || echo "$file"`
     84 	  ;;
     85       esac
     86       ;;
     87   esac
     88 }
     89 
     90 # func_at_file at_file operation archive
     91 # Iterate over all members in AT_FILE performing OPERATION on ARCHIVE
     92 # for each of them.
     93 # When interpreting the content of the @FILE, do NOT use func_file_conv,
     94 # since the user would need to supply preconverted file names to
     95 # binutils ar, at least for MinGW.
     96 func_at_file ()
     97 {
     98   operation=$2
     99   archive=$3
    100   at_file_contents=`cat "$1"`
    101   eval set x "$at_file_contents"
    102   shift
    103 
    104   for member
    105   do
    106     $AR -NOLOGO $operation:"$member" "$archive" || exit $?
    107   done
    108 }
    109 
    110 case $1 in
    111   '')
    112      func_error "no command.  Try '$0 --help' for more information."
    113      ;;
    114   -h | --h*)
    115     cat <<EOF
    116 Usage: $me [--help] [--version] PROGRAM ACTION ARCHIVE [MEMBER...]
    117 
    118 Members may be specified in a file named with @FILE.
    119 
    120 Report bugs to <bug-automake@gnu.org>.
    121 GNU Automake home page: <https://www.gnu.org/software/automake/>.
    122 General help using GNU software: <https://www.gnu.org/gethelp/>.
    123 EOF
    124     exit $?
    125     ;;
    126   -v | --v*)
    127     echo "$me (GNU Automake) $scriptversion"
    128     exit $?
    129     ;;
    130 esac
    131 
    132 if test $# -lt 3; then
    133   func_error "you must specify a program, an action and an archive"
    134 fi
    135 
    136 AR=$1
    137 shift
    138 while :
    139 do
    140   if test $# -lt 2; then
    141     func_error "you must specify a program, an action and an archive"
    142   fi
    143   case $1 in
    144     -lib | -LIB \
    145     | -ltcg | -LTCG \
    146     | -machine* | -MACHINE* \
    147     | -subsystem* | -SUBSYSTEM* \
    148     | -verbose | -VERBOSE \
    149     | -wx* | -WX* )
    150       AR="$AR $1"
    151       shift
    152       ;;
    153     -nologo | -NOLOGO)
    154       # We always invoke AR with -nologo, so don't need to add it again.
    155       shift
    156       ;;
    157     *)
    158       action=$1
    159       shift
    160       break
    161       ;;
    162   esac
    163 done
    164 orig_archive=$1
    165 shift
    166 func_file_conv "$orig_archive"
    167 archive=$file
    168 
    169 # strip leading dash in $action
    170 action=${action#-}
    171 
    172 delete=
    173 extract=
    174 list=
    175 quick=
    176 replace=
    177 index=
    178 create=
    179 
    180 while test -n "$action"
    181 do
    182   case $action in
    183     d*) delete=yes  ;;
    184     x*) extract=yes ;;
    185     t*) list=yes    ;;
    186     q*) quick=yes   ;;
    187     r*) replace=yes ;;
    188     s*) index=yes   ;;
    189     S*)             ;; # the index is always updated implicitly
    190     c*) create=yes  ;;
    191     u*)             ;; # TODO: don't ignore the update modifier
    192     v*)             ;; # TODO: don't ignore the verbose modifier
    193     *)
    194       func_error "unknown action specified"
    195       ;;
    196   esac
    197   action=${action#?}
    198 done
    199 
    200 case $delete$extract$list$quick$replace,$index in
    201   yes,* | ,yes)
    202     ;;
    203   yesyes*)
    204     func_error "more than one action specified"
    205     ;;
    206   *)
    207     func_error "no action specified"
    208     ;;
    209 esac
    210 
    211 if test -n "$delete"; then
    212   if test ! -f "$orig_archive"; then
    213     func_error "archive not found"
    214   fi
    215   for member
    216   do
    217     case $1 in
    218       @*)
    219         func_at_file "${1#@}" -REMOVE "$archive"
    220         ;;
    221       *)
    222         func_file_conv "$1"
    223         $AR -NOLOGO -REMOVE:"$file" "$archive" || exit $?
    224         ;;
    225     esac
    226   done
    227 
    228 elif test -n "$extract"; then
    229   if test ! -f "$orig_archive"; then
    230     func_error "archive not found"
    231   fi
    232   if test $# -gt 0; then
    233     for member
    234     do
    235       case $1 in
    236         @*)
    237           func_at_file "${1#@}" -EXTRACT "$archive"
    238           ;;
    239         *)
    240           func_file_conv "$1"
    241           $AR -NOLOGO -EXTRACT:"$file" "$archive" || exit $?
    242           ;;
    243       esac
    244     done
    245   else
    246     $AR -NOLOGO -LIST "$archive" | tr -d '\r' | sed -e 's/\\/\\\\/g' \
    247       | while read member
    248         do
    249           $AR -NOLOGO -EXTRACT:"$member" "$archive" || exit $?
    250         done
    251   fi
    252 
    253 elif test -n "$quick$replace"; then
    254   if test ! -f "$orig_archive"; then
    255     if test -z "$create"; then
    256       echo "$me: creating $orig_archive"
    257     fi
    258     orig_archive=
    259   else
    260     orig_archive=$archive
    261   fi
    262 
    263   for member
    264   do
    265     case $1 in
    266     @*)
    267       func_file_conv "${1#@}"
    268       set x "$@" "@$file"
    269       ;;
    270     *)
    271       func_file_conv "$1"
    272       set x "$@" "$file"
    273       ;;
    274     esac
    275     shift
    276     shift
    277   done
    278 
    279   if test -n "$orig_archive"; then
    280     $AR -NOLOGO -OUT:"$archive" "$orig_archive" "$@" || exit $?
    281   else
    282     $AR -NOLOGO -OUT:"$archive" "$@" || exit $?
    283   fi
    284 
    285 elif test -n "$list"; then
    286   if test ! -f "$orig_archive"; then
    287     func_error "archive not found"
    288   fi
    289   $AR -NOLOGO -LIST "$archive" || exit $?
    290 fi
    291