andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 1 | # Clang |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 3 | [Clang](http://clang.llvm.org/) is the main supported compiler when |
| 4 | building Chromium on all platforms. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 5 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 6 | Known [clang bugs and feature |
| 7 | requests](http://code.google.com/p/chromium/issues/list?q=label:clang). |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 8 | |
| 9 | [TOC] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 10 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 11 | ## Building with clang |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 12 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 13 | This happens by default, with clang binaries being fetched by gclient |
| 14 | during the `gclient runhooks` phase. To fetch them manually, or build |
| 15 | a local custom clang, use |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 16 | |
Nico Weber | 21e0f4f | 2015-12-04 14:24:47 | [diff] [blame] | 17 | tools/clang/scripts/update.py |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 18 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 19 | Run `gn args` and make sure there is no `is_clang = false` in your args.gn file. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 20 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 21 | Build: `ninja -C out/gn chrome` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 22 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 23 | ## Reverting to gcc on Linux or MSVC on Windows |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 24 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 25 | There are no bots that test this but `is_clang = false` will revert to |
| 26 | gcc on Linux and to Visual Studio on Windows. There is no guarantee it |
| 27 | will work. |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 28 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 29 | ## Mailing List |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 30 | |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 31 | https://groups.google.com/a/chromium.org/group/clang/topics |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 32 | |
| 33 | ## Using plugins |
| 34 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 35 | The |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 36 | [chromium style plugin](https://dev.chromium.org/developers/coding-style/chromium-style-checker-errors) |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 37 | is used by default when clang is used. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 38 | |
| 39 | If you're working on the plugin, you can build it locally like so: |
| 40 | |
Hans Wennborg | d1eec55 | 2019-05-02 14:59:07 | [diff] [blame] | 41 | 1. Run `./tools/clang/scripts/build.py --without-android` |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 42 | to build the plugin. |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 43 | 1. Run `ninja -C third_party/llvm-build/Release+Asserts/` to build incrementally. |
| 44 | 1. Build with clang like described above, but, if you use goma, disable it. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 45 | |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 46 | To test the FindBadConstructs plugin, run: |
| 47 | |
| 48 | (cd tools/clang/plugins/tests && \ |
vmpstr | 0b23ec1a | 2016-02-11 21:01:50 | [diff] [blame] | 49 | ./test.py ../../../../third_party/llvm-build/Release+Asserts/bin/clang \ |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 50 | ../../../../third_party/llvm-build/Release+Asserts/lib/libFindBadConstructs.so) |
| 51 | |
Daniel Cheng | 7174602d | 2018-03-29 20:49:14 | [diff] [blame] | 52 | Since the plugin is rolled with clang changes, behavior changes to the plugin |
| 53 | should be guarded by flags to make it easy to roll clang. A general outline: |
| 54 | 1. Implement new plugin behavior behind a flag. |
| 55 | 1. Wait for a compiler roll to bring in the flag. |
| 56 | 1. Start passing the new flag in `GN` and verify the new behavior. |
| 57 | 1. Enable the new plugin behavior unconditionally and update the plugin to |
| 58 | ignore the flag. |
| 59 | 1. Wait for another compiler roll. |
| 60 | 1. Stop passing the flag from `GN`. |
| 61 | 1. Remove the flag completely. |
| 62 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 63 | ## Using the clang static analyzer |
| 64 | |
kirillbobyrev | eed2584d | 2015-11-02 14:38:29 | [diff] [blame] | 65 | See [clang_static_analyzer.md](clang_static_analyzer.md). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 66 | |
| 67 | ## Windows |
| 68 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 69 | Since October 2017, clang is the default compiler on Windows. It uses |
| 70 | MSVC's linker and SDK, so you still need to have Visual Studio with |
| 71 | C++ support installed. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 72 | |
Daniel Bratell | fd047c6 | 2018-03-12 15:35:02 | [diff] [blame] | 73 | To use MSVC's compiler (if it still works), use `is_clang = false`. |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame] | 74 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 75 | Current brokenness: |
| 76 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 77 | * To get colored diagnostics, you need to be running |
| 78 | [ansicon](https://github.com/adoxa/ansicon/releases). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 79 | |
| 80 | ## Using a custom clang binary |
| 81 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 82 | Set `clang_base_path` in your args.gn to the llvm build directory containing |
| 83 | `bin/clang` (i.e. the directory you ran cmake). This [must][1] be an absolute |
| 84 | path. You also need to disable chromium's clang plugin. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 85 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 86 | Here's an example that also disables debug info and enables the component build |
| 87 | (both not strictly necessary, but they will speed up your build): |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 88 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 89 | ``` |
| 90 | clang_base_path = getenv("HOME") + "/src/llvm-build" |
| 91 | clang_use_chrome_plugins = false |
| 92 | is_debug = false |
| 93 | symbol_level = 1 |
| 94 | is_component_build = true |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 95 | ``` |
| 96 | |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 97 | 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] | 98 | 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] | 99 | -C out/gn` to build. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 100 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 101 | If your clang revision is very different from the one currently used in chromium |
| 102 | |
Nico Weber | 21e0f4f | 2015-12-04 14:24:47 | [diff] [blame] | 103 | * Check `tools/clang/scripts/update.py` to find chromium's clang revision |
thakis | 2a46c2d1 | 2016-11-29 22:41:38 | [diff] [blame] | 104 | * You might have to tweak warning flags. |