3 # Builder of custom stages (cross compilers, GNU/Linux distributions)
5 # Copyright (c) 2014-2022 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.
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
27 # Override locale settings
30 export LC_ALL LANGUAGE
32 # Get physical working directory (absolute path)
33 CWD
="$(CDPATH='' cd -P -- "$
(dirname -- "$0")" && pwd -P)" ||
exit $?
40 "Usage: $PROGRAM [OPTIONS] [FILE]...
41 Builder of custom stages (cross compilers, GNU/Linux distributions).
43 Defaults for the options are specified in brackets.
46 -a Target architecture [${arch}]
47 -j Parallel jobs for the compiler [${jobs}]
48 -k Keep (don't delete) source directory
49 -o Output directory [${output}]
50 -s Stage number to build [${stage}]
51 -h Display this help and exit
52 -V Print version information and exit
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
63 Available targets from ${CWD}/targets ...
66 for name
in "${CWD}/targets"/*
78 Copyright (C) 2014-2022 Matias Andres Fonzo.
79 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
80 This is free software: you are free to change and redistribute it.
81 There is NO WARRANTY, to the extent permitted by law.
87 printf '%s\n' "$@" 1>&2
96 echo "Return status = $status" 1>&2
97 exit "${1-2}"; # If not given, defaults to 2
103 # Function to test and extract compressed files
110 tar tf
"$file" > /dev
/null
&& \
114 *.
tar.gz |
*.tgz |
*.
tar.Z
)
115 gzip -cd "$file" |
tar tf
- > /dev
/null
&& \
116 gzip -cd "$file" |
tar xpf
-
119 *.
tar.bz2 |
*.tbz2 |
*.tbz
)
120 bzip2 -cd "$file" |
tar tf
- > /dev
/null
&& \
121 bzip2 -cd "$file" |
tar xpf
-
125 lzip
-cd "$file" |
tar tf
- > /dev
/null
&& \
126 lzip
-cd "$file" |
tar xpf
-
130 xz
-cd "$file" |
tar tf
- > /dev
/null
&& \
131 xz
-cd "$file" |
tar xpf
-
135 unzip -t "$file" > /dev
/null
&& \
136 unzip "$file" > /dev
/null
141 gzip -cd "$file" > "$(basename -- "$file" .gz)"
146 gzip -cd "$file" > "$(basename -- "$file" .Z)"
150 bzip2 -t "$file" && \
151 bzip2 -cd "$file" > "$(basename -- "$file" .bz2)"
156 lzip
-cd "$file" > "$(basename -- "$file" .lz)"
161 xz
-cd "$file" > "$(basename -- "$file" .xz)"
165 warn
"${PROGRAM}: cannot unpack ${file}: Unsupported extension"
172 # Print a warning for good practices.
174 # Recommended practices is to set variables
175 # in front of `configure' or make(1), see:
177 # http://www.gnu.org/software/make/manual/html_node/Environment.html
178 # http://gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Defining-Variables.html
179 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Setting-Output-Variables.html
184 "WARNING: Environment variable '$1' is set." \
185 "This will be unset to avoid possible risks." \
192 BTCXX
="${BTCXX:=c++}"
193 BTCFLAGS
="${BTCFLAGS:=-O2}"
194 BTCXXFLAGS
="${BTCXXFLAGS:=-O2}"
195 BTLDFLAGS
="${BTLDFLAGS:=}"
196 opt_keep
=opt_keep.off
199 arch
="$(uname -m)" || chkstatus_or_exit
202 output
="${worktree}/OUTPUT.${PROGRAM}"
203 TMPDIR
="${TMPDIR:=${output}/sources}"
205 # Compose vendor name adding "-" as suffix
206 test -n "$VENDOR" && VENDOR
="${VENDOR}-"
210 while getopts :ha
:j
:ko
:s
:V name
237 warn
"Option '-${OPTARG}' requires an argument"
242 warn
"Illegal option -- '-${OPTARG}'"
248 shift $
(( OPTIND
- 1 ))
251 # Check for environment variables, print warning, unset in any case
253 test -n "$CC" && warn_flags CC
254 test -n "$CXX" && warn_flags CXX
255 test -n "$CFLAGS" && warn_flags CFLAGS
256 test -n "$CXXFLAGS" && warn_flags CXXFLAGS
257 test -n "$LDFLAGS" && warn_flags LDFLAGS
258 unset CC CXX CFLAGS CXXFLAGS LDFLAGS warn_flags
260 # Load target architecture-file
262 if test -f "${worktree}/targets/$arch"
264 echo "${PROGRAM}: Loading target $arch ..."
265 .
"${worktree}/targets/$arch"
268 "${PROGRAM}: Target name not recognized -- '${arch}'" \
269 "See '$0 -h' for more information"
273 # Determine the host to use.
275 # Set up the host if a C compiler command was exported,
276 # otherwise, a local `cc' will be used instead
280 host="$(${BTCC} -dumpmachine)" || chkstatus_or_exit
282 host="$(cc -dumpmachine)" || chkstatus_or_exit
285 # If the host and the target are the same triplet, it won't work.
286 # We are changing the host if it is really needed
288 if test "$host" = "$target"
290 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
291 case "${host%-*-*}" in
293 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
296 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
301 # Compose variables for the physical output,
302 # printing some useful information
304 crossdir
="${output}/cross/${target}"
305 rootdir
="${output}/stage${stage}"
310 "BTCFLAGS: $BTCFLAGS" \
311 "BTCXXFLAGS: $BTCXXFLAGS" \
312 "BTLDFLAGS: $BTLDFLAGS" \
315 "Output directory: $output" \
316 "Cross directory: $crossdir" \
317 "Root directory: $rootdir"
319 # Remove write permission for group and other
322 # Create required directories
323 mkdir
-p -- "$output" "$TMPDIR"
326 # Set default PATH, propagate variables
327 PATH
="${crossdir}/bin:${PATH}"
329 export PATH VENDOR TMPDIR
332 for file in ${CWD}/stages/${stage}/${@:-??-*}
336 if test ! -f "${CWD}/stages/${stage}/$file"
338 warn
"${PROGRAM}: ${stage}/${file}: No such file or stage number"
341 if test ! -x "${CWD}/stages/${stage}/$file"
343 warn
"${PROGRAM}: ${stage}/${file}: File not executable, dodging"
347 ### Stage pre-settings
349 # Create sysroot directory, recreating the symlink for the stage 0
353 mkdir
-p -- "${crossdir}/$target" || chkstatus_or_exit
355 if test ! -e "${crossdir}/${target}/usr"
357 ln -sf .
"${crossdir}/${target}/usr" || chkstatus_or_exit
360 # Ensure toolchain sanity for multilib purposes
362 aarch64
* | arm
* | x32 | x86_64 | i?
86 | riscv
* )
363 if test ! -e "${crossdir}/lib" && test -n "$libSuffix"
365 ln -sf lib
${libSuffix} "${crossdir}/lib" || chkstatus_or_exit
371 # Create "tools" directory, recreating the symlink for the stage 1
374 if test ! -d "${rootdir}/tools"
376 mkdir
-p -- "${rootdir}/tools" || chkstatus_or_exit
379 # Make required symlink
380 ln -sf "${rootdir}/tools" / || chkstatus_or_exit
383 echo "${PROGRAM}: Processing $file from stages/${stage} ..."
385 # Set trap before to run the script in order to
386 # catch the return status, exit code 2 if fails
388 trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
390 # Exit immediately on any error
393 .
"${CWD}/stages/${stage}/$file"
395 # Deactivate shell option(s)
398 # Reset given signals
399 trap - EXIT HUP INT QUIT ABRT TERM
401 # Delete declared directories on the cleanup() function
402 if test "$opt_keep" != opt_keep
404 if type cleanup
1> /dev
/null
2> /dev
/null
412 # Back to the current working directory
413 cd -- "$CWD" || chkstatus_or_exit