2.5 make bootstrap: the three-stage native build

GCC 15.2.0. To run the commands: a GCC checkout, and an installed native compiler to build it with.

You configure GCC for the machine you are on and type make. Some hours later one of two things happens. The build stops with Bootstrap comparison failure! and a list of object files (Makefile.in:65455-65458). Or the build succeeds, and the build directory holds stage1-gcc, prev-gcc and gcc, but no stage2-gcc and no stage3-gcc.

Both outcomes come from one piece of machinery. A native make builds GCC three times, each time with the compiler from the build before, and then compares the object files of the last two builds byte for byte.

For a native compiler, a bootstrap is this build of GCC with itself, which checks that the compiler compiles itself the same way twice. Part III uses the same word for an unrelated job, breaking the circular dependency between GCC and the C library for a cross target, and Chapter 3.1 starts that meaning. Part III also has its own "stage 1", a cross compiler built before any C library exists, which is not the stage 1 of a native bootstrap.

This chapter assumes Chapter 1.2, for build, host and target and the uninstalled xgcc. It also assumes Chapter 2.3, for Makefile.def, Makefile.tpl, the @if blocks that configure prunes, and the host and target modules that the top-level Makefile builds. It uses cc1 from Chapter 1.1, -B from Chapter 1.5, and the IR and the .gnu.lto_ sections of Chapter 1.13. From Chapter 2.2 it uses --enable-languages and target_noncanonical.

The install manual documents the procedure in its Building a native compiler section: build the tools and three stages, compare stage 2 with stage 3, and build the runtime libraries with the stage 3 compiler. The same section documents two controls. --disable-bootstrap turns the stages off, so a native make builds GCC once. make bootstrap-lean runs the same sequence but deletes the object files of stages 1 and 2 as soon as they are no longer needed, to save disk space. The manual gives the steps, and the top-level configure.ac, Makefile.def and Makefile.tpl hold the rules that carry them out.

--enable-bootstrap: configure decides from the triples

Unless you pass --enable-bootstrap or --disable-bootstrap, the top-level configure decides whether the build bootstraps. It says yes only when the tree has a compiler and the configuration is native, with host and target both equal to build. Every stage rule in the top-level Makefile depends on that answer. Watch the yes:$build:$build:default case:

case "$have_compiler:$host:$target:$enable_bootstrap" in
  *:*:*:no) ;;

  # Default behavior.  Enable bootstrap if we have a compiler
  # and we are in a native configuration.
  yes:$build:$build:default)
    enable_bootstrap=yes ;;

  *:*:*:default)
    enable_bootstrap=no ;;
  ...
  # Other configurations, but we have a compiler.  Assume the user knows
  # what he's doing.
  yes:*:*:yes)
    AC_MSG_WARN([trying to bootstrap a cross compiler])
    ;;

Listing 2.5-1: the bootstrap decision, abridged (configure.ac:1532-1551).

have_compiler is yes when the tree has a gcc/configure (configure.ac:1526-1530). If you pass --enable-bootstrap on a cross configuration, configure prints a warning and goes ahead.

The answer becomes one word, bootstrap or no-bootstrap (configure.ac:3180-3189). Makefile.in wraps the stage rules in @if gcc-bootstrap lines, and a sed script in configure deletes the markers that match the configuration and keeps what they enclose (configure.ac:3364-3384). The script then deletes every block still marked (:3409-3413), so in a --disable-bootstrap tree the bootstrap and compare targets do not exist (Makefile.tpl:1785-1874).

Two more decisions follow from enable_bootstrap=yes. C++ joins --enable-languages even if you left it out (configure.ac:2248-2258), because GCC is written in C++. The install manual's Prerequisites page lists a C++14 compiler as "Necessary to bootstrap GCC" (install.texi:225-226). The other decision makes --disable-libstdcxx a configure error (configure.ac:3340-3346).

bootstrap_stage in Makefile.def: four stages, two comparisons

Each stage of a bootstrap is a bootstrap_stage record in Makefile.def, and AutoGen turns every record into the make rules for that stage. Four numbered records hold the native sequence. From stage 2 on, each record names the stage that builds it, and the records for stages 3 and 4 also name a comparison with the stage before:

// Toplevel bootstrap
bootstrap_stage = { id=1 ; };
bootstrap_stage = {
	id=2 ; prev=1 ;
	bootstrap_target=bootstrap2 ; };
