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