From 4210f839e2e96d208a202afd0803a9bccebb4567 Mon Sep 17 00:00:00 2001
From: Todd Gamblin <tgamblin@llnl.gov>
Date: Mon, 6 Aug 2018 11:26:19 -0700
Subject: cc: restore ccache support in the wrapper, add a regression test

- Add back ccache support to the wrapper.
- Add a regression test to make sure ccache is working properly.
---
 lib/spack/env/cc           | 15 +++++++++++++--
 lib/spack/spack/test/cc.py | 25 +++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/lib/spack/env/cc b/lib/spack/env/cc
index 5ffaf941e0..e8a0903d2a 100755
--- a/lib/spack/env/cc
+++ b/lib/spack/env/cc
@@ -481,8 +481,19 @@ for lib in "${libs[@]}"; do
     args+=("-l$lib");
 done
 
-full_command=("$command")
-full_command+=("${args[@]}")
+full_command=("$command" "${args[@]}")
+
+# prepend the ccache binary if we're using ccache
+if [ -n "$SPACK_CCACHE_BINARY" ]; then
+    case "$lang_flags" in
+        C|CXX)  # ccache only supports C languages
+            full_command=("${SPACK_CCACHE_BINARY}" "${full_command[@]}")
+            # workaround for stage being a temp folder
+            # see #3761#issuecomment-294352232
+            export CCACHE_NOHASHDIR=yes
+            ;;
+    esac
+fi
 
 # dump the full command if the caller supplies SPACK_TEST_COMMAND=dump-args
 if [[ $SPACK_TEST_COMMAND == dump-args ]]; then
diff --git a/lib/spack/spack/test/cc.py b/lib/spack/spack/test/cc.py
index 3d06890f55..517b7f1dd3 100644
--- a/lib/spack/spack/test/cc.py
+++ b/lib/spack/spack/test/cc.py
@@ -560,3 +560,28 @@ def test_ld_deps_partial(dep1):
             test_rpaths +
             ['-r'] +
             test_args_without_paths)
+
+
+def test_ccache_prepend_for_cc():
+    with set_env(SPACK_CCACHE_BINARY='ccache'):
+        check_cc(
+            'dump-args', test_args,
+            ['ccache'] +  # ccache prepended in cc mode
+            [real_cc] +
+            test_include_paths +
+            test_library_paths +
+            test_wl_rpaths +
+            pkg_wl_rpaths +
+            test_args_without_paths)
+
+
+def test_no_ccache_prepend_for_fc():
+    check_fc(
+        'dump-args', test_args,
+        # no ccache for Fortran
+        [real_cc] +
+        test_include_paths +
+        test_library_paths +
+        test_wl_rpaths +
+        pkg_wl_rpaths +
+        test_args_without_paths)
-- 
cgit v1.2.3-70-g09d2