bootstrap_stage = {
	id=3 ; prev=2 ; lean=1 ;
	compare_target=compare ;
	bootstrap_target=bootstrap ;
	cleanstrap_target=cleanstrap ; };
bootstrap_stage = {
	id=4 ; prev=3 ; lean=2 ;
	compare_target=compare3 ;
	bootstrap_target=bootstrap4 ; };

Listing 2.5-2: the numbered stages (Makefile.def:738-751). Records for the profiled bootstraps follow at :752-765.

The loop in Makefile.tpl that expands the records writes a family of rules, which for stage 3 are stage3-start, stage3-end, stage3-bubble, compare and distclean-stage3 (Makefile.tpl:1727-1876). prev names the stage whose compiler builds this one. compare_target asks for a comparison with that previous stage, so stage 3 gets compare and stage 4 gets compare3. bootstrap_target names the make target that stops at this stage, and lean names the stage that bootstrap-lean deletes when this one starts.

bootstrap=true: the modules that every stage rebuilds

A stage rebuilds only the modules that Makefile.def marks with bootstrap=true, not the whole tree, and configure decides which of their stage rules are live. Together the flag and configure decide which build directories get stage names, and so which objects make compare sees.

On the host side, bootstrap=true is on gcc, on the libraries it links, such as libcpp and libiberty, and on binutils when its sources sit in the tree. On the target side it is on libgcc, libstdc++-v3 and a few others (Makefile.def:49, :99, :115, :169-170, :183, install.texi:3061-3065).

For target libraries, configure starts from libgcc alone. It adds the libraries of each language that stage 1 builds, then libgomp whenever it is built, and three more under conditions. fixincludes has bootstrap=true but is built once by default (configure.ac:2214, :2545-2554, :3240, :3242-3271, :3374-3377).

Plain make is make bootstrap: stage_final and stage3-bubble

With bootstrap enabled, plain make and make bootstrap do the same work: both run the bubble rule stage3-bubble, which rebuilds every stage up to stage 3, in order. The bubble rules also decide what a second make rebuilds after you edit a source file. The default target starts like this:

all:
@if gcc-bootstrap
	[ -f stage_final ] || echo stage3 > stage_final
	@r=`${PWD_COMMAND}`; export r; \
	s=`cd $(srcdir); ${PWD_COMMAND}`; export s; \
	$(MAKE) $(RECURSE_FLAGS_TO_PASS) `cat stage_final`-bubble
@endif gcc-bootstrap
	@: $(MAKE); $(unstage)

Listing 2.5-3: the default target (Makefile.tpl:860-867).

The default target writes stage3 to stage_final unless that file exists, and runs stage3-bubble. make bootstrap writes the same file and runs the same target (Makefile.in:65471-65480). make bootstrap4 writes stage4 instead, and a later plain make reads it back. Makefile.in holds the bubble rule for stage 3 in its generated form:

stage3-bubble:: stage2-bubble
	@r=`${PWD_COMMAND}`; export r; \
	s=`cd $(srcdir); ${PWD_COMMAND}`; export s; \
	if test -f stage3-lean || test -f stage2-lean ; then \
	  echo Skipping rebuild of stage3; \
	else \
	  $(MAKE) stage3-start; \
	  if $(LEAN); then \
	    rm -rf stage1-*; \
	    $(STAMP) stage1-lean; \
	  fi; \
	  $(MAKE) $(RECURSE_FLAGS_TO_PASS) all-stage3; \
	fi
	$(MAKE) $(RECURSE_FLAGS_TO_PASS) compare

Listing 2.5-4: stage 3, as generated (Makefile.in:65407-65420).

stage3-bubble depends on stage2-bubble, which depends on stage1-bubble. Each one moves its stage into place and runs make in every bootstrapped module, and only stage 3 then runs compare. A stage that already has a Makefile is not configured again (Makefile.in:12166), so if you edit a source file and run make again, the change goes through all three stages. The internals manual calls this "bubbling" (makefile.texi:95-106).

After the bubble, the default target runs $(unstage), which moves the final stage into gcc, and then all-host all-target (Makefile.tpl:867-873). Bootstrapped modules are left out of those two lists (:897-908). What remains is built once, by the stage 3 compiler: target libraries such as libgfortran and libquadmath (Makefile.def:187-188), and host tools such as gnattools (:159).

stage2-start and prev-gcc: each stage is built under the name gcc

