Remove premature target writing before validation during `gn gen`.

This change modifies the way GN schedules writing targets to the
Ninja build plan to remove random crashes that have been affecting
the Fuchsia build for several weeks now.

The root cause of the problem, described in the associated bug,
is that a target and *all* its dependencies, including validations,
must be resolved to ensure there are no crash during the write
(which can call Target::GetMetadata() for generated_file() targets,
which will walk standard and validation targets).

Before this CL, the code used the condition that a target had to
be resolved, and all its validations *defined*, which is NOT
sufficient.

The crash was hidden by the fact that target resolution was pushed
to the background, resulting in race conditions and non-deterministic
ordering of the writes. This behavior was removed in a previous CL,
the current one focuses on the actual logic fix.

The fix includes defining a new RecordState enum to better describe
the different states of a given BuilderRecord, replacing the simple
`resolved_` boolean flag. State transitions can only happen under
specific conditions that are now properly documented.

- builder_record.h: Introduce RecordState enum to model the
  state of each record (Init/Defined/Resolved/Finalized). and
  document them properly, along with the conditions when a
  record can change state.

  Rename resolved() to is_resolved(), can_write() to can_finalize(),
  add is_defined() and is_finalized()

  Add Set{Defined,Resolved,Finalized}() methods to change the
  state and document what the caller should do just after that
  to ensure proper tracking of state changes.

  Add NotifyWaitingDependentsOnXXX() template methods to
  clarify the builder.cc code and keep the BuilderRecordSet
  values hidden.

  Remove validation_deps_ which is no longer useful.

  Rename the BuilderRecordSet variables for clarity, better
  explaining their purpose.

  Add DEBUG_BUILDER_RECORD macro. Set this to 1 to enable
  debug logs during Builder operations during development.

- builder.cc: Add FinalizeItem() method, and modify the logic
  accordingly, using the new BuilderRecord methods. Add
  DEBUG_BUILDER_RECORD_LOG() statements to print debug traces
  when DEBUG_BUILD_RECORD is set to 1.

- builder_unittest.cc: Adjust unit-tests to reflect the new
  logic.

Benchmarking shows now statistically difference in `gn gen`
and `gn gen --check` times for Fuchsia and Chrome builds on
a local cloudtop.

Bug: 494481832
Change-Id: Ibeae5350818c6b73fb88247fc73f85ab596c9531
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/21721
Commit-Queue: David Turner <digit@google.com>
Reviewed-by: Andrew Grieve <agrieve@google.com>
5 files changed
tree: cadff450fe5e0c3769df302a93379fc5a7bc2ba3
  1. build/
  2. conductor/
  3. docs/
  4. examples/
  5. infra/
  6. misc/
  7. src/
  8. tools/
  9. .clang-format
  10. .editorconfig
  11. .git-blame-ignore-revs
  12. .gitattributes
  13. .gitignore
  14. .mailmap
  15. .style.yapf
  16. AUTHORS
  17. LICENSE
  18. navbar.md
  19. OWNERS
  20. README.md
README.md

GN

GN is a meta-build system that generates build files for Ninja.

Related resources:

What GN is for

GN is currently used as the build system for Chromium, Fuchsia, and related projects. Some strengths of GN are:

  • It is designed for large projects and large teams. It scales efficiently to many thousands of build files and tens of thousands of source files.

  • It has a readable, clean syntax. Once a build is set-up, it is generally easy for people with no backround in GN to make basic edits to the build.

  • It is designed for multi-platform projects. It can cleanly express many complicated build variants across different platforms. A single build invocation can target multiple platforms.

  • It supports multiple parallel output directories, each with their own configuration. This allows a developer to maintain builds targeting debug, release, or different platforms in parallel without forced rebuilds when switching.

  • It has a focus on correctness. GN checks for the correct dependencies, inputs, and outputs to the extent possible, and has a number of tools to allow developers to ensure the build evolves as desired (for example, gn check, testonly, assert_no_deps).

  • It has comprehensive build-in help available from the command-line.

