@@ -46,33 +46,54 @@ def get_rustc_version():
46
46
print (f"An error occurred while checking the rustc version: { e } " )
47
47
return None
48
48
49
+ def get_rustc_wasi_target ():
50
+ try :
51
+ # Get the list of targets supported by `rustc`
52
+ result = subprocess .run (["rustc" , "--print" , "target-list" ], capture_output = True , text = True )
53
+ targets = result .stdout .strip ().splitlines ()
54
+
55
+ # Filter the `wasi` targets
56
+ filtered_targets = [target for target in targets if "wasm32-wasi" in target ]
57
+
58
+ if "wasm32-wasip1" in filtered_targets :
59
+ return "wasm32-wasip1"
60
+ elif "wasm32-wasi" in filtered_targets :
61
+ return "wasm32-wasi"
62
+ else :
63
+ print (f"Could not find the `wasm32-wasi` target in the list of supported targets (see `rustc --print target-list`)" )
64
+ return None
65
+ except Exception as e :
66
+ print (f"An error occurred while checking the rustc targets: { e } " )
67
+ return None
68
+
49
69
def build_contract (package_name , build_mode , target_dir , stack_size ):
50
70
os .environ ['RUSTFLAGS' ] = f'-C link-arg=-zstack-size={ stack_size } -Clinker-plugin-lto'
51
71
version = get_rustc_version ()
72
+ rust_target = get_rustc_wasi_target ()
52
73
os .environ ['RUSTC_BOOTSTRAP' ] = '1'
53
74
print (f"RUSTC_BOOTSTRAP=\" { os .environ ['RUSTC_BOOTSTRAP' ]} \" " )
54
75
print (f"RUSTFLAGS=\" { os .environ ['RUSTFLAGS' ]} \" " )
55
- cmd = fr'cargo build --target=wasm32-wasi --target-dir={ target_dir } -Zbuild-std --no-default-features { build_mode } -Zbuild-std-features=panic_immediate_abort'
76
+ cmd = fr'cargo build --target={ rust_target } --target-dir={ target_dir } -Zbuild-std --no-default-features { build_mode } -Zbuild-std-features=panic_immediate_abort'
56
77
print (cmd )
57
78
cmd = shlex .split (cmd )
58
79
ret_code = subprocess .call (cmd , stdout = sys .stdout , stderr = sys .stderr )
59
80
if not ret_code == 0 :
60
81
sys .exit (ret_code )
61
82
62
83
try :
63
- check_import_section (f'{ target_dir } /wasm32-wasi /release/{ package_name } .wasm' )
84
+ check_import_section (f'{ target_dir } /{ rust_target } /release/{ package_name } .wasm' )
64
85
except Exception as e :
65
86
print_err (f'{ e } ' )
66
87
sys .exit (- 1 )
67
88
68
89
if shutil .which ('wasm-opt' ):
69
- cmd = f'wasm-opt { target_dir } /wasm32-wasi /release/{ package_name } .wasm --signext-lowering -O3 --strip-debug -o { target_dir } /{ package_name } .wasm'
90
+ cmd = f'wasm-opt { target_dir } /{ rust_target } /release/{ package_name } .wasm --signext-lowering -O3 --strip-debug -o { target_dir } /{ package_name } .wasm'
70
91
cmd = shlex .split (cmd )
71
92
ret_code = subprocess .call (cmd , stdout = sys .stdout , stderr = sys .stderr )
72
93
if not ret_code == 0 :
73
94
sys .exit (ret_code )
74
95
else :
75
- shutil .copy (f'{ target_dir } /wasm32-wasi /release/{ package_name } .wasm' , f'{ target_dir } /{ package_name } .wasm' )
96
+ shutil .copy (f'{ target_dir } /{ rust_target } /release/{ package_name } .wasm' , f'{ target_dir } /{ package_name } .wasm' )
76
97
print_warning ('''
77
98
wasm-opt not found! Make sure the binary is in your PATH environment.
78
99
We use this tool to optimize the size of your contract's Wasm binary.
0 commit comments