archive-source.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 meson ui/keycodemapdb"
  26. submodules="$submodules tests/fp/berkeley-softfloat-3 tests/fp/berkeley-testfloat-3"
  27. sub_deinit=""
  28. function cleanup() {
  29. local status=$?
  30. rm -rf "$sub_tdir"
  31. if test "$sub_deinit" != ""; then
  32. git submodule deinit $sub_deinit
  33. fi
  34. exit $status
  35. }
  36. trap "cleanup" 0 1 2 3 15
  37. function tree_ish() {
  38. local retval='HEAD'
  39. if ! git diff-index --quiet --ignore-submodules=all HEAD -- &>/dev/null
  40. then
  41. retval=$(git stash create)
  42. fi
  43. echo "$retval"
  44. }
  45. git archive --format tar "$(tree_ish)" > "$tar_file"
  46. test $? -ne 0 && error "failed to archive qemu"
  47. for sm in $submodules; do
  48. status="$(git submodule status "$sm")"
  49. smhash="${status#[ +-]}"
  50. smhash="${smhash%% *}"
  51. case "$status" in
  52. -*)
  53. sub_deinit="$sub_deinit $sm"
  54. git submodule update --init "$sm"
  55. test $? -ne 0 && error "failed to update submodule $sm"
  56. ;;
  57. +*)
  58. echo "WARNING: submodule $sm is out of sync"
  59. ;;
  60. esac
  61. (cd $sm; git archive --format tar --prefix "$sm/" $(tree_ish)) > "$sub_file"
  62. test $? -ne 0 && error "failed to archive submodule $sm ($smhash)"
  63. tar --concatenate --file "$tar_file" "$sub_file"
  64. test $? -ne 0 && error "failed append submodule $sm to $tar_file"
  65. done
  66. exit 0