Every stage is built in a directory named gcc, and from stage 2 on, the stage that compiles it sits in prev-gcc. Between builds a stage keeps its own name, such as stage2-gcc, and the -start rule of each stage renames the directories before the build. The renames explain why a finished build has no stage2-gcc and no stage3-gcc. In stage2-start, watch the two mv commands in each block:

stage2-start::
	@: $(MAKE); $(stage); \
	echo stage2 > stage_current; \
	echo stage2 > stage_last; \
	$(SHELL) $(srcdir)/mkinstalldirs $(HOST_SUBDIR)
...
@if gcc
	@cd $(HOST_SUBDIR); [ -d stage2-gcc ] || \
	  mkdir stage2-gcc; \
	mv stage2-gcc gcc; \
	mv stage1-gcc prev-gcc || test -f stage1-lean 
@endif gcc
...
	@[ -d stage2-$(TARGET_SUBDIR) ] || \
	  mkdir stage2-$(TARGET_SUBDIR); \
	mv stage2-$(TARGET_SUBDIR) $(TARGET_SUBDIR); \
	mv stage1-$(TARGET_SUBDIR) prev-$(TARGET_SUBDIR) || test -f stage1-lean 

Listing 2.5-5: the directory swap, abridged to gcc and the target directory (Makefile.in:64708-64866).

Every bootstrapped module gets the same pair of renames, and so does the target subdirectory, which is named after target_noncanonical (config/acx.m4:101). HOST_SUBDIR is . unless you build inside the source directory (config/acx.m4:93-99). stage2-end reverses the renames (Makefile.in:64899-64904). The template explains why the names stay constant:

# Since the 'compare' process will fail (on debugging information) if any
# directory names are different, we need to link the gcc directory for
# the previous stage to a constant name ('prev-gcc'), and to make the name of
# the build directories constant as well. For the latter, we use naked names
# like 'gcc', because the scripts in that directory assume it.  We use
# mv on platforms where symlinks to directories do not work or are not
# reliable.

Listing 2.5-6: why every stage is built under the same name (Makefile.tpl:1713-1719).

Two one-line files record the state. stage_current names the stage that is in gcc now, and stage_last names the stage that was started last (Makefile.tpl:1681-1683). The macro $(stage) runs the -end rule of the current stage, and $(unstage) runs the -start rule of the last stage if no stage is current (:1695-1699). Ordinary targets begin with $(unstage), install among them (Makefile.tpl:1062-1063), so install finds the final stage under the plain names.

The two macros produce the directory names of a finished build. compare runs $(stage) first, so stage 3 is back in stage3-gcc while it compares. Then make bootstrap runs $(unstage), which runs stage3-start, before it builds the remaining libraries. When make returns, gcc holds stage 3, prev-gcc holds stage 2, and only stage 1 keeps its own name.

If you run make stage, all three stages get their own names back, and make stage2-start brings stage 2 back into gcc. The internals manual documents both targets (makefile.texi:157-174).

POSTSTAGE1_HOST_EXPORTS: prev-gcc/xgcc compiles stages 2 and 3

From stage 2 on, the uninstalled driver of the previous stage, prev-gcc/xgcc, takes the place of the host's compiler, so stage 2 is compiled by stage 1 and stage 3 by stage 2. The same substitution decides where each stage finds its cc1 and its C++ library. POSTSTAGE1_HOST_EXPORTS puts the driver in CC:

# Similar, for later GCC stages.
POSTSTAGE1_HOST_EXPORTS = \
	$(HOST_EXPORTS) \
	CC="$(STAGE_CC_WRAPPER) $$r/$(HOST_SUBDIR)/prev-gcc/xgcc$(exeext) \
	  -B$$r/$(HOST_SUBDIR)/prev-gcc/ -B$(build_tooldir)/bin/ \
	  $(XGCC_FLAGS_FOR_TARGET) $$TFLAGS"; export CC; \
	CC_FOR_BUILD="$$CC"; export CC_FOR_BUILD; \

Listing 2.5-7: stage 2 and stage 3 are compiled by the previous stage's xgcc (Makefile.tpl:273-279).

-B points xgcc at that stage's cc1 and its other programs. CXX gets the same treatment with prev-gcc/xg++, plus -nostdinc++ and the headers and libraries of the libstdc++ that the previous stage built in prev-$(TARGET_SUBDIR) (Makefile.tpl:254-271). That is why libstdc++-v3 is always bootstrapped: stage 2 is C++ code, and it compiles against stage 1's library, not against the host's.

