Skip to content

Commit 02e4656

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 8442555 commit 02e4656

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
@@ -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: 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
@@ -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)