Typo
[linux_from_scratch_hints.git] / OLD / crosscompiling-x86.txt
blob2000968c4b011c68faf3762c49ca143287a9eb40
1 TITLE:          Crosscompiling on IA-32 (x86) platforms
2 LFS VERSION:    All (fixes for 4.1)
3 AUTHOR:         Daniel Baumann <danielbaumann@linuxmail.org>
5 SYNOPSIS:
6         How to compile a LFS system on a fast machine and use it on your old box
8 HINT:
10 CHANGELOG
11 =========
13         * 2003-07-23    Initial revision
16 DISCLAIMER
17 ==========
19 You use this LFS-Hint at your own risk.
21 Neither the author, nor the Linux From Scratch project accepts any reponsibility
22 for anything that happens when using this document or associated files.
24 This hint is originally written by Nicholas Dille <webmaster@rakshas.de>, now
25 updated and maintained by Daniel Baumann <danielbaumann@linuxmail.org>.
27 I would like to thank Nicholas for allowing to continue his good work.
30 INTRODUCTION
31 ============
33 Alright, we have gathered here to mourn our failing in compiling our favourite
34 LFS system for our beloved outdated hardware and have either dived right into it
35 being stopped by unforseen difficulties or been hinted at this document to
36 circumvent some known problems.
38 Lets say you call an old i486 your own but moved on to some i686 based box some
39 time ago. Now you decided that the i486 might serve wonderfully as your personal
40 file/printer server, internet gateway, Ogg player, a combination or some
41 entirely different purpose.
43 Without even trying you can imagine that it will probably take quite some time
44 to compile a LFS system caused by the lack of memory, harddisk space and mere
45 performance. Wouldn't it be nice to have your, by comparison, blindingly fast
46 i686 compile LFS and then use the resulting system on your i486?
48 I can assure you that several people had the same idea and tried solving the
49 difficulties encountered. I hope to compile most of those experiences here to
50 help you find the least time consuming path from a bare i486 to a lovely lfs'd
51 linux box.
53 To give you a rough outline how I organized the hint let me elaborate shortly:
54 We will learn what problems you may encounter, under what conditions, why they
55 exist AFAIK and then at last come to the part where you can actively profit from
56 this by giving it a try.
58 In any case if you feel the hint missing something important, doesn't explain
59 something to your convenience, completely missed some point or if you'd just
60 like to give me a big hug, I'd be honestly happy to hear you out.
62 Nicholas like to thank:
64         * Gerard Beekmans, for this wonderful project
65         * Tommy Wareing, for the uname hack
66         * Arthur H. Johnson II, for bullying me into rewriting this hint and
67           helping collecting new material
68         * Christophe Devine, for the kernel module uname hack
69         * Yann Guidon, for extending the kernel module uname hack
72 PREPARATION
73 ===========
75 Why the naive approach fails
76 ----------------------------
78 In case you only paste the commands provided by the LFS book to your console,
79 wait some time and come back for the next package you might want to read through
80 this to understand what really happens.
82 Let us devide the package which you are about to compile into two destinct
83 categories: the friendly and the painful packages.
85 Friendly packages are configured using GNU autoconf (./configure [...]) which
86 usually check for a sane environment and also try to determine which system it
87 will be compiled on. This information is then passed on to gcc which then knows
88 which architecture specific features it is allows to use. These packages are
89 easily persuaded to believe they are being compiled on a weaker platform.
91 In contrast, the painful packages do not provide any automatic check whether
92 their prerequisites are satisfied and trust the user to know what he is doing.
93 In this case the decision which platform is present is left to gcc which uses
94 uname and CFLAGS/CXXFLAGS to guess it. Unfortunately these packages are harder
95 to force to compile for a weaker platform.
97 So, if you leave any of those packages alone they will compile for the platform
98 they detect either by the configure script or by means of gcc. We need to make
99 autoconf and gcc think that the underlying architecture is of a different type.
102 $CHOST
103 ------
105 Autoconf normally tries to guess the type of machine we are compiling on.
106 Because we use our new computer instead of the old, we have to fake this value
107 manually.
109 You can get the correct value with
111         # uname -m
113 from your target computer.
115 If you haven't already a running Linux system on the target machine, use a
116 corresponding value from this list instead:
118         Intel 386:                              i386
119         Intel 486:                              i486
120         Intel Pentium 1 and MMX:                i586
121         Intel Pentium Pro, 2, 3 and 4:          i686
123         AMD K6-1, K6-2 and K6-3:                i586
124         AMD Athlon (all versions):              i686
126 Export this to the build machine with:
128         # export CHOST=$VALUE-pc-linux-gnu
132         # export CHOST=i586-pc-linux-gnu
134 as an example for an Intel Pentium 1.
137 $CFLAGS/$CXXFLAGS
138 -----------------
140 CFLAGS is an environment variable which is used by any honourable package to
141 pass user flags on to gcc. It is often used to set the optimization level (-On
142 where n is a number). For details please refer to the optimization hint. In our
143 case we additionally try to force gcc to compile for the desired architecture by
144 setting CFLAGS.
146 Choose from this list:
148         Intel 386:                      i386
149         Intel 486:                      i486
150         Intel Pentium 1:                pentium
151         Intel Pentium MMX:              pentium-mmx
152         Intel Pentium Pro:              pentiumpro
153         Intel Pentium 2:                pentium2
154         Intel Pentium 3:                pentium3
155         Intel Pentium 4:                pentium4
157         AMD K6-1:                       k6
158         AMD K6-2:                       k6-2
159         AMD K6-3:                       k6-3
160         AMD Athlon 1 (Classic):         athlon
161         AMD Athlon 2 (Thunderbird):     athlon-tbird
162         AMD Athlon 3 (XP):              athlon-xp
163         AMD Athlon 4 (Palomino):        athlon-4
164         AMD Athlon 5 (MP):              athlon-mp
166 Set it to the system with
168         # export CFLAGS=-march=$VALUE
169         # export CXXFLAGS=-march=$VALUE
173         # export CFLAGS=-march=pentium
174         # export CXXFLAGS=-march=pentium
176 as an example for Intel Pentium 1. Consult the gcc online-manual on
177 http://gcc.gnu.org/ for other values. If you know what you do, you can also
178 set some optimization flags, see the optimization hint for that.
181 Installing friendly packages
182 ============================
184 As I have stated before configure scripts are specially easy to persuade into
185 believing to be running on a different architecture.
187 You will have to add
189         "--host=$CHOST --target=$CHOST"
191 to the configure scripts' arguments.
193 As an example, for Bash in Chapter 5, you have to use
195         # ./configure --host=$CHOST --target=$CHOST \
196           --enable-static-link --prefix=$LFS/static --with-curses
199 Installing painful packages
200 ===========================
202 This chapter will provide you with a list of packages which do not come with a
203 configure script or which come with one that does not honour '--host' and
204 '--target' options. Use the following fixes to proper install the packages.
207 Bin86 0.16.3
208 ------------
210 Use the following commands to write your CFLAGS variable into bin86's Makefile.
212         # make CFLAGS="-D_POSIX_SOURCE $CFLAGS"
215 Bzip2 1.0.2
216 -----------
218 Use the following commands to patch bzip2:
220         # cp Makefile Makefile.backup
221         # sed -e 's%$(BIGFILES)%$(BIGFILES) $(OPT)%' \
222                 Makefile.backup > Makefile
223         # cp Makefile-libbz2_so Makefile-libbz2_so.backup
224         # sed -e 's%$(BIGFILES)%$(BIGFILES) $(OPT)%' \
225                 Makefile-libbz2_so.backup > Makefile-libbz2_so
227 Add 'OPT="$CFLAGS"' to the make commands:
229         For chapter 5:
230         # make CC="gcc -static" OPT="$CFLAGS"
232         For chapter 6:
233         # make -f Makefile-libbz2_so OPT="$CFLAGS"
234         # make OPT="$CFLAGS"
237 Gcc 3.1.2
238 ---------
240 Gcc compilation is devided into three parts. Stage1 is the first run, where
241 CFLAGS/CXXFLAGS are respected well. In stage2, gcc compiles itselfs. To make
242 use of our flags, we use BOOT_CFLAGS to pass through our settings. Stage3 is
243 just a check-run to verify a working gcc.
245         For chapter 5:
246         # make BOOT_CFLAGS=$CFLAGS BOOT_LDFLAGS="-static" bootstrap
248         For chapter 6:
249         # make BOOT_CFLAGS=$CFLAGS bootstrap
252 Kbd 1.08
253 --------
255 Use the following commands to write your CFLAGS variable into kbd's Makefile.
257         # make CFLAGS="$CFLAGS"
260 Lilo 22.2
261 ---------
263 Use the following make commands:
265         # make OPT="$CFLAGS"
268 Man 1.5k
269 --------
271 Use the following commands to add your CFLAGS variable to man2html's Makefile:
273         # cp man2html/Makefile.in man2html/Makefile.in.backup
274         # sed -e "s/CFLAGS = /CFLAGS = $CFLAGS /" \
275                 man2html/Makefile.in.backup > man2html/Makefile.in
278 Net-tools 1.60
279 --------------
281 Use the following make command:
283         # make COPTS="-D_GNU_SOURCE -Wall $CFLAGS"
286 Netkit-base 0.17
287 ----------------
289 This package does not need any changes to the book's commands. It will pick the
290 value of the CFLAGS variable and apply it correctly.
293 Perl 5.8.0
294 ----------
296 The configure scripts shipped with perl are not autoconf-scripts. They use
297 diffrent flags for the same meaning:
299         # ./configure.gnu -Dhost=$CHOST -Dtarget=$CHOST -Darchname=$CHOST \
300           --prefix=/usr
303 Procinfo 18
304 -----------
306 Add 'CFLAGS="$CFLAGS"' to the make command:
308         # make LDLIBS=-lncurses CFLAGS="$CFLAGS"
311 Procps 3.1.5
312 ------------
314 Add 'OPT="$CFLAGS"' to the make command:
316         # make OPT="$CFLAGS"
318 Please note that the build process will fail with top.c if your CFLAGS variable
319 did not contain the -On switch. If that is the case please use the following
320 line:
322         # make OPT="$CFLAGS -O2"
324 This switch will not add any additional kind of optimization to your build pro-
325 cess because -O2 is usually assumed by gcc in case it is not told otherwise.
326 I will contact the author about this behaviour of procps and update the hint
327 accordingly.
330 Psmisc 21.2
331 -----------
333 Add 'AM_CFLAGS="$CFLAGS"' to the make command:
335         # make AM_CFLAGS="$CFLAGS"
338 Sysklogd 1.4.1
339 --------------
341 Add 'RPM_OPT_FLAGS="$CFLAGS"' to the make command:
343         # make RPM_OPT_FLAGS="$CFLAGS"
346 Sysvinit 2.84
347 -------------
349 Add 'CFLAGS="-Wall -D_GNU_SOURCE $CFLAGS"' to the make command:
351         # make -C src CFLAGS="-Wall -D_GNU_SOURCE $CFLAGS"
354 APPENDIX
355 ========
357 The uname hack
358 --------------
360 Generally spoken the uname hack will fool the system. It pretends that linux is
361 running on different hardware.
363 For example, you are using an i686 based system to compile a lfs for an old
364 i486. Then the uname hack would force uname to lie about the underlying
365 hardware - not reporting i686 but i486.
367 . Traditional
369   Save the original uname program with:
371         # mv /bin/uname /bin/uname.backup
373   Create the new uname with:
375         cat > /bin/uname < "EOF"
376         #!/bin/sh
378         /bin/uname.backup "$@" | sed "s/i[456]86/$ARCH/"
379         EOF
381   And give it the correct permissions:
383         # chmod 755 /bin/uname
385   The traditional uname hack will have to be installed three times:
386   once before you start your lfs compilation to replace the uname binary of your
387   host system, then when you enter chroot to force your statically linked
388   environment into assuming a different architecture and last after installing
389   sh-utils in chapter 6 because they will replace the uname binary.
391 . Kernel module
393   Please check out the niftiness of this version of the uname hack!
395   Put the code below into the file uname_i586.c and compile with the following
396   command:
398         # gcc -I/usr/src/linux/include -c -DMODULE \
399           -DUNAME_DUMB_STEPPING=\'4\' uname_i586.c
401   By adjusting the '4' in the compilation command you will be able to specify
402   the desired platform at compile time without changing the code :)
404   Listing of uname_i586.c:
405   /*
406         This simple piece of code simply turns your ix86 into a i586 -
407         useful if you're cross-compiling for a weaker platform.
409         Compile with: gcc -I/usr/src/linux/include -c -DMODULE uname_i586.c
410         and then: insmod ./uname_i586.o
412         Revision : whygee@f-cpu.org, Wed Apr 10 07:45:11 CEST 2002 :
413         added the UNAME_DUMB_STEPPING parameter/ifdef so the user can
414         modify it at compile time. it's just a quick hack.
415         your command line for simulating a 486 will look like :
416                 gcc -I/usr/src/linux/include -c -DMODULE \
417                         -DUNAME_DUMB_STEPPING=\'4\' uname_i586.c
418   */
420   #include <linux/module.h>
421   #include <linux/utsname.h>
423   #ifndef UNAME_DUMB_STEPPING
424   #define UNAME_DUMB_STEPPING '5';
425   /* #error "no stepping specified." */
426   #endif
428   char save;
430   int init_module( void )
431   {
432         save = system_utsname.machine[1];
433         system_utsname.machine[1] = UNAME_DUMB_STEPPING;
434         return( 0 );
435   }
437   void cleanup_module( void )
438   {
439         system_utsname.machine[1] = save;
440   }
442 Notes
443 -----
445 . Compiling Glibc without optimization fails (need at leas -O1)
448 END OF CROSSCOMPILING-X86-HINT