archive_repack.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash -e
  2. # Copyright 2019 Google Inc. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Script to extract and repack an archive with specified object files.
  16. # Inputs:
  17. # Environment:
  18. # CLANG_BIN: path to the clang bin directory
  19. # Arguments:
  20. # -i ${file}: input file
  21. # -o ${file}: output file
  22. # -d ${file}: deps file
  23. set -o pipefail
  24. OPTSTRING=d:i:o:
  25. usage() {
  26. cat <<EOF
  27. Usage: archive_repack.sh [options] <objects to repack>
  28. OPTIONS:
  29. -i <file>: input file
  30. -o <file>: output file
  31. -d <file>: deps file
  32. EOF
  33. exit 1
  34. }
  35. while getopts $OPTSTRING opt; do
  36. case "$opt" in
  37. d) depsfile="${OPTARG}" ;;
  38. i) infile="${OPTARG}" ;;
  39. o) outfile="${OPTARG}" ;;
  40. ?) usage ;;
  41. esac
  42. done
  43. shift "$(($OPTIND -1))"
  44. if [ -z "${infile}" ]; then
  45. echo "-i argument is required"
  46. usage
  47. fi
  48. if [ -z "${outfile}" ]; then
  49. echo "-o argument is required"
  50. usage
  51. fi
  52. # Produce deps file
  53. if [ ! -z "${depsfile}" ]; then
  54. cat <<EOF > "${depsfile}"
  55. ${outfile}: ${infile} ${CLANG_BIN}/llvm-ar
  56. EOF
  57. fi
  58. # Get absolute path for outfile and llvm-ar.
  59. LLVM_AR="${PWD}/${CLANG_BIN}/llvm-ar"
  60. if [[ "$outfile" != /* ]]; then
  61. outfile="${PWD}/${outfile}"
  62. fi
  63. tempdir="${outfile}.tmp"
  64. # Clean up any previous temporary files.
  65. rm -f "${outfile}"
  66. rm -rf "${tempdir}"
  67. # Do repack
  68. # We have to change working directory since ar only allows extracting to CWD.
  69. mkdir "${tempdir}"
  70. cp "${infile}" "${tempdir}/archive"
  71. cd "${tempdir}"
  72. "${LLVM_AR}" x "archive"
  73. "${LLVM_AR}" --format=gnu qc "${outfile}" "$@"