@@ -122,6 +122,13 @@ def cmake(cwd, targets, options=None, cflags=None):
122122 __tracebackhide__ = True
123123 if options is None :
124124 options = {}
125+ if os .environ .get ("USE_CCACHE" ):
126+ options .update (
127+ {
128+ "CMAKE_C_COMPILER_LAUNCHER" : "ccache" ,
129+ "CMAKE_CXX_COMPILER_LAUNCHER" : "ccache" ,
130+ }
131+ )
125132 options .update (
126133 {
127134 "CMAKE_RUNTIME_OUTPUT_DIRECTORY" : cwd ,
@@ -167,6 +174,12 @@ def cmake(cwd, targets, options=None, cflags=None):
167174 configure_llvm_cov (config_cmd )
168175 env = dict (os .environ )
169176 env ["CFLAGS" ] = env ["CXXFLAGS" ] = " " .join (cflags )
177+ if env .get ("USE_CCACHE" ):
178+ # Each pytest run builds in a new temp directory. Paths are normalized
179+ # relative to the build dir and CWD hashing is skipped to allow ccache
180+ # hits across runs.
181+ env .setdefault ("CCACHE_BASEDIR" , str (cwd ))
182+ env .setdefault ("CCACHE_NOHASHDIR" , "true" )
170183
171184 config_cmd .append (source_dir )
172185
@@ -206,7 +219,7 @@ def cmake(cwd, targets, options=None, cflags=None):
206219
207220 print ("{} > {}" .format (cwd , " " .join (buildcmd )), flush = True )
208221 try :
209- subprocess .run (buildcmd , cwd = cwd , check = True )
222+ subprocess .run (buildcmd , cwd = cwd , env = env , check = True )
210223 except subprocess .CalledProcessError :
211224 raise pytest .fail .Exception ("cmake build failed" ) from None
212225
0 commit comments