archive-source.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if git diff-index --quiet HEAD -- &>/dev/null
  37. then
  38. HEAD=HEAD
  39. else
  40. HEAD=$(git stash create)
  41. fi
  42. git archive --format tar $HEAD > "$tar_file"
  43. test $? -ne 0 && error "failed to archive qemu"
  44. for sm in $submodules; do
  45. status="$(git submodule status "$sm")"
  46. smhash="${status#[ +-]}"
  47. smhash="${smhash%% *}"
  48. case "$status" in
  49. -*)
  50. sub_deinit="$sub_deinit $sm"
  51. git submodule update --init "$sm"
  52. test $? -ne 0 && error "failed to update submodule $sm"
  53. ;;
  54. +*)
  55. echo "WARNING: submodule $sm is out of sync"
  56. ;;
  57. esac
  58. (cd $sm; git archive --format tar --prefix "$sm/" $smhash) > "$sub_file"
  59. test $? -ne 0 && error "failed to archive submodule $sm ($smhash)"
  60. tar --concatenate --file "$tar_file" "$sub_file"
  61. test $? -ne 0 && error "failed append submodule $sm to $tar_file"
  62. done
  63. exit 0