2 # Builder of custom stages (cross compilers, GNU/Linux distributions)
4 # Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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:=g++}"
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 ))
249 unset -f usage version
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 # If the host and the target are the same triplet, it won't work.
276 # We are changing the host if it is really needed
278 host="$(${BTCC} -dumpmachine)" || chkstatus_or_exit
280 if test "$host" = "$target"
282 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
283 case "${host%-*-*}" in
285 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
288 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
293 # Compose variables for the physical output,
294 # printing some useful information
296 crossdir
="${output}/cross/${target}"
297 rootdir
="${output}/stage${stage}"
302 "BTCFLAGS: $BTCFLAGS" \
303 "BTCXXFLAGS: $BTCXXFLAGS" \
304 "BTLDFLAGS: $BTLDFLAGS" \
307 "Output directory: $output" \
308 "Cross directory: $crossdir" \
309 "Root directory: $rootdir"
311 # Remove write permission for group and other
314 # Create required directories
315 mkdir
-p -- "$output" "$TMPDIR"
318 # Set default PATH, propagate variables
319 PATH
="${crossdir}/bin:${PATH}"
321 export PATH VENDOR TMPDIR
324 for file in ${CWD}/stages/${stage}/${@:-??-*}
328 if test ! -f "${CWD}/stages/${stage}/$file"
330 warn
"${PROGRAM}: ${stage}/${file}: No such file or stage number"
333 if test ! -x "${CWD}/stages/${stage}/$file"
335 warn
"${PROGRAM}: ${stage}/${file}: File not executable, dodging"
339 ### Stage pre-settings
341 # Create sysroot directory, recreating the symlink for the stage 0
345 mkdir
-p -- "${crossdir}/$target" || chkstatus_or_exit
347 if test ! -L "${crossdir}/${target}/usr"
349 ln -s -f .
"${crossdir}/${target}/usr" || chkstatus_or_exit
352 # Ensure toolchain sanity for multilib purposes
354 aarch64
* | arm
* | x32 | x86_64 | i?
86 | riscv
* )
355 if test ! -L "${crossdir}/lib" && test -n "$libSuffix"
357 ln -s -f lib
${libSuffix} "${crossdir}/lib" || chkstatus_or_exit
363 # Create "tools" directory, recreating the symlink for the stage 1
366 if test ! -d "${rootdir}/tools"
368 mkdir
-p -- "${rootdir}/tools" || chkstatus_or_exit
371 # Check and make required symlink
374 ln -s -f "${rootdir}/tools" / || chkstatus_or_exit
378 echo "${PROGRAM}: Processing $file from stages/${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
388 .
"${CWD}/stages/${stage}/$file"
390 # Deactivate shell option(s)
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
399 if type cleanup
1> /dev
/null
2> /dev
/null
407 # Back to the current working directory
408 cd -- "$CWD" || chkstatus_or_exit