Merge remote-tracking branch 'remotes/philmd-gitlab/tags/acceptance-fixes-20200407...
[qemu/ar7.git] / roms / edk2-funcs.sh
blobcd6e4f2c82d2b346cdb7b3199bcae426832f967e
1 # Shell script that defines functions for determining some environmental
2 # characteristics for the edk2 "build" utility.
4 # This script is meant to be sourced, in a bash environment.
6 # Copyright (C) 2019 Red Hat, Inc.
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>.
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.
17 # Verify whether the QEMU system emulation target is supported by the UEFI spec
18 # and edk2. Print a message to the standard error, and return with nonzero
19 # status, if verification fails.
21 # Parameters:
22 # $1: QEMU system emulation target
23 qemu_edk2_verify_arch()
25 local emulation_target="$1"
26 local program_name=$(basename -- "$0")
28 case "$emulation_target" in
29 (arm|aarch64|i386|x86_64)
31 (*)
32 printf '%s: unknown/unsupported QEMU system emulation target "%s"\n' \
33 "$program_name" "$emulation_target" >&2
34 return 1
36 esac
40 # Translate the QEMU system emulation target to the edk2 architecture
41 # identifier. Print the result to the standard output.
43 # Parameters:
44 # $1: QEMU system emulation target
45 qemu_edk2_get_arch()
47 local emulation_target="$1"
49 if ! qemu_edk2_verify_arch "$emulation_target"; then
50 return 1
53 case "$emulation_target" in
54 (arm)
55 printf 'ARM\n'
57 (aarch64)
58 printf 'AARCH64\n'
60 (i386)
61 printf 'IA32\n'
63 (x86_64)
64 printf 'X64\n'
66 esac
70 # Translate the QEMU system emulation target to the gcc cross-compilation
71 # architecture identifier. Print the result to the standard output.
73 # Parameters:
74 # $1: QEMU system emulation target
75 qemu_edk2_get_gcc_arch()
77 local emulation_target="$1"
79 if ! qemu_edk2_verify_arch "$emulation_target"; then
80 return 1
83 case "$emulation_target" in
84 (arm|aarch64|x86_64)
85 printf '%s\n' "$emulation_target"
87 (i386)
88 printf 'i686\n'
90 esac
94 # Determine the gcc cross-compiler prefix (if any) for use with the edk2
95 # toolchain. Print the result to the standard output.
97 # Parameters:
98 # $1: QEMU system emulation target
99 qemu_edk2_get_cross_prefix()
101 local emulation_target="$1"
102 local gcc_arch
103 local host_arch
105 if ! gcc_arch=$(qemu_edk2_get_gcc_arch "$emulation_target"); then
106 return 1
109 host_arch=$(uname -m)
111 if [ "$gcc_arch" == "$host_arch" ] ||
112 ( [ "$gcc_arch" == i686 ] && [ "$host_arch" == x86_64 ] ); then
113 # no cross-compiler needed
115 elif ( [ -e /etc/debian_version ] && [ "$gcc_arch" == arm ] ); then
116 # force soft-float cross-compiler on Debian
117 printf 'arm-linux-gnueabi-'
118 else
119 printf '%s-linux-gnu-\n' "$gcc_arch"
124 # Determine the edk2 toolchain tag for the QEMU system emulation target. Print
125 # the result to the standard output. Print a message to the standard error, and
126 # return with nonzero status, if the (conditional) gcc version check fails.
128 # Parameters:
129 # $1: QEMU system emulation target
130 qemu_edk2_get_toolchain()
132 local emulation_target="$1"
133 local program_name=$(basename -- "$0")
134 local cross_prefix
135 local gcc_version
137 if ! qemu_edk2_verify_arch "$emulation_target"; then
138 return 1
141 case "$emulation_target" in
142 (arm|aarch64)
143 printf 'GCC5\n'
146 (i386|x86_64)
147 if ! cross_prefix=$(qemu_edk2_get_cross_prefix "$emulation_target"); then
148 return 1
151 gcc_version=$("${cross_prefix}gcc" -v 2>&1 | tail -1 | awk '{print $3}')
152 # Run "git-blame" on "OvmfPkg/build.sh" in edk2 for more information on
153 # the mapping below.
154 case "$gcc_version" in
155 ([1-3].*|4.[0-7].*)
156 printf '%s: unsupported gcc version "%s"\n' \
157 "$program_name" "$gcc_version" >&2
158 return 1
160 (4.8.*)
161 printf 'GCC48\n'
163 (4.9.*|6.[0-2].*)
164 printf 'GCC49\n'
167 printf 'GCC5\n'
169 esac
171 esac
175 # Determine the name of the environment variable that exposes the
176 # cross-compiler prefix to the edk2 "build" utility. Print the result to the
177 # standard output.
179 # Parameters:
180 # $1: QEMU system emulation target
181 qemu_edk2_get_cross_prefix_var()
183 local emulation_target="$1"
184 local edk2_toolchain
185 local edk2_arch
187 if ! edk2_toolchain=$(qemu_edk2_get_toolchain "$emulation_target"); then
188 return 1
191 case "$emulation_target" in
192 (arm|aarch64)
193 if ! edk2_arch=$(qemu_edk2_get_arch "$emulation_target"); then
194 return 1
196 printf '%s_%s_PREFIX\n' "$edk2_toolchain" "$edk2_arch"
198 (i386|x86_64)
199 printf '%s_BIN\n' "$edk2_toolchain"
201 esac
205 # Set and export the environment variable(s) necessary for cross-compilation,
206 # whenever needed by the edk2 "build" utility.
208 # Parameters:
209 # $1: QEMU system emulation target
210 qemu_edk2_set_cross_env()
212 local emulation_target="$1"
213 local cross_prefix
214 local cross_prefix_var
216 if ! cross_prefix=$(qemu_edk2_get_cross_prefix "$emulation_target"); then
217 return 1
220 if [ -z "$cross_prefix" ]; then
221 # Nothing to do.
222 return 0
225 if ! cross_prefix_var=$(qemu_edk2_get_cross_prefix_var \
226 "$emulation_target"); then
227 return 1
230 eval "export $cross_prefix_var=\$cross_prefix"
234 # Determine the "-n" option argument (that is, the number of modules to build
235 # in parallel) for the edk2 "build" utility. Print the result to the standard
236 # output.
238 # Parameters:
239 # $1: the value of the MAKEFLAGS variable
240 qemu_edk2_get_thread_count()
242 local makeflags="$1"
244 if [[ "$makeflags" == *--jobserver-auth=* ]] ||
245 [[ "$makeflags" == *--jobserver-fds=* ]]; then
246 # If there is a job server, allow the edk2 "build" utility to parallelize
247 # as many module builds as there are logical CPUs in the system. The "make"
248 # instances forked by "build" are supposed to limit themselves through the
249 # job server. The zero value below causes the edk2 "build" utility to fetch
250 # the logical CPU count with Python's multiprocessing.cpu_count() method.
251 printf '0\n'
252 else
253 # Build a single module at a time.
254 printf '1\n'
259 # Work around <https://bugzilla.tianocore.org/show_bug.cgi?id=1607> by
260 # filtering jobserver-related flags out of MAKEFLAGS. Print the result to the
261 # standard output.
263 # Parameters:
264 # $1: the value of the MAKEFLAGS variable
265 qemu_edk2_quirk_tianocore_1607()
267 local makeflags="$1"
269 printf %s "$makeflags" \
270 | LC_ALL=C sed --regexp-extended \
271 --expression='s/--jobserver-(auth|fds)=[0-9]+,[0-9]+//' \
272 --expression='s/-j([0-9]+)?//'