roms/edk2-build.sh: Allow to run edk2-build.sh from command line
PATCH 1/1 roms/edk2-build.sh: Allow to run edk2-build.sh from command line
The edk2-build.sh script set the 'nounset' option:
BASH(1)
set [arg ...]
-u Treat unset variables and parameters other than the
special parameters "@" and "*" as an error when performing parameter expansion. If expansion is attempted on an unset variable or parameter, the shell prints an error message, and, if not interactive, exits with a non-zero status.
When running this script out of 'make', we get:
$ cd roms $ ./edk2-build.sh aarch64 --arch=AARCH64 --platform=ArmVirtPkg/ArmVirtQemu.dsc > /dev/null ./edk2-build.sh: line 46: MAKEFLAGS: unbound variable
Fix this by checking the variable is defined before using it, else use a default value.
--- roms/edk2-build.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
roms/edk2-build.sh +7 -1
@@ -43,7 +43,13 @@ fi # any), for the edk2 "build" utility. source ../edk2-funcs.sh edk2_toolchain=$(qemu_edk2_get_toolchain "$emulation_target") -edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +if [ -v MAKEFLAGS ]; then + edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +else + # We are not running within 'make', let the edk2 "build" utility to fetch + # the logical CPU count with Python's multiprocessing.cpu_count() method. + edk2_thread_count=0 +fi qemu_edk2_set_cross_env "$emulation_target" # Build the platform firmware.
Signature · 1 line
2.20.1
Cc'ing Eric :)
Quoted 47 lines · Philippe Mathieu-Daudé
The
edk2-build.shscript set the 'nounset' option:BASH(1)
set [arg ...]
-u Treat unset variables and parameters other than the
special parameters "@" and "*" as an error when performing parameter expansion. If expansion is attempted on an unset variable or parameter, the shell prints an error message, and, if not interactive, exits with a non-zero status.When running this script out of 'make', we get:
$ cd roms $ ./edk2-build.sh aarch64 --arch=AARCH64 --platform=ArmVirtPkg/ArmVirtQemu.dsc > /dev/null ./edk2-build.sh: line 46: MAKEFLAGS: unbound variableFix this by checking the variable is defined before using it, else use a default value.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- roms/edk2-build.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)roms/edk2-build.sh +7
-1@@ -43,7 +43,13 @@ fi # any), for the edk2 "build" utility. source ../edk2-funcs.sh edk2_toolchain=$(qemu_edk2_get_toolchain "$emulation_target") -edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +if [ -v MAKEFLAGS ]; then + edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +else + # We are not running within 'make', let the edk2 "build" utility to fetch + # the logical CPU count with Python's multiprocessing.cpu_count() method. + edk2_thread_count=0 +fi qemu_edk2_set_cross_env "$emulation_target" # Build the platform firmware.
Quoted 17 lines · Philippe Mathieu-Daudé
Cc'ing Eric :)
Quoted 14 lines
When running this script out of 'make', we get:
$ cd roms $ ./edk2-build.sh aarch64 --arch=AARCH64 --platform=ArmVirtPkg/ArmVirtQemu.dsc > /dev/null ./edk2-build.sh: line 46: MAKEFLAGS: unbound variableFix this by checking the variable is defined before using it, else use a default value.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- roms/edk2-build.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 4 lines
roms/edk2-build.sh
Quoted 4 lines
roms/edk2-build.sh +0
-0
This is running under /bin/bash (hmm - not '/bin/env bash' like other scripts in qemu?), so...
Quoted 6 lines
roms/edk2-build.sh fi · line 46
Quoted 6 lines
roms/edk2-build.sh +1
-1@@ -43,7 +43,13 @@ fi # any), for the edk2 "build" utility. source ../edk2-funcs.sh edk2_toolchain=$(qemu_edk2_get_toolchain "$emulation_target") -edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +if [ -v MAKEFLAGS ]; then
the non-portable bashism '[ -v' works. However, it's just as easy to work around this problem portably for all POSIX shells without needing 'if':
Quoted 6 lines
roms/edk2-build.sh fi · line 52
Quoted 6 lines
roms/edk2-build.sh +6
-0+ edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +else + # We are not running within 'make', let the edk2 "build" utility to fetch + # the logical CPU count with Python's multiprocessing.cpu_count() method. + edk2_thread_count=0 +fi
edk2_thread_count=$(qemu_edk2_get_thread_count "${MAKEFLAGS:-0}")
at which point the really long comment needs a bit of a tweak.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Quoted 26 lines · Eric Blake
Quoted 17 lines · Philippe Mathieu-Daudé
Cc'ing Eric :)
Quoted 14 lines
When running this script out of 'make', we get:
$ cd roms $ ./edk2-build.sh aarch64 --arch=AARCH64 --platform=ArmVirtPkg/ArmVirtQemu.dsc > /dev/null ./edk2-build.sh: line 46: MAKEFLAGS: unbound variableFix this by checking the variable is defined before using it, else use a default value.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- roms/edk2-build.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)Quoted 4 lines
roms/edk2-build.sh
Quoted 4 lines
roms/edk2-build.sh +0
-0This is running under
/bin/bash(hmm - not '/bin/envbash' like other scripts in qemu?), so...
Quoted 8 lines
Quoted 6 lines
roms/edk2-build.sh fi · line 46
Quoted 6 lines
roms/edk2-build.sh +1
-1@@ -43,7 +43,13 @@ fi # any), for the edk2 "build" utility. source ../edk2-funcs.sh edk2_toolchain=$(qemu_edk2_get_toolchain "$emulation_target") -edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +if [ -v MAKEFLAGS ]; thenthe non-portable bashism '[ -v' works. However, it's just as easy to
Ah, OK.
Quoted 10 lines
work around this problem portably for all POSIX shells without needing 'if':
Quoted 6 lines
roms/edk2-build.sh fi · line 52
Quoted 6 lines
roms/edk2-build.sh +6
-0+ edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +else + # We are not running within 'make', let the edk2 "build" utility to fetch + # the logical CPU count with Python's multiprocessing.cpu_count() method. + edk2_thread_count=0 +fiedk2_thread_count=$(qemu_edk2_get_thread_count "${MAKEFLAGS:-0}")
Argh I'm confuse, this is what I wanted to do first but I couldn't get it working, maybe I forget the '-'.
Thanks a lot for your help, the result is way more elegant :)
Quoted 3 lines
at which point the really long comment needs a bit of a tweak.
Laszlo Ersek
Reviewed-byQuoted 11 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Eric Blake
Quoted 9 lines · Philippe Mathieu-Daudé
Cc'ing Eric :)
Quoted 6 lines
When running this script out of 'make', we get:
$ cd roms $ ./edk2-build.sh aarch64 --arch=AARCH64 --platform=ArmVirtPkg/ArmVirtQemu.dsc > /dev/null ./edk2-build.sh: line 46: MAKEFLAGS: unbound variable
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Fix this by checking the variable is defined before using it, else use a default value.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- roms/edk2-build.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
(1) "allow to run" is not correct English, to my understanding. This form of "allow" requires an object. You could reformulate the subject line as "allow edk2-build.sh to be invoked from the command line".
Quoted 19 lines
Quoted 8 lines
Quoted 4 lines
roms/edk2-build.sh
Quoted 4 lines
roms/edk2-build.sh +0
-0This is running under
/bin/bash(hmm - not '/bin/envbash' like other scripts in qemu?), so...Quoted 8 lines
Quoted 6 lines
roms/edk2-build.sh fi · line 46
Quoted 6 lines
roms/edk2-build.sh +1
-1@@ -43,7 +43,13 @@ fi # any), for the edk2 "build" utility. source ../edk2-funcs.sh edk2_toolchain=$(qemu_edk2_get_toolchain "$emulation_target") -edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +if [ -v MAKEFLAGS ]; thenthe non-portable bashism '[ -v' works. However, it's just as easy to
Ah, OK.
Quoted 8 lines
Quoted 8 lines
work around this problem portably for all POSIX shells without needing 'if':
Quoted 6 lines
roms/edk2-build.sh fi · line 52
Quoted 6 lines
roms/edk2-build.sh +6
-0+ edk2_thread_count=$(qemu_edk2_get_thread_count "$MAKEFLAGS") +else + # We are not running within 'make', let the edk2 "build" utility to fetch + # the logical CPU count with Python's multiprocessing.cpu_count() method. + edk2_thread_count=0 +fi
(2) "let" doesn't take the preposition "to". I'd suggest:
Let the edk2 "build" utility [] fetch ...
Quoted 2 lines
Quoted 2 lines
edk2_thread_count=$(qemu_edk2_get_thread_count "${MAKEFLAGS:-0}")
(3) The expression
edk2_thread_count=$(qemu_edk2_get_thread_count "${MAKEFLAGS:-0}")would pass the string "0" as $1 to the qemu_edk2_get_thread_count() function. That doesn't seem useful (please see the docs on said shell function).
We could write
edk2_thread_count=$(qemu_edk2_get_thread_count "${MAKEFLAGS:-}")instead, to pass "". But that would cause qemu_edk2_get_thread_count() to print "1", which is not what we want here, AIUI.
I think I prefer the approach with "[ -v". While it's nonportable, "edk2-build.sh" is already -- consciously -- so: it uses an array variable, for example.
(4) Phil, did you regression-test this change with plain "make" (i.e., no "-j" option)? The behavior shouldn't change for that case (i.e. qemu_edk2_get_thread_count() should be invoked, and it should print "1").
With (1) and (2) fixed, and (4) confirmed:
Thanks
Laszlo