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 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 19 | tools/clang/scripts/update.sh |
| 20 | |
| 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 | |
| 24 | ### Reverting to gcc on linux |
| 25 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 26 | We don't have bots that test this, but building with gcc4.8+ should still work |
| 27 | on Linux. If your system gcc is new enough, use this to build with gcc if you |
| 28 | don't want to build with clang: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 29 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 30 | GYP_DEFINES=clang=0 build/gyp_chromium |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 31 | |
| 32 | ### Ninja |
| 33 | |
| 34 | Regenerate the build files (`clang=1` is on by default on Mac and Linux): |
| 35 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 36 | If you use gyp: `GYP_DEFINES=clang=1 build/gyp_chromium` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 37 | |
| 38 | If you use gn, run `gn args` and add `is_clang = true` to your args.gn file. |
| 39 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 40 | Build: `ninja -C out/Debug chrome` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 41 | |
| 42 | ## Mailing List |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 43 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 44 | http://groups.google.com/a/chromium.org/group/clang/topics |
| 45 | |
| 46 | ## Using plugins |
| 47 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 48 | The |
| 49 | [chromium style plugin](http://dev.chromium.org/developers/coding-style/chromium-style-checker-errors) |
| 50 | is used by default when clang is used. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 51 | |
| 52 | If you're working on the plugin, you can build it locally like so: |
| 53 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 54 | 1. Run `./tools/clang/scripts/update.sh --force-local-build --without-android` |
| 55 | to build the plugin. |
| 56 | 1. Build with clang like described above. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 57 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 58 | TODO: writing_clang_plugins does not exist. |
| 59 | To run [other plugins](writing_clang_plugins.md), add these to your |
| 60 | `GYP_DEFINES`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 61 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 62 | * `clang_load`: Absolute path to a dynamic library containing your plugin |
| 63 | * `clang_add_plugin`: tells clang to run a specific PluginASTAction |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 64 | |
| 65 | So for example, you could use the plugin in this directory with: |
| 66 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 67 | * `GYP_DEFINES='clang=1 clang_load=/path/to/libFindBadConstructs.so |
| 68 | clang_add_plugin=find-bad-constructs' gclient runhooks` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 69 | |
| 70 | ## Using the clang static analyzer |
| 71 | |
kirillbobyrev | eed2584d | 2015-11-02 14:38:29 | [diff] [blame^] | 72 | See [clang_static_analyzer.md](clang_static_analyzer.md). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 73 | |
| 74 | ## Windows |
| 75 | |
| 76 | **Experimental!** |
| 77 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 78 | clang can be used as compiler on Windows. Clang uses Visual Studio's linker and |
| 79 | SDK, so you still need to have Visual Studio installed. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 80 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 81 | Things should compile, and all tests should pass. You can check these bots for |
| 82 | how things are currently looking: |
| 83 | http://build.chromium.org/p/chromium.fyi/console?category=win%20clang |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 84 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 85 | ``` shell |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 86 | python tools\clang\scripts\update.py |
| 87 | set GYP_DEFINES=clang=1 |
| 88 | python build\gyp_chromium |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 89 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 90 | # or, if you use gn, run `gn args` and add `is_clang = true` to your args.gn |
| 91 | ninja -C out\Debug chrome |
| 92 | ``` |
| 93 | |
| 94 | Current brokenness: |
| 95 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 96 | * Goma doesn't work. |
| 97 | * Debug info is very limited. |
| 98 | * To get colored diagnostics, you need to be running |
| 99 | [ansicon](https://github.com/adoxa/ansicon/releases). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 100 | |
| 101 | ## Using a custom clang binary |
| 102 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 103 | If you want to try building Chromium with your own clang binary that you've |
| 104 | already built, set `make_clang_dir` to the directory containing `bin/clang` |
| 105 | (i.e. the directory you ran cmake in, or your `Release+Asserts` folder if you |
| 106 | use the configure/make build). You also need to disable chromium's clang plugin |
| 107 | by setting `clang_use_chrome_plugins=0`, as it likely won't load in your custom |
| 108 | clang binary. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 109 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 110 | Here's an example that also disables debug info and enables the component build |
| 111 | (both not strictly necessary, but they will speed up your build): |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 112 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 113 | ```shell |
| 114 | GYP_DEFINES="clang=1 fastbuild=1 component=shared_library \ |
| 115 | clang_use_chrome_plugins=0 make_clang_dir=$HOME/src/llvm-build" \ |
| 116 | build/gyp_chromium |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 117 | ``` |
| 118 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 119 | You can then run `head out/Release/build.ninja` and check that the first to |
| 120 | lines set `cc` and `cxx` to your clang binary. If things look good, run `ninja |
| 121 | -C out/Release` to build. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 122 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 123 | If your clang revision is very different from the one currently used in chromium |
| 124 | |
| 125 | * Check `tools/clang/scripts/update.sh` to find chromium's clang revision |
| 126 | * You might have to tweak warning flags. Or you could set `werror=` in the |
| 127 | line above to disable warnings as errors (but this only works on Linux). |