Skip to content

Commit 9cf4bd2

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Add full mingw-w64-git (i.e. regular MSYS2 ecosystem) support (#5971)
Every once in a while, there are bug reports in Git for Windows' bug tracker that describe an issue running [inside MSYS2 proper](https://gitforwindows.org/install-inside-msys2-proper), totally ignoring the big, honking warning on top of [the page](https://gitforwindows.org/install-inside-msys2-proper) that spells out clearly that this is an unsupported use case. At the same time, we cannot easily deflect and say "just use MSYS2 directly" (and leave the "and stop pestering us" out). We cannot do that because there is only an _MSYS_ `git` package in MSYS2 (i.e. a Git that uses the quite slow POSIX emulation layer provided by the MSYS2 runtime), but no `mingw-w64-git` package (which would be equivalent in speed to Git for Windows). In msys2/MINGW-packages#26470, I am preparing to change that. As part of that PR, I noticed and fixed a couple of issues _in `git-for-windows/git` that prevented full support for `mingw-w64-git` in MSYS2, such as problems with CLANG64 and UCRT64. While at it, I simplified the entire setup to trust MSYS2's `MINGW_PREFIX` & related environment variables instead of hard-coding values like the installation prefix and what `MSYSTEM` to fall back on if it is unset.
2 parents b3ec34f + db3fc1d commit 9cf4bd2

File tree

6 files changed

+66
-114
lines changed

6 files changed

+66
-114
lines changed

compat/mingw.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3625,7 +3625,7 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
36253625
}
36263626

