Excluding external CMake FetchContent source directories

So I was recently delighted to discover CMake's FetchContent module, and started to switch over to using that as an easy way to bring dependencies into my projects. In CMakeLists.txt, this looks something like:

FetchContent_Declare(mylib
SOURCE_DIR C:/dev/mylib)

FetchContent_MakeAvailable(mylib)

You can see I'm using `SOURCE_DIR` instead of `GIT_REPOSITORY` or `URL`, etc. This means that the source for `mylib` is not copied into the local build directory, but instead resides externally, in this case in `C:/dev/mylib`. The reason for this is that some of my dependencies are quite large (hundreds of megabytes) and I don't want that duplicated all over my filesystem.

In response, CLion does something.. interesting.

If this was the project layout in the IDE prior:

 myproject
cmake-build
CMakeLists.txt
main.cpp

Then after adding the FetchContent usage with `SOURCE_DIR`, the layout switches to:

 myproject
mylib
mylib.hpp
myproject
cmake-build
CMakeLists.txt
main.cpp

Where the topmost `myproject` directory is marked as a package.

So what's the problem? Well, CLion now thinks that `mylib` is a part of the `myproject` project. So operations like file searches, content searches, refactors, etc. will now include everything that might happen to be in `mylib`, even when set to "project files only".

This wouldn't be a huge deal for a couple of small libraries, but when you start bringing in large dependencies, suddenly that's a lot of workspace bloat/pollution.

One would think the solution to this would be to mark the `mylib` directory as excluded. But this doesn't work for two reasons.

  1. You can't. I don't know why, maybe because it's an external directory, but right-clicking "mark directory as" has no option for excluded. You can get around this by manually adding it to `<excludeRoots>` in `.idea/misc.xml`. Interestingly, once excluded, it disappears from the project and the IDE goes back to looking like it did before without the package. This almost works, but then you run into problem 2...
  2. Even if you hackily exclude it, for some reason this exclusion only applies to that topmost directory. Any files in subdirectories are still considered to be a part of the project. A crude fix for this might be to add every single subdirectory to `misc.xml` in much the same way, but I haven't tried this.

TLDR: I cannot find a good way to exclude external source directories brought in via cmake's FetchContent. With multiple or large dependencies, this makes working within the local project (via searches, refactors, etc) frustrating. And the problem is recursive, so you also get all the crap from the dependencies' dependencies, and so on.

Any help or suggestions appreciated, thanks!

4

Please sign in to leave a comment.