threads / patch / 21522

v2, 4 partsAdd explicit Cygwin check to guard WIN32 header inclusion

Subject: [PATCH v2 4/4] Add explicit Cygwin check to guard WIN32 header inclusion

## tl;dr

5 messages between Nov 7, 2009 and Nov 10, 2009. Diffs are folded; open one to read it.

replies: 4people: 3as markdown or json

Ramsay Jones· Nov 7, 2009, 20:22 UTC · lore

Since commit 435bdf8c ("Make usage of windows.h lean and mean", 16-9-2009), the amount of code potentially including the WIN32 API header files has greatly increased. In particular, the Cygwin build is at greater risk of inadvertently including WIN32 code within preprocessor sections protected by the WIN32 or _WIN32 macros.

The previous commit message, along with comments elsewhere, assert that the WIN32 macro is not defined on Cygwin. Currently, this is true for the cygwin build. However, the cygwin platform can be used to develop WIN32 GUI, WIN32 console, and POSIX applications. Indeed it is possible to create applications which use a mix of the WIN32 API and POSIX code (eg git!).

Unlike native WIN32 compilers, gcc on cygwin does not automatically define the _WIN32 macro. However, as soon as you include the <windows.h> header file, the _WIN32 and WIN32 macros are defined.

In order to reduce the risk of problems in the future, we protect the inclusion of the windows header with an explicit check for __CYGWIN__. Also, we move the other use of the <windows.h> header from compat/win32.h to compat/cygwin.c

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
Changes since v1:
    - removed the definition of the WIN32_API macro.
    - removed all usage of WIN32_API as a replacement for the use
      of the _WIN32 and WIN32 macros in #if(n)def sections.

I wanted to separate these (remaining) changes from the WIN32_API issue, since that is likely to require further discussion (along with some bike-shedding), and I didn't want these to get held up.

 compat/cygwin.c   |    1 +
 compat/win32.h    |    3 ---
 git-compat-util.h |    8 ++++----
 3 files changed, 5 insertions(+), 7 deletions(-)
Show changes to 3 files +5 −7

compat/cygwin.c, compat/win32.h, git-compat-util.h

diff --git a/compat/cygwin.c b/compat/cygwin.c
index b4a51b9..6695515 100644
--- a/compat/cygwin.c
+++ b/compat/cygwin.c
@@ -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 */
 
diff --git a/compat/win32.h b/compat/win32.h
index 8ce9104..a7ed72b 100644
--- a/compat/win32.h
+++ b/compat/win32.h
@@ -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 --git a/git-compat-util.h b/git-compat-util.h
index ef60803..c4b9e5a 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -65,10 +65,10 @@
 #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_LEAN_AND_MEAN  /* stops windows.h including winsock.h */
+# include <winsock2.h>
+# include <windows.h>
 #endif
 
 #include <unistd.h>
-- 
1.6.5
Johannes Sixt· Nov 9, 2009, 07:20 UTC · re: Ramsay Jones · lore

Re: [PATCH v2 4/4] Add explicit Cygwin check to guard WIN32 header inclusion

Ramsay Jones schrieb:
Show 6 quoted lines
> Since commit 435bdf8c ("Make usage of windows.h lean and mean",
> 16-9-2009), the amount of code potentially including the WIN32
> API header files has greatly increased. In particular, the Cygwin
> build is at greater risk of inadvertently including WIN32 code
> within preprocessor sections protected by the WIN32 or _WIN32
> macros.
Thanks, this makes the problem pretty clear that you want to solve.
Show 6 quoted lines
> The previous commit message, along with comments elsewhere, assert
> that the WIN32 macro is not defined on Cygwin. Currently, this is
> true for the cygwin build. However, the cygwin platform can be
> used to develop WIN32 GUI, WIN32 console, and POSIX applications.
> Indeed it is possible to create applications which use a mix of
> the WIN32 API and POSIX code (eg git!).

