git subtreeEmscripten vendors several external libraries under system/lib/ to provide core runtime capabilities, including C/C++ standard libraries, memory allocators, and compiler runtimes. Currently, these libraries are maintained through disparate and often cumbersome mechanisms:
External Fork + Synchronization Scripts:
musl (system/lib/libc/musl): Maintained via an external GitHub fork (https://github.com/emscripten-core/musl) and two sync scripts: system/lib/push_musl_changes.py and system/lib/update_musl.py.compiler-rt, libcxx, libcxxabi, libunwind, llvm-libc, openmp): Synced from branches in emscripten-core/llvm-project using system/lib/push_llvm_changes.py and individual update_*.py scripts.Manual File Copying:
mimalloc (system/lib/mimalloc): Tracked in-tree with Emscripten-specific primitives, but updated periodically by copying new upstream releases directly into the directory without automated tooling.Static Single-File Vendoring:
dlmalloc (system/lib/dlmalloc.c) and stb_image (system/lib/stb_image.c): Single source files with low churn.system/lib/libc/musl), authors often forget to backport them to the downstream fork repo.shutil.copytree or manual file replacement discards commit history, author attribution, and intermediate change context between repositories.Managing vendored system libraries via git subtree was recently and successfully adopted in wasi-libc: https://github.com/WebAssembly/wasi-libc/pull/797
git subtree.git subtree pull --squash).git diff <tag>: HEAD:<prefix>).mimalloc), followed by musl, and later evaluating LLVM runtime libraries.system/lib/push_musl_changes.py and system/lib/update_musl.py).git submodule (submodules require recursive clones, detached HEAD management, and complicate contributor workflows).dlmalloc.c) where upstream churn is negligible.| Library | In-Tree Path | Upstream Repository | Current Workflow | Complexity / Recommendation |
|---|---|---|---|---|
mimalloc | system/lib/mimalloc | microsoft/mimalloc | Manual copy-paste | Recommended Pilot. Dedicated repo, cleanly tagged releases, small footprint, self-contained. |
musl | system/lib/libc/musl | git.musl-libc.org/musl | Fork + update_musl.py | High Priority. Follows wasi-libc precedent. Eliminates sync scripts and perpetual fork drift. |
compiler-rt | system/lib/compiler-rt | llvm/llvm-project | Fork + update_compiler_rt.py | Medium Priority. Upstream is a monorepo; needs subtree split or tracking a branch/fork. |
libcxx / libcxxabi | system/lib/libcxx{,abi} | llvm/llvm-project | Fork + update_libcxx*.py | Medium Priority. Same monorepo considerations as compiler-rt. |
libunwind | system/lib/libunwind | llvm/llvm-project | Fork + update_libunwind.py | Medium Priority. Same monorepo considerations as compiler-rt. |
llvm-libc | system/lib/llvm-libc | llvm/llvm-project | Fork + update_llvm_libc.py | Medium Priority. Same monorepo considerations as compiler-rt. |
openmp | system/lib/openmp | llvm/llvm-project | Fork + update_openmp.py | Medium Priority. Same monorepo considerations as compiler-rt. |
dlmalloc | system/lib/dlmalloc.c | Doug Lea (v2.8.6) | Vendored single file | Low Priority / Skip. Upstream is inactive; heavily customized for Emscripten. |
stb_image | system/lib/stb_image.c | nothings/stb | Vendored single file | Low Priority. Low update frequency; single file. |
mimalloc?We recommend starting the git subtree migration with mimalloc as a pilot before applying it to musl:
mimalloc does not risk breaking the fundamental runtime or core toolchain startup.microsoft/mimalloc) with standard release tags (v3.5.1, v3.4.1, etc.), making remote management straightforward.mimalloc does not have an external emscripten-core fork repo or existing sync scripts to unwind; it has been updated purely by manual file copying.system/lib/mimalloc/src/prim/emscripten/prim.c (much of which is already upstreamed) and compiler flag adjustments in tools/system_libs.py.mimalloc releases regularly (v3.3.1 -> v3.4.1 -> v3.5.1 in recent months). Adopting git subtree provides immediate value for future version bumps.mimalloc)We use git subtree with the --squash flag to vendor external libraries under their designated directory prefixes:
https://github.com/microsoft/mimallocsystem/lib/mimalloc Upstream (https://github.com/microsoft/mimalloc)
│
git fetch / git subtree pull
▼
┌─────────────────────────────────────────────────────────────┐
│ emscripten-core/emscripten │
│ │
│ ├── system/lib/mimalloc/ ◄── Managed via subtree │
│ │ ├── include/ ◄── Tracked in-tree │
│ │ └── src/ │
│ │ └── prim/emscripten/ ◄── Emscripten primitives │
│ └── tools/system_libs.py ◄── Compiles libmimalloc │
└─────────────────────────────────────────────────────────────┘
│
git subtree split / push (Optional)
▼
Upstream PR / Fork (e.g. microsoft/mimalloc)
--squashUpstream repositories often have tens of thousands of commits. Without --squash, the full upstream history is imported directly into Emscripten's git log. With --squash, git synthesizes a single commit representing the upstream snapshot at the merge point, while embedding subtree metadata (git-subtree-dir and git-subtree-split) in the commit message to maintain proper ancestry for future 3-way merges.
When importing an upstream repository via subtree, retain its upstream directory structure:
mimalloc and musl are compact (a few megabytes).git subtree pull operations.tools/system_libs.py explicitly selects which source files are compiled for WebAssembly, ensuring uncompiled files do not affect binary output or cache sizes.Local changes, bug fixes, and porting adaptations are committed directly to the Emscripten repository. Separate fork repositories (like emscripten-core/musl) no longer serve as intermediate synchronization hubs. When local patches are ready to be contributed upstream, git subtree split can extract them cleanly into a branch suitable for a pull request to the upstream project.
By default, git fetch and --tags import all upstream tags (e.g. v1.2.6, v3.5.1) into the repository's local refs/tags/* namespace. This presents two problems:
v1.0, v2.0) can collide with each other or clutter Emscripten's tag list and shell autocompletion.git describe: Automated tooling and build scripts that run git describe --tags to determine the compiler version can pick up reachable upstream tags instead of Emscripten release tags.To prevent foreign tag pollution, we adopt the following conventions:
Configure Remotes with --no-tags: Subtree remotes should be added with --no-tags (or set remote.<name>.tagOpt = --no-tags):
git remote add --no-tags mimalloc-upstream \ https://github.com/microsoft/mimalloc
git subtree pull does not require local tags to exist; Git can fetch the specified tag or commit into FETCH_HEAD on demand without storing it in refs/tags/*.
Namespaced Refspecs (Optional for Local Tag Access): Maintainers who want upstream tags available locally (e.g. for diffing) can namespace them outside of refs/tags/*:
# Isolate under refs/remotes/ (hidden from git tag and git describe) git config --add remote.mimalloc-upstream.fetch \ '+refs/tags/*:refs/remotes/mimalloc-upstream/tags/*'
Alternatively, prefix them inside refs/tags/ using a subdirectory:
git config --add remote.mimalloc-upstream.fetch \ '+refs/tags/*:refs/tags/mimalloc/*'
URL-Based Ephemeral Pulls (The wasi-libc Model): Contributors do not need upstream remotes configured in everyday checkouts. Maintainers performing a version update can pull directly from the repository URL:
git subtree pull --prefix=system/lib/mimalloc \ https://github.com/microsoft/mimalloc v3.5.2 --squash \ -m "Update mimalloc to v3.5.2"
This imports the squashed commit without leaving any foreign remotes or tags behind.
mimallocHere is the step-by-step process for initializing mimalloc as a git subtree without losing existing modifications:
git remote add --no-tags mimalloc-upstream https://github.com/microsoft/mimalloc git fetch mimalloc-upstream tag v3.5.1
Create a temporary branch based on upstream v3.5.1 (the version currently in Emscripten) and overlay our current in-tree files:
# Check out base upstream release tag git checkout -b temp-mimalloc-init FETCH_HEAD # Overlay current Emscripten mimalloc directory to preserve local patches cp -r /path/to/emscripten/system/lib/mimalloc/* . git add -A git commit -m "Apply Emscripten modifications on top of mimalloc v3.5.1"
In the Emscripten working branch:
git checkout -b init-mimalloc-subtree # 1. Remove previously un-tracked mimalloc directory git rm -rf system/lib/mimalloc git commit -m "[lib] Remove loose mimalloc files for subtree initialization" # 2. Add the subtree using the prepared branch git subtree add --prefix=system/lib/mimalloc temp-mimalloc-init --squash \ -m "Initialize system/lib/mimalloc as git subtree tracking mimalloc v3.5.1" # 3. Clean up the temporary branch git branch -D temp-mimalloc-init
./test/runner test_other.test_mimalloc_options ./test/runner test_other.test_mimalloc_no_asan ./test/runner test_hello_world -sMALLOC=mimalloc
mimalloc)Inspect the exact diff between Emscripten's tree and an upstream tag:
git fetch --no-tags mimalloc-upstream tag v3.5.1 # Diff entire subtree against upstream v3.5.1 git diff FETCH_HEAD: HEAD:system/lib/mimalloc # Diff a specific file git diff v3.5.1:src/prim/emscripten/prim.c \ HEAD:system/lib/mimalloc/src/prim/emscripten/prim.c
To view Emscripten's changes as individual commit logs:
git subtree split --prefix=system/lib/mimalloc -b mimalloc-local-changes git log v3.5.1..mimalloc-local-changes
v3.5.2)Updating to a new version is a single command:
# Option A: Pull by remote name (with remote configured with --no-tags) git subtree pull --prefix=system/lib/mimalloc \ mimalloc-upstream v3.5.2 --squash \ -m "Update mimalloc to v3.5.2" # Option B: Pull directly by repository URL (no local remote or tags stored) git subtree pull --prefix=system/lib/mimalloc \ https://github.com/microsoft/mimalloc v3.5.2 --squash \ -m "Update mimalloc to v3.5.2" # Resolve any merge conflicts in-place if necessary # Run tests ./test/runner test_other.test_mimalloc_options
Backporting an upstream commit before a new release:
git fetch mimalloc-upstream git subtree pull --prefix=system/lib/mimalloc \ mimalloc-upstream <commit-sha> --squash \ -m "Backport upstream mimalloc commit <commit-sha>"
Extract local Emscripten commits into a standalone branch for a pull request to microsoft/mimalloc:
git subtree split --prefix=system/lib/mimalloc -b mimalloc-upstream-pr # Push to personal GitHub fork and open upstream PR git push git@github.com:<user>/mimalloc.git mimalloc-upstream-pr:my-fix
muslFollowing successful validation with mimalloc, the same process applies to system/lib/libc/musl:
git remote add --no-tags musl-upstream git://git.musl-libc.org/musl git fetch musl-upstream tag v1.2.6
v1.2.6 (overlaying system/lib/libc/musl).system/lib/libc/musl with:git subtree add --prefix=system/lib/libc/musl temp-musl-init --squash \ -m "Initialize system/lib/libc/musl as git subtree tracking musl v1.2.6"
./test/runner test_libc*).git rm system/lib/push_musl_changes.py system/lib/update_musl.py
Migrating LLVM runtimes (compiler-rt, libcxx, libcxxabi, libunwind, llvm-libc, openmp) presents unique considerations because upstream is a multi-gigabyte monorepo (llvm/llvm-project):
git subtree pull directly against the monorepo root is impractical because each library resides in a nested subdirectory (runtimes/, libcxx/, etc.), and pulling the root repository would drag in the entire LLVM history.emscripten-core/llvm-project (or using GitHub Actions to generate per-runtime subtree branches corresponding to upstream release tags, e.g. llvmorg-22.1.8-libcxx).mimalloc and musl. Until then, the existing update_*.py and push_llvm_changes.py scripts can continue operating.| Operation | Previous Workflow | New git subtree Workflow |
|---|---|---|
| Day-to-day Patches | Edit in Emscripten, run push script, push to fork repo. | Edit files directly in Emscripten and commit. |
| Upstream Updates | Merge in fork repo, resolve conflicts, run copy script. | Run git subtree pull --squash directly in Emscripten. |
| Conflict Resolution | Done in external clone, then copied blindly via script. | Resolved in-place in Emscripten with standard git tools. |
| Auditing Diffs | Manually diffing two separate checkouts. | git diff <tag>: HEAD:<prefix> |
| Single Source of Truth | Split between fork repos and Emscripten repo. | Emscripten repository is the single source of truth. |
mimalloc as a git subtree. Validate day-to-day development, local patches, and an upstream release bump.musl): Convert system/lib/libc/musl to a git subtree following the proven pattern and retire push_musl_changes.py and update_musl.py.compiler-rt, libcxx, and other LLVM runtimes, addressing monorepo subtree extraction strategy.