targets: remove explicit --enable-multilib for i586, x32
[dragora.git] / bootstrap
blobf7a0f8192cfbe9613f5dc0aeb2d84728bdf16686
1 #! /bin/sh -
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/>.
20 # EXIT STATUS
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
26 PROGRAM="${0##*/}"
28 # Override locale settings
29 LC_ALL=C
30 LANGUAGE=C
31 export LC_ALL LANGUAGE
33 # Get physical working directory, absolute path name
34 CWD=$(CDPATH= cd -P -- $(dirname -- "$0") && printf "$PWD")
36 #### Functions
38 usage() {
39 printf "%s\n" \
40 "Builder of custom stages (cross compilers, GNU/Linux distributions)" \
41 "" \
42 "Usage: $PROGRAM [OPTION]... [FILE]..." \
43 "" \
44 "Defaults for the options are specified in brackets." \
45 "" \
46 "Options:" \
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}]" \
54 "" \
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" \
63 "" \
64 "Supported architectures (targets):"
65 for arch in "${CWD}/targets"/*
67 printf "${arch##*/} "
68 done
69 echo
72 version()
74 printf "%s\n" \
75 "$PROGRAM 3.14" \
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."
82 warn()
84 echo "$@" 1>&2
87 checkstatus_or_exit()
89 status=$?
91 if test $status -ne 0
92 then
93 echo "Return status = $status" 1>&2
94 exit ${1-2}; # If not given, defaults to 2
97 unset status
100 # Function to test and extract tarball files
101 untar() {
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
117 warn_flags()
119 warn "WARNING: Environment variable '$1' is set." \
120 "This will be unset to avoid possible risks." \
124 #### Default values
126 BTCC="${BTCC:=cc}"
127 BTCXX="${BTCXX:=c++}"
128 BTCFLAGS="${BTCFLAGS:=-g0 -Os}"
129 BTCXXFLAGS="${BTCXXFLAGS:=$BTCFLAGS}"
130 BTLDFLAGS="${BTLDFLAGS:=-s}"
131 worktree="$CWD"
132 arch="$(uname -m)" || exit 2
133 jobs=1
134 opt_keep=opt_keep.off
135 output=${worktree}/OUTPUT.${PROGRAM}
136 TMPDIR="${TMPDIR:=${output}/sources}"
137 stage=0
139 # Compose vendor name adding "-" as suffix
140 test -n "$VENDOR" && VENDOR="${VENDOR}-"
142 #### Parse options
144 while getopts :ha:j:ko:s:V name
146 case $name in
148 usage
149 exit 0
152 arch="$OPTARG"
155 jobs="$OPTARG"
158 opt_keep=opt_keep
161 output="$OPTARG"
164 stage="$OPTARG"
167 version
168 exit 0
171 warn "Option '-${OPTARG}' requires an argument"
172 usage
173 exit 1
176 warn "Illegal option -- '-${OPTARG}'"
177 usage
178 exit 1
180 esac
181 done
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
194 libSuffix=""
196 # Load target architecture-file
198 if test -f "${worktree}/targets/$arch"
199 then
200 echo "${PROGRAM}: Loading target $arch ..."
201 . "${worktree}/targets/$arch"
202 else
203 warn \
204 "${PROGRAM}: Target name not recognized -- '${arch}'" \
205 "See '$0 -h' for more information"
206 exit 1
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
214 if test -n "$BTCC"
215 then
216 host="$(${BTCC} -dumpmachine)" || exit 2
217 else
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"
225 then
226 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
227 case "${host%-*-*}" in
228 *-*)
229 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
232 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
234 esac
237 # Compose variables for the physical output,
238 # print some useful information
240 crossdir=${output}/cross/${target}
241 rootdir=${output}/stage${stage}
243 printf "%s\n" \
244 "BTCC: $BTCC" \
245 "BTCXX: $BTCXX" \
246 "BTCFLAGS: $BTCFLAGS" \
247 "BTCXXFLAGS: $BTCXXFLAGS" \
248 "BTLDFLAGS: $BTLDFLAGS" \
249 "Host: $host" \
250 "Target: $target" \
251 "Output directory: $output" \
252 "Cross directory: $crossdir" \
253 "Root directory: $rootdir"
255 # Remove write permission for group and other
256 umask 022
258 # Create required directories
259 if test ! -d "$output"
260 then
261 mkdir -p -- "$output" || exit 2
263 if test ! -d "$TMPDIR"
264 then
265 mkdir -p -- "$TMPDIR" || exit 2
268 # Set default path and propagate variables
270 PATH="${crossdir}/bin:${PATH}"
271 export PATH VENDOR TMPDIR
273 # Main loop
274 for file in "${CWD}/stages/${stage}"/${@:-??-*}
276 file="${file##*/}"
278 if test ! -f "${CWD}/stages/${stage}/$file"
279 then
280 warn "${PROGRAM}: ${stage}/${file}: No such file or stage number"
281 exit 1
283 if test ! -x "${CWD}/stages/${stage}/$file"
284 then
285 warn "${PROGRAM}: ${stage}/${file}: File not executable, dodging"
286 continue;
289 #### Stage pre-settings
291 # Create sysroot directory, recreating the symlink for the stage 0
293 if test "$stage" = 0
294 then
295 mkdir -p -- "${crossdir}/$target" || exit 2
297 if test ! -e "${crossdir}/${target}/usr"
298 then
299 ln -sf . "${crossdir}/${target}/usr" || exit 2
302 # Ensure toolchain sanity for multilib purposes
303 case $arch in
304 i586 | *x32 | x86_64 )
305 if test ! -e "${crossdir}/lib" -a -n "$libSuffix"
306 then
307 ln -sf lib${libSuffix} "${crossdir}/lib" || exit 2
310 esac
313 # Create "tools" directory, recreating the symlink for the stage 1
314 if test "$stage" = 1
315 then
316 if test ! -d "${rootdir}/tools"
317 then
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
330 set -e
332 . "${CWD}/stages/${stage}/$file" || checkstatus_or_exit 2
334 # Deactivate shell option
335 set +e
337 # Delete declared directories on the cleanup() function
338 if test "$opt_keep" != opt_keep
339 then
340 if type cleanup 1> /dev/null 2> /dev/null
341 then
342 cleanup || checkstatus_or_exit 2
343 unset cleanup
347 # Back to the current working directory
348 cd -- "$CWD" || exit 2
349 done