00
:
00
:
00
:
00
Corso SEO AI - Usa SEOEMAIL al checkout per il 30% di sconto

Installazione Toolchain

Toolchain per WebAssembly

Ogni linguaggio sorgente ha la propria toolchain per produrre file .wasm. Ecco come configurare quelle principali.

Emscripten (C/C++)

Emscripten è il compilatore più maturo per portare codice C/C++ su WebAssembly.

# Clonare il repository
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk

# Installare l'ultima versione
./emsdk install latest
./emsdk activate latest

# Attivare le variabili d'ambiente (Linux/macOS)
source ./emsdk_env.sh

# Su Windows (PowerShell)
.\emsdk_env.bat

Verifica l’installazione:

emcc --version
# emcc (Emscripten gcc/clang-like replacement) 3.x.x

Compilazione rapida:

# Compilare un file C in Wasm + glue JS
emcc hello.c -o hello.js -s WASM=1

# Solo il modulo .wasm (standalone)
emcc hello.c -o hello.wasm --no-entry

wasm-pack e wasm-bindgen (Rust)

L’ecosistema Rust offre la toolchain più integrata per Wasm.

# 1. Installare Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 2. Aggiungere il target wasm32
rustup target add wasm32-unknown-unknown

# 3. Installare wasm-pack
cargo install wasm-pack

Uso tipico:

# Creare un progetto
cargo new --lib my-wasm-lib
cd my-wasm-lib

# Compilare per il web
wasm-pack build --target web

# Compilare per bundler (webpack, vite)
wasm-pack build --target bundler

# Compilare per Node.js
wasm-pack build --target nodejs

AssemblyScript

Per chi proviene dal mondo TypeScript, AssemblyScript è il percorso più accessibile.

# Creare un progetto Node
mkdir my-as-project && cd my-as-project
npm init -y

# Installare AssemblyScript
npm install --save-dev assemblyscript

# Inizializzare la struttura del progetto
npx asinit .

Struttura generata:

my-as-project/
├── assembly/
│   ├── index.ts      ← codice sorgente
│   └── tsconfig.json
├── build/            ← output .wasm
└── package.json

Compilare:

# Build di debug
npx asc assembly/index.ts -o build/debug.wasm --debug

# Build ottimizzata
npx asc assembly/index.ts -o build/release.wasm --optimize

TinyGo (Go)

TinyGo produce binari Wasm molto più piccoli rispetto al compilatore Go standard.

# macOS
brew install tinygo

# Linux (Ubuntu/Debian)
wget https://github.com/tinygo-org/tinygo/releases/download/v0.31.0/tinygo_0.31.0_amd64.deb
sudo dpkg -i tinygo_0.31.0_amd64.deb

# Windows (scoop)
scoop install tinygo

Compilare con TinyGo:

tinygo build -o main.wasm -target wasm ./main.go

Compilare con Go standard:

GOOS=js GOARCH=wasm go build -o main.wasm main.go

WABT (WebAssembly Binary Toolkit)

Strumenti essenziali per lavorare direttamente con il formato WAT/Wasm.

# macOS
brew install wabt

# Linux (Ubuntu)
sudo apt install wabt

# Windows (scoop)
scoop install wabt

Strumenti inclusi:

# Convertire WAT → Wasm
wat2wasm module.wat -o module.wasm

# Convertire Wasm → WAT (decompilare)
wasm2wat module.wasm -o module.wat

# Validare un modulo
wasm-validate module.wasm

# Ispezionare le sezioni
wasm-objdump -x module.wasm

# Eseguire direttamente (interprete)
wasm-interp module.wasm --run-all-exports

wasm-opt (Binaryen)

Ottimizzatore standalone per ridurre la dimensione e migliorare le prestazioni:

# Installare binaryen
npm install -g binaryen

# Ottimizzare un modulo
wasm-opt -O3 input.wasm -o output.wasm

# Ottimizzazione aggressiva della dimensione
wasm-opt -Oz input.wasm -o output.wasm
Linguaggio Toolchain Comando di build
C/C++ Emscripten emcc file.c -o file.wasm
Rust wasm-pack wasm-pack build --target web
AssemblyScript asc npx asc index.ts -o out.wasm
Go TinyGo tinygo build -target wasm
WAT WABT wat2wasm file.wat