Statically build QEMU

Downloads and builds QEMU 5.0.0. Uses the repo's toolchain's
clang, but the local ld for now.

To resolve issues finding the libraries on the CI servers.

Signed-off-by: Fuad Tabba <tabba@google.com>
Change-Id: I534b75ca9de950e7a06f363645790ab0eba654d1
diff --git a/Makefile b/Makefile
index 9b6507a..eaa09b9 100644
--- a/Makefile
+++ b/Makefile
@@ -211,13 +211,12 @@
 	cp $(BR_OUT)/images/rootfs.ext4 $@
 	cp $(BR_OUT)/per-package/host-qemu/host/share/qemu/efi-virtio.rom $@
 
-# The qemu binary and the libraries it requires
-prebuilts/linux-x86/qemu: buildroot
+# The qemu binary
+prebuilts/linux-x86/qemu:
 	- rm -rf $@
+	${SCRIPTS_DIR}/aarch64/build_qemu.sh $(OUT_DIR)/qemu ${TOOLCHAIN_CLANG}/bin
 	mkdir -p $@/bin
-	cp $(BR_OUT)/host/bin/qemu-system-aarch64 $@/bin
-	mkdir -p $@/lib
-	cp $(BR_OUT)/host/lib/lib* $@/lib
+	cp $(OUT_DIR)/qemu/qemu-system-aarch64 $@/bin
 
 
 ##
diff --git a/aarch64/build_qemu.sh b/aarch64/build_qemu.sh
new file mode 100755
index 0000000..4970cd4
--- /dev/null
+++ b/aarch64/build_qemu.sh
@@ -0,0 +1,61 @@
+#!/usr/bin/env bash
+
+# Copyright 2020 The Android KVM Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+source "$(dirname "${BASH_SOURCE[0]}")/../common.inc"
+
+QEMU_VERSION="qemu-5.0.0"
+OUT_DIR="$1"
+TOOLCHAIN_DIR="$2"
+
+CC="${TOOLCHAIN_DIR}/clang"
+CXX="${TOOLCHAIN_DIR}/clang++"
+OBJCC="${TOOLCHAIN_DIR}/clang"
+
+mkdir -p "${OUT_DIR}"
+cd "${OUT_DIR}"
+
+rm -rf "${QEMU_VERSION}".tar.xz "${QEMU_VERSION}"
+wget https://download.qemu.org/"${QEMU_VERSION}".tar.xz
+tar xJf "${QEMU_VERSION}".tar.xz
+cd "${QEMU_VERSION}"
+
+./configure \
+        --cc="${CC}" \
+        --host-cc="${CC}" \
+        --cxx="${CXX}" \
+        --objcc="${OBJCC}" \
+        --static \
+        --target-list=aarch64-softmmu \
+        --disable-gtk \
+        --disable-vnc \
+        --disable-vhost-net \
+        --disable-vhost-vsock \
+        --disable-vhost-user \
+        --disable-vhost-scsi \
+        --disable-vhost-crypto \
+        --disable-libxml2 \
+        --disable-libusb \
+        --disable-tpm \
+        --disable-kvm \
+        --disable-qom-cast-debug \
+        --disable-guest-agent \
+        --disable-replication \
+        --disable-live-block-migration \
+        --disable-blobs
+
+make -j`nproc`
+strip aarch64-softmmu/qemu-system-aarch64
+cp aarch64-softmmu/qemu-system-aarch64 ..
diff --git a/common.inc b/common.inc
index cb88053..c26a706 100644
--- a/common.inc
+++ b/common.inc
@@ -25,7 +25,7 @@
 PREBUILTS_DIR="${ROOT_DIR}/prebuilts"
 PREBUILTS_IMG_DIR="${PREBUILTS_DIR}/linux-aarch64/images"
 
-PREBUILTS_QEMU_BIN="${PREBUILTS_DIR}/linux-x86/qemu/bin/qemu-system-aarch64.sh"
+PREBUILTS_QEMU_BIN="${PREBUILTS_DIR}/linux-x86/qemu/bin/qemu-system-aarch64"
 PREBUILTS_QEMU_ROM_DIR="${PREBUILTS_IMG_DIR}"
 
 PREBUILTS_KUT_ROOTFS="${PREBUILTS_IMG_DIR}/rootfs.ext4"