source: jhalfs@ c048987

ablfs-more legacy trunk
Last change on this file since c048987 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
Line 
1#!/bin/bash
2# $Id$
3set -e
4# Pass trap handlers to functions
5set -E
6
7# VT100 colors
8declare -r RED=$'\e[31m'
9declare -r GREEN=$'\e[32m'
10declare -r YELLOW=$'\e[33m'
11
12# shellcheck disable=SC2034
13declare -r BLUE=$'\e[34m'
14declare -r OFF=$'\e[0m'
15declare -r BOLD=$'\e[1m'
16declare -r tab_=$'\t'
17declare -r nl_=$'\n'
18
19# shellcheck disable=SC2034
20declare -r DD_BORDER="${BOLD}==============================================================================${OFF}"
21# shellcheck disable=SC2034
22declare -r SD_BORDER="${BOLD}------------------------------------------------------------------------------${OFF}"
23# shellcheck disable=SC2034
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#-----------------------#
35 LASTLINE="$1"
36 LASTERR="$2"
37 LASTSOURCE="$4"
38 error_message "${GREEN} Error $LASTERR at $LASTSOURCE line ${LASTLINE}!"
39}
40
41see_ya() {
42 printf '\n%b%bjhalfs%b exit%b\n' "$L_arrow" "$BOLD" "$R_arrow" "$OFF"
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
57trap 'simple_error "${LINENO}" "$?" "${FUNCNAME}" "${BASH_SOURCE}"' ERR
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
61#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
62
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
102version="
103${BOLD} \"jhalfs\"${OFF} builder tool (development) \$Rev$
104 \$Date$
105
106 Written by George Boudreau, Manuel Canales Esparcia, Pierre Labastie,
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}
113 See the ${BOLD}LICENCE${OFF} file in this directory.
114"
115
116case $1 in
117 -v ) echo "$version" && exit ;;
118 run ) : ;;
119 * )
120 echo "${nl_}${tab_}${BOLD}${RED}This script cannot be called directly: EXITING ${OFF}${nl_}"
121 exit 1
122 ;;
123esac
124
125# If the user has not saved his configuration file, let's ask
126# if he or she really wants to run this stuff
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")" ]
130 then echo -n "Do you want to run jhalfs? yes/no (yes): "
131 read -r ANSWER
132 if [ "x${ANSWER:0:1}" = "xn" ] || [ "x${ANSWER:0:1}" = "xN" ] ; then
133 echo "${nl_}Exiting gracefully.${nl_}"
134 exit
135 fi
136fi
137
138# Change this to 0 to suppress almost all messages
139VERBOSITY=1
140
141load_file configuration "Loading config params from <configuration>"
142
143# These are boolean vars generated from Config.in.
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).
149RUNMAKE=${RUNMAKE:-n}
150GETPKG=${GETPKG:-n}
151COMPARE=${COMPARE:-n}
152RUN_FARCE=${RUN_FARCE:-n}
153RUN_ICA=${RUN_ICA:-n}
154PKGMNGT=${PKGMNGT:-n}
155WRAP_INSTALL=${WRAP_INSTALL:-n}
156BOMB_TEST=${BOMB_TEST:-n}
157STRIP=${STRIP:=n}
158REPORT=${REPORT:=n}
159VIMLANG=${VIMLANG:-n}
160DEL_LA_FILES=${DEL_LA_FILES:-n}
161FULL_LOCALE=${FULL_LOCALE:-n}
162GRSECURITY_HOST=${GRSECURITY_HOST:-n}
163CUSTOM_TOOLS=${CUSTOM_TOOLS:-n}
164REBUILD_MAKEFILE=${REBUILD_MAKEFILE:-n}
165INSTALL_LOG=${INSTALL_LOG:-n}
166CLEAN=${CLEAN:=n}
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}
174UNICODE=${UNICODE:=n}
175LOCAL=${LOCAL:=n}
176REALSBU=${REALSBU:=n}
177
178if [[ "${NO_PROGRESS_BAR}" = "y" ]] ; then
179# shellcheck disable=SC2034
180 NO_PROGRESS="#"
181fi
182
183
184# Sanity check on the location of $BUILDDIR / $JHALFSDIR
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
190fi
191
192# Book sources envars
193BRANCH_ID=${BRANCH_ID:=development}
194
195case $BRANCH_ID in
196 development )
197 case $PROGNAME in
198 clfs* ) TREE="" ;;
199 * ) TREE=trunk/BOOK ;;
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-* )
207 case $PROGNAME in
208 lfs )
209 LFSVRS=${BRANCH_ID}
210 TREE=branches/${BRANCH_ID#branch-}
211 if [ ${BRANCH_ID:7:1} = 6 ]; then
212 TREE=${TREE}/BOOK
213 fi
214 ;;
215 clfs* )
216 LFSVRS=${BRANCH_ID}
217 TREE=${BRANCH_ID#branch-}
218 ;;
219 esac
220 ;;
221 * )
222 case $PROGNAME in
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 )
231 LFSVRS=${BRANCH_ID}
232 TREE=tags/${BRANCH_ID}/BOOK
233 ;;
234 clfs* )
235 LFSVRS=${BRANCH_ID}
236 TREE=clfs-${BRANCH_ID}
237 ;;
238 * )
239 esac
240 ;;
241esac
242
243# Set the document location...
244BOOK=${BOOK:=$JHALFSDIR/$PROGNAME-$LFSVRS}
245
246
247#--- Envars not sourced from configuration
248# shellcheck disable=SC2034
249case $PROGNAME in
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" ;;
254esac
255
256declare -r LOG=000-masterscript.log
257# Needed for fetching BLFS book sources when building CLFS
258# shellcheck disable=SC2034
259declare -r SVN_2="svn://svn.linuxfromscratch.org"
260
261# Set true internal variables
262COMMON_DIR="common"
263PACKAGE_DIR=$(echo "$PROGNAME" | tr '[:lower:]' '[:upper:]')
264MODULE=$PACKAGE_DIR/master.sh
265PKGMNGTDIR="pkgmngt"
266# The name packageManager.xml is hardcoded in *.xsl, so no variable.
267
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
276
277simple_message "${SD_BORDER}${nl_}"
278
279
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'
292check_alfs_tools
293simple_message "${SD_BORDER}${nl_}"
294
295# blfs-tool envars
296BLFS_TOOL=${BLFS_TOOL:-n}
297if [[ "${BLFS_TOOL}" = "y" ]] ; then
298 simple_message 'Checking supplementary tools for installing BLFS'
299 check_blfs_tools
300 simple_message "${SD_BORDER}${nl_}"
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
308 if [[ ! -d "$BLFS_WC_LOCATION/postlfs" ]] ; then
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
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@@')
318 elif [[ "${BLFS_BRANCH}" = "y" ]] ; then
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 ;;
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} ;;
326 * ) BLFS_TREE=tags/${BLFS_BRANCH_ID}/BOOK ;;
327 esac
328 fi
329 load_file "${COMMON_DIR}/libs/func_install_blfs"
330fi
331
332###################################
333### MAIN ###
334###################################
335
336
337validate_config
338echo "${SD_BORDER}${nl_}"
339echo -n "Are you happy with these settings? yes/no (no): "
340read -r ANSWER
341if [ "x$ANSWER" != "xyes" ] ; then
342 echo "${nl_}Rerun make to fix the configuration options.${nl_}"
343 exit
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
350 load_file "${COMMON_DIR}/libs/func_compare.sh" 'Loading compare module'
351fi
352#
353# optimize module
354if [[ "$OPTIMIZE" != "0" ]]; then
355 load_file optimize/optimize_functions 'Loading optimization module'
356 #
357 # optimize configurations
358 load_file optimize/opt_config 'Loading optimization config'
359 # The number of parallel jobs is taken from configuration now
360 # shellcheck disable=SC2034
361 MAKEFLAGS="-j${N_PARALLEL}"
362 # Validate optimize settings, if required
363 validate_opt_settings
364fi
365#
366
367if [[ "$REBUILD_MAKEFILE" = "n" ]] ; then
368
369# If requested, clean the build directory
370 clean_builddir
371
372 if [[ ! -d $JHALFSDIR ]]; then
373 sudo mkdir -p "$JHALFSDIR"
374 sudo chown "$USER":"$USER" "$JHALFSDIR"
375 fi
376
377# Create $BUILDDIR/sources even though it could be created by get_sources()
378 if [[ ! -d $BUILDDIR/sources ]]; then
379 sudo mkdir -p "$BUILDDIR/sources"
380 sudo chmod a+wt "$BUILDDIR/sources"
381 fi
382
383# Create the log directory
384 if [[ ! -d $LOGDIR ]]; then
385 mkdir "$LOGDIR"
386 fi
387 true >"$LOGDIR/$LOG"
388
389# Copy common helper files
390 cp "$COMMON_DIR"/{makefile-functions,progress_bar.sh} "$JHALFSDIR/"
391
392# Copy needed stylesheets
393 cp "$COMMON_DIR"/{packages.xsl,chroot.xsl,kernfs.xsl} "$JHALFSDIR/"
394
395# Fix the XSL book parser
396 case $PROGNAME in
397 clfs* ) sed 's,FAKEDIR,'"${BOOK}/BOOK"',' "${PACKAGE_DIR}/${XSL}" > "${JHALFSDIR}/${XSL}" ;;
398 lfs | hlfs ) sed 's,FAKEDIR,'"$BOOK"',' "${PACKAGE_DIR}/${XSL}" > "${JHALFSDIR}/${XSL}" ;;
399 * ) ;;
400 esac
401 export XSL=$JHALFSDIR/${XSL}
402
403# Copy packageManager.xml, if needed
404 [[ "$PKGMNGT" = "y" ]] && [[ "$PROGNAME" = "lfs" ]] && {
405 cp "$PKGMNGTDIR/packageManager.xml" "$JHALFSDIR/"
406 cp "$PKGMNGTDIR/packInstall.sh" "$JHALFSDIR/"
407 }
408
409# Copy urls.xsl, if needed
410 [[ "$GETPKG" = "y" ]] && cp "$COMMON_DIR/urls.xsl" "$JHALFSDIR/"
411
412# Always create the test-log directory to allow user to "uncomment" test
413# instructions
414 install -d -m 1777 "$TESTLOGDIR"
415
416# Create the installed-files directory, if needed
417 [[ "$INSTALL_LOG" = "y" ]] && [[ ! -d $FILELOGDIR ]] && install -d -m 1777 "$FILELOGDIR"
418
419# Prepare report creation, if needed
420 if [[ "$REPORT" = "y" ]]; then
421 cp $COMMON_DIR/create-sbu_du-report.sh "$JHALFSDIR/"
422 # After making sure that all looks sane, dump the settings to a file
423 # This file will be used to create the REPORT header
424 validate_config >"$JHALFSDIR/jhalfs.config"
425 fi
426
427# Copy optimize files, if needed
428 [[ "$OPTIMIZE" != "0" ]] && cp optimize/opt_override "$JHALFSDIR/"
429
430# Copy compare files, if needed
431 if [[ "$COMPARE" = "y" ]]; then
432 mkdir -p "$JHALFSDIR/extras"
433 cp extras/* "$JHALFSDIR/extras"
434 fi
435
436# Copy custom tools config files, if requested
437 if [[ "${CUSTOM_TOOLS}" = "y" ]]; then
438 echo "Copying custom tool scripts to $JHALFSDIR"
439 mkdir -p "$JHALFSDIR/custom-commands"
440 cp -f custom/config/* "$JHALFSDIR/custom-commands"
441 fi
442
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_}"
449 cd "$CWD" # the functions above change directory
450
451# Install blfs-tool, if requested.
452 if [[ "${BLFS_TOOL}" = "y" ]] ; then
453 echo Installing BLFS book and tools
454 install_blfs_tools 2>&1 | tee -a "$LOGDIR/$LOG"
455 [[ ${PIPESTATUS[0]} != 0 ]] && exit 1
456 fi
457
458fi
459
460# When regenerating the Makefile, we need to know also the
461# canonical book version
462# shellcheck disable=SC2034
463if [[ "$REBUILD_MAKEFILE" = "y" ]] ; then
464 case $PROGNAME in
465 clfs* )
466 VERSION=$(xmllint --noent "$BOOK/prologue/$ARCH/bookinfo.xml" 2>/dev/null | grep subtitle | sed -e 's/^.*ion //' -e 's/<\/.*//') ;;
467 lfs)
468 if [[ "$INITSYS" = "sysv" ]] ; then
469 VERSION=$(grep 'ENTITY version ' "$BOOK/general.ent" | cut -d\" -f2)
470 else
471 VERSION=$(grep 'ENTITY versiond' "$BOOK/general.ent" | cut -d\" -f2)
472 fi
473 ;;
474 *)
475 VERSION=$(xmllint --noent "$BOOK/prologue/bookinfo.xml" 2>/dev/null | grep subtitle | sed -e 's/^.*ion //' -e 's/<\/.*//') ;;
476 esac
477fi
478
479build_Makefile
480
481echo "${SD_BORDER}${nl_}"
482
483# Check for build prerequisites.
484 echo
485 cd "$CWD"
486 check_prerequisites
487 echo "${SD_BORDER}${nl_}"
488# All is well, run the build (if requested)
489run_make
Note: See TracBrowser for help on using the repository browser.