bootstrap: Use pipes by default to speed up the build process
[dragora.git] / bootstrap
blob99d4ae7dd6c7094377ab1ff378a2472aa453c28a
1 #! /bin/sh -
3 # Builder of custom stages (cross compilers, GNU/Linux distributions)
5 # Copyright (c) 2014-2020 Matias Fonzo, <selk@dragora.org>.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # EXIT STATUS
20 # 0 = Successful completion
21 # 1 = Minor common errors (e.g: help usage, support not available)
22 # 2 = Command execution error
23 # 3 = Integrity check error for compressed files
25 PROGRAM="${0##*/}"
27 # Override locale settings
28 LC_ALL=C
29 LANGUAGE=C
30 export LC_ALL LANGUAGE
32 # Get current working directory (absolute path)
33 CWD="$(CDPATH= cd -- $(dirname -- "$0") && pwd)"
35 #### Functions
37 usage() {
38 printf "%s\n" \
39 "Builder of custom stages (cross compilers, GNU/Linux distributions)" \
40 "" \
41 "Usage: $PROGRAM [OPTION]... [FILE]..." \
42 "" \
43 "Defaults for the options are specified in brackets." \
44 "" \
45 "Options:" \
46 " -h display this help and exit" \
47 " -V print version information and exit" \
48 " -a target architecture [${arch}]" \
49 " -j parallel jobs for the compiler [${jobs}]" \
50 " -k keep (don't delete) source directory" \
51 " -o output directory [${output}]" \
52 " -s stage number to build [${stage}]" \
53 "" \
54 "Some influential environment variables:" \
55 " TMPDIR temporary directory for sources [${TMPDIR}]" \
56 " BTCC C compiler command [${BTCC}]" \
57 " BTCXX C++ compiler command [${BTCXX}]" \
58 " BTCFLAGS C compiler flags [${BTCFLAGS}]" \
59 " BTCXXFLAGS C++ compiler flags [${BTCXXFLAGS}]" \
60 " BTLDFLAGS linker flags [${BTLDFLAGS}]" \
61 " VENDOR vendor name to reflect on the target triplet" \
62 "" \
63 "Supported architectures (targets):"
64 for name in "${CWD}/targets"/*
66 printf "%s" "${name##*/} "
67 done
68 unset name
69 echo ""
72 version()
74 printf "%s\n" \
75 "$PROGRAM 3.22" \
76 "Copyright (C) 2014-2020 Matias Andres Fonzo." \
77 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" \
78 "This is free software: you are free to change and redistribute it." \
79 "There is NO WARRANTY, to the extent permitted by law."
82 warn()
84 echo "$@" 1>&2
87 chkstatus_or_exit()
89 status=$?
91 if test $status -ne 0
92 then
93 echo "Return status = $status" 1>&2
94 exit ${1-2}; # If not given, defaults to 2
97 unset status
100 # Function to test and extract compressed files
101 unpack()
103 for file in "$@"
105 case $file in
106 *.tar)
107 tar tf "$file" > /dev/null && \
108 tar xpf "$file"
109 chkstatus_or_exit 3
111 *.tar.gz | *.tgz | *.tar.Z )
112 gzip -cd "$file" | tar tf - > /dev/null && \
113 gzip -cd "$file" | tar xpf -
114 chkstatus_or_exit 3
116 *.tar.bz2 | *.tbz2 | *.tbz )
117 bzip2 -cd "$file" | tar tf - > /dev/null && \
118 bzip2 -cd "$file" | tar xpf -
119 chkstatus_or_exit 3
121 *.tar.lz | *.tlz )
122 lzip -cd "$file" | tar tf - > /dev/null && \
123 lzip -cd "$file" | tar xpf -
124 chkstatus_or_exit 3
126 *.tar.xz | *.txz )
127 xz -cd "$file" | tar tf - > /dev/null && \
128 xz -cd "$file" | tar xpf -
129 chkstatus_or_exit 3
131 *.zip | *.ZIP )
132 unzip -t "$file" > /dev/null && \
133 unzip "$file" > /dev/null
134 chkstatus_or_exit 3
136 *.gz)
137 gzip -t "$file" && \
138 gzip -cd "$file" > "$(basename -- $file .gz)"
139 chkstatus_or_exit 3
141 *.Z)
142 gzip -t "$file" && \
143 gzip -cd "$file" > "$(basename -- $file .Z)"
144 chkstatus_or_exit 3
146 *.bz2)
147 bzip2 -t "$file" && \
148 bzip2 -cd "$file" > "$(basename -- $file .bz2)"
149 chkstatus_or_exit 3
151 *.lz)
152 lzip -t "$file" && \
153 lzip -cd "$file" > "$(basename -- $file .lz)"
154 chkstatus_or_exit 3
156 *.xz)
157 xz -t "$file" && \
158 xz -cd "$file" > "$(basename -- $file .xz)"
159 chkstatus_or_exit 3
162 warn "${PROGRAM}: cannot unpack ${file}: Unsupported extension"
163 exit 1
164 esac
165 done
166 unset file
169 # Print a warning for good practices.
171 # Recommended practices is to set variables
172 # in front of `configure' or make(1), see:
174 # http://www.gnu.org/software/make/manual/html_node/Environment.html
175 # http://gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Defining-Variables.html
176 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Setting-Output-Variables.html
178 warn_flags()
180 warn "WARNING: Environment variable '$1' is set." \
181 "This will be unset to avoid possible risks." \
185 #### Default values
187 BTCC="${BTCC:=cc}"
188 BTCXX="${BTCXX:=c++}"
189 BTCFLAGS="${BTCFLAGS:=-g0 -Os -pipe}"
190 BTCXXFLAGS="${BTCXXFLAGS:=-g0 -Os -pipe}"
191 BTLDFLAGS="${BTLDFLAGS:=-s}"
192 opt_keep=opt_keep.off
193 stage=0
194 libSuffix=""
195 arch="$(uname -m)" || chkstatus_or_exit
196 jobs=1
197 worktree="$CWD"
198 output=${worktree}/OUTPUT.${PROGRAM}
199 TMPDIR="${TMPDIR:=${output}/sources}"
201 # Compose vendor name adding "-" as suffix
202 test -n "$VENDOR" && VENDOR="${VENDOR}-"
204 #### Parse options
206 while getopts :ha:j:ko:s:V name
208 case $name in
210 usage
211 exit 0
214 arch="$OPTARG"
217 jobs="$OPTARG"
220 opt_keep=opt_keep
223 output="$OPTARG"
226 stage="$OPTARG"
229 version
230 exit 0
233 warn "Option '-${OPTARG}' requires an argument"
234 usage
235 exit 1
238 warn "Illegal option -- '-${OPTARG}'"
239 usage
240 exit 1
242 esac
243 done
244 shift $(( OPTIND - 1 ))
246 # Check for environment variables, print warning, unset in any case
248 test "${CC:+CC_defined}" = CC_defined && warn_flags CC
249 test "${CXX:+CXX_defined}" = CXX_defined && warn_flags CXX
250 test "${CFLAGS:+CFLAGS_defined}" = CFLAGS_defined && warn_flags CFLAGS
251 test "${CXXFLAGS:+CXXFLAGS_defined}" = CXXFLAGS_defined && warn_flags CXXFLAGS
252 test "${LDFLAGS:+LDFLAGS_defined}" = LDFLAGS_defined && warn_flags LDFLAGS
253 unset CC CXX CFLAGS CXXFLAGS LDFLAGS warn_flags
255 # Load target architecture-file
257 if test -f "${worktree}/targets/$arch"
258 then
259 echo "${PROGRAM}: Loading target $arch ..."
260 . "${worktree}/targets/$arch"
261 else
262 warn \
263 "${PROGRAM}: Target name not recognized -- '${arch}'" \
264 "See '$0 -h' for more information"
265 exit 1
268 # Determine the host to use.
270 # Set up the host if a C compiler command was exported,
271 # otherwise, a local `cc' will be used instead
273 if test -n "$BTCC"
274 then
275 host="$(${BTCC} -dumpmachine)" || chkstatus_or_exit
276 else
277 host="$(cc -dumpmachine)" || chkstatus_or_exit
280 # If the host and the target are the same triplet, it won't work.
281 # We are changing the host if it is really needed
283 if test "$host" = "$target"
284 then
285 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
286 case "${host%-*-*}" in
287 *-*)
288 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
291 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
293 esac
296 # Compose variables for the physical output,
297 # printing some useful information
299 crossdir=${output}/cross/${target}
300 rootdir=${output}/stage${stage}
302 printf "%s\n" \
303 "BTCC: $BTCC" \
304 "BTCXX: $BTCXX" \
305 "BTCFLAGS: $BTCFLAGS" \
306 "BTCXXFLAGS: $BTCXXFLAGS" \
307 "BTLDFLAGS: $BTLDFLAGS" \
308 "Host: $host" \
309 "Target: $target" \
310 "Output directory: $output" \
311 "Cross directory: $crossdir" \
312 "Root directory: $rootdir"
314 # Remove write permission for group and other
315 umask 022
317 # Create required directories
318 mkdir -p -- "$output" "$TMPDIR"
319 chkstatus_or_exit
321 # Set default PATH, propagate variables
322 PATH="${crossdir}/bin:${PATH}"
324 export PATH VENDOR TMPDIR
326 # Main loop
327 for file in "${CWD}/stages/${stage}"/${@:-??-*}
329 file="${file##*/}"
331 if test ! -f "${CWD}/stages/${stage}/$file"
332 then
333 warn "${PROGRAM}: ${stage}/${file}: No such file or stage number"
334 exit 1
336 if test ! -x "${CWD}/stages/${stage}/$file"
337 then
338 warn "${PROGRAM}: ${stage}/${file}: File not executable, dodging"
339 continue;
342 #### Stage pre-settings
344 # Create sysroot directory, recreating the symlink for the stage 0
346 if test "$stage" = 0
347 then
348 mkdir -p -- "${crossdir}/$target" || chkstatus_or_exit
350 if test ! -e "${crossdir}/${target}/usr"
351 then
352 ln -sf . "${crossdir}/${target}/usr" || chkstatus_or_exit
355 # Ensure toolchain sanity for multilib purposes
356 case $arch in
357 aarch64* | arm* | i586 | *x32 | x86_64 | riscv* )
358 if test ! -e "${crossdir}/lib" && test -n "$libSuffix"
359 then
360 ln -sf lib${libSuffix} "${crossdir}/lib" || chkstatus_or_exit
363 esac
366 # Create "tools" directory, recreating the symlink for the stage 1
367 if test "$stage" = 1
368 then
369 if test ! -d "${rootdir}/tools"
370 then
371 mkdir -p -- "${rootdir}/tools" || chkstatus_or_exit
374 # Make required symlink
375 ln -sf "${rootdir}/tools" / || chkstatus_or_exit
378 echo "${PROGRAM}: Processing $file from stage $stage ..."
380 # Set trap before to run the script in order to
381 # catch the return status, exit code 2 if fails
383 trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
385 # Exit immediately on any error
386 set -e
388 . "${CWD}/stages/${stage}/$file"
390 # Deactivate shell option(s)
391 set +e
393 # Reset given signals
394 trap - EXIT HUP INT QUIT ABRT TERM
396 # Delete declared directories on the cleanup() function
397 if test "$opt_keep" != opt_keep
398 then
399 if type cleanup 1> /dev/null 2> /dev/null
400 then
401 cleanup
402 chkstatus_or_exit 2
403 unset cleanup
407 # Back to the current working directory
408 cd -- "$CWD" || chkstatus_or_exit
409 done