Skip to content

Major Overhaul: Standalone Toolchain Bundling and Modernized LLVM Backend#314

Merged
LunaStev merged 5 commits into
wavefnd:masterfrom
LunaStev:feat/add-comprehensive-LLVM-bundling-and-cross-platform-packaging
May 15, 2026
Merged

Major Overhaul: Standalone Toolchain Bundling and Modernized LLVM Backend#314
LunaStev merged 5 commits into
wavefnd:masterfrom
LunaStev:feat/add-comprehensive-LLVM-bundling-and-cross-platform-packaging

Conversation

@LunaStev
Copy link
Copy Markdown
Member

This PR introduces a massive overhaul of the Wave compiler's distribution and execution architecture. The primary goal is to transform wavec into a standalone compiler that bundles its own LLVM dependencies and linker tools, eliminating the need for users to have a specific LLVM version pre-installed on their system.

Key Changes

1. Standalone Toolchain Bundling & RPATH Patching

The x.py package command has been redesigned to create a fully portable distribution:

  • Binary Aggregation: Bundles essential LLVM tools (llc, llvm-as, llvm-mc, ld.lld, ld64.lld, lld-link) and their required dynamic libraries into the release package.
  • Dynamic Linking Fixes (RPATH):
    • Linux: Uses patchelf to set $ORIGIN/llvm/lib, allowing the binary to find its libraries relative to its own location.
    • macOS: Uses install_name_tool and otool to rewrite @executable_path references. This is followed by an ad-hoc codesign to ensure the modified binaries remain executable on modern macOS.
  • Windows: Automatically collects necessary MinGW and LLVM DLLs to ensure the .exe is standalone.

2. Modernized Execution Pipeline

The compiler now controls the full compilation lifecycle using bundled tools instead of relying on a system-installed clang:

  • Direct LLVM Orchestration: IR is now dispatched directly to llvm-as (bitcode), llc (assembly), and llvm-mc (object code).
  • Internal Linker Selection: Automatically selects the appropriate bundled LLD flavor based on the target OS: ld64.lld for macOS and ld.lld for Linux/other ELF systems.
  • Tool Resolution: Implemented a robust search hierarchy that looks for LLVM tools in environment variables (WAVE_LLVM_BIN), prefixes, or local paths relative to the wavec executable.

3. Build System & Safety (x.py)

  • Architecture Verification: Before building, x.py now uses lipo (on macOS) to verify that the host's dynamic LLVM library supports the requested target architecture, preventing "missing symbol" errors mid-build.
  • Python Resilience: Added support for the built-in tomllib (Python 3.11+), removing the strict external dependency on toml. The GUI builder now also handles missing tkinter gracefully with helpful installation advice.

4. UI/UX & Compatibility

  • Terminal Colors: The colorex utility now respects standard environment variables like NO_COLOR and CLICOLOR=0.
  • Windows Legacy Support: Added detection for ANSI-compatible legacy Windows environments (ANSICON, ConEmu, etc.) to ensure a consistent CLI experience.

Rationale

By bundling the toolchain, we significantly lower the barrier to entry for Wave. Users no longer need to struggle with matching LLVM versions or setting complex LD_LIBRARY_PATH variables. The move from clang to internal llc/lld calls also gives the compiler finer control over the code generation and linking stages.

Example Workflow

Packaging a standalone release:

python3 x.py release
# Results in a portable 'dist/' directory containing wavec and its LLVM sub-tools.

LunaStev and others added 5 commits May 15, 2026 20:47
This commit massively overhauls the release packaging process (`x.py package` and `x.py release`) and introduces embedded toolchain support across the compiler and build scripts. Wave can now bundle its own LLVM dependencies and linker tools to provide standalone compiler distributions.

[Details]
1. LLVM Toolchain Bundling & Rpath Patching:
  - `x.py package` now aggregates necessary LLVM binaries (`llc`, `llvm-as`, `llvm-mc`, `ld.lld`, `ld64.lld`, `lld-link`) and runtime libraries (`libLLVM*.dylib`, `liblld*.so`, etc.) alongside the compiled `wavec` binary into a `dist/` directory.
  - Implemented dynamic runtime patching using `install_name_tool` (macOS) and `patchelf` (Linux) to rewrite binary RPATHs (e.g., `@executable_path/llvm/lib` or `$ORIGIN/llvm/lib`). This ensures packaged `wavec` and `lld` binaries can reliably load bundled dynamic LLVM libraries regardless of where they are extracted.
  - For macOS, `otool` is used to map specific internal dylib dependencies and properly rewire `libLLVM.dylib` references, followed by ad-hoc `codesign` to maintain execution permission.
  - Ensured Windows releases include the correct set of MinGW and LLVM DLLs.

2. Architecture Verification before Build:
  - Added pre-build validation in `x.py` to ensure the host LLVM dynamic library (e.g., `libLLVM.dylib`) actually supports the requested compilation architecture (`aarch64` vs `x86_64`) using `lipo`.

3. LLVM Backend & Execution Pipeline:
  - Modified the internal `wavec` compilation pipeline to dispatch IR to `llc`, `llvm-as`, and `llvm-mc` (using the bundled binaries) instead of `clang`.
  - The default linker is now dynamically selected based on the target OS: `ld64.lld` for macOS and `ld.lld` for others.
  - LLVM tools are resolved automatically from custom search paths, environment variables (`WAVE_LLVM_BIN`, `LLVM_SYS_211_PREFIX`), or alongside the `wavec` executable itself.
  - Re-mapped the `opt_flag` behavior to pass proper LLVM code-generation flags to `llc`.
  - Added support for generating Bitcode, Assembly, and Object files correctly via the new toolchain execution flow in `runner.rs`.

4. Terminal Colors Improvements:
  - Upgraded terminal color utility (`colorex.rs`) to gracefully degrade if colors are explicitly disabled via environment variables (`NO_COLOR`, `CLICOLOR=0`).
  - Added support for legacy Windows ANSI mode environments (`WT_SESSION`, `ANSICON`, `ConEmuANSI`).

5. Python Dependency Fallbacks:
  - `x.py` no longer strictly requires `toml`. It natively supports `tomllib` (built-in for Python 3.11+) and gracefully catches `tkinter` import errors with helpful advice.

Signed-off-by: LunaStev <youngjae681@gmail.com>
Signed-off-by: LunaStev <luna@lunastev.org>
…launcher support

- Added `ldd_shared_libs` and `copy_linux_runtime_deps` to resolve and bundle runtime shared library dependencies for Linux targets.
- Introduced `create_linux_launcher` to generate executable launchers with automated environment configuration.
- Updated `stage_release_package` to include binary launchers and handle Linux-specific runtime packaging.
- Improved `patch_linux_binary` with fallback warnings for missing `patchelf`.

Signed-off-by: LunaStev <luna@lunastev.org>
- Added `configure_bundled_llvm_tool_env` to set up environment paths for bundled LLVM tools.
- Updated CLI lowering and linking commands to use tool-specific environment configuration.
- Enhanced Linux packaging with stricter checks for ELF RUNPATH settings and runtime dependency resolution.
- Removed deprecated Linux launchers in favor of embedded runtime settings.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev merged commit b811657 into wavefnd:master May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant