[PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-18 Thread Steve Lhomme
LoadLibrary is forbidden in such apps (can only load DLLs from within the app
package).
The API entries are available to all apps linking with the Windows API as found
here:
https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis

windowsapp.lib (and mincore.lib for Windows 8) are both available in MinGW as
well.

GetSystemTimePreciseAsFileTime is only allowed in Win10 UWP apps.
---
 lib/gettimeofday.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 19804793a..087f7eada 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -45,12 +45,17 @@ static BOOL initialized = FALSE;
 static void
 initialize (void)
 {
+#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && _WIN32_WINNT >= 
0x0A00 /* _WIN32_WINNT_WIN10 */
+  /* LoadLibrary not allowed but the functions are available with the windows 
runtime */
+  GetSystemTimePreciseAsFileTimeFunc = GetSystemTimePreciseAsFileTime;
+#else /* WINAPI_PARTITION_DESKTOP */
   HMODULE kernel32 = LoadLibrary ("kernel32.dll");
   if (kernel32 != NULL)
 {
   GetSystemTimePreciseAsFileTimeFunc =
 (GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, 
"GetSystemTimePreciseAsFileTime");
 }
+#endif /* WINAPI_PARTITION_DESKTOP */
   initialized = TRUE;
 }
 
-- 
2.26.2




Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-27 Thread Steve Lhomme

Any update on this patch ?

On Dekstop it's better to use kernel32.dll as it's loaded with every 
process, so the LoadLibrary is not loading any new DLL.


On Winstore/UWP apps you cannot use LoadLibrary, only LoadLibraryFromApp 
which cannot be used to load system DLLs. Static linking in necessary in 
this case. Any app targeting UWP is already using the windowsapp.lib 
(replacing the kernel32 lib they used to link with) so no need to fore 
linking with it via pkg-config, libtool, etc.


For example in CLang you either link with windowsapp or kernel32:
https://github.com/llvm-project/clang/blob/master/lib/Driver/ToolChains/MinGW.cpp#L269

On 2020-05-19 8:24, Steve Lhomme wrote:

LoadLibrary is forbidden in such apps (can only load DLLs from within the app
package).
The API entries are available to all apps linking with the Windows API as found
here:
https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis

windowsapp.lib (and mincore.lib for Windows 8) are both available in MinGW as
well.

GetSystemTimePreciseAsFileTime is only allowed in Win10 UWP apps.
---
  lib/gettimeofday.c | 5 +
  1 file changed, 5 insertions(+)

diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 19804793a..087f7eada 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -45,12 +45,17 @@ static BOOL initialized = FALSE;
  static void
  initialize (void)
  {
+#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && _WIN32_WINNT >= 
0x0A00 /* _WIN32_WINNT_WIN10 */
+  /* LoadLibrary not allowed but the functions are available with the windows 
runtime */
+  GetSystemTimePreciseAsFileTimeFunc = GetSystemTimePreciseAsFileTime;
+#else /* WINAPI_PARTITION_DESKTOP */
HMODULE kernel32 = LoadLibrary ("kernel32.dll");
if (kernel32 != NULL)
  {
GetSystemTimePreciseAsFileTimeFunc =
  (GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, 
"GetSystemTimePreciseAsFileTime");
  }
+#endif /* WINAPI_PARTITION_DESKTOP */
initialized = TRUE;
  }
  
--

2.26.2






Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-28 Thread Bruno Haible
Hi,

Steve Lhomme wrote:
> LoadLibrary is forbidden in such apps (can only load DLLs from within the app
> package).
> The API entries are available to all apps linking with the Windows API as 
> found
> here:
> https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis

Thanks for these infos.

> +#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && _WIN32_WINNT >= 
> 0x0A00 /* _WIN32_WINNT_WIN10 */

The GetSystemTimePreciseAsFileTime function is available starting with Windows 
8,
therefore
  - the condition with WINAPI_FAMILY_PARTITION is not necessary,
  - the condition _WIN32_WINNT >= 0x0A00 is overly restrictive;
_WIN32_WINNT >= _WIN32_WINNT_WIN8 will work just as well.

I have added a page about the native Windows APIs at
https://gitlab.com/ghwiki/gnow-how/-/wikis/Platforms/Native_Windows

Then here is a patch to avoid LoadLibrary when possible.


2020-05-28  Bruno Haible  

Avoid dynamic loading of Windows API functions when possible.
Reported by Steve Lhomme  in
.
* lib/gettimeofday.c (GetProcAddress,
GetSystemTimePreciseAsFileTimeFuncType,
GetSystemTimePreciseAsFileTimeFunc, initialized, initialize): Don't
define in a build for Windows 8 or higher.
* lib/isatty.c (GetProcAddress, GetNamedPipeClientProcessIdFuncType,
GetNamedPipeClientProcessIdFunc, QueryFullProcessImageNameFuncType,
QueryFullProcessImageNameFunc, initialized, initialize): Don't define
in a build for Windows Vista or higher.
* lib/stat-w32.c (GetProcAddress, GetFileInformationByHandleExFuncType,
GetFileInformationByHandleExFunc, GetFinalPathNameByHandleFuncType,
GetFinalPathNameByHandleFunc, initialized, initialize): Likewise.

diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 1980479..3d53115 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -33,9 +33,11 @@
 
 #ifdef WINDOWS_NATIVE
 
+# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
+
 /* Avoid warnings from gcc -Wcast-function-type.  */
-# define GetProcAddress \
-   (void *) GetProcAddress
+#  define GetProcAddress \
+(void *) GetProcAddress
 
 /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8.  */
 typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME 
*lpTime);
@@ -54,6 +56,8 @@ initialize (void)
   initialized = TRUE;
 }
 
