1

I have a large project which I have divided into "modules" or "subprojects". The directory structure looks like this (simplified, but the basic idea):

project-root/

    CMakeLists.txt <-- Contains some variable definitions

    module1/
        CMakeLists.txt <-- Receives variable definitions from top-level
        src/

    module2/
        CMakeLists.txt <-- Receives variable definitions from top-level
        src/

The root CMakeLists.txt does not have any libraries or executables defined. It uses add_subdirectory(module1) and add_subdirectory(module2) to refer to the lower-level modules, which do have libraries and executables.

Now, all this works fine. I'm using CLion and it is able to build the entire project. My problem is when a developer wants to compile only one of the modules, without having to deal with the other modules.

I have created variables in the root-level CMakeLists.txt which are used by the CMakeLists.txt files of the modules. But if I try to compile only one of the modules, of course the variable is undefined (or blank). Is there a standard way of dealing with problems like this? I want to be able to compile each module independently, but I don't want to redefine the variables in every CMakeLists.txt file.

vre
  • 6,041
  • 1
  • 25
  • 39
Eli
  • 693
  • 5
  • 12
  • 1) what about using "set cache internal" variables, if not defined defined it in module otherwise get the parent value (i.e. do nothing). 2) You should also look at super build project stuff and the use of externalProject for adding subprojects cf http://crascit.com/2015/07/25/cmake-gtest/ 3) if modules are independant let release them in their own repository... – Mizux Feb 08 '18 at 14:13

1 Answers1

0

I believe it is exactly what people do when they add a "BUILD_TESTS" option.

Look at grpc for instance.

If you follow the option here, you'll see that if the option is defined, then they add a library (they do a whole bunch of other stuff, too).

So to summarize:

  1. Create an option: option(BUILD_MODULE_1 "Build module 1" ON)
  2. Call add_subdirectory(module1) only if(BUILD_MODULE_1)

In CLion, you can either edit the cache and reload CMake or add the option (e.g. -DBUILD_MODULE_1=OFF) to the "CMake options" in the settings.

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95