qi: clarify the order in which qi looks for a recipe
[dragora.git] / bootstrap
blob9def474c044d4affb527c2b9514c9d35fd823dc8
1 #! /bin/sh -
3 # Builder of custom stages (cross compilers, GNU/Linux distributions)
5 # Copyright (c) 2014-2018 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.
19 # EXIT STATUS
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
25 PROGRAM="${0##*/}"
27 # Override locale settings
28 LC_ALL=C
29 LANGUAGE=C
30 export LC_ALL LANGUAGE
32 # Get physical working directory, absolute path name
33 CWD=$(CDPATH= cd -P -- $(dirname -- "$0") && printf "$PWD")
35 #### Functions
37 usage() {
38 printf "%s\n" \
39 "Builder of custom stages (cross compilers, GNU/Linux distributions)" \
40 "" \
41 "Usage: $PROGRAM [OPTION]... [FILE]..." \
42 "" \
43 "Defaults for the options are specified in brackets." \
44 "" \
45 "Options:" \
46 " -h display this help and exit" \
47 " -V print version information and exit" \
48 " -a target architecture [${arch}]" \
49 " -j parallel jobs for the compiler [${jobs}]" \
50 " -k keep (don't delete) source directory" \
51 " -o output directory [${output}]" \
52 " -s stage number to build [${stage}]" \
53 "" \
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" \
62 "" \
63 "Supported architectures (targets):"
64 for arch in "${CWD}/targets"/*
66 printf "${arch##*/} "
67 done
68 echo
71 version()
73 printf "%s\n" \
74 "$PROGRAM 3.19" \
75 "Copyright (C) 2014-2018 Matias Andres Fonzo." \
76 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" \
77 "This is free software: you are free to change and redistribute it." \
78 "There is NO WARRANTY, to the extent permitted by law."
81 warn()
83 echo "$@" 1>&2
86 chkstatus_or_exit()
88 status=$?
90 if test $status -ne 0
91 then
92 echo "Return status = $status" 1>&2
93 exit ${1-2}; # If not given, defaults to 2
96 unset status
99 # Function to test and extract compressed files
100 unpack()
102 for file in "$@"
104 case $file in
105 *.tar)
106 tar tf "$file" > /dev/null && \
107 tar xpf "$file"
108 chkstatus_or_exit 3
110 *.tar.gz | *.tgz | *.tar.Z )
111 gzip -cd "$file" | tar tf - > /dev/null && \
112 gzip -cd "$file" | tar xpf -
113 chkstatus_or_exit 3
115 *.tar.bz2 | *.tbz2 | *.tbz )
116 bzip2 -cd "$file" | tar tf - > /dev/null && \
117 bzip2 -cd "$file" | tar xpf -
118 chkstatus_or_exit 3
120 *.tar.lz | *.tlz )
121 lzip -cd "$file" | tar tf - > /dev/null && \
122 lzip -cd "$file" | tar xpf -
123 chkstatus_or_exit 3
125 *.tar.xz | *.txz )
126 xz -cd "$file" | tar tf - > /dev/null && \
127 xz -cd "$file" | tar xpf -
128 chkstatus_or_exit 3
130 *.zip | *.ZIP )
131 unzip -t "$file" > /dev/null && \
132 unzip "$file" > /dev/null
133 chkstatus_or_exit 3
135 *.gz)
136 gzip -t "$file" && \
137 gzip -cd "$file" > "$(basename -- $file .gz)"
138 chkstatus_or_exit 3
140 *.Z)
141 gzip -t "$file" && \
142 gzip -cd "$file" > "$(basename -- $file .Z)"
143 chkstatus_or_exit 3
145 *.bz2)
146 bzip2 -t "$file" && \
147 bzip2 -cd "$file" > "$(basename -- $file .bz2)"
148 chkstatus_or_exit 3
150 *.lz)
151 lzip -t "$file" && \
152 lzip -cd "$file" > "$(basename -- $file .lz)"
153 chkstatus_or_exit 3
155 *.xz)
156 xz -t "$file" && \
157 xz -cd "$file" > "$(basename -- $file .xz)"
158 chkstatus_or_exit 3
161 warn "${PROGRAM}: cannot unpack ${file}: Unsupported extension"
162 exit 1
163 esac
164 done
165 unset file
168 # Warning for good practices.
170 # Recommended practices is to set variables
171 # in front of `configure', or make(1); see:
173 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Defining-Variables.html
174 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Setting-Output-Variables.html
175 # http://www.gnu.org/software/make/manual/make.html#Environment
177 warn_flags()
179 warn "WARNING: Environment variable '$1' is set." \
180 "This will be unset to avoid possible risks." \
184 #### Default values
186 BTCC="${BTCC:=cc}"
187 BTCXX="${BTCXX:=c++}"
188 BTCFLAGS="${BTCFLAGS:=-g0 -Os}"
189 BTCXXFLAGS="${BTCXXFLAGS:=$BTCFLAGS}"
190 BTLDFLAGS="${BTLDFLAGS:=-s}"
191 opt_keep=opt_keep.off
192 stage=0
193 libSuffix=""
194 arch="$(uname -m)" || chkstatus_or_exit
195 jobs=1
196 worktree="$CWD"
197 output=${worktree}/OUTPUT.${PROGRAM}
198 TMPDIR="${TMPDIR:=${output}/sources}"
200 # Compose vendor name adding "-" as suffix
201 test -n "$VENDOR" && VENDOR="${VENDOR}-"
203 #### Parse options
205 while getopts :ha:j:ko:s:V name
207 case $name in
209 usage
210 exit 0
213 arch="$OPTARG"
216 jobs="$OPTARG"
219 opt_keep=opt_keep
222 output="$OPTARG"
225 stage="$OPTARG"
228 version
229 exit 0
232 warn "Option '-${OPTARG}' requires an argument"
233 usage
234 exit 1
237 warn "Illegal option -- '-${OPTARG}'"
238 usage
239 exit 1
241 esac
242 done
243 shift $(( OPTIND - 1 ))
245 # Check for environment variables, print warning, unset in any case
247 test "${CC:+CC_defined}" = CC_defined && warn_flags CC
248 test "${CXX:+CXX_defined}" = CXX_defined && warn_flags CXX
249 test "${CFLAGS:+CFLAGS_defined}" = CFLAGS_defined && warn_flags CFLAGS
250 test "${CXXFLAGS:+CXXFLAGS_defined}" = CXXFLAGS_defined && warn_flags CXXFLAGS
251 test "${LDFLAGS:+LDFLAGS_defined}" = LDFLAGS_defined && warn_flags LDFLAGS
252 unset CC CXX CFLAGS CXXFLAGS LDFLAGS warn_flags
254 # Load target architecture-file
256 if test -f "${worktree}/targets/$arch"
257 then
258 echo "${PROGRAM}: Loading target $arch ..."
259 . "${worktree}/targets/$arch"
260 else
261 warn \
262 "${PROGRAM}: Target name not recognized -- '${arch}'" \
263 "See '$0 -h' for more information"
264 exit 1
267 # Determine the host to use.
269 # Set up the host if a C compiler command was exported,
270 # otherwise, a local `cc' will be used instead
272 if test -n "$BTCC"
273 then
274 host="$(${BTCC} -dumpmachine)" || chkstatus_or_exit
275 else
276 host="$(cc -dumpmachine)" || chkstatus_or_exit
279 # If the host and the target are the same triplet, it won't work.
280 # We are changing the host if it is really needed
282 if test "$host" = "$target"
283 then
284 # Rename VENDOR to 'cross'. If empty, 'cross-linux' is added
285 case "${host%-*-*}" in
286 *-*)
287 host="$(echo "$host" | sed -e 's/-[^-]*/-cross/')"
290 host="$(echo "$host" | sed -e 's/-[^-]*/-cross-linux/')"
292 esac
295 # Compose variables for the physical output,
296 # printing some useful information
298 crossdir=${output}/cross/${target}
299 rootdir=${output}/stage${stage}
301 printf "%s\n" \
302 "BTCC: $BTCC" \
303 "BTCXX: $BTCXX" \
304 "BTCFLAGS: $BTCFLAGS" \
305 "BTCXXFLAGS: $BTCXXFLAGS" \
306 "BTLDFLAGS: $BTLDFLAGS" \
307 "Host: $host" \
308 "Target: $target" \
309 "Output directory: $output" \
310 "Cross directory: $crossdir" \
311 "Root directory: $rootdir"
313 # Remove write permission for group and other
314 umask 022
316 # Create required directories
317 mkdir -p -- "$output" "$TMPDIR"
318 chkstatus_or_exit
320 # Set default PATH, propagate variables
321 PATH="${crossdir}/bin:${PATH}"
323 export PATH VENDOR TMPDIR
325 # Main loop
326 for file in "${CWD}/stages/${stage}"/${@:-??-*}
328 file="${file##*/}"
330 if test ! -f "${CWD}/stages/${stage}/$file"
331 then
332 warn "${PROGRAM}: ${stage}/${file}: No such file or stage number"
333 exit 1
335 if test ! -x "${CWD}/stages/${stage}/$file"
336 then
337 warn "${PROGRAM}: ${stage}/${file}: File not executable, dodging"
338 continue;
341 #### Stage pre-settings
343 # Create sysroot directory, recreating the symlink for the stage 0
345 if test "$stage" = 0
346 then
347 mkdir -p -- "${crossdir}/$target" || chkstatus_or_exit
349 if test ! -e "${crossdir}/${target}/usr"
350 then
351 ln -sf . "${crossdir}/${target}/usr" || chkstatus_or_exit
354 # Ensure toolchain sanity for multilib purposes
355 case $arch in
356 i586 | *x32 | x86_64 )
357 if test ! -e "${crossdir}/lib" && test -n "$libSuffix"
358 then
359 ln -sf lib${libSuffix} "${crossdir}/lib" || chkstatus_or_exit
362 esac
365 # Create "tools" directory, recreating the symlink for the stage 1
366 if test "$stage" = 1
367 then
368 if test ! -d "${rootdir}/tools"
369 then
370 mkdir -p -- "${rootdir}/tools" || chkstatus_or_exit
373 # Make required symlink
374 ln -sf "${rootdir}/tools" / || chkstatus_or_exit
377 echo "${PROGRAM}: Processing $file from stage $stage ..."
379 # Set trap before to run the script in order to
380 # catch the return status, exit code 2 if fails
382 trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
384 # Exit immediately on any error
385 set -e
387 . "${CWD}/stages/${stage}/$file"
389 # Deactivate shell option(s)
390 set +e
392 # Reset given signals
393 trap - EXIT HUP INT QUIT ABRT TERM
395 # Delete declared directories on the cleanup() function
396 if test "$opt_keep" != opt_keep
397 then
398 if type cleanup 1> /dev/null 2> /dev/null
399 then
400 cleanup
401 chkstatus_or_exit 2
402 unset cleanup
406 # Back to the current working directory
407 cd -- "$CWD" || chkstatus_or_exit
408 done