trees: use wrapper for dry-running make commands

instead of using lots of if/else conditions, do that once
and set a variable, dry, to :

if not doing a dry run, the variable is empty. prefix this
variable in places where you don't want a certain action to
be performed, on dry runs.

more specifically, : does *nothing* and always returns with
zero status (success).

this results in cleaner code, and a small sloccount reduction.

Signed-off-by: Leah Rowe <leah@libreboot.org>
audit6
Leah Rowe 2024-07-10 20:38:36 +01:00
parent 459db1cc2e
commit c0a4df688c
2 changed files with 41 additions and 59 deletions

View File

@ -26,13 +26,12 @@ mkserprog()
mkpayload_grub()
{
[ "$_f" = "-d" ] && return 0 # dry run
eval `setvars "" grub_modules grub_install_modules`
eval `setcfg "$grubdata/module/$tree"`
$dry eval `setcfg "$grubdata/module/$tree"`
x_ rm -f "$cdir/grub.elf"
$dry x_ rm -f "$cdir/grub.elf"
"${cdir}/grub-mkstandalone" --grub-mkimage="${cdir}/grub-mkimage" \
$dry "${cdir}/grub-mkstandalone" --grub-mkimage="${cdir}/grub-mkimage" \
-O i386-coreboot -o "${cdir}/grub.elf" -d "${cdir}/grub-core/" \
--fonts= --themes= --locales= --modules="$grub_modules" \
--install-modules="$grub_install_modules" \
@ -44,12 +43,9 @@ mkpayload_grub()
mkvendorfiles()
{
if [ "$_f" = "-d" ]; then
check_coreboot_utils "$tree"
elif [ "$_f" = "-b" ]; then
printf "%s\n" "${version%%-*}" > "$cdir/.coreboot-version" || \
$err "!mk $cdir .coreboot-version"
fi
[ -z "$mode" ] && [ "$target" != "$tree" ] && \
x_ ./vendor download $target; return 0
}
@ -73,7 +69,6 @@ check_coreboot_utils()
mkcorebootbin()
{
[ "$_f" = "-d" ] && return 0 # dry run
[ "$target" = "$tree" ] && return 0
tmprom="$cdir/build/coreboot.rom"
@ -86,7 +81,7 @@ mkcorebootbin()
[ "$payload_uboot" = "y" ] || payload_seabios="y"
[ "$payload_grub" = "y" ] && payload_seabios="y"
[ "$payload_seabios" = "y" ] && [ "$payload_uboot" = "y" ] && \
$err "$target: U-Boot and SeaBIOS/GRUB are both enabled."
$dry $err "$target: U-Boot and SeaBIOS/GRUB are both enabled."
[ -z "$grub_scan_disk" ] && grub_scan_disk="nvme ahci ata"
@ -96,15 +91,15 @@ mkcorebootbin()
[ "$payload_memtest" = "y" ] || payload_memtest="n"
[ "$(uname -m)" = "x86_64" ] || payload_memtest="n"
[ "$payload_seabios" = "y" ] && pname="seabios" && add_seabios
[ "$payload_uboot" = "y" ] && pname="uboot" && add_uboot
[ "$payload_seabios" = "y" ] && pname="seabios" && $dry add_seabios
[ "$payload_uboot" = "y" ] && pname="uboot" && $dry add_uboot
newrom="bin/$target/${pname}_${target}_$initmode$displaymode.rom"
x_ mkdir -p "${newrom%/*}"; x_ mv "$tmprom" "$newrom"
$dry x_ mkdir -p "${newrom%/*}"; $dry x_ mv "$tmprom" "$newrom"
[ "$XBMK_RELEASE" = "y" ] || return 0
mksha512sum "$newrom" "vendorhashes"
./vendor inject -r "$newrom" -b "$target" -n nuke || $err "!n $newrom"
$dry mksha512sum "$newrom" "vendorhashes"; $dry ./vendor inject \
-r "$newrom" -b "$target" -n nuke || $err "!nuke $newrom"
}
add_seabios()
@ -147,7 +142,6 @@ add_uboot()
mkcoreboottar()
{
[ "$_f" = "-d" ] && return 0 # dry run
[ "$target" = "$tree" ] && return 0; [ "$XBMK_RELEASE" = "y" ] && \
[ "$release" != "n" ] && mkrom_tarball "bin/$target"; return 0
[ "$release" != "n" ] && $dry mkrom_tarball "bin/$target"; return 0
}

View File

@ -12,13 +12,13 @@ set -u -e
eval `setvars "" xarch cdir defconfig cmakedir xlang mode makeargs elfdir cmd \
project target target_dir targets xtree _f release bootstrapargs mkhelper \
autoconfargs listfile autogenargs btype tree rev tree_depend build_depend \
premake postmake mkhelpercfg`
premake postmake mkhelpercfg dry`
main()
{
while getopts f:b:m:u:c:x:s:l:n:d: option; do
[ -n "$_f" ] && $err "only one flag is permitted"
_f="$1"
_f="$1" && [ "$_f" = "-d" ] && dry=":"
case "$1" in
-d) mode="" ;;
-b) mode="" ;;
@ -62,8 +62,7 @@ handle_project()
cmd="build_project"; $cmd
[ -f "$listfile" ] || return 0
[ -z "$mode" ] && [ "$_f" = "-b" ] && \
printf "\n\nOK! Check %s/\n\n" "$elfdir"; return 0
[ -z "$mode" ] && $dry printf "\n\nOK! Check %s/\n\n" "$elfdir"; :
}
build_project()
@ -79,7 +78,7 @@ build_project()
[ "$mode" = "distclean" ] && mode="clean"
run_make_command || return 0
[ -n "$mode" ] || copy_elf; return 0
[ -n "$mode" ] || $dry copy_elf; return 0
}
build_targets()
@ -115,7 +114,7 @@ handle_defconfig()
[ -n "$mode" ] || check_defconfig || continue
handle_makefile
[ -n "$mode" ] || copy_elf
[ -n "$mode" ] || $dry copy_elf
done; return 0
}
@ -135,7 +134,7 @@ handle_src_tree()
fi
x_ ./update trees -f "$project" "$target"
[ -z "$mode" ] && [ "$_f" = "-b" ] && check_cross_compiler; return 0
[ -z "$mode" ] && $dry check_cross_compiler; return 0
}
configure_project()
@ -163,16 +162,13 @@ configure_project()
[ "$XBMK_RELEASE" = "y" ] && [ "$release" = "n" ] && return 1
[ -z "$btype" ] || [ "${mode%config}" = "$mode" ] || return 1
if [ "$_f" = "-d" ]; then
:
elif [ -z "$mode" ]; then
if [ -z "$mode" ]; then
for bd in $build_depend; do
bd_project="${bd%%/*}"; bd_tree="${bd##*/}"
[ -z "$bd_project" ] && \
$err "$project/$tree: bad bd: '$bd'"
[ "${bd##*/}" = "$bd" ] && bd_tree=""
[ -z "$bd_project" ] || ./update trees -b $bd_project \
$bd_tree || $err "$project/$tree: !bd $bd"
bd_p="${bd%%/*}"; bd_t="${bd##*/}"
[ -z "$bd_p" ] && $dry $err "$project/$tree: !bd '$bd'"
[ "${bd##*/}" = "$bd" ] && bd_t=""
[ -z "$bd_p" ] || $dry ./update trees -b $bd_p $bd_t \
|| $err "!mk $project/$tree $bd_p/$bd_t"
done
fi
@ -202,19 +198,16 @@ check_cross_compiler()
check_defconfig()
{
[ "$_f" = "-d" ] && return 0
[ -f "$defconfig" ] || $err "$project/$target: missing defconfig"
[ -f "$defconfig" ] || $dry $err "$project/$target: missing defconfig"
dest_dir="$elfdir/$target/${defconfig#"$target_dir/config/"}"
elfcheck || return 1 # skip build if a previous one exists
$dry elfcheck || return 1 # skip build if a previous one exists
x_ mkdir -p "$dest_dir"
$dry x_ mkdir -p "$dest_dir"
}
elfcheck()
{
[ "$_f" = "-d" ] && return 0 # dry run. assume a build exists.
# TODO: very hacky check. do it properly (based on build.list)
for elftest in "$dest_dir"/*; do
[ -e "$elftest" ] && e "$elftest" f && return 1
@ -223,48 +216,45 @@ elfcheck()
handle_makefile()
{
check_makefile "$cdir" && x_ make clean -C "$cdir"
$dry check_makefile "$cdir" && x_ make clean -C "$cdir"
[ -f "$defconfig" ] && x_ cp "$defconfig" "$cdir/.config"
[ "$_f" = "-d" ] || [ -n "$mode" ] || [ -n "$btype" ] || make -C \
[ -n "$mode" ] || [ -n "$btype" ] || $dry make -C \
"$cdir" silentoldconfig || make -C "$cdir" oldconfig || :
run_make_command || $err "handle_makefile $cdir: no makefile!"
[ "$_f" = "-d" ] && return 0
_copy=".config" && [ "$mode" = "savedefconfig" ] && _copy="defconfig"
[ "${mode%config}" = "$mode" ] || x_ cp "$cdir/$_copy" "$defconfig"
[ "${mode%config}" = "$mode" ] || $dry x_ cp "$cdir/$_copy" "$defconfig"
[ -e "$cdir/.git" ] && [ "$project" = "u-boot" ] && \
[ "$mode" = "distclean" ] && x_ git -C "$cdir" clean -fdx; return 0
[ "$mode" = "distclean" ] && $dry x_ git -C "$cdir" clean -fdx; :
}
run_make_command()
{
[ -z "$premake" ] || [ -n "$mode" ] || $premake || $err "!$premake"
check_cmake "$cdir" && [ -z "$mode" ] && check_autoconf "$cdir"
check_makefile "$cdir" || return 1
$dry check_cmake "$cdir" && [ -z "$mode" ] && $dry check_autoconf \
"$cdir"; $dry check_makefile "$cdir" || return 1
[ "$_f" = "-d" ] || make -C "$cdir" $mode -j$XBMK_THREADS $makeargs \
$dry make -C "$cdir" $mode -j$XBMK_THREADS $makeargs \
|| $err "$cdir mk$mode"
[ -z "$mkhelper" ] || [ -n "$mode" ] || $mkhelper || $err "!$mkhelper"
[ "$_f" = "-d" ] && return 0
[ "$mode" = "clean" ] && make -C "$cdir" distclean || :; return 0
[ "$mode" = "clean" ] && $dry make -C "$cdir" distclean || :; return 0
}
check_cmake()
{
[ "$_f" = "-d" ] && return 0 # dry run
[ -z "$cmakedir" ] || check_makefile "$1" || cmake -B "$1" \
"$1/$cmakedir" || check_makefile "$1" || $err "$1: !cmk $cmakedir"
[ -z "$cmakedir" ] || check_makefile "$1" || \
[ -z "$cmakedir" ] || $dry check_makefile "$1" || cmake -B "$1" \
"$1/$cmakedir" || $dry check_makefile "$1" || $err \
"$1: !cmk $cmakedir"
[ -z "$cmakedir" ] || $dry check_makefile "$1" || \
$err "check_cmake $1: can't generate Makefile"; return 0
}
check_autoconf()
{
[ "$_f" = "-d" ] && return 0 # dry run
(
cd "$1" || $err "!cd $1"
[ -f "bootstrap" ] && x_ ./bootstrap $bootstrapargs
@ -275,14 +265,12 @@ check_autoconf()
check_makefile()
{
[ "$_f" = "-d" ] && return 0 # dry run
[ -f "$1/Makefile" ] || [ -f "$1/makefile" ] || \
[ -f "$1/GNUmakefile" ] || return 1; return 0
}
copy_elf()
{
[ "$_f" = "-d" ] && return 0 # dry run
[ -f "$listfile" ] && x_ mkdir -p "$dest_dir" && while read -r f; do
[ -f "$cdir/$f" ] && x_ cp "$cdir/$f" "$dest_dir"
done < "$listfile"; x_ make clean -C "$cdir"