source: jhalfs@ bc2f591

ablfs-more legacy trunk
Last change on this file since bc2f591 was 0e4ddfa, checked in by Pierre Labastie <pierre@…>, 6 years ago

Improve top-level jhalfs based on shellcheck

  • Various improvements from shellcheck around quoting, the use of read, vs $() and other logical comparisons
  • Provide a function for sourcing/loading files so we can remove a lot of duplication. Move loading of some functions from common/common-functions to ./jhalfs for consistency
  • Provide some functions for standardized messaging
  • Remove other duplication in the code, especially checking VERBOSITY
  • Standardize indentation. Two spaces seemed the most prevalent so I went with that.

Patch by Jeremy Huntwork, with slight modifications.

  • Property mode set to 100755
File size: 13.9 KB
RevLine 
[4da2512]1#!/bin/bash
2# $Id$
3set -e
[bbcdeab]4# Pass trap handlers to functions
5set -E
[4da2512]6
7# VT100 colors
8declare -r RED=$'\e[31m'
9declare -r GREEN=$'\e[32m'
10declare -r YELLOW=$'\e[33m'
11
[0e4ddfa]12# shellcheck disable=SC2034
13declare -r BLUE=$'\e[34m'
[4da2512]14declare -r OFF=$'\e[0m'
15declare -r BOLD=$'\e[1m'
16declare -r tab_=$'\t'
17declare -r nl_=$'\n'
18
[0e4ddfa]19# shellcheck disable=SC2034
[4da2512]20declare -r DD_BORDER="${BOLD}==============================================================================${OFF}"
[0e4ddfa]21# shellcheck disable=SC2034
[4da2512]22declare -r SD_BORDER="${BOLD}------------------------------------------------------------------------------${OFF}"
[0e4ddfa]23# shellcheck disable=SC2034
[4da2512]24declare -r STAR_BORDER="${BOLD}******************************************************************************${OFF}"
25
26# bold yellow > < pair
27declare -r R_arrow=$'\e[1;33m>\e[0m'
28declare -r L_arrow=$'\e[1;33m<\e[0m'
29
30
31#>>>>>>>>>>>>>>>ERROR TRAPPING >>>>>>>>>>>>>>>>>>>>
32#-----------------------#
33simple_error() { # Basic error trap.... JUST DIE
34#-----------------------#
[0e4ddfa]35 LASTLINE="$1"
36 LASTERR="$2"
37 LASTSOURCE="$4"
38 error_message "${GREEN} Error $LASTERR at $LASTSOURCE line ${LASTLINE}!"
[4da2512]39}
40
41see_ya() {
[0e4ddfa]42 printf '\n%b%bjhalfs%b exit%b\n' "$L_arrow" "$BOLD" "$R_arrow" "$OFF"
[4da2512]43}
44##### Simple error TRAPS
45# ctrl-c SIGINT
46# ctrl-y
47# ctrl-z SIGTSTP
48# SIGHUP 1 HANGUP
49# SIGINT 2 INTRERRUPT FROM KEYBOARD Ctrl-C
50# SIGQUIT 3
51# SIGKILL 9 KILL
52# SIGTERM 15 TERMINATION
53# SIGSTOP 17,18,23 STOP THE PROCESS
54#####
55set -e
56trap see_ya 0
[bbcdeab]57trap 'simple_error "${LINENO}" "$?" "${FUNCNAME}" "${BASH_SOURCE}"' ERR
[d505128]58trap 'echo -e "\n\n${RED}INTERRUPT${OFF} trapped\n" && exit 2' \
59 HUP INT QUIT TERM # STOP stops tterminal output and does not seem to
60 # execute the handler
[4da2512]61#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
62
[0e4ddfa]63simple_message() {
64 # Prevents having to check $VERBOSITY everywhere
65 if [ "$VERBOSITY" -ne 0 ] ; then
66 # shellcheck disable=SC2059
67 printf "$*"
68 fi
69}
70
71warning_message() {
72 simple_message "${YELLOW}\\nWARNING:${OFF} $*\\n\\n"
73}
74
75error_message() {
76 # Prints an error message and exits with LASTERR or 1
77 if [ -n "$LASTERR" ] ; then
78 LASTERR=$(printf '%d' "$LASTERR")
79 else
80 LASTERR=1
81 fi
82 # If +e then disable text output
83 if [[ "$-" =~ e ]]; then
84 printf '\n\n%bERROR:%b %s\n' "$RED" "$OFF" "$*" >&2
85 fi
86 exit "$LASTERR"
87}
88
89load_file() {
90 # source files in a consistent way with an optional message
91 file="$1"
92 shift
93 msg="Loading file ${file}..."
94 [ -z "$*" ] || msg="$*..."
95 simple_message "$msg"
96
97 # shellcheck disable=SC1090
98 source "$file" 2>/dev/null || error_message "$file did not load"
99 simple_message "OK\\n"
100}
101
[4da2512]102version="
[8d56c87]103${BOLD} \"jhalfs\"${OFF} builder tool (development) \$Rev$
[4da2512]104 \$Date$
105
[8d56c87]106 Written by George Boudreau, Manuel Canales Esparcia, Pierre Labastie,
[4da2512]107 plus several contributions.
108
109 Based on an idea from Jeremy Huntwork
110
111 This set of files are published under the
112 ${BOLD}Gnu General Public License, Version 2.${OFF}
[8d56c87]113 See the ${BOLD}LICENCE${OFF} file in this directory.
[4da2512]114"
115
116case $1 in
[909ee37]117 -v ) echo "$version" && exit ;;
[4da2512]118 run ) : ;;
119 * )
120 echo "${nl_}${tab_}${BOLD}${RED}This script cannot be called directly: EXITING ${OFF}${nl_}"
121 exit 1
122 ;;
123esac
124
[65c998b]125# If the user has not saved his configuration file, let's ask
126# if he or she really wants to run this stuff
[0e4ddfa]127time_old=$(stat -c '%Y' configuration.old 2>/dev/null)
128time_current=$(stat -c '%Y' configuration 2>/dev/null)
129if [ "$(printf '%d' "$time_old")" -ge "$(printf '%d' "$time_current")" ]
[65c998b]130 then echo -n "Do you want to run jhalfs? yes/no (yes): "
[0e4ddfa]131 read -r ANSWER
132 if [ "x${ANSWER:0:1}" = "xn" ] || [ "x${ANSWER:0:1}" = "xN" ] ; then
[65c998b]133 echo "${nl_}Exiting gracefully.${nl_}"
134 exit
135 fi
136fi
137
138# Change this to 0 to suppress almost all messages
[4da2512]139VERBOSITY=1
140
[0e4ddfa]141load_file configuration "Loading config params from <configuration>"
[4da2512]142
143# These are boolean vars generated from Config.in.
[7072e1f]144# ISSUE: If a boolean parameter is not set to y(es) there
145# is no variable defined by the menu app. This can
146# cause a headache if you are not aware.
147# The following variables MUST exist. If they don't, the
148# default value is n(o).
[4da2512]149RUNMAKE=${RUNMAKE:-n}
150GETPKG=${GETPKG:-n}
151COMPARE=${COMPARE:-n}
152RUN_FARCE=${RUN_FARCE:-n}
153RUN_ICA=${RUN_ICA:-n}
[7072e1f]154PKGMNGT=${PKGMNGT:-n}
[945ccaa]155WRAP_INSTALL=${WRAP_INSTALL:-n}
[4da2512]156BOMB_TEST=${BOMB_TEST:-n}
157STRIP=${STRIP:=n}
158REPORT=${REPORT:=n}
159VIMLANG=${VIMLANG:-n}
[dc7fd7b]160DEL_LA_FILES=${DEL_LA_FILES:-n}
[b339c94]161FULL_LOCALE=${FULL_LOCALE:-n}
[4da2512]162GRSECURITY_HOST=${GRSECURITY_HOST:-n}
[9a536f7]163CUSTOM_TOOLS=${CUSTOM_TOOLS:-n}
[a16f769]164REBUILD_MAKEFILE=${REBUILD_MAKEFILE:-n}
[93346ee]165INSTALL_LOG=${INSTALL_LOG:-n}
[486e9a7]166CLEAN=${CLEAN:=n}
[d035526]167SET_SSP=${SET_SSP:=n}
168SET_ASLR=${SET_ASLR:=n}
169SET_PAX=${SET_PAX:=n}
170SET_HARDENED_TMP=${SET_HARDENED_TMP:=n}
171SET_WARNINGS=${SET_WARNINGS:=n}
172SET_MISC=${SET_MISC:=n}
173SET_BLOWFISH=${SET_BLOWFISH:=n}
[945ccaa]174UNICODE=${UNICODE:=n}
[3b43e17b]175LOCAL=${LOCAL:=n}
[84a3fda]176REALSBU=${REALSBU:=n}
[4da2512]177
[75d6d1c]178if [[ "${NO_PROGRESS_BAR}" = "y" ]] ; then
[0e4ddfa]179# shellcheck disable=SC2034
180 NO_PROGRESS="#"
[75d6d1c]181fi
182
183
[38ae01f]184# Sanity check on the location of $BUILDDIR / $JHALFSDIR
[0e4ddfa]185CWD="$(cd "$(dirname "$0")" && pwd)"
186if [[ $JHALFSDIR == "$CWD" ]]; then
187 echo " The jhalfs source directory conflicts with the jhalfs build directory."
188 echo " Please move the source directory or change the build directory."
189 exit 2
[38ae01f]190fi
191
[0873ccc]192# Book sources envars
[4965fa8]193BRANCH_ID=${BRANCH_ID:=development}
194
195case $BRANCH_ID in
196 development )
197 case $PROGNAME in
[92b7cb8]198 clfs* ) TREE="" ;;
[c4ad7bf]199 * ) TREE=trunk/BOOK ;;
[4965fa8]200 esac
201 LFSVRS=development
202 ;;
203 *EDIT* ) echo " You forgot to set the branch or stable book version."
204 echo " Please rerun make and fix the configuration."
205 exit 2 ;;
206 branch-* )
[c4ad7bf]207 case $PROGNAME in
208 lfs )
209 LFSVRS=${BRANCH_ID}
[bb2862d]210 TREE=branches/${BRANCH_ID#branch-}
[dd08e14]211 if [ ${BRANCH_ID:7:1} = 6 ]; then
[bb2862d]212 TREE=${TREE}/BOOK
213 fi
[c4ad7bf]214 ;;
215 clfs* )
216 LFSVRS=${BRANCH_ID}
217 TREE=${BRANCH_ID#branch-}
218 ;;
219 esac
[4965fa8]220 ;;
221 * )
222 case $PROGNAME in
[e650dc7]223 lfs )
224 LFSVRS=${BRANCH_ID}
225 TREE=tags/${BRANCH_ID}
226 if (( ${BRANCH_ID:0:1} < 7 )) ; then
227 TREE=${TREE}/BOOK
228 fi
229 ;;
230 hlfs )
[4965fa8]231 LFSVRS=${BRANCH_ID}
232 TREE=tags/${BRANCH_ID}/BOOK
233 ;;
[92b7cb8]234 clfs* )
[4965fa8]235 LFSVRS=${BRANCH_ID}
[34d1b6a]236 TREE=clfs-${BRANCH_ID}
[4965fa8]237 ;;
[92b7cb8]238 * )
[4965fa8]239 esac
240 ;;
241esac
242
[4da2512]243# Set the document location...
[1cf621b]244BOOK=${BOOK:=$JHALFSDIR/$PROGNAME-$LFSVRS}
[4da2512]245
[4965fa8]246
[728955b]247#--- Envars not sourced from configuration
[0e4ddfa]248# shellcheck disable=SC2034
[728955b]249case $PROGNAME in
[0e4ddfa]250 clfs ) declare -r GIT="git://git.clfs.org/cross-lfs" ;;
251 clfs2 ) declare -r GIT="git://git.clfs.org/clfs-sysroot" ;;
252 clfs3 ) declare -r GIT="git://git.clfs.org/clfs-embedded" ;;
253 *) declare -r SVN="svn://svn.linuxfromscratch.org" ;;
[728955b]254esac
[0e4ddfa]255
[728955b]256declare -r LOG=000-masterscript.log
[0e4ddfa]257# Needed for fetching BLFS book sources when building CLFS
258# shellcheck disable=SC2034
[728955b]259declare -r SVN_2="svn://svn.linuxfromscratch.org"
260
[4da2512]261# Set true internal variables
262COMMON_DIR="common"
[0e4ddfa]263PACKAGE_DIR=$(echo "$PROGNAME" | tr '[:lower:]' '[:upper:]')
[4da2512]264MODULE=$PACKAGE_DIR/master.sh
[7072e1f]265PKGMNGTDIR="pkgmngt"
266# The name packageManager.xml is hardcoded in *.xsl, so no variable.
[4da2512]267
[0e4ddfa]268for file in \
269 "$COMMON_DIR/common-functions" \
270 "$COMMON_DIR/libs/func_book_parser" \
271 "$COMMON_DIR/libs/func_download_pkgs" \
272 "$COMMON_DIR/libs/func_wrt_Makefile" \
273 "$MODULE" ; do
274 load_file "$file"
275done
[9a536f7]276
[0e4ddfa]277simple_message "${SD_BORDER}${nl_}"
[9a536f7]278
279
[0e4ddfa]280#*******************************************************************#
281LASTERR=2
282for file in \
283 "$COMMON_DIR/libs/func_check_version.sh" \
284 "$COMMON_DIR/libs/func_validate_configs.sh" \
285 "$COMMON_DIR/libs/func_custom_pkgs" ; do
286 load_file "$file"
287done
288unset LASTERR
289
290simple_message "${SD_BORDER}${nl_}"
291simple_message 'Checking tools required for jhalfs'
[6ed3dd7]292check_alfs_tools
[0e4ddfa]293simple_message "${SD_BORDER}${nl_}"
[4da2512]294
[e576789]295# blfs-tool envars
296BLFS_TOOL=${BLFS_TOOL:-n}
297if [[ "${BLFS_TOOL}" = "y" ]] ; then
[0e4ddfa]298 simple_message 'Checking supplementary tools for installing BLFS'
[6ed3dd7]299 check_blfs_tools
[0e4ddfa]300 simple_message "${SD_BORDER}${nl_}"
[854854e]301 BLFS_SVN=${BLFS_SVN:-n}
302 BLFS_WORKING_COPY=${BLFS_WORKING_COPY:-n}
303 BLFS_BRANCH=${BLFS_BRANCH:-n}
304 if [[ "${BLFS_SVN}" = "y" ]]; then
305 BLFS_BRANCH_ID=development
306 BLFS_TREE=trunk/BOOK
307 elif [[ "${BLFS_WORKING_COPY}" = "y" ]]; then
[0e4ddfa]308 if [[ ! -d "$BLFS_WC_LOCATION/postlfs" ]] ; then
[854854e]309 echo " BLFS tools: This is not a working copy: $BLFS_WC_LOCATION."
310 echo " Please rerun make and fix the configuration."
311 exit 2
[0e4ddfa]312 fi
313 BLFS_TREE=$(cd "$BLFS_WC_LOCATION"; svn info | grep '^URL' | sed 's@.*BLFS/@@')
314 BLFS_BRANCH_ID=$(echo "$BLFS_TREE" | sed -e 's@trunk/BOOK@development@' \
315 -e 's@branches/@branch-@' \
316 -e 's@tags/@@' \
317 -e 's@/BOOK@@')
[0ba80be]318 elif [[ "${BLFS_BRANCH}" = "y" ]] ; then
[854854e]319 case $BLFS_BRANCH_ID in
320 *EDIT* ) echo " You forgot to set the BLFS branch or stable book version."
321 echo " Please rerun make and fix the configuration."
322 exit 2 ;;
[dd08e14]323 branch-6.* ) BLFS_TREE=branches/${BLFS_BRANCH_ID#branch-}/BOOK ;;
324 branch-* ) BLFS_TREE=branches/${BLFS_BRANCH_ID#branch-} ;;
325 6.2* | 7.* | 8.*) BLFS_TREE=tags/${BLFS_BRANCH_ID} ;;
[854854e]326 * ) BLFS_TREE=tags/${BLFS_BRANCH_ID}/BOOK ;;
327 esac
328 fi
[0e4ddfa]329 load_file "${COMMON_DIR}/libs/func_install_blfs"
[e576789]330fi
[4da2512]331
332###################################
333### MAIN ###
334###################################
335
336
337validate_config
338echo "${SD_BORDER}${nl_}"
339echo -n "Are you happy with these settings? yes/no (no): "
[0e4ddfa]340read -r ANSWER
341if [ "x$ANSWER" != "xyes" ] ; then
[881c96f]342 echo "${nl_}Rerun make to fix the configuration options.${nl_}"
[d023a35]343 exit
[4da2512]344fi
345echo "${nl_}${SD_BORDER}${nl_}"
346
347# Load additional modules or configuration files based on global settings
348# compare module
349if [[ "$COMPARE" = "y" ]]; then
[0e4ddfa]350 load_file "${COMMON_DIR}/libs/func_compare.sh" 'Loading compare module'
[4da2512]351fi
352#
353# optimize module
354if [[ "$OPTIMIZE" != "0" ]]; then
[0e4ddfa]355 load_file optimize/optimize_functions 'Loading optimization module'
[4da2512]356 #
357 # optimize configurations
[0e4ddfa]358 load_file optimize/opt_config 'Loading optimization config'
[854854e]359 # The number of parallel jobs is taken from configuration now
[0e4ddfa]360 # shellcheck disable=SC2034
[854854e]361 MAKEFLAGS="-j${N_PARALLEL}"
[4da2512]362 # Validate optimize settings, if required
363 validate_opt_settings
364fi
365#
366
[a16f769]367if [[ "$REBUILD_MAKEFILE" = "n" ]] ; then
[4da2512]368
[200fbde]369# If requested, clean the build directory
[7b6ecc5]370 clean_builddir
[4da2512]371
[7b6ecc5]372 if [[ ! -d $JHALFSDIR ]]; then
[0e4ddfa]373 sudo mkdir -p "$JHALFSDIR"
374 sudo chown "$USER":"$USER" "$JHALFSDIR"
[7b6ecc5]375 fi
[3e7ceed]376
[200fbde]377# Create $BUILDDIR/sources even though it could be created by get_sources()
[7b6ecc5]378 if [[ ! -d $BUILDDIR/sources ]]; then
[0e4ddfa]379 sudo mkdir -p "$BUILDDIR/sources"
380 sudo chmod a+wt "$BUILDDIR/sources"
[7b6ecc5]381 fi
[200fbde]382
383# Create the log directory
[7b6ecc5]384 if [[ ! -d $LOGDIR ]]; then
[0e4ddfa]385 mkdir "$LOGDIR"
[7b6ecc5]386 fi
[0e4ddfa]387 true >"$LOGDIR/$LOG"
[200fbde]388
389# Copy common helper files
[0e4ddfa]390 cp "$COMMON_DIR"/{makefile-functions,progress_bar.sh} "$JHALFSDIR/"
[200fbde]391
392# Copy needed stylesheets
[0e4ddfa]393 cp "$COMMON_DIR"/{packages.xsl,chroot.xsl,kernfs.xsl} "$JHALFSDIR/"
[200fbde]394
395# Fix the XSL book parser
[92b7cb8]396 case $PROGNAME in
[0e4ddfa]397 clfs* ) sed 's,FAKEDIR,'"${BOOK}/BOOK"',' "${PACKAGE_DIR}/${XSL}" > "${JHALFSDIR}/${XSL}" ;;
398 lfs | hlfs ) sed 's,FAKEDIR,'"$BOOK"',' "${PACKAGE_DIR}/${XSL}" > "${JHALFSDIR}/${XSL}" ;;
[92b7cb8]399 * ) ;;
400 esac
[3e7ceed]401 export XSL=$JHALFSDIR/${XSL}
[7b6ecc5]402
[200fbde]403# Copy packageManager.xml, if needed
[3aa1acd]404 [[ "$PKGMNGT" = "y" ]] && [[ "$PROGNAME" = "lfs" ]] && {
[0e4ddfa]405 cp "$PKGMNGTDIR/packageManager.xml" "$JHALFSDIR/"
406 cp "$PKGMNGTDIR/packInstall.sh" "$JHALFSDIR/"
[3aa1acd]407 }
[200fbde]408
409# Copy urls.xsl, if needed
[0e4ddfa]410 [[ "$GETPKG" = "y" ]] && cp "$COMMON_DIR/urls.xsl" "$JHALFSDIR/"
[200fbde]411
[c672fa8]412# Always create the test-log directory to allow user to "uncomment" test
413# instructions
[0e4ddfa]414 install -d -m 1777 "$TESTLOGDIR"
[200fbde]415
416# Create the installed-files directory, if needed
[0e4ddfa]417 [[ "$INSTALL_LOG" = "y" ]] && [[ ! -d $FILELOGDIR ]] && install -d -m 1777 "$FILELOGDIR"
[200fbde]418
419# Prepare report creation, if needed
[7b6ecc5]420 if [[ "$REPORT" = "y" ]]; then
[0e4ddfa]421 cp $COMMON_DIR/create-sbu_du-report.sh "$JHALFSDIR/"
[7072e1f]422 # After making sure that all looks sane, dump the settings to a file
[7b6ecc5]423 # This file will be used to create the REPORT header
[0e4ddfa]424 validate_config >"$JHALFSDIR/jhalfs.config"
[7b6ecc5]425 fi
[200fbde]426
427# Copy optimize files, if needed
[0e4ddfa]428 [[ "$OPTIMIZE" != "0" ]] && cp optimize/opt_override "$JHALFSDIR/"
[200fbde]429
430# Copy compare files, if needed
[3e7ceed]431 if [[ "$COMPARE" = "y" ]]; then
[0e4ddfa]432 mkdir -p "$JHALFSDIR/extras"
433 cp extras/* "$JHALFSDIR/extras"
[3e7ceed]434 fi
[200fbde]435
436# Copy custom tools config files, if requested
[3e7ceed]437 if [[ "${CUSTOM_TOOLS}" = "y" ]]; then
438 echo "Copying custom tool scripts to $JHALFSDIR"
[0e4ddfa]439 mkdir -p "$JHALFSDIR/custom-commands"
440 cp -f custom/config/* "$JHALFSDIR/custom-commands"
[3e7ceed]441 fi
[200fbde]442
[f733772]443# Download or updates the book source
444# Note that all customization to $JHALFSDIR have to be done before this.
445# But the LFS book is needed for BLFS tools.
446 get_book
447 extract_commands
448 echo "${SD_BORDER}${nl_}"
[0e4ddfa]449 cd "$CWD" # the functions above change directory
[f733772]450
[200fbde]451# Install blfs-tool, if requested.
[7b6ecc5]452 if [[ "${BLFS_TOOL}" = "y" ]] ; then
[6ed3dd7]453 echo Installing BLFS book and tools
[0e4ddfa]454 install_blfs_tools 2>&1 | tee -a "$LOGDIR/$LOG"
[bbcdeab]455 [[ ${PIPESTATUS[0]} != 0 ]] && exit 1
[7b6ecc5]456 fi
457
[4965fa8]458fi
459
[e576789]460# When regenerating the Makefile, we need to know also the
461# canonical book version
[0e4ddfa]462# shellcheck disable=SC2034
[a16f769]463if [[ "$REBUILD_MAKEFILE" = "y" ]] ; then
464 case $PROGNAME in
[92b7cb8]465 clfs* )
[0e4ddfa]466 VERSION=$(xmllint --noent "$BOOK/prologue/$ARCH/bookinfo.xml" 2>/dev/null | grep subtitle | sed -e 's/^.*ion //' -e 's/<\/.*//') ;;
[c9598f2]467 lfs)
468 if [[ "$INITSYS" = "sysv" ]] ; then
[0e4ddfa]469 VERSION=$(grep 'ENTITY version ' "$BOOK/general.ent" | cut -d\" -f2)
[c9598f2]470 else
[0e4ddfa]471 VERSION=$(grep 'ENTITY versiond' "$BOOK/general.ent" | cut -d\" -f2)
[c9598f2]472 fi
473 ;;
[a16f769]474 *)
[0e4ddfa]475 VERSION=$(xmllint --noent "$BOOK/prologue/bookinfo.xml" 2>/dev/null | grep subtitle | sed -e 's/^.*ion //' -e 's/<\/.*//') ;;
[a16f769]476 esac
477fi
478
[4da2512]479build_Makefile
[3e7ceed]480
[4da2512]481echo "${SD_BORDER}${nl_}"
482
[1cf621b]483# Check for build prerequisites.
484 echo
[0e4ddfa]485 cd "$CWD"
[1cf621b]486 check_prerequisites
487 echo "${SD_BORDER}${nl_}"
488# All is well, run the build (if requested)
[4da2512]489run_make
Note: See TracBrowser for help on using the repository browser.