99from pathlib import Path
1010from typing import NamedTuple , Optional
1111import difflib
12+ import functools
13+ import json
1214import os
1315import re
1416import shlex
@@ -217,21 +219,7 @@ def build_cpp(self):
217219 def build_rust (self ):
218220 exp = self .expectations
219221
220- cc2rs_dir = BUILD_DIR / "libcc2rs-target" / "release"
221- libc_dep_deps = BUILD_DIR / "libc-dep-target" / "release" / "deps"
222- # pick the most recently compiled libc
223- libc_rlib = max (
224- libc_dep_deps .glob ("liblibc-*.rlib" ),
225- key = lambda p : p .stat ().st_mtime ,
226- )
227- nix_rlib = max (
228- libc_dep_deps .glob ("libnix-*.rlib" ),
229- key = lambda p : p .stat ().st_mtime ,
230- )
231- jiff_rlib = max (
232- libc_dep_deps .glob ("libjiff-*.rlib" ),
233- key = lambda p : p .stat ().st_mtime ,
234- )
222+ libs = read_cargo_artifacts ()
235223 cmd = [
236224 "rustc" ,
237225 "+" + read_rust_version (),
@@ -248,19 +236,11 @@ def build_rust(self):
248236 "panic=abort" ,
249237 "--out-dir" ,
250238 str (self .tmp_dir ),
251- "-L" ,
252- f"dependency={ cc2rs_dir / 'deps' } " ,
253- "-L" ,
254- f"dependency={ libc_dep_deps } " ,
255- "--extern" ,
256- f"libcc2rs={ cc2rs_dir / 'liblibcc2rs.rlib' } " ,
257- "--extern" ,
258- f"libc={ libc_rlib } " ,
259- "--extern" ,
260- f"nix={ nix_rlib } " ,
261- "--extern" ,
262- f"jiff={ jiff_rlib } " ,
263239 ]
240+ for lib_dir in sorted ({p .parent for p in libs .values ()}):
241+ cmd += ["-L" , f"dependency={ lib_dir } " ]
242+ for crate in ("libcc2rs" , "libc" , "nix" , "jiff" ):
243+ cmd += ["--extern" , f"{ crate } ={ libs [crate ]} " ]
264244 _ , err , returncode = lit .util .executeCommand (cmd , str (self .tmp_dir ))
265245 if exp .should_not_compile :
266246 if returncode != 0 :
@@ -403,6 +383,21 @@ def read_rust_version():
403383 raise Exception ("could not find rust version in " + toolchain_path )
404384
405385
386+ @functools .cache
387+ def read_cargo_artifacts ():
388+ libs = {}
389+ for crate in ("libc-dep" , "libcc2rs" ):
390+ path = BUILD_DIR / f"{ crate } -target" / "artifacts.json"
391+ for line in path .read_text ().splitlines ():
392+ msg = json .loads (line )
393+ if msg .get ("reason" ) != "compiler-artifact" :
394+ continue
395+ for f in map (Path , msg ["filenames" ]):
396+ if f .suffix in (".rlib" , ".so" , ".dylib" ):
397+ libs [msg ["target" ]["name" ]] = f
398+ return libs
399+
400+
406401def get_temp_dir ():
407402 shm = Path ("/dev/shm" )
408403 if shm .exists () and os .access (shm , os .W_OK ):
0 commit comments