Although small projects successfully use GN, the focus on large projects has some disadvanages:

  • GN has the goal of being minimally expressive. Although it can be quite flexible, a design goal is to direct members of a large team (who may not have much knowledge about the build) down an easy-to-understand, well-lit path. This isn't necessarily the correct trade-off for smaller projects.

  • The minimal build configuration is relatively heavyweight. There are several files required and the exact way all compilers and linkers are run must be specified in the configuration (see “Examples” below). There is no default compiler configuration.

  • It is not easily composable. GN is designed to compile a single large project with relatively uniform settings and rules. Projects like Chromium do bring together multiple repositories from multiple teams, but the projects must agree on some conventions in the build files to allow this to work.

  • GN is designed with the expectation that the developers building a project want to compile an identical configuration. So while builds can integrate with the user‘s environment like the CXX and CFLAGS variables if they want, this is not the default and most project’s builds do not do this. The result is that many GN projects do not integrate well with other systems like ebuild.

  • There is no simple release scheme (see “Versioning and distribution” below). Projects are expected to manage the version of GN they require. Getting an appropriate GN binary can be a hurdle for new contributors to a project. Since GN is relatively uncommon, it can be more difficult to find information and examples.

GN can generate Ninja build files for C, C++, Rust, Objective C, and Swift source on most popular platforms. Other languages can be compiled using the general “action” rules which are executed by Python or another scripting language (Google does this to compile Java and Go). But because this is not as clean, generally GN is only used when the bulk of the build is in one of the main built-in languages.

Getting a binary

You can download the latest version of GN binary for Linux, macOS and Windows from Google's build infrastructure (see “Versioning and distribution” below for how this is expected to work).

Alternatively, you can build GN from source with a C++17 compiler:

git clone https://gn.googlesource.com/gn
cd gn
python build/gen.py # --allow-warning if you want to build with warnings.
ninja -C out
# To run tests:
out/gn_unittests

On Windows, it is expected that cl.exe, link.exe, and lib.exe can be found in PATH, so you'll want to run from a Visual Studio command prompt, or similar.

On Linux, Mac and z/OS, the default compiler is clang++, a recent version is expected to be found in PATH. This can be overridden by setting the CC, CXX, and AR environment variables.

On MSYS and MinGW, the default compiler is g++, a recent version is expected to be found in PATH. This can be overridden by setting the CC, CXX, LD and AR environment variables.

On z/OS, building GN requires ZOSLIB to be installed, as described at that URL. When building with build/gen.py, use the option --zoslib-dir to specify the path to ZOSLIB:

cd gn
python build/gen.py --zoslib-dir /path/to/zoslib

By default, if you don't specify --zoslib-dir, gn/build/gen.py expects to find zoslib directory under gn/third_party/.

Examples

There is a simple example in examples/simple_build directory that is a good place to get started with the minimal configuration.

To build and run the simple example with the default gcc compiler:

cd examples/simple_build
../../out/gn gen -C out
ninja -C out
./out/hello

For a maximal configuration see the Chromium setup:

and the Fuchsia setup:

Reporting bugs

If you find a bug, you can see if it is known or report it in the bug database.

Sending patches

GN uses Gerrit for code review hosted at gn-review.googlesource.com. The short version of how to patch is:

Register at https://gn-review.googlesource.com.

... edit code ...
ninja -C out && out/gn_unittests

Then, to upload a change for review:

git commit
git push origin HEAD:refs/for/main

The first time you do this you'll get an error from the server about a missing change-ID. Follow the directions in the error message to install the change-ID hook and run git commit --amend to apply the hook to the current commit.

When revising a change, use:

git commit --amend
git push origin HEAD:refs/for/main

which will add the new changes to the existing code review, rather than creating a new one.

We ask that all contributors sign Google's Contributor License Agreement (either individual or corporate as appropriate, select ‘any other Google project’).

Community

You may ask questions and follow along with GN‘s development on Chromium’s gn-dev@ Google Group.

Versioning and distribution

Most open-source projects are designed to use the developer‘s computer’s current toolchain such as compiler, linker, and build tool. But the large centrally controlled projects that GN is designed for typically want a more hermetic environment. They will ensure that developers are using a specific compatible toolchain that is versioned with the code.

As a result, GN expects that the project choose the appropriate version of GN that will work with each version of the project. There is no “current stable version” of GN that is expected to work for all projects.

As a result, the GN developers do not maintain any packages in any of the various packaging systems (Debian, RedHat, HomeBrew, etc.). Some of these systems to have GN packages, but they are maintained by third parties and you should use them at your own risk. Instead, we recommend you refer your checkout tooling to download binaries for a specific hash from Google's build infrastructure or compile your own.

GN does not guarantee the backwards-compatibility of new versions and has no branches or versioning scheme beyond the sequence of commits to the main git branch (which is expected to be stable).

In practice, however, GN is very backwards-compatible. The core functionality has been stable for many years and there is enough GN code at Google alone to make non-backwards-compatible changes very difficult, even if they were desirable.

There have been discussions about adding a versioning scheme with some guarantees about backwards-compatibility, but nothing has yet been implemented.