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 $?
39 "Builder of custom stages (cross compilers, GNU/Linux distributions)" \
41 "Usage: $PROGRAM [OPTION...] [FILE]..." \
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."
84 printf '%s\n' "$@" 1>&2
93 echo "Return status = $status" 1>&2
94 exit "${1-2}"; # If not given, defaults to 2
100 # Function to test and extract compressed files
107 tar tf
"$file" > /dev
/null
&& \
111 *.
tar.gz |
*.tgz |
*.
tar.Z
)
112 gzip -cd "$file" |
tar tf
- > /dev
/null
&& \
113 gzip -cd "$file" |
tar xpf
-
116 *.
tar.bz2 |
*.tbz2 |
*.tbz
)
117 bzip2 -cd "$file" |
tar tf
- > /dev
/null
&& \
118 bzip2 -cd "$file" |
tar xpf
-
122 lzip
-cd "$file" |
tar tf
- > /dev
/null
&& \
123 lzip
-cd "$file" |
tar xpf
-
127 xz
-cd "$file" |
tar tf
- > /dev
/null
&& \
128 xz
-cd "$file" |
tar xpf
-
132 unzip -t "$file" > /dev
/null
&& \
133 unzip "$file" > /dev
/null
138 gzip -cd "$file" > "$(basename -- "$file" .gz)"
143 gzip -cd "$file" > "$(basename -- "$file" .Z)"
147 bzip2 -t "$file" && \
148 bzip2 -cd "$file" > "$(basename -- "$file" .bz2)"
153 lzip
-cd "$file" > "$(basename -- "$file" .lz)"
158 xz
-cd "$file" > "$(basename -- "$file" .xz)"
162 warn
"${PROGRAM}: cannot unpack ${file}: Unsupported extension"
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
181 "WARNING: Environment variable '$1' is set." \
182 "This will be unset to avoid possible risks." \
189 BTCXX
="${BTCXX:=c++}"
190 BTCFLAGS
="${BTCFLAGS:=-g0 -Os -pipe}"
191 BTCXXFLAGS
="${BTCXXFLAGS:=-g0 -Os -pipe}"
192 BTLDFLAGS
="${BTLDFLAGS:=-s}"
193 opt_keep
=opt_keep.off
196 arch
="$(uname -m)" || chkstatus_or_exit
199 output
=${worktree}/OUTPUT.
${PROGRAM}
200 TMPDIR
="${TMPDIR:=${output}/sources}"
202 # Compose vendor name adding "-" as suffix
203 test -n "$VENDOR" && VENDOR
="${VENDOR}-"
207 while getopts :ha
:j
:ko
:s
:V name
234 warn
"Option '-${OPTARG}' requires an argument"
239 warn
"Illegal option -- '-${OPTARG}'"
245 shift $
(( OPTIND
- 1 ))
247 # Check for environment variables, print warning, unset in any case
249 test -n "$CC" && warn_flags CC
250 test -n "$CXX" && warn_flags CXX
251 test -n "$CFLAGS" && warn_flags CFLAGS
252 test -n "$CXXFLAGS" && warn_flags CXXFLAGS
253 test -n "$LDFLAGS" && warn_flags LDFLAGS
254 unset CC CXX CFLAGS CXXFLAGS LDFLAGS warn_flags
256 # Load target architecture-file
258 if test -f "${worktree}/targets/$arch"
260 echo "${PROGRAM}: Loading target $arch ..."
261 .
"${worktree}/targets/$arch"
264 "${PROGRAM}: Target name not recognized -- '${arch}'" \
265 "See '$0 -h' for more information"
269 # Determine the host to use.
271 # Set up the host if a C compiler command was exported,
272 # otherwise, a local `cc' will be used instead
276 host="$(${BTCC} -dumpmachine)" || chkstatus_or_exit
278 host="$(cc -dumpmachine)" || chkstatus_or_exit
281 # If the host and the target are the same triplet, it won't work.
282 # We are changing the host if it is really needed
284 if test "$host" = "$target"
286 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
287 case "${host%-*-*}" in
289 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
292 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
297 # Compose variables for the physical output,
298 # printing some useful information
300 crossdir
=${output}/cross
/${target}
301 rootdir
=${output}/stage
${stage}
306 "BTCFLAGS: $BTCFLAGS" \
307 "BTCXXFLAGS: $BTCXXFLAGS" \
308 "BTLDFLAGS: $BTLDFLAGS" \
311 "Output directory: $output" \
312 "Cross directory: $crossdir" \
313 "Root directory: $rootdir"
315 # Remove write permission for group and other
318 # Create required directories
319 mkdir
-p -- "$output" "$TMPDIR"
322 # Set default PATH, propagate variables
323 PATH
="${crossdir}/bin:${PATH}"
325 export PATH VENDOR TMPDIR
328 for file in ${CWD}/stages/${stage}/${@:-??-*}
332 if test ! -f "${CWD}/stages/${stage}/$file"
334 warn
"${PROGRAM}: ${stage}/${file}: No such file or stage number"
337 if test ! -x "${CWD}/stages/${stage}/$file"
339 warn
"${PROGRAM}: ${stage}/${file}: File not executable, dodging"
343 ### Stage pre-settings
345 # Create sysroot directory, recreating the symlink for the stage 0
349 mkdir
-p -- "${crossdir}/$target" || chkstatus_or_exit
351 if test ! -e "${crossdir}/${target}/usr"
353 ln -sf .
"${crossdir}/${target}/usr" || chkstatus_or_exit
356 # Ensure toolchain sanity for multilib purposes
358 aarch64
* | arm
* | i586 |
*x32 | x86_64 | riscv
* )
359 if test ! -e "${crossdir}/lib" && test -n "$libSuffix"
361 ln -sf lib
${libSuffix} "${crossdir}/lib" || chkstatus_or_exit
367 # Create "tools" directory, recreating the symlink for the stage 1
370 if test ! -d "${rootdir}/tools"
372 mkdir
-p -- "${rootdir}/tools" || chkstatus_or_exit
375 # Make required symlink
376 ln -sf "${rootdir}/tools" / || chkstatus_or_exit
379 echo "${PROGRAM}: Processing $file from stage $stage ..."
381 # Set trap before to run the script in order to
382 # catch the return status, exit code 2 if fails
384 trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
386 # Exit immediately on any error
389 .
"${CWD}/stages/${stage}/$file"
391 # Deactivate shell option(s)
394 # Reset given signals
395 trap - EXIT HUP INT QUIT ABRT TERM
397 # Delete declared directories on the cleanup() function
398 if test "$opt_keep" != opt_keep
400 if type cleanup
1> /dev
/null
2> /dev
/null
408 # Back to the current working directory
409 cd -- "$CWD" || chkstatus_or_exit