3 # Builder of custom stages (cross compilers, GNU/Linux distributions)
5 # Copyright (C) 2014-2017 Matias Andres Fonzo <selk@dragora.org>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # 0 = Successful completion
22 # 1 = Minor common errors (e.g: help usage, support not available)
23 # 2 = Command execution error
24 # 3 = Integrity check error for compressed files
28 # Override locale settings
31 export LC_ALL LANGUAGE
33 # Get physical working directory, absolute path name
34 CWD
=$
(CDPATH
= cd -P -- $
(dirname -- "$0") && printf "$PWD")
40 "Builder of custom stages (cross compilers, GNU/Linux distributions)" \
42 "Usage: $PROGRAM [OPTION]... [FILE]..." \
44 "Defaults for the options are specified in brackets." \
47 " -h display this help and exit" \
48 " -V print version information and exit" \
49 " -a target architecture [${arch}]" \
50 " -j parallel jobs for the compiler [${jobs}]" \
51 " -k keep (don't delete) source directory" \
52 " -o output directory [${output}]" \
53 " -s stage number to build [${stage}]" \
55 "Some influential environment variables:" \
56 " TMPDIR temporary directory for sources [${TMPDIR}]" \
57 " BTCC C compiler command [${BTCC}]" \
58 " BTCXX C++ compiler command [${BTCXX}]" \
59 " BTCFLAGS C compiler flags [${BTCFLAGS}]" \
60 " BTCXXFLAGS C++ compiler flags [${BTCXXFLAGS}]" \
61 " BTLDFLAGS linker flags [${BTLDFLAGS}]" \
62 " VENDOR vendor name to reflect on the target triplet" \
64 "Supported architectures (targets):"
65 for arch
in "${CWD}/targets"/*
76 "Copyright (C) 2014-2017 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."
93 echo "Return status = $status" 1>&2
94 exit ${1-2}; # If not given, defaults to 2
100 # Function to test and extract tarball files
102 tar tf
"$1" > /dev
/null
&& {
103 cd -- "$2" && tar xpf
"$1"
105 checkstatus_or_exit
3
108 #### Warning for good practices
110 # Recommended practices is to set variables in front of `configure',
111 # or make(1), see also:
113 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Defining-Variables.html
114 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Setting-Output-Variables.html
115 # http://www.gnu.org/software/make/manual/make.html#Environment
119 warn
"WARNING: Environment variable '$1' is set." \
120 "This will be unset to avoid possible risks." \
127 BTCXX
="${BTCXX:=c++}"
128 BTCFLAGS
="${BTCFLAGS:=-g0 -Os}"
129 BTCXXFLAGS
="${BTCXXFLAGS:=$BTCFLAGS}"
130 BTLDFLAGS
="${BTLDFLAGS:=-s}"
132 arch
="$(uname -m)" ||
exit 2
134 opt_keep
=opt_keep.off
135 output
=${worktree}/OUTPUT.
${PROGRAM}
136 TMPDIR
="${TMPDIR:=${output}/sources}"
139 # Compose vendor name adding "-" as suffix
140 test -n "$VENDOR" && VENDOR
="${VENDOR}-"
144 while getopts :ha
:j
:ko
:s
:V name
171 warn
"Option '-${OPTARG}' requires an argument"
176 warn
"Illegal option -- '-${OPTARG}'"
182 shift $
(( OPTIND
- 1 ))
184 # Check for environment variables, print warning, unset in any case
185 test "${CC:+CC_defined}" = CC_defined
&& warn_flags CC
186 test "${CXX:+CXX_defined}" = CXX_defined
&& warn_flags CXX
187 test "${CFLAGS:+CFLAGS_defined}" = CFLAGS_defined
&& warn_flags CFLAGS
188 test "${CXXFLAGS:+CXXFLAGS_defined}" = CXXFLAGS_defined
&& warn_flags CXXFLAGS
189 test "${LDFLAGS:+LDFLAGS_defined}" = LDFLAGS_defined
&& warn_flags LDFLAGS
191 unset CC CXX CFLAGS CXXFLAGS LDFLAGS warn_flags
193 # Avoid exportation of 'libSuffix' when a target is loaded
196 # Load target architecture-file
198 if test -f "${worktree}/targets/$arch"
200 echo "${PROGRAM}: Loading target $arch ..."
201 .
"${worktree}/targets/$arch"
204 "${PROGRAM}: Target name not recognized -- '${arch}'" \
205 "See '$0 -h' for more information"
209 # Determine the host to use.
211 # Set up the host if a C compiler command was exported,
212 # otherwise, a local `cc' will be used instead
216 host="$(${BTCC} -dumpmachine)" ||
exit 2
218 host="$(cc -dumpmachine)" ||
exit 2
221 # If the host and the target are the same triplet, it won't work.
222 # We are changing the host if it is really needed
224 if test "$host" = "$target"
226 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
227 case "${host%-*-*}" in
229 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
232 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
237 # Compose variables for the physical output,
238 # print some useful information
240 crossdir
=${output}/cross
/${target}
241 rootdir
=${output}/stage
${stage}
246 "BTCFLAGS: $BTCFLAGS" \
247 "BTCXXFLAGS: $BTCXXFLAGS" \
248 "BTLDFLAGS: $BTLDFLAGS" \
251 "Output directory: $output" \
252 "Cross directory: $crossdir" \
253 "Root directory: $rootdir"
255 # Remove write permission for group and other
258 # Create required directories
259 if test ! -d "$output"
261 mkdir
-p -- "$output" ||
exit 2
263 if test ! -d "$TMPDIR"
265 mkdir
-p -- "$TMPDIR" ||
exit 2
268 # Set default path and propagate variables
270 PATH
="${crossdir}/bin:${PATH}"
271 export PATH VENDOR TMPDIR
274 for file in "${CWD}/stages/${stage}"/${@:-??-*}
278 if test ! -f "${CWD}/stages
/${stage}/$file"
280 warn "${PROGRAM}: ${stage}/${file}: No such
file or stage number
"
283 if test ! -x "${CWD}/stages
/${stage}/$file"
285 warn "${PROGRAM}: ${stage}/${file}: File not executable
, dodging
"
289 #### Stage pre-settings
291 # Create sysroot directory, recreating the symlink for the stage 0
295 mkdir -p -- "${crossdir}/$target" || exit 2
297 if test ! -e "${crossdir}/${target}/usr
"
299 ln -sf . "${crossdir}/${target}/usr
" || exit 2
302 # Ensure toolchain sanity for multilib purposes
304 i586 | *x32 | x86_64 )
305 if test ! -e "${crossdir}/lib
" -a -n "$libSuffix"
307 ln -sf lib${libSuffix} "${crossdir}/lib
" || exit 2
313 # Create "tools
" directory, recreating the symlink for the stage 1
316 if test ! -d "${rootdir}/tools
"
318 mkdir -p -- "${rootdir}/tools
" || exit 2
321 # Make required symlink
322 ln -sf "${rootdir}/tools
" / || exit 2
325 #### Load the file containing the instruction
327 echo "${PROGRAM}: Processing
$file from stage
$stage ...
"
329 # Exit immediately on any error
332 . "${CWD}/stages
/${stage}/$file" || checkstatus_or_exit 2
334 # Deactivate shell option
337 # Delete declared directories on the cleanup() function
338 if test "$opt_keep" != opt_keep
340 if type cleanup 1> /dev/null 2> /dev/null
342 cleanup || checkstatus_or_exit 2
347 # Back to the current working directory
348 cd -- "$CWD" || exit 2