pw_presubmit: Use ccache for faster repeat compiles

The same compilations are often run multiple times while fixing
presubmit errors locally. If ccache is available use that to avoid
recompiles during pw presubmit.

No-Docs-Update-Reason: Speed optimization
Change-Id: I4766a2c2284570d8c6c9e6c085579c7dc28fce7c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/50920
Pigweed-Auto-Submit: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Rob Mohr <mohrr@google.com>
diff --git a/pw_presubmit/py/pw_presubmit/build.py b/pw_presubmit/py/pw_presubmit/build.py
index 38622c1..3879cf3 100644
--- a/pw_presubmit/py/pw_presubmit/build.py
+++ b/pw_presubmit/py/pw_presubmit/build.py
@@ -22,6 +22,7 @@
 from pathlib import Path
 import re
 import subprocess
+from shutil import which
 from typing import (Collection, Container, Dict, Iterable, List, Mapping, Set,
                     Tuple, Union)
 
@@ -64,6 +65,10 @@
         # Fall-back case handles integers as well as strings that already
         # contain double quotation marks, or look like scopes or lists.
         transformed_args.append(f'{arg}={val}')
+    # Use ccache if available for faster repeat presubmit runs.
+    if which('ccache'):
+        transformed_args.append('pw_command_launcher="ccache"')
+
     return '--args=' + ' '.join(transformed_args)