blob: ca94e49978f153bd96792099f29e8a975669eb02 [file] [log] [blame]
Fam Zheng6b560c72017-09-05 10:11:52 +08001#!/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
12error() {
13 printf %s\\n "$*" >&2
14 exit 1
15}
16
17if test $# -lt 1; then
18 error "Usage: $0 <output tarball>"
19fi
20
Mao Zhongyi934821e2018-10-15 17:17:34 +080021tar_file=$(realpath "$1")
Gerd Hoffmann8fc76172019-05-20 14:47:03 +020022sub_tdir=$(mktemp -d "${tar_file%.tar}.sub.XXXXXXXX")
23sub_file="${sub_tdir}/submodule.tar"
Fam Zheng6b560c72017-09-05 10:11:52 +080024
Daniel P. Berrange47bb9082017-09-29 11:11:57 +010025# 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é Lureau7c57bdd2019-04-24 13:00:41 +020029submodules="dtc slirp ui/keycodemapdb tests/fp/berkeley-softfloat-3 tests/fp/berkeley-testfloat-3"
Gerd Hoffmann8fc76172019-05-20 14:47:03 +020030sub_deinit=""
Daniel P. Berrange47bb9082017-09-29 11:11:57 +010031
Gerd Hoffmann8fc76172019-05-20 14:47:03 +020032function 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}
40trap "cleanup" 0 1 2 3 15
Daniel P. Berrange47bb9082017-09-29 11:11:57 +010041
42if git diff-index --quiet HEAD -- &>/dev/null
43then
44 HEAD=HEAD
45else
Mao Zhongyi934821e2018-10-15 17:17:34 +080046 HEAD=$(git stash create)
Fam Zheng6b560c72017-09-05 10:11:52 +080047fi
Gerd Hoffmann8fc76172019-05-20 14:47:03 +020048
49git archive --format tar $HEAD > "$tar_file"
50test $? -ne 0 && error "failed to archive qemu"
Philippe Mathieu-Daudéd3b44262019-01-24 02:00:23 +010051for sm in $submodules; do
Gerd Hoffmann8fc76172019-05-20 14:47:03 +020052 status="$(git submodule status "$sm")"
53 smhash="${status#[ +-]}"
54 smhash="${smhash%% *}"
55 case "$status" in
56 -*)
57 sub_deinit="$sub_deinit $sm"
58 git submodule update --init "$sm"
59 test $? -ne 0 && error "failed to update submodule $sm"
60 ;;
61 +*)
62 echo "WARNING: submodule $sm is out of sync"
63 ;;
64 esac
65 (cd $sm; git archive --format tar --prefix "$sm/" $smhash) > "$sub_file"
66 test $? -ne 0 && error "failed to archive submodule $sm ($smhash)"
67 tar --concatenate --file "$tar_file" "$sub_file"
68 test $? -ne 0 && error "failed append submodule $sm to $tar_file"
Philippe Mathieu-Daudéd3b44262019-01-24 02:00:23 +010069done
Fam Zheng6b560c72017-09-05 10:11:52 +080070exit 0