Skip to content

fix(sys): compile mimalloc as C++ on windows-msvc targets#65

Merged
Brooooooklyn merged 1 commit into
napi-rs:mainfrom
shulaoda:05-12-fix_sys_compile_mimalloc_as_c_on_windows-msvc_targets
May 14, 2026
Merged

fix(sys): compile mimalloc as C++ on windows-msvc targets#65
Brooooooklyn merged 1 commit into
napi-rs:mainfrom
shulaoda:05-12-fix_sys_compile_mimalloc_as_c_on_windows-msvc_targets

Conversation

@shulaoda

@shulaoda shulaoda commented May 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #64

Summary

Force libmimalloc-sys/c_src/mimalloc/src/static.c to be compiled as C++ on every windows-msvc target, not just MSVC-style drivers. This fixes the aarch64-pc-windows-msvc build under TARGET_CC=clang and aligns the post-#16 cc-rs path with the pre-#16 cmake path, which explicitly set MI_USE_CXX=ON.

Why

Since libmimalloc-sys2 0.1.55 bumped the bundled mimalloc submodule, aarch64-pc-windows-msvc builds invoked with TARGET_CC=clang fail:

./c_src/mimalloc/include/mimalloc/atomic.h:230:14: error: call to undeclared function '__ldar64'
./c_src/mimalloc/include/mimalloc/atomic.h:252:7:  error: call to undeclared function '__stlr64'
./c_src/mimalloc/include/mimalloc/atomic.h:273:14: error: call to undeclared function '__ldar64'
./c_src/mimalloc/include/mimalloc/atomic.h:298:7:  error: call to undeclared function '__stlr64'
error: failed to run custom build command for `libmimalloc-sys2 v0.1.55`

Failing CI run (rolldown): https://github.com/rolldown/rolldown/actions/runs/25723489236/job/75533705378

Same regression reported in oxc: oxc-project/oxc#22329

Root cause

atomic.h has three mutually exclusive branches:

#if defined(__cplusplus)            // <atomic> path (always safe)
#elif defined(_MSC_VER)             // MSVC plain-C wrapper using MSVC intrinsics
#else                               // C11 <stdatomic.h> path
  • clang invoked as clang --target=*-pc-windows-msvc defines _MSC_VER for MSVC ABI compatibility, so when the source is compiled as C it takes the _MSC_VER branch.
  • The _M_ARM64 sub-branch of that path calls __ldar64 / __stlr64, which are MSVC-only intrinsics. clang does not declare them in C mode on Windows, hence the build failure.
  • Upstream mimalloc explicitly recommends "always compile as C++ when using MSVC" and uses set_source_files_properties(... LANGUAGE CXX) in its own CMakeLists.txt to enforce this. Compiling as C++ takes the #if defined(__cplusplus) branch and avoids the MSVC-intrinsics path entirely.

Why the previous code did not already do this

build_mimalloc_win was introduced in #16, replacing a cmake path that explicitly set MI_USE_CXX=ON. The intent was preserved as:

.cpp(false)
...
if build.get_compiler().is_like_msvc() {
    build.cpp(true);
}

However cc::Build::cpp(true) does not inject /TP or -x c++ (a search of cc-1.2.x confirms this) — it only changes which env vars are read (CXX / CXXFLAGS), the compiler binary name resolution (clangclang++, gccg++), and C++ stdlib linking. cl.exe still compiles static.c as C, and clang++ dispatches by extension and also compiles .c as C. So MI_USE_CXX=ON was never actually re-implemented after the cmakecc-rs migration; the bug was masked by coincidence:

Compiler Arch Branch taken Why it worked
cl.exe x64 _M_X64 uses portable __iso_volatile_load64 etc.
cl.exe aarch64 _M_ARM64 cl declares __ldar64 / __stlr64 natively
clang (msvc target) x64 _M_X64 clang also declares __iso_volatile_load64
clang (msvc target) aarch64 _M_ARM64 clang does not declare __ldar64 / __stlr64 → fails

The recent mimalloc submodule bump made the _M_ARM64 sub-branch the only path that matters on ARM64, exposing the coincidence.

Fix

Explicitly force C++ compilation of static.c regardless of driver:

if build.get_compiler().is_like_msvc() {
    build.flag("/TP");
} else {
    build.flag("-xc++");
}

cpp(true) is still kept so CXX / C++ stdlib resolution behave correctly. /TP covers cl.exe and clang-cl.exe; -xc++ covers plain clang.exe (gnu-driver) with --target=*-pc-windows-msvc.

This restores the original MI_USE_CXX=ON semantics that #16 intended to carry over.

Related

Downstream pin workarounds (can be reverted once this lands and a new libmimalloc-sys2 is published):

@shulaoda

Copy link
Copy Markdown
Contributor Author

Test in rolldown/rolldown#9373

@shulaoda shulaoda marked this pull request as ready for review May 14, 2026 13:38
@Brooooooklyn Brooooooklyn merged commit 442e465 into napi-rs:main May 14, 2026
19 checks passed
This was referenced May 14, 2026
shulaoda added a commit to rolldown/rolldown that referenced this pull request May 15, 2026
camc314 added a commit to oxc-project/oxc that referenced this pull request May 16, 2026
See napi-rs/mimalloc-safe#65

Co-authored-by: Cameron <cameron.clark@hey.com>
IWANABETHATGUY pushed a commit to rolldown/rolldown that referenced this pull request May 18, 2026
V1OL3TF0X pushed a commit to V1OL3TF0X/rolldown that referenced this pull request May 25, 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.

libmimalloc-sys2 0.1.55 fails to build on aarch64-pc-windows-msvc with clang: undeclared __ldar64 / __stlr64

2 participants