Continuing from Bootstrapping on Windows (BOOTSTRAP-WINDOWS file), if you are
using your own MinGW distribution, then the resulting build2 binaries will most
likely require a number of DLLs in order to run. It is therefore recommended
that you copy the following files from your MinGW bin\ subdirectory to
C:\build2\bin\ (* in the last name will normally be dw2-1, seh-1, or sjlj-1):

libwinpthread-1.dll
libstdc++-6.dll
libgcc_s_*.dll

To build with MinGW you can either perform the following steps manually or, if
after reviewing the steps, you are happy with using the defaults, run the
build-mingw.bat batch file. It performs (and echoes) the same set of steps as
outlined below but only allows you to customize the compiler, installation
directory, and a few other things (run build-mingw.bat /? for usage).

For example, if your MinGW distribution is in C:\mingw\, then you could run it
(from the command prompt that we have started earlier) like this:

> .\build-mingw.bat C:\mingw\bin\g++

If you are using the build2-mingw package then you should be able to use just
g++ for the compiler:

> .\build-mingw.bat g++

If you would like to speed the process up by compiling in parallel, then you
can instruct build-mingw.bat to bootstrap using GNU make (comes in the
build2-mingw package), for example:

> .\build-mingw.bat --make mingw32-make --make -j8 g++

| Note that at about half way through (bpkg fetch at step 4 below) the script
| will stop and prompt you to verify the authenticity of the repository
| certificate. To run the script unattended you can specify the certificate
| fingerprint with the --trust option (see build-mingw.bat /? for details).

The end result of the bootstrap process (performed either with the script or
manually) is the installed toolchain as well as the bpkg configuration in
build2-toolchain-X.Y\ that can be used to upgrade (UPGRADE file) to newer
versions. It can also be used to uninstall the toolchain:

> cd build2-toolchain-X.Y
> bpkg uninstall --all

| Note that in both cases (manual or scripted bootstrap), if something goes
| wrong and you need to restart the process, you must start with a clean
| toolchain source by unpacking it afresh from the archive.

The rest of this section outlines the manual bootstrap process.

1. Bootstrap, Phase 1

    First, we build a minimal build system with the provided
    bootstrap-mingw.bat batch file. Normally, the only argument you will pass
    to this script is the C++ compiler to use but there is also a way to
    specify compile options; run bootstrap-mingw.bat /? and see the
    build2\INSTALL file for details.

    > cd build2
    > .\bootstrap-mingw.bat g++ -w -static

    > build2\b-boot --version

    Alternatively, we can use the bootstrap.gmake GNU makefile to bootstrap in
    parallel:

    > cd build2
    > mingw32-make -f bootstrap.gmake -j 8 CXX=g++ CXXFLAGS=-w LDFLAGS=-static

    > build2\b-boot --version

2. Bootstrap, Phase 2

    Then, we rebuild the build system with the result of Phase 1 linking
    libraries statically.

    > build2\b-boot config.cxx=g++ config.bin.lib=static build2\exe{b}
    > move /y build2\b.exe build2\b-boot.exe

    > build2\b-boot --version

