blob: 4d8051ff602d80ba8d5123766426724caad07ce9 [file] [log] [blame]
Nico Weberbbceba62018-11-17 02:21:53 +00001=====================
2Building LLVM with GN
3=====================
4
5.. contents::
6 :local:
7
8.. _Introduction:
9
10Introduction
11============
12
13*Warning* The GN build is experimental and best-effort. It might not work,
14and if you use it you're expected to feel comfortable to unbreak it if
15necessary. LLVM's official build system is CMake, if in doubt use that.
16If you add files, you're expected to update the CMake build but you don't need
17to update GN build files. Reviewers should not ask authors to update GN build
18files. Keeping the GN build files up-to-date is on the people who use the GN
19build.
20
Nico Weberbb7c7762019-01-08 15:19:00 +000021`GN <https://gn.googlesource.com/gn/>`_ is a metabuild system. It always
Nico Weberbbceba62018-11-17 02:21:53 +000022creates ninja files, but it can create some IDE projects (MSVC, Xcode, ...)
23which then shell out to ninja for the actual build.
24
25Its main features are that GN is very fast (it currently produces ninja files
26for LLVM's build in 35ms on the author's laptop, compared to 66s for CMake) --
27a 2000x difference), and since it's so fast it doesn't aggressively cache,
28making it possible to switch e.g. between release and debug builds in one build
29directory.
30
Nico Weberbbceba62018-11-17 02:21:53 +000031The main motivation behind the GN build is that some people find it more
32convenient for day-to-day hacking on LLVM than CMake. Distribution, building
33just parts of LLVM, and embedding the LLVM GN build from other builds are a
34non-goal for the GN build.
35
36This is a `good overview of GN <https://docs.google.com/presentation/d/15Zwb53JcncHfEwHpnG_PoIbbzQ3GQi_cpujYwbpcbZo/edit#slide=id.g119d702868_0_12>`_.
37
38.. _Quick start:
39
40Quick start
41===========
42
Nico Weberbbceba62018-11-17 02:21:53 +000043GN only works in the monorepo layout.
44
45#. Obtain a `gn binary <https://gn.googlesource.com/gn/#getting-started>`_.
46
Nico Webercfdd7792019-01-14 18:26:55 +000047#. In the root of the monorepo, run `llvm/utils/gn/gn.py gen out/gn`.
Nico Weber38ab6cd2019-01-14 12:50:40 +000048 `out/gn` is the build directory, it can have any name, and you can have as
49 many as you want, each with different build settings. (The `gn.py` script
50 adds `--dotfile=llvm/utils/gn/.gn --root=.` and just runs regular `gn`;
51 you can manually pass these parameters and not use the wrapper if you
52 prefer.)
Nico Weberbbceba62018-11-17 02:21:53 +000053
Nico Weberbb7c7762019-01-08 15:19:00 +000054#. Run e.g. `ninja -C out/gn check-lld` to build all prerequisites for and
55 run the LLD tests.
Nico Weberbbceba62018-11-17 02:21:53 +000056
57By default, you get a release build with assertions enabled that targets
58the host arch. You can set various build options by editing `out/gn/args.gn`,
59for example putting `is_debug = true` in there gives you a debug build. Run
Nico Webercfdd7792019-01-14 18:26:55 +000060`llvm/utils/gn/gn.py args --list out/gn` to see a list of all possible
Nico Weber38ab6cd2019-01-14 12:50:40 +000061options. After touching `out/gn/args.gn`, just run ninja, it will re-invoke gn
62before starting the build.
Nico Weberbbceba62018-11-17 02:21:53 +000063
64GN has extensive built-in help; try e.g. `gn help gen` to see the help
65for the `gen` command. The full GN reference is also `available online
66<https://gn.googlesource.com/gn/+/master/docs/reference.md>`_.
67
68GN has an autoformatter: `git ls-files '*.gn' '*.gni' | xargs -n 1 gn format`
69after making GN build changes is your friend.
70
Nico Webercefe2702018-11-27 05:19:17 +000071To not put `BUILD.gn` into the main tree, they are all below
72`utils/gn/secondary`. For example, the build file for `llvm/lib/Support` is in
73`utils/gn/secondary/llvm/lib/Support`.
Nico Weberbbceba62018-11-17 02:21:53 +000074
Nico Weber218bd442018-11-29 22:25:31 +000075.. _Syncing GN files from CMake files:
76
77Syncing GN files from CMake files
78=================================
79
80Sometimes after pulling in the latest changes, the GN build doesn't work.
81Most of the time this is due to someone adding a file to CMakeLists.txt file.
82Run `llvm/utils/gn/build/sync_source_lists_from_cmake.py` to print a report
83of which files need to be added to or removed from `BUILD.gn` files to
84match the corresponding `CMakeLists.txt`. You have to manually read the output
85of the script and implement its suggestions.
86
87If new `CMakeLists.txt` files have been added, you have to manually create
88a new corresponding `BUILD.gn` file below `llvm/utils/gn/secondary/`.
89
90If the dependencies in a `CMakeLists.txt` file have been changed, you have to
91manually analyze and fix.
92
Nico Weberbbceba62018-11-17 02:21:53 +000093.. _Philosophy:
94
95Philosophy
96==========
97
98GN believes in using GN arguments to configure the build explicitly, instead
99of implicitly figuring out what to do based on what's available on the current
100system.
101
102configure is used for three classes of feature checks:
103
104- compiler checks. In GN, these could use exec_script to identify the host
105 compiler at GN time. For now the build has explicit toggles for compiler
106 features. (Maybe there could be a script that writes args.gn based on the
107 host compiler). It's possible we'll use exec_script() for this going forward,
108 but we'd have one exec_script call to identify compiler id and version,
109 and then base GN arg default values of compiler id and version instead of
110 doing one exec_script per feature check.
111 (In theory, the config approach means a new os / compiler just needs to tweak
112 the checks and not the code, but in practice a) new os's / compilers are rare
113 b) they will require code changes anyhow, so the configure tradeoff seems
114 not worth it.)
115
116- library checks. For e.g. like zlib, GN thinks it's better to say "we require
117 zlib, else we error at build time" than silently omitting features. People
118 who really don't want to install zlib can explicitly set the GN arg to turn
119 off zlib.
120
121- header checks (does system header X exist). These are generally not needed
122 (just keying this off the host OS works fine), but if they should become
123 necessary in the future, they should be done at build time and the few
124 targets that need to know if header X exists then depend on that build-time
125 check while everything else can build parallel with it.
126
127- LLVM-specific build toggles (assertions on/off, debug on/off, targets to
128 build, ...). These map cleanly to GN args (which then get copied into
129 config.h in a build step).
130
131For the last two points, it would be nice if LLVM didn't have a single
132`config.h` header, but one header per toggle. That way, when e.g.
133`llvm_enable_terminfo` is toggled, only the 3 files caring about that setting
134would need to be rebuilt, instead of everything including `config.h`.
135
136GN doesn't believe in users setting arbitrary cflags from an environment
137variable, it wants the build to be controlled by .gn files.