Skip to content

Commit

Permalink
support build with 32bit wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanKung committed Jan 20, 2025
1 parent e2e2820 commit 67a2217
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
7 changes: 2 additions & 5 deletions dev-cli/container/src/ao-build-module
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def main():

# Inject rust files into c_program if language is rust
if(language == 'rust'):
c_program = inject_rust_files(definition, c_program)
c_program = inject_rust_files(definition, c_program, config)

# Inject lua files into c_program always to load lua files and replace placeholders
if(language == 'lua'):
Expand Down Expand Up @@ -159,10 +159,7 @@ def main():
# shell_exec(*meter_cmd)

if config.keep_js is False:
process_js_path = os.path.join(os.getcwd(), 'process.js')
# For rust case, js code is not exists
if os.path.exists(process_js_path):
shell_exec(*['rm', process_js_path])
shell_exec(*['rm', os.path.join(os.getcwd(), 'process.js')])

if __name__ == '__main__':
main()
Expand Down
8 changes: 5 additions & 3 deletions dev-cli/container/src/ao_module_lib/languages/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

from ao_module_lib.helper import shell_exec
from ao_module_lib.definition import Definition
from ao_module_lib.config import Config

RUST_DIR = '/opt/aorustlib'
def inject_rust_files(definition: Definition, c_program: str):
def inject_rust_files(definition: Definition, c_program: str, config: Config):

c_header_files = []
# rust_source_files = []
Expand Down Expand Up @@ -37,19 +38,20 @@ def inject_rust_files(definition: Definition, c_program: str):
# os.chdir(RUST_DIR)

# build rust code at '/src'
target = config.target == 64 and 'wasm64-unknown-unknown' or 'wasm32-unknown-unknown'
os.environ["RUSTFLAGS"] = "--cfg=web_sys_unstable_apis --Z wasm_c_abi=spec"
cmd = [
'cargo',
'+nightly',
'build',
'-Zbuild-std=std,panic_unwind,panic_abort',
# '-Zbuild-std-features=panic_immediate_abort',
'--target=wasm64-unknown-unknown',
f'--target={target}',
'--release'
]

shell_exec(*cmd)
rust_lib = glob.glob('/src/target/wasm64-unknown-unknown/release/*.a', recursive=True)[0]
rust_lib = glob.glob(f'/src/target/{target}/release/*.a', recursive=True)[0]
rust_lib = shutil.copy2(rust_lib, RUST_DIR)

cmd = [
Expand Down
5 changes: 4 additions & 1 deletion dev-cli/src/starters/rust/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all:build bindgen

build:
RUSTFLAGS="--cfg=web_sys_unstable_apis --Z wasm_c_abi=spec" \
RUSTFLAGS="--Z wasm_c_abi=spec " \
cargo +nightly build \
-Zbuild-std=std,panic_unwind,panic_abort \
--target=wasm64-unknown-unknown \
Expand All @@ -13,3 +13,6 @@ bindgen:
clean:
cargo clean
rm aorust.h

opt:
wasm-opt --post-emscripten -O3 --low-memory-unused process.wasm --enable-bulk-memory --enable-memory64 --no-validation -o process.wasm
16 changes: 8 additions & 8 deletions dev-cli/src/starters/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use core::ffi::c_char;
use core::ffi::CStr;
use dlmalloc::GlobalDlmalloc;

#[global_allocator]
static GLOBAL: GlobalDlmalloc = GlobalDlmalloc;
// #[global_allocator]
// static GLOBAL: GlobalDlmalloc = GlobalDlmalloc;

/// halt the thread on panic; messages are discarded:
#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
// /// halt the thread on panic; messages are discarded:
// #[cfg(not(test))]
// #[panic_handler]
// fn panic(_info: &core::panic::PanicInfo) -> ! {
// loop {}
// }

/// Processes the input `msg` and `env` strings, performs some operations,
/// and returns the result as a C-compatible string.
Expand Down

0 comments on commit 67a2217

Please sign in to comment.