77import difflib , os , re , shutil , random
88import tomli
99
10- def read_rust_version ():
11- toolchain_path = os .path .join (os .path .dirname (__file__ ),
12- '../../../../libcc2rs/rust-toolchain.toml' )
13- with open (toolchain_path , 'rb' ) as f :
14- return tomli .load (f )['toolchain' ]['channel' ]
15-
1610class Cpp2RustTest (TestFormat ):
1711 def __init__ (self ):
1812 self .regex_xfail = re .compile (r"//\s*XFAIL:\s*(.*)" )
1913 self .regex_panic = re .compile (r"//\s*panic\s*(?::\s*(.*))?$" , re .MULTILINE )
2014 self .regex_nocompile = re .compile (r"//\s*no-compile\s*(?::\s*(.*))?$" , re .MULTILINE )
2115 self .regex_nondet_result = re .compile (r"//\s*nondet-result\s*(?::\s*(.*))?$" , re .MULTILINE )
22- self .rust_version = read_rust_version ()
16+ self .rust_version = self . readRustVersion ()
2317 os .environ ['RUSTFLAGS' ] = '-Awarnings -A dangerous-implicit-autorefs'
2418
19+ def readRustVersion (self ):
20+ toolchain_path = os .path .join (os .path .dirname (__file__ ),
21+ '../../../../libcc2rs/rust-toolchain.toml' )
22+ with open (toolchain_path , 'rb' ) as f :
23+ return tomli .load (f )['toolchain' ]['channel' ]
24+
25+ def cargoEnv (self ):
26+ return dict (os .environ , CARGO_TARGET_DIR = os .path .abspath (self .sharedTargetDir ()))
27+
28+ def sharedTargetDir (self ):
29+ return os .path .abspath (os .path .join (
30+ os .path .dirname (__file__ ),
31+ '../../../../build/tmp/cargo-target' ))
32+
2533 def updateExpected (self , generated , expected_path ):
2634 os .makedirs (os .path .dirname (expected_path ), exist_ok = True )
2735 with open (expected_path , 'w' ) as f :
@@ -120,24 +128,32 @@ def fail(str, code = fail_code):
120128 fromfile = 'expected' , tofile = 'generated' ))
121129 return fail ('different output\n ' + diff )
122130
131+ pkg_name = "test_" + re .sub (r'[^a-zA-Z0-9_]' , '_' ,
132+ os .path .basename (tmp_dir ))
133+
123134 # Check if we can compile the rust file
124135 with open (tmp_dir + "/rust-toolchain.toml" , 'w' ) as f :
125136 f .write (f'[toolchain]\n channel = "{ self .rust_version } "\n ' )
126137 with open (tmp_dir + "/Cargo.toml" , 'w' ) as f :
127138 f .write (f"""
128139[package]
129- name = "test "
140+ name = "{ pkg_name } "
130141version = "0.1.0"
131142edition = "2021"
132143rust-version = "{ self .rust_version } "
133144
145+ [[bin]]
146+ name = "{ pkg_name } "
147+ path = "src/main.rs"
148+
134149[dependencies]
135150libc = "0.2.169"
136151libcc2rs = {{ path = "../../../libcc2rs" }}
152+ rules = {{ path = "../../../rules" }}
137153""" )
138154
139155 cmd = ['cargo' , 'build' , '--release' , '--quiet' ]
140- _ , err , returncode = lit .util .executeCommand (cmd , tmp_dir )
156+ _ , err , returncode = lit .util .executeCommand (cmd , tmp_dir , env = self . cargoEnv () )
141157 if should_not_compile :
142158 if returncode != 0 :
143159 shutil .rmtree (tmp_dir , True )
@@ -146,7 +162,7 @@ def fail(str, code = fail_code):
146162 if returncode != 0 :
147163 return fail ('cargo failed\n ' + err )
148164
149- rust_bin = tmp_dir + "/target/ release/test"
165+ rust_bin = os . path . join ( self . sharedTargetDir (), " release" , pkg_name )
150166
151167 if not skip_run :
152168 if should_panic :
0 commit comments