|
| 1 | +# GN Build |
| 2 | + |
| 3 | +Similar to GYP, GN is a build system designed for building Chromium. The |
| 4 | +official builds of Node.js are built with GYP, but GN build files are also |
| 5 | +provided as an unofficial alternative build system. |
| 6 | + |
| 7 | +The GN build files only support a subset of the Node.js build configurations. |
| 8 | +It's not required for all pull requests to Node.js to support GN builds, and the |
| 9 | +CI of Node.js does not currently test GN builds, though Node.js welcomes pull |
| 10 | +requests that improve GN support or fix breakages introduced by other pull |
| 11 | +requests. |
| 12 | + |
| 13 | +Currently the GN build is used by: |
| 14 | + |
| 15 | +1. Electron for building Node.js together with Chromium. |
| 16 | +2. V8 for testing the integration of Node.js in CI. |
| 17 | + |
| 18 | +## Files for GN support |
| 19 | + |
| 20 | +Node.js contains following GN build files: |
| 21 | + |
| 22 | +* `node.gni` - Public GN arguments for configuring the build. |
| 23 | +* `*/BUILD.gn` - This is the file where GN looks for build rules, in Node.js it |
| 24 | + just includes the `unofficial.gni` file to avoid accidental modifications. |
| 25 | +* `*/unofficial.gni` - The actual build rules for each component. |
| 26 | + |
| 27 | +## Build with GN |
| 28 | + |
| 29 | +Unlike GYP, the GN tool does not include any built-in rules for compiling a |
| 30 | +project, which means projects building with GN must provide their own build |
| 31 | +configurations for things like how to invoke a C++ compiler. Chromium related |
| 32 | +projects like V8 and skia choose to reuse Chromium's build configurations, and |
| 33 | +V8's Node.js integration testing repository |
| 34 | +([node-ci](https://chromium.googlesource.com/v8/node-ci/)) can be reused for |
| 35 | +building Node.js. |
| 36 | + |
| 37 | +### 1. Install `depot_tools` |
| 38 | + |
| 39 | +The `depot_tools` is a set of tools used by Chromium related projects for |
| 40 | +checking out code and managing dependencies, and since this guide is reusing the |
| 41 | +infra of V8, it needs to be installed and added to `PATH`: |
| 42 | + |
| 43 | +```bash |
| 44 | +git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git |
| 45 | +export PATH=/path/to/depot_tools:$PATH |
| 46 | +``` |
| 47 | + |
| 48 | +You can also follow the [official tutorial of |
| 49 | +`depot_tools`](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html). |
| 50 | + |
| 51 | +### 2. Check out code of Node.js |
| 52 | + |
| 53 | +To check out the latest main branch of Node.js for building, use the `fetch` |
| 54 | +tool from `depot_tools`: |
| 55 | + |
| 56 | +```bash |
| 57 | +mkdir node_gn |
| 58 | +cd node_gn |
| 59 | +fetch node |
| 60 | +``` |
| 61 | + |
| 62 | +You can choose to save some time by omitting git history: |
| 63 | + |
| 64 | +```bash |
| 65 | +fetch --no-history node |
| 66 | +``` |
| 67 | + |
| 68 | +After syncing is done, you will get a directory structure like this: |
| 69 | + |
| 70 | +```console |
| 71 | +node_gn/ |
| 72 | +├── .gclient |
| 73 | +├── .gclient_entries |
| 74 | +├── ... |
| 75 | +└── node |
| 76 | + ├── DEPS |
| 77 | + ├── ... |
| 78 | + ├── build/ |
| 79 | + ├── node/ |
| 80 | + └── tools/ |
| 81 | +``` |
| 82 | + |
| 83 | +The `node_gn` is a workspace directory, which only contains configurations and |
| 84 | +caches of the `gclient` tool from `depot_tools`, and the repository is checked |
| 85 | +out at `node_gn/node`. |
| 86 | + |
| 87 | +The `node_gn/node` directory is not a checkout of Node.js, it is actually |
| 88 | +[node-ci](https://chromium.googlesource.com/v8/node-ci/), the repository used by |
| 89 | +V8 for testing integration with Node.js. The source code of Node.js is checked |
| 90 | +out at `node_gn/node/node`. |
| 91 | + |
| 92 | +### 3. Build |
| 93 | + |
| 94 | +GN only supports [`ninja`](https://ninja-build.org) for building, so to build |
| 95 | +Node.js with GN, `ninja` build files should be generated first, and then |
| 96 | +`ninja` can be invoked to do the building. |
| 97 | + |
| 98 | +The `node-ci` repository provides a script for calling GN: |
| 99 | + |
| 100 | +```bash |
| 101 | +cd node # Enter `node_gn/node` which contains a node-ci checkout |
| 102 | +./tools/gn-gen.py out/Release |
| 103 | +``` |
| 104 | + |
| 105 | +which writes `ninja` build files into the `out/Release` directory under |
| 106 | +`node_gn/node`. |
| 107 | + |
| 108 | +And then you can execute `ninja`: |
| 109 | + |
| 110 | +```bash |
| 111 | +ninja -C out/Release node |
| 112 | +``` |
| 113 | + |
| 114 | +After the build is completed, the compiled Node.js executable can be found in |
| 115 | +`out/Release/node`. |
| 116 | + |
| 117 | +## Status of the GN build |
| 118 | + |
| 119 | +Currently the GN build of Node.js is not fully functioning. It builds for macOS |
| 120 | +and Linux, while the Windows build is still a work in progress. And some tests |
| 121 | +are still failing with the GN build. |
| 122 | + |
| 123 | +There are also efforts on making GN build work without using `depot_tools`, |
| 124 | +which is tracked in the issue |
| 125 | +[#51689](https://github.com/nodejs/node/issues/51689). |
0 commit comments