Skip to content

Cross-compile GCC, GNU Binutils, and libc++ to wasm32-wasi#162

Merged
rennergade merged 27 commits intomainfrom
compile-gcc-cpp
Apr 8, 2026
Merged

Cross-compile GCC, GNU Binutils, and libc++ to wasm32-wasi#162
rennergade merged 27 commits intomainfrom
compile-gcc-cpp

Conversation

@rennergade
Copy link
Copy Markdown
Contributor

Adds build scripts and Makefile integration for cross-compiling a complete C compilation toolchain to run inside the Lind sandbox: GCC 15.2.0 (cc1), GNU Binutils 2.46.0 (ld, as), and LLVM libc++/libc++abi. Together these provide the full preprocess → compile → assemble → link pipeline targeting x86_64-linux-gnu from within WASI.

libc++ (llvm-project/compile_libcxx.sh)

  • Cross-compiles LLVM libc++ and libc++abi to wasm32-wasi using CMake
  • Based on the approach from Libcpp compilation integration lind-wasm#976 by @123R3N321
  • Installs headers and static libraries (libc++.a, libc++abi.a) to the sysroot overlay
  • Required by GCC's C++ source code (GCC is written in C++)
  • Makefile: make libcxx, merged via merge-libcxx stamp into the sysroot

GCC (gcc/compile_gcc.sh)

  • Cross-compiles GCC 15.2.0 (C language only) with --host=wasm32-unknown-wasi --target=x86_64-linux-gnu
  • Builds GMP, MPFR, MPC prerequisites in-tree; patches config.sub and MPFR float128 detection for WASI compatibility
  • Uses -Os to minimize binary size; requires libc++ in the merged sysroot for GCC's C++ source
  • Asyncify removelist with wildcard patterns to prevent Cranelift AOT precompilation failures caused by auto-generated match.pd pattern matchers, x86 backend switch tables, and wide-int template instantiations inflating 1000x+ under asyncify instrumentation
  • Falls back to staging the .wasm for JIT compilation if AOT precompile still fails

Binutils (binutils/compile_binutils.sh)

  • Cross-compiles GNU Binutils 2.46.0 with the same host/target triplets as GCC
  • Builds only ld and as (make all-ld all-gas), skipping gold, gdb, gprofng, sim
  • Pure C — no libc++ dependency; depends on zlib in the merged sysroot (for BFD compressed section support)
  • Shared post_process_binary helper handles wasm-opt + lind-boot for both binaries

Makefile

  • make libcxx — builds libc++/libc++abi, merged into sysroot via merge-libcxx stamp
  • make gcc — depends on merge-libcxx
  • make binutils — depends on merge-zlib
  • make install-gcc / make install-binutils — stage into $LINDFS_ROOT
  • Added to make install aggregate and make clean

Key technical details

  • Asyncify removelist uses Binaryen's @@ response file syntax with * wildcard glob matching (--pass-arg=asyncify-removelist@@file.txt)
  • --strip-debug must run AFTER asyncify (not before) so the wasm name section is available for removelist matching
  • Generator tools (genrecog, gengtype, etc.) are built natively via CC_FOR_BUILD=gcc

@Yaxuan-w
Copy link
Copy Markdown
Member

This contains huge amount of files, and my GitHub web failed on loading all of them. Could we separate source and script to different prs?

@rennergade
Copy link
Copy Markdown
Contributor Author

This contains huge amount of files, and my GitHub web failed on loading all of them. Could we separate source and script to different prs?

Makes sense, merged binutils source separately. Fixed now.

Copy link
Copy Markdown
Member

@Yaxuan-w Yaxuan-w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Overall makes sense to me. Left a few comments

Comment thread gcc/compile_gcc.sh Outdated
Comment thread gcc/compile_gcc.sh
Comment thread gcc/compile_gcc.sh
Comment thread llvm-project/compile_libcxx.sh
Comment thread Makefile Outdated
Comment thread gcc/compile_gcc.sh
Comment thread gcc/compile_gcc.sh Outdated
Comment thread gcc/compile_gcc.sh Outdated
Comment thread gcc/compile_gcc.sh Outdated
Comment thread binutils/compile_binutils.sh Outdated
Comment thread binutils/compile_binutils.sh
Comment thread binutils/compile_binutils.sh Outdated
@rennergade rennergade merged commit 35f06f0 into main Apr 8, 2026
rishabhBudhouliya pushed a commit that referenced this pull request Apr 21, 2026
* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* fix localtime timespec

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* compilation scripts for gcc and libcpp

* add binutils

* add binutils

* add binutils

* address pr comments

* pr fixes
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.

3 participants