8

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
oferlivny
  • 300
  • 4
  • 15
  • I don't know cmake at all but does it stick the compiler selected with `CMAKE_C_COMPILER` in a makefile variable and use that in the makefile itself? Or does it embed the actual compiler name all over the generated makefiles? – Etan Reisner Sep 07 '14 at 16:26
  • I believe it embeds the fixed string inside the Makefile in multiple places. So it is not simple to override. – oferlivny Sep 07 '14 at 17:12
  • The more I learn about cmake the less I like it. You could write your own script and use that as the compiler and have that respond to the presence of a `LOCAL_BUILD` or whatever variable to pick which compiler to use. – Etan Reisner Sep 07 '14 at 17:34
  • I thought there should be a more elegant solution. Anyways, since I use CMake for multiple platforms, with different compilers, can you suggest how to implement the script? It can be nice if it can be generated by CMake... – oferlivny Sep 08 '14 at 04:24

2 Answers2

7

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RoddyM
  • 91
  • 1
  • 3
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • I thought there should be a more elegant solution. Perhaps using a shell variable as a prefix to the compiler cmd. Anyways, since I use CMake for multiple platforms, with different compilers, can you suggest how to implement the script? It can be nice if it can be generated by CMake... – oferlivny Sep 08 '14 at 04:24
  • Will probably use a script of the form: $MYDISTCC $@, and then just set the $MYDISTCC variable before running make... – oferlivny Sep 08 '14 at 04:28
  • `"$MYDISTCC" "$@"` for correctness but yes, that's the basic idea and it would be really nice if cmake didn't force you to do it this way. And you could just use `$CC` there since that is what make uses normally and is what, I imagine, things like the autotools are using in their cross-compilation support as well. – Etan Reisner Sep 08 '14 at 12:51