Skip to content

Commit 9018def

Browse files
authored
Adding possibility of faster builds using ccache (#3381)
Adding possibility of faster builds using ccache
2 parents 04a8485 + 6680d9c commit 9018def

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

.ci/azure-pipelines/build-ubuntu.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ jobs:
1313
variables:
1414
BUILD_DIR: '$(Agent.BuildDirectory)/build'
1515
CMAKE_CXX_FLAGS: '-Wall -Wextra -Wabi -O2'
16+
CCACHE_DIR: $(Pipeline.Workspace)/ccache
17+
CCACHE_MAXSIZE: 15G
1618
steps:
19+
- task: CacheBeta@0
20+
inputs:
21+
key: ccache | gcc | $(Agent.OS)
22+
path: $(CCACHE_DIR)
23+
displayName: Fetch cached files for ccache
24+
- script: ccache -z
25+
displayName: 'Zero ccache statistics'
1726
- script: |
1827
mkdir $BUILD_DIR && cd $BUILD_DIR
1928
cmake $(Build.SourcesDirectory) \
2029
-DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" \
30+
-DPCL_ENABLE_CCACHE=ON \
2131
-DPCL_ONLY_CORE_POINT_TYPES=ON \
2232
-DBUILD_simulation=ON \
2333
-DBUILD_surface_on_nurbs=ON \
@@ -41,6 +51,8 @@ jobs:
4151
cmake --build . -- test_filters test_registration test_registration_api
4252
cmake --build . -- -j2
4353
displayName: 'Build Library'
54+
- script: ccache -s
55+
displayName: 'Display ccache statistics'
4456
- script: |
4557
cd $BUILD_DIR/test
4658
ctest -V -T Test

.ci/azure-pipelines/build-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
displayName: 'Update System PATH'
2727
- script: |
2828
vcpkg.exe install eigen3 flann gtest qhull --triplet %PLATFORM%-windows && vcpkg.exe list
29-
displayName: 'Install Dependencies'
29+
displayName: 'Install c++ dependencies via vcpkg'
3030
- script: |
3131
rmdir %VCPKG_ROOT%\downloads /S /Q
3232
rmdir %VCPKG_ROOT%\packages /S /Q

.dev/docker/env/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ENV DEBIAN_FRONTEND=noninteractive
66

77
RUN apt-get update \
88
&& apt-get install -y \
9+
ccache \
910
cmake \
1011
g++ \
1112
wget \

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ include("${PCL_SOURCE_DIR}/cmake/pcl_targets.cmake")
9595
include("${PCL_SOURCE_DIR}/cmake/pcl_options.cmake")
9696
include("${PCL_SOURCE_DIR}/cmake/clang-format.cmake")
9797

98+
if(${PCL_ENABLE_CCACHE})
99+
include (UseCompilerCache)
100+
UseCompilerCache(ccache REQUIRED)
101+
endif()
102+
98103
# Enable verbose timing display?
99104
if(CMAKE_TIMING_VERBOSE AND UNIX)
100105
set_property(GLOBAL PROPERTY RULE_MESSAGES OFF)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# .rst:
2+
# UseCompilerCache
3+
# --------
4+
#
5+
# This module provides a function to setup a compiler cache tool (default: ``ccache``)
6+
# Main function of interest is ``UseCompilerCache``
7+
#
8+
# Needs CMake 3.4 at least
9+
# Inspired from:
10+
# * https://crascit.com/2016/04/09/using-ccache-with-cmake/
11+
# * https://stackoverflow.com/a/36515503/
12+
# * https://gitlab.kitware.com/henryiii/cmake/blob/cache/Modules/UseCompilerCache.cmake
13+
14+
# .rst
15+
# pcl_ccache_compat_file_gen
16+
# -- Generates a wrapper file which launches the compiler commands using ccache.
17+
# This allows support for XCode and CCache < 3.3
18+
function(pcl_ccache_compat_file_gen FILE_NAME CCACHE_PROGRAM COMPILER)
19+
message(STATUS "${FILE_NAME} for ${CCACHE_PROGRAM} with ${COMPILER}")
20+
file(WRITE "${CMAKE_BINARY_DIR}/${FILE_NAME}" ""
21+
"#! /usr/bin/env sh\n"
22+
"\n"
23+
"# Xcode generator doesn't include the compiler as the\n"
24+
"# first argument, Ninja and Makefiles do. Handle both cases.\n"
25+
"if [ \"$1\" = \"${COMPILER}\" ] ; then\n"
26+
" shift\n"
27+
"fi\n"
28+
"\n"
29+
"export CCACHE_CPP2=true\n"
30+
"exec \"${CCACHE_PROGRAM}\" \"${COMPILER}\" \"$@\"\n")
31+
endfunction()
32+
33+
# .rst
34+
# UseCompilerCache([PROGRAM <ccache_name>] [QUIET] [REQUIRED])
35+
# -- Add the compiler cache tool (default to look for ccache on the path)
36+
# to your build through CMAKE_<LANG>_COMPILER_LAUNCHER variables. Also
37+
# supports XCode. Uses a wrapper for XCode and CCache < 3.3.
38+
# Sets the COMPILER_CACHE_VERSION variable.
39+
function(UseCompilerCache)
40+
set(options QUIET REQUIRED)
41+
set(oneValueArgs CCACHE)
42+
set(multiValueArgs)
43+
44+
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
45+
46+
if(NOT ARGS_CCACHE)
47+
set(ARGS_CCACHE ccache)
48+
endif()
49+
50+
find_program(CCACHE_PROGRAM ${ARGS_CCACHE})
51+
52+
# Quit if not found
53+
if(NOT CCACHE_PROGRAM)
54+
if(REQUIRED)
55+
message(FATAL_ERROR "Failed to find ${CCACHE_PROGRAM} (REQUIRED)")
56+
endif()
57+
return()
58+
endif()
59+
60+
if(CMAKE_GENERATOR MATCHES "Visual")
61+
message(FATAL_ERROR "MSVC isn't compatible with current solutions. Please rename compiler cache to cl.exe and prepend its location in env PATH variable")
62+
return()
63+
endif()
64+
65+
# Get version number
66+
execute_process(COMMAND "${CCACHE_PROGRAM}" --version OUTPUT_VARIABLE output)
67+
string(REPLACE "\n" ";" output "${output}")
68+
foreach(line ${output})
69+
string(TOLOWER ${line} line)
70+
string(REGEX REPLACE "^ccache version ([\\.0-9]+)$" "\\1" version "${line}")
71+
if(version)
72+
set(COMPILER_CACHE_VERSION ${version} PARENT_SCOPE)
73+
break()
74+
endif()
75+
endforeach()
76+
77+
if(NOT QUIET)
78+
message(STATUS "Using Compiler Cache (${CCACHE_PROGRAM}) v${version} in the C/C++ toolchain")
79+
endif()
80+
81+
set(xcode_compat FALSE)
82+
if(CMAKE_GENERATOR STREQUAL Xcode)
83+
set(xcode_compat TRUE)
84+
endif()
85+
set(ccache_compat FALSE)
86+
if((ARGS_CCACHE STREQUAL ccache) AND (version VERSION_LESS 3.3.0))
87+
set(ccache_compat TRUE)
88+
endif()
89+
90+
# Indirect wrapper is needed for CCache < 3.3 or XCode
91+
if(NOT (${xcode_compat} OR ${ccache_compat}))
92+
# Support Unix Makefiles and Ninja
93+
message(STATUS "Compiler cache via cmake launcher prefix")
94+
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" PARENT_SCOPE)
95+
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" PARENT_SCOPE)
96+
set(CMAKE_CUDA_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" PARENT_SCOPE)
97+
return()
98+
endif()
99+
100+
message(STATUS "Generating launch helpers for compiler cache")
101+
102+
pcl_ccache_compat_file_gen("launch-c" ${CCACHE_PROGRAM} ${CMAKE_C_COMPILER})
103+
pcl_ccache_compat_file_gen("launch-cxx" ${CCACHE_PROGRAM} ${CMAKE_CXX_COMPILER})
104+
execute_process(COMMAND chmod a+rx
105+
"${CMAKE_BINARY_DIR}/launch-c"
106+
"${CMAKE_BINARY_DIR}/launch-cxx")
107+
108+
# Cuda support only added in CMake 3.10
109+
set(cuda_supported FALSE)
110+
if (NOT (CMAKE_VERSION VERSION_LESS 3.10) AND CMAKE_CUDA_COMPILER)
111+
set(cuda_supported TRUE)
112+
endif()
113+
if(${cuda_supported})
114+
pcl_ccache_compat_file_gen("launch-cuda" ${CCACHE_PROGRAM} ${CMAKE_CUDA_COMPILER})
115+
execute_process(COMMAND chmod a+rx
116+
"${CMAKE_BINARY_DIR}/launch-cuda")
117+
endif()
118+
119+
if(${xcode_compat})
120+
# Set Xcode project attributes to route compilation and linking properly
121+
message(STATUS "Compiler cache via launch files to support XCode")
122+
set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/launch-c" PARENT_SCOPE)
123+
set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/launch-cxx" PARENT_SCOPE)
124+
set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/launch-c" PARENT_SCOPE)
125+
set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/launch-cxx" PARENT_SCOPE)
126+
else()
127+
message(STATUS "Compiler cache via launch files to support Unix Makefiles and Ninja")
128+
set(CMAKE_C_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/launch-c" PARENT_SCOPE)
129+
set(CMAKE_CXX_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/launch-cxx" PARENT_SCOPE)
130+
if (${cuda_supported})
131+
set(CMAKE_CUDA_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/launch-cuda" PARENT_SCOPE)
132+
endif()
133+
endif()
134+
endfunction()

cmake/pcl_options.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ mark_as_advanced(PCL_NO_PRECOMPILE)
4242
option(PCL_ENABLE_SSE "Enable or Disable SSE optimizations." ON)
4343
mark_as_advanced(PCL_ENABLE_SSE)
4444

45+
# Allow the user to disable ccache if ccache is available
46+
option(PCL_ENABLE_CCACHE "Enable using compiler cache for compilation" OFF)
47+
mark_as_advanced(PCL_ENABLE_CCACHE)
48+
4549
# Display timing information for each compiler instance on screen
4650
option(CMAKE_TIMING_VERBOSE "Enable the display of timing information for each compiler instance." OFF)
4751
mark_as_advanced(CMAKE_TIMING_VERBOSE)

0 commit comments

Comments
 (0)