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