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

We use the following to allow make time (rather than cmake time) switching on and off of the -Werror flag.

if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
    # TODO: this approach for the WERROR only works with makefiles not Ninja
    set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> $(WERROR) -o <OBJECT> -c <SOURCE>")
endif()

Then we run

make WERROR=-Werror

to turn on warnings as error.

I expect you could do something similar to have whether to use distcc come from a make variable. Like this:

set(CMAKE_CXX_COMPILE_OBJECT "$(USE_DISTCC) <CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")

And then run either

make USE_DISTCC=distcc

or just

make

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.