+# endif
+
 #endif
 
 /* This is a wrapper for gettimeofday.  It is used only on systems
@@ -84,8 +88,10 @@ gettimeofday (struct timeval *restrict tv, void *restrict tz)
  .  */
   FILETIME current_time;
 
+# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
   if (!initialized)
 initialize ();
+# endif
   if (GetSystemTimePreciseAsFileTimeFunc != NULL)
 GetSystemTimePreciseAsFileTimeFunc (¤t_time);
   else
diff --git a/lib/isatty.c b/lib/isatty.c
index 6cdc0fb..fc771d1 100644
--- a/lib/isatty.c
+++ b/lib/isatty.c
@@ -39,9 +39,11 @@
 # include 
 #endif
 
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
+
 /* Avoid warnings from gcc -Wcast-function-type.  */
-#define GetProcAddress \
-  (void *) GetProcAddress
+# define GetProcAddress \
+   (void *) GetProcAddress
 
 /* GetNamedPipeClientProcessId was introduced only in Windows Vista.  */
 typedef BOOL (WINAPI * GetNamedPipeClientProcessIdFuncType) (HANDLE hPipe,
@@ -69,6 +71,8 @@ initialize (void)
   initialized = TRUE;
 }
 
+#endif
+
 static BOOL IsConsoleHandle (HANDLE h)
 {
   DWORD mode;
@@ -84,8 +88,10 @@ static BOOL IsCygwinConsoleHandle (HANDLE h)
   BOOL result = FALSE;
   ULONG processId;
 
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
   if (!initialized)
 initialize ();
+#endif
 
   /* GetNamedPipeClientProcessId
  

diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index b9163f5..02ad9ab 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -40,18 +40,20 @@
 #include "pathmax.h"
 #include "verify.h"
 
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
+
 /* Avoid warnings from gcc -Wcast-function-type.  */
-#define GetProcAddress \
-  (void *) GetProcAddress
+# define GetProcAddress \
+   (void *) GetProcAddress
 
-#if _GL_WINDOWS_STAT_INODES == 2
+# if _GL_WINDOWS_STAT_INODES == 2
 /* GetFileInformationByHandleEx was introduced only in Windows Vista.  */
 typedef DWORD (WINAPI * GetFileInformationByHandleExFuncType) (HANDLE hFile,

FILE_INFO_BY_HANDLE_CLASS fiClass,
LPVOID lpBuffer,
DWORD 
dwBufferSize);
 static GetFileInformationByHandleExFuncType GetFileInformationByHandleExFunc = 
NULL;
-#endif
+# endif
 /* GetFinalPathNameByHandle was introduced only in Windows Vista.  */
 typedef DWORD (WINAPI * GetFinalPathNameByHandleFuncType) (

Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-28 Thread Daiki Ueno
Hello,

Bruno Haible  writes:

> diff --git a/lib/stat-w32.c b/lib/stat-w32.c
> index b9163f5..02ad9ab 100644
> --- a/lib/stat-w32.c
> +++ b/lib/stat-w32.c
> @@ -40,18 +40,20 @@
>  #include "pathmax.h"
>  #include "verify.h"
>  
> +#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)
> +

I am totally unfamiliar with Windows code, but this change seems to
break MinGW cross build, because GetFinalPathNameByHandleFunc is defined
only if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA), but referred to from
_gl_convert_FILETIME_to_timespec without the guard:

 ../../gl/stat-w32.c: In function '_gl_fstat_by_handle':
 ../../gl/stat-w32.c:259:23: error: 'GetFinalPathNameByHandleFunc' undeclared 
(first use in this function); did you mean 'GetFinalPathNameByHandleW'?
   259 |   || (GetFinalPathNameByHandleFunc != NULL
   |   ^~~~
   |   GetFinalPathNameByHandleW

Regards,
-- 
Daiki Ueno



Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-29 Thread Bruno Haible
Daiki Ueno wrote:
> this change seems to
> break MinGW cross build, because GetFinalPathNameByHandleFunc is defined
> only if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA), but referred to from
> _gl_convert_FILETIME_to_timespec without the guard:

Oops, indeed. With the appropriate _WIN32_WINNT setting, I'm getting errors in
all three files:

C:\cygwin64\home\bruno\testdir2\gllib\gettimeofday.c(95): error C2065: 
'GetSystemTimePreciseAsFileTimeFunc': undeclared identifier

C:\cygwin64\home\bruno\testdir2\gllib\isatty.c(99): error C2065: 
'GetNamedPipeClientProcessIdFunc': undeclared identifier
C:\cygwin64\home\bruno\testdir2\gllib\isatty.c(100): error C2065: 
'QueryFullProcessImageNameFunc': undeclared identifier

C:\cygwin64\home\bruno\testdir2\gllib\stat-w32.c(259): error C2065: 
'GetFinalPathNameByHandleFunc': undeclared identifier

This patch fixes it.


2020-05-29  Bruno Haible  

Fix compilation error on native Windows (regression from 2020-05-28).
Reported by Daiki Ueno.
* lib/gettimeofday.c (GetSystemTimePreciseAsFileTimeFunc): Define as
macro when not using dynamic loading.
* lib/isatty.c (GetNamedPipeClientProcessIdFunc,
QueryFullProcessImageNameFunc): Likewise.
* lib/stat-w32.c (GetFileInformationByHandleExFunc,
GetFinalPathNameByHandleFunc): Likewise.

diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 3d53115..93914ba 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -56,6 +56,10 @@ initialize (void)
   initialized = TRUE;
 }
 