In this paragraph, you are only saying that cygwin comes with headers and libraries that can be used to write code using the Windows API in addition to the POSIX headers and libraries. (I'm just asking, not complaining; perhaps this could be stated differently.)

Show 8 quoted lines
> Unlike native WIN32 compilers, gcc on cygwin does not automatically
> define the _WIN32 macro. However, as soon as you include the
> <windows.h> header file, the _WIN32 and WIN32 macros are defined.
> 
> In order to reduce the risk of problems in the future, we protect
> the inclusion of the windows header with an explicit check for
> __CYGWIN__. Also, we move the other use of the <windows.h> header
> from compat/win32.h to compat/cygwin.c

But I sense a contradiction here. Above you are arguing that much more WIN32 code is included, but here you are saying that the explicit check for __CYGWIN__ is just a safety measure to protect us from failures in future changes. Indeed, looking at the code it seems that this extra check is *currently* not necessary:

- Cygwin does not define WIN32, hence, in the original code of this hunk,
Show 19 quoted lines
> diff --git a/git-compat-util.h b/git-compat-util.h
> index ef60803..c4b9e5a 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -65,10 +65,10 @@
>  #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_LEAN_AND_MEAN  /* stops windows.h including winsock.h */
> +# include <winsock2.h>
> +# include <windows.h>
>  #endif
>  
>  #include <unistd.h>
windows.h is not included in cygwin. (Nor is it in the changed version.)
- The other files that include windows.h are compat/win32.h and nedmalloc.
The latter isn't used on cygwin.
- win32.h is included only from cygwin.c, mingw.c, and msvc.c. Only the
first one is used by cygwin, and since it is a .c file, pulling in
windows.h can only concern code in cygwin.c, but not code.

IOW, I disagree with your analysis that a lot of code suffers from windows.h pollution. What am I missing?

-- Hannes
Ramsay Jones· Nov 10, 2009, 18:48 UTC · re: Johannes Sixt · lore

Re: [PATCH v2 4/4] Add explicit Cygwin check to guard WIN32 header inclusion

Johannes Sixt wrote:
Show 10 quoted lines
> Ramsay Jones schrieb:
>> Since commit 435bdf8c ("Make usage of windows.h lean and mean",
>> 16-9-2009), the amount of code potentially including the WIN32
>> API header files has greatly increased. In particular, the Cygwin
>> build is at greater risk of inadvertently including WIN32 code
>> within preprocessor sections protected by the WIN32 or _WIN32
>> macros.
> 
> Thanks, this makes the problem pretty clear that you want to solve.
> 
Hmm, OK. Note the "... potentially including ..." above.
Show 12 quoted lines
>> The previous commit message, along with comments elsewhere, assert
>> that the WIN32 macro is not defined on Cygwin. Currently, this is
>> true for the cygwin build. However, the cygwin platform can be
>> used to develop WIN32 GUI, WIN32 console, and POSIX applications.
>> Indeed it is possible to create applications which use a mix of
>> the WIN32 API and POSIX code (eg git!).
> 
> In this paragraph, you are only saying that cygwin comes with headers and
> libraries that can be used to write code using the Windows API in addition
> to the POSIX headers and libraries. (I'm just asking, not complaining;
> perhaps this could be stated differently.)
> 

;-) The above paragraph, along with the one below, was once part of an even larger paragraph which I had edited multiple times and, eventually, split up. I had intended to delete the last two sentences from the above paragraph. Whilst the content of those sentences is true, it is not necessary for them to be there and, having been orphaned at the end of the paragraph, now sound a bit odd.

If I were to create a version 4 of the patch, I would delete that text.
Show 11 quoted lines
>> Unlike native WIN32 compilers, gcc on cygwin does not automatically
>> define the _WIN32 macro. However, as soon as you include the
>> <windows.h> header file, the _WIN32 and WIN32 macros are defined.
>>
>> In order to reduce the risk of problems in the future, we protect
>> the inclusion of the windows header with an explicit check for
>> __CYGWIN__. Also, we move the other use of the <windows.h> header
>> from compat/win32.h to compat/cygwin.c
> 
> But I sense a contradiction here. Above you are arguing that much more
> WIN32 code is included, but here you are saying that the explicit check

---------------^ potentially

> for __CYGWIN__ is just a safety measure to protect us from failures in
> future changes. Indeed, looking at the code it seems that this extra check
> is *currently* not necessary:
*correct*! I have not claimed otherwise.
[snipped other text I also agree with!]
> IOW, I disagree with your analysis that a lot of code suffers from
> windows.h pollution. What am I missing?

Hmm, I don't know; but the analysis that you claim for me, I don't think is mine! :-D

Hmm, I don't have a great investment in this patch (it doesn't fix a bug after all), so given that you don't see any merit in it, I'm more than happy just to drop it! :-P

So, Junio, could you please drop this patch from the series.
Thanks!

ATB, Ramsay Jones

Ramsay Jones· Nov 9, 2009, 19:26 UTC · re: Ramsay Jones · lore

Re: [PATCH v2 4/4] Add explicit Cygwin check to guard WIN32 header inclusion

Ramsay Jones wrote:
Show 16 quoted lines
> diff --git a/git-compat-util.h b/git-compat-util.h
> index ef60803..c4b9e5a 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -65,10 +65,10 @@
>  #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_LEAN_AND_MEAN  /* stops windows.h including winsock.h */
> +# include <winsock2.h>
> +# include <windows.h>
Arrgghhh.
Sorry, Junio, I meant to change this before sending it out. :(

I had intended to remove the added indentation (which I don't even remember doing!), since it obscures the "real change", which simply consists of the new expression in the #if preprocessor line.

I'll send a new version of this patch.

ATB, Ramsay Jones

Junio C Hamano· Nov 9, 2009, 20:30 UTC · re: Ramsay Jones · lore

Re: [PATCH v2 4/4] Add explicit Cygwin check to guard WIN32 header inclusion

Ramsay Jones <ramsay@ramsay1.demon.co.uk> writes:
Show 13 quoted lines
> Ramsay Jones wrote:
>> diff --git a/git-compat-util.h b/git-compat-util.h
>>...
>
> Arrgghhh.
>
> Sorry, Junio, I meant to change this before sending it out. :(
>
> I had intended to remove the added indentation (which I don't even remember
> doing!), since it obscures the "real change", which simply consists of the
> new expression in the #if preprocessor line.
>
> I'll send a new version of this patch.
Thanks for a notice.

The only reason I queue patches to 'pu' is to give them (slightly) wider exposure than merely being in the mailing list archive, and I do not expect myself merging this series to 'next' before seeing Acks from Windows stakeholders, as I do not build anything on that platform myself.

I saw a comment on substance not form of this patch from J6t, and I suspect that it was a more important issue. You can de-indent while sending your updated patch to address that issue.

Thanks.

← back to recent threads