Each stage is also configured on its own, with these variables set (Makefile.in:12160-12187). The internals manual says why: "to minimize problems due to ABI incompatibilities between the native compiler and GCC" (makefile.texi:91-93).

STAGE1_CFLAGS and BOOT_CFLAGS: the flags and languages of each stage

Stage 1 is compiled with -g and no optimisation, and stages 2 and 3 with BOOT_CFLAGS, which is -g -O2. Each flag variable is named after the stage it builds, not after the compiler that receives it, so STAGE2_CFLAGS are the flags that the stage 1 compiler receives while it compiles stage 2. Stage 1 also differs in its checking and in the languages it builds:

StageCompiled bySTAGEN_CFLAGS by defaultLanguages
1the host's $CC and $CXX-gC, C++ and LTO, plus Ada and D when enabled
2stage 1, from prev-gcc-g -O2 -fno-checking, plus -gtoggle from bootstrap-debugall enabled languages
3stage 2, from prev-gcc-g -O2 -fchecking=1all enabled languages

Stage 1 gets its flags from configure (configure.ac:4153-4163). Stages 2 and 3 start from BOOT_CFLAGS (Makefile.tpl:398-400, :514-528). The install manual documents BOOT_CFLAGS under Building a native compiler, with the override make BOOT_CFLAGS='-O' bootstrap. No comment says why stage 1 is not optimised. Its object code is never compared, and the bootstrap-debug fragment calls it "the unoptimized stage1" (Listing 2.5-10).

Stage 1 is configured with internal checking, --enable-checking=yes,types for a release (configure.ac:4165-4188). The install manual documents --enable-stage1-checking, which replaces that default, under Configuration. The -fchecking options switch verification of the IR on or off stage by stage, and the GCC manual documents them under Developer Options. The comment above the settings calls it IL verification:

# When using the slow stage1 compiler disable IL verification and forcefully
# enable it when using the stage2 compiler instead.  As we later compare
# stage2 and stage3 we are merely avoid doing redundant work, plus we apply
# checking when building all target libraries for release builds.
STAGE1_TFLAGS += -fno-checking
STAGE2_CFLAGS += -fno-checking
STAGE2_TFLAGS += -fno-checking
STAGE3_CFLAGS += -fchecking=1
STAGE3_TFLAGS += -fchecking=1

Listing 2.5-8: IR verification by stage (Makefile.tpl:550-558).

Only stage 1 has to cope with an older compiler. Its configure gets --disable-coverage, and also --disable-build-format-warnings, because "older GCCs don't understand newer %s" (Makefile.tpl:537-543). If the host compiler is GCC, the top-level configure adds -std=c++14 to it, so that a C++14 compiler is enough to start (configure.ac:1563-1567). Before GCC 15 a C++11 compiler was enough, and before GCC 4.8 a C89 compiler (install.texi:229-232).

Stage 1 also builds only the languages that stage 2 needs. C++ is always on that list (configure.ac:2364-2366), and LTO is on it when LTO is enabled (gcc/lto/config-lang.in:32-33). Ada and D join it when you enable them, because "the Ada front end is written in Ada" and "the D front end is written in D" (gcc/ada/gcc-interface/config-lang.in:30, gcc/d/config-lang.in:27, install.texi:265-272, :322-326).

What make compare compares

make compare compares each .o file of stage 3 with the same file from stage 2, and records a difference in .bad_compare unless the file is excluded. What the loop skips decides what a successful bootstrap proves, and which files a Bootstrap comparison failure! can name. Watch the find that picks the files, the continue for a file that stage 2 lacks, and the case on @compare_exclusions@:

	echo Comparing stages 2 and 3; \
        sed=`echo stage3 | sed 's,^stage,,;s,.,.,g'`; \
	files=`find stage3-* -name "*$(objext)" -print | \
		 sed -n s,^stage$$sed-,,p`; \
	for file in $${files} ${extra-compare}; do \
	  f1=$$r/stage2-$$file; f2=$$r/stage3-$$file; \
	  if test ! -f $$f1; then continue; fi; \
	  $(do-compare) > /dev/null 2>&1; \
	  if test $$? -eq 1; then \
	    case $$file in \
	      @compare_exclusions@) \
	        echo warning: $$file differs ;; \
	      *) \
	        echo $$file differs >> .bad_compare ;; \
	    esac; \
	  fi; \
	done; \
	if [ -f .bad_compare ]; then \
	  echo "Bootstrap comparison failure!"; \
	  cat .bad_compare; \
	  exit 1; \

