2
0

archive-source.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. #
  3. # Author: Fam Zheng <famz@redhat.com>
  4. #
  5. # Archive source tree, including submodules. This is created for test code to
  6. # export the source files, in order to be built in a different environment,
  7. # such as in a docker instance or VM.
  8. #
  9. # This code is licensed under the GPL version 2 or later. See
  10. # the COPYING file in the top-level directory.
  11. error() {
  12. printf %s\\n "$*" >&2
  13. exit 1
  14. }
  15. if test $# -lt 1; then
  16. error "Usage: $0 <output tarball>"
  17. fi
  18. tar_file=$(realpath "$1")
  19. sub_tdir=$(mktemp -d "${tar_file%.tar}.sub.XXXXXXXX")
  20. sub_file="${sub_tdir}/submodule.tar"
  21. # We want a predictable list of submodules for builds, that is
  22. # independent of what the developer currently has initialized
  23. # in their checkout, because the build environment is completely
  24. # different to the host OS.
  25. submodules="dtc slirp ui/keycodemapdb tests/fp/berkeley-softfloat-3 tests/fp/berkeley-testfloat-3"
  26. sub_deinit=""
  27. function cleanup() {
  28. local status=$?
  29. rm -rf "$sub_tdir"
  30. if test "$sub_deinit" != ""; then
  31. git submodule deinit $sub_deinit
  32. fi
  33. exit $status
  34. }
  35. trap "cleanup" 0 1 2 3 15
  36. function tree_ish() {
  37. local retval='HEAD'
  38. if ! git diff-index --quiet --ignore-submodules=all HEAD -- &>/dev/null
  39. then
  40. retval=$(git stash create)
  41. fi
  42. echo "$retval"
  43. }
  44. git archive --format tar "$(tree_ish)" > "$tar_file"
  45. test $? -ne 0 && error "failed to archive qemu"
  46. for sm in $submodules; do
  47. status="$(git submodule status "$sm")"
  48. smhash="${status#[ +-]}"
  49. smhash="${smhash%% *}"
  50. case "$status" in
  51. -*)
  52. sub_deinit="$sub_deinit $sm"
  53. git submodule update --init "$sm"
  54. test $? -ne 0 && error "failed to update submodule $sm"
  55. ;;
  56. +*)
  57. echo "WARNING: submodule $sm is out of sync"
  58. ;;
  59. esac
  60. (cd $sm; git archive --format tar --prefix "$sm/" $(tree_ish)) > "$sub_file"
  61. test $? -ne 0 && error "failed to archive submodule $sm ($smhash)"
  62. tar --concatenate --file "$tar_file" "$sub_file"
  63. test $? -ne 0 && error "failed append submodule $sm to $tar_file"
  64. done
  65. exit 0