xref: /openbmc/qemu/tests/uefi-test-tools/build.sh (revision 40d6ee94)
1#!/bin/bash
2
3# Build script that determines the edk2 toolchain to use, invokes the edk2
4# "build" utility, and copies the built UEFI binary to the requested location.
5#
6# Copyright (C) 2019, Red Hat, Inc.
7#
8# This program and the accompanying materials are licensed and made available
9# under the terms and conditions of the BSD License that accompanies this
10# distribution. The full text of the license may be found at
11# <http://opensource.org/licenses/bsd-license.php>.
12#
13# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14# WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16set -e -u -C
17
18# Save the command line arguments. We need to reset $# to 0 before sourcing
19# "edksetup.sh", as it will inherit $@.
20program_name=$(basename -- "$0")
21edk2_dir=$1
22dsc_component=$2
23emulation_target=$3
24uefi_binary=$4
25shift 4
26
27# Set up the environment for edk2 building.
28export PACKAGES_PATH=$(realpath -- "$edk2_dir")
29export WORKSPACE=$PWD
30mkdir -p Conf
31
32# Source "edksetup.sh" carefully.
33set +e +u +C
34source "$PACKAGES_PATH/edksetup.sh"
35ret=$?
36set -e -u -C
37if [ $ret -ne 0 ]; then
38  exit $ret
39fi
40
41# Map the QEMU system emulation target to the following types of architecture
42# identifiers:
43# - edk2,
44# - gcc cross-compilation.
45# Cover only those targets that are supported by the UEFI spec and edk2.
46case "$emulation_target" in
47  (arm)
48    edk2_arch=ARM
49    gcc_arch=arm
50    ;;
51  (aarch64)
52    edk2_arch=AARCH64
53    gcc_arch=aarch64
54    ;;
55  (i386)
56    edk2_arch=IA32
57    gcc_arch=i686
58    ;;
59  (x86_64)
60    edk2_arch=X64
61    gcc_arch=x86_64
62    ;;
63  (*)
64    printf '%s: unknown/unsupported QEMU system emulation target "%s"\n' \
65      "$program_name" "$emulation_target" >&2
66    exit 1
67    ;;
68esac
69
70# Check if cross-compilation is needed.
71host_arch=$(uname -m)
72if [ "$gcc_arch" == "$host_arch" ] ||
73   ( [ "$gcc_arch" == i686 ] && [ "$host_arch" == x86_64 ] ); then
74  cross_prefix=
75else
76  cross_prefix=${gcc_arch}-linux-gnu-
77fi
78
79# Expose cross_prefix (which is possibly empty) to the edk2 tools. While at it,
80# determine the suitable edk2 toolchain as well.
81# - For ARM and AARCH64, edk2 only offers the GCC5 toolchain tag, which covers
82#   the gcc-5+ releases.
83# - For IA32 and X64, edk2 offers the GCC44 through GCC49 toolchain tags, in
84#   addition to GCC5. Unfortunately, the mapping between the toolchain tags and
85#   the actual gcc releases isn't entirely trivial. Run "git-blame" on
86#   "OvmfPkg/build.sh" in edk2 for more information.
87# And, because the above is too simple, we have to assign cross_prefix to an
88# edk2 build variable that is specific to both the toolchain tag and the target
89# architecture.
90case "$edk2_arch" in
91  (ARM)
92    edk2_toolchain=GCC5
93    export GCC5_ARM_PREFIX=$cross_prefix
94    ;;
95  (AARCH64)
96    edk2_toolchain=GCC5
97    export GCC5_AARCH64_PREFIX=$cross_prefix
98    ;;
99  (IA32|X64)
100    gcc_version=$("${cross_prefix}gcc" -v 2>&1 | tail -1 | awk '{print $3}')
101    case "$gcc_version" in
102      ([1-3].*|4.[0-3].*)
103        printf '%s: unsupported gcc version "%s"\n' \
104          "$program_name" "$gcc_version" >&2
105        exit 1
106        ;;
107      (4.4.*)
108        edk2_toolchain=GCC44
109        ;;
110      (4.5.*)
111        edk2_toolchain=GCC45
112        ;;
113      (4.6.*)
114        edk2_toolchain=GCC46
115        ;;
116      (4.7.*)
117        edk2_toolchain=GCC47
118        ;;
119      (4.8.*)
120        edk2_toolchain=GCC48
121        ;;
122      (4.9.*|6.[0-2].*)
123        edk2_toolchain=GCC49
124        ;;
125      (*)
126        edk2_toolchain=GCC5
127        ;;
128    esac
129    eval "export ${edk2_toolchain}_BIN=\$cross_prefix"
130    ;;
131esac
132
133# Build the UEFI binary
134mkdir -p log
135build \
136  --arch="$edk2_arch" \
137  --buildtarget=DEBUG \
138  --platform=UefiTestToolsPkg/UefiTestToolsPkg.dsc \
139  --tagname="$edk2_toolchain" \
140  --module="UefiTestToolsPkg/$dsc_component/$dsc_component.inf" \
141  --log="log/$dsc_component.$edk2_arch.log" \
142  --report-file="log/$dsc_component.$edk2_arch.report"
143cp -a -- \
144  "Build/UefiTestTools/DEBUG_${edk2_toolchain}/$edk2_arch/$dsc_component.efi" \
145  "$uefi_binary"
146