+# else
+
+#  define GetSystemTimePreciseAsFileTimeFunc GetSystemTimePreciseAsFileTime
+
 # endif
 
 #endif
diff --git a/lib/isatty.c b/lib/isatty.c
index fc771d1..4c5b8e3 100644
--- a/lib/isatty.c
+++ b/lib/isatty.c
@@ -71,6 +71,11 @@ initialize (void)
   initialized = TRUE;
 }
 
+#else
+
+# define GetNamedPipeClientProcessIdFunc GetNamedPipeClientProcessId
+# define QueryFullProcessImageNameFunc QueryFullProcessImageName
+
 #endif
 
 static BOOL IsConsoleHandle (HANDLE h)
diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index 02ad9ab..cca12dd 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -78,6 +78,11 @@ initialize (void)
   initialized = TRUE;
 }
 
+#else
+
+# define GetFileInformationByHandleExFunc GetFileInformationByHandleEx
+# define GetFinalPathNameByHandleFunc GetFinalPathNameByHandle
+
 #endif
 
 /* Converts a FILETIME to GMT time since 1970-01-01 00:00:00.  */




Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-29 Thread Steve Lhomme

On 2020-05-29 2:04, Bruno Haible wrote:

Hi,

Steve Lhomme wrote:

LoadLibrary is forbidden in such apps (can only load DLLs from within the app
package).
The API entries are available to all apps linking with the Windows API as found
here:
https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis


Thanks for these infos.


+#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && _WIN32_WINNT >= 
0x0A00 /* _WIN32_WINNT_WIN10 */


The GetSystemTimePreciseAsFileTime function is available starting with Windows 
8,
therefore
   - the condition with WINAPI_FAMILY_PARTITION is not necessary,
   - the condition _WIN32_WINNT >= 0x0A00 is overly restrictive;
 _WIN32_WINNT >= _WIN32_WINNT_WIN8 will work just as well.


