Skip to content

Commit c023a70

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 530d94d + 14ba93f commit c023a70

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
@@ -3615,7 +3615,7 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
36153615
}
36163616

36173617
#ifdef ENSURE_MSYSTEM_IS_SET
3618-
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
3618+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR) || !defined(MINGW_PREFIX)
36193619
static size_t append_system_bin_dirs(char *path UNUSED, size_t size UNUSED)
36203620
{
36213621
return 0;
@@ -3633,25 +3633,16 @@ static size_t append_system_bin_dirs(char *path, size_t size)
36333633
/* strip trailing `git.exe` */
36343634
len = slash - prefix;
36353635

3636-
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
3637-
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
3638-
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
3636+
/* strip trailing `cmd` or `<mingw-prefix>\bin` or `bin` or `libexec\git-core` */
3637+
if (strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\libexec\\git-core") ||
3638+
strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\bin"))
36393639
off += xsnprintf(path + off, size - off,
3640-
"%.*s\\mingw64\\bin;", (int)len, prefix);
3641-
else if (strip_suffix_mem(prefix, &len, "\\clangarm64\\libexec\\git-core") ||
3642-
strip_suffix_mem(prefix, &len, "\\clangarm64\\bin"))
3643-
off += xsnprintf(path + off, size - off,
3644-
"%.*s\\clangarm64\\bin;", (int)len, prefix);
3645-
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
3646-
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
3647-
off += xsnprintf(path + off, size - off,
3648-
"%.*s\\mingw32\\bin;", (int)len, prefix);
3640+
"%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
36493641
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
36503642
strip_suffix_mem(prefix, &len, "\\bin") ||
36513643
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
36523644
off += xsnprintf(path + off, size - off,
3653-
"%.*s\\mingw%d\\bin;", (int)len, prefix,
3654-
(int)(sizeof(void *) * 8));
3645+
"%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
36553646
else
36563647
return 0;
36573648

@@ -3747,13 +3738,7 @@ static void setup_windows_environment(void)
37473738
char buf[32768];
37483739
size_t off = 0;
37493740

3750-
#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
3751-
setenv("MSYSTEM", "CLANGARM64", 1);
3752-
#elif defined(__MINGW64__) || defined(_M_AMD64)
3753-
setenv("MSYSTEM", "MINGW64", 1);
3754-
#else
3755-
setenv("MSYSTEM", "MINGW32", 1);
3756-
#endif
3741+
setenv("MSYSTEM", ENSURE_MSYSTEM_IS_SET, 1);
37573742

37583743
if (home)
37593744
off += xsnprintf(buf + off, sizeof(buf) - off,

config.mak.uname

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,8 @@ ifeq ($(uname_S),Windows)
437437
GIT_VERSION := $(GIT_VERSION).MSVC
438438
pathsep = ;
439439
# Assume that this is built in Git for Windows' SDK
440-
ifeq (MINGW32,$(MSYSTEM))
441-
prefix = /mingw32
442-
else
443-
ifeq (CLANGARM64,$(MSYSTEM))
444-
prefix = /clangarm64
445-
else
446-
prefix = /mingw64
447-
endif
440+
ifneq (,$(MSYSTEM))
441+
prefix = $(MINGW_PREFIX)
448442
endif
449443
# Prepend MSVC 64-bit tool-chain to PATH.
450444
#
@@ -498,7 +492,7 @@ ifeq ($(uname_S),Windows)
498492
NATIVE_CRLF = YesPlease
499493
DEFAULT_HELP_FORMAT = html
500494
SKIP_DASHED_BUILT_INS = YabbaDabbaDoo
501-
ifeq (/mingw64,$(subst 32,64,$(subst clangarm,mingw,$(prefix))))
495+
ifneq (,$(MINGW_PREFIX))
502496
# Move system config into top-level /etc/
503497
ETC_GITCONFIG = ../etc/gitconfig
504498
ETC_GITATTRIBUTES = ../etc/gitattributes
@@ -514,7 +508,9 @@ endif
514508
compat/win32/pthread.o compat/win32/syslog.o \
515509
compat/win32/trace2_win32_process_info.o \
516510
compat/win32/dirent.o compat/win32/fscache.o compat/win32/wsl.o
517-
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\"
511+
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY \
512+
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\"" \
513+
-DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
518514
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO
519515
# invalidcontinue.obj allows Git's source code to close the same file
520516
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -732,26 +728,25 @@ ifeq ($(uname_S),MINGW)
732728
ifneq (,$(findstring -O,$(filter-out -O0 -Og,$(CFLAGS))))
733729
BASIC_LDFLAGS += -Wl,--dynamicbase
734730
endif
735-
ifeq (MINGW32,$(MSYSTEM))
736-
prefix = /mingw32
737-
HOST_CPU = i686
738-
BASIC_LDFLAGS += -Wl,--pic-executable,-e,_mainCRTStartup
739-
endif
740-
ifeq (MINGW64,$(MSYSTEM))
741-
prefix = /mingw64
742-
HOST_CPU = x86_64
743-
BASIC_LDFLAGS += -Wl,--pic-executable,-e,mainCRTStartup
744-
else ifeq (CLANGARM64,$(MSYSTEM))
745-
prefix = /clangarm64
746-
HOST_CPU = aarch64
747-
BASIC_LDFLAGS += -Wl,--pic-executable,-e,mainCRTStartup
748-
else
749-
COMPAT_CFLAGS += -D_USE_32BIT_TIME_T
750-
BASIC_LDFLAGS += -Wl,--large-address-aware
731+
ifneq (,$(MSYSTEM))
732+
ifeq ($(MINGW_PREFIX),$(filter-out /%,$(MINGW_PREFIX)))
733+
# Override if empty or does not start with a slash
734+
MINGW_PREFIX := /$(shell echo '$(MSYSTEM)' | tr A-Z a-z)
735+
endif
736+
prefix = $(MINGW_PREFIX)
737+
HOST_CPU = $(patsubst %-w64-mingw32,%,$(MINGW_CHOST))
738+
BASIC_LDFLAGS += -Wl,--pic-executable
739+
COMPAT_CFLAGS += -DDETECT_MSYS_TTY \
740+
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" \
741+
-DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\""
742+
ifeq (MINGW32,$(MSYSTEM))
743+
BASIC_LDFLAGS += -Wl,--large-address-aware
744+
endif
745+
# Move system config into top-level /etc/
746+
ETC_GITCONFIG = ../etc/gitconfig
747+
ETC_GITATTRIBUTES = ../etc/gitattributes
751748
endif
752-
CC = gcc
753-
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
754-
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
749+
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -fstack-protector-strong
755750
EXTLIBS += -lntdll
756751
EXTRA_PROGRAMS += headless-git$X
757752
INSTALL = /bin/install
@@ -761,62 +756,6 @@ ifeq ($(uname_S),MINGW)
761756
USE_LIBPCRE = YesPlease
762757
USE_MIMALLOC = YesPlease
763758
NO_PYTHON =
764-
ifeq (/mingw64,$(subst 32,64,$(subst clangarm,mingw,$(prefix))))
765-
# Move system config into top-level /etc/
766-
ETC_GITCONFIG = ../etc/gitconfig
767-
ETC_GITATTRIBUTES = ../etc/gitattributes
768-
endif
769-
MINGW_PREFIX := $(subst /,,$(prefix))
770-
771-
DESTDIR_WINDOWS = $(shell cygpath -aw '$(DESTDIR_SQ)')
772-
DESTDIR_MIXED = $(shell cygpath -am '$(DESTDIR_SQ)')
773-
install-mingit-test-artifacts:
774-
install -m755 -d '$(DESTDIR_SQ)/usr/bin'
775-
printf '%s\n%s\n' >'$(DESTDIR_SQ)/usr/bin/perl' \
776-
"#!/mingw64/bin/busybox sh" \
777-
"exec \"$(shell cygpath -am /usr/bin/perl.exe)\" \"\$$@\""
778-
779-
install -m755 -d '$(DESTDIR_SQ)'
780-
printf '%s%s\n%s\n%s\n%s\n%s\n' >'$(DESTDIR_SQ)/init.bat' \
781-
"PATH=$(DESTDIR_WINDOWS)\\$(MINGW_PREFIX)\\bin;" \
782-
"C:\\WINDOWS;C:\\WINDOWS\\system32" \
783-
"@set GIT_TEST_INSTALLED=$(DESTDIR_MIXED)/$(MINGW_PREFIX)/bin" \
784-
"@`echo "$(DESTDIR_WINDOWS)" | sed 's/:.*/:/'`" \
785-
"@cd `echo "$(DESTDIR_WINDOWS)" | sed 's/^.://'`\\test-git\\t" \
786-
"@echo Now, run 'helper\\test-run-command testsuite'"
787-
788-
install -m755 -d '$(DESTDIR_SQ)/test-git'
789-
sed 's/^\(NO_PERL\|NO_PYTHON\)=.*/\1=YesPlease/' \
790-
<GIT-BUILD-OPTIONS >'$(DESTDIR_SQ)/test-git/GIT-BUILD-OPTIONS'
791-
792-
install -m755 -d '$(DESTDIR_SQ)/test-git/t/helper'
793-
install -m755 $(TEST_PROGRAMS) '$(DESTDIR_SQ)/test-git/t/helper'
794-
(cd t && $(TAR) cf - t[0-9][0-9][0-9][0-9] lib-diff) | \
795-
(cd '$(DESTDIR_SQ)/test-git/t' && $(TAR) xf -)
796-
install -m755 t/t556x_common t/*.sh '$(DESTDIR_SQ)/test-git/t'
797-
798-
install -m755 -d '$(DESTDIR_SQ)/test-git/templates'
799-
(cd templates && $(TAR) cf - blt) | \
800-
(cd '$(DESTDIR_SQ)/test-git/templates' && $(TAR) xf -)
801-
802-
# po/build/locale for t0200
803-
install -m755 -d '$(DESTDIR_SQ)/test-git/po/build/locale'
804-
(cd po/build/locale && $(TAR) cf - .) | \
805-
(cd '$(DESTDIR_SQ)/test-git/po/build/locale' && $(TAR) xf -)
806-
807-
# git-daemon.exe for t5802, git-http-backend.exe for t5560
808-
install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
809-
install -m755 git-daemon.exe git-http-backend.exe \
810-
'$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
811-
812-
# git-upload-archive (dashed) for t5000
813-
install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
814-
install -m755 git-upload-archive.exe '$(DESTDIR_SQ)/$(MINGW_PREFIX)/bin'
815-
816-
# git-difftool--helper for t7800
817-
install -m755 -d '$(DESTDIR_SQ)/$(MINGW_PREFIX)/libexec/git-core'
818-
install -m755 git-difftool--helper \
819-
'$(DESTDIR_SQ)/$(MINGW_PREFIX)/libexec/git-core'
820759
endif
821760
ifeq ($(uname_S),QNX)
822761
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
@@ -1273,7 +1273,6 @@ elif host_machine.system() == 'windows'
12731273

12741274
libgit_c_args += [
12751275
'-DDETECT_MSYS_TTY',
1276-
'-DENSURE_MSYSTEM_IS_SET',
12771276
'-DNATIVE_CRLF',
12781277
'-DNOGDI',
12791278
'-DNO_POSIX_GOODIES',
@@ -1283,6 +1282,18 @@ elif host_machine.system() == 'windows'
12831282
'-D__USE_MINGW_ANSI_STDIO=0',
12841283
]
12851284

1285+
msystem = get_option('msystem')
1286+
if msystem != ''
1287+
mingw_prefix = get_option('mingw_prefix')
1288+
if mingw_prefix == ''
1289+
mingw_prefix = '/' + msystem.to_lower()
1290+
endif
1291+
libgit_c_args += [
1292+
'-DENSURE_MSYSTEM_IS_SET="' + msystem + '"',
1293+
'-DMINGW_PREFIX="' + mingw_prefix + '"'
1294+
]
1295+
endif
1296+
12861297
libgit_dependencies += compiler.find_library('ntdll')
12871298
libgit_include_directories += 'compat/win32'
12881299
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)