lfs/chapter05/gcc-pass1.xml
2025-05-31 19:08:08 +08:00

279 lines
10 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % general-entities SYSTEM "../general.ent">
%general-entities;
]>
<sect1 id="ch-tools-gcc-pass1" role="wrap" xreflabel="gcc-pass1">
<?dbhtml filename="gcc-pass1.html"?>
<sect1info condition="script">
<productname>gcc-pass1</productname>
<productnumber>&gcc-version;</productnumber>
<address>&gcc-url;</address>
</sect1info>
<title>GCC-&gcc-version; - Pass 1</title>
<indexterm zone="ch-tools-gcc-pass1">
<primary sortas="a-GCC">GCC</primary>
<secondary>tools, pass 1</secondary>
</indexterm>
<sect2 role="package">
<title/>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="../chapter08/gcc.xml"
xpointer="xpointer(/sect1/sect2[1]/para[1])"/>
<segmentedlist>
<segtitle>&buildtime;</segtitle>
<segtitle>&diskspace;</segtitle>
<seglistitem>
<seg>&gcc-tmpp1-sbu;</seg>
<seg>&gcc-tmpp1-du;</seg>
</seglistitem>
</segmentedlist>
</sect2>
<sect2 role="installation">
<title>Installation of Cross GCC</title>
<para>GCC requires the GMP, MPFR and MPC packages. As these packages may
not be included in your host distribution, they will be built with
GCC. Unpack each package into the GCC source directory and rename the
resulting directories so the GCC build procedures will automatically
use them:</para>
<note><para>There are frequent misunderstandings about this chapter. The
procedures are the same as every other chapter, as explained earlier (<xref
linkend='buildinstr'/>). First, extract the gcc-&gcc-version; tarball from the sources
directory, and then change to the directory created. Only then should you
proceed with the instructions below.</para></note>
<screen><userinput remap="pre">tar -xf ../mpfr-&mpfr-version;.tar.xz
mv -v mpfr-&mpfr-version; mpfr
tar -xf ../gmp-&gmp-version;.tar.xz
mv -v gmp-&gmp-version; gmp
tar -xf ../mpc-&mpc-version;.tar.gz
mv -v mpc-&mpc-version; mpc</userinput></screen>
<para>On 64-bit MIPS hosts, set the default directory name for
64-bit libraries to <quote>lib</quote>:</para>
<screen><userinput remap="pre">sed -e 's/lib64/lib/' \
-i.orig gcc/config/mips/{mips.h,t-linux64}</userinput></screen>
<para>The GCC documentation recommends building GCC
in a dedicated build directory:</para>
<screen><userinput remap="pre">mkdir -v build
cd build</userinput></screen>
<para>Probe the NaN encoding of the CPU:</para>
<screen><userinput remap="pre">cat > nan2008.c &lt;&lt; \EOF
<literal>#include &lt;stdio.h&gt;
int main()
{
int x;
asm("cfc1\t%0,$31":"=r"(x));
return !(x &amp; 1 &lt;&lt; 18);
}</literal>
EOF
gcc nan2008.c -o nan2008
if ! grep -q nan_legacy /proc/cpuinfo &amp;&amp;
(grep -q nan_2008 /proc/cpuinfo || ./nan2008) then
NAN_SWITCH=--with-nan=2008
fi</userinput></screen>
<para>Prepare GCC for compilation:</para>
<screen><userinput remap="configure">../configure \
--target=$LFS_TGT \
--prefix=$LFS/tools \
--with-arch=mips64r2 \
--with-glibc-version=&glibc-version; \
--with-sysroot=$LFS \
--with-newlib \
--without-headers \
--enable-default-pie \
--enable-default-ssp \
--disable-nls \
--disable-shared \
--disable-multilib \
--disable-threads \
--disable-libatomic \
--disable-libgomp \
--disable-libquadmath \
--disable-libssp \
--disable-libvtv \
--disable-libstdcxx \
--enable-languages=c,c++ \
$NAN_SWITCH
unset NAN_SWITCH</userinput></screen>
<variablelist>
<title>The meaning of the configure options:</title>
<varlistentry>
<term><parameter>--with-arch=mips64r2</parameter></term>
<listitem>
<para>Set the default value of <option>-march=</option> to
mips64r2. Most distros use it as the baseline for the MIPS port,
so we are doing the same. You may change it if your CPU is not
compatible with mips64r2 (for example, mips3 or mips64r6), or
your CPU is stronger than mips64r2 and you don't care about the
compatibility with mips64r2, but we've not tested any setting
other than mips64r2.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--with-glibc-version=&glibc-version;</parameter></term>
<listitem>
<para>This option specifies the version of Glibc which will be
used on the target. It is not relevant to the libc of the host
distro because everything compiled by pass1 GCC will run in the
chroot environment, which is isolated from libc of the host
distro.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--with-newlib</parameter></term>
<listitem>
<para>Since a working C library is not yet available, this ensures
that the inhibit_libc constant is defined when building libgcc. This prevents
the compiling of any code that requires libc support.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--without-headers</parameter></term>
<listitem>
<para>When creating a complete cross-compiler, GCC requires
standard headers compatible with the target system. For our
purposes these headers will not be needed. This switch prevents
GCC from looking for them.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--enable-default-pie and
--enable-default-ssp</parameter></term>
<listitem>
<para>Those switches allow GCC to compile programs with
some hardening security features (more information on those in
the <xref linkend="pie-ssp-info"/> in chapter 8) by default. They
are not strictly needed at this stage, since the compiler will
only produce temporary executables. But it is cleaner to have the
temporary packages be as close as possible to the final ones.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--disable-shared</parameter></term>
<listitem>
<para>This switch forces GCC to link its internal libraries
statically. We need this because the shared libraries require Glibc,
which is not yet installed on the target system.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--disable-multilib</parameter></term>
<listitem>
<para>On 64-bit MIPS, LFS does not support a multilib configuration.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--disable-threads,
--disable-libatomic, --disable-libgomp,
--disable-libquadmath, --disable-libssp, --disable-libvtv,
--disable-libstdcxx</parameter></term>
<listitem>
<para>These switches disable support for
threading, libatomic, libgomp, libquadmath, libssp,
libvtv, and the C++ standard library respectively. These features
may fail to compile when building a cross-compiler and are not
necessary for the task of cross-compiling the temporary libc.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>--enable-languages=c,c++</parameter></term>
<listitem>
<para>This option ensures that only the C and C++ compilers are built.
These are the only languages needed now.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>$NAN_SWITCH</parameter></term>
<listitem>
<para>If the CPU encodes NaN (Not a Number) values following
IEEE 754-2008 (indicated by the 16th bit of FCSR), this variable
will be set to <option>--with-nan=2008</option> so GCC will be
configured to use the IEEE 754-2008 encoding for NaN values as
the default. If this option is not properly set to match the CPU,
depending on the host kernel configuration, the programs compiled
by GCC may fail to start with
<computeroutput>Exec format error</computeroutput> (causing
<xref linkend='ch-tools-chroot'/> to fail), or produce bad results
(causing test failures building some packages, for example Glibc
and Python).</para>
</listitem>
</varlistentry>
</variablelist>
<para>Compile GCC by running:</para>
<screen><userinput remap="make">make</userinput></screen>
<para>Install the package:</para>
<screen><userinput remap="install">make install</userinput></screen>
<para>This build of GCC has installed a couple of internal system
headers. Normally one of them, <filename>limits.h</filename>, would in turn
include the corresponding system <filename>limits.h</filename> header, in
this case, <filename>$LFS/usr/include/limits.h</filename>. However, at the
time of this build of GCC <filename>$LFS/usr/include/limits.h</filename>
does not exist, so the internal header that has just been installed is a
partial, self-contained file and does not include the extended features of
the system header. This is adequate for building Glibc, but the full
internal header will be needed later. Create a full version of the internal
header using a command that is identical to what the GCC build system does
in normal circumstances:</para>
<note>
<para>The command below shows an example of nested command substitution
using two methods: backquotes and a <literal>$()</literal> construct.
It could be rewritten using the same method for both substitutions,
but is shown this way to demonstrate how they can be mixed. Generally
the <literal>$()</literal> method is preferred.</para>
</note>
<screen><userinput remap="install">cd ..
cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
`dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include/limits.h</userinput></screen>
</sect2>
<sect2 role="content">
<title/>
<para>Details on this package are located in
<xref linkend="contents-gcc" role="."/></para>
</sect2>
</sect1>