Skip to content

Commit f4626df

Browse files
author
Johannes Sixt
committed
Add target architecture MinGW.
With this change GIT can be compiled and linked using MinGW. Builtins that only read the repository such as the log family and grep already work. Simple stubs are provided for a number of functions that the Windows C runtime does not offer. They will be completed in later patches. However, a fix for the snprintf/vsnprintf replacement is applied here to avoid buffer overflows. Dmitry Kakurin pointed out that access(..., X_OK) would always fails on Vista and suggested the -D__USE_MINGW_ACCESS workaround. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
1 parent 58eda02 commit f4626df

File tree

5 files changed

+244
-7
lines changed

5 files changed

+244
-7
lines changed

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ LIB_H += builtin.h
335335
LIB_H += cache.h
336336
LIB_H += cache-tree.h
337337
LIB_H += commit.h
338+
LIB_H += compat/mingw.h
338339
LIB_H += csum-file.h
339340
LIB_H += decorate.h
340341
LIB_H += delta.h
@@ -711,6 +712,33 @@ ifeq ($(uname_S),HP-UX)
711712
NO_HSTRERROR = YesPlease
712713
NO_SYS_SELECT_H = YesPlease
713714
endif
715+
ifneq (,$(findstring MINGW,$(uname_S)))
716+
NO_MMAP = YesPlease
717+
NO_PREAD = YesPlease
718+
NO_OPENSSL = YesPlease
719+
NO_CURL = YesPlease
720+
NO_SYMLINK_HEAD = YesPlease
721+
NO_IPV6 = YesPlease
722+
NO_SETENV = YesPlease
723+
NO_UNSETENV = YesPlease
724+
NO_STRCASESTR = YesPlease
725+
NO_STRLCPY = YesPlease
726+
NO_MEMMEM = YesPlease
727+
NEEDS_LIBICONV = YesPlease
728+
OLD_ICONV = YesPlease
729+
NO_C99_FORMAT = YesPlease
730+
NO_STRTOUMAX = YesPlease
731+
NO_MKDTEMP = YesPlease
732+
SNPRINTF_RETURNS_BOGUS = YesPlease
733+
NO_SVN_TESTS = YesPlease
734+
NO_PERL_MAKEMAKER = YesPlease
735+
NO_POSIX_ONLY_PROGRAMS = YesPlease
736+
COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat
737+
COMPAT_CFLAGS += -DSNPRINTF_SIZE_CORR=1
738+
COMPAT_OBJS += compat/mingw.o compat/fnmatch.o compat/regex.o
739+
EXTLIBS += -lws2_32
740+
X = .exe
741+
endif
714742
ifneq (,$(findstring arm,$(uname_M)))
715743
ARM_SHA1 = YesPlease
716744
endif

