Skip to content

Commit 8f881eb

Browse files
author
David Roberts
authored
[ML] Simplify CLion setup instructions (#715)
The CLion setup instructions used to specify two different ways of setting up the compilation database, one of which didn't always work. This removes the option that was claimed to not always work so that there is now one recommended way to do the setup. It also adds the scripts that are used into the repo so that they don't have to be created locally by every user.
1 parent 0a95ace commit 8f881eb

File tree

3 files changed

+51
-58
lines changed

3 files changed

+51
-58
lines changed

build-setup/clion/using_clion.md

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ by simply calling `g++` or `clang++`.
1414
For testing and evaluation purposes you can use [30-day trial version of CLion](https://www.jetbrains.com/clion) or
1515
participate on the [CLion early access program](https://www.jetbrains.com/clion/nextversion/).
1616

17-
You need to install the current [CLion](https://www.jetbrains.com/clion/) (version 2018.2 or later) with the following
17+
You need to install the current [CLion](https://www.jetbrains.com/clion/) (version 2018.2.3 or later) with the following
1818
plugins:
19-
* Compilation Database
19+
2020
* File Watchers
2121
* Makefile support
2222

@@ -27,7 +27,7 @@ You can install the plugins either during the initialization dialog, when you fi
2727

2828
Install Python module [compiledb](https://github.com/nickdiego/compiledb) from PyPi:
2929
```
30-
pip install compiledb
30+
sudo pip install compiledb
3131
```
3232

3333
It is better to install the module on the system level, since it will create an executable `compiledb`, which can be
@@ -41,44 +41,12 @@ projects](https://www.jetbrains.com/help/clion/managing-makefile-projects.html).
4141
additional instruction and screenshots. For more information on using compilation database in CLion, see [JetBrains
4242
CLion Help page](https://www.jetbrains.com/help/clion/compilation-database.html)
4343

44-
To create the compilation database file `compile_commands.json` in your project, simply call
45-
```
46-
compiledb -n make -j
47-
```
48-
from the project root directory.
49-
50-
The argument `-n` will avoid the complete build of the targets (which somewhat accelerates the process), while
51-
all arguments after `make` will simply be passed to the `make` command.
52-
53-
To add unittests files to the compilation database, you need to go to the directories of the individual unit tests, e.g.
54-
`lib/core/unittest` and run
55-
```
56-
compiledb -o ../../../compile_commands.json -n make -j
57-
```
58-
59-
New files are usually **added** to `compile_commands.json` unless you additionally specify `-f` for override.
60-
61-
Note that on macOS `compiledb` may not correctly populate the `compile_commands.json` output file when invoking `make`
62-
as above. An alternative is to call `make` directly and pipe the output to `compiledb`. For convenience these commands
63-
can be encapsulated in a shell script `compiledb.sh`
64-
```
65-
#!/usr/bin/env bash
44+
To create the compilation database file `compile_commands.json` in your project, run:
6645

67-
make -Bnwk -j7 | compiledb -o $1
6846
```
69-
Set the executable bit on the script
70-
```
71-
chmod a+x ./compiledb.sh
72-
```
73-
The script can then be used to populate the compilation database as follows
74-
```
75-
./compiledb.sh compile_commands.json
76-
for dir in `find lib -name unittest`; do echo $dir; (cd $dir; $CPP_SRC_HOME/compiledb.sh $CPP_SRC_HOME/compile_commands.json); done
47+
$CPP_SRC_HOME/dev-tools/init_compiledb.sh
7748
```
7849

79-
If the command runs suspiciously quickly and the `compile_commands.json` file is empty, this means that your project is
80-
already built and `make` didn't do anything. Simply run `make clean` before running `compiledb`.
81-
8250
Now, in CLion navigate to **File | Open** on the main menu and choose the `compile_commands.json` file or a directory
8351
that contains it and click **Open as Project**. All files and symbols processed during the run of `compiledb make` are
8452
visible and accessible from CLion.
@@ -89,20 +57,10 @@ auto-import** checkbox in **Settings / Preferences | Build, Execution, Deploymen
8957
To follow up the changes in the Makefiles, we can create File Watchers (you should have installed File Watchers plugin).
9058
Navigate to **Settings / Preferences | Tools | File Watchers** and create a new File Watcher for all files of type **GNU
9159
Makefile** located in the project root and subdirectories:
92-
93-
**File type:** GNU Makefile\
94-
**Scope:** Project Files\
95-
**Program:** `compiledb`\
96-
**Arguments:** `-n -o $ProjectFileDir$/compile_commands.json make -j`\
97-
**Working directory:** `$ProjectFileDir$`
98-
- [x] Auto-save edited files to trigger the watcher
99-
- [x] Trigger the watcher on external changes
100-
101-
Alternatively, if using the `compiledb.sh` wrapper script:
10260

10361
**File type:** GNU Makefile\
10462
**Scope:** Project Files\
105-
**Program:** `$ProjectFileDir$/compiledb.sh`\
63+
**Program:** `$ProjectFileDir$/dev-tools/compiledb.sh`\
10664
**Arguments:** `$ProjectFileDir$/compile_commands.json`\
10765
**Working directory:** `$FileDir$`
10866
- [x] Auto-save edited files to trigger the watcher
@@ -121,9 +79,9 @@ Let's create a custom target to build the libraries, which gives you the same be
12179
project root. Go to **Settings / Preference | Build, Execution, Deployment | Custom Build Targets** and click **+** to
12280
add a new target. Pick the name, in this tutorial we will use the name *make build*.
12381

124-
In the area **Toolchain** we have to specify custom tools for building and cleaning the project.
82+
In the area **Toolchain** we have to specify custom tools for building and cleaning the project.
12583

126-
For **Build:** click on **...** to open **External Tools** window and then on **+** to create a new external tool.
84+
For **Build:** click on **...** to open **External Tools** window and then on **+** to create a new external tool.
12785

12886
In the window **Edit Tool** specify:
12987

@@ -148,15 +106,15 @@ Similarly, for **Clean:** create a new external tool with the following entries
148106
- [x] Synchronize files after execution\
149107
- [x] Open console for tool output
150108

151-
Once you are done, your **Custom Build Targets** window should look similar to this:
109+
Once you are done, your **Custom Build Targets** window should look similar to this:
152110

153111
![Custom Build Targets Window](./custom_build_targets_window.png)
154112

155113
### Run/Debug Configuration
156114

157115
Once we created the custom build target, we can use it to build projects and run/debug unit tests withing IDE.
158116

159-
Let's go to menu **Run | Edit Configurations...** and click on **+** to create a new configuration.
117+
Let's go to menu **Run | Edit Configurations...** and click on **+** to create a new configuration.
160118

161119
Since we installed **Makefile support** plugin, we can add **Makefile** configuration to build the complete project:
162120

@@ -167,7 +125,7 @@ Since we installed **Makefile support** plugin, we can add **Makefile** configur
167125
- [x] Allow parallel run
168126

169127
I assume that you want to build the project with debug symbols activated, but, obviously, you need to remove the
170-
argument `ML_DUBUG=1` if you don't.
128+
argument `ML_DUBUG=1` if you don't.
171129

172130
It is important that you specify the environment variable `CPP_SRC_HOME`. If you specified it on the system
173131
level, CLion can pick it up automatically, otherwise you have to specify it explicitly in the **Environment variables**
@@ -179,12 +137,12 @@ You can now build the project manually by selecting the configuration `Libraries
179137
and clicking the green play button. Moreover, we will create a configuration for running unit tests and use `Libraries`
180138
as a build dependency so we ensure that the project is up-to-date every time we run those tests.
181139

182-
Let's create another **Run/Debug Configuration** for building the `core` unit tests. Go to menu
140+
Let's create another **Run/Debug Configuration** for building the `core` unit tests. Go to menu
183141
**Run | Edit Configurations...** and click on **+** to create a new configuration.
184142

185143
**Name:** `Build test core`\
186144
**Makefile:** Makefile\
187-
**Working Directory:** *Navigate to `lib/core/unittest/*\
145+
**Working Directory:** *Navigate to `lib/core/unittest/`*\
188146
**Arguments:** `-j7 ML_DEBUG=1`
189147
- [x] Allow parallel run
190148

@@ -194,7 +152,7 @@ Configurations...** and click on the **+** symbol to create a new **Custom Build
194152
**Name:** `Run test core`\
195153
**Target:** *Select the custom build target `make build` that we created before*\
196154
**Executable:** *Navigate to `lib/core/unittest/` and select the `ml_test` binary*\
197-
If you cannot find the executable `ml-test`, then you don't have one yet. Simply, build the unittests by executing
155+
If you cannot find the executable `ml-test`, then you don't have one yet. Simply, build the unittests by executing
198156
`make` in the `lib/core/unittest` directory once to create it.\
199157
**Working directory:** `lib/core/unittest`
200158

@@ -209,7 +167,7 @@ arguments**.
209167
Now, you can run and debug your code by selecting the appropriate configuration and using **play** or **debug** symbols.
210168

211169
Once build configurations for all unit tests have bee created it is possible to create a **Run/Debug Configuration** to
212-
invoke them all. Go to **Run | Edit Configurations...** and click on the **+** symbol to create a new
170+
invoke them all. Go to **Run | Edit Configurations...** and click on the **+** symbol to create a new
213171
**Compound** configuration named e.g. `Test All`. Click on the **+** symbol repeatedly to add each of the `Build test...`
214172
configurations.
215173

@@ -234,7 +192,7 @@ Alternatively, you may wish to set up a File Watcher to invoke `clang-format` fr
234192

235193
### Integration with `valgrind`
236194

237-
Clion has full support for running and analyzing code using the `valgrind` suite.
195+
Clion has full support for running and analyzing code using the `valgrind` suite.
238196
Valgrind is readily available on most linux distributions but on macOS an experimental build is required
239197
```
240198
brew install --HEAD https://raw.githubusercontent.com/sowson/valgrind/master/valgrind.rb

dev-tools/compiledb.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
#
3+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
4+
# or more contributor license agreements. Licensed under the Elastic License;
5+
# you may not use this file except in compliance with the Elastic License.
6+
#
7+
8+
# Used to create the compile_commands.json files required by CLion
9+
10+
make -Bnwk -j7 | grep -v "Finding dependencies of" | compiledb -o $1
11+

dev-tools/init_compiledb.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
#
3+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
4+
# or more contributor license agreements. Licensed under the Elastic License;
5+
# you may not use this file except in compliance with the Elastic License.
6+
#
7+
8+
# Used to initialize from scratch the compile_commands.json file required by
9+
# CLion
10+
11+
# Ensure $CPP_SRC_HOME is set
12+
CPP_SRC_HOME="${CPP_SRC_HOME:-`git rev-parse --show-toplevel`}"
13+
14+
cd "$CPP_SRC_HOME"
15+
rm -f compile_commands.json
16+
17+
dev-tools/compiledb.sh compile_commands.json
18+
for DIR in `find lib bin -name unittest`
19+
do
20+
echo "$DIR"
21+
(cd "$DIR" && "$CPP_SRC_HOME/dev-tools/compiledb.sh" "$CPP_SRC_HOME/compile_commands.json")
22+
done
23+
cd devbin && ../dev-tools/compiledb.sh ../compile_commands.json
24+

0 commit comments

Comments
 (0)