WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 531d0f2

Browse files
dschoGit for Windows Build Agent
authored andcommitted
mingw: rely on MSYS2's metadata instead of hard-coding it
MSYS2 defines some helpful environment variables, e.g. `MSYSTEM`. There is code in Git for Windows to ensure that that `MSYSTEM` variable is set, hard-coding a default. However, the existing solution jumps through hoops to reconstruct the proper default, and is even incomplete doing so, as we found out when we extended it to support CLANGARM64. This is absolutely unnecessary because there is already a perfectly valid `MSYSTEM` value we can use at build time. This is even true when building the MINGW32 variant on a MINGW64 system because `makepkg-mingw` will override the `MSYSTEM` value as per the `MINGW_ARCH` array. The same is equally true for the `/mingw64`, `/mingw32` and `/clangarm64` prefix: those values are already available via the `MINGW_PREFIX` environment variable, and we just need to pass that setting through. Only when `MINGW_PREFIX` is not set (as is the case in Git for Windows' minimal SDK, where only `MSYSTEM` is guaranteed to be set correctly), we use as fall-back the top-level directory whose name is the down-cased value of the `MSYSTEM` variable. Incidentally, this also broadens the support to all the configurations supported by the MSYS2 project, i.e. clang64 & ucrt64, too. Note: This keeps the same, hard-coded MSYSTEM platform support for CMake as before, but drops it for Meson (because it is unclear how Meson could do this in a more flexible manner). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 0490b75 commit 531d0f2

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
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: 12 additions & 10 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
#
@@ -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
@@ -748,10 +744,16 @@ ifeq ($(uname_S),MINGW)
748744
BASIC_LDFLAGS += -Wl,--dynamicbase
749745
endif
750746
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
751751
prefix = $(MINGW_PREFIX)
752752
HOST_CPU = $(patsubst %-w64-mingw32,%,$(MINGW_CHOST))
753753
BASIC_LDFLAGS += -Wl,--pic-executable
754-
COMPAT_CFLAGS += -DDETECT_MSYS_TTY -DENSURE_MSYSTEM_IS_SET
754+
COMPAT_CFLAGS += -DDETECT_MSYS_TTY \
755+
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" \
756+
-DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\""
755757
ifeq (MINGW32,$(MSYSTEM))
756758
BASIC_LDFLAGS += -Wl,--large-address-aware
757759
endif

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

meson.build

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

12781278
libgit_c_args += [
12791279
'-DDETECT_MSYS_TTY',
1280-
'-DENSURE_MSYSTEM_IS_SET',
12811280
'-DNATIVE_CRLF',
12821281
'-DNOGDI',
12831282
'-DNO_POSIX_GOODIES',
@@ -1287,6 +1286,18 @@ elif host_machine.system() == 'windows'
12871286
'-D__USE_MINGW_ANSI_STDIO=0',
12881287
]
12891288

1289+
msystem = get_option('msystem')
1290+
if msystem != ''
1291+
mingw_prefix = get_option('mingw_prefix')
1292+
if mingw_prefix == ''
1293+
mingw_prefix = '/' + msystem.to_lower()
1294+
endif
1295+
libgit_c_args += [
1296+
'-DENSURE_MSYSTEM_IS_SET="' + msystem + '"',
1297+
'-DMINGW_PREFIX="' + mingw_prefix + '"'
1298+
]
1299+
endif
1300+
12901301
libgit_dependencies += compiler.find_library('ntdll')
12911302
libgit_include_directories += 'compat/win32'
12921303
if compiler.get_id() == 'msvc'

meson_options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ 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.')
2424
option('submodule-encoding', type: 'boolean', value: false,
2525
description: 'Enable submoduleEncoding extension by default at build time.')
26+
option('msystem', type: 'string', value: '',
27+
description: 'Fall-back on Windows when MSYSTEM is not set.')
28+
option('mingw_prefix', type: 'string', value: '',
29+
description: 'Fall-back on Windows when MINGW_PREFIX is not set.')
2630

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

0 commit comments

Comments
 (0)