Skip to contentSkip to conversation

Fix compile errors with MSys2

PATCH Superseded Johannes Schindelin 6 participants 23 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Fix compile errors with MSys2

Git for Windows 2.x is based on MSys2, and its first official version was released already almost half a year ago, yet it is still impossible even to compile Git from the official, platform-independent Git source code.

This patch series merely fixes the compile errors, and will be followed by other patch series that fix the compile warnings and test failures. The idea is to move all Windows-specific patches of Git for Windows' fork to upstream Git proper.

Johannes Schindelin (5):
config.mak.uname: support MSys2 config.mak.uname: supporting 64-bit MSys2
nedmalloc: allow compiling with MSys2's compiler compat/mingw: support MSys2-based MinGW build compat/winansi: support compiling with MSys2

 compat/mingw.h              | 24 +++++++++++++++++++++++-
 compat/nedmalloc/malloc.c.h |  6 ++++++
 compat/winansi.c            |  2 ++
 config.mak.uname            | 31 ++++++++++++++++++++++++++++---
 4 files changed, 59 insertions(+), 4 deletions(-)
Signature · 1 line
2.6.3.windows.1.300.g1c25e49
#1 lore raw

PATCH 1/5 config.mak.uname: support MSys2

For a long time, Git for Windows lagged behind Git's 2.x releases because the Git for Windows developers wanted to let that big jump coincide with a well-needed jump away from MSys to MSys2.

To understand why this is such a big issue, it needs to be noted that many parts of Git are not written in portable C, but instead Git relies on a POSIX shell and Perl to be available.

To support the scripts, Git for Windows has to ship a minimal POSIX emulation layer with Bash and Perl thrown in, and when the Git for Windows effort started in August 2007, this developer settled on using MSys, a stripped down version of Cygwin. Consequently, the original name of the project was "msysGit" (which, sadly, caused a *lot* of confusion because few Windows users know about MSys, and even less care).

To compile the C code of Git for Windows, MSys was used, too: it sports two versions of the GNU C Compiler: one that links implicitly to the POSIX emulation layer, and another one that targets the plain Win32 API (with a few convenience functions thrown in). Git for Windows' executables are built using the latter, and therefore they are really just Win32 programs. To discern executables requiring the POSIX emulation layer from the ones that do not, the latter are called MinGW (Minimal GNU for Windows) when the former are called MSys executables.

This reliance on MSys incurred challenges, too, though: some of our changes to the MSys runtime -- necessary to support Git for Windows better -- were not accepted upstream, so we had to maintain our own fork. Also, the MSys runtime was not developed further to support e.g. UTF-8 or 64-bit, and apart from lacking a package management system until much later (when mingw-get was introduced), many packages provided by the MSys/MinGW project lag behind the respective source code versions, in particular Bash and OpenSSL. For a while, the Git for Windows project tried to remedy the situation by trying to build newer versions of those packages, but the situation quickly became untenable, especially with problems like the Heartbleed bug requiring swift action that has nothing to do with developing Git for Windows further.

