Typo
[linux_from_scratch_hints.git] / OLD / pure_lfs.txt
blob16557e112d37ff7f480be11c18a5411ef3e953ac
1 TITLE:          How to build a "pure" LFS
2 LFS VERSION:    CVS
3 AUTHORS:        Ryan Oliver <Ryan.Oliver@pha.com.au>,
4                 Greg Schafer <gschafer@zip.com.au>
6 SYNOPSIS:
7         The current LFS build method is a lot better than it used to be but is
8 still somewhat flawed in that not enough emphasis is placed on building a
9 correct toolchain. This was recently highlighted by criticism levelled at LFS
10 from senior Linux kernel hacker Alan Cox on the kernel mailing list. Here we
11 present a new strategy aimed at building a correct toolchain and thus a "pure"
12 LFS system. It is our belief that building a correct toolchain is the single
13 most important lesson that people reading the LFS book need to know. Everything
14 else is secondary.
16 HINT:
18 CHANGELOG
19 =========
21 The latest version of this document should always be available at:-
23     http://linuxfromscratch.org/~greg/
25 Failing that, a backup copy should also be available at:-
27     http://www.zipworld.com.au/~gschafer/
29   * 1.0   2003-03-21   Initial revision
31 DISCLAIMER
32 ==========
34 You use this hint at your own risk. Neither the authors, nor the Linux From
35 Scratch project accept any reponsibility for anything that happens when using
36 this document or associated files. This thing is very well tested but if in the
37 unlikely event that it happens to break your system, you get to keep both
38 pieces.
40 INTRODUCTION
41 ============
43 Broadly speaking, the toolchain comprises binutils, gcc and glibc. As of this
44 writing, the current LFS version is 4.0. The problems with the build method as
45 used in this version can be summarised as follows:-
47   * too reliant on the host (build) system
48     e.g. the autoconf tests for the static tools are performed whilst looking at
49     the host system which may be vastly different from the final system. The
50     most widely documented and glaring manifestation of this problem is gcc
51     which ends up miscompiling a portion of glibc due to this flaw (the
52     HAVE_GAS_HIDDEN hack works around this particular problem).
54   * too much code is brought in from the host system
55     e.g. all the static tools are statically linked against the host's glibc.
56     The host's glibc is an unknown quantity and may contain bugs or anything.
58   * too susceptible to incompatible glibc changes
59     e.g. statically linked binaries compiled on a glibc-2.2.x based system that
60     contain calls to getpwuid() crash when run on a glibc-2.3.x based system.
62   * glibc is compiled whilst no existing glibc is present
63     e.g. the glibc autoconf tests produce different results depending on whether
64     an existing glibc is found or not. This leads to inconsistencies and
65     encourages hacks and workarounds.
67 (NOTE - some of the abovementioned issues have been partially addressed in the
68 CVS version of the book but still do not go far enough and leave too much room
69 for error.)
71 To overcome all the issues, a new approach is required. The essence of what is
72 presented below can be summarised as follows:-
74   * build a fully "self contained" and "self hosting" toolchain that has no
75     dependencies on the host system.
77 The key differences between this approach and current LFS are:-
79   * a glibc is built in Chapter 5
81   * this glibc is built using the Chapter 5 gcc and binutils (NOT the versions
82     from the host)
84   * the rest of Chapter 5 is built against the new glibc using the new binutils
85     and gcc
87   * Chapter 5 packages are installed into a prefix of "/stage1" rather than
88     "$LFS/static" 
90 Some of the advantages to doing it this way are:-
92   * we can build everything shared. It isn't necessary that we build all our
93     binaries static in Chapter 5 (though you still can if you want to) as we
94     will still have access to the shared libs we create when we chroot.
96   * everything we use to build our shared binaries and libraries in Chapter 6
97     will ALREADY be built and linked against the glibc we are migrating to. This
98     avoids glibc migration issues now and in the future.
100   * the range of hosts suitable for building LFS from should increase due to
101     less reliance on the host i.e. theoretically, the only requirement is that
102     the host's compiler can bootstrap gcc.
104   * a large portion of current LFS support related questions will disappear e.g.
105     "failure to build Chapter 5 packages statically", "cannot build texinfo",
106     "binutils detection related problems".
108 Some key technical points of how the new approach works are:-
110   * similar in principle to cross compiling whereby tools installed into the
111     same prefix work in cooperation and thus utilise a little GNU "magic"
113   * careful manipulation of ld's linker scripts (in particular, the SEARCH_DIR
114     variables)
116   * editing of gcc's "specs" file to tell gcc which dynamic linker to produce
117     executables for
119 At this point, it is worth mentioning there are other, less sophisticated ways
120 of attaining a "pure" LFS. A "brute force" method is to build Chapter 6 twice.
121 i.e. remove the /static directory (both physically and from the `PATH') then use
122 Chapter 6 to rebuild itself. This is the basic idea behind the method employed
123 by the Linux Standards Base folk with their LSB Sample Implementation[1]. Taking
124 this approach is not practical for the LFS book. It is far too laborious and
125 nowhere near as educational with regard to some of the toolchain internals we
126 describe below.
128 PROCEDURE
129 =========
131 Toolchain cleanliness
132 ---------------------
133 It is important to realise there are varying degrees of "anal retentiveness" we
134 can employ when building a new toolchain. The more anal we are, the "cleaner"
135 the toolchain will be. For this reason it might seem we are building packages
136 more times than we really need to, but you have to keep in mind, the goal here
137 is to have a fully self hosted and very clean toolchain in which to perform the
138 final clean build of our target system in Chapter 6. Steps that are not strictly
139 necessary (i.e. less anal) will be pointed out as we move through the build. In
140 other words, a few shortcuts can be taken at the expense of anal retentiveness.
143 How we tested
144 -------------
145 We started from a glibc-2.2.5/gcc-2.95.3 based host to ensure any glitches were
146 caught early. The versions of the toolchain packages used in testing were
147 binutils-2.13.2, gcc-3.2.1 and glibc-2.3.1. The main test platform was
148 "i686-pc-linux-gnu".
151 Where to get the patches and scripts
152 ------------------------------------
153 The patches used in this document can be obtained from:-
155   http://linuxfromscratch.org/~greg/patches/pure_lfs/
157 Additionally, we have made some scripts available:-
159   http://linuxfromscratch.org/~greg/scripts/pure_lfs/
161 Please note, at this point in time the scripts are a bit further advanced than
162 this document and do not match it exactly.
165 Build considerations
166 --------------------
167 Throughout this document we make a few assumptions that you may need to allow
168 for depending on your circumstances. For example, in a few places you'll see
169 hardcoded references to "i686-pc-linux-gnu" within the actual build commands. If
170 your platform is anything other than i686-pc-linux-gnu (e.g. "i586-pc-linux-gnu"
171 or "powerpc-unknown-linux-gnu") then you'll need to adjust accordingly.
173 We also make use of the "*" wildcard operator so be mindful of this as it may
174 cause you grief if for instance you have multiple versions of source packages
175 lying around.
177 It is also worth mentioning that the core toolchain packages all build in their
178 own build directories (glibc is an exception in that it creates some files in
179 the "manual" subdir but we take action to allow for this). It is therefore
180 possible to reuse the same sources from previous build phases. If you do this,
181 please be careful to undo any patches that may not be appropriate for the next
182 phase.  We'll warn you where necessary.
184 Towards the end of this document you'll find a separate section that covers the
185 perennial debate over which binutils release to use (FSF or HJL). For now, just
186 be aware that a choice is available.
188 Lastly, NLS is enabled throughout the build. The only 2 places where we use
189 "--disable-nls" are the static "pass 1" builds of binutils and gcc. This is due
190 to the fact that in certain circumstances, "--enable-nls" combined with a static
191 link can cause build errors due to symbol conflicts. Please note that you are
192 free to use "--disable-nls" throughout the remainder of the entire build if NLS
193 is not desired and/or you wish to save space.
196 Running the test suites - A quick overview
197 ------------------------------------------
198 Most packages provide a test suite. Running the test suite is a good idea as it
199 provides a nice "sanity check" and proves that a newly built package is
200 functioning mostly as the developer intended. It does NOT guarantee that the
201 package is bug free as this nigh on impossible.
203 The test suites for gcc and binutils rely on the presence of 3 external packages
204 i.e. tcl, expect and dejagnu. If you want to run the test suites from the very
205 outset, you'll need to ensure those 3 packages are installed on the host system.
206 This document assumes those 3 packages are not installed on the host and
207 therefore does not run the test suites for the static "pass 1" builds of
208 binutils and gcc. The "pass 1" builds are soon overwritten by the "pass 2"
209 builds so skipping the "pass 1" test suites is not a major problem in our view.
211 Be warned, the test suites for gcc and glibc can take a very long time to
212 complete. If build time is a major problem for you then shortcuts are possible
213 by skipping the Chapter 5 test suites but we do not recommend this. If you
214 decide to take the shortcuts, then at the very least DO NOT skip the Chapter 6
215 test suites of glibc, binutils and gcc. But keep in mind, if you get to Chapter
216 6 then discover a problem that would have been picked up earlier by running the
217 test suites in Chapter 5, you'll surely rue your earlier decision to take the
218 shortcuts.
221 Chapter 5 - Creating the /stage1 directory
222 ------------------------------------------
224 We assume you have already reached Chapter 5 of the LFS book. i.e. partition
225 created, filesystem made, partition mounted, $LFS/static created, lfs user
226 added, environment set up.
228 As mentioned in the introduction, we will use "/stage1" instead of "/static" due
229 to the fact we will be building a dynamic (shared) Chapter 5. This naming is
230 only a cosmetic thing. Leaving it as /static is likely to cause confusion.
231 Please revisit the "Creating the $LFS/static directory" chapter of the book but
232 instead run the following:-
234     mkdir $LFS/stage1
236 Also, don't forget to perform the chown operation:-
238     chown lfs $LFS/stage1
240 The next step is to create a "/stage1" symlink on the host system. It will point
241 to the directory we just created on the LFS partition:-
243     ln -s $LFS/stage1 /
245 This ensures our toolchain will look in the same place (i.e. /stage1) in both
246 Chapters 5 and 6 (when we are inside the chroot). This is an important concept
247 to grasp. Don't worry if it's not clear right now. All will make sense once we
248 get into Chapter 6.
251 Chapter 5 - Setting up the environment
252 --------------------------------------
254 Some adjustments to the environment set up are needed. Firstly, we do not use
255 the:-    export CC="gcc -s"    thing. We are trying to do things correctly here
256 and strictly speaking, the "-s" is a linker flag. Therefore we will put "-s" in
257 the LDFLAGS where it belongs like so:-
259     export LDFLAGS="-s"
261 Secondly, we need to add "/stage1/bin" to the lfs user's `PATH' like so:-
263     export PATH=/stage1/bin:$PATH
265 As we progressively install Chapter 5 packages into "/stage1", the tools we just
266 built will get picked up in the `PATH' and thus get integrated into the build
267 process as we move along.
269 Thirdly, if using the bash shell, it's not a bad idea to switch off the "hash"
270 function of bash like so:-
272     set +h
274 Bash has a useful feature whereby it uses a hash table to remember the full
275 pathnames of executable files to avoid multiple `PATH' searches. However, we'd
276 like the new tools to become available as soon as they are installed. By
277 switching off the hash function, our "interactive" commands (make, patch, sed,
278 cp and so forth) will always use the newest available during the build process.
279 Shells spawned by configure scripts are not influenced by the state of the hash
280 facility in the current interactive shell so this step is considered optional.
281 The performance impact of disabling the hash is negligible. To confirm the
282 feature is switched off, simply type:-    hash    at the shell prompt and it
283 should report something like "su: hash: hashing disabled". If you are using a
284 shell other than bash, you might want to consult its documentation to see if a
285 hash like feature exists.
287 Please note, the intention here is to add the above commands to your
288 "/home/lfs/.bash_profile" so that they are always available when "su'ing" to the
289 lfs user. Therefore the "Setting up the environment" commands will be:-
291     cat > ~/.bash_profile << "EOF"
292     set +h
293     umask 022
294     LFS=/mnt/lfs
295     LC_ALL=POSIX
296     LDFLAGS="-s"
297     PATH=/stage1/bin:$PATH
298     export LFS LC_ALL LDFLAGS PATH
299     EOF
300     source ~/.bash_profile
302 One thing you'll notice throughout the build is some careful adjustment of the
303 CFLAGS environment variable. This is NOT TO FIDDLE WITH OPTIMISATION! The sole
304 purpose is to strip out the "-g" flag and thus save space during the builds. We
305 also throw in "-pipe" for marginally quicker builds. We do not export the CFLAGS
306 into the lfs user's environment globally because not all packages are
307 cooperative with their treatment of CFLAGS.
309 Another thing you'll notice is that in a few places we still specify
310 LDFLAGS="-s" on the make command line even though we have already exported it
311 into the environment. This is because those packages do not place the LDFLAGS
312 from the environment into the top level Makefile and thus do not pass it down to
313 the sub-make processes.
316 Chapter 5 - Installing binutils - Pass 1 (static)
317 -------------------------------------------------
319 This is basically the same as the current Chapter 5 binutils installation, but
320 with an extra twist.
322     mkdir ../binutils-build &&
323     cd ../binutils-build &&
324     CFLAGS="-O2 -pipe" ../binutils-2*/configure --prefix=/stage1 \
325        --disable-nls &&
326     make LDFLAGS="-all-static -s" &&
327     make install
329 NOTE - if you happen to be using a HJL binutils release of 2.13.90.0.18 or
330 greater and experience a build failure "C compiler cannot create executables"
331 you will need to insert a "make configure-host" in between the configure and
332 make lines. This is due to recent changes in the top level build system that do
333 not cope with having "-all-static" in the LDFLAGS.
335 IMPORTANT! - Do NOT remove the binutils-build dir as we are going to reuse it a
336 bit further down the track. This will be explained further on, but for now, just
337 run the following:-
339     cd ld &&
340     make clean &&
341     make LIB_PATH=/stage1/lib
343 DO NOT RUN "make install" AT THIS POINT! We are saving this for later. All will
344 become clear in due course.
346 One of the main differences is we use a prefix of /stage1 instead of the usual
347 $LFS/static. Note the lack of $LFS. It is worth mentioning here that having the
348 new binutils available from the outset is important because both glibc and gcc
349 perform various feature tests on the linker and assembler to determine which
350 software features to enable.
353 Chapter 5 - Installing gcc - Pass 1 (static)
354 --------------------------------------------
356 This is basically the same as the current Chapter 5 gcc installation, but with a
357 few variations. Firstly, we need to apply the "no_fixincludes" patch to prevent
358 the fixincludes script from running. We apply a patch instead of using the
359 "install-no-fixedincludes" target because it is cleaner and results in a
360 slightly quicker build and doesn't fail if you happen to use the
361 "--enable-version-specific-runtime-libs" configure switch. We also apply the
362 "mmap_test" patch which fixes a subtle gcc bug[2] that can (and will) undermine
363 the integrity of the gcc build in certain circumstances.
365     patch -Np1 -i ../gcc-3*.no_fixincludes.patch &&
366     patch -Np1 -i ../gcc-3*.mmap_test.patch &&
367     mkdir ../gcc-build &&
368     cd ../gcc-build &&
369     CFLAGS="-O2 -pipe" ../gcc-3*/configure --prefix=/stage1 \
370        --with-local-prefix=/stage1 --enable-languages=c \
371        --disable-nls --enable-shared &&
372     make BOOT_LDFLAGS="-static -s" BOOT_CFLAGS="-O2 -pipe" \
373        STAGE1_CFLAGS="-pipe" bootstrap &&
374     make install &&
375     ln -s gcc /stage1/bin/cc
377 Here we start to experience some of the magic of the GNU toolchain. It is
378 important to realise that gcc does not search the `PATH' during its configure
379 run to search for which binutils to use. It actually searches
380 $prefix/TARGET-TRIPLE/bin where TARGET-TRIPLE is for e.g. i686-pc-linux-gnu.
381 This means everything "just works" and we don't have to specify --with-ld= and
382 --with-as= to configure.
384 Note the use of "make bootstrap". In subsequent gcc passes we can skip the
385 bootstrap part and thus save lots of build time. But for this initial pass of
386 gcc it is absolutely CRITICAL to use the bootstrap target. This ensures a 3
387 stage bootstrap of the compiler and is vital to ensuring the integrity of the
388 overall build method.
390 The extra BOOT_CFLAGS and STAGE1_CFLAGS on the make line are just extra steps to
391 remove "-g" from the gcc build. A few occurrences still remain but the only
392 clean way to remove them without resorting to use of "make -e" is to patch the
393 source. You will find 2 patches at the URL mentioned above that do the job. The
394 patches are of course optional and are named "gcc-3.2.2.libgcc_no_debug.patch"
395 and "gcc-3.2.2.libstdc++_no_debug.patch".
397 Another difference between normal LFS and here is that we use "--enable-shared"
398 instead of "--disable-shared". This may seem counter-intuitive at first. But
399 using this switch allows the building of "libgcc_s.so.1" and "libgcc_eh.a".
400 Having "libgcc_eh.a" available ensures that the configure script for glibc (the
401 next package) produces correct results for proper building of glibc itself.
402 Please note, the gcc binaries will still be linked statically as this is
403 controlled by the "-static" in the BOOT_LDFLAGS on the make command line.
405 The other interesting configure switch is --with-local-prefix=/stage1. The
406 purpose of this switch is to remove /usr/local/include from gcc's include search
407 path. It is not absolutely essential but we want to try and minimise the
408 influence from the host system so this is a logical thing to do. The gcc install
409 page[3] says:-
411 "The same value can be used for both --with-local-prefix and --prefix provided
412 it is not /usr. This can be used to avoid the default search of
413 /usr/local/include."
415 which is exactly what we want. You can see the gcc search include order by
416 compiling a test program with "-v" e.g. gcc -v foo.c. The following snippets of
417 output demonstrate the issue. This is with the "--with-local-prefix" switch:-
419 #include "..." search starts here:
420 #include <...> search starts here:
421  /stage1/include
422  /stage1/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include
423  /usr/include
424 End of search list
426 And this is without:-
428 #include "..." search starts here:
429 #include <...> search starts here:
430  /usr/local/include
431  /stage1/include
432  /stage1/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include
433  /usr/include
434 End of search list
437 Chapter 5 - Installing kernel headers
438 -------------------------------------
440 In normal LFS we install the kernel headers in Chapter 6. But seeing as we are
441 now installing a glibc in Chapter 5, we need to install the kernel headers in
442 Chapter 5 also.
444 The procedure is essentially the same as current LFS with the exception that the
445 destination is /stage1 not /usr. In other words, follow the procedure from
446 current LFS (minus the /bin/pwd hacks) and substitute "cp blah /usr/include"
447 with "cp blah /stage1/include" and "touch /usr/blah" with "touch /stage1/blah":-
449 make mrproper &&
450 make include/linux/version.h &&
451 make symlinks &&
452 cp -HR include/asm /stage1/include &&
453 cp -R include/asm-generic /stage1/include &&
454 cp -R include/linux /stage1/include &&
455 touch /stage1/include/linux/autoconf.h
457 (NOTE - SHORTCUT ALERT! - seeing as we will be installing glibc again in Chapter
458 6, a potential shortcut here is to ALSO copy the kernel headers to the final
459 destination of $LFS/usr/include. By doing this, the kernel headers will already
460 be in place for the glibc compile in Chapter 6. But don't forget, we still need
461 the headers in /stage1/include as per the above commands.)
464 Chapter 5 - Installing glibc
465 ----------------------------
467 By this stage we have a new binutils and a new gcc (both statically linked
468 against the glibc on the host). We'll use both of these to build the Chapter 5
469 glibc. As per usual, don't forget to unpack the glibc-linuxthreads package. We
470 are also going to run the glibc test suite by issuing "make check".
472     mkdir -p /stage1/etc &&
473     touch /stage1/etc/ld.so.conf &&
474     mkdir ../glibc-build &&
475     cd ../glibc-build &&
476     CFLAGS="-O2 -pipe" ../glibc-2*/configure --prefix=/stage1 \
477        --enable-add-ons --disable-profile \
478        --with-binutils=/stage1/bin --with-headers=/stage1/include \
479        --without-gd &&
480     make &&
481     make check &&
482     make install &&
483     make localedata/install-locales
485 "make check" is considered very important for glibc. It's the most important lib
486 on the system and should not be installed unless it passes "make check". Any
487 failures need to be investigated and resolved before continuing. Some possible
488 causes of failure include:-
490   * faulty or misconfigured gcc
491   * faulty or misconfigured binutils
492   * overoptimisation
493   * environment not sane
494   * bugs in glibc itself
496 The math tests are known to be very CPU specific so any failures there are not
497 surprising. If you happen to get some math failures, and after having looked at
498 the specifics you are happy to continue on, you can issue "make -k check" to
499 continue through the remainder of the test suite. Though if you do this, be sure
500 to log the output so any further failures can be detected by perusing the log
501 file. If you get a failure in "libio/tst-atime", the partition you're working on
502 is probably mounted with the "noatime" option. Remount it with "atime" enabled
503 then continue on.
505 "make localedata/install-locales" is of course optional as per current LFS.
506 However, not having some certain locales installed will cause failures in the
507 gcc test suite (libstdc++ in particular). If you happen to skip "make
508 localedata/install-locales", you can install the minimum locales necessary by
509 issuing the following commands:-
511     mkdir -p /stage1/lib/locale &&
512     localedef -i de_DE -f ISO-8859-1 de_DE &&
513     localedef -i de_DE@euro -f ISO-8859-15 de_DE@euro &&
514     localedef -i en_HK -f ISO-8859-1 en_HK &&
515     localedef -i en_PH -f ISO-8859-1 en_PH &&
516     localedef -i en_US -f ISO-8859-1 en_US &&
517     localedef -i es_MX -f ISO-8859-1 es_MX &&
518     localedef -i fr_FR -f ISO-8859-1 fr_FR &&
519     localedef -i fr_FR@euro -f ISO-8859-15 fr_FR@euro &&
520     localedef -i it_IT -f ISO-8859-1 it_IT &&
521     localedef -i ja_JP -f EUC-JP ja_JP
523 The above commands can be issued even after you've deleted the glibc-build dir.
525 Please note, the use of --with-binutils= and --with-headers= configure switches
526 is not strictly required. But they do ensure nothing can go wrong with regard
527 to the kernel headers and binutils that get used during the glibc build. If you
528 perform a "diff" comparison between the two configured glibc-build dirs, both
529 with, and without those switches, you'll surely believe it is a lot cleaner to
530 use them.
532 The --without-gd switch ensures that we don't build the "memusagestat" program
533 which insists on linking against the host's libs (for libgd, libpng, libz and so
534 forth). During a normal LFS install, memusagestat does not get built.
536 As mentioned above, the CFLAGS saves space during the build. If you rely solely
537 on the LDFLAGS from the environment then the *.a archives will still be chock
538 full of debug symbols. It is important to understand the glibc build system is
539 very sensible about how it treats the CFLAGS supplied by the user. In other
540 words, the CFLAGS above do not change any vital aspect of the glibc build. They
541 simply remove "-g" and add "-pipe".
543 The other important thing to realise is that glibc is pretty well self contained
544 in terms of its build mechanics. No fancy linker switches are required. It
545 doesn't rely on compiler or linker defaults as all the required build switches
546 are supplied "by hand". e.g. you will see stuff like:-
548   -Wl,-dynamic-linker=/stage1/lib/ld-linux.so.2
550 during the glibc build. What is shown there is simply an example of how glibc's
551 build system overrides gcc's default behaviour of passing:-
553   -dynamic-linker=/lib/ld-linux.so.2
555 to the linker (ld) as it goes about its business of constructing the binaries
556 and libraries.
559 Linker scripts and library search order - A quick overview
560 ----------------------------------------------------------
561 At this juncture, it is worth discussing how ld (the linker) performs its magic.
562 On a normal LFS system you will find the linker scripts located in the
563 /usr/lib/ldscripts dir. They have names like elf_i386.x, elf_i386.xc and so on.
564 There are 13 of these scripts altogether but only the 8 that start with elf*
565 concern us. The others are involved when creating a.out format binaries which
566 basically means never. The a.out format was superceded years ago by the more
567 modern ELF format.
569 The linker scripts for our new toolchain in /stage1 are located in
570 /stage1/TARGET-TRIPLE/lib/ldscripts. If you view the scripts, you'll notice the
571 default linker search path (as indicated by the SEARCH_DIR variables) is:-
573   $prefix/TARGET-TRIPLE/lib, $prefix/lib, /usr/local/lib, /lib, /usr/lib 
575 where $prefix is the value of the --prefix configure option and TARGET-TRIPLE is
576 the cpu-arch-os triple for the host (e.g. i686-pc-linux-gnu). Anything linked by
577 ld using these linker scripts will link against our new glibc installed into
578 /stage1/lib because /stage1/lib comes before /usr/lib in the search path. So far
579 so good. This is exactly what we want. However, what we really need is to remove
580 /usr/local/lib, /lib, and /usr/lib from the search path altogther. In this way
581 we can be 100% certain that nothing links against the libraries on the host
582 system. There are a few ways we can achieve this:-
584   1. edit all the linker scripts by hand
585   2. perform some sort of sed substitution on each of the scripts
586   3. utilise the LIB_PATH variable in ld's Makefile to our advantage
588 No 3 is the easiest solution. A small mention of LIB_PATH was made back in the
589 "binutils - pass 1" section above. A bit further down, you'll see how its usage
590 helps us out.
593 Gcc's specs file - A quick overview
594 -----------------------------------
595 >From the gcc info page:-
597 "`gcc' is a driver program.  It performs its job by invoking a sequence of other
598 programs to do the work of compiling, assembling and linking.  GCC interprets
599 its command-line parameters and uses these to deduce which programs it should
600 invoke, and which command-line options it ought to place on their command lines.
601 This behavior is controlled by "spec strings".  In most cases there is one spec
602 string for each program that GCC can invoke, but a few programs have multiple
603 spec strings to control their behavior.  The spec strings built into GCC can be
604 overridden by using the `-specs=' command-line switch to specify a spec file.
605 "Spec files" are plaintext files that are used to construct spec strings."
607 To find out where the specs file is located simply type in "gcc -v" like so:-
609   gws@tigers-lfs:~$ gcc -v 2>&1 | head -n 1
610   Reading specs from /static/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/specs
612 If you examine a normal dynamic executable using the readelf utility, you can
613 determine which dynamic linker (i.e. program interpreter) the executable was
614 compiled for. Take a look at this example:-
616   gws@tigers-lfs:~/src$ readelf -a foo | grep interpreter
617         [Requesting program interpreter: /lib/ld-linux.so.2]
619 If we want to change the program interpreter that gets embedded into our
620 binaries we need to utilise the linker switch -dynamic-linker e.g.
621 -Wl,-dynamic-linker=/stage1/lib/ld-linux.so.2. But forcing this onto every
622 package we compile can be quite troublesome. It is much easier to simply force
623 the issue by adjusting the specs string in the specs file to point where we
624 want.
626 There are a few ways we can achieve this:-
628   1. edit, sed or patch the specs file
629   2. patch the gcc source before we compile it
631 Both methods are used below, depending on which stage of the build process we
632 are at.
635 Chapter 5 - "Locking in" our new glibc
636 --------------------------------------
638 Now that we have the lectures out of the way, it is back to business. By now we
639 have our Chapter 5 glibc installed and operational. This is the point in the
640 proceedings where we need to ensure that we do not link against the libraries on
641 the host system. i.e. we will "lock in" the new glibc.
643 The first thing to do is adjust the linker scripts. Remember back in the
644 "binutils - pass 1" section above we added a couple of extra commands at the
645 end? Well here is where we utilise those commands. Simply cd back into your
646 binutils-build dir and run the following:-
648     cd ld &&
649     make install-data-local
651 This installs the adjusted linker scripts. The linker scripts now contain no
652 mention of /lib, /usr/lib or /usr/local/lib. From this point onwards, everything
653 will link ONLY against the libs in /stage1/lib. It is now safe to remove the
654 binutils-build dir.
656 The next thing to do is amend our gcc specs file so that it points to the new
657 dynamic linker. A sed can be used to accomplish this:-
659     SPECFILE=/stage1/lib/gcc-lib/i686-pc-linux-gnu/*/specs &&
660     cp ${SPECFILE} ./XX &&
661     sed 's@/lib/ld-linux.so.2@/stage1/lib/ld-linux.so.2@g' ./XX > ${SPECFILE} &&
662     unset SPECFILE &&
663     rm -f ./XX
665 We recommend you cut'n'paste the above rather than type it all in. You can
666 always edit the specs file by hand if you really want to. Just replace
667 "/lib/ld-linux.so.2" with "/stage1/lib/ld-linux.so.2".
669 At this point, it is a good idea to check the linker scripts and the specs file
670 to ensure the intended changes were actually made.
673 Chapter 5 - Installing tcl, expect and dejagnu
674 ----------------------------------------------
676 We now need to install the 3 external packages so that the gcc and binutils test
677 suites can be run. If these 3 packages are already installed on the host system
678 you can delay this step until the end of Chapter 5 if you wish. The versions we
679 used in testing were tcl8.4.1, expect-5.38.0 and dejagnu-1.4.3.
681 tcl:
682     cd unix &&
683     ./configure --prefix=/stage1 &&
684     make &&
685     make install &&
686     ln -s tclsh8.4 /stage1/bin/tclsh
688 NOTE - you must leave the tcl source dir in place until the expect package is
689 installed as the expect package needs the tcl internal headers to build.
691 expect:
692     patch -Np1 -i ../expect-5*.patch &&
693     ./configure --prefix=/stage1 &&
694     make &&
695     make install
697 The patch fixes a bug in expect that can result in bogus failures during the gcc
698 test suite run[4].
700 dejagnu:
701     ./configure --prefix=/stage1 &&
702     make &&
703     make install
705 NOTE - you can run the test suites for these 3 packages if you wish but you'll
706 more than likely see failures. Luckily, there is enough functionality to run the
707 gcc and binutils test suites and that is only what we care about at this point.
709 ADDITIONAL NOTE - you must have /dev/pts mounted (or be using devfs) for the
710 dejagnu test suite to function properly.
713 Chapter 5 - Installing gcc - Pass 2 (shared)
714 --------------------------------------------
716 (NOTE - SHORTCUT ALERT! - this step could be considered optional as we already
717 have a new gcc to begin compiling Chapter 6 with. However, this gcc is
718 statically linked against the host's glibc. We recompile now so that we can add
719 c++ to the mix and so that we end up with a shared gcc linked against the new
720 glibc. c++ is not absolutely essential to bootstrap the Chapter 6 final build.
721 However, the advantages to adding c++ are 1) if running "make check" for
722 binutils (the next package) all tests will pass and 2) no need to disable
723 building of the ncurses c++ binding when we get to the Chapter 5 ncurses. If you
724 don't wish to build c++, just ensure that "--enable-languages" is equal to "c"
725 only and drop the c++ specific switches i.e. --enable-threads=posix
726 --enable-__cxa_atexit --enable-clocale=gnu.)
728 Before we start this recompile of gcc, we need to change the default location of
729 the traditional spec string for the dynamic linker. We therefore patch gcc (the
730 file is gcc/config/i386/linux.h). It is just a simple substitution of
731 "/lib/ld-linux.so.2" with "/stage1/lib/ld-linux.so.2". The reason why we patch
732 now rather than adjust the specs file after installation is that it ensures our
733 new dynamic linker gets used during the actual gcc build. i.e. all the final
734 (and temporary) binaries created during the build will link against the new
735 glibc. If you haven't already done so, don't forget to remove the previous
736 gcc-build dir left over from pass 1 above. If using the same source from pass 1
737 there is no need to reapply the no_fixincludes or mmap_test patches (or the
738 no_debug patches if you applied those as well). We are also going to run the gcc
739 test suite by issuing "make -k check".
741     patch -Np1 -i ../gcc-3*.no_fixincludes.patch &&
742     patch -Np1 -i ../gcc-3*.mmap_test.patch &&
743     patch -Np1 -i ../gcc-3*.specs.patch &&
744     mkdir ../gcc-build &&
745     cd ../gcc-build &&
746     CFLAGS="-O2 -pipe" CXXFLAGS="-O2 -pipe" ../gcc-3*/configure \
747        --prefix=/stage1 --with-local-prefix=/stage1 --enable-languages=c,c++ \
748        --enable-shared --enable-threads=posix --enable-__cxa_atexit \
749        --enable-clocale=gnu &&
750     make LDFLAGS="-s" &&
751     make -k check
752     make install
754 The main differences between here and pass 1 is the dropping of the "bootstrap"
755 target, the addition of the c++ specific bits and no mention of "-static" in any
756 of the FLAGS. Of course there is also the addition of "make -k check" to run the
757 test suite.
759 As mentioned earlier, the gcc "bootstrap" target is not needed at this stage. It
760 is worth looking at the need for bootstrapping gcc from a historical viewpoint.
761 It was first implemented to aid in building a GNU compiler on systems where
762 there was only a non-GNU or proprietary compiler installed. Seeing as we already
763 bootstrapped gcc in pass 1 and built it from the exact same source version as we
764 are using now, there is no need to bootstrap again from this point onwards. We
765 have thoroughly tested this aspect and can confirm that the installed files from
766 a "bootstrapped" gcc build are "byte-for-byte" identical with the installed
767 files from a "non-bootstrapped" build, AS LONG AS THE INITIAL PASS 1 WAS
768 BOOTSTRAPPED! All bets are off if the initial pass 1 gcc was not bootstrapped.
769 You can still "make bootstrap" pass 2 if you want to. Just substitute the "make"
770 command from above with the one below.
772     make BOOT_LDFLAGS="-s" BOOT_CFLAGS="-O2 -pipe" \
773        STAGE1_CFLAGS="-pipe" bootstrap
775 The reason for the "-k" (and the lack of "&&") is we need the test suite to run
776 through to completion and not stop at the first failure. The gcc test suite is
777 very comprehensive and is almost always guaranteed to have a few failures. The
778 gcc testing page[5] contains more information. The simplest way to get a summary
779 of the test suite run is to use the "test_summary" script from within the
780 gcc-build dir:-
782     ../gcc-3*/contrib/test_summary | less
784 Then you can compare your results to those that have been posted to the
785 gcc-testresults mailing list for similar configurations to your own. Here is an
786 example of how current gcc-3.2.x should look on i686-pc-linux-gnu:-
788     http://gcc.gnu.org/ml/gcc-testresults/2003-02/msg00204.html
790 Note that the results contain:-
792   * 1 XPASS (unexpected pass) for g++
793   * 26 XPASS's for libstdc++
794   * 1 FAIL for libstdc++
796 The unexpected pass for g++ is due to the use of "--enable-__cxa_atexit".
797 Apparently not all platforms supported by gcc have support for "__cxa_atexit" in
798 their C libraries hence this test is not always expected to pass. The 26
799 unexpected passes for libstdc++ are due to the use of "--enable-clocale=gnu"
800 which is the correct choice on glibc based systems of 2.2.5 and above. The
801 underlying locale support in the GNU C library is superior to that of the
802 otherwise selected "generic" model (which may be applicable if for instance you
803 were using newlibc, sun-libc or whatever-libc). The libstdc++ test suite is
804 apparently expecting the "generic" model hence those tests are not always
805 expected to pass. The failure for libstdc++ is
806 "26_numerics/c99_classification_macros_c.cc" and is a long standing known
807 failure (since at least January 2002) that the developers are apparently unable
808 to easily fix.
811 Chapter 5 - Installing binutils - Pass 2 (shared)
812 -------------------------------------------------
814 (NOTE - SHORTCUT ALERT! - this step could be considered optional as we already
815 have a new binutils to begin compiling Chapter 6 with. However, this binutils is
816 statically linked against the host's glibc and was compiled by the host's gcc. A
817 recompile now and we'll have our binutils compiled by the new gcc and we'll end
818 up with a completely shared toolchain linked against the new glibc.)
820 Before recompiling binutils, we apply a patch (backported from the binutils CVS
821 HEAD and currently appearing in the HJ Lu binutils releases) that allows us to
822 specify --with-lib-path to configure. Specifying "--with-lib-path=/stage1/lib"
823 allows us to keep /usr/local/lib, /lib and /usr/lib out of the linker scripts
824 search paths. The reason why we couldn't use the --with-lib-path patch earlier
825 in pass 1 is we needed the ability to link against the host's glibc for the
826 static compile of binutils and gcc. If you haven't already done so, don't forget
827 to remove the previous binutils-build dir left over from pass 1 above. We are
828 also going to run the binutils test suite by issuing "make check".
830     patch -Np1 -i ../binutils-2*.lib-path.patch &&
831     mkdir ../binutils-build &&
832     cd ../binutils-build &&
833     CFLAGS="-O2 -pipe" ../binutils-2*/configure --prefix=/stage1 \
834        --with-lib-path=/stage1/lib --enable-shared &&
835     make LDFLAGS="-s" &&
836     make check &&
837     make install
839 NOTE - if you happen to be using a HJL binutils release of 2.13.90.0.10 or
840 greater you will not need to apply the abovementioned patch.
842 IMPORTANT! - Similar to our "binutils - pass 1" above, do NOT remove the
843 binutils-build dir as we are going to reuse it in Chapter 6. For now, just run
844 the following:-
846     cd ld &&
847     make clean &&
848     make LIB_PATH=/usr/lib:/lib
849     
850 Again, DO NOT RUN "make install" just yet. We will wait for the appropriate time
851 in Chapter 6 when we need to adjust the linker scripts. Note that the binutils
852 test suite should always pass.
855 Chapter 5 - Toolchain Summary
856 -----------------------------
858 At this stage, we have compiled gcc twice, binutils twice and glibc once. A bit
859 excessive you might think? Absolutely not. The toolchain is now very clean and
860 that feeling of cleanliness is very powerful and worth every bit of effort in
861 our opinion.
864 Chapter 5 - Installing remaining packages - (shared)
865 ----------------------------------------------------
867 The remaining Chapter 5 packages are built pretty much according to current LFS
868 instructions with the obvious exceptions being:-
870   * everything gets built with "--prefix=/stage1"
872   * no mention of "LDFLAGS=-static" (we are building everything shared)
874   * no mention of "CPPFLAGS=-Dre_max_failures=re_max_failures2" (this only
875     applies to static builds)
877 Here is a suggested list of packages and a suggested build order. Compared to
878 current LFS there are 3 additional packages: ncurses, gettext and perl. The perl
879 we install is a special trimmed down "miniperl". We don't need a full perl with
880 all its associated bloat. Building a minimal perl as opposed to the full version
881 also has the advantage of saving space and build time. Having perl available
882 early in Chapter 6 is not absolutely essential. But it does ensure a more
883 complete run of glibc's "make check" and also removes the need to patch glibc as
884 per current LFS. It also takes away any doubts for various packages that may
885 make assumptions about the presence of perl.
887 Unlike current Chapter 5 LFS, order DOES matter (due to dependencies).
889 NOTE - If you happen to be using the HJL binutils, you'll also need to add m4,
890 bison and flex to the list below. This is due to packaging quirks in the HJL
891 release.
893 Feel free to add any other packages that you think may be useful once inside the
894 chroot. For example the "less" package is a good candidate.
896 gawk
897 fileutils
898 bzip2
899 gzip
900 diffutils
901 findutils
902 make
903 grep* (see below for suggested build commands)
905 gettext* (see below for suggested build commands)
906 textutils
907 sh-utils
908 ncurses* (see below for suggested build commands)
909 patch
911 texinfo
912 bash
913 util-linux
914 perl* (see below for suggested build commands)
917 grep:
918     CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
919        --disable-perl-regexp --with-included-regex &&
920     make &&
921     make install
923 The "--with-included-regex" is needed so that all tests pass. Without this
924 switch the regex code from glibc is used and as of glibc-2.3.1 that code is
925 known to be a bit buggy.
926 (NOTE - we are using the newer grep version of 2.5.1 which passes all tests.)
929 gettext:
930     CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 &&
931     make &&
932     make install &&
933     rm -f /stage1/lib/gettext/gnu.gettext.*
935 The "rm -f /stage1/lib/gettext/gnu.gettext.*" is optional. If gettext finds a
936 java compiler on your system it builds some binaries that link against the
937 host. Leaving the binaries there is harmless but we might as well remove them.
940 ncurses:
941     patch -Np1 -i ../ncurses-5*.etip.patch &&
942     ./configure --prefix=/stage1 --with-shared \
943        --without-debug &&
944     make &&
945     make install
947 NOTE - you may need to add "--without-cxx-binding" if you opted not to enable
948 c++ in the gcc pass 2 build.
950 The patch is needed to fix a subtle bug[6] in ncurses that results in an
951 incorrect header file.
954 perl:
955     patch -Np1 -i ../perl-5*.libc.patch &&
956     ./configure.gnu --prefix=/stage1 -Doptimize='-O2 -pipe' &&
957     make utilities &&
958     cp miniperl /stage1/bin/perl &&
959     cp pod/pod2man /stage1/bin &&
960     mkdir -p /stage1/lib/perl5/5.8.0 &&
961     cp -R lib/* /stage1/lib/perl5/5.8.0
963 NOTE - Be sure to issue "make utilities" instead of the usual "make" due to our
964 desire to not build a full perl. But it should still be possible to build a full
965 perl if you really want to. Just issue the usual "make" and "make install". The
966 patch sets up perl to correctly build against our glibc in /stage1. The
967 "-Doptimize='-O2 -pipe'" is there simply to use O2 instead of perl's default of
968 03. We install pod2man because it is used during the binutils build.
970 If you previously skipped the installation of the tcl, expect and dejagnu
971 packages because they were already installed on the host system, now is the time
972 to install them into prefix /stage1 so that they are available for running the
973 binutils and gcc test suites in Chapter 6.
976 Chapter 6 - Initial steps
977 -------------------------
979 The start of Chapter 6 is pretty much according to current LFS instructions with
980 the obvious exception being:-
982   * replace all occurrences of /static with /stage1
984 Enter the chroot, change ownership, create directories, mount the proc
985 filesystem, create the mtab file, create the bash and sh symlinks, create the
986 passwd and group files, create devices, install kernel headers.
988 NOTE - we propose renaming the section of the LFS book entitled "Create the bash
989 and sh symlinks" to "Create the essential symlinks". In this section we need to
990 create a bunch of symlinks like so:-
992     ln -s /stage1/bin/bash /bin/bash &&
993     ln -s bash /bin/sh &&
994     ln -s /stage1/bin/cat /bin/cat &&
995     ln -s /stage1/bin/perl /usr/bin/perl &&
996     ln -s /stage1/bin/pwd /bin/pwd &&
997     ln -s /stage1/bin/stty /bin/stty
999 /bin/bash and /bin/sh are self explanatory. /bin/cat is needed by the glibc test
1000 suite. /usr/bin/perl is needed to ensure glibc's mtrace script is installed
1001 correctly. /bin/pwd is needed by the kernel headers install and glibc's
1002 configure script (as of glibc-2.3.2). /bin/stty is needed by the dejagnu test
1003 suite runs of binutils and gcc.
1005 The symlinks will stay in place until each one is overwritten by the
1006 corresponding real file during Chapter 6 package installation (except of course
1007 for /bin/sh).
1009 IMPORTANT! - make sure everything above is done prior to proceeding, especially
1010 the kernel headers installed into /usr/include! Be sure to NOT do the /bin/pwd
1011 symlink creation and deletion as per current LFS kernel headers install as we
1012 already created the symlink above and need to keep it around for later.
1014 As per current LFS, the chroot command will set the `PATH' for the environment
1015 inside the chroot. It's important to realise that /stage1/bin must be at the end
1016 of the `PATH' so as to ensure the newest tools always get used during the
1017 remainder of the build.
1019 It's worth mentioning here that user name and group name resolution will start
1020 working immediately after the passwd and group files are created because we have
1021 a glibc already installed. You can therefore execute:-
1023     exec /stage1/bin/bash --login
1025 to get rid of the "I have no name!" message in the command prompt.
1027 If preferred, it is worth doing the 'export LDFLAGS="-s"' and 'set +h' things
1028 like we did at the start of Chapter 5.
1031 Chapter 6 - Installing glibc
1032 ----------------------------
1034 As mentioned earlier, the glibc build system is very well self contained and
1035 will install perfectly, even though our gcc specs file and ldscripts are still
1036 pointing at /stage1. We cannot adjust the specs and ldscripts before the glibc
1037 install because the glibc autoconf tests will give bogus results and thus defeat
1038 our goal of achieving a clean build. We are again going to run the glibc test
1039 suite by issuing "make check". As per usual, don't forget to unpack the
1040 glibc-linuxthreads package.
1042 NOTE - if reusing the same source from Chapter 5 then you'll need to issue:-
1044     rm -f manual/{texis,summary.texi,stamp-summary,chapters.texi,top-menu.texi}
1046 before configuring and whilst still inside the source dir to ensure the info 
1047 pages are rebuilt using the new makeinfo.
1049     touch /etc/ld.so.conf &&
1050     mkdir ../glibc-build &&
1051     cd ../glibc-build &&
1052     CFLAGS="-O2 -pipe" ../glibc-2*/configure --prefix=/usr \
1053        --enable-add-ons --disable-profile --libexecdir=/usr/bin \
1054        --with-headers=/usr/include &&
1055     make &&
1056     make check &&
1057     make install &&
1058     make localedata/install-locales
1060 "make localedata/install-locales" is of course optional as per current LFS.
1061 However, you'll need to install the minimum locales necessary for proper running
1062 of the gcc test suite as per the Chapter 5 glibc instructions above.
1064 We pass "--with-headers=/usr/include" to configure to ensure the kernel headers
1065 in /usr/include are used for this build. If we don't pass this switch then the
1066 headers from /stage1/include are used which of course is not ideal (although
1067 they should be identical). An advantage to using the switch is that the user
1068 will be informed immediately should they have forgotten to install the kernel
1069 headers into /usr/include.
1071 Once glibc is installed, don't forget to create the glibc configuration files
1072 (/etc/nsswitch.conf, /etc/localtime symlink and /etc/ld.so.conf).
1075 Chapter 6 - Adjusting our toolchain
1076 -----------------------------------
1078 Now that we have our Chapter 6 glibc successfully installed into /usr, it is
1079 time to adjust our toolchain to use the new glibc. From this point onwards we
1080 need to ensure that we do not link against the glibc in /stage1. This is
1081 basically "undoing" what we did in the Chapter 5 "lock in" stage.
1083 The first thing to do is adjust the linker scripts. We retained the
1084 binutils-build dir from "Chapter 5 binutils - pass 2" for this reason. Simply cd
1085 back into your binutils-build dir and run the following:-
1087     cd ld &&
1088     make INSTALL=/stage1/bin/install install-data-local
1090 This installs the adjusted linker scripts. The linker scripts now contain no
1091 mention of /stage1/lib. From now on everything will link ONLY against the libs
1092 in /usr/lib and /lib. The extra "INSTALL=/stage1/bin/install" is needed because
1093 the Makefile was created in Chapter 5 and still contains the reference to
1094 /usr/bin/install which we obviously haven't yet installed in Chapter 6. It is
1095 now safe to remove the binutils-build dir.
1097 The next thing to do is amend our gcc specs file so that it points to the new
1098 dynamic linker. Just like in Chapter 5, we use a sed to accomplish this:-
1100     SPECFILE=/stage1/lib/gcc-lib/i686-pc-linux-gnu/*/specs &&
1101     cp ${SPECFILE} ./XX && 
1102     sed 's@/stage1/lib/ld-linux.so.2@/lib/ld-linux.so.2@g' ./XX > ${SPECFILE} &&
1103     unset SPECFILE &&
1104     rm -f ./XX
1106 Again, cut'n'pasting the above is recommended. And just like before, it is a
1107 good idea to check the linker scripts and the specs file to ensure the intended
1108 changes were actually made.
1110 NOTE - the linker scripts will still contain a reference to
1111 "/stage1/i686-pc-linux-gnu/lib". This is unavoidable but luckily does not
1112 present a problem. There are no libs in that location as all the stage1 libs are
1113 located in "/stage1/lib".
1116 Chapter 6 - Installing binutils
1117 -------------------------------
1119 Basically the same as current LFS:-
1121     mkdir ../binutils-build &&
1122     cd ../binutils-build &&
1123     CFLAGS="-O2 -pipe" ../binutils-2*/configure --prefix=/usr --enable-shared &&
1124     make tooldir=/usr LDFLAGS="-s" &&
1125     make check &&
1126     make tooldir=/usr install &&
1127     rm /usr/lib/libiberty.a
1129 We remove libiberty.a because it is generally not meant to be installed on its
1130 own. For this reason we also elect not to install libiberty.h. Note also that
1131 with recent binutils there is no need for an additional "make tooldir=/usr
1132 install-info" as the info pages are now installed by the normal "make
1133 tooldir=/usr install".
1136 Chapter 6 - Installing gcc
1137 --------------------------
1139 Basically the same as current LFS. If using the same source from Chapter 5 there
1140 is no need to reapply the no_fixincludes or test_mmap patches (or the no_debug
1141 patches if you applied those as well).
1143     patch -Np1 -i ../gcc-3*.no_fixincludes.patch &&
1144     patch -Np1 -i ../gcc-3*.mmap_test.patch &&
1145     mkdir ../gcc-build &&
1146     cd ../gcc-build &&
1147     CFLAGS="-O2 -pipe" CXXFLAGS="-O2 -pipe" ../gcc-3*/configure --prefix=/usr \
1148        --enable-shared --enable-languages=c,c++ --enable-threads=posix \
1149        --enable-__cxa_atexit --enable-clocale=gnu &&
1150     make LDFLAGS="-s" &&
1151     make -k check
1152     make install &&
1153     ln -s ../usr/bin/cpp /lib &&
1154     ln -s gcc /usr/bin/cc &&
1155     rm /usr/lib/libiberty.a
1157 WARNING! - if reusing the same source from Chapter 5 pass 2 be sure to reverse
1158 the "specs" patch we applied in that step.
1160 Again, no need for "make bootstrap" here. You can still "make bootstrap" if you
1161 want to. Just substitute the "make" command from above with the one below.
1163     make BOOT_LDFLAGS="-s" BOOT_CFLAGS="-O2 -pipe" \
1164        STAGE1_CFLAGS="-pipe" bootstrap
1166 The notable difference between here and current LFS is the omission of
1167 "--with-slibdir=/lib" due to the fact that standard C executables no longer link
1168 against the shared libgcc_s.so.1 as used to happen when gcc-3.x was first
1169 released. We also do not symlink /usr/lib/cpp as we have never yet seen a
1170 package that needs this. And again we remove /usr/lib/libiberty.a for the reason
1171 mentioned earlier.
1174 Chapter 6 - Installing remaining packages
1175 -----------------------------------------
1177 The remaining packages in Chapter 6 are installed as per current LFS and in the
1178 same order. Except of course for binutils, gcc and glibc which are already
1179 installed. There should be no need to reinstall glibc at the end of Chapter 6.
1181 Something to think about here, theoretically, one should be able to take a newly
1182 built LFS and use it to rebuild itself (essentially perform Chapter 6 again
1183 without the assistance of the /stage1 stuff) and have it reproduce identical
1184 bytes. We have performed an "Iterative Analysis" of this and hope to present an
1185 automated way of verifying it in the future. But for now, from our findings we
1186 are able to make the following recommendations so as to ensure code
1187 repeatability:-
1189   * ncurses - be sure to use the "etip" patch that was used in Chapter 5
1191   * shadow - be sure to issue "touch /usr/bin/passwd" before configuring
1192            - be sure to perform the "Creating the /var/run/utmp, /var/log/wtmp
1193              and /var/log/btmp files" commands (from the end of Chapter 6)
1194              before the shadow package installation
1196   * groff - be sure to to configure like so:-
1198     PAGE=XX ./configure --prefix=/usr
1200     where "XX" is either "A4" or "letter" depending on your requiremnt.
1202   * perl - be sure to perform the "Creating the /etc/hosts file" commands before
1203     the perl installation
1206 Chapter 8 - Kernel compilation
1207 ------------------------------
1209 The following paragraph from the CHANGES file in linux-2.4.20 explains things
1210 nicely:-
1212 "The recommended compiler for the kernel is gcc 2.95.x (x >= 3), and it
1213 should be used when you need absolute stability. You may use gcc 3.0.x
1214 instead if you wish, although it may cause problems. Later versions of gcc
1215 have not received much testing for Linux kernel compilation, and there are
1216 almost certainly bugs (mainly, but not exclusively, in the kernel) that
1217 will need to be fixed in order to use these compilers. In any case, using
1218 pgcc instead of egcs or plain gcc is just asking for trouble."
1220 The current wisdom on the Linux Kernel Mailing List still seems to suggest that
1221 gcc-2.95.3 is the optimal compiler for compiling the kernel. Please note we are
1222 talking about current linux-2.4.x. As gcc-3.x continues to mature and linux-2.6
1223 nears release, the kernel developers may end up preferring gcc-3.x, but for now,
1224 the best bet is to use gcc-2.95.3 for your kernel compiles.
1226 The simplest way to achieve this is to install gcc-2.95.3 into a non-standard
1227 prefix, i.e. somewhere NOT in the standard system `PATH':-
1229     patch -Np1 -i ../gcc-2.95.3-2.patch &&
1230     echo timestamp > gcc/cstamp-h.in &&
1231     mkdir ../gcc-2-build &&
1232     cd ../gcc-2-build &&
1233     ../gcc-2.95*/configure --prefix=/opt/gcc-2.95.3 --enable-shared \
1234        --enable-languages=c &&
1235     make bootstrap &&
1236     make install
1238 *** WARNING *** BE EXTRA CAREFUL NOT TO INSTALL INTO /USR OR YOU WILL DESTROY
1239 YOUR CURRENT GCC!
1241 The patch used is the one from LFS 3.3. The line starting with "echo" is just a
1242 safeguard to prevent a "Makefile induced" rerun of autoconf in certain unknown
1243 circumstances.
1245 Then when it comes time to compile the kernel:-
1247     make mrproper &&
1248     make menuconfig &&
1249     make CC=/opt/gcc-2.95.3/bin/gcc dep &&
1250     make CC=/opt/gcc-2.95.3/bin/gcc bzImage &&
1251     make CC=/opt/gcc-2.95.3/bin/gcc modules &&
1252     make CC=/opt/gcc-2.95.3/bin/gcc modules_install &&
1253     cp arch/i386/boot/bzImage /boot/lfskernel &&
1254     cp System.map /boot
1256 The important thing is obviously the "CC=/opt/gcc-2.95.3/bin/gcc" on the make
1257 command lines.
1259 It is worth mentioning here that the BLFS project uses gcc-2.95.3 to create the
1260 c++ libs required by various closed source and precompiled binaries. With this
1261 in mind, you may want to combine the above gcc-2.95.3 instructions with the BLFS
1262 instructions to save yourself some time. Just be careful NOT to use
1263 --prefix=/usr.
1266 The perennial debate over which binutils release to use
1267 -------------------------------------------------------
1269 The binutils release that you typically find on ftp.gnu.org is commonly known as
1270 the "FSF" binutils. Noted hacker H.J. Lu also makes releases out of the main CVS
1271 repository and these are commonly known as the "HJL" binutils and can usually be
1272 found on ftp.kernel.org. Debate often arises over which version to use due to
1273 the fact that most mainstream distros tend to use the HJL releases even though
1274 they are typically marked as "beta".
1276 Here is our interpretation of the differences between the two:-
1278 HJL:
1279   - for Linux OS only
1280   - marked as "beta"
1281   - closely follows the CVS HEAD
1282   - usually contains the latest subtle bug fixes
1283   - usually has latest bug fixes for non-x86 arch's
1284   - usually a new release every time a significant bug that affects Linux gets
1285     fixed
1286   - theoretically less stable due to newness of code
1288 FSF:
1289   - supports more OS's (not only Linux)
1290   - latest code from the stable branch of CVS
1291   - sometimes not up-to-date WRT to the latest bleeding edge kernel, gcc and
1292     glibc subtleties
1293   - often includes features backported from the CVS HEAD after a period of
1294     testing
1295   - theoretically more stable
1297 You'll notice in the above points words like "usually" and "sometimes". This
1298 demonstrates how the situation can be different depending on which particular
1299 point in time you happen to be referring to. For example, from time to time
1300 there will be a new bleeding edge feature in gcc or glibc that requires support
1301 from binutils. During these times you will often hear the developers say "you
1302 must be using the latest HJL binutils version x.y.z.a.b".
1304 The only way to correctly choose the most appropriate release to use is to:-
1306   * stay abreast of the issues on the project mailing lists of the core
1307     toolchain packages
1309   * have a large dose of technical prowess and/or programming talent to
1310     understand all the issues
1312   * test like crazy by running the test suites
1314   * test like crazy by building full systems
1316 The facts of the matter are that the core toolchain packages are all very
1317 tightly bound and must be tested to ensure they work together. You basically
1318 have to build a full working distro and test every aspect of it to be fully
1319 satisfied.
1321 If you follow the project mailing lists of the core toolchain packages for long
1322 enough, you'll soon realise that the developers do not care much whether a
1323 particular release of "Package A" works with a particular release of "Package
1324 B". In other words, release coordination between the projects is not a priority.
1325 In reality, this means that Alan Cox is right when he says that you cannot just
1326 go to ftp.gnu.org and grab the latest of everything and always expect it to just
1327 work.
1330 Conclusion
1331 ----------
1333 We hope you have as much fun with this as we had making it. It was a lot of hard
1334 work but well worth it. Enjoy.
1336 Comments, feedback, flames, corrections, whatever, are all welcome.
1338 P.S. - Do not believe anyone who says that what is presented in this document is
1339        overkill. Those folk do not have enough knowledge to understand all the
1340        subtle issues.
1345 TODO
1346 ====
1348   - look at integrating test suites for the remaining packages
1349   - look at ways to remove /usr/include from gcc's include search path
1350   - look at ways to remove unwanted dirs from "gcc -print-search-dirs"
1351   - look at using gcc's -enable-version-specific-runtime-libs for installing
1352     multiple versions of gcc into the one prefix
1357 CREDITS
1358 =======
1360 Ryan - Devised the whole scheme. Bucket loads of testing and research. Helped
1361        with documentation.
1363 Greg - Wrote the bulk of the documentation. Helped refine the process, Helped
1364        with testing and research. Initially ranted on lfs-dev about potential
1365        build flaws and thus spurred Ryan into action :-)
1367 All the good folk on lfs-dev have been very helpful with ideas, feedback,
1368 corrections and testing. Thanks guys.
1371 FOOTNOTES
1372 =========
1374 1. http://www.linuxbase.org/impl/
1375 2. http://archive.linuxfromscratch.org/mail-archives/lfs-dev/2003/03/0193.html
1376 3. http://gcc.gnu.org/install/configure.html
1377 4. http://gcc.gnu.org/ml/gcc/2002-08/msg01071.html
1378 5. http://gcc.gnu.org/install/test.html
1379 6. http://mail.gnu.org/archive/html/bug-ncurses/2003-03/msg00019.html
1381 Copyright (c) 2003
1382 Ryan Oliver <Ryan.Oliver@pha.com.au> and Greg Schafer <gschafer@zip.com.au>