build.html revision 1.16
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 1988-2022 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with no
8Invariant Sections, the Front-Cover texts being (a) (see below), and
9with the Back-Cover Texts being (b) (see below).  A copy of the
10license is included in the section entitled "GNU
11Free Documentation License".
12
13(a) The FSF's Front-Cover Text is:
14
15A GNU Manual
16
17(b) The FSF's Back-Cover Text is:
18
19You have freedom to copy and modify this GNU Manual, like GNU
20     software.  Copies published by the Free Software Foundation raise
21     funds for GNU development. -->
22<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
23<head>
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<title>Installing GCC: Building</title>
26
27<meta name="description" content="Installing GCC: Building">
28<meta name="keywords" content="Installing GCC: Building">
29<meta name="resource-type" content="document">
30<meta name="distribution" content="global">
31<meta name="Generator" content="makeinfo">
32<style type="text/css">
33<!--
34a.summary-letter {text-decoration: none}
35blockquote.indentedblock {margin-right: 0em}
36blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
37blockquote.smallquotation {font-size: smaller}
38div.display {margin-left: 3.2em}
39div.example {margin-left: 3.2em}
40div.lisp {margin-left: 3.2em}
41div.smalldisplay {margin-left: 3.2em}
42div.smallexample {margin-left: 3.2em}
43div.smalllisp {margin-left: 3.2em}
44kbd {font-style: oblique}
45pre.display {font-family: inherit}
46pre.format {font-family: inherit}
47pre.menu-comment {font-family: serif}
48pre.menu-preformatted {font-family: serif}
49pre.smalldisplay {font-family: inherit; font-size: smaller}
50pre.smallexample {font-size: smaller}
51pre.smallformat {font-family: inherit; font-size: smaller}
52pre.smalllisp {font-size: smaller}
53span.nolinebreak {white-space: nowrap}
54span.roman {font-family: initial; font-weight: normal}
55span.sansserif {font-family: sans-serif; font-weight: normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en">
64<h1 class="settitle" align="center">Installing GCC: Building</h1>
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85<a name="index-Installing-GCC_003a-Building"></a>
86
87<p>Now that GCC is configured, you are ready to build the compiler and
88runtime libraries.
89</p>
90<p>Some commands executed when making the compiler may fail (return a
91nonzero status) and be ignored by <code>make</code>.  These failures, which
92are often due to files that were not found, are expected, and can safely
93be ignored.
94</p>
95<p>It is normal to have compiler warnings when compiling certain files.
96Unless you are a GCC developer, you can generally ignore these warnings
97unless they cause compilation to fail.  Developers should attempt to fix
98any warnings encountered, however they can temporarily continue past
99warnings-as-errors by specifying the configure flag
100<samp>--disable-werror</samp>.
101</p>
102<p>On certain old systems, defining certain environment variables such as
103<code>CC</code> can interfere with the functioning of <code>make</code>.
104</p>
105<p>If you encounter seemingly strange errors when trying to build the
106compiler in a directory other than the source directory, it could be
107because you have previously configured the compiler in the source
108directory.  Make sure you have done all the necessary preparations.
109</p>
110<p>If you build GCC on a BSD system using a directory stored in an old System
111V file system, problems may occur in running <code>fixincludes</code> if the
112System V file system doesn&rsquo;t support symbolic links.  These problems
113result in a failure to fix the declaration of <code>size_t</code> in
114<samp>sys/types.h</samp>.  If you find that <code>size_t</code> is a signed type and
115that type mismatches occur, this could be the cause.
116</p>
117<p>The solution is not to use such a directory for building GCC.
118</p>
119<p>Similarly, when building from the source repository or snapshots, or if you modify
120<samp>*.l</samp> files, you need the Flex lexical analyzer generator
121installed.  If you do not modify <samp>*.l</samp> files, releases contain
122the Flex-generated files and you do not need Flex installed to build
123them.  There is still one Flex-based lexical analyzer (part of the
124build machinery, not of GCC itself) that is used even if you only
125build the C front end.
126</p>
127<p>When building from the source repository or snapshots, or if you modify Texinfo
128documentation, you need version 4.7 or later of Texinfo installed if you
129want Info documentation to be regenerated.  Releases contain Info
130documentation pre-built for the unmodified documentation in the release.
131</p>
132<a name="Building-a-native-compiler"></a>
133<h3 class="section">Building a native compiler</h3>
134
135<p>For a native build, the default configuration is to perform
136a 3-stage bootstrap of the compiler when &lsquo;<samp>make</samp>&rsquo; is invoked.
137This will build the entire GCC system and ensure that it compiles
138itself correctly.  It can be disabled with the <samp>--disable-bootstrap</samp>
139parameter to &lsquo;<samp>configure</samp>&rsquo;, but bootstrapping is suggested because
140the compiler will be tested more completely and could also have
141better performance.
142</p>
143<p>The bootstrapping process will complete the following steps:
144</p>
145<ul>
146<li> Build tools necessary to build the compiler.
147
148</li><li> Perform a 3-stage bootstrap of the compiler.  This includes building
149three times the target tools for use by the compiler such as binutils
150(bfd, binutils, gas, gprof, ld, and opcodes) if they have been
151individually linked or moved into the top level GCC source tree before
152configuring.
153
154</li><li> Perform a comparison test of the stage2 and stage3 compilers.
155
156</li><li> Build runtime libraries using the stage3 compiler from the previous step.
157
158</li></ul>
159
160<p>If you are short on disk space you might consider &lsquo;<samp>make
161bootstrap-lean</samp>&rsquo; instead.  The sequence of compilation is the
162same described above, but object files from the stage1 and
163stage2 of the 3-stage bootstrap of the compiler are deleted as
164soon as they are no longer needed.
165</p>
166<p>If you wish to use non-default GCC flags when compiling the stage2
167and stage3 compilers, set <code>BOOT_CFLAGS</code> on the command line when
168doing &lsquo;<samp>make</samp>&rsquo;.  For example, if you want to save additional space
169during the bootstrap and in the final installation as well, you can
170build the compiler binaries without debugging information as in the
171following example.  This will save roughly 40% of disk space both for
172the bootstrap and the final installation.  (Libraries will still contain
173debugging information.)
174</p>
175<div class="smallexample">
176<pre class="smallexample">make BOOT_CFLAGS='-O' bootstrap
177</pre></div>
178
179<p>You can place non-default optimization flags into <code>BOOT_CFLAGS</code>; they
180are less well tested here than the default of &lsquo;<samp>-g -O2</samp>&rsquo;, but should
181still work.  In a few cases, you may find that you need to specify special
182flags such as <samp>-msoft-float</samp> here to complete the bootstrap; or,
183if the native compiler miscompiles the stage1 compiler, you may need
184to work around this, by choosing <code>BOOT_CFLAGS</code> to avoid the parts
185of the stage1 compiler that were miscompiled, or by using &lsquo;<samp>make
186bootstrap4</samp>&rsquo; to increase the number of stages of bootstrap.
187</p>
188<p><code>BOOT_CFLAGS</code> does not apply to bootstrapped target libraries.
189Since these are always compiled with the compiler currently being
190bootstrapped, you can use <code>CFLAGS_FOR_TARGET</code> to modify their
191compilation flags, as for non-bootstrapped target libraries.
192Again, if the native compiler miscompiles the stage1 compiler, you may
193need to work around this by avoiding non-working parts of the stage1
194compiler.  Use <code>STAGE1_TFLAGS</code> to this end.
195</p>
196<p>If you used the flag <samp>--enable-languages=&hellip;</samp> to restrict
197the compilers to be built, only those you&rsquo;ve actually enabled will be
198built.  This will of course only build those runtime libraries, for
199which the particular compiler has been built.  Please note,
200that re-defining <code>LANGUAGES</code> when calling &lsquo;<samp>make</samp>&rsquo;
201<strong>does not</strong> work anymore!
202</p>
203<p>If the comparison of stage2 and stage3 fails, this normally indicates
204that the stage2 compiler has compiled GCC incorrectly, and is therefore
205a potentially serious bug which you should investigate and report.  (On
206a few systems, meaningful comparison of object files is impossible; they
207always appear &ldquo;different&rdquo;.  If you encounter this problem, you will
208need to disable comparison in the <samp>Makefile</samp>.)
209</p>
210<p>If you do not want to bootstrap your compiler, you can configure with
211<samp>--disable-bootstrap</samp>.  In particular cases, you may want to
212bootstrap your compiler even if the target system is not the same as
213the one you are building on: for example, you could build a
214<code>powerpc-unknown-linux-gnu</code> toolchain on a
215<code>powerpc64-unknown-linux-gnu</code> host.  In this case, pass
216<samp>--enable-bootstrap</samp> to the configure script.
217</p>
218<p><code>BUILD_CONFIG</code> can be used to bring in additional customization
219to the build.  It can be set to a whitespace-separated list of names.
220For each such <code>NAME</code>, top-level <samp>config/<code>NAME</code>.mk</samp> will
221be included by the top-level <samp>Makefile</samp>, bringing in any settings
222it contains.  The default <code>BUILD_CONFIG</code> can be set using the
223configure option <samp>--with-build-config=<code>NAME</code>...</samp>.  Some
224examples of supported build configurations are:
225</p>
226<dl compact="compact">
227<dt>&lsquo;<samp>bootstrap-O1</samp>&rsquo;</dt>
228<dd><p>Removes any <samp>-O</samp>-started option from <code>BOOT_CFLAGS</code>, and adds
229<samp>-O1</samp> to it.  &lsquo;<samp>BUILD_CONFIG=bootstrap-O1</samp>&rsquo; is equivalent to
230&lsquo;<samp>BOOT_CFLAGS='-g -O1'</samp>&rsquo;.
231</p>
232</dd>
233<dt>&lsquo;<samp>bootstrap-O3</samp>&rsquo;</dt>
234<dt>&lsquo;<samp>bootstrap-Og</samp>&rsquo;</dt>
235<dd><p>Analogous to <code>bootstrap-O1</code>.
236</p>
237</dd>
238<dt>&lsquo;<samp>bootstrap-lto</samp>&rsquo;</dt>
239<dd><p>Enables Link-Time Optimization for host tools during bootstrapping.
240&lsquo;<samp>BUILD_CONFIG=bootstrap-lto</samp>&rsquo; is equivalent to adding
241<samp>-flto</samp> to &lsquo;<samp>BOOT_CFLAGS</samp>&rsquo;.  This option assumes that the host
242supports the linker plugin (e.g. GNU ld version 2.21 or later or GNU gold
243version 2.21 or later).
244</p>
245</dd>
246<dt>&lsquo;<samp>bootstrap-lto-noplugin</samp>&rsquo;</dt>
247<dd><p>This option is similar to <code>bootstrap-lto</code>, but is intended for
248hosts that do not support the linker plugin.  Without the linker plugin 
249static libraries are not compiled with link-time optimizations.  Since 
250the GCC middle end and back end are in <samp>libbackend.a</samp> this means
251that only the front end is actually LTO optimized.
252</p>
253</dd>
254<dt>&lsquo;<samp>bootstrap-lto-lean</samp>&rsquo;</dt>
255<dd><p>This option is similar to <code>bootstrap-lto</code>, but is intended for
256faster build by only using LTO in the final bootstrap stage.
257With &lsquo;<samp>make profiledbootstrap</samp>&rsquo; the LTO frontend
258is trained only on generator files.
259</p>
260</dd>
261<dt>&lsquo;<samp>bootstrap-debug</samp>&rsquo;</dt>
262<dd><p>Verifies that the compiler generates the same executable code, whether
263or not it is asked to emit debug information.  To this end, this
264option builds stage2 host programs without debug information, and uses
265<samp>contrib/compare-debug</samp> to compare them with the stripped stage3
266object files.  If <code>BOOT_CFLAGS</code> is overridden so as to not enable
267debug information, stage2 will have it, and stage3 won&rsquo;t.  This option
268is enabled by default when GCC bootstrapping is enabled, if
269<code>strip</code> can turn object files compiled with and without debug
270info into identical object files.  In addition to better test
271coverage, this option makes default bootstraps faster and leaner.
272</p>
273</dd>
274<dt>&lsquo;<samp>bootstrap-debug-big</samp>&rsquo;</dt>
275<dd><p>Rather than comparing stripped object files, as in
276<code>bootstrap-debug</code>, this option saves internal compiler dumps
277during stage2 and stage3 and compares them as well, which helps catch
278additional potential problems, but at a great cost in terms of disk
279space.  It can be specified in addition to &lsquo;<samp>bootstrap-debug</samp>&rsquo;.
280</p>
281</dd>
282<dt>&lsquo;<samp>bootstrap-debug-lean</samp>&rsquo;</dt>
283<dd><p>This option saves disk space compared with <code>bootstrap-debug-big</code>,
284but at the expense of some recompilation.  Instead of saving the dumps
285of stage2 and stage3 until the final compare, it uses
286<samp>-fcompare-debug</samp> to generate, compare and remove the dumps
287during stage3, repeating the compilation that already took place in
288stage2, whose dumps were not saved.
289</p>
290</dd>
291<dt>&lsquo;<samp>bootstrap-debug-lib</samp>&rsquo;</dt>
292<dd><p>This option tests executable code invariance over debug information
293generation on target libraries, just like <code>bootstrap-debug-lean</code>
294tests it on host programs.  It builds stage3 libraries with
295<samp>-fcompare-debug</samp>, and it can be used along with any of the
296<code>bootstrap-debug</code> options above.
297</p>
298<p>There aren&rsquo;t <code>-lean</code> or <code>-big</code> counterparts to this option
299because most libraries are only build in stage3, so bootstrap compares
300would not get significant coverage.  Moreover, the few libraries built
301in stage2 are used in stage3 host programs, so we wouldn&rsquo;t want to
302compile stage2 libraries with different options for comparison purposes.
303</p>
304</dd>
305<dt>&lsquo;<samp>bootstrap-debug-ckovw</samp>&rsquo;</dt>
306<dd><p>Arranges for error messages to be issued if the compiler built on any
307stage is run without the option <samp>-fcompare-debug</samp>.  This is
308useful to verify the full <samp>-fcompare-debug</samp> testing coverage.  It
309must be used along with <code>bootstrap-debug-lean</code> and
310<code>bootstrap-debug-lib</code>.
311</p>
312</dd>
313<dt>&lsquo;<samp>bootstrap-cet</samp>&rsquo;</dt>
314<dd><p>This option enables Intel CET for host tools during bootstrapping.
315&lsquo;<samp>BUILD_CONFIG=bootstrap-cet</samp>&rsquo; is equivalent to adding
316<samp>-fcf-protection</samp> to &lsquo;<samp>BOOT_CFLAGS</samp>&rsquo;.  This option
317assumes that the host supports Intel CET (e.g. GNU assembler version
3182.30 or later).
319</p>
320</dd>
321<dt>&lsquo;<samp>bootstrap-time</samp>&rsquo;</dt>
322<dd><p>Arranges for the run time of each program started by the GCC driver,
323built in any stage, to be logged to <samp>time.log</samp>, in the top level of
324the build tree.
325</p>
326</dd>
327<dt>&lsquo;<samp>bootstrap-asan</samp>&rsquo;</dt>
328<dd><p>Compiles GCC itself using Address Sanitization in order to catch invalid memory
329accesses within the GCC code.
330</p>
331</dd>
332<dt>&lsquo;<samp>bootstrap-hwasan</samp>&rsquo;</dt>
333<dd><p>Compiles GCC itself using HWAddress Sanitization in order to catch invalid
334memory accesses within the GCC code.  This option is only available on AArch64
335systems that are running Linux kernel version 5.4 or later.
336</p>
337</dd>
338</dl>
339
340<a name="Building-a-cross-compiler"></a>
341<h3 class="section">Building a cross compiler</h3>
342
343<p>When building a cross compiler, it is not generally possible to do a
3443-stage bootstrap of the compiler.  This makes for an interesting problem
345as parts of GCC can only be built with GCC.
346</p>
347<p>To build a cross compiler, we recommend first building and installing a
348native compiler.  You can then use the native GCC compiler to build the
349cross compiler.  The installed native compiler needs to be GCC version
3502.95 or later.
351</p>
352<p>Assuming you have already installed a native copy of GCC and configured
353your cross compiler, issue the command <code>make</code>, which performs the
354following steps:
355</p>
356<ul>
357<li> Build host tools necessary to build the compiler.
358
359</li><li> Build target tools for use by the compiler such as binutils (bfd,
360binutils, gas, gprof, ld, and opcodes)
361if they have been individually linked or moved into the top level GCC source
362tree before configuring.
363
364</li><li> Build the compiler (single stage only).
365
366</li><li> Build runtime libraries using the compiler from the previous step.
367</li></ul>
368
369<p>Note that if an error occurs in any step the make process will exit.
370</p>
371<p>If you are not building GNU binutils in the same source tree as GCC,
372you will need a cross-assembler and cross-linker installed before
373configuring GCC.  Put them in the directory
374<samp><var>prefix</var>/<var>target</var>/bin</samp>.  Here is a table of the tools
375you should put in this directory:
376</p>
377<dl compact="compact">
378<dt><samp>as</samp></dt>
379<dd><p>This should be the cross-assembler.
380</p>
381</dd>
382<dt><samp>ld</samp></dt>
383<dd><p>This should be the cross-linker.
384</p>
385</dd>
386<dt><samp>ar</samp></dt>
387<dd><p>This should be the cross-archiver: a program which can manipulate
388archive files (linker libraries) in the target machine&rsquo;s format.
389</p>
390</dd>
391<dt><samp>ranlib</samp></dt>
392<dd><p>This should be a program to construct a symbol table in an archive file.
393</p></dd>
394</dl>
395
396<p>The installation of GCC will find these programs in that directory,
397and copy or link them to the proper place to for the cross-compiler to
398find them when run later.
399</p>
400<p>The easiest way to provide these files is to build the Binutils package.
401Configure it with the same <samp>--host</samp> and <samp>--target</samp>
402options that you use for configuring GCC, then build and install
403them.  They install their executables automatically into the proper
404directory.  Alas, they do not support all the targets that GCC
405supports.
406</p>
407<p>If you are not building a C library in the same source tree as GCC,
408you should also provide the target libraries and headers before
409configuring GCC, specifying the directories with
410<samp>--with-sysroot</samp> or <samp>--with-headers</samp> and
411<samp>--with-libs</samp>.  Many targets also require &ldquo;start files&rdquo; such
412as <samp>crt0.o</samp> and
413<samp>crtn.o</samp> which are linked into each executable.  There may be several
414alternatives for <samp>crt0.o</samp>, for use with profiling or other
415compilation options.  Check your target&rsquo;s definition of
416<code>STARTFILE_SPEC</code> to find out what start files it uses.
417</p>
418<a name="Building-in-parallel"></a>
419<h3 class="section">Building in parallel</h3>
420
421<p>GNU Make 3.80 and above, which is necessary to build GCC, support
422building in parallel.  To activate this, you can use &lsquo;<samp>make -j 2</samp>&rsquo;
423instead of &lsquo;<samp>make</samp>&rsquo;.  You can also specify a bigger number, and
424in most cases using a value greater than the number of processors in
425your machine will result in fewer and shorter I/O latency hits, thus
426improving overall throughput; this is especially true for slow drives
427and network filesystems.
428</p>
429<a name="Building-the-Ada-compiler"></a>
430<h3 class="section">Building the Ada compiler</h3>
431
432<p><a href="prerequisites.html#GNAT-prerequisite">GNAT prerequisites</a>.
433</p>
434<a name="Building-the-D-compiler"></a>
435<h3 class="section">Building the D compiler</h3>
436
437<p><a href="prerequisites.html#GDC-prerequisite">GDC prerequisites</a>.
438</p>
439<a name="Building-with-profile-feedback"></a>
440<h3 class="section">Building with profile feedback</h3>
441
442<p>It is possible to use profile feedback to optimize the compiler itself.  This
443should result in a faster compiler binary.  Experiments done on x86 using gcc
4443.3 showed approximately 7 percent speedup on compiling C programs.  To
445bootstrap the compiler with profile feedback, use <code>make profiledbootstrap</code>.
446</p>
447<p>When &lsquo;<samp>make profiledbootstrap</samp>&rsquo; is run, it will first build a <code>stage1</code>
448compiler.  This compiler is used to build a <code>stageprofile</code> compiler
449instrumented to collect execution counts of instruction and branch
450probabilities.  Training run is done by building <code>stagetrain</code>
451compiler.  Finally a <code>stagefeedback</code> compiler is built
452using the information collected.
453</p>
454<p>Unlike standard bootstrap, several additional restrictions apply.  The
455compiler used to build <code>stage1</code> needs to support a 64-bit integral type.
456It is recommended to only use GCC for this.
457</p>
458<p>On Linux/x86_64 hosts with some restrictions (no virtualization) it is
459also possible to do autofdo build with &lsquo;<samp>make
460autoprofiledback</samp>&rsquo;. This uses Linux perf to sample branches in the
461binary and then rebuild it with feedback derived from the profile.
462Linux perf and the <code>autofdo</code> toolkit needs to be installed for
463this.
464</p>
465<p>Only the profile from the current build is used, so when an error
466occurs it is recommended to clean before restarting. Otherwise
467the code quality may be much worse.
468</p>
469<hr />
470<p>
471<p><a href="./index.html">Return to the GCC Installation page</a>
472</p>
473
474
475
476
477
478
479<hr>
480
481
482
483</body>
484</html>
485