Skip to content

feat: add CC_MASM_ASM and fall back to llvm-ml for MASM files - #1975

Merged
NobodyXu merged 4 commits into
rust-lang:mainfrom
waltuov:feat/masm-assembler-override
Oct 6, 2026
Merged

NobodyXu merged 4 commits into
rust-lang:mainfrom
waltuov:feat/masm-assembler-override

Conversation

@waltuov

@waltuov waltuov commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Cross compiling a crate with .asm files for an MSVC target from Linux or macOS fails with failed to find tool "ml64.exe", because cc always runs ml64.exe or ml.exe for them. The usual workaround is an ml64.exe script on PATH that runs llvm-ml -m64, as in conda-forge/polars-feedstock#240.

This implements both parts agreed in the issue:

  • CC_MASM_ASM overrides the assembler for .asm files on MSVC targets. It is read with the usual target prefixes (CC_MASM_ASM_<target>, TARGET_CC_MASM_ASM and so on) and parsed like AR, so CC_MASM_ASM="llvm-ml -m64" works.
  • When it is not set and neither Visual Studio nor PATH has ml64.exe or ml.exe, x86 and x86_64 targets fall back to llvm-ml. cc looks next to clang-cl first, with the lookup the archiver already uses for llvm-lib (now one shared helper), then on PATH.

When the assembler is llvm-ml (by file name, ignoring case), cc passes -m64 or -m32 for the target, since llvm-ml assembles 32 bit code unless it runs as llvm-ml64. The flag goes before the arguments from CC_MASM_ASM and asm_flag, and llvm-ml takes the last -m, so a user's choice still wins. cc also leaves out -Zi for llvm-ml, which ignores it with a warning on every debug build.

Behavior change

  • The fallback only runs where the build fails today. If Visual Studio or PATH has ml64.exe or ml.exe, including wrapper scripts with that name, cc runs it as before.
  • ARM targets keep armasm.exe and armasm64.exe with no fallback, since llvm-ml only assembles x86. CC_MASM_ASM applies to them too.
  • .asm compiles on MSVC targets now read CC_MASM_ASM and its target variants, so Cargo gets rerun-if-env-changed lines for them.
  • The llvm-lib lookup for clang-cl moved into the shared helper without changes.
  • cc looks up the assembler once per compile call instead of once per .asm file, so its rerun-if-env-changed lines no longer repeat for every file.

Validation

  • 10 new tests in tests/test.rs. The 5 CC_MASM_ASM tests run on every host. The 5 in msvc_masm_fallback are cfg(not(windows)), because a Windows host with Visual Studio finds ml64.exe and never reaches the fallback, and their shims have no .exe suffix. The test shim now records the program it runs as, so the tests check which program cc ran, or that nothing ran.
  • 7 of them fail on main with failed to find tool "ml64.exe". The other 3 pass on main and guard unchanged behavior: ml64.exe on PATH still wins over llvm-ml, ARM doesn't fall back, and clang-cl still finds llvm-lib next to it.
  • Checked on macOS: cargo test with and without --features parallel (except 7 clang_apple_*/gnu_apple_* tests that need a full Xcode install), cargo clippy --workspace --all-targets -- -D warnings (also with parallel, and -p cc for x86_64-pc-windows-msvc), cargo fmt --check, cargo doc, and a rustc 1.65 check of the library.
  • Not run here: the Windows jobs, including dev-tools/cc-test, and a real llvm-ml binary. llvm-ml's handling of -m, -Zi and the other flags cc passes (-nologo, -c, -Fo, -I, -D, -safeseh) was checked against its option table and GetTriple in llvm/tools/llvm-ml.

Limitations

  • As with AR, a wrapper such as sccache in CC_MASM_ASM is dropped.
  • The fallback looks for a program named llvm-ml only.

Fixes #1022

`CC_MASM_ASM` overrides the assembler for `.asm` files on MSVC targets. It
is read with target prefixes and can include arguments, like `AR`.

When it is not set and neither Visual Studio nor `PATH` has `ml64.exe` or
`ml.exe`, x86 targets fall back to `llvm-ml`: next to clang-cl, the way the
archiver already finds `llvm-lib` (now one shared helper), then on `PATH`.

llvm-ml gets `-m64` or `-m32` for the target, before any user arguments so
a `-m` there still wins, and no `-Zi`, which it ignores with a warning.

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you! Just two feedback

Comment thread src/lib.rs Resolved
Comment thread src/lib.rs Resolved

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry one more for the tests

Comment thread tests/test.rs Resolved
The test shim now records the program it runs as, before its arguments, so
tests can check which shim cc invoked. The tests for the MASM assembler and
the clang-cl archiver use it, and the ARM test checks that nothing ran.
Windows file names are case-insensitive, so `LLVM-ML.exe` is llvm-ml too and
needs `-m64` or `-m32`.
`msvc_macro_assembler` returns a `Tool` now. `compile_objects` keeps it in an
`Option` that the first `.asm` file fills, so the assembler lookup and the
`CC_MASM_ASM` read run once per build instead of once per file.

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you

@NobodyXu
NobodyXu merged commit 2ac0cd6 into rust-lang:main Oct 6, 2026
83 checks passed
@waltuov
waltuov deleted the feat/masm-assembler-override branch October 6, 2026 14:33
@lockfile-maintenance lockfile-maintenance Bot mentioned this pull request Oct 16, 2026
1 task
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.

Ability to cross-compile MASM files

2 participants