From 8bc3f7d726d18d2c76e5bcc64d4160ae950b2cae Mon Sep 17 00:00:00 2001
From: Christoph Junghans <christoph.junghans@gmail.com>
Date: Mon, 9 Jul 2018 15:06:10 -0600
Subject: [PATCH] Add ccache support (#3761)

If the user sets "ccache: true" in spack's config.yaml, Spack will use an available
ccache executable when compiling c/c++ code. This feature is disabled by default
(i.e. "ccache: false") and the documentation is updated with how to enable
ccache support
---
 etc/spack/defaults/config.yaml       |  4 ++++
 lib/spack/docs/config_yaml.rst       | 19 +++++++++++++++++++
 lib/spack/env/cc                     | 10 +++++++++-
 lib/spack/spack/build_environment.py |  8 ++++++++
 lib/spack/spack/schema/config.py     |  1 +
 5 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/etc/spack/defaults/config.yaml b/etc/spack/defaults/config.yaml
index e4914e3fa3..28628a3d49 100644
--- a/etc/spack/defaults/config.yaml
+++ b/etc/spack/defaults/config.yaml
@@ -90,3 +90,7 @@ config:
   # If set to 4, for example, `spack install` will run `make -j4`.
   # If not set, all available cores are used by default.
   # build_jobs: 4
+
+
+  # If set to true, spack will use ccache to cache c compiles.
+  ccache: false
diff --git a/lib/spack/docs/config_yaml.rst b/lib/spack/docs/config_yaml.rst
index 35bc22d72e..1db5679278 100644
--- a/lib/spack/docs/config_yaml.rst
+++ b/lib/spack/docs/config_yaml.rst
@@ -191,3 +191,22 @@ to 4, for example, commands like ``spack install`` will run ``make -j4``
 instead of hogging every core.
 
 To build all software in serial, set ``build_jobs`` to 1.
+
+--------------------
+``ccache``
+--------------------
+
+When set to ``true`` Spack will use ccache to cache compiles. This is
+useful specifically un two cases: (1) Use with ``spack setup``, (2)
+Build the same package with many different variants. The default is
+``false``.
+
+When enabled Spack will look inside your ``PATH`` for a ``ccache``
+executable and stop if it is not found. Some systems come with
+``ccache``, but it can also be installed using ``spack install
+ccache``. ``ccache`` comes with reasonable defaults for cache size
+and location. (See the *Configuration settings* secion of ``man
+ccache`` to learn more about the default settings and how change
+them.) Please note that we currently disable ccache's ``hash_dir``
+feature to avoid an issue with the stage directory (see
+https://github.com/LLNL/spack/pull/3761#issuecomment-294352232 ).
diff --git a/lib/spack/env/cc b/lib/spack/env/cc
index 22f6cb345c..bc4a20dc3e 100755
--- a/lib/spack/env/cc
+++ b/lib/spack/env/cc
@@ -342,7 +342,15 @@ case "$mode" in
         args=("${args[@]}" ${SPACK_LDLIBS[@]}) ;;
 esac
 
-full_command=("$command" "${args[@]}")
+#ccache only supports C languages, so filtering out Fortran
+if [[ ( ${lang_flags} = "C" || ${lang_flags} = "CXX" ) && ${SPACK_CCACHE_BINARY} ]]; then
+    full_command=("${SPACK_CCACHE_BINARY}" "$command" "${args[@]}")
+    # #3761#issuecomment-294352232
+    # workaround for stage being a temp folder
+    export CCACHE_NOHASHDIR=yes
+else
+   full_command=("$command" "${args[@]}")
+fi
 
 # In test command mode, write out full command for Spack tests.
 if [[ $SPACK_TEST_COMMAND == dump-args ]]; then
diff --git a/lib/spack/spack/build_environment.py b/lib/spack/spack/build_environment.py
index 4d272b51f3..e48e570e76 100644
--- a/lib/spack/spack/build_environment.py
+++ b/lib/spack/spack/build_environment.py
@@ -98,6 +98,7 @@
 SPACK_SHORT_SPEC = 'SPACK_SHORT_SPEC'
 SPACK_DEBUG_LOG_ID = 'SPACK_DEBUG_LOG_ID'
 SPACK_DEBUG_LOG_DIR = 'SPACK_DEBUG_LOG_DIR'
+SPACK_CCACHE_BINARY = 'SPACK_CCACHE_BINARY'
 
 
 # Platform-specific library suffix.
@@ -335,6 +336,13 @@ def set_build_environment_variables(pkg, env, dirty):
     env.set(SPACK_DEBUG_LOG_ID, pkg.spec.format('${PACKAGE}-${HASH:7}'))
     env.set(SPACK_DEBUG_LOG_DIR, spack.main.spack_working_dir)
 
+    # Find ccache binary and hand it to build environment
+    if spack.config.get('config:ccache'):
+        ccache = Executable('ccache')
+        if not ccache:
+            raise RuntimeError("No ccache binary found in PATH")
+        env.set(SPACK_CCACHE_BINARY, ccache)
+
     # Add any pkgconfig directories to PKG_CONFIG_PATH
     for prefix in build_link_prefixes:
         for directory in ('lib', 'lib64', 'share'):
diff --git a/lib/spack/spack/schema/config.py b/lib/spack/spack/schema/config.py
index 4a5b25ed63..ac94a12558 100644
--- a/lib/spack/spack/schema/config.py
+++ b/lib/spack/spack/schema/config.py
@@ -69,6 +69,7 @@
                 'locks': {'type': 'boolean'},
                 'dirty': {'type': 'boolean'},
                 'build_jobs': {'type': 'integer', 'minimum': 1},
+                'ccache': {'type': 'boolean'},
             }
         },
     },
-- 
GitLab