# ARM64 Cross-Compilation Setup

This guide explains how to set up ARM64 cross-compilation for building `msger` binaries that run on Raspberry Pi and other ARM64 Linux systems.

## Prerequisites

- WSL2 or Linux development environment
- Rust toolchain installed (`rustup`)
- Node.js and npm

## Quick Setup

Run this command to automatically install the ARM64 cross-compilation toolchain:

```bash
npm run setup:arm64
```

This will:
- Install GCC ARM64 cross-compiler
- Install required development libraries
- Add Rust ARM64 target (`aarch64-unknown-linux-gnu`)

## Manual Setup

If you prefer to install manually or the script doesn't work for your distribution:

### Ubuntu/Debian

```bash
sudo apt update
sudo apt install -y \
    gcc-aarch64-linux-gnu \
    g++-aarch64-linux-gnu \
    libc6-dev-arm64-cross \
    libwebkit2gtk-4.1-dev \
    libgtk-3-dev \
    pkg-config

rustup target add aarch64-unknown-linux-gnu
```

### Fedora/RHEL

```bash
sudo dnf install -y \
    gcc-aarch64-linux-gnu \
    gcc-c++-aarch64-linux-gnu \
    webkit2gtk4.1-devel \
    gtk3-devel \
    pkg-config

rustup target add aarch64-unknown-linux-gnu
```

### Arch Linux

```bash
sudo pacman -S --needed \
    aarch64-linux-gnu-gcc \
    webkit2gtk \
    gtk3 \
    pkg-config

rustup target add aarch64-unknown-linux-gnu
```

## Building ARM64 Binaries

Once the toolchain is set up, build the ARM64 binary:

```bash
npm run build:native:arm64
```

This creates `msgernative-linux-arm64` which can be deployed to Raspberry Pi or other ARM64 Linux systems.

## Testing on ARM64

To test the binary on a Raspberry Pi:

1. Copy the binary to your Pi:
   ```bash
   scp msgernative-linux-arm64 pi@raspberrypi.local:~/
   ```

2. SSH to the Pi and make it executable:
   ```bash
   ssh pi@raspberrypi.local
   chmod +x msgernative-linux-arm64
   ```

3. Install required libraries on the Pi:
   ```bash
   sudo apt install libwebkit2gtk-4.1-0 libgtk-3-0
   ```

4. Test it:
   ```bash
   echo '{"message":"Hello from ARM64!"}' | ./msgernative-linux-arm64
   ```

## Troubleshooting

### "linker 'aarch64-linux-gnu-gcc' not found"

The ARM64 cross-compiler is not installed. Run:
```bash
sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
```

### "error: failed to run custom build command for `webkit2gtk-sys`"

WebKit development libraries are missing:
```bash
sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev pkg-config
```

### Binary works locally but fails on Raspberry Pi

The Pi may be missing runtime libraries:
```bash
# On the Raspberry Pi:
sudo apt update
sudo apt install libwebkit2gtk-4.1-0 libgtk-3-0
```

Check what's missing with:
```bash
ldd ./msgernative-linux-arm64
```
