diff --git a/aclocal.m4 b/aclocal.m4
index 3522df4442827c1b5339cce9b5f563cefc70da09..512751593d8922747e4a04d098c307eaf8358f08 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -8,7 +8,6 @@ builtin(include, build/autoconf/acwinpaths.m4)dnl
 builtin(include, build/autoconf/hooks.m4)dnl
 builtin(include, build/autoconf/config.status.m4)dnl
 builtin(include, build/autoconf/toolchain.m4)dnl
-builtin(include, build/autoconf/ccache.m4)dnl
 builtin(include, build/autoconf/wrapper.m4)dnl
 builtin(include, build/autoconf/nspr.m4)dnl
 builtin(include, build/autoconf/nspr-build.m4)dnl
diff --git a/build/autoconf/ccache.m4 b/build/autoconf/ccache.m4
deleted file mode 100644
index 5e24691955be2bea21c48b019a6ea3134d8db63b..0000000000000000000000000000000000000000
--- a/build/autoconf/ccache.m4
+++ /dev/null
@@ -1,36 +0,0 @@
-dnl This Source Code Form is subject to the terms of the Mozilla Public
-dnl License, v. 2.0. If a copy of the MPL was not distributed with this
-dnl file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
-dnl ======================================================
-dnl = Enable compiling with ccache
-dnl ======================================================
-AC_DEFUN([MOZ_CHECK_CCACHE],
-[
-MOZ_ARG_WITH_STRING(ccache,
-[  --with-ccache[=path/to/ccache]
-                          Enable compiling with ccache],
-    CCACHE=$withval, CCACHE="no")
-
-if test "$CCACHE" != "no"; then
-    if test -z "$CCACHE" -o "$CCACHE" = "yes"; then
-        CCACHE=
-    else
-        if test ! -e "$CCACHE"; then
-            AC_MSG_ERROR([$CCACHE not found])
-        fi
-    fi
-    MOZ_PATH_PROGS(CCACHE, $CCACHE ccache)
-    if test -z "$CCACHE" -o "$CCACHE" = ":"; then
-        AC_MSG_ERROR([ccache not found])
-    elif test -x "$CCACHE"; then
-        CC="$CCACHE $CC"
-        CXX="$CCACHE $CXX"
-        MOZ_USING_CCACHE=1
-    else
-        AC_MSG_ERROR([$CCACHE is not executable])
-    fi
-fi
-
-AC_SUBST(MOZ_USING_CCACHE)
-])
diff --git a/build/autoconf/nspr-build.m4 b/build/autoconf/nspr-build.m4
index dc882800f1f5018c02ba401265a9dc6ccefd72ff..305f13ab7d79e975b1f0c6ab8d9d629e3a375bbb 100644
--- a/build/autoconf/nspr-build.m4
+++ b/build/autoconf/nspr-build.m4
@@ -197,6 +197,11 @@ AC_DEFUN([MOZ_SUBCONFIGURE_NSPR], [
 
 if test -z "$MOZ_NATIVE_NSPR"; then
     ac_configure_args="$_SUBDIR_CONFIG_ARGS --with-dist-prefix=$MOZ_BUILD_ROOT/dist --with-mozilla"
+    if test -n "$MOZ_USING_CCACHE"; then
+        # Avoid double prepending ccache by omitting --with-ccache in building NSPR.
+        ac_configure_args="`echo $ac_configure_args | sed -e 's/--with-ccache[[^ ]]*//'`"
+    fi
+
     if test -z "$MOZ_DEBUG"; then
         ac_configure_args="$ac_configure_args --disable-debug"
     else
diff --git a/build/autoconf/wrapper.m4 b/build/autoconf/wrapper.m4
index a61df70c8b43f2bc741805671e70d6532ad42e08..50f3ce28325a9c244101c87160445c1650f840f8 100644
--- a/build/autoconf/wrapper.m4
+++ b/build/autoconf/wrapper.m4
@@ -12,6 +12,36 @@ MOZ_ARG_WITH_STRING(compiler_wrapper,
     Enable compiling with wrappers such as distcc and ccache],
     COMPILER_WRAPPER=$withval, COMPILER_WRAPPER="no")
 
+MOZ_ARG_WITH_STRING(ccache,
+[  --with-ccache[=path/to/ccache]
+                          Enable compiling with ccache],
+    CCACHE=$withval, CCACHE="no")
+
+if test "$CCACHE" != "no"; then
+    if test -z "$CCACHE" -o "$CCACHE" = "yes"; then
+        CCACHE=
+    else
+        if test ! -e "$CCACHE"; then
+            AC_MSG_ERROR([$CCACHE not found])
+        fi
+    fi
+    MOZ_PATH_PROGS(CCACHE, $CCACHE ccache)
+    if test -z "$CCACHE" -o "$CCACHE" = ":"; then
+        AC_MSG_ERROR([ccache not found])
+    elif test -x "$CCACHE"; then
+        if test "$COMPILER_WRAPPER" != "no"; then
+            COMPILER_WRAPPER="$CCACHE $COMPILER_WRAPPER"
+        else
+            COMPILER_WRAPPER="$CCACHE"
+        fi
+        MOZ_USING_CCACHE=1
+    else
+        AC_MSG_ERROR([$CCACHE is not executable])
+    fi
+fi
+
+AC_SUBST(MOZ_USING_CCACHE)
+
 if test "$COMPILER_WRAPPER" != "no"; then
     case "$target" in
     *-mingw*)