Listing 2.5-9: the compare rule, as generated (Makefile.in:65438-65458).

The loop applies five rules:

  1. Only files that end in .o under a stage3-* directory count. objext is set to .o with the comment "We only want to compare .o files" (Makefile.tpl:1724-1725). Executables, archives and shared libraries are not compared, unless a build configuration (BUILD_CONFIG) adds one through extra-compare, as bootstrap-lto does for lto1 (config/bootstrap-lto.mk:19-20).
  2. A file that stage 2 does not have is skipped without a message.
  3. Only exit status 1 from do-compare counts as a difference. Any other status passes.
  4. A file that matches compare_exclusions gives a warning, not a failure.
  5. Every other difference goes into .bad_compare in the top build directory, and the rule fails.

The stage3-* pattern covers the bootstrapped modules, on the host side and the target side. Other target libraries are built once, by stage 3, after the comparison, so nothing compares them.

The comparison pairs stages 2 and 3, not 1 and 2, because the host compiler produced stage 1's objects, which differ from stage 2's for that reason alone. Stage 2's objects come from the stage 1 compiler and stage 3's from the stage 2 compiler, and those two compilers are the same source, built two ways. The internals manual states the test: the compiler "should produce the same object files regardless of how it itself was compiled" (makefile.texi:133-136). A third stage is the first point at which a correct GCC must produce two identical sets of objects.

do-compare, compare-debug and compare_exclusions: allowed differences

Stages 2 and 3 can differ in a few expected ways, and do-compare, compare-debug and compare_exclusions are the three places where the comparison allows for them. do-compare is the command that compares one pair of objects, compare-debug is the script that the default build configuration puts in its place, and compare_exclusions lists the files whose differences only warn.

The default do-compare: cmp past 16 bytes

configure sets do-compare to a cmp that ignores the first 16 bytes of each file. It uses cmp --ignore-initial=16, cmp f1 f2 16 16 or tail -c +17, whichever the build machine supports (config/acx.m4:468-490). The source does not say why 16. When bootstrap-debug is selected, as it is by default, its fragment replaces this command.

bootstrap-debug: stage 2 without -g, compared after strip

bootstrap-debug is the default BUILD_CONFIG, and it makes a bootstrap test two things: stage 3 must match stage 2, and -g must not change the code. To test the second, it builds stage 2 without debug information and compares stripped copies of the objects.

configure selects BUILD_CONFIG=bootstrap-debug whenever it enables bootstrap (configure.ac:3180-3184), then tests the host compiler. It compiles int f (void) { return 0; } with and without -g, runs contrib/compare-debug on the two objects, and empties BUILD_CONFIG if they differ (configure.ac:3204-3216). --with-build-config overrides both (:3193-3202), and the top-level Makefile includes config/NAME.mk for each name (Makefile.tpl:834-839). The install manual documents BUILD_CONFIG and --with-build-config, and lists the fragments, under Building a native compiler. The default fragment sets two variables:

# This BUILD_CONFIG option builds checks that toggling debug
# information generation doesn't affect the generated object code.

# It is very lightweight: in addition to not performing any additional
# compilation (unlike bootstrap-debug-lean), it actually speeds up
# stage2, for no debug information is generated when compiling with
# the unoptimized stage1.

# For more thorough testing, see bootstrap-debug-lean.mk

STAGE2_CFLAGS += -gtoggle
do-compare = $(SHELL) $(srcdir)/contrib/compare-debug $$f1 $$f2

Listing 2.5-10: the default BUILD_CONFIG (config/bootstrap-debug.mk:1-12).

-gtoggle, another of the GCC manual's Developer Options, turns -g off for stage 2. For the comparison, compare-debug copies both objects and runs strip on the copies (contrib/compare-debug:60-74), and then compares them:

if cmp "$1.$suf1" "$2.$suf2"; then
  status=0
else
  status=1

  # Remove any .comment sections.
  if (objcopy -v) 2>&1 | grep ' --remove-section' > /dev/null \
     && (objdump --help) 2>&1 | grep ' --\[*section-\]*headers' > /dev/null; then
    remove_comment "$1.$suf1"
    remove_comment "$2.$suf2"
    if cmp "$1.$suf1" "$2.$suf2"; then
      status=0
    fi
  fi

