CLion 2021.3 Help

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.

# meson.build project('tutorial', 'c') executable('demo', 'main.c')
  1. Create a build directory for the project:

    cd meson_test meson setup builddir
  2. Meson 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

  1. In CLion, go to File | Open and select the compile_command.json file created in step 1. Select Open as Project when prompted.

  2. By default, CLion sets the directory containing compile_command.json as project root. Go to Tools | Compilation Database | Change Project Root and change it to the top-level meson_test directory.

Loading a Meson project via compilation database

Step 3. Install the File Watchers plugin

  • Go to Settings / Preferences | Plugins, switch to Marketplace, search for File Watchers, and install the plugin:

    Installing the File Watchers plugin

Step 4. Register meson.build as a recognizable file type

  1. Go to Settings / Preferences | Editor | File Types.

  2. In the Recognized File Types section, click the Add button and specify the file type name (for example, Meson).

  3. Select Meson in Recognized File Types, then click the Add button in the File name patterns field. Set the file pattern (meson.build in our case):

    Registering the Meson file type

Step 5. Create a File Watcher for meson.build

Now we can create a File Watcher to follow up the changes in meson.build.

  1. Go to Settings / Preferences | Tools | File Watchers and add (Icons welcome create new project) a new custom watcher.

  2. Configure 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 the reconfigure argument, which will re-generate the compilation database.

      Also, we need to change the Working directory the builddir.

    File watcher settings

Step 6. Modify meson.build

  1. As an example of how we can change the build script, let's add a new file (New | C/C++ Source File in the context menu of the Project view) and call it calc.c. Then we can add this file in meson.build:

    Adding a file to a Meson project
  2. Right 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.

  3. We can check that now there are two entries in compile_commands.json:

    The regenerated and reloaded compilation database

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:

    global File Watcher

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.

Last modified: 08 March 2021