diff --git a/configure.in b/configure.in
index fa6a1bf1dfc27ea7310a86aa639fce33a805d599..69e25a0fd5a16703906321058a21cb5b48101871 100644
--- a/configure.in
+++ b/configure.in
@@ -7327,8 +7327,6 @@ if test -n "$JS_GC_ZEAL" -o -n "$MOZ_DEBUG"; then
     AC_DEFINE(JS_GC_ZEAL)
 fi
 
-MOZ_CHECK_CCACHE
-
 dnl ========================================================
 dnl = Enable static checking using gcc-dehydra
 dnl ========================================================
@@ -9192,11 +9190,6 @@ if test "$COMPILE_ENVIRONMENT" -a -z "$LIBXUL_SDK_DIR"; then
 
 export WRAP_LDFLAGS
 
-if test -n "$MOZ_USING_CCACHE"; then
-    # Avoid double prepending ccache by omitting --with-ccache in building NSPR.
-    _SUBDIR_CONFIG_ARGS="`echo $_SUBDIR_CONFIG_ARGS | sed -e 's/--with-ccache[[^ ]]*//'`"
-fi
-
 MOZ_SUBCONFIGURE_NSPR()
 
 dnl ========================================================
diff --git a/js/src/aclocal.m4 b/js/src/aclocal.m4
index b92e07b29409144c01d0be882dbde7d89190245c..1ec2da493de6ef25273e7f61cd5ce869225234e5 100644
--- a/js/src/aclocal.m4
+++ b/js/src/aclocal.m4
@@ -8,7 +8,6 @@ builtin(include, ../../build/autoconf/acwinpaths.m4)dnl
 builtin(include, ../../build/autoconf/hooks.m4)dnl
 builtin(include, ../../build/autoconf/config.status.m4)dnl
 builtin(include, ../../build/autoconf/toolchain.m4)dnl
-builtin(include, ../../build/autoconf/ccache.m4)dnl
 builtin(include, ../../build/autoconf/wrapper.m4)dnl
 builtin(include, ../../build/autoconf/pkg.m4)dnl
 builtin(include, ../../build/autoconf/nspr.m4)dnl
diff --git a/js/src/configure.in b/js/src/configure.in
index 894f58d35be0e09eeb7708fa27731fac87c7d2f7..73b6378332dc1d1ebfb82f821ee76402c3abbca2 100644
--- a/js/src/configure.in
+++ b/js/src/configure.in
@@ -3442,8 +3442,6 @@ if test -n "$JS_OOM_BREAKPOINT"; then
     AC_DEFINE(JS_OOM_BREAKPOINT)
 fi
 
-MOZ_CHECK_CCACHE
-
 dnl ========================================================
 dnl = Enable static checking using gcc-dehydra
 dnl ========================================================