gcc-pass1.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?xml version="1.0" encoding="UTF-8"?>
  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. <sect1 id="ch-tools-gcc-pass1" role="wrap" xreflabel="第一遍的 GCC">
  8. <?dbhtml filename="gcc-pass1.html"?>
  9. <sect1info condition="script">
  10. <productname>gcc-pass1</productname>
  11. <productnumber>&gcc-version;</productnumber>
  12. <address>&gcc-url;</address>
  13. </sect1info>
  14. <title>GCC-&gcc-version; - 第一遍</title>
  15. <indexterm zone="ch-tools-gcc-pass1">
  16. <primary sortas="a-GCC">GCC</primary>
  17. <secondary>tools, pass 1</secondary>
  18. </indexterm>
  19. <sect2 role="package">
  20. <title/>
  21. <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
  22. href="../chapter06/gcc.xml"
  23. xpointer="xpointer(/sect1/sect2[1]/para[1])"/>
  24. <segmentedlist>
  25. <segtitle>&buildtime;</segtitle>
  26. <segtitle>&diskspace;</segtitle>
  27. <seglistitem>
  28. <seg>&gcc-ch5p1-sbu;</seg>
  29. <seg>&gcc-ch5p1-du;</seg>
  30. </seglistitem>
  31. </segmentedlist>
  32. </sect2>
  33. <sect2 role="installation">
  34. <title>安装交叉工具链中的 GCC</title>
  35. <!--para>GCC now requires the GMP, MPFR and MPC packages. As these packages may
  36. not be included in your host distribution, they will be built with
  37. GCC. Unpack each package into the GCC source directory and rename the
  38. resulting directories so the GCC build procedures will automatically
  39. use them:</para-->
  40. <para>目前 GCC 依赖于 GMP、MPFR 和 MPC 这三个包。
  41. 由于宿主发行版未必包含它们,我们将它们和 GCC 一同构建。
  42. 将它们都解压到 GCC 源码目录中,并重命名解压出的目录,
  43. 这样 GCC 构建过程就能自动使用它们:</para>
  44. <note><!--para>There are frequent misunderstandings about this chapter. The
  45. procedures are the same as every other chapter as explained earlier (<xref
  46. linkend='buildinstr'/>). First extract the gcc tarball from the sources
  47. directory and then change to the directory created. Only then should you
  48. proceed with the instructions below.</para-->
  49. <para>对于本章内容有一些很常见的误解。该软件包的构建过程就像之前
  50. (<xref linkend='buildinstr'/>) 解释的那样,
  51. 首先解压 GCC 压缩包,切换到解压出的目录中,再执行下面的命令。
  52. </para>
  53. </note>
  54. <screen><userinput remap="pre">tar -xf ../mpfr-&mpfr-version;.tar.xz
  55. mv -v mpfr-&mpfr-version; mpfr
  56. tar -xf ../gmp-&gmp-version;.tar.xz
  57. mv -v gmp-&gmp-version; gmp
  58. tar -xf ../mpc-&mpc-version;.tar.gz
  59. mv -v mpc-&mpc-version; mpc</userinput></screen>
  60. <para>下面的命令修改 GCC 的默认动态链接器位置,以使用
  61. <filename class="directory">/tools</filename> 中的动态链接器。
  62. 同时,从 GCC 的头文件搜索路径中删除
  63. <filename class="directory">/usr/include</filename> 。执行:</para>
  64. <screen><userinput remap="pre">for file in gcc/config/{linux,i386/linux{,64}}.h
  65. do
  66. cp -uv $file{,.orig}
  67. sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&amp;@g' \
  68. -e 's@/usr@/tools@g' $file.orig &gt; $file
  69. echo '
  70. #undef STANDARD_STARTFILE_PREFIX_1
  71. #undef STANDARD_STARTFILE_PREFIX_2
  72. #define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
  73. #define STANDARD_STARTFILE_PREFIX_2 ""' &gt;&gt; $file
  74. touch $file.orig
  75. done</userinput></screen>
  76. <!--para>In case the above seems hard to follow, let's break it down a bit.
  77. First we copy the files <filename>gcc/config/linux.h</filename>,
  78. <filename>gcc/config/i386/linux.h</filename>, and
  79. <filename>gcc/config/i368/linux64.h</filename> to a file of
  80. the same name but with an added suffix of <quote>.orig</quote>. Then the
  81. first sed expression prepends <quote>/tools</quote> to every instance of
  82. <quote>/lib/ld</quote>, <quote>/lib64/ld</quote> or
  83. <quote>/lib32/ld</quote>, while the second one replaces hard-coded
  84. instances of <quote>/usr</quote>. Next, we add our define statements which
  85. alter the default startfile prefix to the end of the file. Note that the
  86. trailing <quote>/</quote> in <quote>/tools/lib/</quote> is required.
  87. Finally, we use <command>touch</command> to update the timestamp on the
  88. copied files. When used in conjunction with <command>cp -u</command>, this
  89. prevents unexpected changes to the original files in case the commands are
  90. inadvertently run twice.</para-->
  91. <para>如果上面的命令看上去难以理解,把它分开来看。首先我们复制
  92. <filename>gcc/config/linux.h</filename>、
  93. <filename>gcc/config/i386/linux.h</filename>、
  94. <filename>gcc/config/i386/linux64.h</filename>,
  95. 将它们备份为名称是原文件名加上 <quote>.orig</quote> 后缀的文件。
  96. 然后第一个 sed 表达式将所有 <quote>/lib/ld</quote>、
  97. <quote>/lib64/ld</quote> 或者 <quote>/lib32/ld</quote>
  98. 之前都加上 <quote>/tools</quote>,第二个将硬编码的
  99. <quote>/usr</quote> 替换掉。然后,我们在文件末尾增加自己的宏定义,
  100. 修改默认的启动文件 (startfile) 前缀。注意,在
  101. <quote>/tools/lib/</quote> 中,最后的 <quote>/</quote> 是必要的。
  102. 最后,使用 <command>touch</command> 命令更新被复制的文件的时间戳。
  103. 这与 <command>cp -u</command> 命令结合起来,
  104. 防止我们不小心把命令输入两次,从而对文件造成非预期的修改。</para>
  105. <!--para>Finally, on x86_64 hosts, set the default directory name for
  106. 64-bit libraries to <quote>lib</quote>:</para-->
  107. <para>对于 x86_64 平台,还要设置存放 64 位库的默认目录为
  108. <quote>lib</quote>:</para>
  109. <screen><userinput remap="pre">case $(uname -m) in
  110. x86_64)
  111. sed -e '/m64=/s/lib64/lib/' \
  112. -i.orig gcc/config/i386/t-linux64
  113. ;;
  114. esac</userinput></screen>
  115. <!--
  116. <para>GCC doesn't detect stack protection correctly, which causes problems
  117. for the build of Glibc-&glibc-version;, so fix that by issuing the following
  118. command:</para>
  119. <screen><userinput remap="pre">sed -i '/k prot/agcc_cv_libc_provides_ssp=yes' gcc/configure</userinput></screen>
  120. -->
  121. <!--
  122. <para>Also fix a problem identified upstream:</para>
  123. <screen><userinput remap="pre">sed -i 's/if \((code.*))\)/if (\1 \&amp;\&amp; \!DEBUG_INSN_P (insn))/' gcc/sched-deps.c</userinput></screen>
  124. -->
  125. <!--para>The GCC documentation recommends building GCC
  126. in a dedicated build directory:</para-->
  127. <para>GCC 文档建议在一个专用目录中构建 GCC:</para>
  128. <screen><userinput remap="pre">mkdir -v build
  129. cd build</userinput></screen>
  130. <para>准备编译 GCC:</para>
  131. <screen><userinput remap="configure">../configure \
  132. --target=$LFS_TGT \
  133. --prefix=/tools \
  134. --with-glibc-version=2.11 \
  135. --with-sysroot=$LFS \
  136. --with-newlib \
  137. --without-headers \
  138. --with-local-prefix=/tools \
  139. --with-native-system-header-dir=/tools/include \
  140. --disable-nls \
  141. --disable-shared \
  142. --disable-multilib \
  143. --disable-decimal-float \
  144. --disable-threads \
  145. --disable-libatomic \
  146. --disable-libgomp \
  147. --disable-libquadmath \
  148. --disable-libssp \
  149. --disable-libvtv \
  150. --disable-libstdcxx \
  151. --enable-languages=c,c++</userinput></screen>
  152. <variablelist>
  153. <title>配置选项的含义:</title>
  154. <varlistentry>
  155. <term><parameter>--with-newlib</parameter></term>
  156. <listitem>
  157. <!--para>Since a working C library is not yet available, this ensures
  158. that the inhibit_libc constant is defined when building libgcc. This prevents
  159. the compiling of any code that requires libc support.</para-->
  160. <para>由于现在没有可用的 C 运行库,使用该选项保证构建 libgcc
  161. 时 inhibit_libc 常量被定义,
  162. 以防止编译任何需要 libc 支持的代码。</para>
  163. <note><title>译注</title>
  164. <para>这个选项对于嵌入式系统工程师来说显得很奇怪,
  165. 因为 LFS 和嵌入式系统中常用的 C 运行库 newlib 毫无关系。
  166. 参见译者和 GCC 开发者的讨论:
  167. <ulink url="https://gcc.gnu.org/ml/gcc-help/2017-04/msg00029.html"/>。
  168. </para></note>
  169. </listitem>
  170. </varlistentry>
  171. <varlistentry>
  172. <term><parameter>--without-headers</parameter></term>
  173. <listitem>
  174. <!--para>When creating a complete cross-compiler, GCC requires
  175. standard headers compatible with the target system. For our
  176. purposes these headers will not be needed. This switch prevents
  177. GCC from looking for them.</para-->
  178. <para>在创建完整的交叉编译器时,
  179. GCC 需要与目标系统兼容的标准头文件。
  180. 由于我们的特殊目的,这些头文件并不必要。
  181. 这个开关防止 GCC 查找它们。</para>
  182. </listitem>
  183. </varlistentry>
  184. <varlistentry>
  185. <term><parameter>--with-local-prefix=/tools</parameter></term>
  186. <listitem>
  187. <!--para>The local prefix is the location in the system that GCC will search
  188. for locally installed include files. The default is <filename>/usr/local</filename>.
  189. Setting this to <filename>/tools</filename> helps keep the host location of
  190. <filename>/usr/local</filename> out of this GCC's search path.</para-->
  191. <para>本地前缀是 GCC 在系统中寻找本地安装的包含文件的位置,
  192. 它的默认值是
  193. <filename class="directory">/usr/local</filename> 。
  194. 将它设置为 <filename class="directory">/tools</filename>,
  195. 从而将宿主系统的
  196. <filename class="directory">/usr/local</filename>
  197. 排除在 GCC 搜索路径外。</para>
  198. </listitem>
  199. </varlistentry>
  200. <varlistentry>
  201. <term><parameter>--with-native-system-header-dir=/tools/include</parameter></term>
  202. <listitem>
  203. <!--para>By default GCC searches <filename>/usr/include</filename> for
  204. system headers. In conjunction with the sysroot switch, this would
  205. normally translate to <filename>$LFS/usr/include</filename>. However
  206. the headers that will be installed in the next two sections will go
  207. to <filename>$LFS/tools/include</filename>. This switch ensures that
  208. gcc will find them correctly. In the second pass of GCC, this same
  209. switch will ensure that no headers from the host system are
  210. found.</para-->
  211. <para>默认情况下,GCC 在
  212. <filename class="directory">/usr/include</filename>
  213. 中搜索系统头文件。由于我们使用了 sysroot 开关,
  214. 它会被改写成
  215. <filename class="directory">$LFS/usr/include</filename>。
  216. 然而,在之后的两节中,我们会把头文件安装在
  217. <filename class="directory">$LFS/tools/include</filename>,
  218. 本开关保证 GCC 能够正确地找到它们。
  219. 在第二遍编译 GCC 时,这个开关保证不会查找宿主系统的头文件。
  220. </para>
  221. </listitem>
  222. </varlistentry>
  223. <varlistentry>
  224. <term><parameter>--disable-shared</parameter></term>
  225. <listitem>
  226. <!--para>This switch forces GCC to link its internal libraries
  227. statically. We do this to avoid possible issues with the host
  228. system.</para-->
  229. <para>这个开关强制 GCC 静态链接它的内部库,
  230. 以防止宿主系统可能带来的问题。</para>
  231. </listitem>
  232. </varlistentry>
  233. <varlistentry>
  234. <term><parameter>--disable-decimal-float, --disable-threads,
  235. --disable-libatomic, --disable-libgomp, <!--- -disable-libmpx,-->
  236. --disable-libquadmath, --disable-libssp, --disable-libvtv,
  237. --disable-libstdcxx</parameter></term>
  238. <listitem>
  239. <!--para>These switches disable support for the decimal floating point
  240. extension, threading, libatomic, libgomp, <!- -libmpx, - -> libquadmath, libssp,
  241. libvtv, and the C++ standard library respectively. These features
  242. will fail to compile when building a cross-compiler and are not
  243. necessary for the task of cross-compiling the temporary libc.</para-->
  244. <para>这些开关禁用对于十进制浮点数、线程、libatomic、libgomp、
  245. libquadmath、libssp、libvtv 和 C++ 标准库的支持。
  246. 在构建交叉编译器时它们的编译会失败,而且在交叉编译临时 libc
  247. 时并不需要它们。</para>
  248. </listitem>
  249. </varlistentry>
  250. <varlistentry>
  251. <term><parameter>--disable-multilib</parameter></term>
  252. <listitem>
  253. <!--para>On x86_64, LFS does not yet support a multilib configuration.
  254. This switch is harmless for x86.</para-->
  255. <para>在 x86_64 平台上,LFS 目前不支持 multilib 配置。
  256. 这个开关对于 x86 来说可有可无。</para>
  257. </listitem>
  258. </varlistentry>
  259. <varlistentry>
  260. <term><parameter>--enable-languages=c,c++</parameter></term>
  261. <listitem>
  262. <!--para>This option ensures that only the C and C++ compilers are built.
  263. These are the only languages needed now.</para-->
  264. <para>这个选项保证只构建 C 和 C++ 编译器。目前只需要这两个语言。
  265. </para>
  266. </listitem>
  267. </varlistentry>
  268. </variablelist>
  269. <para>执行以下命令编译 GCC:</para>
  270. <screen><userinput remap="make">make</userinput></screen>
  271. <!--para>Compilation is now complete. At this point, the test suite would
  272. normally be run, but, as mentioned before, the test suite framework is
  273. not in place yet. The benefits of running the tests at this point
  274. are minimal since the programs from this first pass will soon be
  275. replaced.</para-->
  276. <para>现在我们编译完成了 GCC。此时,通常来说应该执行测试套件。
  277. 然而正如前文所述,测试套件框架还没有就位,
  278. 而且第一遍编译的 GCC 程序很快就会被替换,运行测试套件的收益极小。
  279. </para>
  280. <para>安装该软件包:</para>
  281. <screen><userinput remap="install">make install</userinput></screen>
  282. <!--
  283. <para>Using <parameter>- -disable-shared</parameter> means that the
  284. <filename>libgcc_eh.a</filename> file isn't created and installed. The
  285. Glibc package depends on this library as it uses
  286. <parameter>-lgcc_eh</parameter> within its build system. This dependency
  287. can be satisfied by creating a symlink to <filename>libgcc.a</filename>,
  288. since that file will end up containing the objects normally contained in
  289. <filename>libgcc_eh.a</filename>:</para>
  290. <screen><userinput remap="install">ln -sv libgcc.a `$LFS_TGT-gcc -print-libgcc-file-name | sed 's/libgcc/&amp;_eh/'`</userinput></screen>
  291. -->
  292. </sect2>
  293. <sect2 role="content">
  294. <title/>
  295. <para>该软件包的更多细节在
  296. <xref linkend="contents-gcc"/> 中可以找到。</para>
  297. </sect2>
  298. </sect1>