Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 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 | |
| 12 | error() { |
| 13 | printf %s\\n "$*" >&2 |
| 14 | exit 1 |
| 15 | } |
| 16 | |
| 17 | if test $# -lt 1; then |
| 18 | error "Usage: $0 <output tarball>" |
| 19 | fi |
| 20 | |
Mao Zhongyi | 934821e | 2018-10-15 17:17:34 +0800 | [diff] [blame] | 21 | tar_file=$(realpath "$1") |
Gerd Hoffmann | 8fc7617 | 2019-05-20 14:47:03 +0200 | [diff] [blame] | 22 | sub_tdir=$(mktemp -d "${tar_file%.tar}.sub.XXXXXXXX") |
| 23 | sub_file="${sub_tdir}/submodule.tar" |
Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 24 | |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 25 | # We want a predictable list of submodules for builds, that is |
| 26 | # independent of what the developer currently has initialized |
| 27 | # in their checkout, because the build environment is completely |
| 28 | # different to the host OS. |
Marc-André Lureau | 7c57bdd8 | 2019-04-24 13:00:41 +0200 | [diff] [blame] | 29 | submodules="dtc slirp ui/keycodemapdb tests/fp/berkeley-softfloat-3 tests/fp/berkeley-testfloat-3" |
Gerd Hoffmann | 8fc7617 | 2019-05-20 14:47:03 +0200 | [diff] [blame] | 30 | sub_deinit="" |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 31 | |
Gerd Hoffmann | 8fc7617 | 2019-05-20 14:47:03 +0200 | [diff] [blame] | 32 | function cleanup() { |
| 33 | local status=$? |
| 34 | rm -rf "$sub_tdir" |
| 35 | if test "$sub_deinit" != ""; then |
| 36 | git submodule deinit $sub_deinit |
| 37 | fi |
| 38 | exit $status |
| 39 | } |
| 40 | trap "cleanup" 0 1 2 3 15 |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 41 | |
Marc-André Lureau | 84963b5 | 2019-07-09 00:02:49 +0400 | [diff] [blame] | 42 | function tree_ish() { |
| 43 | local retval='HEAD' |
| 44 | if ! git diff-index --quiet --ignore-submodules=all HEAD -- &>/dev/null |
| 45 | then |
| 46 | retval=$(git stash create) |
| 47 | fi |
| 48 | echo "$retval" |
| 49 | } |
Gerd Hoffmann | 8fc7617 | 2019-05-20 14:47:03 +0200 | [diff] [blame] | 50 | |
Marc-André Lureau | 84963b5 | 2019-07-09 00:02:49 +0400 | [diff] [blame] | 51 | git archive --format tar "$(tree_ish)" > "$tar_file" |
Gerd Hoffmann | 8fc7617 | 2019-05-20 14:47:03 +0200 | [diff] [blame] | 52 | test $? -ne 0 && error "failed to archive qemu" |
Philippe Mathieu-Daudé | d3b4426 | 2019-01-24 02:00:23 +0100 | [diff] [blame] | 53 | for sm in $submodules; do |
Gerd Hoffmann | 8fc7617 | 2019-05-20 14:47:03 +0200 | [diff] [blame] | 54 | status="$(git submodule status "$sm")" |
| 55 | smhash="${status#[ +-]}" |
| 56 | smhash="${smhash%% *}" |
| 57 | case "$status" in |
| 58 | -*) |
| 59 | sub_deinit="$sub_deinit $sm" |
| 60 | git submodule update --init "$sm" |
| 61 | test $? -ne 0 && error "failed to update submodule $sm" |
| 62 | ;; |
| 63 | +*) |
| 64 | echo "WARNING: submodule $sm is out of sync" |
| 65 | ;; |
| 66 | esac |
Marc-André Lureau | 84963b5 | 2019-07-09 00:02:49 +0400 | [diff] [blame] | 67 | (cd $sm; git archive --format tar --prefix "$sm/" $(tree_ish)) > "$sub_file" |
Gerd Hoffmann | 8fc7617 | 2019-05-20 14:47:03 +0200 | [diff] [blame] | 68 | test $? -ne 0 && error "failed to archive submodule $sm ($smhash)" |
| 69 | tar --concatenate --file "$tar_file" "$sub_file" |
| 70 | test $? -ne 0 && error "failed append submodule $sm to $tar_file" |
Philippe Mathieu-Daudé | d3b4426 | 2019-01-24 02:00:23 +0100 | [diff] [blame] | 71 | done |
Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 72 | exit 0 |