ar-lib 5.7 KB

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