Skip to content

Commit cb6b241

Browse files
author
pradeep
committed
Enable ccache based compilation when it is available (arrayfire#2893)
* Enable ccache based compilation when it is available automatically. If not present, no change in behavior. * The user can turn off using ccache using the cmake option `AF_USE_CCACHE`. Note that this is an advanced cmake option.
1 parent a6da2be commit cb6b241

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ project(ArrayFire VERSION 3.7.1 LANGUAGES C CXX)
1111

1212
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
1313

14+
include(config_ccache)
1415
include(AFBuildConfigurations)
1516
include(AFInstallDirs)
1617
include(CMakeDependentOption)

CMakeModules/config_ccache.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# picked up original content from https://crascit.com/2016/04/09/using-ccache-with-cmake/
2+
3+
if (UNIX)
4+
find_program(CCACHE_PROGRAM ccache)
5+
6+
set(CCACHE_FOUND OFF)
7+
if(CCACHE_PROGRAM)
8+
set(CCACHE_FOUND ON)
9+
endif()
10+
11+
option(AF_USE_CCACHE "Use ccache when compiling" ${CCACHE_FOUND})
12+
13+
if(${AF_USE_CCACHE})
14+
# Set up wrapper scripts
15+
set(C_LAUNCHER "${CCACHE_PROGRAM}")
16+
set(CXX_LAUNCHER "${CCACHE_PROGRAM}")
17+
configure_file(${ArrayFire_SOURCE_DIR}/CMakeModules/launch-c.in launch-c)
18+
configure_file(${ArrayFire_SOURCE_DIR}/CMakeModules/launch-cxx.in launch-cxx)
19+
execute_process(COMMAND chmod a+rx
20+
"${ArrayFire_BINARY_DIR}/launch-c"
21+
"${ArrayFire_BINARY_DIR}/launch-cxx"
22+
)
23+
if(CMAKE_GENERATOR STREQUAL "Xcode")
24+
# Set Xcode project attributes to route compilation and linking
25+
# through our scripts
26+
set(CMAKE_XCODE_ATTRIBUTE_CC "${ArrayFire_BINARY_DIR}/launch-c")
27+
set(CMAKE_XCODE_ATTRIBUTE_CXX "${ArrayFire_BINARY_DIR}/launch-cxx")
28+
set(CMAKE_XCODE_ATTRIBUTE_LD "${ArrayFire_BINARY_DIR}/launch-c")
29+
set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${ArrayFire_BINARY_DIR}/launch-cxx")
30+
else()
31+
# Support Unix Makefiles and Ninja
32+
set(CMAKE_C_COMPILER_LAUNCHER "${ArrayFire_BINARY_DIR}/launch-c")
33+
set(CMAKE_CXX_COMPILER_LAUNCHER "${ArrayFire_BINARY_DIR}/launch-cxx")
34+
endif()
35+
endif()
36+
mark_as_advanced(CCACHE_PROGRAM)
37+
mark_as_advanced(AF_USE_CCACHE)
38+
endif()

CMakeModules/launch-c.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# Xcode generator doesn't include the compiler as the
4+
# first argument, Ninja and Makefiles do. Handle both cases.
5+
if [[ "$1" = "${CMAKE_C_COMPILER}" ]] ; then
6+
shift
7+
fi
8+
9+
export CCACHE_CPP2=true
10+
exec "${C_LAUNCHER}" "${CMAKE_C_COMPILER}" "$@"

CMakeModules/launch-cxx.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# Xcode generator doesn't include the compiler as the
4+
# first argument, Ninja and Makefiles do. Handle both cases.
5+
if [[ "$1" = "${CMAKE_CXX_COMPILER}" ]] ; then
6+
shift
7+
fi
8+
9+
export CCACHE_CPP2=true
10+
exec "${CXX_LAUNCHER}" "${CMAKE_CXX_COMPILER}" "$@"

0 commit comments

Comments
 (0)