summaryrefslogtreecommitdiffstats
path: root/temporary-bootstrap-builder-setup.txt
blob: 986a28cebf2ec523eca744f928bca9660291cc3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
ABOUT THIS GUIDE
================

This is a guide to building a custom BusyBox/Linux-libre system with the GNU
toolchain.  It mostly follows Cross Linux From Scratch Sysroot version
SVN-0.0.1-20090726-x86, except that it uses more recent versions of software
packages and uses a BusyBox-based userspace rather than a GNU-based one.  This
guide assumes the use of a GNU/Linux host system with all the necessary
development software (GCC, GNU Binutils, Subversion, etc.) already installed.

It is recommended that you read Cross Linux From Scratch Sysroot version
SVN-0.0.1-20090726-x86 to understand the full build process and the rationale
behind various elements of the setup.


LEGAL NOTICE
============

THIS GUIDE:

  Copyright (C) 2012 Patrick "P. J." McDermott

  This guide may be reproduced, distributed, modified, and otherwise dealt in
  under the terms of the Expat/MIT License:
  http://www.jclark.com/xml/copying.txt

CERTAIN COMMAND LINES AND FILE CONTENTS:

  Cross-Compiled Linux From Scratch - Sysroot
  Version SVN-0.0.1-20090726-x86

  Copyright © 2005-2009, Jim Gifford, & Ryan Oliver

  This material may be distributed only subject to the terms and conditions set
  forth in the Open Publication License v1.0 or later (the latest version is
  presently available at http://www.opencontent.org/openpub/).

  The latest version of the original book may be downloaded from:
  http://trac.cross-lfs.org/wiki/download#CLFSSysroot1.xUnderDevelopment
  It may also be read online at:
  http://cross-lfs.org/view/clfs-sysroot/x86/

TRADEMARKS:

  Linux is a Registered Trademark of Linus Torvalds.


PROCEDURE
=========

Make a directory in which all work will be done.  Under this directory, make
directories 'src' and sys':
  $ mkdir src sys

Download all of the necessary source archives and branches.
  $ cd src
  $ wget http://www.busybox.net/downloads/busybox-1.19.3.tar.bz2
  $ wget http://www.fsfla.org/svnwiki/selibre/linux-libre/download/releases/\
  > 3.2.4-libre/linux-3.2.4-libre.tar.bz2
  $ svn co svn://svn.eglibc.org/branches/eglibc-2_14 eglibc-2.14
  $ wget http://ftp.gnu.org/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.bz2
  $ wget http://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.bz2
  $ wget http://ftp.gnu.org/gnu/make/make-3.82.tar.bz2
  $ wget http://ftp.gnu.org/gnu/automake/automake-1.11.2.tar.bz2
  $ wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.68.tar.bz2
  $ wget http://ftp.gnu.org/gnu/m4/m4-1.4.16.tar.bz2
  $ wget ftp://ftp.gmplib.org/pub/gmp-5.0.2/gmp-5.0.2.tar.bz2
  $ wget http://www.mpfr.org/mpfr-current/mpfr-3.1.0.tar.bz2
  $ wget http://www.multiprecision.org/mpc/download/mpc-0.9.tar.gz
  $ for file in *.tar.bz2; do tar -xjf ${file}; done
  $ for file in *.tar.gz; do tar -xzf ${file}; done
  $ cd ..

Make and mount the filesystem.  mkfs and mount must be run as the superuser, but
dd shouldn't need to be.
  $ cd sys
  $ dd if=/dev/zero of=fs bs=1024 count=$((3*1024*1024))
  # mkfs -t ext4 fs
  $ mkdir fsmnt
  # mount -o loop fs fsmnt

Set up a new user.
  # groupadd bbl
  # useradd -s /bin/bash -g bbl -d /home/bbl bbl
  # mkdir -pv /home/bbl
  # chown -v bbl:bbl /home/bbl
  # passwd bbl
  # chown -Rv bbl fsmnt

Set up the toolchain work environment.  Be sure to change '<BASE_DIR>' to the
absolute path to the base work directory you made earlier, and adjust the
configuration name in BBL_TARGET if you're building for a different
architecture.  We're using GNU Bash as the new user's default shell for
convenience; this has no effect on the target system.
  $ su - bbl
  $ cat > ~/.bash_profile <<EOF
  > exec env -i HOME="${HOME}" TERM="${TERM}" /bin/bash
  > EOF
  $ cat > ~/.bashrc <<EOF
  > set +h
  > umask 022
  > BBL=<BASE_DIR>/sys/fsmnt
  > LC_ALL=C
  > PATH=<BASE_DIR>/sys/fsmnt/cross-tools/bin:/bin:/usr/bin
  > export BBL LC_ALL PATH
  > PS1='$ '
  > unset CFLAGS
  > unset CXXFLAGS
  > export BBL_HOST=$(echo ${MACHTYPE} | \
  > sed "s/$(echo ${MACHTYPE} | cut -d - -f 2)/cross/")
  > export BBL_TARGET=x86_64-pc-linux-gnu
  > EOF
  $ source ~/.bash_profile

Make system directories.  These commands should give you a system that is
compliant with Filesystem Hierarchy Standard version 2.3.  Note that the range
syntax in many of these command lines is a non-standard feature of GNU Bash;
this may not work on other shells.
  $ mkdir -pv ${BBL}/{bin,boot,dev,etc/opt,home,lib,media,mnt,opt,proc}
  $ install -dv -m 0750 ${BBL}/root
  $ mkdir -pv ${BBL}/{sbin,srv,sys}
  $ install -dv -m 1777 ${BBL}{,/var}/tmp
  $ mkdir -pv ${BBL}/usr/{,local/}{bin,include,lib,sbin,share/{man,misc},src}
  $ mkdir -pv ${BBL}/usr/local/{etc,games}
  $ mkdir -pv ${BBL}/usr/{,local/}share/{doc,info,locale,terminfo,zoneinfo}
  $ mkdir -pv ${BBL}/usr/{,local/}share/man/man{1,2,3,4,5,6,7,8}
  $ ln -sv share/man ${BBL}/usr/local/man
  $ mkdir -pv ${BBL}/var/{cache,lib/misc,local,lock,log,mail,opt,run,spool}

Write the passwd and group files.
  $ cat > ${BBL}/etc/passwd <<EOF
  > root::0:0:root:/root:/bin/bash
  > EOF
  $ cat > ${BBL}/etc/group <<EOF
  > root:x:0:
  > bin:x:1:
  > sys:x:2:
  > kmem:x:3:
  > tty:x:4:
  > tape:x:5:
  > daemon:x:6:
  > floppy:x:7:
  > disk:x:8:
  > lp:x:9:
  > dialout:x:10:
  > audio:x:11:
  > video:x:12:
  > utmp:x:13:
  > usb:x:14:
  > cdrom:x:15:
  > EOF

If CONFIG_FEATURE_UTMP and/or CONFIG_FEATURE_WTMP are enabled in BusyBox,
certain BusyBox applets (getty, login, init, etc.) will log user logins to
/var/run/utmp and/or /var/run/wtmp.  However, BusyBox's supporting library won't
create /var/run/wtmp if it doesn't exist.  So create these files now and set
proper modes for them.
  $ touch ${BBL}/var/run/{u,w}tmp
  $ chmod -v 664 ${BBL}/var/run/{u,w}tmp
/var/run/utmp is used to track logged-in users.  /var/run/wtmp is used to record
when users log into and out of the system.  NB: This is an odd location for
wtmp, but that's where an unpatched BusyBox will expect it.

Make and change to a directory for package source code.
  $ mkdir -v ${BBL}/src
  $ cd ${BBL}/src

At this point and after every milestone (maybe after each package installation),
I recommend you create a snapshot of the filesystem.  To do so, first exit the
shell environment of user 'bbl'.
  $ exit
As the superuser, unmount the filesystem.
  # umount fs
Make a copy of the filesystem.  Replace '<UNIQUE_NAME>' with something to
identify the milestone (for example, at this point I named my snapshot image
'fs.01-fs-hier' to note that it was the first snapshot and that I just finished
making the filesystem hierarchy).
  $ cp -p fs fs.<UNIQUE_NAME>
As the superuser, mount the filesystem again.
  # mount -o loop fs fsmnt
Log in as user 'bbl' and return to the src directory in the filesystem.
  $ su - bbl
  $ cd ${BBL}/src
Now, if something goes wrong, you can revert to a previous filesystem image by
going through this section and swapping the arguments to 'cp'.

Install Linux's headers for use by EGLIBC.
  $ cp -pR ${BBL}/../../src/linux-3.1.6 .
  $ cd linux-3.1.6
  $ make mrproper
  $ make headers_check
  $ make INSTALL_HDR_PATH=${BBL}/usr headers_install
  $ find ${BBL}/usr/include/ -name .install -o -name ..install.cmd | \
  > xargs rm -fv
  $ cd ..
  $ rm -Rf linux-3.1.6

Install GMP for use by the cross-compiling GCC build.
  $ cp -pR ${BBL}/../../src/gmp-5.0.2 .
  $ cd gmp-5.0.2
  $ CPPFLAGS=-fexceptions ./configure --prefix=${BBL}/cross-tools --enable-cxx
  $ make
  $ make install
  $ make check
  $ cd ..
  $ rm -Rf gmp-5.0.2

Install MPFR for use by the cross-compiling GCC build.
  $ cp -pR ${BBL}/../../src/mpfr-3.1.0 .
  $ cd mpfr-3.1.0
  $ LDFLAGS="-Wl,-rpath,${BBL}/cross-tools/lib" ./configure \
  > --prefix=${BBL}/cross-tools --enable-shared --with-gmp=${BBL}/cross-tools
  $ make
  $ make install
  $ cd ..
  $ rm -Rf mpfr-3.1.0

Install MPC for use by the cross-compiling GCC build.
  $ cp -pR ${BBL}/../../src/mpc-0.9 .
  $ cd mpc-0.9
  $ LDFLAGS="-Wl,-rpath,${BBL}/cross-tools/lib" ./configure \
  > --prefix=${BBL}/cross-tools --with-mpfr=${BBL}/cross-tools \
  > --with-gmp=${BBL}/cross-tools
  $ make
  $ make install
  $ cd ..
  $ rm -Rf mpc-0.9

Configure and build a cross-compiling GNU Binutils.
  $ cp -pR ${BBL}/../../src/binutils-2.22 .
  $ mkdir -v binutils-build
  $ cd binutils-build
  $ AR=ar AS=as ../binutils-2.22/configure --prefix=${BBL}/cross-tools \
  > --host=${BBL_HOST} --target=${BBL_TARGET} --with-sysroot=${BBL} \
  > --disable-nls --enable-shared --disable-multilib
  $ make configure-host
  $ make
  $ make install
  $ cp -v ../binutils-2.22/include/libiberty.h ${BBL}/usr/include
  $ cd ..
  $ rm -Rf binutils-2.22 binutils-build

Configure and build a cross-compiling GCC with a static libgcc and no threads.
  $ cp -pR ${BBL}/../../src/gcc-4.6.2 .
  $ mkdir -v gcc-build
  $ cd gcc-build
  $ AR=ar LDFLAGS="-Wl,-rpath,${BBL}/cross-tools/lib" ../gcc-4.6.2/configure \
  > --prefix=${BBL}/cross-tools --build=${BBL_HOST} --host=${BBL_HOST} \
  > --target=${BBL_TARGET} --disable-multilib --with-sysroot=${BBL} \
  > --disable-nls --without-headers --with-newlib --disable-decimal-float \
  > --disable-libgomp --disable-libmudflap --disable-libssp \
  > --with-mpfr=${BBL}/cross-tools --with-gmp=${BBL}/cross-tools \
  > --with-mpc=${BBL}/cross-tools --without-ppl --without-cloog \
  > --disable-shared --disable-threads --enable-languages=c
  $ make all-gcc all-target-libgcc
  $ make install-gcc install-target-libgcc
  $ cd ..
  $ rm -Rf gcc-4.6.2

Configure and build a cross-compiling EGLIBC.  Change '<LINUX_VER>' to the lower
of either version 3.1.6 or the version of Linux running on your host system.
  $ cp -pR ${BBL}/../../src/eglibc-2.14 .
  $ cd eglibc-2.14/libc
  $ cp -v Makeconfig{,.orig}
  $ sed -e 's/-lgcc_eh//g' Makeconfig.orig > Makeconfig
  $ cd ../..
  $ mkdir -v eglibc-build
  $ cd eglibc-build
  $ cat > config.cache <<EOF
  > libc_cv_forced_unwind=yes
  > libc_cv_c_cleanup=yes
  > libc_cv_gnu89_inline=yes
  > libc_cv_ssp=no
  > EOF
  $ cat > configparams <<EOF
  > install_root=${BBL}
  > EOF
  $ BUILD_CC=gcc CC=${BBL_TARGET}-gcc AR=${BBL_TARGET}-ar \
  > RANLIB=${BBL_TARGET}-ranlib \
  > CFLAGS="-march=$(cut -d - -f 1 <<< ${BBL_TARGET} | sed 's/x86_64/x86-64/') \
  > -mtune=generic -g -O2" ../eglibc-2.14/libc/configure --prefix=/usr \
  > --libexecdir=/usr/lib/eglibc --host=${BBL_TARGET} --build=${BBL_HOST} \
  > --disable-profile --enable-add-ons --with-tls --enable-kernel=<LINUX_VER> \
  > --with-__thread --with-binutils=${BBL}/cross-tools/bin \
  > --with-headers=${BBL}/usr/include --cache-file=config.cache
  $ make
  $ make install_root=${BBL} install
  $ rm -Rf *

Build EGLIBC's localedef and install locales.
  $ CPPFLAGS=-DNOT_IN_libc ../eglibc-2.14/localedef/configure
  > --prefix=${BBL}/usr --with-glibc=../eglibc-2.14/libc 
  $ make
  $ make SUPPORTED-LOCALES='\
  > de_DE.UTF-8/UTF-8 de_DE/ISO-8859-1 de_DE@euro/ISO-8859-15 \
  > en_HK.UTF-8/UTF-8 en_HK/ISO-8859-1 \
  > en_PH.UTF-8/UTF-8 en_PH/ISO-8859-1 \
  > en_US.UTF-8/UTF-8 en_US/ISO-8859-1 \
  > es_MX.UTF-8/UTF-8 es_MX/ISO-8859-1 \
  > fa_IR/UTF-8 \
  > fr_FR.UTF-8/UTF-8 fr_FR/ISO-8859-1 fr_FR@euro/ISO-8859-15 \
  > it_IT.UTF-8/UTF-8 it_IT/ISO-8859-1 \
  > ja_JP.EUC-JP/EUC-JP ja_JP.UTF-8/UTF-8' install-locales
  $ cd ..
  $ rm -Rf eglibc-2.14 eglibc-build

Write the Name Service Switch configuration file.
  $ cat > ${BBL}/etc/nsswitch.conf <<EOF
  > # /etc/nsswitch.conf
  > 
  > passwd:         files
  > group:          files
  > shadow:         files
  > 
  > hosts:          files dns
  > networks:       files
  > 
  > protocols:      files
  > services:       files
  > ethers:         files
  > rpc:            files
  > EOF

Set the system's timezone.
  $ TZDIR=${BBL}/usr/share/zoneinfo ${BBL}/usr/bin/tzselect
Answer the prompts and the tzselect script will output the name of a time zone.
Substitute '<TZ>' below with this name.
  $ ln -sfv ../usr/share/zoneinfo/<TZ> ${BBL}/etc/localtime

Configure the dynamic loader to search for libraries in /usr/local/lib and
/opt/lib in addition to its default locations /lib and /usr/lib.
  $ cat > ${BBL}/etc/ld.so.conf <<EOF
  > # /etc/ld.so.conf
  > 
  > /usr/local/lib
  > /opt/lib
  > EOF

Configure and build a final cross-compiling GCC.
  $ cp -pR ${BBL}/../../src/gcc-4.6.2 .
  $ mkdir -v gcc-build
  $ cd gcc-build 
  $ AR=ar LDFLAGS="-Wl,-rpath,${BBL}/cross-tools/lib" ../gcc-4.6.2/configure \
  > --prefix=${BBL}/cross-tools --build=${BBL_HOST} --host=${BBL_HOST} \
  > --target=${BBL_TARGET} --disable-multilib --with-sysroot=${BBL} \
  > --disable-nls --enable-shared --enable-languages=c,c++ \
  > --enable-__cxa_atexit --with-mpfr=${BBL}/cross-tools \
  > --with-gmp=${BBL}/cross-tools --with-mpc=${BBL}/cross-tools --without-ppl \
  > --without-cloog --enable-c99 --enable-long-long --enable-threads=posix
  $ make AS_FOR_TARGET="${BBL_TARGET}-as" LD_FOR_TARGET="${BBL_TARGET}-ld"
  $ make install
  $ cd ..
  $ rm -Rf gcc-4.6.2 gcc-build


Plan for building native system software, including the toolchain:
 1. Cross-compile and install BusyBox.
 2. Run ash in a chroot environment as user bbl.
 3. Build and install GMP, MPFR, MPC, PPL, CLooG, GNU Binutils, and GCC.
    Yes, CLooG configuration will work this time (due to the changed root), so
    GCC will have Graphite loop memory optimizations!
 4. Natively build and install Linux.
 5. ???
 6. Profit!


Set up environment variables to have all further packages built using the cross-
compiling toolchain.
  $ cat >> ~/.bashrc <<EOF
  > export CC=${BBL_TARGET}-gcc
  > export CXX=${BBL_TARGET}-g++
  > export AR=${BBL_TARGET}-ar
  > export AS=${BBL_TARGET}-as
  > export RANLIB=${BBL_TARGET}-ranlib
  > export LD=${BBL_TARGET}-ld
  > export STRIP=${BBL_TARGET}-strip
  > export READELF=${BBL_TARGET}-readelf
  > EOF
  $ source ~/.bashrc

Configure, cross-compile, and install BusyBox.
TODO: Provide a default configuration.
  $ cp -pR ${BBL}/../../src/busybox-1.19.3 .
  $ cd busybox-1.19.3
  $ make CROSS_COMPILE=${BBL_TARGET}- menuconfig
  $ make oldconfig
  $ make CROSS_COMPILE=${BBL_TARGET}-
  $ make CROSS_COMPILE=${BBL_TARGET}- CONFIG_PREFIX=${BBL} install
  $ chmod u+s ${BBL}/bin/busybox
  $ cd ..
  $ rm -Rf busybox-1.19.3