[Bf-blender-cvs] [fdb7623e098] master: Unix/macOS: support building with Ccache

Matt Hill noreply at git.blender.org
Mon Dec 21 06:18:52 CET 2020


Commit: fdb7623e098fe431397815fc67667c5904074913
Author: Matt Hill
Date:   Mon Dec 21 10:47:35 2020 +0530
Branches: master
https://developer.blender.org/rBfdb7623e098fe431397815fc67667c5904074913

Unix/macOS: support building with Ccache

This adds an option (WITH_COMPILER_CCACHE) to build using Ccache if it's
found. Makefiles-based, Ninja-based and Xcode generators are supported.

Pass `-DWITH_COMPILER_CCACHE=ON` to cmake to enable Ccache.
Utility option in GNUmakefile is also added: for e.g.,
`make ninja ccache`.

Reviewed By: brecht, ankitm
Differential Revision: https://developer.blender.org/D9665

===================================================================

M	CMakeLists.txt
M	GNUmakefile
M	build_files/cmake/platform/platform_apple.cmake
M	build_files/cmake/platform/platform_apple_xcode.cmake
M	build_files/cmake/platform/platform_unix.cmake

===================================================================

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c8ae562f6ad..a630f0ae63d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -610,6 +610,11 @@ if(WIN32)
 
 endif()
 