OK. I thought GetSystemTimePreciseAsFileTime was not available in Win8 
UWP apps. But it seems it was: 
https://docs.microsoft.com/en-us/windows/win32/apiindex/windows-81-api-sets



I have added a page about the native Windows APIs at
https://gitlab.com/ghwiki/gnow-how/-/wikis/Platforms/Native_Windows

Then here is a patch to avoid LoadLibrary when possible.


2020-05-28  Bruno Haible  

Avoid dynamic loading of Windows API functions when possible.
Reported by Steve Lhomme  in
.
* lib/gettimeofday.c (GetProcAddress,
GetSystemTimePreciseAsFileTimeFuncType,
GetSystemTimePreciseAsFileTimeFunc, initialized, initialize): Don't
define in a build for Windows 8 or higher.
* lib/isatty.c (GetProcAddress, GetNamedPipeClientProcessIdFuncType,
GetNamedPipeClientProcessIdFunc, QueryFullProcessImageNameFuncType,
QueryFullProcessImageNameFunc, initialized, initialize): Don't define
in a build for Windows Vista or higher.
* lib/stat-w32.c (GetProcAddress, GetFileInformationByHandleExFuncType,
GetFileInformationByHandleExFunc, GetFinalPathNameByHandleFuncType,
GetFinalPathNameByHandleFunc, initialized, initialize): Likewise.

diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 1980479..3d53115 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -33,9 +33,11 @@
  
  #ifdef WINDOWS_NATIVE
  
+# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)

+
  /* Avoid warnings from gcc -Wcast-function-type.  */
-# define GetProcAddress \
-   (void *) GetProcAddress
+#  define GetProcAddress \
+(void *) GetProcAddress
  
  /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8.  */

  typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME 
*lpTime);
@@ -54,6 +56,8 @@ initialize (void)
initialized = TRUE;
  }
  
+# endif

+
  #endif
  
  /* This is a wrapper for gettimeofday.  It is used only on systems

@@ -84,8 +88,10 @@ gettimeofday (struct timeval *restrict tv, void *restrict tz)
   .  */
FILETIME current_time;
  
+# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)

if (!initialized)
  initialize ();
+# endif
if (GetSystemTimePreciseAsFileTimeFunc != NULL)
  GetSystemTimePreciseAsFileTimeFunc (¤t_time);
else
diff --git a/lib/isatty.c b/lib/isatty.c
index 6cdc0fb..fc771d1 100644
--- a/lib/isatty.c
+++ b/lib/isatty.c
@@ -39,9 +39,11 @@
  # include 
  #endif
  
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)

+
  /* Avoid warnings from gcc -Wcast-function-type.  */
-#define GetProcAddress \
-  (void *) GetProcAddress
+# define GetProcAddress \
+   (void *) GetProcAddress
  
  /* GetNamedPipeClientProcessId was introduced only in Windows Vista.  */

  typedef BOOL (WINAPI * GetNamedPipeClientProcessIdFuncType) (HANDLE hPipe,
@@ -69,6 +71,8 @@ initialize (void)
initialized = TRUE;
  }
  
+#endif

+
  static BOOL IsConsoleHandle (HANDLE h)
  {
DWORD mode;
@@ -84,8 +88,10 @@ static BOOL IsCygwinConsoleHandle (HANDLE h)
BOOL result = FALSE;
ULONG processId;
  
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)

if (!initialized)
  initialize ();
+#endif
  
/* GetNamedPipeClientProcessId

   

diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index b9163f5..02ad9ab 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -40,18 +40,20 @@
  #include "pathmax.h"
  #include "verify.h"
  
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)

+
  /* Avoid warnings from gcc -Wcast-function-type.  */
-#define GetProcAddress \
-  (void *) GetProcAddress
+# define GetProcAddress \
+   (void *) GetProcAddress
  
-#if _GL_WINDOWS_STAT_INODES == 2

+# if _GL_WINDOWS_STAT_INODES == 2
  /* GetFileInformationByHandleEx was introduced only in Windows Vista.  */
  typedef DWORD (WINAPI * GetFileInformationByHandleExFuncType) (HANDLE hFile,
 
FILE_INFO_BY_HANDLE_CLASS fiClass,
 LPVOID 
lpBuffer,