How-To's Tips & Tricks

Working With Meson in CLion Using Compilation DB

Since CLion 2023.3, you can natively open, build, and run/debug Meson projects. Meson support works on all platforms and for all local and remote toolchains, including WSL and Docker.

During the recent Q&A session on Reddit, we got a surprising number of questions about our plans for Meson support. Today we are going to show you how you can already use Meson-based projects in CLion. For this we will use compilation database support in CLion.

Meson is a relatively new build system that strives to be fast yet simple to use. It converts build scripts written in a Python-like language into one of the backend build systems: Ninja (the default and recommended one), Visual Studio, or Xcode. Recently Meson has started to gain popularity among C++ developers. CLion currently does not natively support Meson projects. However, in this blog post we’ll describe how you can use compilation databases to set up your Meson project in CLion in just a couple of simple steps.

Note: In this post we will use the DPDK project as an example. Steps described here can be reproduced on Windows (including all available toolchains), Linux, and macOS.

How to import a Meson project into CLion

To open a Meson project in CLion, you just need to perform two simple steps:

  1. Generate a compile_commands.json file from your project.
  2. Open this file in CLion as a project.

However, as always there are some important details to discuss.

What are compilation databases

Compilation database is a simple JSON file format for associating compilation flags with files in a project. It was originally introduced as a part of Clang tooling. However, it has gained popularity in general C/C++ tools as a simple yet powerful way to describe project files. CLion has natively supported compilation database projects since version 2018.2. Many build systems offer the ability to generate compilation database files (compile_commands.json), and Meson is one of them.

Note: compile_commands.json can only be generated if Ninja is used as the backend build system. Other backends (Visual Studio and Xcode) do not support compilation database generation.

Generate a compilation database

You don’t need to perform any additional steps to generate the compilation database from a Meson project. Meson will automatically place compile_commands.json inside the generated build directory. Compilation database generation is provided by Ninja, rather than by Meson itself. If, for some reason, Meson doesn’t generate compile_commands.json, you can use Ninja. Simply invoke this command:
ninja -t compdb -x c_COMPILER cpp_COMPILER > compile_commands.json. In this command, -t compdb instructs Ninja to generate compile_commands.json instead of building the project and -x tells Ninja to expand response files (rspfile) during database generation (available since version 1.9), while c_COMPILER and cpp_COMPILER are the names of Ninja rules generated by Meson.

Note: If you accidentally deleted the compile_commands.json file, it can be regenerated by invoking Meson reconfiguration (meson --reconfigure).

Open the compilation database in CLion

Now that you have a compile_commands.json file, you can use it to create a CLion project. To do so, simply open the directory that contains the file in CLion. Otherwise you can open the compile_commands.json file directly and select Open as Project:

Open as Project

After that CLion will automatically create a project from this file and import all its contents. You can see the results of this process in the Build tool window.

Imported project

If you are using Windows, it’s important to specify the toolchain in Settings/Preferences | Build, Execution, Deployment | Compilation Database. This is necessary for ensuring that CLion can correctly convert paths specified in compile_commands.json to native system paths. Note that compilation database projects do not yet support remote toolchains (CPP-16202).

Once CLion has finished loading and indexing the project, you can start working on it while benefiting from its full support for language features.

CLion will generate the project’s .idea directory in the same location as the compile_commands.json file. This directory will be used as the root directory in the Project View tool window. This may be inconvenient, since compile_commands.json is often located under the build subdirectory in the project. To solve this problem, you can use Tools | Compilation Database | Change Project Root to select a project root that corresponds to the actual root of the project. This way CLion will correctly organize all your project files according to your project structure.

Project View

While working on the project, you may need to change the contents of the generated compilation database, e.g. by adding new source files or changing compilation flags. In order for CLion to see these changes, the compilation database has to be reloaded. This can be done using Tools | Compilation Database | Reload Compilation Database Project. Or you can tell CLion to reload the project automatically whenever the compile_commands.json file changes in Settings/Preferences | Build Tools.

Building, running, and debugging

At this point, you have a working project that you can navigate and edit, and you can even compile individual files (Build | Recompile file). However, a couple of important things are still missing. You still can’t build the whole project, nor can you run or debug it. This is because the compilation database doesn’t provide necessary information about build targets and executable artifacts. Nevertheless, there is a way to integrate Meson (or any other build system) with CLion and debug your artifacts using custom build targets and applications.

Custom build target

First you will need to tell CLion how to build the project. CLion provides custom build targets for this purpose.

Custom Build Target

On the page Settings/Preferences | Build, Execution, Deployment | Custom Build Targets you can create a new custom build target and specify any command or script that CLion will use to build and clean it. In the case of Meson, you can simply use ninja as your build command and ninja clean as your clean command.

Custom build application

Now you can create a run configuration based on this build target. Simply go to Edit Configurations and create a new Custom Build Application run configuration. In this dialog you can specify the build target created earlier and the executable you want to run or debug.

Custom build app

That’s it! Now you can run and debug your artifacts, and CLion will automatically build them.

Debug

Note: CLion will use the debugger from the toolchain specified in your custom build target

Your CLion team
JetBrains
The Drive to Develop

image description