blob: 4e12a7e48c05c4b533bdfe3566f70d4aba2b024b [file] [log] [blame] [view]
andybons6eaa0c0d2015-08-26 20:12:521# Clang
andybons3322f762015-08-24 21:37:092
andybons6eaa0c0d2015-08-26 20:12:523[Clang](http://clang.llvm.org/) is a compiler with many desirable features
4(outlined on their website).
andybons3322f762015-08-24 21:37:095
andybons6eaa0c0d2015-08-26 20:12:526Chrome can be built with Clang. It is now the default compiler on Mac and Linux
7for building Chrome, and it is currently useful for its warning and error
8messages on Android and Windows.
9
10See
11[the open bugs](http://code.google.com/p/chromium/issues/list?q=label:clang).
12
13[TOC]
andybons3322f762015-08-24 21:37:0914
15## Build instructions
16
17Get clang (happens automatically during `gclient runhooks` on Mac and Linux):
andybons3322f762015-08-24 21:37:0918
Nico Weber21e0f4f2015-12-04 14:24:4719 tools/clang/scripts/update.py
andybons6eaa0c0d2015-08-26 20:12:5220
rnkc052ba32016-04-13 21:13:0021Only needs to be run once per checkout, and clang will be automatically updated
22by `gclient runhooks`.
andybons3322f762015-08-24 21:37:0923
rnkc052ba32016-04-13 21:13:0024Regenerate the ninja build files with Clang enabled. Again, on Linux and Mac,
25Clang is the default compiler.
andybons3322f762015-08-24 21:37:0926
andybons6eaa0c0d2015-08-26 20:12:5227If you use gyp: `GYP_DEFINES=clang=1 build/gyp_chromium`
andybons3322f762015-08-24 21:37:0928
29If you use gn, run `gn args` and add `is_clang = true` to your args.gn file.
30
andybons6eaa0c0d2015-08-26 20:12:5231Build: `ninja -C out/Debug chrome`
andybons3322f762015-08-24 21:37:0932
rnkc052ba32016-04-13 21:13:0033## Reverting to gcc on linux
34
35We don't have bots that test this, but building with gcc4.8+ should still work
36on Linux. If your system gcc is new enough, use this to build with gcc if you
37don't want to build with clang:
38
39 GYP_DEFINES=clang=0 build/gyp_chromium
40
andybons3322f762015-08-24 21:37:0941## Mailing List
andybons6eaa0c0d2015-08-26 20:12:5242
andybons3322f762015-08-24 21:37:0943http://groups.google.com/a/chromium.org/group/clang/topics
44
45## Using plugins
46
andybons6eaa0c0d2015-08-26 20:12:5247The
48[chromium style plugin](http://dev.chromium.org/developers/coding-style/chromium-style-checker-errors)
49is used by default when clang is used.
andybons3322f762015-08-24 21:37:0950
51If you're working on the plugin, you can build it locally like so:
52
jyasskin37110bc2015-12-04 03:40:24531. Run `./tools/clang/scripts/update.py --force-local-build --without-android`
andybons6eaa0c0d2015-08-26 20:12:5254 to build the plugin.
jyasskin37110bc2015-12-04 03:40:24551. Run `ninja -C third_party/llvm-build/Release+Asserts/` to build incrementally.
561. Build with clang like described above, but, if you use goma, disable it.
andybons3322f762015-08-24 21:37:0957
jyasskin37110bc2015-12-04 03:40:2458To test the FindBadConstructs plugin, run:
59
60 (cd tools/clang/plugins/tests && \
vmpstr0b23ec1a2016-02-11 21:01:5061 ./test.py ../../../../third_party/llvm-build/Release+Asserts/bin/clang \
jyasskin37110bc2015-12-04 03:40:2462 ../../../../third_party/llvm-build/Release+Asserts/lib/libFindBadConstructs.so)
63
andybons6eaa0c0d2015-08-26 20:12:5264To run [other plugins](writing_clang_plugins.md), add these to your
65`GYP_DEFINES`:
andybons3322f762015-08-24 21:37:0966
andybons6eaa0c0d2015-08-26 20:12:5267* `clang_load`: Absolute path to a dynamic library containing your plugin
68* `clang_add_plugin`: tells clang to run a specific PluginASTAction
andybons3322f762015-08-24 21:37:0969
70So for example, you could use the plugin in this directory with:
71
andybons6eaa0c0d2015-08-26 20:12:5272* `GYP_DEFINES='clang=1 clang_load=/path/to/libFindBadConstructs.so
73 clang_add_plugin=find-bad-constructs' gclient runhooks`
andybons3322f762015-08-24 21:37:0974
75## Using the clang static analyzer
76
kirillbobyreveed2584d2015-11-02 14:38:2977See [clang_static_analyzer.md](clang_static_analyzer.md).
andybons3322f762015-08-24 21:37:0978
79## Windows
80
andybons6eaa0c0d2015-08-26 20:12:5281clang can be used as compiler on Windows. Clang uses Visual Studio's linker and
82SDK, so you still need to have Visual Studio installed.
andybons3322f762015-08-24 21:37:0983
andybons6eaa0c0d2015-08-26 20:12:5284Things should compile, and all tests should pass. You can check these bots for
85how things are currently looking:
86http://build.chromium.org/p/chromium.fyi/console?category=win%20clang
andybons3322f762015-08-24 21:37:0987
andybons6eaa0c0d2015-08-26 20:12:5288``` shell
andybons3322f762015-08-24 21:37:0989python tools\clang\scripts\update.py
90set GYP_DEFINES=clang=1
91python build\gyp_chromium
andybons6eaa0c0d2015-08-26 20:12:5292
andybons3322f762015-08-24 21:37:0993# or, if you use gn, run `gn args` and add `is_clang = true` to your args.gn
94ninja -C out\Debug chrome
95```
96
rnkc052ba32016-04-13 21:13:0097The `update.py` script only needs to be run once per checkout. Clang will be
98kept up-to-date by `gclient runhooks`.
99
andybons3322f762015-08-24 21:37:09100Current brokenness:
101
andybons6eaa0c0d2015-08-26 20:12:52102* Debug info is very limited.
103* To get colored diagnostics, you need to be running
104 [ansicon](https://github.com/adoxa/ansicon/releases).
andybons3322f762015-08-24 21:37:09105
106## Using a custom clang binary
107
andybons6eaa0c0d2015-08-26 20:12:52108If you want to try building Chromium with your own clang binary that you've
109already built, set `make_clang_dir` to the directory containing `bin/clang`
110(i.e. the directory you ran cmake in, or your `Release+Asserts` folder if you
111use the configure/make build). You also need to disable chromium's clang plugin
112by setting `clang_use_chrome_plugins=0`, as it likely won't load in your custom
113clang binary.
andybons3322f762015-08-24 21:37:09114
andybons6eaa0c0d2015-08-26 20:12:52115Here's an example that also disables debug info and enables the component build
116(both not strictly necessary, but they will speed up your build):
andybons3322f762015-08-24 21:37:09117
andybons6eaa0c0d2015-08-26 20:12:52118```shell
119GYP_DEFINES="clang=1 fastbuild=1 component=shared_library \
120clang_use_chrome_plugins=0 make_clang_dir=$HOME/src/llvm-build" \
121build/gyp_chromium
andybons3322f762015-08-24 21:37:09122```
123
andybons6eaa0c0d2015-08-26 20:12:52124You can then run `head out/Release/build.ninja` and check that the first to
125lines set `cc` and `cxx` to your clang binary. If things look good, run `ninja
126-C out/Release` to build.
andybons3322f762015-08-24 21:37:09127
andybons6eaa0c0d2015-08-26 20:12:52128If your clang revision is very different from the one currently used in chromium
129
Nico Weber21e0f4f2015-12-04 14:24:47130* Check `tools/clang/scripts/update.py` to find chromium's clang revision
andybons6eaa0c0d2015-08-26 20:12:52131* You might have to tweak warning flags. Or you could set `werror=` in the
132 line above to disable warnings as errors (but this only works on Linux).
rnkc052ba32016-04-13 21:13:00133
134## Using LLD
135
136**Experimental!**
137
138LLD is a relatively new linker from LLVM. The current focus is on Windows and
139Linux support, where it can link Chrome approximately twice as fast as gold and
140MSVC's link.exe as of this writing. LLD does not yet support generating PDB
141files, which makes it hard to debug Chrome while using LLD.
142
143If you use gyp, you can enable it with `GYP_DEFINES=lld=1`. If you use gn, set
144`use_lld = true` in args.gn. Currently this configuration is only supported on
145Windows.