Listing 2.5-11: .comment sections do not count (contrib/compare-debug:90-103).

If the files still differ, the script removes .eh_frame, the section where the assembler writes each function's unwind table (Chapter 4.3), and any .gnu.lto_ sections, and tries once more. It does this when only one file has .eh_frame, or when LTO sections are present. The comment explains the first case: "Assembler-generated CFI will add an .eh_frame section for -g not present in -g0" (contrib/compare-debug:105-179). Last, it compares any .gkd dump files next to the objects (:186-212). Under bootstrap-debug-big (config/bootstrap-debug-big.mk:6-7), another developer option, -fdump-final-insns, writes those files (invoke.texi:21352-21357).

Apart from what strip removes, compare-debug removes only .comment, .eh_frame and LTO sections, and it has no rule for timestamps or __DATE__ strings. bootstrap-lto takes the other approach and removes a source of variation at compile time. It compiles stages 2 and 3 with -frandom-seed=1 (config/bootstrap-lto.mk:3-4), the option that the GCC manual offers "to produce reproducibly identical object files" (invoke.texi:21284-21290). Its script, contrib/compare-lto, holds the only timestamp rule: "PE-COFF executables are timestamped so skip leading bytes for them" (contrib/compare-lto:105-120).

compare_exclusions and cc1-checksum.o

compare_exclusions is the case pattern in the comparison loop, so a file that matches it gives a warning when it differs and does not fail the build. configure builds the pattern line by line:

compare_exclusions="gcc/cc*-checksum\$(objext) | gcc/ada/*tools/*"
compare_exclusions="$compare_exclusions | gcc/m2/gm2-compiler-boot/M2Version*"
compare_exclusions="$compare_exclusions | gcc/m2/gm2-compiler-boot/SYSTEM*"
compare_exclusions="$compare_exclusions | gcc/m2/gm2version*"
compare_exclusions="$compare_exclusions | gcc/cobol/parse\$(objext)"

Listing 2.5-12: files that only warn (configure.ac:4215-4219). AIX adds libgomp objects at :4220-4223.

The source explains only the first pattern. cc1 embeds a checksum of its object files for precompiled-header validation (gcc/genchecksum.cc:1). In the final stage the build copies the previous stage's checksum source instead, and the comment says why: "re-use the checksum from the prev-final stage so it passes the bootstrap comparison and allows comparing of the cc1 binary" (gcc/c/Make-lang.in:71-83). cc1plus does the same (gcc/cp/Make-lang.in:129-141).

Cross compilers do not bootstrap: stage 2 would be target code

A cross compiler does not bootstrap by default, because stage 3 would need the build machine to run stage 2, and stage 2 is target code. From stage 2 on, CC is prev-gcc/xgcc (Listing 2.5-7), and that driver produces code for the target, so on a cross build stage 2's cc1 would be a target program. Running it on the build machine works only when that machine can run target code.

The install manual's Configuration page gives that case, an i686 host with an i486 target, and asks for an explicit --enable-bootstrap (install.texi:1947-1953). For every other cross, its Building a cross compiler section says: "it is not generally possible to do a 3-stage bootstrap" (install.texi:3247-3251). Listing 2.5-1 encodes that default.

A cross build therefore compiles GCC once, "single stage only", and builds the runtime libraries with that compiler (install.texi:3273-3276). Those libraries need the target's C library, which needs a compiler first, the cycle that Chapter 1.2 showed. Breaking that cycle is the other meaning of bootstrap, which Chapter 3.1 begins.

Checking a bootstrap: stage_current, make stage and .bad_compare

A bootstrap leaves its state in the build directory, where you can read it after make returns. stage_final and stage_current name stages, the directory names show which stage is where, and .bad_compare lists the objects that differed. A full native bootstrap takes hours, but one check needs only an installed compiler and a checkout, because it repeats the test by which configure picks the BUILD_CONFIG of Listing 2.5-10.

Untested recipe. The results described below come from the rules that the listings quote. The commands were not run.

The check compiles the function that configure uses, with and without -g, and runs compare-debug on the two objects:

$ echo 'int f (void) { return 0; }' > conftest.c
$ gcc -c conftest.c && mv conftest.o conftest.o.g0
$ gcc -c -g conftest.c && mv conftest.o conftest.o.g
$ sh ~/src/gcc/contrib/compare-debug conftest.o.g0 conftest.o.g; echo $?

