archive-source.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. subprojects="keycodemapdb libvfio-user berkeley-softfloat-3
  26. berkeley-testfloat-3 proc-macro2-1-rs quote-1-rs
  27. syn-2-rs unicode-ident-1-rs"
  28. sub_deinit=""
  29. function cleanup() {
  30. local status=$?
  31. rm -rf "$sub_tdir"
  32. if test "$sub_deinit" != ""; then
  33. git submodule deinit $sub_deinit
  34. fi
  35. exit $status
  36. }
  37. trap "cleanup" 0 1 2 3 15
  38. function tree_ish() {
  39. local retval='HEAD'
  40. if ! git diff-index --quiet --ignore-submodules=all HEAD -- &>/dev/null
  41. then
  42. retval=$(git stash create)
  43. fi
  44. echo "$retval"
  45. }
  46. function subproject_dir() {
  47. if test ! -f "subprojects/$1.wrap"; then
  48. error "scripts/archive-source.sh should only process wrap subprojects"
  49. fi
  50. # Print the directory key of the wrap file, defaulting to the
  51. # subproject name. The wrap file is in ini format and should
  52. # have a single section only. There should be only one section
  53. # named "[wrap-*]", which helps keeping the script simple.
  54. local dir
  55. dir=$(sed -n \
  56. -e '/^\[wrap-[a-z][a-z]*\]$/,/^\[/{' \
  57. -e '/^directory *= */!b' \
  58. -e 's///p' \
  59. -e 'q' \
  60. -e '}' \
  61. "subprojects/$1.wrap")
  62. echo "${dir:-$1}"
  63. }
  64. git archive --format tar "$(tree_ish)" > "$tar_file"
  65. test $? -ne 0 && error "failed to archive qemu"
  66. for sp in $subprojects; do
  67. meson subprojects download $sp
  68. test $? -ne 0 && error "failed to download subproject $sp"
  69. tar --append --file "$tar_file" --exclude=.git subprojects/"$(subproject_dir $sp)"
  70. test $? -ne 0 && error "failed to append subproject $sp to $tar_file"
  71. done
  72. exit 0