andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 1 | # Clang |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 3 | [Clang](http://clang.llvm.org/) is a compiler with many desirable features |
| 4 | (outlined on their website). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 5 | |
Andrew Grieve | fe71bbc | 2017-09-22 18:44:40 | [diff] [blame] | 6 | Chrome can be built with Clang. It is now the default compiler on Android, Mac |
| 7 | and Linux for building Chrome, and it is currently useful for its warning and |
| 8 | error messages on Windows. |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 9 | |
| 10 | See |
| 11 | [the open bugs](http://code.google.com/p/chromium/issues/list?q=label:clang). |
| 12 | |
| 13 | [TOC] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 14 | |
| 15 | ## Build instructions |
| 16 | |
| 17 | Get clang (happens automatically during `gclient runhooks` on Mac and Linux): |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 18 | |
Nico Weber | 21e0f4f | 2015-12-04 14:24:47 | [diff] [blame] | 19 | tools/clang/scripts/update.py |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 20 | |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 21 | Only needs to be run once per checkout, and clang will be automatically updated |
| 22 | by `gclient runhooks`. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 23 | |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 24 | Regenerate the ninja build files with Clang enabled. Again, on Linux and Mac, |
| 25 | Clang is the default compiler. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 26 | |
brettw | dbb91a1b | 2016-08-03 20:14:54 | [diff] [blame] | 27 | Run `gn args` and add `is_clang = true` to your args.gn file. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 28 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 29 | Build: `ninja -C out/gn chrome` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 30 | |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 31 | ## Reverting to gcc on linux |
| 32 | |
| 33 | We don't have bots that test this, but building with gcc4.8+ should still work |
brettw | c25693b3 | 2016-05-26 01:11:52 | [diff] [blame] | 34 | on Linux. If your system gcc is new enough, run `gn args` and add `is_clang = |
| 35 | false`. |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 36 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 37 | ## Mailing List |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 38 | |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 39 | https://groups.google.com/a/chromium.org/group/clang/topics |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 40 | |
| 41 | ## Using plugins |
| 42 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 43 | The |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 44 | [chromium style plugin](https://dev.chromium.org/developers/coding-style/chromium-style-checker-errors) |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 45 | is used by default when clang is used. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 46 | |
| 47 | If you're working on the plugin, you can build it locally like so: |
| 48 | |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 49 | 1. Run `./tools/clang/scripts/update.py --force-local-build --without-android` |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 50 | to build the plugin. |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 51 | 1. Run `ninja -C third_party/llvm-build/Release+Asserts/` to build incrementally. |
| 52 | 1. Build with clang like described above, but, if you use goma, disable it. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 53 | |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 54 | To test the FindBadConstructs plugin, run: |
| 55 | |
| 56 | (cd tools/clang/plugins/tests && \ |
vmpstr | 0b23ec1a | 2016-02-11 21:01:50 | [diff] [blame] | 57 | ./test.py ../../../../third_party/llvm-build/Release+Asserts/bin/clang \ |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 58 | ../../../../third_party/llvm-build/Release+Asserts/lib/libFindBadConstructs.so) |
| 59 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 60 | ## Using the clang static analyzer |
| 61 | |
kirillbobyrev | eed2584d | 2015-11-02 14:38:29 | [diff] [blame] | 62 | See [clang_static_analyzer.md](clang_static_analyzer.md). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 63 | |
| 64 | ## Windows |
| 65 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 66 | clang can be used as compiler on Windows. Clang uses Visual Studio's linker and |
| 67 | SDK, so you still need to have Visual Studio installed. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 68 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 69 | Things should compile, and all tests should pass. You can check these bots for |
| 70 | how things are currently looking: |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 71 | https://build.chromium.org/p/chromium.fyi/console?category=win%20clang |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 72 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 73 | ```shell |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 74 | python tools\clang\scripts\update.py |
brettw | dbb91a1b | 2016-08-03 20:14:54 | [diff] [blame] | 75 | # run `gn args` and add `is_clang = true` to your args.gn, then... |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 76 | ninja -C out\gn chrome |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 77 | ``` |
| 78 | |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 79 | The `update.py` script only needs to be run once per checkout. Clang will be |
thakis | 3e861de | 2016-06-14 14:24:01 | [diff] [blame] | 80 | kept up to date by `gclient runhooks`. |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 81 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 82 | Current brokenness: |
| 83 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 84 | * To get colored diagnostics, you need to be running |
| 85 | [ansicon](https://github.com/adoxa/ansicon/releases). |
thakis | 3a2294e | 2016-09-12 22:38:49 | [diff] [blame] | 86 | * Debug info does now work, but support for it is new. If you see something |
| 87 | not working right, please file a bug and mark it as blocking the |
| 88 | [clang/win debug info tracking bug](https://crbug.com/636111). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 89 | |
| 90 | ## Using a custom clang binary |
| 91 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 92 | Set `clang_base_path` in your args.gn to the llvm build directory containing |
| 93 | `bin/clang` (i.e. the directory you ran cmake). This [must][1] be an absolute |
| 94 | path. You also need to disable chromium's clang plugin. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 95 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 96 | Here's an example that also disables debug info and enables the component build |
| 97 | (both not strictly necessary, but they will speed up your build): |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 98 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 99 | ``` |
| 100 | clang_base_path = getenv("HOME") + "/src/llvm-build" |
| 101 | clang_use_chrome_plugins = false |
| 102 | is_debug = false |
| 103 | symbol_level = 1 |
| 104 | is_component_build = true |
| 105 | is_clang = true # Implicitly set on Mac, Linux, iOS; needed on Win and Android. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 106 | ``` |
| 107 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 108 | You can then run `head out/gn/toolchain.ninja` and check that the first to |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 109 | lines set `cc` and `cxx` to your clang binary. If things look good, run `ninja |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 110 | -C out/gn` to build. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 111 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 112 | If your clang revision is very different from the one currently used in chromium |
| 113 | |
Nico Weber | 21e0f4f | 2015-12-04 14:24:47 | [diff] [blame] | 114 | * Check `tools/clang/scripts/update.py` to find chromium's clang revision |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 115 | * You might have to tweak warning flags. |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 116 | |
| 117 | ## Using LLD |
| 118 | |
| 119 | **Experimental!** |
| 120 | |
| 121 | LLD is a relatively new linker from LLVM. The current focus is on Windows and |
| 122 | Linux support, where it can link Chrome approximately twice as fast as gold and |
Andrew Grieve | fe71bbc | 2017-09-22 18:44:40 | [diff] [blame] | 123 | MSVC's link.exe as of this writing. |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 124 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 125 | Set `use_lld = true` in args.gn. |