dev-libs/atcore: new package
[gentoo.git] / scripts / bootstrap.sh
1 #!/bin/bash
2 # Copyright 1999-2014 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # people who were here:
6 # (drobbins, 06 Jun 2003)
7 # (solar, Jul 2004)
8 # (vapier, Aug 2004)
9 # (compnerd, Nov 2004)
10 # (wolf31o2, Jan 2005)
11 # (azarah, Mar 2005)
12 # (uberlord, May 2007)
13 # (kumba, May 2007)
14 # (williamh, Mar 2014)
15 # (kumba, Feb 2015)
16
17 # sanity check
18 [[ -e /etc/profile ]] && . /etc/profile
19
20 if [[ -e /lib/gentoo/functions.sh ]] ; then
21         source /lib/gentoo/functions.sh
22         elif [[ -e /etc/init.d/functions.sh ]] ; then
23         source /etc/init.d/functions.sh
24 else
25         eerror() { echo "!!! $*"; }
26         einfo() { echo "* $*"; }
27 fi
28
29 # Use our own custom script, else logger cause things to
30 # 'freeze' if we do not have a system logger running
31 esyslog() {
32         :
33 }
34
35 show_status() {
36         local num=$1
37         shift
38         echo "  [[ ($num/3) $* ]]"
39 }
40
41 # Track progress of the bootstrap process to allow for
42 # semi-transparent resuming
43 progressfile=/var/run/bootstrap-progress
44 [[ -e ${progressfile} ]] && source ${progressfile}
45 export BOOTSTRAP_STAGE=${BOOTSTRAP_STAGE:-1}
46
47 set_bootstrap_stage() {
48         [[ -z ${STRAP_RUN} ]] && return 0
49         export BOOTSTRAP_STAGE=$1
50         echo "BOOTSTRAP_STAGE=$1" > ${progressfile}
51 }
52
53 v_echo() {
54         einfo "Executing: $*"
55         env "$@"
56 }
57
58 cvsver="$Id$" # TODO: FIXME for Git era
59 cvsver=${cvsver##*,v }
60 cvsver=${cvsver%%Exp*}
61 cvsyear=${cvsver#* }
62 cvsyear=${cvsyear%%/*}
63
64 usage() {
65         echo -e "Usage: ${HILITE}${0##*/}${NORMAL} ${GOOD}[options]${NORMAL}"
66         echo -e "  ${GOOD}--debug (-d)${NORMAL}     Run with debug information turned on"
67         echo -e "  ${GOOD}--fetchonly (-f)${NORMAL} Just download all the source files"
68         echo -e "  ${GOOD}--info (-i)${NORMAL}      Show system related information"
69         echo -e "  ${GOOD}--pretend (-p)${NORMAL}   Display the packages that will be merged"
70         echo -e "  ${GOOD}--quiet (-q)${NORMAL}     Reduced or condensed output from portage's displays."
71         echo -e "  ${GOOD}--tree (-t)${NORMAL}      Display the dependency tree, forces -p"
72         echo -e "  ${GOOD}--resume (-r)${NORMAL}    Build/use binary packages"
73 }
74
75 STRAP_EMERGE_OPTS="--oneshot"
76 STRAP_RUN=1
77 V_ECHO=env
78 DEBUG=0
79
80 for opt in "$@" ; do
81         case ${opt} in
82                 --fetchonly|-f)
83                         echo "Running in fetch-only mode ..."
84                         STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -f"
85                         unset STRAP_RUN;;
86                 --help|-h)
87                         usage
88                         exit 0;;
89                 --debug|-d)   STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --debug"; DEBUG=1;;
90                 --info|-i)    STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --info" ; unset STRAP_RUN ;;
91                 --pretend|-p) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -p"     ; unset STRAP_RUN ;;
92                 --quiet|-q)   STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -q"     ; unset STRAP_RUN ;;
93                 --tree|-t)    STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -p -t"  ; unset STRAP_RUN ;;
94                 --resume|-r)  STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --usepkg --buildpkg";;
95                 --verbose|-v) STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} -v"; V_ECHO=v_echo;;
96                 --version|-V)
97                         einfo "Gentoo Linux bootstrap ${cvsver}"
98                         exit 0
99                         ;;
100                 *)
101                         eerror "Unknown option '${opt}'"
102                         usage
103                         exit 1;;
104         esac
105 done
106
107 RESUME=0
108 if [[ -n ${STRAP_RUN} ]]  ; then
109         if [ ${BOOTSTRAP_STAGE} -ge 3 ] ; then
110                 echo
111                 einfo "System has been bootstrapped already!"
112                 einfo "If you re-bootstrap the system, you must complete the entire bootstrap process"
113                 einfo "otherwise you will have a broken system."
114                 einfo "Press enter to continue or CTRL+C to abort ..."
115                 read
116                 set_bootstrap_stage 1
117         elif [ ${BOOTSTRAP_STAGE} -gt 1 ] ; then
118                 einfo "Resuming bootstrap at internal stage #${BOOTSTRAP_STAGE} ..."
119                 RESUME=1
120         fi
121 else
122         export BOOTSTRAP_STAGE=0
123 fi
124
125 for p in /etc/portage /etc ; do
126         p+="/make.profile"
127         [[ -e ${p} ]] || continue
128         if type -P realpath >/dev/null ; then
129                 MYPROFILEDIR=$(realpath ${p})
130         else
131                 MYPROFILEDIR=$(readlink -f ${p})
132         fi
133 done
134 if [[ ! -d ${MYPROFILEDIR} ]] ; then
135         eerror "Error:  '${MYPROFILEDIR}' does not exist.  Exiting."
136         exit 1
137 fi
138
139 echo -e "\n${GOOD}Gentoo Linux; ${BRACKET}http://www.gentoo.org/${NORMAL}"
140 echo -e "Copyright 1999-${cvsyear} Gentoo Foundation; Distributed under the GPLv2"
141 if [[ " ${STRAP_EMERGE_OPTS} " == *" -f "* ]] ; then
142         echo "Fetching all bootstrap-related archives ..."
143 elif [[ -n ${STRAP_RUN} ]] ; then
144         if [ ${BOOTSTRAP_STAGE} -gt 2 ] ; then
145                 echo "Resuming Bootstrap of base system ..."
146         else
147                 echo "Starting Bootstrap of base system ..."
148         fi
149 fi
150 echo -------------------------------------------------------------------------------
151 show_status 0 Locating packages
152
153 # This should not be set to get glibc to build properly. See bug #7652.
154 unset LD_LIBRARY_PATH
155
156 # We do not want stray $TMP, $TMPDIR or $TEMP settings
157 unset TMP TMPDIR TEMP
158
159 cleanup() {
160         if [[ -n ${STRAP_RUN} ]] ; then
161                 if [ ${BOOTSTRAP_STAGE} -le 2 ] ; then
162                         cp -f /var/cache/edb/mtimedb /var/run/bootstrap-mtimedb
163                 else
164                         rm -f /var/run/bootstrap-mtimedb
165                 fi
166         fi
167         exit $1
168 }
169
170 pycmd() {
171         [[ ${DEBUG} = "1" ]] && echo /usr/bin/python -c "$@" > /dev/stderr
172         /usr/bin/python -c "$@"
173 }
174
175 # TSTP messes ^Z of bootstrap up, so we don't trap it anymore.
176 trap "cleanup" TERM KILL INT QUIT ABRT
177
178 # Bug #50158 (don't use `which` in a bootstrap).
179 if ! type -path portageq &>/dev/null ; then
180         echo -------------------------------------------------------------------------------
181         eerror "Your portage version is too old.  Please use a newer stage1 image."
182         echo
183         cleanup 1
184 fi
185
186 # USE may be set from the environment so we back it up for later.
187 export ORIGUSE=$(portageq envvar USE)
188
189 # Check for 'build' or 'bootstrap' in USE ...
190 INVALID_USE=$(gawk -v ORIGUSE="${ORIGUSE}" '
191         BEGIN {
192                 if (ORIGUSE ~ /[[:space:]](build|bootstrap)[[:space:]]/)
193                         print "yes"
194         }')
195
196 # Do not do the check for stage build scripts ...
197 if [[ ${INVALID_USE} = "yes" ]] ; then
198         echo
199         eerror "You have 'build' or 'bootstrap' in your USE flags. Please"
200         eerror "remove it before trying to continue, since these USE flags"
201         eerror "are used for internal purposes and shouldn't be specified"
202         eerror "by you."
203         echo
204         cleanup 1
205 fi
206
207 # since our logic here ignores stuff found in package.use, let's warn the
208 # user so they can avert disaster early
209 if [[ -n $(sed -n '/^[  ]*#/d;/^[       ]*$/d;p' /etc/portage/package.use 2>/dev/null) ]] ; then
210         echo
211         ewarn "You appear to have custom USE flags set in /etc/portage/package.use."
212         ewarn "Be aware that these settings may be ignored while running this script"
213         ewarn "(due to limitations in the bootstrap process).  If you have some USE"
214         ewarn "flags you wish to apply to say gcc or glibc, you should hit CTRL+C"
215         ewarn "now, export them in your environment (see below), and then restart."
216         ewarn " # export USE='some flags i want'"
217 fi
218
219 # gettext should only be needed when used with nls
220 for opt in ${ORIGUSE} ; do
221         case "${opt}" in
222                 bindist)
223                         ALLOWED_USE="${ALLOWED_USE} bindist"
224                         ;;
225                 nls)
226                         USE_NLS=1
227                         ALLOWED_USE="${ALLOWED_USE} nls"
228                         ;;
229                 nptl)
230                         export MYARCH=$(portageq envvar ARCH)
231                         if [[ -z $(portageq best_visible / '>=sys-kernel/linux-headers-2.6.0') ]] ; then
232                                 eerror "You need to have >=sys-kernel/linux-headers-2.6.0 unmasked!"
233                                 eerror "Please edit the latest >=sys-kernel/linux-headers-2.6.0 package,"
234                                 eerror "and add your ARCH to KEYWORDS or change your make.profile link"
235                                 eerror "to a profile which does not have 2.6 headers masked."
236                                 echo
237                                 cleanup 1
238                         fi
239                         USE_NPTL=1
240                         ;;
241                 nptlonly)
242                         USE_NPTLONLY=1
243                         ;;
244                 multilib)
245                         ALLOWED_USE="${ALLOWED_USE} multilib"
246                         ;;
247                 userlocales)
248                         ALLOWED_USE="${ALLOWED_USE} userlocales"
249                         ;;
250         esac
251 done
252
253 # With cascading profiles, the packages profile at the leaf is not a
254 # complete system, just the restrictions to it for the specific profile.
255 # The complete profile consists of an aggregate of the leaf and all its
256 # parents.  So we now call portage to read the aggregate profile and store
257 # that into a variable.
258
259 eval $(pycmd '
260 import portage
261 import sys
262 for atom in portage.settings.packages:
263         if not isinstance(atom, portage.dep.Atom):
264                 atom = portage.dep.Atom(atom.lstrip("*"))
265         varname = "my" + portage.catsplit(atom.cp)[1].upper().replace("-", "_")
266         sys.stdout.write("%s=\"%s\"; " % (varname, atom))
267 ')
268
269 # This stuff should never fail but will if not enough is installed.
270 [[ -z ${myBASELAYOUT} ]] && myBASELAYOUT=">=$(portageq best_version / sys-apps/baselayout)"
271 [[ -z ${myPORTAGE}    ]] && myPORTAGE="portage"
272 [[ -z ${myBINUTILS}   ]] && myBINUTILS="binutils"
273 [[ -z ${myGCC}        ]] && myGCC="gcc"
274 [[ -z ${myGETTEXT}    ]] && myGETTEXT="gettext"
275 [[ -z ${myLIBC}       ]] && myLIBC="$(portageq expand_virtual / virtual/libc)"
276 [[ -z ${myTEXINFO}    ]] && myTEXINFO="sys-apps/texinfo"
277 [[ -z ${myZLIB}       ]] && myZLIB="zlib"
278 [[ -z ${myNCURSES}    ]] && myNCURSES="ncurses"
279
280 # Do we really want gettext/nls?
281 [[ ${USE_NLS} != 1 ]] && myGETTEXT=
282
283 # Do we really have no 2.4.x nptl kernels in portage?
284 if [[ ${USE_NPTL} = "1" ]] ; then
285         myOS_HEADERS="$(portageq best_visible / '>=sys-kernel/linux-headers-2.6.0')"
286         [[ -n ${myOS_HEADERS} ]] && myOS_HEADERS=">=${myOS_HEADERS}"
287         ALLOWED_USE="${ALLOWED_USE} nptl"
288         # Should we build with nptl only?
289         [[ ${USE_NPTLONLY} = "1" ]] && ALLOWED_USE="${ALLOWED_USE} nptlonly"
290 fi
291 [[ -z ${myOS_HEADERS} ]] && myOS_HEADERS="virtual/os-headers"
292
293 einfo "Using baselayout : ${myBASELAYOUT}"
294 einfo "Using portage    : ${myPORTAGE}"
295 einfo "Using os-headers : ${myOS_HEADERS}"
296 einfo "Using binutils   : ${myBINUTILS}"
297 einfo "Using gcc        : ${myGCC}"
298 [[ ${USE_NLS} = "1" ]] && einfo "Using gettext    : ${myGETTEXT}"
299 einfo "Using libc       : ${myLIBC}"
300 einfo "Using texinfo    : ${myTEXINFO}"
301 einfo "Using zlib       : ${myZLIB}"
302 einfo "Using ncurses    : ${myNCURSES}"
303 echo -------------------------------------------------------------------------------
304 show_status 1 Configuring environment
305 echo -------------------------------------------------------------------------------
306
307 [[ -x /usr/sbin/gcc-config ]] && GCC_CONFIG="/usr/sbin/gcc-config"
308 [[ -x /usr/bin/gcc-config  ]] && GCC_CONFIG="/usr/bin/gcc-config"
309
310 # Allow portage to overwrite stuff
311 export CONFIG_PROTECT="-*"
312
313 # disable collision-protection
314 export FEATURES="${FEATURES} -collision-protect"
315
316 # query BOOTSTRAP_USE from the profile
317 BOOTSTRAP_USE=$(portageq envvar BOOTSTRAP_USE)
318
319 if [ ${BOOTSTRAP_STAGE} -le 1 ] ; then
320         show_status 2 Updating portage
321         ${V_ECHO} USE="-* build bootstrap ${ALLOWED_USE} ${BOOTSTRAP_USE}" emerge ${STRAP_EMERGE_OPTS} ${myPORTAGE} || cleanup 1
322         echo -------------------------------------------------------------------------------
323         set_bootstrap_stage 2
324 fi
325 export USE="-* bootstrap ${ALLOWED_USE} ${BOOTSTRAP_USE}"
326
327 # We can't unmerge headers which may or may not exist yet. If your
328 # trying to use nptl, it may be needed to flush out any old headers
329 # before fully bootstrapping.
330 if [ ${BOOTSTRAP_STAGE} -le 2 ] ; then
331         show_status 3 Emerging packages
332         if [[ ${RESUME} -eq 1 ]] ; then
333                 STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} --resume"
334                 cp /var/run/bootstrap-mtimedb /var/cache/edb
335         else
336                 STRAP_EMERGE_OPTS="${STRAP_EMERGE_OPTS} \
337                         ${myOS_HEADERS} ${myTEXINFO} ${myGETTEXT} ${myBINUTILS} \
338                         ${myGCC} ${myLIBC} ${myBASELAYOUT} ${myZLIB}"
339         fi
340         ${V_ECHO} emerge ${STRAP_EMERGE_OPTS} || cleanup 1
341         echo -------------------------------------------------------------------------------
342         set_bootstrap_stage 3
343 fi
344
345 # Basic support for gcc multi version/arch scheme ...
346 if [[ -n ${STRAP_RUN} ]] ; then
347         if [[ -x ${GCC_CONFIG} ]] && ${GCC_CONFIG} --get-current-profile &>/dev/null
348         then
349                 # Make sure we get the old gcc unmerged ...
350                 emerge --prune sys-devel/gcc || cleanup 1
351                 # Make sure the profile and /lib/cpp and /usr/bin/cc are valid ...
352                 ${GCC_CONFIG} "$(${GCC_CONFIG} --get-current-profile)" &>/dev/null
353         fi
354 fi
355
356 if [[ -n ${STRAP_RUN} ]] ; then
357         echo -------------------------------------------------------------------------------
358         einfo "Please note that you should now add the '-e' option for emerge system:"
359         echo
360         einfo "  # emerge -e system"
361         echo
362 fi
363
364 cleanup 0