win32: Improve the conditional inclusion of WIN32 API code
PATCH 4/4 win32: Improve the conditional inclusion of WIN32 API code
After experimenting with several win32 compilers, it is clear that the _WIN32 macro should be used to guard the inclusion of the main WIN32 API header files, particularly in the main git-compat-util.h header. In addition, the cygwin build should restrict the use of the WIN32 API to compat/cygwin.c
In order to avoid mistakes with the use of WIN32 and _WIN32 macros, define a new WIN32_API macro, with a single point of definition in git-compat-util.h, to isolate WIN32 specific code.
--- compat/cygwin.c | 1 + compat/snprintf.c | 2 +- compat/win32.h | 3 --- diff-no-index.c | 2 +- git-compat-util.h | 9 +++++---- help.c | 2 +- pager.c | 4 ++-- run-command.c | 8 ++++---- run-command.h | 2 +- setup.c | 2 +- test-chmtime.c | 2 +- thread-utils.c | 2 +- 12 files changed, 19 insertions(+), 20 deletions(-)
compat/cygwin.c +1 -0
@@ -1,5 +1,6 @@#define WIN32_LEAN_AND_MEAN #include "../git-compat-util.h" +#include <windows.h> #include "win32.h" #include "../cache.h" /* to read configuration */
compat/snprintf.c +1 -1
@@ -9,7 +9,7 @@* always have room for a trailing NUL byte. */ #ifndef SNPRINTF_SIZE_CORR -#if defined(WIN32 ) && (!defined(__GNUC__) || __GNUC__ <4 ) +#if defined(WIN32_API ) && (!defined(__GNUC__) || __GNUC__ <4 )#define SNPRINTF_SIZE_CORR 1#else #define SNPRINTF_SIZE_CORR 0
compat/win32.h +0 -3
@@ -2,9 +2,6 @@#define WIN32_H /* common Win32 functions for MinGW and Cygwin */ -#ifndef WIN32 /* Not defined by Cygwin */ -#include <windows.h> -#endif static inline int file_attr_to_st_mode (DWORD attr) {
diff-no-index.c +1 -1
@@ -38,7 +38,7 @@ static int get_mode(const char *path, int *mode)if (!path || !strcmp (path,"/dev/null" )) *mode =0 ; -#ifdef _WIN32 +#ifdef WIN32_API else if (!strcasecmp(path, "nul")) *mode =0 ;#endif
git-compat-util.h +5 -4
@@ -65,10 +65,11 @@#define _NETBSD_SOURCE 1#define _SGI_SOURCE 1 -#ifdef WIN32 /* Both MinGW and MSVC */ -#define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */ -#include <winsock2.h> -#include <windows.h> +#if defined(_WIN32) && !defined(__CYGWIN__)/* Both MinGW and MSVC */ +# define WIN32_API +# define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */ +# include <winsock2.h> +# include <windows.h> #endif #include <unistd.h>
help.c +1 -1
@@ -126,7 +126,7 @@ static int is_executable(const char *name) !S_ISREG(st.st_mode))return 0 ; -#ifdef WIN32 +#ifdef WIN32_API {/* cannot trust the executable bit, peek into the file instead */ char buf[3 ] = {0 };int n;
pager.c +2 -2
@@ -9,7 +9,7 @@static int spawned_pager; -#ifndef WIN32 +#ifndef WIN32_API static void pager_preexec (void ) {/*
@@ -72,7 +72,7 @@ void setup_pager(void)static const char *env[] = {"LESS=FRSX" ,NULL }; pager_process.env = env; } -#ifndef WIN32 +#ifndef WIN32_API pager_process.preexec_cb = pager_preexec;#endif if (start_command (&pager_process))
run-command.c +4 -4
@@ -75,7 +75,7 @@ fail_pipe:trace_argv_printf (cmd->argv,"trace: run_command:" ); -#ifndef WIN32 +#ifndef WIN32_API fflush (NULL ); cmd->pid =fork ();if (!cmd->pid) {
@@ -315,7 +315,7 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, constreturn run_command (&cmd); } -#ifdef WIN32 +#ifdef WIN32_API static unsigned __stdcallrun_thread (void *data) {struct async *async = data;
@@ -331,7 +331,7 @@ int start_async(struct async *async)return error ("cannot create pipe: %s" ,strerror (errno)); async->out = pipe_out[0 ]; -#ifndef WIN32 +#ifndef WIN32_API /* Flush stdio before fork() to avoid cloning buffers */ fflush (NULL );
@@ -360,7 +360,7 @@ int start_async(struct async *async)int finish_async (struct async *async) { -#ifndef WIN32 +#ifndef WIN32_API int ret =wait_or_whine (async->pid,"child process" ,0 );#else DWORD ret =0 ;
run-command.h +1 -1
@@ -70,7 +70,7 @@ struct async {int (*proc)(int fd,void *data);void *data;int out;/* caller reads from here and closes it */ -#ifndef WIN32 +#ifndef WIN32_API pid_t pid;#else HANDLE tid;
setup.c +1 -1
@@ -41,7 +41,7 @@ const char *prefix_path(const char *prefix, int len, const char *path)const char *prefix_filename (const char *pfx,int pfx_len,const char *arg) {static char path[PATH_MAX ]; -#ifndef WIN32 +#ifndef WIN32_API if (!pfx || !*pfx ||is_absolute_path (arg))return arg;memcpy (path, pfx, pfx_len);
test-chmtime.c +1 -1
@@ -87,7 +87,7 @@ int main(int argc, const char *argv[])return -1 ; } -#ifdef WIN32 +#ifdef WIN32_API if (!(sb.st_mode &S_IWUSR ) &&chmod (argv[i], sb.st_mode |S_IWUSR )) { fprintf(stderr, "Could not make user-writable %s: %s",
thread-utils.c +1 -1
@@ -23,7 +23,7 @@ int online_cpus(void)long ncpus;#endif -#ifdef _WIN32 +#ifdef WIN32_API SYSTEM_INFO info;GetSystemInfo (&info);
Signature · 1 line
1.6.5
Quoted 10 lines · Ramsay Jones
After experimenting with several win32 compilers, it is clear that the
_WIN32macro should be used to guard the inclusion of the main WIN32 API header files, particularly in the maingit-compat-util.hheader. In addition, the cygwin build should restrict the use of the WIN32 API tocompat/cygwin.cIn order to avoid mistakes with the use of WIN32 and
_WIN32macros, define a newWIN32_APImacro, with a single point of definition ingit-compat-util.h, to isolate WIN32 specific code.
IMO, the name "WIN32_API" is very misleading. It suggests that it can be used to protect sections of code that use the API. But in fact all places outside (and even some places in) the compatibility layer use "WIN32" to protect *platform* specific code (as opposed to the API specific code).
In this light, the name "WIN32" isn't to the point, either, but it does at least not pretend that the code is about API.
Therefore, I think that a better change would be to treat _WIN32 and WIN32 as synonyms, and to make sure that both are defined if at least one of them is defined.
It may be that I understand something incorrectly; but then I blame the justification that you gave. In this case, it would be helpful to reword the commit message, and perhaps add some results from your experiments.
-- Hannes
Quoted 4 lines · Johannes Sixt
It may be that I understand something incorrectly; but then I blame the justification that you gave. In this case, it would be helpful to reword the commit message, and perhaps add some results from your experiments.
The discussion which lead to this patch, including the experiments, can be found in the email thread starting here:
http://thread.gmane.org/gmane.comp.version-control.git/129403
(along with some other unrelated stuff; but it's not a long read :)
In the above thread, Marius suggested API_WIN32, but I switched it around, since I thought it sounded better! I also thought about GIT_WIN32. Suggestions?
ATB,
Ramsay Jones
Quoted 9 lines · Ramsay Jones
Quoted 4 lines · Johannes Sixt
It may be that I understand something incorrectly; but then I blame the justification that you gave. In this case, it would be helpful to reword the commit message, and perhaps add some results from your experiments.
The discussion which lead to this patch, including the experiments, can be found in the email thread starting here:
Quoted 6 lines
http://thread.gmane.org/gmane.comp.version-control.git/129403(along with some other unrelated stuff; but it's not a long read :)
In the above thread, Marius suggested
API_WIN32, but I switched it around, since I thought it sounded better! I also thought aboutGIT_WIN32. Suggestions?
I suggested to treat WIN32 and _WIN32 as synonyms. The commit message should summarize what you observed in your experiments.
But you can also tell me now why this is not possible. (I recall that your report about the experiments was rather long; I don't have the time to read and understand it again and to draw the correct conclusions.)
-- Hannes