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.
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 Targets from ${CWD}/targets/ ..."
64 for name
in "${CWD}/targets"/*
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.
85 printf '%s\n' "$@" 1>&2
94 echo "Return status = $status" 1>&2
95 exit "${1-2}"; # If not given, defaults to 2
101 # Function to test and extract compressed files
108 tar tf
"$file" > /dev
/null
&& \
112 *.
tar.gz |
*.tgz |
*.
tar.Z
)
113 gzip -cd "$file" |
tar tf
- > /dev
/null
&& \
114 gzip -cd "$file" |
tar xpf
-
117 *.
tar.bz2 |
*.tbz2 |
*.tbz
)
118 bzip2 -cd "$file" |
tar tf
- > /dev
/null
&& \
119 bzip2 -cd "$file" |
tar xpf
-
123 lzip
-cd "$file" |
tar tf
- > /dev
/null
&& \
124 lzip
-cd "$file" |
tar xpf
-
128 xz
-cd "$file" |
tar tf
- > /dev
/null
&& \
129 xz
-cd "$file" |
tar xpf
-
133 unzip -t "$file" > /dev
/null
&& \
134 unzip "$file" > /dev
/null
139 gzip -cd "$file" > "$(basename -- "$file" .gz)"
144 gzip -cd "$file" > "$(basename -- "$file" .Z)"
148 bzip2 -t "$file" && \
149 bzip2 -cd "$file" > "$(basename -- "$file" .bz2)"
154 lzip
-cd "$file" > "$(basename -- "$file" .lz)"
159 xz
-cd "$file" > "$(basename -- "$file" .xz)"
163 warn
"${PROGRAM}: cannot unpack ${file}: Unsupported extension"
170 # Print a warning for good practices.
172 # Recommended practices is to set variables
173 # in front of `configure' or make(1), see:
175 # http://www.gnu.org/software/make/manual/html_node/Environment.html
176 # http://gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Defining-Variables.html
177 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Setting-Output-Variables.html
182 "WARNING: Environment variable '$1' is set." \
183 "This will be unset to avoid possible risks." \
190 BTCXX
="${BTCXX:=c++}"
191 BTCFLAGS
="${BTCFLAGS:=-g0 -O2 -pipe}"
192 BTCXXFLAGS
="${BTCXXFLAGS:=-g0 -O2 -pipe}"
193 BTLDFLAGS
="${BTLDFLAGS:=-s}"
194 opt_keep
=opt_keep.off
197 arch
="$(uname -m)" || chkstatus_or_exit
200 output
=${worktree}/OUTPUT.
${PROGRAM}
201 TMPDIR
="${TMPDIR:=${output}/sources}"
203 # Compose vendor name adding "-" as suffix
204 test -n "$VENDOR" && VENDOR
="${VENDOR}-"
208 while getopts :ha
:j
:ko
:s
:V name
235 warn
"Option '-${OPTARG}' requires an argument"
240 warn
"Illegal option -- '-${OPTARG}'"
246 shift $
(( OPTIND
- 1 ))
248 # Check for environment variables, print warning, unset in any case
250 test -n "$CC" && warn_flags CC
251 test -n "$CXX" && warn_flags CXX
252 test -n "$CFLAGS" && warn_flags CFLAGS
253 test -n "$CXXFLAGS" && warn_flags CXXFLAGS
254 test -n "$LDFLAGS" && warn_flags LDFLAGS
255 unset CC CXX CFLAGS CXXFLAGS LDFLAGS warn_flags
257 # Load target architecture-file
259 if test -f "${worktree}/targets/$arch"
261 echo "${PROGRAM}: Loading target $arch ..."
262 .
"${worktree}/targets/$arch"
265 "${PROGRAM}: Target name not recognized -- '${arch}'" \
266 "See '$0 -h' for more information"
270 # Determine the host to use.
272 # Set up the host if a C compiler command was exported,
273 # otherwise, a local `cc' will be used instead
277 host="$(${BTCC} -dumpmachine)" || chkstatus_or_exit
279 host="$(cc -dumpmachine)" || chkstatus_or_exit
282 # If the host and the target are the same triplet, it won't work.
283 # We are changing the host if it is really needed
285 if test "$host" = "$target"
287 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
288 case "${host%-*-*}" in
290 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
293 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
298 # Compose variables for the physical output,
299 # printing some useful information
301 crossdir
=${output}/cross
/${target}
302 rootdir
=${output}/stage
${stage}
307 "BTCFLAGS: $BTCFLAGS" \
308 "BTCXXFLAGS: $BTCXXFLAGS" \
309 "BTLDFLAGS: $BTLDFLAGS" \
312 "Output directory: $output" \
313 "Cross directory: $crossdir" \
314 "Root directory: $rootdir"
316 # Remove write permission for group and other
319 # Create required directories
320 mkdir
-p -- "$output" "$TMPDIR"
323 # Set default PATH, propagate variables
324 PATH
="${crossdir}/bin:${PATH}"
326 export PATH VENDOR TMPDIR
329 for file in ${CWD}/stages/${stage}/${@:-??-*}
333 if test ! -f "${CWD}/stages/${stage}/$file"
335 warn
"${PROGRAM}: ${stage}/${file}: No such file or stage number"
338 if test ! -x "${CWD}/stages/${stage}/$file"
340 warn
"${PROGRAM}: ${stage}/${file}: File not executable, dodging"
344 ### Stage pre-settings
346 # Create sysroot directory, recreating the symlink for the stage 0
350 mkdir
-p -- "${crossdir}/$target" || chkstatus_or_exit
352 if test ! -e "${crossdir}/${target}/usr"
354 ln -sf .
"${crossdir}/${target}/usr" || chkstatus_or_exit
357 # Ensure toolchain sanity for multilib purposes
359 aarch64
* | arm
* | i586 |
*x32 | x86_64 | riscv
* )
360 if test ! -e "${crossdir}/lib" && test -n "$libSuffix"
362 ln -sf lib
${libSuffix} "${crossdir}/lib" || chkstatus_or_exit
368 # Create "tools" directory, recreating the symlink for the stage 1
371 if test ! -d "${rootdir}/tools"
373 mkdir
-p -- "${rootdir}/tools" || chkstatus_or_exit
376 # Make required symlink
377 ln -sf "${rootdir}/tools" / || chkstatus_or_exit
380 echo "${PROGRAM}: Processing $file from stage $stage ..."
382 # Set trap before to run the script in order to
383 # catch the return status, exit code 2 if fails
385 trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
387 # Exit immediately on any error
390 .
"${CWD}/stages/${stage}/$file"
392 # Deactivate shell option(s)
395 # Reset given signals
396 trap - EXIT HUP INT QUIT ABRT TERM
398 # Delete declared directories on the cleanup() function
399 if test "$opt_keep" != opt_keep
401 if type cleanup
1> /dev
/null
2> /dev
/null
409 # Back to the current working directory
410 cd -- "$CWD" || chkstatus_or_exit