Proposal-Figuring out cmake root file without setting it yourslf

Hi, I have a project with nested projects, and some have peer dependency on each other and on other includes.

F.e

- cmake_modules
- Foo
----CmakeLists.txt #depends on cmake_modules
- Bar
----CmakeLists.txt #depends on cmake_modules
CMakeLists.txt

When I want to work only on the Foo project I usually open only the foo folder in my ide, but the foo project cannot be compiled without the dependency!

My options are:

  • set the cmake root when I edit a specific folder
  • open the root folder only
  • make the subproject include all the needed things (which would use a lot of relative includes

My proposal:
Add some command non_root() that I can place on the top of the cmake file to announce to users that this cannot be used as a root. But instead cmake will treat try to find the root in the parent directory.

Also, to make it even better, we could do that the only exported targets would be the ones that are from our Foo/CmakeLists.txt - so it would be just like the file included all the needed things.

What do you guys think?

Sorry if this is the wrong tag/place, I am new here

If you want to develop them separately, I suspect ExternalProject and/or FetchContent is the way to go here. Each has their drawbacks for development of the entire project, but maybe something can be set up with the dependency provider setup.

Cc: @craig.scott

1 Like

For cases where you want to be able to build nested projects on their own, and/or where you have common build logic you want to share between projects, I use FetchContent quite often for that scenario. But your case seems different, you appear to want to keep a single build and just limit the developer’s view of it to some sub-part. Your use case of being able to load one of the nested projects and have it take care of bringing in your dependencies can be achieved using FetchContent, but it probably won’t work how you want it to (it would be a separate build, not a build shared with a parent or other sibling projects).

It is the user’s responsibility to tell CMake where the top of the source tree is. That’s the directory they give to the cmake command. There are things CMake does before it even starts reading the CMakeLists.txt file, and some of those things need to know the top source directory (e.g. looking for and loading presets, as just one example). The sort of functionality you’re describing seems more like something that would be implemented by IDEs to limit the scope of what they show you.

1 Like