Skip to content

Commit b4490cf

Browse files
committed
Added the compatibility with the renamed target wasm32-wasip1
In January 2025 `rustc` started using the name `wasm32-wasip1` as replacement of `wasm32-wasi`. For additional information see: - https://doc.rust-lang.org/nightly/rustc/platform-support/wasm32-wasip1.html - rust-lang/compiler-team#607 - rust-lang/compiler-team#695
1 parent cfca020 commit b4490cf

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## installation
44

55
```bash
6-
python3 -m pip install rust-contracts-builder
6+
python -m pip install rust-contracts-builder
77
```
88

99
## init
@@ -19,3 +19,16 @@ cd hello
1919
rust-contract build --stack-size 8192
2020
```
2121

22+
# Development
23+
24+
To run the `tool` from the project folder:
25+
26+
```bash
27+
python -m pysrc.__main__ build --stack-size 8192
28+
```
29+
30+
To install the package from the project folder:
31+
32+
```bash
33+
pip install .
34+
```

pysrc/__init__.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,54 @@ def get_rustc_version():
4646
print(f"An error occurred while checking the rustc version: {e}")
4747
return None
4848

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+
4969
def build_contract(package_name, build_mode, target_dir, stack_size):
5070
os.environ['RUSTFLAGS'] = f'-C link-arg=-zstack-size={stack_size} -Clinker-plugin-lto'
5171
version = get_rustc_version()
72+
rust_target = get_rustc_wasi_target()
5273
os.environ['RUSTC_BOOTSTRAP'] = '1'
5374
print(f"RUSTC_BOOTSTRAP=\"{os.environ['RUSTC_BOOTSTRAP']}\"")
5475
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'
5677
print(cmd)
5778
cmd = shlex.split(cmd)
5879
ret_code = subprocess.call(cmd, stdout=sys.stdout, stderr=sys.stderr)
5980
if not ret_code == 0:
6081
sys.exit(ret_code)
6182

6283
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')
6485
except Exception as e:
6586
print_err(f'{e}')
6687
sys.exit(-1)
6788

6889
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'
7091
cmd = shlex.split(cmd)
7192
ret_code = subprocess.call(cmd, stdout=sys.stdout, stderr=sys.stderr)
7293
if not ret_code == 0:
7394
sys.exit(ret_code)
7495
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')
7697
print_warning('''
7798
wasm-opt not found! Make sure the binary is in your PATH environment.
7899
We use this tool to optimize the size of your contract's Wasm binary.

0 commit comments

Comments
 (0)