Rogue Ninja support in CLion
In a past few years I’ve been using C++ as my main programming language and during this time I’ve been in constant search for better IDE for it which would run on Linux. I was very happy when Jetbrains released CLion IDE and immediately gave it a try. Though there is a lot to improve yet, I’d say that it has best autocompletion and modern C++ support among C++ IDEs on Linux. The only problem I had with it was a build toolchain which it uses.
Currently CLion supports only CMake projects (with is totally fine for me) with GNU Make generator (which is sad). When using CMake, I always preferred Ninja build system, especially for large projects like one I work on as my main job. For some reason Make does not a very good job at incremental builds. For example, even if there is nothing to rebuild, it spends 5 seconds to only verify this fact, which is pretty annoying. On other hand, Ninja does this in like 200 ms.
Unfortunately, CLion developers currently have no plans on supporting Ninja (I realize that they have many things to do way more important than Ninja support), so I decided to solve this problem by myself.
CMake-Ninja wrapper for CLion
I’ve found a kind of solution for my problem on internet but it had two very critical drawbacks:
- You meed to edit manually CMake cache for each project you work on.
- You have to run
cmake -G Ninja
manually eash time you add or remove files from your project.
After messing around CMake and ~/.clion11
directory for a while I’ve came up with a simple python script which wrapped around CMake binary used by CLion, replacing -G "Unix Makefiles"
command line option with -G Ninja
. It did work in terms of making CLion using Ninja for building a project and didn’t require any additional actions from me unlike previous solution. Unfortunatelly, it absolutely broke autocompletion support in the IDE, since it relied upon some artifacts, which Unix Makefiles generator was producing.
After some trial and errors I modified my script to act according following rules:
-
Whenever it’s called outside of CLion’s private directory (
~/.clionXX
) or there is no-G
option in command line, it simply passes control over to CMake. -
If there is
-G
option, it does some black magic to make me, CMake and CLion happy:- First, it calls real CMake with original arguments, producing Makefiles required by CLion.
- Then it alters CMakeCache.txt to make CMake think that previous generator used was Ninja, not Unix Makefiles.
- Finally, replace “Unix Makefiles” occurrance in generator name with “Ninja” and call CMake again.
You may grab the script on GitHub. I’ve tested it on Linux and Python 2.7 but I suppose it should work on Windows and Mac too, maybe with minor modifications. Please, let me know in comments if it worked for you :-)
Finally, this script has several imperfections, which I’ll fix in future. One of the most important is that it currently mizes stderr and stdout of CMake which seems to confuse CLion a little bit when dealing with invalid CMakeList.txt file.