|
| 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() |
0 commit comments