3. Stage

    At this step the build system and package manager are built with shared
    libraries and then staged:

    > cd ..  # Back to build2-toolchain-X.Y.Z\

    > build2\build2\b-boot configure      ^
      config.cxx=g++                      ^
      config.bin.lib=shared               ^
      config.bin.suffix=-stage            ^
      config.install.root=C:\build2       ^
      config.install.data_root=root\stage

    > build2\build2\b-boot install: build2\ bpkg\

    The strange-looking config.install.data_root=root\stage means install data
    files (as opposed to executable files) into the stage\ subdirectory of
    wherever config.install.root points to (so in our case it will be
    C:\build2\stage\). This subdirectory is temporary and will be removed in a
    few steps.

    Verify that the toolchain binaries can be found and work (this relies on
    the PATH environment variable we have set earlier):

    > where b-stage
    C:\build2\bin\b-stage.exe

    > where bpkg-stage
    C:\build2\bin\bpkg-stage.exe

    > b-stage --version
    > bpkg-stage --version

    At the next step we will use bpkg to build and install the entire
    toolchain. If for some reason you prefer not to build from packages (for
    example, because the machine is offline), then you can convert this step
    into a local installation and skip the rest of the steps.

    | To perform a local installation with the build-mingw.bat batch file, pass
    | the --local option.

    To perform a local installation you will need to change the configure and
    install command lines above along these lines (see also a note on the
    following step about only building shared libraries, toolchain executables
    prefix/suffix, etc):

    > build2\build2\b-boot configure ^
      config.config.hermetic=true    ^
      config.cxx=g++                 ^
      config.cc.coptions=-O3         ^
      config.bin.lib=shared          ^
      config.install.root=C:\build2

    > build2\build2\b-boot install: build2\ bpkg\ bdep\

    You will also need to build and install the standard build system modules:

    > b install: !config.install.scope=project libbuild2-*\

    | To verify the build system modules installation you can load them with
    | the following command:
    |
    | > b noop: tests\libbuild2-*\

    To uninstall such a local installation, run:

    > b uninstall: build2\ bpkg\ bdep\ libbuild2-*\

4. Install

    Next, we use the staged tools to build and install the entire toolchain
    from the package repository with the bpkg package manager. First, we create
    the bpkg configuration. The configuration values are pretty similar to the
    previous step and you may want/need to make similar adjustments.

    > cd ..  # Back to build2-build\
    > md build2-toolchain-X.Y
    > cd build2-toolchain-X.Y

    > bpkg-stage create             ^
      cc                            ^
      config.config.hermetic=true   ^
      config.cxx=g++                ^
      config.cc.coptions=-O3        ^
      config.bin.lib=shared         ^
      config.install.root=C:\build2

    | The above configuration will only build shared libraries. If you would
    | like to build both shared and static, remove config.bin.lib=shared.

    | To add a custom prefix/suffix to the toolchain executables names, add
    | config.bin.exe.prefix=... and/or config.bin.exe.suffix=....

    | The config.config.hermetic=true configuration variable in the above
    | command makes sure the embedded ~host and ~build2 configurations include
    | the current environment. This is especially important for ~build2 which
    | is used to dynamically build and load ad hoc recipes and build system
    | modules and must therefore match the environment that was used to build
    | the build system itself.

    Next, we add the package repository, build, and install:

    > bpkg-stage add https://pkg.cppget.org/1/alpha
    > bpkg-stage fetch
    > bpkg-stage build --for install build2 bpkg bdep
    > bpkg-stage install --all

    | By default bpkg will build the latest available version of each package.
    | You can, however, specify the desired versions explicitly, for example:
    |
    | > bpkg-stage build --for install build2/X.Y.Z bpkg/X.Y.Z bdep/X.Y.Z

    To verify the result, run (note that the where command is not available on
    Windows XP without the Resource Kit installed):

    > where b
    C:\build2\bin\b.exe

    > where bpkg
    C:\build2\bin\bpkg.exe

    > where bdep
    C:\build2\bin\bdep.exe

    > b --version
    > bpkg --version
    > bdep --version

    Finally, we build and install the standard build system modules:

    > bpkg build --for install libbuild2-autoconf libbuild2-kconfig
    > bpkg install !config.install.scope=project ^
      --all-pattern=libbuild2-*

    | To get a list of the standard pre-installed build system modules in a
    | specific version of the toolchain, run:
    |
    | > cd ..\build2-toolchain-X.Y.Z
    | > dir /B libbuild2-*

    | To verify the build system modules installation you can load them with
    | the following command:
    |
    | > b noop: ..\build2-toolchain-X.Y.Z\tests\libbuild2-*\

5. Clean

    The last thing we need to do is uninstall the staged tools:

    > cd ..\build2-toolchain-X.Y.Z  # Back to bootstrap.
    > b uninstall: build2\ bpkg\