compat/mingw.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "../git-compat-util.h"
2+
3+
unsigned int _CRT_fmode = _O_BINARY;
4+
5+
unsigned int sleep (unsigned int seconds)
6+
{
7+
Sleep(seconds*1000);
8+
return 0;
9+
}
10+
11+
int mkstemp(char *template)
12+
{
13+
char *filename = mktemp(template);
14+
if (filename == NULL)
15+
return -1;
16+
return open(filename, O_RDWR | O_CREAT, 0600);
17+
}
18+
19+
int gettimeofday(struct timeval *tv, void *tz)
20+
{
21+
return -1;
22+
}
23+
24+
int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
25+
{
26+
return -1;
27+
}
28+
29+
struct tm *gmtime_r(const time_t *timep, struct tm *result)
30+
{
31+
/* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
32+
memcpy(result, gmtime(timep), sizeof(struct tm));
33+
return result;
34+
}
35+
36+
struct tm *localtime_r(const time_t *timep, struct tm *result)
37+
{
38+
/* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
39+
memcpy(result, localtime(timep), sizeof(struct tm));
40+
return result;
41+
}
42+
43+
struct passwd *getpwuid(int uid)
44+
{
45+
static struct passwd p;
46+
return &p;
47+
}
48+
49+
int setitimer(int type, struct itimerval *in, struct itimerval *out)
50+
{
51+
return -1;
52+
}
53+
54+
int sigaction(int sig, struct sigaction *in, struct sigaction *out)
55+
{
56+
return -1;
57+
}

compat/mingw.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <winsock2.h>
2+
3+
/*
4+
* things that are not available in header files
5+
*/
6+
7+
typedef int pid_t;
8+
#define hstrerror strerror
9+
10+
#define S_IFLNK 0120000 /* Symbolic link */
11+
#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
12+
#define S_ISSOCK(x) 0
13+
#define S_IRGRP 0
14+
#define S_IWGRP 0
15+
#define S_IXGRP 0
16+
#define S_ISGID 0
17+
#define S_IROTH 0
18+
#define S_IXOTH 0
19+
20+
#define WIFEXITED(x) ((unsigned)(x) < 259) /* STILL_ACTIVE */
21+
#define WEXITSTATUS(x) ((x) & 0xff)
22+
#define WIFSIGNALED(x) ((unsigned)(x) > 259)
23+
24+
#define SIGKILL 0
25+
#define SIGCHLD 0
26+
#define SIGPIPE 0
27+
#define SIGHUP 0
28+
#define SIGQUIT 0
29+
#define SIGALRM 100
30+
31+
#define F_GETFD 1
32+
#define F_SETFD 2
33+
#define FD_CLOEXEC 0x1
34+
35+
struct passwd {
36+
char *pw_name;
37+
char *pw_gecos;
38+
char *pw_dir;
39+
};
40+
41+
struct pollfd {
42+
int fd; /* file descriptor */
43+
short events; /* requested events */
44+
short revents; /* returned events */
45+
};
46+
#define POLLIN 1
47+
#define POLLHUP 2
48+
49+
typedef void (__cdecl *sig_handler_t)(int);
50+
struct sigaction {
51+
sig_handler_t sa_handler;
52+
unsigned sa_flags;
53+
};
54+
#define sigemptyset(x) (void)0
55+
#define SA_RESTART 0
56+
57+
struct itimerval {
58+
struct timeval it_value, it_interval;
59+
};
60+
#define ITIMER_REAL 0
61+
62+
#define st_blocks st_size/512 /* will be cleaned up later */
63+
#define lstat stat
64+
65+
/*
66+
* trivial stubs
67+
*/
68+
69+
static inline int readlink(const char *path, char *buf, size_t bufsiz)
70+
{ errno = ENOSYS; return -1; }
71+
static inline int symlink(const char *oldpath, const char *newpath)
72+
{ errno = ENOSYS; return -1; }
73+
static inline int link(const char *oldpath, const char *newpath)
74+
{ errno = ENOSYS; return -1; }
75+
static inline int fchmod(int fildes, mode_t mode)
76+
{ errno = ENOSYS; return -1; }
77+
static inline int fork(void)
78+
{ errno = ENOSYS; return -1; }
79+
static inline unsigned int alarm(unsigned int seconds)
80+
{ return 0; }
81+
static inline int fsync(int fd)
82+
{ return 0; }
83+
static inline int getppid(void)
84+
{ return 1; }
85+
static inline void sync(void)
86+
{}
87+
static inline int getuid()
88+
{ return 1; }
89+
static inline struct passwd *getpwnam(const char *name)
90+
{ return NULL; }
91+
static inline int fcntl(int fd, int cmd, long arg)
92+
{
93+
if (cmd == F_GETFD || cmd == F_SETFD)
94+
return 0;
95+
errno = EINVAL;
96+
return -1;
97+
}
98+
99+
/*
100+
* simple adaptors
101+
*/
102+
103+
static inline int mingw_mkdir(const char *path, int mode)
104+
{
105+
return mkdir(path);
106+
}
107+
#define mkdir mingw_mkdir
108+
109+
static inline int waitpid(pid_t pid, unsigned *status, unsigned options)
110+
{
111+
if (options == 0)
112+
return _cwait(status, pid, 0);
113+
errno = EINVAL;
114+
return -1;
115+
}
116+
117+
118+
static inline int pipe(int filedes[2])
119+
{ return _pipe(filedes, 8192, 0); }
120+
121+
/*
122+
* implementations of missing functions
123+
*/
124+
125+
unsigned int sleep (unsigned int seconds);
126+
int mkstemp(char *template);
127+
int gettimeofday(struct timeval *tv, void *tz);
128+
int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
129+
struct tm *gmtime_r(const time_t *timep, struct tm *result);
130+
struct tm *localtime_r(const time_t *timep, struct tm *result);
131+
int getpagesize(void); /* defined in MinGW's libgcc.a */
132+
struct passwd *getpwuid(int uid);
133+
int setitimer(int type, struct itimerval *in, struct itimerval *out);
134+
int sigaction(int sig, struct sigaction *in, struct sigaction *out);

compat/snprintf.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
#include "../git-compat-util.h"
22

3+
/*
4+
* The size parameter specifies the available space, i.e. includes
5+
* the trailing NUL byte; but Windows's vsnprintf expects the
6+
* number of characters to write without the trailing NUL.
7+
*/
8+
#ifndef SNPRINTF_SIZE_CORR
9+
#define SNPRINTF_SIZE_CORR 0
10+
#endif
11+
312
#undef vsnprintf
413
int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
514
{
615
char *s;
7-
int ret;
16+
int ret = -1;
817

9-
ret = vsnprintf(str, maxsize, format, ap);
18+
if (maxsize > 0) {
19+
ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
20+
/* Windows does not NUL-terminate if result fills buffer */
21+
str[maxsize-1] = 0;
22+
}
1023
if (ret != -1)
1124
return ret;
1225

@@ -20,7 +33,7 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
2033
if (! str)
2134
break;
2235
s = str;
23-
ret = vsnprintf(str, maxsize, format, ap);
36+
ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
2437
}
2538
free(s);
2639
return ret;

git-compat-util.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,18 @@
6363
#include <sys/time.h>
6464
#include <time.h>
6565
#include <signal.h>
66-
#include <sys/wait.h>
6766
#include <fnmatch.h>
67+
#include <assert.h>
68+
#include <regex.h>
69+
#include <utime.h>
70+
#ifndef __MINGW32__
71+
#include <sys/wait.h>
6872
#include <sys/poll.h>
6973
#include <sys/socket.h>
7074
#include <sys/ioctl.h>
71-
#include <utime.h>
7275
#ifndef NO_SYS_SELECT_H
7376
#include <sys/select.h>
7477
#endif
75-
#include <assert.h>
76-
#include <regex.h>
7778
#include <netinet/in.h>
7879
#include <netinet/tcp.h>
7980
#include <arpa/inet.h>
@@ -89,6 +90,10 @@
8990
#include <grp.h>
9091
#define _ALL_SOURCE 1
9192
#endif
93+
#else /* __MINGW32__ */
94+
/* pull in Windows compatibility stuff */
95+
#include "compat/mingw.h"
96+
#endif /* __MINGW32__ */
9297

9398
#ifndef NO_ICONV
9499
#include <iconv.h>

0 commit comments

Comments
 (0)