Search code examples
c++gccmakefilecmakedistcc

DistCC and CMake - select between local and distributed build when running make


My project is build using CMake and is compiled with DistCC + GCC.

I configure the compiler as follows:

SET(CMAKE_C_COMPILER "distcc variation-of-gcc")

To build the project, I simply run 'cmake' and then 'make -jXX'.

Although distcc really speeds up things, I sometimes want to build without distribution - I want it to build locally on the machine.

I know I can modify DISTCC_HOSTS to include only localhost - but this still has the overhead of distcc networking, although it is faster than the overhead for other machines...

I can also do that by rerunning cmake again and modifying the CMAKE_C_COMPILER using customization flags.

But I am looking for a way to do that by just adding a flag directly to 'make'.

I.e.
# This will use distcc:
make -jXX ...
# This will run locally:
make LOCAL_BUILD=1 -jX ...

Is there a CMake trick I can use?


Solution

  • The simplest thing to do (IMO) is write a little script in your project that invokes the compiler, and change your CMake files to run that script instead of containing the name of the compiler directly:

    SET(CMAKE_C_COMPILER "my-gcc-script")
    

    Now you can have that script normally run distcc, but (based on an environment variable or something) also run without distcc. There isn't any need to change anything in your CMake files.