1
0

gcc-pass1.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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</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-libmpx \
  148. --disable-libquadmath \
  149. --disable-libssp \
  150. --disable-libvtv \
  151. --disable-libstdcxx \
  152. --enable-languages=c,c++</userinput></screen>
  153. <variablelist>
  154. <title>配置选项的含义:</title>
  155. <varlistentry>
  156. <term><parameter>--with-newlib</parameter></term>
  157. <listitem>
  158. <!--para>Since a working C library is not yet available, this ensures
  159. that the inhibit_libc constant is defined when building libgcc. This prevents
  160. the compiling of any code that requires libc support.</para-->
  161. <para>由于现在没有可用的 C 运行库,使用该选项保证构建 libgcc
  162. 时 inhibit_libc 常量被定义,
  163. 以防止编译任何需要 libc 支持的代码。</para>
  164. <note><title>译注</title>
  165. <para>这个选项对于嵌入式系统工程师来说显得很奇怪,
  166. 因为 LFS 和嵌入式系统中常用的 C 运行库 newlib 毫无关系。
  167. 参见译者和 GCC 开发者的讨论:
  168. <ulink url="https://gcc.gnu.org/ml/gcc-help/2017-04/msg00029.html"/>。
  169. </para></note>
  170. </listitem>
  171. </varlistentry>
  172. <varlistentry>
  173. <term><parameter>--without-headers</parameter></term>
  174. <listitem>
  175. <!--para>When creating a complete cross-compiler, GCC requires
  176. standard headers compatible with the target system. For our
  177. purposes these headers will not be needed. This switch prevents
  178. GCC from looking for them.</para-->
  179. <para>在创建完整的交叉编译器时,
  180. GCC 需要与目标系统兼容的标准头文件。
  181. 由于我们的特殊目的,这些头文件并不必要。
  182. 这个开关防止 GCC 查找它们。</para>
  183. </listitem>
  184. </varlistentry>
  185. <varlistentry>
  186. <term><parameter>--with-local-prefix=/tools</parameter></term>
  187. <listitem>
  188. <!--para>The local prefix is the location in the system that GCC will search
  189. for locally installed include files. The default is <filename>/usr/local</filename>.
  190. Setting this to <filename>/tools</filename> helps keep the host location of
  191. <filename>/usr/local</filename> out of this GCC's search path.</para-->
  192. <para>本地前缀是 GCC 在系统中寻找本地安装的包含文件的位置,
  193. 它的默认值是
  194. <filename class="directory">/usr/local</filename> 。
  195. 将它设置为 <filename class="directory">/tools</filename>,
  196. 从而将宿主系统的
  197. <filename class="directory">/usr/local</filename>
  198. 排除在 GCC 搜索路径外。</para>
  199. </listitem>
  200. </varlistentry>
  201. <varlistentry>
  202. <term><parameter>--with-native-system-header-dir=/tools/include</parameter></term>
  203. <listitem>
  204. <!--para>By default GCC searches <filename>/usr/include</filename> for
  205. system headers. In conjunction with the sysroot switch, this would
  206. normally translate to <filename>$LFS/usr/include</filename>. However
  207. the headers that will be installed in the next two sections will go
  208. to <filename>$LFS/tools/include</filename>. This switch ensures that
  209. gcc will find them correctly. In the second pass of GCC, this same
  210. switch will ensure that no headers from the host system are
  211. found.</para-->
  212. <para>默认情况下,GCC 在
  213. <filename class="directory">/usr/include</filename>
  214. 中搜索系统头文件。由于我们使用了 sysroot 开关,
  215. 它会被改写成
  216. <filename class="directory">$LFS/usr/include</filename>。
  217. 然而,在之后的两节中,我们会把头文件安装在
  218. <filename class="directory">$LFS/tools/include</filename>,
  219. 本开关保证 GCC 能够正确地找到它们。
  220. 在第二遍编译 GCC 时,这个开关保证不会查找宿主系统的头文件。
  221. </para>
  222. </listitem>
  223. </varlistentry>
  224. <varlistentry>
  225. <term><parameter>--disable-shared</parameter></term>
  226. <listitem>
  227. <!--para>This switch forces GCC to link its internal libraries
  228. statically. We do this to avoid possible issues with the host
  229. system.</para-->
  230. <para>这个开关强制 GCC 静态链接它的内部库,
  231. 以防止宿主系统可能带来的问题。</para>
  232. </listitem>
  233. </varlistentry>
  234. <varlistentry>
  235. <term><parameter>--disable-decimal-float, --disable-threads,
  236. --disable-libatomic, --disable-libgomp, --disable-libmpx,
  237. --disable-libquadmath, --disable-libssp, --disable-libvtv,
  238. --disable-libstdcxx</parameter></term>
  239. <listitem>
  240. <!--para>These switches disable support for the decimal floating point
  241. extension, threading, libatomic, libgomp, libmpx, libquadmath, libssp,
  242. libvtv, and the C++ standard library respectively. These features
  243. will fail to compile when building a cross-compiler and are not
  244. necessary for the task of cross-compiling the temporary libc.</para-->
  245. <para>这些开关禁用对于十进制浮点数、线程、libatomic、libgomp、
  246. libmpx、libquadmath、libssp、libvtv 和 C++ 标准库的支持。
  247. 在构建交叉编译器时它们的编译会失败,而且在交叉编译临时 libc
  248. 时并不需要它们。</para>
  249. </listitem>
  250. </varlistentry>
  251. <varlistentry>
  252. <term><parameter>--disable-multilib</parameter></term>
  253. <listitem>
  254. <!--para>On x86_64, LFS does not yet support a multilib configuration.
  255. This switch is harmless for x86.</para-->
  256. <para>在 x86_64 平台上,LFS 目前不支持 multilib 配置。
  257. 这个开关对于 x86 来说可有可无。</para>
  258. </listitem>
  259. </varlistentry>
  260. <varlistentry>
  261. <term><parameter>--enable-languages=c,c++</parameter></term>
  262. <listitem>
  263. <!--para>This option ensures that only the C and C++ compilers are built.
  264. These are the only languages needed now.</para-->
  265. <para>这个选项保证只构建 C 和 C++ 编译器。目前只需要这两个语言。
  266. </para>
  267. </listitem>
  268. </varlistentry>
  269. </variablelist>
  270. <para>执行以下命令编译 GCC:</para>
  271. <screen><userinput remap="make">make</userinput></screen>
  272. <!--para>Compilation is now complete. At this point, the test suite would
  273. normally be run, but, as mentioned before, the test suite framework is
  274. not in place yet. The benefits of running the tests at this point
  275. are minimal since the programs from this first pass will soon be
  276. replaced.</para-->
  277. <para>现在我们编译完成了 GCC。此时,通常来说应该执行测试套件。
  278. 然而正如前文所述,测试套件框架还没有就位,
  279. 而且第一遍编译的 GCC 程序很快就会被替换,运行测试套件的收益极小。
  280. </para>
  281. <para>安装该软件包:</para>
  282. <screen><userinput remap="install">make install</userinput></screen>
  283. <!--
  284. <para>Using <parameter>- -disable-shared</parameter> means that the
  285. <filename>libgcc_eh.a</filename> file isn't created and installed. The
  286. Glibc package depends on this library as it uses
  287. <parameter>-lgcc_eh</parameter> within its build system. This dependency
  288. can be satisfied by creating a symlink to <filename>libgcc.a</filename>,
  289. since that file will end up containing the objects normally contained in
  290. <filename>libgcc_eh.a</filename>:</para>
  291. <screen><userinput remap="install">ln -sv libgcc.a `$LFS_TGT-gcc -print-libgcc-file-name | sed 's/libgcc/&amp;_eh/'`</userinput></screen>
  292. -->
  293. </sect2>
  294. <sect2 role="content">
  295. <title/>
  296. <para>该软件包的更多细节在
  297. <xref linkend="contents-gcc"/> 中可以找到。</para>
  298. </sect2>
  299. </sect1>