Compare

b721adeb10fd377009d6e8efe1df932dba05bee1..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

Browse files
7 files changed, +176 −38
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 compat/mingw.c
@@ -3134,6 +3134,45 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
3134 3134 return -1;
3135 3135 }
3136 3136
3137 + #ifdef ENSURE_MSYSTEM_IS_SET
3138 + #if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR) || !defined(MINGW_PREFIX)
3139 + static size_t append_system_bin_dirs(char *path UNUSED, size_t size UNUSED)
3140 + {
3141 + return 0;
3142 + }
3143 + #else
3144 + static size_t append_system_bin_dirs(char *path, size_t size)
3145 + {
3146 + char prefix[32768];
3147 + const char *slash;
3148 + size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
3149 +
3150 + if (len == 0 || len >= sizeof(prefix) ||
3151 + !(slash = find_last_dir_sep(prefix)))
3152 + return 0;
3153 + /* strip trailing `git.exe` */
3154 + len = slash - prefix;
3155 +
3156 + /* strip trailing `cmd` or `<mingw-prefix>\bin` or `bin` or `libexec\git-core` */
3157 + if (strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\libexec\\git-core") ||
3158 + strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\bin"))
3159 + off += xsnprintf(path + off, size - off,
3160 + "%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
3161 + else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
3162 + strip_suffix_mem(prefix, &len, "\\bin") ||
3163 + strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
3164 + off += xsnprintf(path + off, size - off,
3165 + "%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
3166 + else
3167 + return 0;
3168 +
3169 + off += xsnprintf(path + off, size - off,
3170 + "%.*s\\usr\\bin;", (int)len, prefix);
3171 + return off;
3172 + }
3173 + #endif
3174 + #endif
3175 +
3137 3176 static void setup_windows_environment(void)
3138 3177 {
3139 3178 char *tmp = getenv("TMPDIR");
@@ -3185,6 +3224,35 @@ static void setup_windows_environment(void)
3185 3224 if (!tmp && (tmp = getenv("USERPROFILE")))
3186 3225 setenv("HOME", tmp, 1);
3187 3226 }
3227 +
3228 + if (!getenv("PLINK_PROTOCOL"))
3229 + setenv("PLINK_PROTOCOL", "ssh", 0);
3230 +
3231 + #ifdef ENSURE_MSYSTEM_IS_SET
3232 + if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
3233 + const char *home = getenv("HOME"), *path = getenv("PATH");
3234 + char buf[32768];
3235 + size_t off = 0;
3236 +
3237 + setenv("MSYSTEM", ENSURE_MSYSTEM_IS_SET, 1);
3238 +
3239 + if (home)
3240 + off += xsnprintf(buf + off, sizeof(buf) - off,
3241 + "%s\\bin;", home);
3242 + off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
3243 + if (path)
3244 + off += xsnprintf(buf + off, sizeof(buf) - off,
3245 + "%s", path);
3246 + else if (off > 0)
3247 + buf[off - 1] = '\0';
3248 + else
3249 + buf[0] = '\0';
3250 + setenv("PATH", buf, 1);
3251 + }
3252 + #endif
3253 +
3254 + if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
3255 + setenv("LC_CTYPE", "C.UTF-8", 1);
3188 3256 }
3189 3257
3190 3258 int mingw_platform_has_symlinks(void)
modified config.mak.uname
@@ -465,14 +465,8 @@ ifeq ($(uname_S),Windows)
465 465 GIT_VERSION := $(GIT_VERSION).MSVC
466 466 pathsep = ;
467 467 # Assume that this is built in Git for Windows' SDK
468 - ifeq (MINGW32,$(MSYSTEM))
469 - prefix = /mingw32
470 - else
471 - ifeq (CLANGARM64,$(MSYSTEM))
472 - prefix = /clangarm64
473 - else
474 - prefix = /mingw64
475 - endif
468 + ifneq (,$(MSYSTEM))
469 + prefix = $(MINGW_PREFIX)
476 470 endif
477 471 # Prepend MSVC 64-bit tool-chain to PATH.
478 472 #
@@ -524,7 +518,8 @@ ifeq ($(uname_S),Windows)
524 518 NO_POSIX_GOODIES = UnfortunatelyYes
525 519 NATIVE_CRLF = YesPlease
526 520 DEFAULT_HELP_FORMAT = html
527 - ifeq (/mingw64,$(subst 32,64,$(subst clangarm,mingw,$(prefix))))
521 + SKIP_DASHED_BUILT_INS = YabbaDabbaDoo
522 + ifneq (,$(MINGW_PREFIX))
528 523 # Move system config into top-level /etc/
529 524 ETC_GITCONFIG = ../etc/gitconfig
530 525 ETC_GITATTRIBUTES = ../etc/gitattributes
@@ -540,7 +535,9 @@ endif
540 535 compat/win32/pthread.o compat/win32/syslog.o \
541 536 compat/win32/trace2_win32_process_info.o \
542 537 compat/win32/dirent.o
543 - COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
538 + COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY \
539 + -DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\"" \
540 + -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
544 541 BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
545 542 # invalidcontinue.obj allows Git's source code to close the same file
546 543 # handle twice, or to access the osfhandle of an already-closed stdout
@@ -716,6 +713,7 @@ ifeq ($(uname_S),MINGW)
716 713 FSMONITOR_DAEMON_BACKEND = win32
717 714 FSMONITOR_OS_SETTINGS = win32
718 715
716 + SKIP_DASHED_BUILT_INS = YabbaDabbaDoo
719 717 RUNTIME_PREFIX = YesPlease
720 718 HAVE_WPGMPTR = YesWeDo
721 719 NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
@@ -754,26 +752,48 @@ ifeq ($(uname_S),MINGW)
754 752 ifneq (,$(findstring -O,$(filter-out -O0 -Og,$(CFLAGS))))
755 753 BASIC_LDFLAGS += -Wl,--dynamicbase
756 754 endif
757 - ifeq (MINGW32,$(MSYSTEM))
758 - prefix = /mingw32
759 - HOST_CPU = i686
760 - BASIC_LDFLAGS += -Wl,--pic-executable,-e,_mainCRTStartup
761 - endif
762 - ifeq (MINGW64,$(MSYSTEM))
763 - prefix = /mingw64
764 - HOST_CPU = x86_64
765 - BASIC_LDFLAGS += -Wl,--pic-executable,-e,mainCRTStartup
766 - else ifeq (CLANGARM64,$(MSYSTEM))
767 - prefix = /clangarm64
768 - HOST_CPU = aarch64
769 - BASIC_LDFLAGS += -Wl,--pic-executable,-e,mainCRTStartup
770 - else
771 - COMPAT_CFLAGS += -D_USE_32BIT_TIME_T
772 - BASIC_LDFLAGS += -Wl,--large-address-aware
755 + ifneq (,$(MSYSTEM))
756 + ifeq ($(MINGW_PREFIX),$(filter-out /%,$(MINGW_PREFIX)))
757 + # Override if empty or does not start with a slash
758 + MINGW_PREFIX := /$(shell echo '$(MSYSTEM)' | tr A-Z a-z)
759 + endif
760 + prefix = $(MINGW_PREFIX)
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 +
785 + BASIC_LDFLAGS += -Wl,--pic-executable
786 + COMPAT_CFLAGS += -DDETECT_MSYS_TTY \
787 + -DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" \
788 + -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\""
789 + ifeq (MINGW32,$(MSYSTEM))
790 + BASIC_LDFLAGS += -Wl,--large-address-aware
791 + endif
792 + # Move system config into top-level /etc/
793 + ETC_GITCONFIG = ../etc/gitconfig
794 + ETC_GITATTRIBUTES = ../etc/gitattributes
773 795 endif
774 - CC = gcc
775 - COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
776 - -fstack-protector-strong
796 + COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -fstack-protector-strong
777 797 EXTLIBS += -lntdll
778 798 EXTRA_PROGRAMS += headless-git$X
779 799 INSTALL = /bin/install
@@ -782,11 +802,6 @@ ifeq ($(uname_S),MINGW)
782 802 USE_GETTEXT_SCHEME = fallthrough
783 803 USE_LIBPCRE = YesPlease
784 804 NO_PYTHON =
785 - ifeq (/mingw64,$(subst 32,64,$(subst clangarm,mingw,$(prefix))))
786 - # Move system config into top-level /etc/
787 - ETC_GITCONFIG = ../etc/gitconfig
788 - ETC_GITATTRIBUTES = ../etc/gitattributes
789 - endif
790 805 endif
791 806 ifeq ($(uname_S),QNX)
792 807 COMPAT_CFLAGS += -DSA_RESTART=0
modified contrib/buildsystems/CMakeLists.txt
@@ -256,7 +256,14 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
256 256 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
257 257 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
258 258 OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
259 - HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET HAVE_RTLGENRANDOM)
259 + HAVE_WPGMPTR HAVE_RTLGENRANDOM)
260 + if(CMAKE_GENERATOR_PLATFORM STREQUAL "x64")
261 + add_compile_definitions(ENSURE_MSYSTEM_IS_SET="MINGW64" MINGW_PREFIX="mingw64")
262 + elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "arm64")
263 + add_compile_definitions(ENSURE_MSYSTEM_IS_SET="CLANGARM64" MINGW_PREFIX="clangarm64")
264 + elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
265 + add_compile_definitions(ENSURE_MSYSTEM_IS_SET="MINGW32" MINGW_PREFIX="mingw32")
266 + endif()
260 267 list(APPEND compat_SOURCES
261 268 compat/mingw.c
262 269 compat/winansi.c
modified meson.build
@@ -1318,7 +1318,6 @@ elif host_machine.system() == 'windows'
1318 1318
1319 1319 libgit_c_args += [
1320 1320 '-DDETECT_MSYS_TTY',
1321 - '-DENSURE_MSYSTEM_IS_SET',
1322 1321 '-DNATIVE_CRLF',
1323 1322 '-DNOGDI',
1324 1323 '-DNO_POSIX_GOODIES',
@@ -1328,6 +1327,20 @@ elif host_machine.system() == 'windows'
1328 1327 '-D__USE_MINGW_ANSI_STDIO=0',
1329 1328 ]
1330 1329
1330 + msystem = get_option('msystem')
1331 + if msystem != ''
1332 + mingw_prefix = get_option('mingw_prefix')
1333 + if mingw_prefix == ''
1334 + mingw_prefix = msystem.to_lower()
1335 + elif mingw_prefix.startswith('/')
1336 + mingw_prefix = mingw_prefix.substring(1)
1337 + endif
1338 + libgit_c_args += [
1339 + '-DENSURE_MSYSTEM_IS_SET="' + msystem + '"',
1340 + '-DMINGW_PREFIX="' + mingw_prefix + '"'
1341 + ]
1342 + endif
1343 +
1331 1344 libgit_dependencies += compiler.find_library('ntdll')
1332 1345 libgit_include_directories += 'compat/win32'
1333 1346 if compiler.get_id() == 'msvc'
modified meson_options.txt
@@ -21,6 +21,10 @@ option('runtime_prefix', type: 'boolean', value: false,
21 21 description: 'Resolve ancillary tooling and support files relative to the location of the runtime binary instead of hard-coding them into the binary.')
22 22 option('sane_tool_path', type: 'array', value: [],
23 23 description: 'An array of paths to pick up tools from in case the normal tools are broken or lacking.')
24 + option('msystem', type: 'string', value: '',
25 + description: 'Fall-back on Windows when MSYSTEM is not set.')
26 + option('mingw_prefix', type: 'string', value: '',
27 + description: 'Fall-back on Windows when MINGW_PREFIX is not set.')
24 28
25 29 # Build information compiled into Git and other parts like documentation.
26 30 option('build_date', type: 'string', value: '',
modified t/t0060-path-utils.sh
@@ -602,7 +602,8 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX wor
602 602 echo "echo HERE" | write_script pretend/libexec/git-core/git-here &&
603 603 GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
604 604 echo HERE >expect &&
605 - test_cmp expect actual'
605 + test_cmp expect actual
606 + '
606 607
607 608 test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works' '
608 609 git config yes.path "%(prefix)/yes" &&
@@ -611,4 +612,34 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works'
611 612 test_cmp expect actual
612 613 '
613 614
615 + test_expect_success MINGW,RUNTIME_PREFIX 'MSYSTEM/PATH is adjusted if necessary' '
616 + if test -z "$MINGW_PREFIX"
617 + then
618 + MINGW_PREFIX="/$(echo "${MSYSTEM:-MINGW64}" | tr A-Z a-z)"
619 + fi &&
620 + mkdir -p "$HOME"/bin pretend"$MINGW_PREFIX"/bin \
621 + pretend"$MINGW_PREFIX"/libexec/git-core pretend/usr/bin &&
622 + cp "$GIT_EXEC_PATH"/git.exe pretend"$MINGW_PREFIX"/bin/ &&
623 + cp "$GIT_EXEC_PATH"/git.exe pretend"$MINGW_PREFIX"/libexec/git-core/ &&
624 + # copy the .dll files, if any (happens when building via CMake)
625 + if test -n "$(ls "$GIT_EXEC_PATH"/*.dll 2>/dev/null)"
626 + then
627 + cp "$GIT_EXEC_PATH"/*.dll pretend"$MINGW_PREFIX"/bin/ &&
628 + cp "$GIT_EXEC_PATH"/*.dll pretend"$MINGW_PREFIX"/libexec/git-core/
629 + fi &&
630 + echo "env | grep MSYSTEM=" | write_script "$HOME"/bin/git-test-home &&
631 + echo "echo ${MINGW_PREFIX#/}" | write_script pretend"$MINGW_PREFIX"/bin/git-test-bin &&
632 + echo "echo usr" | write_script pretend/usr/bin/git-test-bin2 &&
633 +
634 + (
635 + MSYSTEM= &&
636 + GIT_EXEC_PATH= &&
637 + pretend"$MINGW_PREFIX"/libexec/git-core/git.exe test-home >actual &&
638 + pretend"$MINGW_PREFIX"/libexec/git-core/git.exe test-bin >>actual &&
639 + pretend"$MINGW_PREFIX"/bin/git.exe test-bin2 >>actual
640 + ) &&
641 + test_write_lines MSYSTEM=$MSYSTEM "${MINGW_PREFIX#/}" usr >expect &&
642 + test_cmp expect actual
643 + '
644 +
614 645 test_done