Overview
Cross-compilation is the process of building QuickJS-ng on one platform (the host) to run on a different platform (the target). This is commonly used for:- Building for embedded systems
- Creating ARM binaries on x86 machines
- Compiling for mobile platforms (iOS, Android)
- WebAssembly builds
- Windows builds from Linux
Build System Support
Meson
Recommended for cross-compilation with excellent toolchain support and automatic handling of native builds
CMake
Supported via toolchain files, requires manual configuration of cross-compilation settings
Meson Cross-Compilation
Meson provides the best cross-compilation experience with automatic handling of native vs. target builds.Native vs. Target Builds
When cross-compiling, Meson automatically builds two versions of certain tools:- Native tools: Built for the host machine (e.g.,
qjscto compile JavaScript to bytecode) - Target binaries: Built for the target platform (e.g.,
qjsruntime for ARM)
qjsc) needs to run on the build machine during compilation.
Cross-Compilation File
Create a cross-compilation file describing your target platform:- ARM64 Linux
- ARM32 Linux
- Windows (MinGW)
Building with Cross-Compilation
How Meson Handles Native Builds
When cross-compiling, Meson automatically:- Detects that a native
qjscis needed to compile JavaScript files - Builds a native version of
qjscandqjsfor the host machine - Uses the native tools during the build process
- Builds the target binaries for the cross-compilation target
meson.build:342-401:
CMake Cross-Compilation
Toolchain Files
CMake uses toolchain files for cross-compilation:- ARM64 Linux
- Windows (MinGW)
Building with Toolchain File
Platform-Specific Cross-Compilation
Android (NDK)
QuickJS-ng supports Android NDK 26.0.10792818 or later.- Meson
- CMake
iOS
iOS cross-compilation is typically done on macOS using Xcode toolchains.CLI tools (
qjs, qjsc) are not installed for iOS, tvOS, or watchOS builds. Only the library is built.WebAssembly (WASI)
WASI is the WebAssembly System Interface standard.- Using wasi-sdk
- WASI Reactor
Emscripten
Emscripten compiles C/C++ to WebAssembly for web browsers.- CMake
- Configuration
MinGW (Windows on Linux)
- Meson
- CMake
MinGW builds automatically link statically with libgcc when
QJS_BUILD_CLI_STATIC is enabled.Common Issues and Solutions
Issue: qjsc Not Found During Cross-Compilation
Problem: Nativeqjsc is needed to compile JavaScript files during build.
Solution:
- Meson
- CMake
Meson handles this automatically. No action needed.
Issue: Thread Library Not Found
Problem: Cross-compilation toolchain doesn’t provide pthread library. Solution: WASI builds automatically skip thread library:Issue: Module Loading on Windows
Problem: Windows cannot resolve symbols at runtime like Unix systems. Solution: QuickJS-ng automatically links modules to the main library on Windows:Issue: Static vs. Shared Libraries
Problem: Binary modules with static qjs on Windows can cause runtime errors. Solution: Use shared library builds on Windows when loading binary modules:Testing Cross-Compiled Binaries
Using QEMU
Test ARM binaries on x86 using QEMU:Using Wine
Test Windows binaries on Linux:Using wasmtime
Test WASI binaries:Advanced Configuration
Custom Sysroot
Specify a custom sysroot for cross-compilation:- CMake
- Meson
Cross-Compiling with Dependencies
When using mimalloc or other dependencies:Multi-Architecture Builds
Build for multiple architectures:Best Practices
- Use Meson for cross-compilation - Better automatic handling of native vs. target builds
- Test early and often - Use emulators (QEMU, Wine) to test cross-compiled binaries
- Static linking for distribution - Use static builds when distributing to avoid dependency issues
- Verify toolchain - Ensure cross-compilation toolchain matches target platform exactly
- Check platform features - Some platforms have limited features (no threads on WASI)
- Use shared libs carefully - Static libraries are safer for cross-compilation