Skip to content

feat: implement standalone toolchain packaging for Windows and Linux#315

Merged
LunaStev merged 1 commit into
wavefnd:masterfrom
LunaStev:feat/add-standalone-Windows-and-Linux-compiler-toolchain-packaging
May 18, 2026
Merged

feat: implement standalone toolchain packaging for Windows and Linux#315
LunaStev merged 1 commit into
wavefnd:masterfrom
LunaStev:feat/add-standalone-Windows-and-Linux-compiler-toolchain-packaging

Conversation

@LunaStev
Copy link
Copy Markdown
Member

This PR significantly enhances the Wave compiler's distribution model by enabling the creation of fully standalone toolchains for Windows and Linux. The goal is to eliminate the requirement for a host-side C compiler (like gcc or msvc) or a pre-configured sysroot. With these changes, wavec now bundles all necessary runtime objects, libraries, and linkers to produce native binaries out of the box.

Key Changes

1. Standalone Windows GNU Support

  • Direct Linking: The compiler now invokes the bundled ld.lld directly for Windows targets, removing the legacy dependency on an external gcc for the linking stage.
  • MinGW Runtime Bundling: Automatically discovers and bundles critical MinGW runtime and import libraries (e.g., crt2.o, libmingw32.a, libkernel32.a, libmsvcrt.a). These are sourced via the Rust toolchain or custom environment paths.
  • Recursive DLL Resolution: The x.py script now uses objdump to recursively identify all non-system PE/DLL dependencies for wavec.exe and its LLVM sub-tools, ensuring all required shared libraries are included in the package.

2. Linux Standalone & CRT Management

  • CRT Bundling: The build script now compiles crt1.s using the bundled llvm-mc to produce architecture-specific crt1.o objects for Linux.
  • Relative Path Resolution: Updated the LLVM backend to automatically locate these bundled start/end objects and standard libraries relative to the wavec executable. This allows Wave to produce functional Linux binaries in "clean" environments (like minimal Docker containers) without a system-wide libc-dev installation.

3. Backend & Linking Refinement

  • Argument Propagation: Fixed a bug where global codegen flags (optimization levels, code models, relocation models) were not being consistently passed down to the llc and ld.lld processes.
  • Robust RPATH Patching:
    • For Linux, switched to using DT_RPATH or enforcing patchelf --force-rpath.
    • This ensures that transitive dependencies (like libffi required by libLLVM.so) are correctly resolved from the bundled lib/ directory, even when the system loader ignores the newer DT_RUNPATH tag.

4. Toolchain Quality Assurance (QA)

  • Binary Verification: The x.py package command now utilizes the file utility to verify that every single bundled binary (executables, shared objects, and DLLs) exactly matches the intended target architecture. The build process will strictly abort if a "poisoned" binary (e.g., an x86_64 library in an aarch64 package) is detected.

Rationale

Previously, wavec relied on the user having a compatible toolchain installed on their system to perform the final linking. By transitioning to a "Battery-Included" model where wavec carries its own linker and C runtime objects, we provide a consistent, predictable experience for developers across all supported platforms.

This commit massively improves the cross-platform capabilities of the `wavec` compiler release process. It enhances the packaging script (`x.py package`) and LLVM backend integration to fully bundle the MinGW Windows runtime and Linux C runtime objects into standalone distributions, eliminating the need for a host compiler or sysroot during user development.

[Details]

1. Windows GNU Cross-Compilation & Packaging (`x.py` & `cli.rs`):
  - Instead of passing linking off to `gcc`, `wavec` now bundles and invokes `ld.lld` directly for Windows GNU targets.
  - Automatically discovers and bundles MinGW's self-contained C runtime and import libraries (`crt2.o`, `libmingw32.a`, `libkernel32.a`, `libmsvcrt.a`, etc.) via Rust's toolchain (`WAVE_WINDOWS_RUST_TOOLCHAIN`) or user-provided `WAVE_WINDOWS_MINGW_LIB`.
  - Added PE/DLL dependency resolution in `x.py` (`pe_imported_dlls`) using `objdump` to recursively find and bundle all necessary Windows runtime DLLs (excluding system DLLs) alongside the `wavec.exe` and LLVM binaries.

2. Linux CRT & Sysroot Bundling:
  - The `x.py` script now compiles `crt1.s` using the bundled `llvm-mc` to produce and package architecture-specific `crt1.o` for Linux targets.
  - The LLVM backend automatically locates these bundled start/end/libc runtime files alongside the compiler executable if standard system paths fail, allowing `wavec` to produce Linux binaries completely standalone.

3. LLVM Backend & Command Argument Polish:
  - Fixed issues where global compiler arguments (`opt_flag`, `code_model`, `relocation_model`, etc.) were not consistently passed to `llc` or `ld.lld`.
  - Corrected `-rpath` patching for Linux binaries. Transferred from `DT_RUNPATH` to legacy `DT_RPATH` (`--disable-new-dtags`) or enforced with `patchelf --force-rpath` to ensure transitive dependencies like `libffi` are correctly resolved via the bundled `libLLVM.so` in clean container environments.

4. Robust Toolchain Validation:
  - `x.py package` now uses `file` output (`is_binary_for_target`) to verify every copied binary, shared object, or DLL exactly matches the intended architecture (`PE32+ x86-64`, `ELF aarch64`, etc.). The build strictly aborts if an architecture mismatch is detected in the bundled toolchain.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev merged commit cdabbc6 into wavefnd:master May 18, 2026
2 checks passed
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