Status 0 means that configure would select bootstrap-debug on this machine. The full run builds GCC in a build directory outside the source tree:

$ mkdir build && cd build
$ ~/src/gcc/configure --prefix="$HOME/opt/gcc-15" --enable-languages=c,c++
$ grep -E '^(BUILD_CONFIG|STAGE1_CFLAGS|STAGE1_CHECKING|STAGE1_LANGUAGES) =' Makefile
$ make -j"$(nproc)"
$ cat stage_final stage_current
$ ls -d stage*-* prev-* gcc
$ make stage && ls -d stage*-gcc

The grep works as soon as configure returns, and shows the values substituted into Makefile.tpl:534-536 and :835. After make, stage_final and stage_current both say stage3. The ls finds stage1-gcc, prev-gcc and gcc, and the same three names for each other bootstrapped module. After make stage, it finds stage1-gcc, stage2-gcc and stage3-gcc.

After a comparison failure the directories keep their stage names. compare-debug -p keeps the stripped copies for inspection (contrib/compare-debug:26-31):

$ cat .bad_compare
$ sh ~/src/gcc/contrib/compare-debug -p stage2-gcc/FILE.o stage3-gcc/FILE.o

Three things to carry away

A native make is make bootstrap. It builds three stages, each with the previous one, and compares the .o files of stages 2 and 3.

The stage being built is always in gcc, and its compiler in prev-gcc. The names stay constant because directory names end up in the objects being compared.

A cross compiler does not bootstrap by default. Stage 2 would be target code, and the build machine would have to run it.

Things that surprise people

After a successful make there is no stage2-gcc and no stage3-gcc. They are prev-gcc and gcc. Run make stage to rename them.

--enable-languages=c still builds C++ when it bootstraps. configure adds C++, because GCC is written in it.

The default bootstrap compiles stage 2 without debug information. bootstrap-debug adds -gtoggle, and the comparison strips both stages.

BUILD_CONFIG=bootstrap-lto-noplugin turns the comparison off. It sets do-compare = /bin/true (config/bootstrap-lto-noplugin.mk:9).

Most target libraries are never compared. Only the bootstrapped ones, such as libgcc and libstdc++, exist in a stage3-* directory.

Documentation coverage

The install manual covers the procedure in Building a native compiler (gcc/doc/install.texi:3044): the steps (:3054-3073), bootstrap-lean (:3075-3079), BOOT_CFLAGS (:3081-3101), what a failed comparison means (:3118-3123), and the BUILD_CONFIG names (:3133-3245). Configuration documents --disable-bootstrap and --enable-bootstrap (:1941, :1947), --enable-werror (:2253), --enable-stage1-checking (:2303), --enable-stage1-languages (:2117), and the stage link options (:2516-2536). The internals manual's Makefile Targets node (gcc/doc/makefile.texi:5) documents bubbling, stageN-start, stage, compare and distclean-stageN (:86-205). -gtoggle is in Developer Options (gcc/doc/invoke.texi:21402).

What is not documented:

  • That STAGE1_CFLAGS defaults to -g with no -O, and why.
  • The -fno-checking and -fchecking=1 flags that each stage receives.
  • prev-gcc, and the reason that each stage is built under a constant name.
  • That after make, gcc holds stage 3 and prev-gcc holds stage 2.
  • What compare skips: files other than .o, files that stage 2 lacks, compare_exclusions, and every target library that is not bootstrapped.
  • The default 16-byte skip, and what compare-debug removes: .comment, .eh_frame and LTO sections.
  • Why a third stage is needed. The internals manual states the test (makefile.texi:133-136). The install manual only names the step.

Two statements in the internals manual are out of date. It says the first stage builds "typically, C and maybe Ada" (makefile.texi:182-183). In GCC 15.2.0 it always builds C and C++, and LTO by default. It also says each stage is "copied to its subdirectory" (:96-98). The rules rename the directories with mv.

The install manual says that, with no --enable-checking, stage 1 gets yes checking (install.texi:2307-2310). configure.ac gives yes,types for a release, yes,types,extra for an experimental tree, and a longer list for an LTO BUILD_CONFIG (configure.ac:4165-4188).

Next: Chapter 2.6, Where __LDBL_MANT_DIG__ comes from, how that macro and the other floating-point macros get their values, and which part of target selection fixes them.