36273627
#ifdef ENSURE_MSYSTEM_IS_SET
3628-
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
3628+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR) || !defined(MINGW_PREFIX)
36293629
static size_t append_system_bin_dirs(char *path UNUSED, size_t size UNUSED)
36303630
{
36313631
return 0;
@@ -3643,25 +3643,16 @@ static size_t append_system_bin_dirs(char *path, size_t size)
36433643
/* strip trailing `git.exe` */
36443644
len = slash - prefix;
36453645

3646-
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
3647-
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
3648-
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
3646+
/* strip trailing `cmd` or `<mingw-prefix>\bin` or `bin` or `libexec\git-core` */
3647+
if (strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\libexec\\git-core") ||
3648+
strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\bin"))
36493649
off += xsnprintf(path + off, size - off,
3650-
"%.*s\\mingw64\\bin;", (int)len, prefix);
3651-
else if (strip_suffix_mem(prefix, &len, "\\clangarm64\\libexec\\git-core") ||
3652-
strip_suffix_mem(prefix, &len, "\\clangarm64\\bin"))
3653-
off += xsnprintf(path + off, size - off,
3654-
"%.*s\\clangarm64\\bin;", (int)len, prefix);
3655-
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
3656-
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
3657-
off += xsnprintf(path + off, size - off,
3658-
"%.*s\\mingw32\\bin;", (int)len, prefix);
3650+
"%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
36593651
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
36603652
strip_suffix_mem(prefix, &len, "\\bin") ||
36613653
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
36623654
off += xsnprintf(path + off, size - off,
3663-
"%.*s\\mingw%d\\bin;", (int)len, prefix,
3664-
(int)(sizeof(void *) * 8));
3655+
"%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
36653656
else
36663657
return 0;
36673658

@@ -3757,13 +3748,7 @@ static void setup_windows_environment(void)
37573748
char buf[32768];
37583749
size_t off = 0;
37593750

3760-
#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
3761-
setenv("MSYSTEM", "CLANGARM64", 1);
3762-
#elif defined(__MINGW64__) || defined(_M_AMD64)
3763-
setenv("MSYSTEM", "MINGW64", 1);
3764-
#else
3765-
setenv("MSYSTEM", "MINGW32", 1);
3766-
#endif
3751+
setenv("MSYSTEM", ENSURE_MSYSTEM_IS_SET, 1);
37673752

37683753
if (home)
37693754
off += xsnprintf(buf + off, sizeof(buf) - off,

config.mak.uname

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,8 @@ ifeq ($(uname_S),Windows)
452452
GIT_VERSION := $(GIT_VERSION).MSVC
453453
pathsep = ;
454454
# Assume that this is built in Git for Windows' SDK
455-
ifeq (MINGW32,$(MSYSTEM))
456-
prefix = /mingw32
457-
else
458-
ifeq (CLANGARM64,$(MSYSTEM))
459-
prefix = /clangarm64
460-
else
461-
prefix = /mingw64
462-
endif
455+
ifneq (,$(MSYSTEM))
456+
prefix = $(MINGW_PREFIX)
463457
endif
464458
# Prepend MSVC 64-bit tool-chain to PATH.
465459
#
@@ -513,7 +507,7 @@ ifeq ($(uname_S),Windows)
513507
NATIVE_CRLF = YesPlease
514508
DEFAULT_HELP_FORMAT = html
515509
SKIP_DASHED_BUILT_INS = YabbaDabbaDoo
516-
ifeq (/mingw64,$(subst 32,64,$(subst clangarm,mingw,$(prefix))))
510+
ifneq (,$(MINGW_PREFIX))
517511
# Move system config into top-level /etc/
518512
ETC_GITCONFIG = ../etc/gitconfig
519513
ETC_GITATTRIBUTES = ../etc/gitattributes
@@ -529,7 +523,9 @@ endif
529523
compat/win32/pthread.o compat/win32/syslog.o \
530524
compat/win32/trace2_win32_process_info.o \
531525
compat/win32/dirent.o compat/win32/fscache.o compat/win32/wsl.o
532-
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DENSURE_MSYSTEM_IS_SET -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
526+
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY \
527+
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\"" \
528+
-DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
533529
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO
534530
# invalidcontinue.obj allows Git's source code to close the same file
535531
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -747,26 +743,25 @@ ifeq ($(uname_S),MINGW)
747743
ifneq (,$(findstring -O,$(filter-out -O0 -Og,$(CFLAGS))))
748744
BASIC_LDFLAGS += -Wl,--dynamicbase
749745
endif
750-
ifeq (MINGW32,$(MSYSTEM))
751-
prefix = /mingw32
752-
HOST_CPU = i686
753-
BASIC_LDFLAGS += -Wl,--pic-executable,-e,_mainCRTStartup
754-
endif
755-
ifeq (MINGW64,$(MSYSTEM))
756-
prefix = /mingw64
757-
HOST_CPU = x86_64
758-
BASIC_LDFLAGS += -Wl,--pic-executable,-e,mainCRTStartup
759-
else ifeq (CLANGARM64,$(MSYSTEM))
760-
prefix = /clangarm64
761-
HOST_CPU = aarch64
762-
BASIC_LDFLAGS += -Wl,--pic-executable,-e,mainCRTStartup
763-
else
764-
COMPAT_CFLAGS += -D_USE_32BIT_TIME_T
765-
BASIC_LDFLAGS += -Wl,--large-address-aware
746+
ifneq (,$(MSYSTEM))
747+
ifeq ($(MINGW_PREFIX),$(filter-out /%,$(MINGW_PREFIX)))
748+
# Override if empty or does not start with a slash
749+
MINGW_PREFIX := /$(shell echo '$(MSYSTEM)' | tr A-Z a-z)
750+
endif
751+
prefix = $(MINGW_PREFIX)
752+
HOST_CPU = $(patsubst %-w64-mingw32,%,$(MINGW_CHOST))
753+
BASIC_LDFLAGS += -Wl,--pic-executable
754+
COMPAT_CFLAGS += -DDETECT_MSYS_TTY \
755+
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" \
756+
-DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\""
757+
ifeq (MINGW32,$(MSYSTEM))
758+
BASIC_LDFLAGS += -Wl,--large-address-aware
759+
endif
760+
# Move system config into top-level /etc/
761+
ETC_GITCONFIG = ../etc/gitconfig
762+
ETC_GITATTRIBUTES = ../etc/gitattributes
766763
endif
767-
CC = gcc
768-
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
769-
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
764+
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -fstack-protector-strong
770765
EXTLIBS += -lntdll
771766
EXTRA_PROGRAMS += headless-git$X
772767
INSTALL = /bin/install
@@ -776,62 +771,6 @@ ifeq ($(uname_S),MINGW)
776771
USE_LIBPCRE = YesPlease
777772
USE_MIMALLOC = YesPlease
778773
NO_PYTHON =
779-
ifeq (/mingw64,$(subst 32,64,$(subst clangarm,mingw,$(prefix))))
780-
# Move system config into top-level /etc/
781-
ETC_GITCONFIG = ../etc/gitconfig
782-
ETC_GITATTRIBUTES = ../etc/gitattributes
783-
endif
784-
MINGW_PREFIX := $(subst /,,$(prefix))
785-
786-
DESTDIR_WINDOWS = $(shell cygpath -aw '$(DESTDIR_SQ)')
787-
DESTDIR_MIXED = $(shell cygpath -am '$(DESTDIR_SQ)')
788-
install-mingit-test-artifacts:
789-
install -m755 -d '$(DESTDIR_SQ)/usr/bin'
790-
printf '%s\n%s\n' >'$(DESTDIR_SQ)/usr/bin/perl' \
791-
"#!/mingw64/bin/busybox sh" \
792-
"exec \"$(shell cygpath -am /usr/bin/perl.exe)\" \"\$$@\""
793-
794-
install -m755 -d '$(DESTDIR_SQ)'
795-
printf '%s%s\n%s\n%s\n%s\n%s\n' >'$(DESTDIR_SQ)/init.bat' \
796-
"PATH=$(DESTDIR_WINDOWS)\\$(MINGW_PREFIX)\\bin;" \
797-
"C:\\WINDOWS;C:\\WINDOWS\\system32" \
798-
"@set GIT_TEST_INSTALLED=$(DESTDIR_MIXED)/$(MINGW_PREFIX)/bin" \
799-
"@`echo "$(DESTDIR_WINDOWS)" | sed 's/:.*/:/'`" \
800-
"@cd `echo "$(DESTDIR_WINDOWS)" | sed 's/^.://'`\\test-git\\t" \
801-
"@echo Now, run 'helper\\test-run-command testsuite'"
802-
803-
install -m755 -d '$(DESTDIR_SQ)/test-git'
804-
sed 's/^\(NO_PERL\|NO_PYTHON\)=.*/\1=YesPlease/' \
805-
<GIT-BUILD-OPTIONS >'$(DESTDIR_SQ)/test-git/GIT-BUILD-OPTIONS'
806-
807-
install -m755 -d '$(DESTDIR_SQ)/test-git/t/helper'
808-
install -m755 $(TEST_PROGRAMS) '$(DESTDIR_SQ)/test-git/t/helper'
809-
(cd t && $(TAR) cf - t[0-9][0-9][0-9][0-9] lib-diff) | \
810-
(cd '$(DESTDIR_SQ)/test-git/t' && $(TAR) xf -)
811-
install -m755 t/t556x_common t/*.sh '$(DESTDIR_SQ)/test-git/t'
812-
813-
install -m755 -d '$(DESTDIR_SQ)/test-git/templates'
814-
(cd templates && $(TAR) cf - blt) | \
815-
(cd '$(DESTDIR_SQ)/test-git/templates' && $(TAR) xf -)
816-
817-
# po/build/locale for t0200
818-
install -m755 -d '$(DESTDIR_SQ)/test-git/po/build/locale'
819-
(cd po/build/locale && $(TAR) cf - .) | \
820-
(cd '$(DESTDIR_SQ)/test-git/po/build/locale' && $(TAR) xf -)
821-
822-
# git-daemon.exe for t5802, git-http-backend.exe for t5560
823-
install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
824-
install -m755 git-daemon.exe git-http-backend.exe \
825-
'$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
826-
827-
# git-upload-archive (dashed) for t5000
828-
install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
829-
install -m755 git-upload-archive.exe '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
830-
831-
# git-difftool--helper for t7800
832-
install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/libexec/git-core'
833-
install -m755 git-difftool--helper \
834-
'$(DESTDIR_SQ)/$(MINGW_PREFIX)/libexec/git-core'
835774
endif
836775
ifeq ($(uname_S),QNX)
837776
COMPAT_CFLAGS += -DSA_RESTART=0

contrib/buildsystems/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,14 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
282282
_CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
283283
NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
284284
USE_NED_ALLOCATOR OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
285-
HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET HAVE_RTLGENRANDOM)
285+
HAVE_WPGMPTR HAVE_RTLGENRANDOM)
286+
if(CMAKE_GENERATOR_PLATFORM STREQUAL "x64")
287+
add_compile_definitions(ENSURE_MSYSTEM_IS_SET="MINGW64" MINGW_PREFIX="mingw64")
288+
elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "arm64")
289+
add_compile_definitions(ENSURE_MSYSTEM_IS_SET="CLANGARM64" MINGW_PREFIX="clangarm64")
290+
elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
291+
add_compile_definitions(ENSURE_MSYSTEM_IS_SET="MINGW32" MINGW_PREFIX="mingw32")
292+
endif()
286293
list(APPEND compat_SOURCES
287294
compat/mingw.c
288295
compat/winansi.c

environment.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,22 @@ int max_allowed_tree_depth =
9191
* the stack overflow can occur.
9292
*/
9393
512;
94-
#elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__) && defined(__aarch64__)
94+
#elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__)
9595
/*
96-
* Similar to Visual C, it seems that on Windows/ARM64 the clang-based
97-
* builds have a smaller stack space available. When running out of
98-
* that stack space, a `STATUS_STACK_OVERFLOW` is produced. When the
96+
* Similar to Visual C, it seems that clang-based builds on Windows
97+
* have a smaller stack space available. When running out of that
98+
* stack space, a `STATUS_STACK_OVERFLOW` is produced. When the
9999
* Git command was run from an MSYS2 Bash, this unfortunately results
100100
* in an exit code 127. Let's prevent that by lowering the maximal
101-
* tree depth; This value seems to be low enough.
101+
* tree depth; Unfortunately, it seems that the exact limit differs
102+
* for aarch64 vs x86_64, and the difference is too large to simply
103+
* use a single limit.
102104
*/
105+
#if defined(__aarch64__)
103106
1280;
107+
#else
108+
1152;
109+
#endif
104110
#else
105111
2048;
106112
#endif

meson.build

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,6 @@ elif host_machine.system() == 'windows'
12711271

12721272
libgit_c_args += [
12731273
'-DDETECT_MSYS_TTY',
1274-
'-DENSURE_MSYSTEM_IS_SET',
12751274
'-DNATIVE_CRLF',
12761275
'-DNOGDI',
12771276
'-DNO_POSIX_GOODIES',
@@ -1281,6 +1280,18 @@ elif host_machine.system() == 'windows'
12811280
'-D__USE_MINGW_ANSI_STDIO=0',
12821281
]
12831282

1283+
msystem = get_option('msystem')
1284+
if msystem != ''
1285+
mingw_prefix = get_option('mingw_prefix')
1286+
if mingw_prefix == ''
1287+
mingw_prefix = '/' + msystem.to_lower()
1288+
endif
1289+
libgit_c_args += [
1290+
'-DENSURE_MSYSTEM_IS_SET="' + msystem + '"',
1291+
'-DMINGW_PREFIX="' + mingw_prefix + '"'
1292+
]
1293+
endif
1294+
12841295
libgit_dependencies += compiler.find_library('ntdll')
12851296
libgit_include_directories += 'compat/win32'
12861297
if compiler.get_id() == 'msvc'

meson_options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ option('runtime_prefix', type: 'boolean', value: false,
2121
description: 'Resolve ancillary tooling and support files relative to the location of the runtime binary instead of hard-coding them into the binary.')
2222
option('sane_tool_path', type: 'array', value: [],
2323
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.')
2428

2529
# Build information compiled into Git and other parts like documentation.
2630
option('build_date', type: 'string', value: '',

0 commit comments

Comments
 (0)