From eea45e77dc0eb2f477e911a590c35d5f2275b1a6 Mon Sep 17 00:00:00 2001 From: Klemens Nanni Date: Thu, 10 Mar 2016 19:07:05 +0000 Subject: build: Simplify, make code more readable Use functions for common subroutines, get rid of unneeded variables, use a here document instead of multiple invocations of printf and slightly restructure the code to make it more concise. --- build | 102 +++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 41 deletions(-) (limited to 'build') diff --git a/build b/build index 53e16f82..f1b94722 100755 --- a/build +++ b/build @@ -1,9 +1,10 @@ -#!/bin/bash +#!/bin/sh -# generic build script, for building libreboot (all of it) +# generic build script, for building components (all of them) # # Copyright (C) 2014, 2015 Francis Rowe -# 2015 Patrick "P. J." McDermott +# Copyright (C) 2015 Patrick "P. J." McDermott +# Copyright (C) 2015, 2016 Klemens Nanni # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -22,48 +23,67 @@ [ "x${DEBUG+set}" = 'xset' ] && set -v set -u -e -build="./resources/scripts/helpers/build" -mode="unknown" -option="unknown" +build=./resources/scripts/helpers/build -usage="./build mode option" -availablemodes="$(for mode in ${build}/*; do printf "%s\n" "${mode##*/}"; done)" -availableoptions="unknown" # unknown until the mode is determined +listmodes() { + for mode in "${build}"/*; do + printf '%s\n' "${mode##*/}" + done +} -# User specified no or too few/many parameters -if [ $# -lt 2 ]; then - printf "%s\n\n" "${usage}" - printf "possible values for 'mode':\n%s\n\n" "${availablemodes}" - printf "Example: ./build module all\n" - printf "Example: ./build module flashrom\n" - printf "Example: ./build roms withgrub\n" - printf "Example: ./build clean all\n" - printf "Example (extra option) ./build module bucts static\n" - printf "Refer to the libreboot documentation for more info\n\n" - exit 1 -fi -mode="${1}" -option="${2}" -shift 2 +# Takes exactly one mode as parameter +listoptions() { + for option in "${build}"/"${1}"/*; do + printf '%s\n' "${option##*/}" + done +} -if [ -d "${build}/${mode}" ]; then - availableoptions="$(for buildoption in ${build}/${mode}/*; do printf "%s\n" "${buildoption##*/}"; done)" - if [ "${option}" = "list" ]; then - printf "Available options for '%s' are:\nall\n%s\n\n" "${mode}" "${availableoptions}" - elif [ -f "${build}/${mode}/${option}" ]; then - "${build}/${mode}/${option}" "$@" - elif [ "${option}" = "all" ]; then - for option in ${availableoptions}; do - "${build}/${mode}/${option}" "$@" - done - else - printf "Invalid option for '%s'. Available options are:\nall\n%s\n\n" "${mode}" "${availableoptions}" - exit 1 - fi -else - printf "Invalid mode. Available modes are:\n%s\n\n" "${availablemodes}" +help() { + cat <<- EOF + USAGE: ./build