source: chapter05/gcc-pass2.xml@ 5f7456b

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 6.6 6.7 6.8 7.0 7.1 7.2 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since 5f7456b was 5f7456b, checked in by Matthew Burgess <matthew@…>, 15 years ago

Various text corrections from Chris Staub. Fixes #2470.

git-svn-id: http://svn.linuxfromscratch.org/LFS/trunk/BOOK@9037 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

  • Property mode set to 100644
File size: 12.2 KB
Line 
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
4 <!ENTITY % general-entities SYSTEM "../general.ent">
5 %general-entities;
6]>
7
8<sect1 id="ch-tools-gcc-pass2" role="wrap">
9 <?dbhtml filename="gcc-pass2.html"?>
10
11 <sect1info condition="script">
12 <productname>gcc</productname>
13 <productnumber>&gcc-version;</productnumber>
14 <address>&gcc-url;</address>
15 </sect1info>
16
17 <title>GCC-&gcc-version; - Pass 2</title>
18
19 <indexterm zone="ch-tools-gcc-pass2">
20 <primary sortas="a-GCC">GCC</primary>
21 <secondary>tools, pass 2</secondary>
22 </indexterm>
23
24 <sect2 role="package">
25 <title/>
26
27 <xi:include xmlns:xi="http://www.w3.org/2003/XInclude"
28 href="../chapter06/gcc.xml"
29 xpointer="xpointer(/sect1/sect2[1]/para[1])"/>
30
31 <segmentedlist>
32 <segtitle>&buildtime;</segtitle>
33 <segtitle>&diskspace;</segtitle>
34
35 <seglistitem>
36 <seg>&gcc-ch5p2-sbu;</seg>
37 <seg>&gcc-ch5p2-du;</seg>
38 </seglistitem>
39 </segmentedlist>
40
41 </sect2>
42
43 <sect2 role="installation">
44 <title>Installation of GCC</title>
45
46 <para>Versions of GCC later than 4.3 will treat this build as if
47 it were a relocated compiler and disallow searching for startfiles in
48 the location specified by <parameter>--prefix</parameter>. Since this
49 will not be a relocated compiler, and the startfiles in
50 <filename class="directory">/tools</filename> are crucial to building
51 a working compiler linked to the libs in <filename class="directory">/tools</filename>,
52 apply the following patch which partially reverts GCC to its old behavior:</para>
53
54<screen><userinput remap="pre">patch -Np1 -i ../&gcc-startfiles-patch;</userinput></screen>
55
56 <para>Under normal circumstances the GCC <command>fixincludes</command> script
57 is run in order to fix potentially broken header files. As GCC-&gcc-version;
58 and Glibc-&glibc-version; have already been installed at this point, and
59 their respective header files are known to not require fixing, the
60 <command>fixincludes</command> script is not required. In fact, running
61 this script may actually pollute the build environment by
62 installing fixed headers from the host system into GCC's private include
63 directory. The running of the <command>fixincludes</command> script can
64 be suppressed by issuing the following commands:</para>
65
66<screen><userinput remap="pre">cp -v gcc/Makefile.in{,.orig}
67sed 's@\./fixinc\.sh@-c true@' gcc/Makefile.in.orig &gt; gcc/Makefile.in</userinput></screen>
68
69 <para>For x86 machines, a bootstrap build of GCC uses the
70 <option>-fomit-frame-pointer</option> compiler flag. Non-bootstrap builds
71 omit this flag by default, and the goal should be to produce a compiler
72 that is exactly the same as if it were bootstrapped. Apply the following
73 <command>sed</command> command to force the build to use the flag:</para>
74
75<screen><userinput remap="pre">cp -v gcc/Makefile.in{,.tmp}
76sed 's/^T_CFLAGS =$/&amp; -fomit-frame-pointer/' gcc/Makefile.in.tmp \
77 &gt; gcc/Makefile.in</userinput></screen>
78
79 <para>The following command will change the location of GCC's default
80 dynamic linker to use the one installed in
81 <filename class="directory">/tools</filename>. It also removes <filename
82 class="directory">/usr/include</filename> from GCC's include search path.
83 Doing this now rather than adjusting the specs file after installation
84 ensures that the new dynamic linker is used during the actual build of
85 GCC. That is, all of the binaries created during the build will link
86 against the new Glibc. Issue:</para>
87
88<screen><userinput remap="pre">for file in \
89 $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
90do
91 cp -uv $file{,.orig}
92 sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&amp;@g' \
93 -e 's@/usr@/tools@g' $file.orig &gt; $file
94 echo '
95#undef STANDARD_INCLUDE_DIR
96#define STANDARD_INCLUDE_DIR 0
97#define STANDARD_STARTFILE_PREFIX_1 ""
98#define STANDARD_STARTFILE_PREFIX_2 ""' &gt;&gt; $file
99 touch $file.orig
100done</userinput></screen>
101
102 <para>In case the above seems hard to follow, let's break it down a bit.
103 First we find all the files under the
104 <filename class="directory">gcc/config</filename> directory that are named
105 either <filename>linux.h</filename>, <filename>linux64.h</filename> or
106 <filename>sysv4.h</filename>.
107 For each file found, we copy it to a file of the same name but with an added
108 suffix of <quote>.orig</quote>. Then the first sed expression prepends
109 <quote>/tools</quote> to every instance of <quote>/lib/ld</quote>,
110 <quote>/lib64/ld</quote> or <quote>/lib32/ld</quote>, while the second one
111 replaces hard-coded instances of <quote>/usr</quote>. Then we add our define
112 statements which alter the include search path and the default startfile prefix
113 to the end of the file.
114 Finally, we use <command>touch</command> to update the timestamp on the copied files.
115 When used in conjunction with <command>cp -u</command>, this prevents unexpected
116 changes to the original files in case the commands are inadvertently run twice.
117 </para>
118
119 <para>On x86_64, unsetting the multilib spec for GCC ensures that it
120 won't attempt to link against libraries on the host:</para>
121
122<screen><userinput remap="pre">case $(uname -m) in
123 x86_64)
124 for file in $(find gcc/config -name t-linux64) ; do \
125 cp -v $file{,.orig}
126 sed '/MULTILIB_OSDIRNAMES/d' $file.orig &gt; $file
127 done
128 ;;
129esac</userinput></screen>
130
131 <para>As in the first build of GCC it requires the GMP and MPFR packages.
132 Unpack the tarballs and move them into the required directory names:</para>
133
134<screen><userinput remap="pre">tar -jxf ../mpfr-&mpfr-version;.tar.bz2
135mv -v mpfr-&mpfr-version; mpfr
136tar -jxf ../gmp-&gmp-version;.tar.bz2
137mv -v gmp-&gmp-version; gmp</userinput></screen>
138
139 <para>Create a separate build directory again:</para>
140
141<screen><userinput remap="pre">mkdir -v ../gcc-build
142cd ../gcc-build</userinput></screen>
143
144 <para>Before starting to build GCC, remember to unset any environment
145 variables that override the default optimization flags.</para>
146
147 <para>Now prepare GCC for compilation:</para>
148
149<screen><userinput remap="configure">CC="$LFS_TGT-gcc -B/tools/lib/" \
150 AR=$LFS_TGT-ar RANLIB=$LFS_TGT-ranlib \
151 ../gcc-&gcc-version;/configure --prefix=/tools \
152 --with-local-prefix=/tools --enable-clocale=gnu \
153 --enable-shared --enable-threads=posix \
154 --enable-__cxa_atexit --enable-languages=c,c++ \
155 --disable-libstdcxx-pch --disable-multilib \
156 --disable-bootstrap</userinput></screen>
157
158 <variablelist>
159 <title>The meaning of the new configure options:</title>
160
161 <varlistentry>
162 <term><parameter>--enable-clocale=gnu</parameter></term>
163 <listitem>
164 <para>This option ensures the correct locale model is selected
165 for the C++ libraries under all circumstances. If the configure
166 script finds the <emphasis>de_DE</emphasis> locale installed,
167 it will select the correct gnu locale model. However, if the
168 <emphasis>de_DE</emphasis> locale is not installed, there is the
169 risk of building Application Binary Interface (ABI)-incompatible
170 C++ libraries because the incorrect generic locale model may be
171 selected.</para>
172 </listitem>
173 </varlistentry>
174
175 <varlistentry>
176 <term><parameter>--enable-threads=posix</parameter></term>
177 <listitem>
178 <para>This enables C++ exception handling for multi-threaded code.</para>
179 </listitem>
180 </varlistentry>
181
182 <varlistentry>
183 <term><parameter>--enable-__cxa_atexit</parameter></term>
184 <listitem>
185 <para>This option allows use of <function>__cxa_atexit</function>,
186 rather than <function>atexit</function>, to register C++ destructors
187 for local statics and global objects. This option is essential for
188 fully standards-compliant handling of destructors. It also affects
189 the C++ ABI, and therefore results in C++ shared libraries and C++
190 programs that are interoperable with other Linux distributions.</para>
191 </listitem>
192 </varlistentry>
193
194 <varlistentry>
195 <term><parameter>--enable-languages=c,c++</parameter></term>
196 <listitem>
197 <para>This option ensures that both the C and C++ compilers are
198 built.</para>
199 </listitem>
200 </varlistentry>
201
202 <varlistentry>
203 <term><parameter>--disable-libstdcxx-pch</parameter></term>
204 <listitem>
205 <para>Do not build the pre-compiled header (PCH) for
206 <filename class="libraryfile">libstdc++</filename>. It takes up a
207 lot of space, and we have no use for it.</para>
208 </listitem>
209 </varlistentry>
210
211 <varlistentry>
212 <term><parameter>--disable-bootstrap</parameter></term>
213 <listitem>
214 <para>For native builds of GCC, the default is to do a "bootstrap"
215 build. This does not just compile GCC, but compiles it several times.
216 It uses the programs compiled in a first round to compile itself a
217 second time, and then again a third time. The second and third
218 iterations are compared to make sure it can reproduce itself
219 flawlessly. This also implies that it was compiled correctly.
220 However, the LFS build method should provide a solid compiler
221 without the need to bootstrap each time.</para>
222 </listitem>
223 </varlistentry>
224
225 </variablelist>
226
227 <para>Compile the package:</para>
228
229<screen><userinput remap="make">make</userinput></screen>
230
231 <para>Install the package:</para>
232
233<screen><userinput remap="install">make install</userinput></screen>
234
235 <para>As a finishing touch, create a symlink. Many programs and scripts
236 run <command>cc</command> instead of <command>gcc</command>, which is
237 used to keep programs generic and therefore usable on all kinds of UNIX
238 systems where the GNU C compiler is not always installed. Running
239 <command>cc</command> leaves the system administrator free to decide
240 which C compiler to install:</para>
241
242<screen><userinput remap="install">ln -vs gcc /tools/bin/cc</userinput></screen>
243
244 <caution>
245 <para>At this point, it is imperative to stop and ensure that the basic
246 functions (compiling and linking) of the new toolchain are working as
247 expected. To perform a sanity check, run the following commands:</para>
248
249<screen><userinput>echo 'main(){}' &gt; dummy.c
250cc dummy.c
251readelf -l a.out | grep ': /tools'</userinput></screen>
252
253 <para>If everything is working correctly, there should be no errors,
254 and the output of the last command will be of the form:</para>
255
256<screen><computeroutput>[Requesting program interpreter: /tools/lib/ld-linux.so.2]</computeroutput></screen>
257
258 <para>Note that <filename class="directory">/tools/lib</filename>, or
259 <filename class="directory">/tools/lib64</filename> for 64-bit machines
260 appears as the prefix of the dynamic linker.</para>
261
262 <para>If the output is not shown as above or there was no output at all,
263 then something is wrong. Investigate and retrace the steps to find out
264 where the problem is and correct it. This issue must be resolved before
265 continuing on. First, perform the sanity check again, using
266 <command>gcc</command> instead of <command>cc</command>. If this works,
267 then the <filename class="symlink">/tools/bin/cc</filename> symlink is
268 missing. Install the symlink as per above.
269 Next, ensure that the <envar>PATH</envar> is correct. This
270 can be checked by running <command>echo $PATH</command> and verifying that
271 <filename class="directory">/tools/bin</filename> is at the head of the
272 list. If the <envar>PATH</envar> is wrong it could mean that you are not
273 logged in as user <systemitem class="username">lfs</systemitem> or that
274 something went wrong back in <xref linkend="ch-tools-settingenviron"
275 role="."/></para>
276
277 <para>Once all is well, clean up the test files:</para>
278
279<screen><userinput>rm -v dummy.c a.out</userinput></screen>
280
281 </caution>
282
283 </sect2>
284
285 <sect2 role="content">
286 <title/>
287
288 <para>Details on this package are located in
289 <xref linkend="contents-gcc" role="."/></para>
290
291 </sect2>
292
293</sect1>
Note: See TracBrowser for help on using the repository browser.