Commit
ce6f9ffaa7e2882d0ebda143f8ac2c307782af8a
rust: pick a GCC-compatible Cargo target under MSYS2/MinGW
When Git is built under MSYS2/MinGW with Rust support enabled, the
Makefile expects `cargo build` to drop a `target/release/libgitcore.a`
that is linkable by the same MinGW GCC used for every other object. With
Rust installed via `rustup` (the way it ships on the GitHub-hosted
`windows-2022` and `windows-11-arm` runners that build git/git and its
forks), the default toolchain targets the MSVC ABI; cargo then writes
`target/release/gitcore.lib` instead, which the MinGW `ld.exe` cannot
consume:
LINK git-shell.exe
D:\git-sdk-64-minimal\mingw64\bin/ld.exe: cannot find target/release/libgitcore.a: No such file or directory
collect2.exe: error: ld returned 1 exit status
See https://github.com/microsoft/git/actions/runs/27341625000 for a
full example log.
Let's define the correct target, using the `CARGO_BUILD_TARGET` variable
that will be picked up by Rust, see
https://dirname.github.io/rust-std-doc/cargo/reference/environment-variables.html#:~:text=CARGO%5FBUILD%5FTARGET
Re-use (and fix) the existing `HOST_CPU` variable to determine the
correct value. Avoid relying on environment variables that are simply
not defined in Git for Windows' minimal SDK that Git uses in its CI
runs.
Assisted-by: Claude Opus 4.7
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin <johannes.schindelin@gmx.de> · 2026-09-13 15:57 UTC · committed by Junio C Hamano
2 files changed,
+25
−2
modified Makefile
| @@ -959,7 +959,7 @@ RUST_LIB_NAME = gitcore.lib | |||
| 959 | 959 | else | |
| 960 | 960 | RUST_LIB_NAME = libgitcore.a | |
| 961 | 961 | endif | |
| 962 | - | RUST_LIB = target/$(RUST_BUILD_CONFIG)/$(RUST_LIB_NAME) | |
| 962 | + | RUST_LIB = target$(if $(CARGO_BUILD_TARGET),/$(CARGO_BUILD_TARGET))/$(RUST_BUILD_CONFIG)/$(RUST_LIB_NAME) | |
| 963 | 963 | endif | |
| 964 | 964 | ||
| 965 | 965 | GITLIBS = common-main.o $(LIB_FILE) | |
modified config.mak.uname
| @@ -758,7 +758,30 @@ ifeq ($(uname_S),MINGW) | |||
| 758 | 758 | MINGW_PREFIX := /$(shell echo '$(MSYSTEM)' | tr A-Z a-z) | |
| 759 | 759 | endif | |
| 760 | 760 | prefix = $(MINGW_PREFIX) | |
| 761 | - | HOST_CPU = $(patsubst %-w64-mingw32,%,$(MINGW_CHOST)) | |
| 761 | + | ||
| 762 | + | # A rustup-managed Rust on Windows defaults to the MSVC ABI and | |
| 763 | + | # produces a `gitcore.lib` that the MinGW `ld.exe` cannot link. | |
| 764 | + | # Pick a GCC-compatible Rust target triple matching the MSYS2 | |
| 765 | + | # subsystem instead: `*-pc-windows-gnullvm` for the Clang/LLVM | |
| 766 | + | # subsystems (which on Windows is also the only choice for | |
| 767 | + | # ARM64, where no MinGW-GCC port exists) and `*-pc-windows-gnu` | |
| 768 | + | # for the MSVCRT-based MinGW subsystems. For a `staticlib` | |
| 769 | + | # crate-type Cargo does not invoke an external linker, so | |
| 770 | + | # `rustup target add <triple>` is sufficient. | |
| 771 | + | ifneq (,$(filter %ARM64, $(MSYSTEM))) | |
| 772 | + | HOST_CPU = aarch64 | |
| 773 | + | else ifneq (,$(filter %32, $(MSYSTEM))) | |
| 774 | + | HOST_CPU = i686 | |
| 775 | + | else | |
| 776 | + | HOST_CPU = x86_64 | |
| 777 | + | endif | |
| 778 | + | ifneq (,$(filter CLANG%, $(MSYSTEM))) | |
| 779 | + | CARGO_BUILD_TARGET = $(HOST_CPU)-pc-windows-gnullvm | |
| 780 | + | else | |
| 781 | + | CARGO_BUILD_TARGET = $(HOST_CPU)-pc-windows-gnu | |
| 782 | + | endif | |
| 783 | + | export CARGO_BUILD_TARGET | |
| 784 | + | ||
| 762 | 785 | BASIC_LDFLAGS += -Wl,--pic-executable | |
| 763 | 786 | COMPAT_CFLAGS += -DDETECT_MSYS_TTY \ | |
| 764 | 787 | -DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" \ | |