aboutsummaryrefslogtreecommitdiff
path: root/roms/edk2-funcs.sh
blob: cd6e4f2c82d2b346cdb7b3199bcae426832f967e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Shell script that defines functions for determining some environmental
# characteristics for the edk2 "build" utility.
#
# This script is meant to be sourced, in a bash environment.
#
# Copyright (C) 2019 Red Hat, Inc.
#
# This program and the accompanying materials are licensed and made available
# under the terms and conditions of the BSD License that accompanies this
# distribution. The full text of the license may be found at
# <http://opensource.org/licenses/bsd-license.php>.
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
# WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.


# Verify whether the QEMU system emulation target is supported by the UEFI spec
# and edk2. Print a message to the standard error, and return with nonzero
# status, if verification fails.
#
# Parameters:
#   $1: QEMU system emulation target
qemu_edk2_verify_arch()
{
  local emulation_target="$1"
  local program_name=$(basename -- "$0")

  case "$emulation_target" in
    (arm|aarch64|i386|x86_64)
      ;;
    (*)
      printf '%s: unknown/unsupported QEMU system emulation target "%s"\n' \
        "$program_name" "$emulation_target" >&2
      return 1
      ;;
  esac
}


# Translate the QEMU system emulation target to the edk2 architecture
# identifier. Print the result to the standard output.
#
# Parameters:
#   $1: QEMU system emulation target
qemu_edk2_get_arch()
{
  local emulation_target="$1"

  if ! qemu_edk2_verify_arch "$emulation_target"; then
    return 1
  fi

  case "$emulation_target" in
    (arm)
      printf 'ARM\n'
      ;;
    (aarch64)
      printf 'AARCH64\n'
      ;;
    (i386)
      printf 'IA32\n'
      ;;
    (x86_64)
      printf 'X64\n'
      ;;
  esac
}


# Translate the QEMU system emulation target to the gcc cross-compilation
# architecture identifier. Print the result to the standard output.
#
# Parameters:
#   $1: QEMU system emulation target
qemu_edk2_get_gcc_arch()
{
  local emulation_target="$1"

  if ! qemu_edk2_verify_arch "$emulation_target"; then
    return 1
  fi

  case "$emulation_target" in
    (arm|aarch64|x86_64)
      printf '%s\n' "$emulation_target"
      ;;
    (i386)
      printf 'i686\n'
      ;;
  esac
}


# Determine the gcc cross-compiler prefix (if any) for use with the edk2
# toolchain. Print the result to the standard output.
#
# Parameters:
#   $1: QEMU system emulation target
qemu_edk2_get_cross_prefix()
{
  local emulation_target="$1"
  local gcc_arch
  local host_arch

  if ! gcc_arch=$(qemu_edk2_get_gcc_arch "$emulation_target"); then
    return 1
  fi

  host_arch=$(uname -m)

  if [ "$gcc_arch" == "$host_arch" ] ||
     ( [ "$gcc_arch" == i686 ] && [ "$host_arch" == x86_64 ] ); then
    # no cross-compiler needed
    :
  elif ( [ -e /etc/debian_version ] && [ "$gcc_arch" == arm ] ); then
    # force soft-float cross-compiler on Debian
    printf 'arm-linux-gnueabi-'
  else
    printf '%s-linux-gnu-\n' "$gcc_arch"
  fi
}