Happily, in the meantime the MSys2 project (https://msys2.github.io/)
emerged, and was chosen to be the base of the Git for Windows 2.x. Just like MSys, MSys2 is a stripped down version of Cygwin, but it is actively kept up-to-date with Cygwin's source code. Thereby, it already supports Unicode internally, and it also offers the 64-bit support that we yearned for since the beginning of the Git for Windows project.

MSys2 also ported the Pacman package management system from Arch Linux and uses it heavily. This brings the same convenience to which Linux users are used to from yum or apt-get, and to which MacOSX users are used to from Homebrew or MacPorts, or BSD users from the Ports system, to MSys2: a simple pacman -Syu will update all installed packages to the newest versions currently available.

MSys2 is also *very* active, typically providing package updates multiple times per week.

It still required a two-month effort to bring everything to a state where Git's test suite passes, many more months until the first official Git for Windows 2.x was released, and a couple of patches still await their submission to the respective upstream projects. Yet without MSys2, the modernization of Git for Windows would simply not have happened.

This commit lays the ground work to supporting MSys2-based Git builds.

Assisted-by Waldek Maleska <[email protected]>
---
 config.mak.uname | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
config.mak.uname +19 -2
index f34dcaa..b0592c1 100644
@@ -518,7 +518,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_INET_NTOP = YesPlease
 	NO_POSIX_GOODIES = UnfortunatelyYes
 	DEFAULT_HELP_FORMAT = html
-	COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
+	COMPAT_CFLAGS += -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
 	COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
 	COMPAT_OBJS += compat/mingw.o compat/winansi.o \
 		compat/win32/pthread.o compat/win32/syslog.o \
@@ -541,8 +541,25 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
 	INTERNAL_QSORT = YesPlease
 	HAVE_LIBCHARSET_H = YesPlease
 	NO_GETTEXT = YesPlease
+	COMPAT_CLFAGS += -D__USE_MINGW_ACCESS
 else
-	NO_CURL = YesPlease
+	ifeq ($(shell expr "$(uname_R)" : '2\.'),2)
+		# MSys2
+		CC = gcc
+		prefix = /mingw32
+		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0
+		INSTALL = /bin/install
+		NO_R_TO_GCC_LINKER = YesPlease
+		INTERNAL_QSORT = YesPlease
+		HAVE_LIBCHARSET_H = YesPlease
+		NO_GETTEXT = YesPlease
+		USE_LIBPCRE= YesPlease
+		NO_CURL =
+		USE_NED_ALLOCATOR = YesPlease
+	else
+		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO
+		NO_CURL = YesPlease
+	endif
 endif
 endif
 ifeq ($(uname_S),QNX)
Signature · 1 line
2.6.3.windows.1.300.g1c25e49
#2 lore raw

Re: [PATCH 1/5] config.mak.uname: support MSys2

Quoted 4 lines · Johannes Schindelin

For a long time, Git for Windows lagged behind Git's 2.x releases because the Git for Windows developers wanted to let that big jump coincide with a well-needed jump away from MSys to MSys2.
[...]

I can't say much about the patch itself, but I think this commit is worth it for the message alone. Now I finally know the subtle differences between all of those systems!

-Peff

#3 lore raw

Re: [PATCH 1/5] config.mak.uname: support MSys2

Quoted 9 lines · Jeff King
Quoted 6 lines · Johannes Schindelin

For a long time, Git for Windows lagged behind Git's 2.x releases because
the Git for Windows developers wanted to let that big jump coincide with
a well-needed jump away from MSys to MSys2.
[...]

Quoted 3 lines

I can't say much about the patch itself, but I think this commit is worth it for the message alone. Now I finally know the subtle differences between all of those systems!

Agreed. It might even make sense to have that explanation somewhere in Documentation/technical/

Signature · 2 lines
Dennis Kaarsemaker
www.kaarsemaker.net
#4 lore raw

Re: [PATCH 1/5] config.mak.uname: support MSys2

In reply to Jeff King

Hi Peff,

Quoted 10 lines · Jeff King
Quoted 4 lines · Johannes Schindelin

For a long time, Git for Windows lagged behind Git's 2.x releases because the Git for Windows developers wanted to let that big jump coincide with a well-needed jump away from MSys to MSys2.
[...]

I can't say much about the patch itself, but I think this commit is worth it for the message alone. Now I finally know the subtle differences between all of those systems!

Thanks! I spent some quality time on reworking this particular commit message this morning, so I am extra glad about your positive feedback!

Ciao,
Dscho

#5 lore raw

PATCH 2/5 config.mak.uname: supporting 64-bit MSys2

In reply to Johannes Schindelin

This just makes things compile, the test suite needs extra tender loving care in addition to this change. We will address these issues in later commits.

While at it, also allow building MSys2 Git (i.e. a Git that uses MSys2's POSIX emulation layer).

---
 config.mak.uname | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
config.mak.uname +11 -3
index b0592c1..4b2e1b8 100644
@@ -518,13 +518,12 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_INET_NTOP = YesPlease
 	NO_POSIX_GOODIES = UnfortunatelyYes
 	DEFAULT_HELP_FORMAT = html
-	COMPAT_CFLAGS += -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
+	COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32
 	COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
 	COMPAT_OBJS += compat/mingw.o compat/winansi.o \
 		compat/win32/pthread.o compat/win32/syslog.o \
 		compat/win32/dirent.o
 	BASIC_CFLAGS += -DPROTECT_NTFS_DEFAULT=1
-	BASIC_LDFLAGS += -Wl,--large-address-aware
 	EXTLIBS += -lws2_32
 	GITLIBS += git.res
 	PTHREAD_LIBS =
@@ -545,8 +544,17 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
 else
 	ifeq ($(shell expr "$(uname_R)" : '2\.'),2)
 		# MSys2
+		prefix = /usr/
+		ifeq (MINGW32,$(MSYSTEM))
+			prefix = /mingw32
+		endif
+		ifeq (MINGW64,$(MSYSTEM))
+			prefix = /mingw64
+		else
+			COMPAT_CFLAGS += -D_USE_32BIT_TIME_T
+			BASIC_LDFLAGS += -Wl,--large-address-aware
+		endif
 		CC = gcc
-		prefix = /mingw32
 		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0
 		INSTALL = /bin/install
 		NO_R_TO_GCC_LINKER = YesPlease
Signature · 1 line
2.6.3.windows.1.300.g1c25e49
#6 lore raw

Re: [PATCH 2/5] config.mak.uname: supporting 64-bit MSys2

Quoted 21 lines

This just makes things compile, the test suite needs extra tender loving care in addition to this change. We will address these issues in later commits.

While at it, also allow building MSys2 Git (i.e. a Git that uses MSys2's POSIX emulation layer).

---
 config.mak.uname | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
config.mak.uname +0 -1
index b0592c1..4b2e1b8 100644
@@ -518,13 +518,12 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_INET_NTOP = YesPlease
 	NO_POSIX_GOODIES = UnfortunatelyYes
 	DEFAULT_HELP_FORMAT = html
-	COMPAT_CFLAGS += -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32

config.mak.uname ifneq (,$(findstring MINGW,$(uname_S))) · line 529

Quoted 10 lines
config.mak.uname +1 -1
+	COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32
 	COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
 	COMPAT_OBJS += compat/mingw.o compat/winansi.o \
 		compat/win32/pthread.o compat/win32/syslog.o \
 		compat/win32/dirent.o
 	BASIC_CFLAGS += -DPROTECT_NTFS_DEFAULT=1
-	BASIC_LDFLAGS += -Wl,--large-address-aware
 	EXTLIBS += -lws2_32
 	GITLIBS += git.res
 	PTHREAD_LIBS =

(On Windows I only used git, and only briefly, and it was back in the v1.9.x era, so please pardon my ignorance...)

I'm puzzled by the if statements in the hunk below:

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 549

Quoted 7 lines
config.mak.uname +3 -0
@@ -545,8 +544,17 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
 else
 	ifeq ($(shell expr "$(uname_R)" : '2\.'),2)
 		# MSys2
+		prefix = /usr/
+		ifeq (MINGW32,$(MSYSTEM))
+			prefix = /mingw32

Here prefix is set for 32 bit MSys2. OK.

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 552

Quoted 3 lines
config.mak.uname +3 -0
+		endif
+		ifeq (MINGW64,$(MSYSTEM))
+			prefix = /mingw64

Here prefix is set for 64 bit MSys2. Still OK.

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 555

Quoted 3 lines
config.mak.uname +3 -0
+		else
+			COMPAT_CFLAGS += -D_USE_32BIT_TIME_T
+			BASIC_LDFLAGS += -Wl,--large-address-aware

But then these flags are set for any MSys2 that is not 64 bit, which also includes MINGW32, which we've already dealt with above explicitly. Hmm.

Is this intentional? It would be easier to follow if the settings specific to MINGW32 weren't scattered in two discontinuous blocks. What values can MSYSTEM have here besides MINGW32 and MINGW64?

Thanks,
Gábor

Quoted 8 lines
config.mak.uname +1 -1
+		endif
 		CC = gcc
-		prefix = /mingw32
 		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0
 		INSTALL = /bin/install
 		NO_R_TO_GCC_LINKER = YesPlease
Signature · 1 line
2.6.3.windows.1.300.g1c25e49
#7 lore raw

Re: [PATCH 2/5] config.mak.uname: supporting 64-bit MSys2

Hi Gábor,

Quoted 3 lines · SZEDER Gábor
Quoted 3 lines

This just makes things compile, the test suite needs extra tender loving care in addition to this change. We will address these issues in later commits.

Please note this statement:

Quoted 9 lines

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 544

Quoted 9 lines

While at it, also allow building MSys2 Git (i.e. a Git that uses MSys2's POSIX emulation layer).
[...]

config.mak.uname +0 -0
index b0592c1..4b2e1b8 100644
@@ -545,8 +544,17 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
 else

... and that this here condition triggers only for MSYS2 [*1*], but in all modes, i.e. 32-bit/64-bit MSYS2/MINGW...

Quoted 7 lines

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 549

Quoted 5 lines
config.mak.uname +3 -0
 	ifeq ($(shell expr "$(uname_R)" : '2\.'),2)
 		# MSys2
+		prefix = /usr/
+		ifeq (MINGW32,$(MSYSTEM))
+			prefix = /mingw32

Here prefix is set for 32 bit MSys2. OK.

Actually, it is not 32-bit MSYS2 but 32-bit MINGW. Please remember that MSYS2 programs use the MSYS2 runtime (in essence a portable version of Cygwin's runtime), i.e. a POSIX emulation layer, while MINGW programs do not.

As a consequence, MINGW programs are 1) faster, and 2) cannot use POSIX functionality such as fork().

So yes, this triggers when we are using the 32-bit MINGW environment, which is just a mode in which you can start MSYS2 (or for that matter, the Git for Windows SDK), simply by setting the environment variable MSYSTEM accordingly.

The modes are MINGW32, MINGW64 and MSYS. You cannot switch between 32-bit MSYS2 and 64-bit MSYS2 that way because you can run either with 32-bit MSYS2 runtime & programs or 64-bit MSYS2 runtime & programs. You can easily mix & match 32-bit and 64-bit MINGW programs, though.

Quoted 5 lines

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 552

Quoted 3 lines
config.mak.uname +3 -0
+		endif
+		ifeq (MINGW64,$(MSYSTEM))
+			prefix = /mingw64

Here prefix is set for 64 bit MSys2. Still OK.

Well, 64-bit *MINGW*, not 64-bit *MSYS2*.

Quoted 7 lines

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 555

Quoted 3 lines
config.mak.uname +3 -0
+		else
+			COMPAT_CFLAGS += -D_USE_32BIT_TIME_T
+			BASIC_LDFLAGS += -Wl,--large-address-aware

But then these flags are set for any MSys2 that is not 64 bit, which also includes MINGW32, which we've already dealt with above explicitly. Hmm.

Remember the second paragraph of the commit message here, please: we want MSYS2 Git to be compiled, too. As opposed to MINGW Git. It is slower, yes, but by being able to use POSIX functionality, it can side-step any bugs in our Windows-specific code, if any.

And even 64-bit MSYS2's tools can handle those C and LD flags, last time I checked, it is just 64-bit MINGW's tools that cannot.

Ciao,
Johannes

Footnote *1*: While "MSYS2" suggests that it is a four-letter acronym rather than a two-letter one (it really only stands for "*M*inimal *Sys*tem" version 2), at least one of the MSYS2 developers is adamant about using all-caps. It's a fight I don't need, so I don't fight it.

#8 lore raw

PATCH 3/5 nedmalloc: allow compiling with MSys2's compiler

In reply to Johannes Schindelin

With MSys2's GCC, ReadWriteBarrier is already defined, and FORCEINLINE unfortunately gets defined incorrectly.

Let's work around both problems, using the MSys2-specific __MINGW64_VERSION_MAJOR constant to guard them.

---
 compat/nedmalloc/malloc.c.h | 6 ++++++
 1 file changed, 6 insertions(+)
compat/nedmalloc/malloc.c.h +6 -0
index f216a2a..18634e3 100644
@@ -720,6 +720,9 @@ struct mallinfo {
   inlining are defined as macros, so these aren't used for them.
 */
 
+#ifdef __MINGW64_VERSION_MAJOR
+#undef FORCEINLINE
+#endif
 #ifndef FORCEINLINE
   #if defined(__GNUC__)
 #define FORCEINLINE __inline __attribute__ ((always_inline))
@@ -1382,6 +1385,9 @@ LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value);
 
   /*** Atomic operations ***/
   #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
+    #ifdef __MINGW64_VERSION_MAJOR
+      #undef _ReadWriteBarrier
+    #endif
     #define _ReadWriteBarrier() __sync_synchronize()
   #else
     static __inline__ __attribute__((always_inline)) long __sync_lock_test_and_set(volatile long * const Target, const long Value)
Signature · 1 line
2.6.3.windows.1.300.g1c25e49
#9 lore raw

Re: [PATCH 3/5] nedmalloc: allow compiling with MSys2's compiler

Quoted 8 lines · Johannes Schindelin

With MSys2's GCC, ReadWriteBarrier is already defined, and FORCEINLINE unfortunately gets defined incorrectly.

Let's work around both problems, using the MSys2-specific __MINGW64_VERSION_MAJOR constant to guard them.

---

Thanks. I actually do not think #undef _ReadWriteBarrier needs to be inside any #ifdef, though. It isn't like it is an error to #undef that you did not #define before.

Quoted 27 lines
 compat/nedmalloc/malloc.c.h | 6 ++++++
 1 file changed, 6 insertions(+)
compat/nedmalloc/malloc.c.h +6 -0
index f216a2a..18634e3 100644
@@ -720,6 +720,9 @@ struct mallinfo {
   inlining are defined as macros, so these aren't used for them.
 */
 
+#ifdef __MINGW64_VERSION_MAJOR
+#undef FORCEINLINE
+#endif
 #ifndef FORCEINLINE
   #if defined(__GNUC__)
 #define FORCEINLINE __inline __attribute__ ((always_inline))
@@ -1382,6 +1385,9 @@ LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value);
 
   /*** Atomic operations ***/
   #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
+    #ifdef __MINGW64_VERSION_MAJOR
+      #undef _ReadWriteBarrier
+    #endif
     #define _ReadWriteBarrier() __sync_synchronize()
   #else
     static __inline__ __attribute__((always_inline)) long __sync_lock_test_and_set(volatile long * const Target, const long Value)
#10 lore raw

PATCH 4/5 compat/mingw: support MSys2-based MinGW build

In reply to Johannes Schindelin

The excellent MSys2 project brings a substantially updated MinGW environment including newer GCC versions and new headers. To support compiling Git, let's special-case the new MinGW (tell-tale: the _MINGW64_VERSION_MAJOR constant is defined).

Note: this commit only addresses compile failures, not compile warnings (that task is left for a future patch).

---
 compat/mingw.h | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
compat/mingw.h +23 -1
index b3e5044..a2da783 100644
@@ -1,14 +1,26 @@
+#ifdef __MINGW64_VERSION_MAJOR
+#include <stdint.h>
+#include <wchar.h>
+typedef _sigset_t sigset_t;
+#endif
 #include <winsock2.h>
 #include <ws2tcpip.h>
 
+/* MinGW-w64 reports to have flockfile, but it does not actually have it. */
+#ifdef __MINGW64_VERSION_MAJOR
+#undef _POSIX_THREAD_SAFE_FUNCTIONS
+#endif
+
 /*
  * things that are not available in header files
  */
 
-typedef int pid_t;
 typedef int uid_t;
 typedef int socklen_t;
+#ifndef __MINGW64_VERSION_MAJOR
+typedef int pid_t;
 #define hstrerror strerror
+#endif
 
 #define S_IFLNK    0120000 /* Symbolic link */
 #define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
@@ -100,8 +112,10 @@ static inline int symlink(const char *oldpath, const char *newpath)
 { errno = ENOSYS; return -1; }
 static inline int fchmod(int fildes, mode_t mode)
 { errno = ENOSYS; return -1; }
+#ifndef __MINGW64_VERSION_MAJOR
 static inline pid_t fork(void)
 { errno = ENOSYS; return -1; }
+#endif
 static inline unsigned int alarm(unsigned int seconds)
 { return 0; }
 static inline int fsync(int fd)
@@ -176,8 +190,10 @@ int pipe(int filedes[2]);
 unsigned int sleep (unsigned int seconds);
 int mkstemp(char *template);
 int gettimeofday(struct timeval *tv, void *tz);
+#ifndef __MINGW64_VERSION_MAJOR
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
 struct tm *localtime_r(const time_t *timep, struct tm *result);
+#endif
 int getpagesize(void);	/* defined in MinGW's libgcc.a */
 struct passwd *getpwuid(uid_t uid);
 int setitimer(int type, struct itimerval *in, struct itimerval *out);
@@ -301,8 +317,10 @@ static inline int getrlimit(int resource, struct rlimit *rlp)
 /*
  * Use mingw specific stat()/lstat()/fstat() implementations on Windows.
  */
+#ifndef __MINGW64_VERSION_MAJOR
 #define off_t off64_t
 #define lseek _lseeki64
+#endif
 
 /* use struct stat with 64 bit st_size */
 #ifdef stat
@@ -383,8 +401,12 @@ static inline char *mingw_find_last_dir_sep(const char *path)
 int mingw_offset_1st_component(const char *path);
 #define offset_1st_component mingw_offset_1st_component
 #define PATH_SEP ';'
+#ifndef __MINGW64_VERSION_MAJOR
 #define PRIuMAX "I64u"
 #define PRId64 "I64d"
+#else
+#include <inttypes.h>
+#endif
 
 void mingw_open_html(const char *path);
 #define open_html mingw_open_html
Signature · 1 line
2.6.3.windows.1.300.g1c25e49
#11 lore raw

PATCH 5/5 compat/winansi: support compiling with MSys2

In reply to Johannes Schindelin

MSys2 already defines the _CONSOLE_FONT_INFOEX structure.

---
 compat/winansi.c | 2 ++
 1 file changed, 2 insertions(+)
compat/winansi.c +2 -0
index ceff55b..3aa5535 100644
@@ -23,6 +23,7 @@ static HANDLE hthread, hread, hwrite;
 static HANDLE hconsole1, hconsole2;
 
 #ifdef __MINGW32__
+#if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 5
 typedef struct _CONSOLE_FONT_INFOEX {
 	ULONG cbSize;
 	DWORD nFont;
@@ -32,6 +33,7 @@ typedef struct _CONSOLE_FONT_INFOEX {
 	WCHAR FaceName[LF_FACESIZE];
 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
 #endif
+#endif
 
 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
 		PCONSOLE_FONT_INFOEX);
Signature · 1 line
2.6.3.windows.1.300.g1c25e49
#12 lore raw
In reply to Johannes Schindelin

It would really be nice to see the endgame of what this series starts sometime not in too distant a future. Many users in Windows land would appreciate this effort, I am sure.

Thanks, will queue.

#14 lore raw

v2 cover Fix compile errors with MSys2

In reply to Johannes Schindelin

Git for Windows 2.x is based on MSys2, and its first official version was released already almost half a year ago, yet it is still impossible even to compile Git from the official, platform-independent Git source code.

This patch series merely fixes the compile errors, and will be followed by other patch series that fix the compile warnings and test failures. The idea is to move all Windows-specific patches of Git for Windows' fork to upstream Git proper.

Relative to v1, I only removed the guards about the "#undef ReadWriteBarrier".

Johannes Schindelin (5):
config.mak.uname: support MSys2 config.mak.uname: supporting 64-bit MSys2
nedmalloc: allow compiling with MSys2's compiler compat/mingw: support MSys2-based MinGW build compat/winansi: support compiling with MSys2

 compat/mingw.h              | 24 +++++++++++++++++++++++-
 compat/nedmalloc/malloc.c.h |  4 ++++
 compat/winansi.c            |  2 ++
 config.mak.uname            | 31 ++++++++++++++++++++++++++++---
 4 files changed, 57 insertions(+), 4 deletions(-)

Interdiff vs v1:

diff --git a/compat/nedmalloc/malloc.c.h b/compat/nedmalloc/malloc.c.h
index 18634e3..bf70037 100644
--- a/compat/nedmalloc/malloc.c.h
+++ b/compat/nedmalloc/malloc.c.h
@@ -1385,9 +1385,7 @@ LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value);

    /*** Atomic operations ***/
    #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
 -    #ifdef __MINGW64_VERSION_MAJOR
 -      #undef _ReadWriteBarrier
 -    #endif
 +    #undef _ReadWriteBarrier
      #define _ReadWriteBarrier() __sync_synchronize()
    #else
      static __inline__ __attribute__((always_inline)) long __sync_lock_test_and_set(volatile long * const Target, const long Value)
Signature · 1 line
2.7.0.windows.1.7.g55a05c8
#15 lore raw

PATCH v2 1/5 config.mak.uname: support MSys2

For a long time, Git for Windows lagged behind Git's 2.x releases because the Git for Windows developers wanted to let that big jump coincide with a well-needed jump away from MSys to MSys2.

To understand why this is such a big issue, it needs to be noted that many parts of Git are not written in portable C, but instead Git relies on a POSIX shell and Perl to be available.

To support the scripts, Git for Windows has to ship a minimal POSIX emulation layer with Bash and Perl thrown in, and when the Git for Windows effort started in August 2007, this developer settled on using MSys, a stripped down version of Cygwin. Consequently, the original name of the project was "msysGit" (which, sadly, caused a *lot* of confusion because few Windows users know about MSys, and even less care).

To compile the C code of Git for Windows, MSys was used, too: it sports two versions of the GNU C Compiler: one that links implicitly to the POSIX emulation layer, and another one that targets the plain Win32 API (with a few convenience functions thrown in). Git for Windows' executables are built using the latter, and therefore they are really just Win32 programs. To discern executables requiring the POSIX emulation layer from the ones that do not, the latter are called MinGW (Minimal GNU for Windows) when the former are called MSys executables.

This reliance on MSys incurred challenges, too, though: some of our changes to the MSys runtime -- necessary to support Git for Windows better -- were not accepted upstream, so we had to maintain our own fork. Also, the MSys runtime was not developed further to support e.g. UTF-8 or 64-bit, and apart from lacking a package management system until much later (when mingw-get was introduced), many packages provided by the MSys/MinGW project lag behind the respective source code versions, in particular Bash and OpenSSL. For a while, the Git for Windows project tried to remedy the situation by trying to build newer versions of those packages, but the situation quickly became untenable, especially with problems like the Heartbleed bug requiring swift action that has nothing to do with developing Git for Windows further.

Happily, in the meantime the MSys2 project (https://msys2.github.io/)
emerged, and was chosen to be the base of the Git for Windows 2.x. Just like MSys, MSys2 is a stripped down version of Cygwin, but it is actively kept up-to-date with Cygwin's source code. Thereby, it already supports Unicode internally, and it also offers the 64-bit support that we yearned for since the beginning of the Git for Windows project.

MSys2 also ported the Pacman package management system from Arch Linux and uses it heavily. This brings the same convenience to which Linux users are used to from yum or apt-get, and to which MacOSX users are used to from Homebrew or MacPorts, or BSD users from the Ports system, to MSys2: a simple pacman -Syu will update all installed packages to the newest versions currently available.

MSys2 is also *very* active, typically providing package updates multiple times per week.

It still required a two-month effort to bring everything to a state where Git's test suite passes, many more months until the first official Git for Windows 2.x was released, and a couple of patches still await their submission to the respective upstream projects. Yet without MSys2, the modernization of Git for Windows would simply not have happened.

This commit lays the ground work to supporting MSys2-based Git builds.

Assisted-by Waldek Maleska <[email protected]>
---
 config.mak.uname | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
config.mak.uname +19 -2
index f34dcaa..b0592c1 100644
@@ -518,7 +518,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_INET_NTOP = YesPlease
 	NO_POSIX_GOODIES = UnfortunatelyYes
 	DEFAULT_HELP_FORMAT = html
-	COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
+	COMPAT_CFLAGS += -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
 	COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
 	COMPAT_OBJS += compat/mingw.o compat/winansi.o \
 		compat/win32/pthread.o compat/win32/syslog.o \
@@ -541,8 +541,25 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
 	INTERNAL_QSORT = YesPlease
 	HAVE_LIBCHARSET_H = YesPlease
 	NO_GETTEXT = YesPlease
+	COMPAT_CLFAGS += -D__USE_MINGW_ACCESS
 else
-	NO_CURL = YesPlease
+	ifeq ($(shell expr "$(uname_R)" : '2\.'),2)
+		# MSys2
+		CC = gcc
+		prefix = /mingw32
+		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0
+		INSTALL = /bin/install
+		NO_R_TO_GCC_LINKER = YesPlease
+		INTERNAL_QSORT = YesPlease
+		HAVE_LIBCHARSET_H = YesPlease
+		NO_GETTEXT = YesPlease
+		USE_LIBPCRE= YesPlease
+		NO_CURL =
+		USE_NED_ALLOCATOR = YesPlease
+	else
+		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO
+		NO_CURL = YesPlease
+	endif
 endif
 endif
 ifeq ($(uname_S),QNX)
Signature · 1 line
2.7.0.windows.1.7.g55a05c8
#16 lore raw

Re: [PATCH v2 1/5] config.mak.uname: support MSys2

Am 14.01.2016 um 17:51 schrieb Johannes Schindelin:

Quoted 14 lines
config.mak.uname +1 -1
index f34dcaa..b0592c1 100644
@@ -518,7 +518,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
  	NO_INET_NTOP = YesPlease
  	NO_POSIX_GOODIES = UnfortunatelyYes
  	DEFAULT_HELP_FORMAT = html
-	COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
+	COMPAT_CFLAGS += -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
  	COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
  	COMPAT_OBJS += compat/mingw.o compat/winansi.o \
  		compat/win32/pthread.o compat/win32/syslog.o \

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 549

Quoted 10 lines
config.mak.uname +5 -1
@@ -541,8 +541,25 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
  	INTERNAL_QSORT = YesPlease
  	HAVE_LIBCHARSET_H = YesPlease
  	NO_GETTEXT = YesPlease
+	COMPAT_CLFAGS += -D__USE_MINGW_ACCESS
  else
-	NO_CURL = YesPlease
+	ifeq ($(shell expr "$(uname_R)" : '2\.'),2)
+		# MSys2
+		CC = gcc
+		prefix = /mingw32

Hmm. Setting the prefix like this is certainly ideal for Git for Windows (the installer), but it is very uncommon to set the prefix in config.mak.uname.

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 558

Quoted 9 lines
config.mak.uname +9 -0
+		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0
+		INSTALL = /bin/install
+		NO_R_TO_GCC_LINKER = YesPlease
+		INTERNAL_QSORT = YesPlease
+		HAVE_LIBCHARSET_H = YesPlease
+		NO_GETTEXT = YesPlease
+		USE_LIBPCRE= YesPlease
+		NO_CURL =
+		USE_NED_ALLOCATOR = YesPlease

USE_NED_ALLOCATOR is already set in the common section (line 510).

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 560

Quoted 2 lines
config.mak.uname +2 -0
+	else
+		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO

Did you mean to set -D__USE_MINGW_ACCESS here? But, as I noticed, it doesn't make a whole lot of a difference; my build (which picks this branch, if I'm not mistaken) still passes the test suite.

config.mak.uname ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) · line 565

Quoted 6 lines
config.mak.uname +2 -0
+		NO_CURL = YesPlease
+	endif
  endif
  endif
  ifeq ($(uname_S),QNX)

-- Hannes

#17 lore raw

PATCH v2 2/5 config.mak.uname: supporting 64-bit MSys2

In reply to Johannes Schindelin

This just makes things compile, the test suite needs extra tender loving care in addition to this change. We will address these issues in later commits.

While at it, also allow building MSys2 Git (i.e. a Git that uses MSys2's POSIX emulation layer).

---
 config.mak.uname | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
config.mak.uname +11 -3
index b0592c1..4b2e1b8 100644
@@ -518,13 +518,12 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_INET_NTOP = YesPlease
 	NO_POSIX_GOODIES = UnfortunatelyYes
 	DEFAULT_HELP_FORMAT = html
-	COMPAT_CFLAGS += -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
+	COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32
 	COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
 	COMPAT_OBJS += compat/mingw.o compat/winansi.o \
 		compat/win32/pthread.o compat/win32/syslog.o \
 		compat/win32/dirent.o
 	BASIC_CFLAGS += -DPROTECT_NTFS_DEFAULT=1
-	BASIC_LDFLAGS += -Wl,--large-address-aware
 	EXTLIBS += -lws2_32
 	GITLIBS += git.res
 	PTHREAD_LIBS =
@@ -545,8 +544,17 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
 else
 	ifeq ($(shell expr "$(uname_R)" : '2\.'),2)
 		# MSys2
+		prefix = /usr/
+		ifeq (MINGW32,$(MSYSTEM))
+			prefix = /mingw32
+		endif
+		ifeq (MINGW64,$(MSYSTEM))
+			prefix = /mingw64
+		else
+			COMPAT_CFLAGS += -D_USE_32BIT_TIME_T
+			BASIC_LDFLAGS += -Wl,--large-address-aware
+		endif
 		CC = gcc
-		prefix = /mingw32
 		COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0
 		INSTALL = /bin/install
 		NO_R_TO_GCC_LINKER = YesPlease
Signature · 1 line
2.7.0.windows.1.7.g55a05c8
#18 lore raw
In reply to Johannes Schindelin

Am 14.01.2016 um 17:51 schrieb Johannes Schindelin:

Quoted 34 lines

Git for Windows 2.x is based on MSys2, and its first official version was released already almost half a year ago, yet it is still impossible even to compile Git from the official, platform-independent Git source code.

This patch series merely fixes the compile errors, and will be followed by other patch series that fix the compile warnings and test failures. The idea is to move all Windows-specific patches of Git for Windows' fork to upstream Git proper.

Relative to v1, I only removed the guards about the "#undef ReadWriteBarrier".

Johannes Schindelin (5):
config.mak.uname: support MSys2 config.mak.uname: supporting 64-bit MSys2
nedmalloc: allow compiling with MSys2's compiler compat/mingw: support MSys2-based MinGW build compat/winansi: support compiling with MSys2

  compat/mingw.h              | 24 +++++++++++++++++++++++-
  compat/nedmalloc/malloc.c.h |  4 ++++
  compat/winansi.c            |  2 ++
  config.mak.uname            | 31 ++++++++++++++++++++++++++++---
  4 files changed, 57 insertions(+), 4 deletions(-)

Interdiff vs v1:

  diff --git a/compat/nedmalloc/malloc.c.h b/compat/nedmalloc/malloc.c.h
  index 18634e3..bf70037 100644
  --- a/compat/nedmalloc/malloc.c.h
  +++ b/compat/nedmalloc/malloc.c.h
  @@ -1385,9 +1385,7 @@ LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value);
Quoted 10 lines
     /*** Atomic operations ***/
     #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
  -    #ifdef __MINGW64_VERSION_MAJOR
  -      #undef _ReadWriteBarrier
  -    #endif
  +    #undef _ReadWriteBarrier
       #define _ReadWriteBarrier() __sync_synchronize()
     #else
       static __inline__ __attribute__((always_inline)) long __sync_lock_test_and_set(volatile long * const Target, const long Value)

It is very considerate of you to keep compatibility with the old MSYS build environment, which I'm still using. (The new Git for Windows build environment is already on my disk, but I haven't found time to get familiar with it.) So, it doesn't mean a whole lot, unfortunatley, when I report that I've tested v1, and that it passes the test suite, because I've exercised almost none of the new code.

-- Hannes

#19 lore raw

PATCH v2 3/5 nedmalloc: allow compiling with MSys2's compiler

In reply to Johannes Schindelin

With MSys2's GCC, ReadWriteBarrier is already defined, and FORCEINLINE unfortunately gets defined incorrectly.

Let's work around both problems, using the MSys2-specific __MINGW64_VERSION_MAJOR constant to guard the FORCEINLINE definition so as not to affect other platforms.

---
 compat/nedmalloc/malloc.c.h | 4 ++++
 1 file changed, 4 insertions(+)
compat/nedmalloc/malloc.c.h +4 -0
index f216a2a..bf70037 100644
@@ -720,6 +720,9 @@ struct mallinfo {
   inlining are defined as macros, so these aren't used for them.
 */
 
+#ifdef __MINGW64_VERSION_MAJOR
+#undef FORCEINLINE
+#endif
 #ifndef FORCEINLINE
   #if defined(__GNUC__)
 #define FORCEINLINE __inline __attribute__ ((always_inline))
@@ -1382,6 +1385,7 @@ LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value);
 
   /*** Atomic operations ***/
   #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
+    #undef _ReadWriteBarrier
     #define _ReadWriteBarrier() __sync_synchronize()
   #else
     static __inline__ __attribute__((always_inline)) long __sync_lock_test_and_set(volatile long * const Target, const long Value)
Signature · 1 line
2.7.0.windows.1.7.g55a05c8
#20 lore raw

PATCH v2 4/5 compat/mingw: support MSys2-based MinGW build

In reply to Johannes Schindelin

The excellent MSys2 project brings a substantially updated MinGW environment including newer GCC versions and new headers. To support compiling Git, let's special-case the new MinGW (tell-tale: the _MINGW64_VERSION_MAJOR constant is defined).

Note: this commit only addresses compile failures, not compile warnings (that task is left for a future patch).

---
 compat/mingw.h | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
compat/mingw.h +23 -1
index b3e5044..a2da783 100644
@@ -1,14 +1,26 @@
+#ifdef __MINGW64_VERSION_MAJOR
+#include <stdint.h>
+#include <wchar.h>
+typedef _sigset_t sigset_t;
+#endif
 #include <winsock2.h>
 #include <ws2tcpip.h>
 
+/* MinGW-w64 reports to have flockfile, but it does not actually have it. */
+#ifdef __MINGW64_VERSION_MAJOR
+#undef _POSIX_THREAD_SAFE_FUNCTIONS
+#endif
+
 /*
  * things that are not available in header files
  */
 
-typedef int pid_t;
 typedef int uid_t;
 typedef int socklen_t;
+#ifndef __MINGW64_VERSION_MAJOR
+typedef int pid_t;
 #define hstrerror strerror
+#endif
 
 #define S_IFLNK    0120000 /* Symbolic link */
 #define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
@@ -100,8 +112,10 @@ static inline int symlink(const char *oldpath, const char *newpath)
 { errno = ENOSYS; return -1; }
 static inline int fchmod(int fildes, mode_t mode)
 { errno = ENOSYS; return -1; }
+#ifndef __MINGW64_VERSION_MAJOR
 static inline pid_t fork(void)
 { errno = ENOSYS; return -1; }
+#endif
 static inline unsigned int alarm(unsigned int seconds)
 { return 0; }
 static inline int fsync(int fd)
@@ -176,8 +190,10 @@ int pipe(int filedes[2]);
 unsigned int sleep (unsigned int seconds);
 int mkstemp(char *template);
 int gettimeofday(struct timeval *tv, void *tz);
+#ifndef __MINGW64_VERSION_MAJOR
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
 struct tm *localtime_r(const time_t *timep, struct tm *result);
+#endif
 int getpagesize(void);	/* defined in MinGW's libgcc.a */
 struct passwd *getpwuid(uid_t uid);
 int setitimer(int type, struct itimerval *in, struct itimerval *out);
@@ -301,8 +317,10 @@ static inline int getrlimit(int resource, struct rlimit *rlp)
 /*
  * Use mingw specific stat()/lstat()/fstat() implementations on Windows.
  */
+#ifndef __MINGW64_VERSION_MAJOR
 #define off_t off64_t
 #define lseek _lseeki64
+#endif
 
 /* use struct stat with 64 bit st_size */
 #ifdef stat
@@ -383,8 +401,12 @@ static inline char *mingw_find_last_dir_sep(const char *path)
 int mingw_offset_1st_component(const char *path);
 #define offset_1st_component mingw_offset_1st_component
 #define PATH_SEP ';'
+#ifndef __MINGW64_VERSION_MAJOR
 #define PRIuMAX "I64u"
 #define PRId64 "I64d"
+#else
+#include <inttypes.h>
+#endif
 
 void mingw_open_html(const char *path);
 #define open_html mingw_open_html
Signature · 1 line
2.7.0.windows.1.7.g55a05c8
#21 lore raw

PATCH v2 5/5 compat/winansi: support compiling with MSys2

In reply to Johannes Schindelin

MSys2 already defines the _CONSOLE_FONT_INFOEX structure.

---
 compat/winansi.c | 2 ++
 1 file changed, 2 insertions(+)
compat/winansi.c +2 -0
index ceff55b..3aa5535 100644
@@ -23,6 +23,7 @@ static HANDLE hthread, hread, hwrite;
 static HANDLE hconsole1, hconsole2;
 
 #ifdef __MINGW32__
+#if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 5
 typedef struct _CONSOLE_FONT_INFOEX {
 	ULONG cbSize;
 	DWORD nFont;
@@ -32,6 +33,7 @@ typedef struct _CONSOLE_FONT_INFOEX {
 	WCHAR FaceName[LF_FACESIZE];
 } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
 #endif
+#endif
 
 typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
 		PCONSOLE_FONT_INFOEX);
Signature · 1 line
2.7.0.windows.1.7.g55a05c8