xref: /qemu/scripts/archive-source.sh (revision 474dcfc0ab093043fee9952282bcab5f1dedc43a)
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
12error() {
13    printf %s\\n "$*" >&2
14    exit 1
15}
16
17if test $# -lt 1; then
18    error "Usage: $0 <output tarball>"
19fi
20
21tar_file=$(realpath "$1")
22sub_tdir=$(mktemp -d "${tar_file%.tar}.sub.XXXXXXXX")
23sub_file="${sub_tdir}/submodule.tar"
24
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.
29subprojects="keycodemapdb libvfio-user berkeley-softfloat-3 berkeley-testfloat-3"
30sub_deinit=""
31
32function 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
41
42function 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}
50
51function subproject_dir() {
52    if test ! -f "subprojects/$1.wrap"; then
53      error "scripts/archive-source.sh should only process wrap subprojects"
54    fi
55
56    # Print the directory key of the wrap file, defaulting to the
57    # subproject name.  The wrap file is in ini format and should
58    # have a single section only.  There should be only one section
59    # named "[wrap-*]", which helps keeping the script simple.
60    local dir
61    dir=$(sed -n \
62      -e '/^\[wrap-[a-z][a-z]*\]$/,/^\[/{' \
63      -e    '/^directory *= */!b' \
64      -e    's///p' \
65      -e    'q' \
66      -e '}' \
67      "subprojects/$1.wrap")
68
69    echo "${dir:-$1}"
70}
71
72git archive --format tar "$(tree_ish)" > "$tar_file"
73test $? -ne 0 && error "failed to archive qemu"
74
75for sp in $subprojects; do
76    meson subprojects download $sp
77    test $? -ne 0 && error "failed to download subproject $sp"
78    tar --append --file "$tar_file" --exclude=.git subprojects/"$(subproject_dir $sp)"
79    test $? -ne 0 && error "failed to append subproject $sp to $tar_file"
80done
81exit 0
82