+if(UNIX)
+  # See WITH_WINDOWS_SCCACHE for Windows.
+  option(WITH_COMPILER_CCACHE "Use ccache to improve rebuild times (Works with Ninja, Makefiles and Xcode)" OFF)
+endif()
+
 # The following only works with the Ninja generator in CMake >= 3.0.
 if("${CMAKE_GENERATOR}" MATCHES "Ninja")
   option(WITH_NINJA_POOL_JOBS
diff --git a/GNUmakefile b/GNUmakefile
index 1a462b7a351..3b5b9d65521 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -41,6 +41,7 @@ Convenience Targets
    * developer:     Enable faster builds, error checking and tests, recommended for developers.
    * config:        Run cmake configuration tool to set build options.
    * ninja:         Use ninja build tool for faster builds.
+   * ccache:        Use ccache for faster rebuilds.
 
    Note: passing the argument 'BUILD_DIR=path' when calling make will override the default build dir.
    Note: passing the argument 'BUILD_CMAKE_ARGS=args' lets you add cmake arguments.
@@ -241,6 +242,10 @@ ifneq "$(findstring developer, $(MAKECMDGOALS))" ""
 	CMAKE_CONFIG_ARGS:=-C"$(BLENDER_DIR)/build_files/cmake/config/blender_developer.cmake" $(CMAKE_CONFIG_ARGS)
 endif
 
+ifneq "$(findstring ccache, $(MAKECMDGOALS))" ""
+	CMAKE_CONFIG_ARGS:=-DWITH_COMPILER_CCACHE=YES $(CMAKE_CONFIG_ARGS)
+endif
+
 # -----------------------------------------------------------------------------
 # build tool
 
@@ -340,6 +345,7 @@ headless: all
 bpy: all
 developer: all
 ninja: all
+ccache: all
 
 # -----------------------------------------------------------------------------
 # Build dependencies
diff --git a/build_files/cmake/platform/platform_apple.cmake b/build_files/cmake/platform/platform_apple.cmake
index 6baa2f3d855..31da5292eaf 100644
--- a/build_files/cmake/platform/platform_apple.cmake
+++ b/build_files/cmake/platform/platform_apple.cmake
@@ -470,3 +470,17 @@ set(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
 set(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
+
+if(WITH_COMPILER_CCACHE)
+  if(NOT CMAKE_GENERATOR STREQUAL "Xcode")
+    find_program(CCACHE_PROGRAM ccache)
+    if(CCACHE_PROGRAM)
+      # Makefiles and ninja
+      set(CMAKE_C_COMPILER_LAUNCHER   "${CCACHE_PROGRAM}" CACHE STRING "" FORCE)
+      set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" CACHE STRING "" FORCE)
+    else()
+      message(WARNING "Ccache NOT found, disabling WITH_COMPILER_CCACHE")
+      set(WITH_COMPILER_CCACHE OFF)
+    endif()
+  endif()
+endif()
diff --git a/build_files/cmake/platform/platform_apple_xcode.cmake b/build_files/cmake/platform/platform_apple_xcode.cmake
index e4b804fc4ea..f12de540353 100644
--- a/build_files/cmake/platform/platform_apple_xcode.cmake
+++ b/build_files/cmake/platform/platform_apple_xcode.cmake
@@ -154,3 +154,32 @@ if(NOT ${CMAKE_GENERATOR} MATCHES "Xcode")
   string(APPEND CMAKE_CXX_FLAGS " -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
   add_definitions("-DMACOSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}")
 endif()
+
+if(WITH_COMPILER_CCACHE)
+  if(CMAKE_GENERATOR STREQUAL "Xcode")
+    find_program(CCACHE_PROGRAM ccache)
+    if(CCACHE_PROGRAM)
+      get_filename_component(ccompiler "${CMAKE_C_COMPILER}" NAME)
+      get_filename_component(cxxcompiler "${CMAKE_CXX_COMPILER}" NAME)
+      # Ccache can figure out which compiler to use if it's invoked from
+      # a symlink with the name of the compiler.
+      # https://ccache.dev/manual/4.1.html#_run_modes
+      set(_fake_compiler_dir "${CMAKE_BINARY_DIR}/ccache")
+      execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${_fake_compiler_dir})
+      set(_fake_C_COMPILER "${_fake_compiler_dir}/${ccompiler}")
+      set(_fake_CXX_COMPILER "${_fake_compiler_dir}/${cxxcompiler}")
+      execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${CCACHE_PROGRAM}" ${_fake_C_COMPILER})
+      execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${CCACHE_PROGRAM}" ${_fake_CXX_COMPILER})
+      set(CMAKE_XCODE_ATTRIBUTE_CC         ${_fake_C_COMPILER} CACHE STRING "" FORCE)
+      set(CMAKE_XCODE_ATTRIBUTE_CXX        ${_fake_CXX_COMPILER} CACHE STRING "" FORCE)
+      set(CMAKE_XCODE_ATTRIBUTE_LD         ${_fake_C_COMPILER} CACHE STRING "" FORCE)
+      set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS ${_fake_CXX_COMPILER} CACHE STRING "" FORCE)
+      unset(_fake_compiler_dir)
+      unset(_fake_C_COMPILER)
+      unset(_fake_CXX_COMPILER)
+    else()
+      message(WARNING "Ccache NOT found, disabling WITH_COMPILER_CCACHE")
+      set(WITH_COMPILER_CCACHE OFF)
+    endif()
+  endif()
+endif()
diff --git a/build_files/cmake/platform/platform_unix.cmake b/build_files/cmake/platform/platform_unix.cmake
index c4d1383c767..b304e89d74f 100644
--- a/build_files/cmake/platform/platform_unix.cmake
+++ b/build_files/cmake/platform/platform_unix.cmake
@@ -684,3 +684,15 @@ set(PLATFORM_LINKFLAGS
 if(WITH_INSTALL_PORTABLE)
   string(APPEND CMAKE_EXE_LINKER_FLAGS " -no-pie")
 endif()
+
+if(WITH_COMPILER_CCACHE)
+  find_program(CCACHE_PROGRAM ccache)
+  if(CCACHE_PROGRAM)
+    # Makefiles and ninja
+    set(CMAKE_C_COMPILER_LAUNCHER   "${CCACHE_PROGRAM}" CACHE STRING "" FORCE)
+    set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" CACHE STRING "" FORCE)
+  else()
+    message(WARNING "Ccache NOT found, disabling WITH_COMPILER_CCACHE")
+    set(WITH_COMPILER_CCACHE OFF)
+  endif()
+endif()



More information about the Bf-blender-cvs mailing list