manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 1 | # CLion Dev |
| 2 | |
| 3 | CLion is an IDE |
| 4 | |
| 5 | Prerequisite: |
| 6 | [Checking out and building the chromium code base](README.md#Checking-Out-and-Building) |
| 7 | |
| 8 | [TOC] |
| 9 | |
| 10 | ## Setting up CLion |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 11 | |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 12 | 1. Install CLion |
| 13 | - Googlers only: See |
| 14 | [go/intellij-getting-started](https://goto.google.com/intellij-getting-started) |
| 15 | for installation and license authentication instructions |
| 16 | |
| 17 | 1. Run CLion |
| 18 | 1. Increase CLion's memory allocation |
| 19 | |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 20 | This step will help performance with large projects |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 21 | 1. Option 1 |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 22 | 1. At the startup dialog, in the bottom left corner, click on the Cog |
| 23 | icon and then click on `Edit Custom VM Options`: |
| 24 | 1. In the textbox, you will see different flags. Add or replace the |
| 25 | following flags (to learn |
| 26 | more: [Advanced configuration | IntelliJ IDEA](https://www.jetbrains.com/help/idea/tuning-the-ide.html#common-jvm-options)): |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 27 | ``` |
| 28 | -Xss2m |
| 29 | -Xms1g |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 30 | -Xmx31g |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 31 | ... |
| 32 | ``` |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 33 | 1. Close the `Edit Custom VM Options` dialog. |
| 34 | 1. Again, at the startup dialogue, in the bottom left corner, click on |
| 35 | the Cog icon and then click on `Edit Custom Properties`: |
| 36 | 1. Add or replace the following flags (to learn |
| 37 | more: [Platform Properties | IntelliJ IDEA](https://www.jetbrains.com/help/idea/tuning-the-ide.html#configure-platform-properties)): |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 38 | ``` |
| 39 | idea.max.intellisense.filesize=12500 |
| 40 | ``` |
| 41 | 1. Option 2; 2017 and prior versions may not include the options to setup |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 42 | your `VM Options` and `Properties` in the `configure` menu. Instead: |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 43 | 1. `Create New Project` |
| 44 | 1. `Help` > `Edit Custom VM Options` |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 45 | 1. `Help` > `Edit Custom Properties` |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 46 | |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 47 | ## Creating a new CLion project |
| 48 | |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 49 | 1. Import project |
| 50 | - At the startup dialog, select `Import Project` and select your `chromium` |
| 51 | directory; this should be the parent directory to `src`. Selecting `src` |
| 52 | instead would result in some CLion IDE files appearing in your repository. |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 53 | |
| 54 | ## Building, Running, and Debugging within CLion |
| 55 | |
| 56 | Configure CLion to use ninja for building |
| 57 | (see [Linux Build Instructions](https://g3doc.corp.google.com/company/teams/chrome/linux_build_instructions.md)) |
| 58 | |
| 59 | ### Custom Build Targets |
| 60 | |
| 61 | A custom Build Target allows CLion to compile Chromium similarly to running |
| 62 | ninja in the command line. |
| 63 | |
| 64 | #### Add a Custom Build Target |
| 65 | |
| 66 | The first step is to add Ninja (specifically autoninja) as an external tool |
| 67 | that will be used to create the build targets. |
| 68 | |
| 69 | 1. Go to `Settings` |
| 70 | 1. Then, to `Build, Execution, Deployment > Custom Build Targets` |
| 71 | 1. Click on the `+` sign to add a new build target. |
| 72 | 1. Fill the Name: `Default` |
| 73 | 1. Next to `Build`, click on the three dots (`...`) |
| 74 | 1. Click on `+` to add an External Tool |
| 75 | 1. Fill the form: |
| 76 | 1. Name: `autoninja - Default` |
| 77 | 1. Program: `<absolute path to depot_tools/autoninja>` |
Etienne Noel | 870b079 | 2022-09-14 01:12:36 | [diff] [blame] | 78 | 1. Arguments: `-C out/Default chrome` |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 79 | 1. Working Directory: `<absolute path to chromium/src>` |
| 80 | 1. Click `Ok` to close the dialog. |
| 81 | |
| 82 | #### Run/Debug configuration |
| 83 | |
| 84 | The build target configured in the previous section was necessary to configure |
| 85 | the Run/Debug configuration. |
| 86 | |
| 87 | 1. Click on the `Run` menu and then on `Edit Configurations`. |
| 88 | 1. Click on the `+` sign then on `Custom Build Application` |
| 89 | 1. Fill the form: |
| 90 | 1. Name: `Default` |
| 91 | 1. Target: Select the `Default` build target that was created previously. |
| 92 | 1. Executable: Click the three dots (`...`) and select |
| 93 | `src/out/Default/chrome` |
| 94 | 1. Click `Ok` to close the dialog. |
| 95 | |
can liu | b9da181 | 2024-04-10 18:17:09 | [diff] [blame] | 96 | ### Configure `~/.gdbinit` |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 97 | |
| 98 | Before being able to debug, you need to configure `~/.gdbinit`. Open it with a |
| 99 | text editor and enter: |
| 100 | |
| 101 | ``` |
| 102 | source <path to chromium>/src/tools/gdb/gdbinit |
| 103 | ``` |
| 104 | |
| 105 | ### Set the local gdb executable |
| 106 | |
| 107 | There have been some reports of slowness while debugging using CLion. |
| 108 | It seems that using that CLion's bundled gdb executable is the culprit. To use |
| 109 | the local one: |
| 110 | 1. Open `Settings` |
| 111 | 1. Search for `Toolchains` |
| 112 | 1. Next to `Debugger`, select `/usr/bin/gdb` |
| 113 | |
| 114 | #### Run Chrome! |
| 115 | |
| 116 | You can now run chrome directly from CLion. You can also debug and add a |
| 117 | breakpoint. Try it! |
| 118 | |
| 119 | `Run` > `Run` (`shift+f10`) or `Run` > `Debug` (`shift+f9`) (screenshot) |
| 120 |  |
| 121 | |
| 122 | ## Autocompletion |
| 123 | |
| 124 | ### Approach 1: Using CMakeLists |
| 125 | **Note**: It's possible that when you open the project, CLion created one for you. |
| 126 | |
| 127 | 1. Create or Modify the `chromium/CMakeLists.txt` file |
| 128 | 1. Open `chromium/CMakeLists.txt` |
| 129 | 1. Add or Replace the content with: |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 130 | ``` |
| 131 | cmake_minimum_required(VERSION 3.10) |
| 132 | project(chromium) |
| 133 | |
| 134 | set(CMAKE_CXX_STANDARD 14) |
| 135 | |
| 136 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) |
manuk hovanesian | b2953e4 | 2021-03-20 00:14:04 | [diff] [blame] | 137 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/out/Default/gen) |
manukh | 38a8b74 | 2022-08-03 22:25:52 | [diff] [blame] | 138 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/third_party/protobuf/src) |
manuk hovanesian | b2953e4 | 2021-03-20 00:14:04 | [diff] [blame] | 139 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/third_party/googletest/src/googletest/include) |
manukh | 38a8b74 | 2022-08-03 22:25:52 | [diff] [blame] | 140 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/third_party/googletest/src/googlemock/include) |
| 141 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/third_party/abseil-cpp) |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 142 | |
| 143 | # The following file used is irrelevant, it's only to improve CLion |
| 144 | # performance. Leaving at least 1 file is required in order for CLion |
| 145 | # to provide code completion, navigation, etc. |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 146 | add_executable(chromium src/components/omnibox/browser/document_provider.cc) |
| 147 | ``` |
| 148 | |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 149 | ### Approach 2: Compilation database |
| 150 | |
| 151 | Since Chromium does not use CMake to build, to “benefit from the advanced IDE |
| 152 | features that CLion provides”, a |
| 153 | [Compilation database](https://www.jetbrains.com/help/clion/compilation-database.html) |
| 154 | must be created to expose to CLion how Chromium is built. |
| 155 | |
| 156 | In the Chromium code source, there’s a python script |
| 157 | `src/tools/clang/scripts/generate_compdb.py` that will generate |
| 158 | the Compilation Database. |
| 159 | |
| 160 | **Note**: The two approaches are not currently compatible and switching between |
| 161 | one and the other is cumbersome. To go from approach 1 to 2, the |
| 162 | `CMakeWorkspace` line from `.idea/misc.xml` must be removed first. |
| 163 | |
| 164 | 1. Run from your `chromium` folder: |
| 165 | |
| 166 | ``` |
| 167 | python3 src/tools/clang/scripts/generate_compdb.py -p src/out/Default -o ./compile_commands.json --target_os=linux |
| 168 | ``` |
| 169 | |
| 170 | This generates the compilation database at `chromium/compile_commands.json`. |
| 171 | |
| 172 | 1. In CLion, select the `compile_commands.json` file in the project navigator, |
| 173 | right-click on it, then click on `Load Compilation Database Project`. |
| 174 | |
| 175 | **Note:** This step will produce a few hundred errors but it doesn't cause |
| 176 | any issues. It's simply because some files are not compiled. To remove some |
| 177 | errors, these flags can be added in `src/out/Default/args.gn`: |
| 178 | ``` |
| 179 | enable_nocompile_tests=true |
| 180 | ``` |
| 181 | |
| 182 | 1. CLion will start processing the files and will start indexing the symbols. |
| 183 | This step can take a few hours. To expedite this process, see the section |
| 184 | [Exclude irrelevant folders from indexing to speed up CLion](#exclude-irrelevant-folders-from-indexing-to-speed-up-clion) |
| 185 | below. |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 186 | |
| 187 | ## Optional Steps |
| 188 | |
| 189 | ### Create terminal cli or desktop entry |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 190 | |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 191 | To make it easier to startup CLion or open individual files: |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 192 | |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 193 | 1. Open the actions menu (`ctrl+shift+a`) |
| 194 | 1. Type `create desktop entry` and press `enter` |
| 195 | 1. Open the actions menu and enter `create command-line launcher` |
| 196 | |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 197 | ### Exclude irrelevant folders from indexing to speed up CLion |
| 198 | |
| 199 | Exclude folders that are irrelevant to your work from the indexing. Exclude at |
| 200 | first the following folders and manually include the ones relevant to you. Open |
| 201 | `chromium/.idea/misc.xml` and under `<project>` add or edit the following |
| 202 | XML component: |
| 203 | |
| 204 | **Note**: This step can be done from the UI in the Project view by selecting |
| 205 | all the folders to exclude, right-clicking on them, selecting |
| 206 | `Mark Directory As` and then selecting `Excluded`. |
| 207 | |
| 208 | ``` |
| 209 | <component name="CidrRootsConfiguration"> |
| 210 | <excludeRoots> |
| 211 | <file path="$PROJECT_DIR$/src/android_webview" /> |
| 212 | <file path="$PROJECT_DIR$/src/apps" /> |
| 213 | <file path="$PROJECT_DIR$/src/ash" /> |
| 214 | <file path="$PROJECT_DIR$/src/build" /> |
| 215 | <file path="$PROJECT_DIR$/src/build_overrides" /> |
| 216 | <file path="$PROJECT_DIR$/src/buildtools" /> |
| 217 | <file path="$PROJECT_DIR$/src/cc" /> |
| 218 | <file path="$PROJECT_DIR$/src/chromecast" /> |
| 219 | <file path="$PROJECT_DIR$/src/chromeos" /> |
| 220 | <file path="$PROJECT_DIR$/src/codelabs" /> |
| 221 | <file path="$PROJECT_DIR$/src/content" /> |
| 222 | <file path="$PROJECT_DIR$/src/courgette" /> |
| 223 | <file path="$PROJECT_DIR$/src/crypto" /> |
| 224 | <file path="$PROJECT_DIR$/src/dbus" /> |
| 225 | <file path="$PROJECT_DIR$/src/device" /> |
| 226 | <file path="$PROJECT_DIR$/src/docs" /> |
| 227 | <file path="$PROJECT_DIR$/src/extensions" /> |
| 228 | <file path="$PROJECT_DIR$/src/fuchsia_web" /> |
| 229 | <file path="$PROJECT_DIR$/src/gin" /> |
| 230 | <file path="$PROJECT_DIR$/src/google_apis" /> |
| 231 | <file path="$PROJECT_DIR$/src/google_update" /> |
| 232 | <file path="$PROJECT_DIR$/src/gpu" /> |
| 233 | <file path="$PROJECT_DIR$/src/headless" /> |
| 234 | <file path="$PROJECT_DIR$/src/infra" /> |
| 235 | <file path="$PROJECT_DIR$/src/ios" /> |
| 236 | <file path="$PROJECT_DIR$/src/ipc" /> |
| 237 | <file path="$PROJECT_DIR$/src/media" /> |
| 238 | <file path="$PROJECT_DIR$/src/mojo" /> |
| 239 | <file path="$PROJECT_DIR$/src/native_client" /> |
| 240 | <file path="$PROJECT_DIR$/src/native_client_sdk" /> |
| 241 | <file path="$PROJECT_DIR$/src/net" /> |
| 242 | <file path="$PROJECT_DIR$/src/out" /> |
| 243 | <file path="$PROJECT_DIR$/src/pdf" /> |
| 244 | <file path="$PROJECT_DIR$/src/ppapi" /> |
| 245 | <file path="$PROJECT_DIR$/src/printing" /> |
| 246 | <file path="$PROJECT_DIR$/src/remoting" /> |
| 247 | <file path="$PROJECT_DIR$/src/rlz" /> |
| 248 | <file path="$PROJECT_DIR$/src/sandbox" /> |
| 249 | <file path="$PROJECT_DIR$/src/services" /> |
| 250 | <file path="$PROJECT_DIR$/src/skia" /> |
| 251 | <file path="$PROJECT_DIR$/src/sql" /> |
| 252 | <file path="$PROJECT_DIR$/src/storage" /> |
| 253 | <file path="$PROJECT_DIR$/src/styleguide" /> |
| 254 | <file path="$PROJECT_DIR$/src/testing" /> |
| 255 | <file path="$PROJECT_DIR$/src/third_party" /> |
| 256 | <file path="$PROJECT_DIR$/src/tools" /> |
| 257 | <file path="$PROJECT_DIR$/src/ui" /> |
| 258 | <file path="$PROJECT_DIR$/src/url" /> |
| 259 | <file path="$PROJECT_DIR$/src/v8" /> |
| 260 | <file path="$PROJECT_DIR$/src/venv" /> |
| 261 | <file path="$PROJECT_DIR$/src/weblayer" /> |
| 262 | </excludeRoots> |
| 263 | </component> |
| 264 | ``` |
| 265 | |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 266 | ### Mark directories as `Library Files` |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 267 | |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 268 | To speed up CLion, optionally mark directories such as `src/third_party` as |
| 269 | `Library Files` |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 270 | |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 271 | 1. Open the `Project` navigation (`alt+1`) |
Etienne Noel | 835d6d5 | 2022-08-23 15:30:20 | [diff] [blame] | 272 | 1. Select and right-click the directories > `Mark directory as` > |
manukh | 080b47e | 2019-12-19 22:06:19 | [diff] [blame] | 273 | `Library Files` |
| 274 | 1. See `https://blog.jetbrains.com/clion/2015/12/mark-dir-as/` for more details |