Using compiler prefix commands with CMake (distcc, ccache)

There are utilities that use the existing compiler by adding the command as a prefix (so instead of calling cc -c file.c you can call distcc cc -c file.c ).

When using CMake, the compiler command can be modified, however, I ran into problems trying to use distcc , although this probably applies to any command prefix for the compiler ( ccache too).

  • CMake expects the compiler to be an absolute path, so setting CMAKE_C_COMPILER to /usr/bin/distcc /usr/bin/cc gives an error:

    /usr/bin/distcc /usr/bin/cc is not a full path to an existing compiler tool.

  • Installing the compiler in /usr/bin/distcc and CMAKE_C_COMPILER_ARG1 or CMAKE_C_FLAGS to start with /usr/bin/cc works in some cases, but with the error CHECK_C_SOURCE_COMPILES
    (checked if there is a way to support this even the CMAKE_REQUIRED_FLAGS prefix CMAKE_REQUIRED_FLAGS not work).

The only way I found this is to wrap the commands in a shell script.

 #!/bin/sh exec /usr/bin/distcc /usr/bin/cc " $@ " 

While this works, it would be nice to be able to use compiler helpers with CMake without leaving shell scripts (giving a little overhead when the build system could just use the command prefix).


So my question is:

Can CMake use compiler prefix commands (such as distcc) directly ?, without shell wrapper script?

+6
source share
1 answer

Since CMake 3.4.0 , there was CMAKE_ <LANG> _COMPILER_LAUNCHER and the corresponding target property is <LANG> _COMPILER_LAUNCHER . So if your project is C-only, you would do something like:

 cmake -DCMAKE_C_COMPILER_LAUNCHER=ccache /path/to/source CCACHE_PREFIX=distcc make -j`distcc -j` 

If you have a C ++ project, use -DCMAKE_CXX_COMPILER_LAUNCHER=ccache .

Or make your CMakeLists.txt smart and use ccache automatically if it can be found:

 #----------------------------------------------------------------------------- # Enable ccache if not already enabled by symlink masquerading and if no other # CMake compiler launchers are already defined #----------------------------------------------------------------------------- find_program(CCACHE_EXECUTABLE ccache) mark_as_advanced(CCACHE_EXECUTABLE) if(CCACHE_EXECUTABLE) foreach(LANG C CXX) if(NOT DEFINED CMAKE_${LANG}_COMPILER_LAUNCHER AND NOT CMAKE_${LANG}_COMPILER MATCHES ".*/ccache$") message(STATUS "Enabling ccache for ${LANG}") set(CMAKE_${LANG}_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE} CACHE STRING "") endif() endforeach() endif() 
+8
source

All Articles