[CMake] Using ccache with Xcode generator

Craig Scott craig.scott at crascit.com
Sat Apr 9 05:54:52 EDT 2016


So it took me some time to find a technique, but it turns out there is a
way to get ccache working with the Xcode generator, just not through
RULE_LAUNCH_COMPILE. The full article explaining how is now available here:

https://crascit.com/2016/04/09/using-ccache-with-cmake/

The general gist of the technique is to set up your CMakeLists.txt file
like this:


cmake_minimum_required(VERSION 2.8)

find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
    # Support Unix Makefiles and Ninja
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

project(SomeProject)

if(CCACHE_PROGRAM AND CMAKE_GENERATOR STREQUAL "Xcode")
    # Set up wrapper scripts
    configure_file(ccache-c.in   ccache-c)
    configure_file(ccache-cxx.in ccache-cxx)
    execute_process(COMMAND chmod a+rx "${CMAKE_BINARY_DIR}/ccache-c"
"${CMAKE_BINARY_DIR}/ccache-cxx")

    # Set Xcode project attributes to route compilation through our scripts
    set(CMAKE_XCODE_ATTRIBUTE_CC  "${CMAKE_BINARY_DIR}/ccache-c")
    set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/ccache-cxx")
endif()

The two helper scripts ccache-c.in and ccache-cxx.in look like this:

*ccache-c.in <http://ccache-c.in>:*

#!/bin/sh
export CCACHE_CPP2=true
exec "${CCACHE_PROGRAM}" "${CMAKE_C_COMPILER}" "$@"

*ccache-cxx.in <http://ccache-cxx.in>:*

#!/bin/sh
export CCACHE_CPP2=true
exec "${CCACHE_PROGRAM}" "${CMAKE_CXX_COMPILER}" "$@"

The above gives full ccache support for Unix Makefiles, Ninja and Xcode,
with graceful fallback to normal non-ccache use where ccache is not
available or supported by the developer's current version of CMake. My
question now is, given that the above seems to highlight an avenue for
Xcode projects, can CMake's support for RULE_LAUNCH_COMPILE be enhanced to
support the Xcode generator too by populating the CC and CXX user-defined
Xcode project settings? That would be a nice improvement to CMake for
developers working on Apple platforms.



On Tue, Feb 2, 2016 at 7:03 AM, Gregor Jasny <gjasny at googlemail.com> wrote:

> Hello,
>
> On 01/02/16 04:52, Craig Scott wrote:
> > After a bit of experimenting, it seems that getting ccache working with
> the
> > Xcode generator isn't so straightforward. For Ninja and Unix Makefiles,
> the
> > RULE_LAUNCH_COMPILE global property nicely gives us the behaviour we
> want,
> > but this doesn't work for Xcode. Under the constraint that we can't
> modify
> > the build machines (meaning we can't assume ccache symlinks have been set
> > up for the clang compiler set), is there a way to coax CMake to use
> ccache
> > with Xcode? I know I could write a wrapper script to explicitly invoke
> > ccache, but this seems like something CMake should be able to do without
> > having to resort to that. Any suggestions?
>
> I guess you will have to modify the SDK within Xcode to be able to
> override the compiler for the SDK.
>
> As far as I remember Bullseye has the same problem to solve:
> http://www.bullseye.com/help/tool-xcode.html
> Maybe you can have a look there.
>
> Thanks,
> Gregor
>



-- 
Craig Scott
Melbourne, Australia
http://crascit.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160409/38f1c4f1/attachment-0001.html>


More information about the CMake mailing list