# Determine the edk2 toolchain tag for the QEMU system emulation target. Print
# the result to the standard output. Print a message to the standard error, and
# return with nonzero status, if the (conditional) gcc version check fails.
#
# Parameters:
#   $1: QEMU system emulation target
qemu_edk2_get_toolchain()
{
  local emulation_target="$1"
  local program_name=$(basename -- "$0")
  local cross_prefix
  local gcc_version

  if ! qemu_edk2_verify_arch "$emulation_target"; then
    return 1
  fi

  case "$emulation_target" in
    (arm|aarch64)
      printf 'GCC5\n'
      ;;

    (i386|x86_64)
      if ! cross_prefix=$(qemu_edk2_get_cross_prefix "$emulation_target"); then
        return 1
      fi

      gcc_version=$("${cross_prefix}gcc" -v 2>&1 | tail -1 | awk '{print $3}')
      # Run "git-blame" on "OvmfPkg/build.sh" in edk2 for more information on
      # the mapping below.
      case "$gcc_version" in
        ([1-3].*|4.[0-7].*)
          printf '%s: unsupported gcc version "%s"\n' \
            "$program_name" "$gcc_version" >&2
          return 1
          ;;
        (4.8.*)
          printf 'GCC48\n'
          ;;
        (4.9.*|6.[0-2].*)
          printf 'GCC49\n'
          ;;
        (*)
          printf 'GCC5\n'
          ;;
      esac
      ;;
  esac
}


# Determine the name of the environment variable that exposes the
# cross-compiler prefix to the edk2 "build" utility. Print the result to the
# standard output.
#
# Parameters:
#   $1: QEMU system emulation target
qemu_edk2_get_cross_prefix_var()
{
  local emulation_target="$1"
  local edk2_toolchain
  local edk2_arch

  if ! edk2_toolchain=$(qemu_edk2_get_toolchain "$emulation_target"); then
    return 1
  fi

  case "$emulation_target" in
    (arm|aarch64)
      if ! edk2_arch=$(qemu_edk2_get_arch "$emulation_target"); then
        return 1
      fi
      printf '%s_%s_PREFIX\n' "$edk2_toolchain" "$edk2_arch"
      ;;
    (i386|x86_64)
      printf '%s_BIN\n' "$edk2_toolchain"
      ;;
  esac
}


# Set and export the environment variable(s) necessary for cross-compilation,
# whenever needed by the edk2 "build" utility.
#
# Parameters:
#   $1: QEMU system emulation target
qemu_edk2_set_cross_env()
{
  local emulation_target="$1"
  local cross_prefix
  local cross_prefix_var

  if ! cross_prefix=$(qemu_edk2_get_cross_prefix "$emulation_target"); then
    return 1
  fi

  if [ -z "$cross_prefix" ]; then
    # Nothing to do.
    return 0
  fi

  if ! cross_prefix_var=$(qemu_edk2_get_cross_prefix_var \
                            "$emulation_target"); then
    return 1
  fi

  eval "export $cross_prefix_var=\$cross_prefix"
}


# Determine the "-n" option argument (that is, the number of modules to build
# in parallel) for the edk2 "build" utility. Print the result to the standard
# output.
#
# Parameters:
#   $1: the value of the MAKEFLAGS variable
qemu_edk2_get_thread_count()
{
  local makeflags="$1"

  if [[ "$makeflags" == *--jobserver-auth=* ]] ||
     [[ "$makeflags" == *--jobserver-fds=* ]]; then
    # If there is a job server, allow the edk2 "build" utility to parallelize
    # as many module builds as there are logical CPUs in the system. The "make"
    # instances forked by "build" are supposed to limit themselves through the
    # job server. The zero value below causes the edk2 "build" utility to fetch
    # the logical CPU count with Python's multiprocessing.cpu_count() method.
    printf '0\n'
  else
    # Build a single module at a time.
    printf '1\n'
  fi
}


# Work around <https://bugzilla.tianocore.org/show_bug.cgi?id=1607> by
# filtering jobserver-related flags out of MAKEFLAGS. Print the result to the
# standard output.
#
# Parameters:
#   $1: the value of the MAKEFLAGS variable
qemu_edk2_quirk_tianocore_1607()
{
  local makeflags="$1"

  printf %s "$makeflags" \
  | LC_ALL=C sed --regexp-extended \
      --expression='s/--jobserver-(auth|fds)=[0-9]+,[0-9]+//' \
      --expression='s/-j([0-9]+)?//'
}