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 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 6 | Chrome can be built with Clang. It is now the default compiler on Mac and Linux |
| 7 | for building Chrome, and it is currently useful for its warning and error |
| 8 | messages on Android and Windows. |
| 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 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 27 | If you use gyp: `GYP_DEFINES=clang=1 build/gyp_chromium` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 28 | |
| 29 | If you use gn, run `gn args` and add `is_clang = true` to your args.gn file. |
| 30 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 31 | Build: `ninja -C out/Debug chrome` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 32 | |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame^] | 33 | ## Reverting to gcc on linux |
| 34 | |
| 35 | We don't have bots that test this, but building with gcc4.8+ should still work |
| 36 | on Linux. If your system gcc is new enough, use this to build with gcc if you |
| 37 | don't want to build with clang: |
| 38 | |
| 39 | GYP_DEFINES=clang=0 build/gyp_chromium |
| 40 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 41 | ## Mailing List |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 42 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 43 | http://groups.google.com/a/chromium.org/group/clang/topics |
| 44 | |
| 45 | ## Using plugins |
| 46 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 47 | The |
| 48 | [chromium style plugin](http://dev.chromium.org/developers/coding-style/chromium-style-checker-errors) |
| 49 | is used by default when clang is used. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 50 | |
| 51 | If you're working on the plugin, you can build it locally like so: |
| 52 | |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 53 | 1. Run `./tools/clang/scripts/update.py --force-local-build --without-android` |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 54 | to build the plugin. |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 55 | 1. Run `ninja -C third_party/llvm-build/Release+Asserts/` to build incrementally. |
| 56 | 1. Build with clang like described above, but, if you use goma, disable it. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 57 | |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 58 | To test the FindBadConstructs plugin, run: |
| 59 | |
| 60 | (cd tools/clang/plugins/tests && \ |
vmpstr | 0b23ec1a | 2016-02-11 21:01:50 | [diff] [blame] | 61 | ./test.py ../../../../third_party/llvm-build/Release+Asserts/bin/clang \ |
jyasskin | 37110bc | 2015-12-04 03:40:24 | [diff] [blame] | 62 | ../../../../third_party/llvm-build/Release+Asserts/lib/libFindBadConstructs.so) |
| 63 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 64 | To run [other plugins](writing_clang_plugins.md), add these to your |
| 65 | `GYP_DEFINES`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 66 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 67 | * `clang_load`: Absolute path to a dynamic library containing your plugin |
| 68 | * `clang_add_plugin`: tells clang to run a specific PluginASTAction |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 69 | |
| 70 | So for example, you could use the plugin in this directory with: |
| 71 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 72 | * `GYP_DEFINES='clang=1 clang_load=/path/to/libFindBadConstructs.so |
| 73 | clang_add_plugin=find-bad-constructs' gclient runhooks` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 74 | |
| 75 | ## Using the clang static analyzer |
| 76 | |
kirillbobyrev | eed2584d | 2015-11-02 14:38:29 | [diff] [blame] | 77 | See [clang_static_analyzer.md](clang_static_analyzer.md). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 78 | |
| 79 | ## Windows |
| 80 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 81 | clang can be used as compiler on Windows. Clang uses Visual Studio's linker and |
| 82 | SDK, so you still need to have Visual Studio installed. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 83 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 84 | Things should compile, and all tests should pass. You can check these bots for |
| 85 | how things are currently looking: |
| 86 | http://build.chromium.org/p/chromium.fyi/console?category=win%20clang |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 87 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 88 | ``` shell |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 89 | python tools\clang\scripts\update.py |
| 90 | set GYP_DEFINES=clang=1 |
| 91 | python build\gyp_chromium |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 92 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 93 | # or, if you use gn, run `gn args` and add `is_clang = true` to your args.gn |
| 94 | ninja -C out\Debug chrome |
| 95 | ``` |
| 96 | |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame^] | 97 | The `update.py` script only needs to be run once per checkout. Clang will be |
| 98 | kept up-to-date by `gclient runhooks`. |
| 99 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 100 | Current brokenness: |
| 101 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 102 | * Debug info is very limited. |
| 103 | * To get colored diagnostics, you need to be running |
| 104 | [ansicon](https://github.com/adoxa/ansicon/releases). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 105 | |
| 106 | ## Using a custom clang binary |
| 107 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 108 | If you want to try building Chromium with your own clang binary that you've |
| 109 | already 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 |
| 111 | use the configure/make build). You also need to disable chromium's clang plugin |
| 112 | by setting `clang_use_chrome_plugins=0`, as it likely won't load in your custom |
| 113 | clang binary. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 114 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 115 | Here'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): |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 117 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 118 | ```shell |
| 119 | GYP_DEFINES="clang=1 fastbuild=1 component=shared_library \ |
| 120 | clang_use_chrome_plugins=0 make_clang_dir=$HOME/src/llvm-build" \ |
| 121 | build/gyp_chromium |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 122 | ``` |
| 123 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 124 | You can then run `head out/Release/build.ninja` and check that the first to |
| 125 | lines set `cc` and `cxx` to your clang binary. If things look good, run `ninja |
| 126 | -C out/Release` to build. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 127 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 128 | If your clang revision is very different from the one currently used in chromium |
| 129 | |
Nico Weber | 21e0f4f | 2015-12-04 14:24:47 | [diff] [blame] | 130 | * Check `tools/clang/scripts/update.py` to find chromium's clang revision |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 131 | * 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). |
rnk | c052ba3 | 2016-04-13 21:13:00 | [diff] [blame^] | 133 | |
| 134 | ## Using LLD |
| 135 | |
| 136 | **Experimental!** |
| 137 | |
| 138 | LLD is a relatively new linker from LLVM. The current focus is on Windows and |
| 139 | Linux support, where it can link Chrome approximately twice as fast as gold and |
| 140 | MSVC's link.exe as of this writing. LLD does not yet support generating PDB |
| 141 | files, which makes it hard to debug Chrome while using LLD. |
| 142 | |
| 143 | If 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 |
| 145 | Windows. |