Managing Meson projects (applicable to other build systems)
This article describes a workaround which you can use to work with Meson or other build system in CLion using compilation databases and File Watchers.
Our example shows how to construct a meta build system which monitors changes in the Meson build script, and then regenerates and reloads the compilation database accordingly. This way, it lets you manage a Meson project to a full extent from within CLion. Moreover, you can build and run/debug such projects with the help of custom build targets and custom Run/Debug configurations.
Step 1. Create a Meson build directory and get the compilation database
Suppose we have a simple Meson project consisting of a main.c file and the corresponding meson.build script, both located in the meson_test directory.
Create a build directory for the project:
cd meson_test meson setup builddirMeson automatically generates compile_command.json in builddir.
If you need to create it manually, run the following ninja command:
cd builddir ninja -t compdb c_COMPILER cpp_COMPILER > compile_commands.json
Step 2. Open and configure the compilation database in CLion
In CLion, go to compile_command.json file created in step 1. Select Open as Project when prompted.
and select theBy default, CLion sets the directory containing compile_command.json as project root. Go to and change it to the top-level meson_test directory.
Step 3. Install the File Watchers plugin
Go to Marketplace, search for File Watchers, and install the plugin:
, switch to
Step 4. Register meson.build as a recognizable file type
Go to
.In the Recognized File Types section, click and specify the file type name (for example, Meson).
Select Meson in Recognized File Types, then click in the File name patterns field. Set the file pattern (meson.build in our case):
Step 5. Create a File Watcher for meson.build
Now we can create a File Watcher to follow up the changes in meson.build.
Go to custom watcher.
and add () a newConfigure the following settings:
In the File to Watch section, assign the watcher to Meson (the file type we created in step 4).
Set the Scope to Project files.
The Tool to Run on Changes section controls the program to be when the watcher is triggered. In our case, its
ninja
with thereconfigure
argument, which will re-generate the compilation database.Also, we need to change the Working directory the builddir.
Step 6. Modify meson.build
As an example of how we can change the build script, let's add a new file (calc.c. Then we can add this file in meson.build:
in the context menu of the Project view) and call itRight after that change in meson.build, the file watcher is triggered to run
ninja reconfigure
. This command will regenerate the compilation database, and CLion will automatically reload it so the project gets synchronized with the modified build script.We can check that now there are two entries in compile_commands.json:
Step 7. Make the File Watcher global
The final thing that we might want to do is make the Meson_watcher global. With this setting enabled, the watcher becomes available across all projects so you will be able to easily reuse it for other Meson applications in CLion:
This way, the combination of a File Watcher and a compilation database creates a mechanism to work with Meson projects without leaving CLion to edit the build scripts or recreate the compilation database manually. You can apply this approach to any build system that has complementary tools for generating compilation databases.
Note that currently, in this scheme, CLion reloads the entire compilation database for each change in the meson.build that triggers the File Watcher. Due to that, you may experience performance problems when applying this workflow to large code bases.
As the next steps, you can build and run/debug Meson projects with the help of custom build targets and custom Run/Debug configurations.