# ๐Ÿ—๏ธ Nyash Build Guide - Complete Build Patterns Everything you need to know about building Nyash for different platforms and configurations. ## ๐Ÿ“š Table of Contents 1. [Quick Start](#quick-start) 2. [Build Patterns Overview](#build-patterns-overview) 3. [Platform-Specific Builds](#platform-specific-builds) 4. [Plugin Development](#plugin-development) 5. [Distribution Packages](#distribution-packages) 6. [Troubleshooting](#troubleshooting) ## ๐Ÿš€ Quick Start ### Basic Build (Linux/WSL) ```bash # Standard build cargo build --release -j32 # Run ./target/release/nyash program.nyash ``` ### Windows Cross-Compile from WSL ```bash # Install cargo-xwin cargo install cargo-xwin # Build for Windows cargo xwin build --target x86_64-pc-windows-msvc --release ``` ## ๐Ÿ“Š Build Patterns Overview | Pattern | Command | Output | Use Case | |---------|---------|--------|----------| | **Standard** | `cargo build --release` | Linux/macOS binary | Local development | | **Windows Cross** | `cargo xwin build --target x86_64-pc-windows-msvc` | Windows .exe | Windows distribution | | **WebAssembly** | `wasm-pack build --target web` | .wasm + JS | Browser deployment | | **AOT Native** | `./tools/build_aot.sh program.nyash` | Standalone executable | High-performance deployment | | **Plugins** | `cargo build --release` (in plugin dir) | .so/.dll/.dylib | Extending Nyash | ## ๐Ÿ–ฅ๏ธ Platform-Specific Builds ### ๐Ÿง Linux/WSL Build Standard Rust build process: ```bash # Debug build cargo build # Release build (recommended) cargo build --release -j32 # With specific features cargo build --release --features cranelift-jit ``` ### ๐ŸชŸ Windows Build #### Prerequisites (Native Windows) - Rust MSVC toolchain (host = x86_64-pc-windows-msvc) - Visual Studio Build Tools (C++ ใƒ‡ใ‚นใ‚ฏใƒˆใƒƒใƒ—้–‹็™บ) - LLVM/clang (AOTใƒชใƒณใ‚ฏใซๆŽจๅฅจ) - PowerShell ๅฎŸ่กŒ่จฑๅฏ๏ผˆ`-ExecutionPolicy Bypass`ใงไธ€ๆ™‚ๅ›ž้ฟๅฏ๏ผ‰ #### Option 1: Cross-compile from Linux/WSL (Recommended) ```bash # One-time setup cargo install cargo-xwin # Build Windows binary cargo xwin build --target x86_64-pc-windows-msvc --release # Output: target/x86_64-pc-windows-msvc/release/nyash.exe ``` #### Option 2: Native Windows Build Requirements: - Rust with MSVC toolchain - Visual Studio Build Tools (C++ Desktop Development) - LLVM/clang (optional, for AOT) ```bash # On Windows cargo build --release ``` #### AOT on Windows (Native EXE) ```powershell # Requires Cranelift + (clang ใพใŸใฏ MSYS2/WSL ใฎ bash+cc) cargo build --release --features cranelift-jit powershell -ExecutionPolicy Bypass -File tools\build_aot.ps1 -Input examples\aot_min_string_len.nyash -Out app.exe ./app.exe ``` Notes: - EXEใฏใพใšๅฎŸ่กŒใƒ•ใ‚กใ‚คใƒซใจๅŒใ˜ใƒ•ใ‚ฉใƒซใƒ€ใฎ `nyash.toml` ใ‚’ๆŽขใ—ใพใ™ใ€‚ใชใ‘ใ‚Œใฐใ‚ซใƒฌใƒณใƒˆใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’ๅ‚็…งใ—ใพใ™ใ€‚ - ่ฟฝๅŠ ใƒญใ‚ฐใฏ `-v` ใ‚‚ใ—ใใฏ `set NYASH_CLI_VERBOSE=1` ใง่กจ็คบใ€‚ - ใƒ—ใƒฉใ‚ฐใ‚คใƒณใฎไพๅญ˜DLLใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ๅ„ใƒ—ใƒฉใ‚ฐใ‚คใƒณDLLใจๅŒใ˜ใƒ•ใ‚ฉใƒซใƒ€ใซ้…็ฝฎใ™ใ‚‹ใ‹ใ€`PATH` ใซ่ฟฝๅŠ ใ—ใฆใใ ใ•ใ„ใ€‚ ### ๐ŸŒ WebAssembly Build Two types of WASM builds: #### 1. Nyash Interpreter in Browser ```bash # Build Nyash itself as WASM wasm-pack build --target web # Files generated in pkg/ # - nyash_rust_bg.wasm # - nyash_rust.js # - nyash_rust.d.ts ``` #### 2. Compile Nyash Code to WASM ```bash # Compile .nyash to .wat (WebAssembly Text) ./target/release/nyash --compile-wasm program.nyash -o output.wat # Convert to binary WASM (requires wabt) wat2wasm output.wat -o output.wasm ``` ### ๐Ÿš€ AOT (Ahead-of-Time) Native Compilation Compile Nyash programs to standalone native executables: #### Linux/WSL ```bash # Build with Cranelift support cargo build --release --features cranelift-jit # Compile to native ./tools/build_aot.sh program.nyash -o app ./app # Standalone executable! ``` #### Windows ```powershell # From PowerShell cargo build --release --features cranelift-jit powershell -ExecutionPolicy Bypass -File tools\build_aot.ps1 -Input program.nyash -Out app.exe .\app.exe ``` ## ๐Ÿ”Œ Plugin Development ### Building Plugins Plugins must be built for each target platform: #### Linux Plugin (.so) ```bash cd plugins/nyash-example-plugin cargo build --release # Output: target/release/libnyash_example_plugin.so ``` #### Windows Plugin (.dll) ```bash # From WSL cd plugins/nyash-example-plugin cargo xwin build --target x86_64-pc-windows-msvc --release # Output: target/x86_64-pc-windows-msvc/release/nyash_example_plugin.dll ``` #### macOS Plugin (.dylib) ```bash cd plugins/nyash-example-plugin cargo build --release # Output: target/release/libnyash_example_plugin.dylib ``` ### Plugin Naming Convention **Important**: Windows removes the `lib` prefix automatically: - Linux/macOS: `libnyash_example_plugin.so/dylib` - Windows: `nyash_example_plugin.dll` The plugin loader handles this automatically with platform-agnostic configuration. ## ๐Ÿ“ฆ Distribution Packages ### Creating a Windows Distribution Perfect for sharing Nyash applications: ```bash # 1. Build main executable cargo xwin build --target x86_64-pc-windows-msvc --release # 2. Build plugins for plugin in filebox array map string integer; do (cd plugins/nyash-$plugin-plugin && \ cargo xwin build --target x86_64-pc-windows-msvc --release) done # 3. Create distribution structure mkdir -p dist/plugins cp target/x86_64-pc-windows-msvc/release/nyash.exe dist/ cp nyash.toml dist/ # 4. Copy plugin DLLs cp plugins/*/target/x86_64-pc-windows-msvc/release/*.dll dist/plugins/ # 5. Add your .nyash files cp your_app.nyash dist/ ``` **Distribution structure:** ``` dist/ โ”œโ”€โ”€ nyash.exe โ”œโ”€โ”€ nyash.toml โ”œโ”€โ”€ your_app.nyash โ””โ”€โ”€ plugins/ โ”œโ”€โ”€ nyash_filebox_plugin.dll โ”œโ”€โ”€ nyash_array_plugin.dll โ””โ”€โ”€ ... ``` ### Platform-Agnostic Configuration Use `.so` extensions in `nyash.toml` - they work on all platforms: ```toml [plugin_paths] search_paths = ["./plugins"] [libraries] [libraries."libnyash_filebox_plugin.so"] boxes = ["FileBox"] path = "libnyash_filebox_plugin.so" # Works on Windows/Linux/macOS! ``` The plugin loader automatically: - Converts `.so` โ†’ `.dll` (Windows) or `.dylib` (macOS) - Tries with/without `lib` prefix on Windows - Searches in configured paths ## ๐Ÿ”ง Build Optimization ### Performance Builds ```bash # Maximum optimization RUSTFLAGS="-C target-cpu=native" cargo build --release # Link-time optimization CARGO_PROFILE_RELEASE_LTO=true cargo build --release ``` ### Size Optimization ```bash # Smaller binary cargo build --release --profile=min-size # Strip symbols (Linux/macOS) strip target/release/nyash ``` ## โ“ Troubleshooting ### Common Issues #### "LoadLibraryExW failed" on Windows - Ensure plugins are built for Windows (`cargo xwin build`) - Check that DLLs are in the `plugins/` directory - Verify `nyash.toml` has correct `plugin_paths` #### Cross-compilation fails - Install `cargo-xwin`: `cargo install cargo-xwin` - For mingw target: `sudo apt install mingw-w64` #### WASM build errors - Install wasm-pack: `curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh` - Check Rust target: `rustup target add wasm32-unknown-unknown` ## ๐Ÿ“š Related Documentation - [Cross-Platform Development](cross-platform.md) - [Plugin Development Guide](../../reference/plugin-system/) - [AOT Compilation Details](aot-compilation.md) - [Performance Tuning](../../reference/performance/)