include/tools/UnixWrappers.h           |   66 ++++++++++++++++++++++-----------
 tools/Module_tools.mk                  |    1 
 tools/StaticLibrary_UseUnixWrappers.mk |   23 +++++++++++
 tools/source/misc/UseUnixWrappers.c    |   18 +++++++++
 4 files changed, 87 insertions(+), 21 deletions(-)

New commits:
commit b28a46aa5012d1538e403a316e6a3824122b12dc
Author:     Tor Lillqvist <[email protected]>
AuthorDate: Sat Dec 13 14:37:33 2025 +0100
Commit:     Tor Lillqvist <[email protected]>
CommitDate: Mon Dec 15 07:31:21 2025 +0100

    Make sure that <tools/UnixWrappers.h> stays compilable as C
    
    We don't use it yet from any C source file, but the plan is to do
    that. Mainly from freetype, fontconfig, and cairo, when these are
    built for Windows when the --enable-headless options is used (for the
    new Collabora Office).
    
    Also some cleanups, sorting #include lines, avoiding warnings, etc.
    
    Change-Id: Ifef3588d06fdcceaeaeb3482399737faaf558d7d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/195602
    Reviewed-by: Mike Kaganski <[email protected]>
    Tested-by: Jenkins

diff --git a/include/tools/UnixWrappers.h b/include/tools/UnixWrappers.h
index 377188e367a0..bf4e58a17090 100644
--- a/include/tools/UnixWrappers.h
+++ b/include/tools/UnixWrappers.h
@@ -32,10 +32,12 @@
  * All functions behave like the wrapped ones, set errno on errors.
  */
 
-#include <wchar.h>
-#include <stdio.h>
 #include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/stat.h>
+#include <wchar.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -44,7 +46,7 @@ extern "C" {
 #ifdef _WIN32
 #include <io.h>
 #include <string.h>
-#include <cassert>
+#include <assert.h>
 #define WIN32_LEAN_AND_MEAN
 #include <Windows.h>
 /* Undo the mapping in <Windows.h> of CreateFont => CreateFontW etc, as vcl 
also uses these
@@ -72,16 +74,33 @@ extern "C" {
 
 #endif
 
+#ifdef __cplusplus
+#define UNIXWRAPPERS_H_MAYBE_UNUSED [[maybe_unused]]
+#else
+#define UNIXWRAPPERS_H_MAYBE_UNUSED
+#endif
+
+#ifdef __GNUC__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+#endif
+
 #ifdef _WIN32
-[[maybe_unused]] static wchar_t* string_to_wide_string(const char* string)
+static wchar_t* string_to_wide_string(const char* string)
 {
-    const int len = strlen(string);
+    const size_t len = strlen(string);
     if (len == 0)
     {
         return _wcsdup(L"");
     }
 
-    const int wlen = MultiByteToWideChar(CP_UTF8, 0, string, len, NULL, 0);
+    if (len > INT_MAX)
+    {
+        /* Trying to be funny, eh? */
+        return _wcsdup(L"");
+    }
+
+    const int wlen = MultiByteToWideChar(CP_UTF8, 0, string, (int)len, NULL, 
0);
     if (wlen <= 0)
     {
         return NULL;
@@ -89,16 +108,15 @@ extern "C" {
 
     wchar_t* result = (wchar_t*)malloc(wlen * 2 + 2);
     assert(result);
-    MultiByteToWideChar(CP_UTF8, 0, string, len, result, wlen);
+    MultiByteToWideChar(CP_UTF8, 0, string, (int)len, result, wlen);
     result[wlen] = L'
 
     return result;
 }
-
 #endif
 
 /* Pass in UTF-8 filename */
-[[maybe_unused]] static int wrap_open(const char* path, int flags, int mode)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_open(const char* path, int flags, 
int mode)
 {
 #ifdef _WIN32
     wchar_t* wpath = string_to_wide_string(path);
@@ -113,7 +131,7 @@ extern "C" {
 #if 0
 
 /* Pass in UTF-16 filename */
-[[maybe_unused]] static int wrap_wopen(const wchar_t* path, int flags, int 
mode)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_wopen(const wchar_t* path, int 
flags, int mode)
 {
 #ifdef _WIN32
     return _wopen(path, flags, mode);
@@ -144,7 +162,7 @@ extern "C" {
 
 #endif
 
-[[maybe_unused]] static int wrap_read(int fd, void* buf, int nbytes)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_read(int fd, void* buf, int nbytes)
 {
 #ifdef _WIN32
     return _read(fd, buf, nbytes);
@@ -153,7 +171,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static int wrap_write(int fd, const void* buf, int nbytes)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_write(int fd, const void* buf, int 
nbytes)
 {
 #ifdef _WIN32
     return _write(fd, buf, nbytes);
@@ -162,7 +180,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static int wrap_fstat(int fd, struct stat* st)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_fstat(int fd, struct stat* st)
 {
 #ifdef _WIN32
     /* Sadly just "struct stat" in the Microsoft C library means a legacy one 
with 32-bit size,
@@ -190,7 +208,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static void* wrap_mmap(int64_t size, int fd, intptr_t* handle)
+UNIXWRAPPERS_H_MAYBE_UNUSED static void* wrap_mmap(int64_t size, int fd, 
intptr_t* handle)
 {
 #ifdef _WIN32
     *handle = (intptr_t)CreateFileMappingW((HANDLE)_get_osfhandle(fd), NULL,
@@ -203,7 +221,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static int wrap_munmap(void* pointer, int64_t size, intptr_t 
handle)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_munmap(void* pointer, int64_t 
size, intptr_t handle)
 {
 #ifdef _WIN32
     (void)size;
@@ -220,7 +238,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static int wrap_dup(int fd)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_dup(int fd)
 {
 #ifdef _WIN32
     return _dup(fd);
@@ -229,7 +247,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static int wrap_close(int fd)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_close(int fd)
 {
 #ifdef _WIN32
     return _close(fd);
@@ -242,7 +260,7 @@ extern "C" {
  * anyway.
  */
 
-[[maybe_unused]] static FILE* wrap_fopen(const char* path, const char* mode)
+UNIXWRAPPERS_H_MAYBE_UNUSED static FILE* wrap_fopen(const char* path, const 
char* mode)
 {
 #ifdef _WIN32
     wchar_t* wpath = string_to_wide_string(path);
@@ -256,7 +274,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static int wrap_stat(const char* path, struct stat* st)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_stat(const char* path, struct 
stat* st)
 {
 #ifdef _WIN32
     struct _stat32 st32;
@@ -283,7 +301,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static int wrap_access(const char* path, int mode)
+UNIXWRAPPERS_H_MAYBE_UNUSED static int wrap_access(const char* path, int mode)
 {
 #ifdef _WIN32
     wchar_t* wpath = string_to_wide_string(path);
@@ -295,7 +313,7 @@ extern "C" {
 #endif
 }
 
-[[maybe_unused]] static const char* wrap_realpath(const char* path, char* 
resolved_path)
+UNIXWRAPPERS_H_MAYBE_UNUSED static const char* wrap_realpath(const char* path, 
char* resolved_path)
 {
 #ifdef _WIN32
     strcpy(resolved_path, path);
@@ -305,8 +323,14 @@ extern "C" {
 #endif
 }
 
+#ifdef __GNUC__
+#pragma GCC diagnostic pop
+#endif
+
 #ifdef __cplusplus
 }
 #endif
 
+#undef UNIXWRAPPERS_H_MAYBE_UNUSED
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/tools/Module_tools.mk b/tools/Module_tools.mk
index 4d477462c0fe..4a13e8a1aa9b 100644
--- a/tools/Module_tools.mk
+++ b/tools/Module_tools.mk
@@ -24,6 +24,7 @@ $(eval $(call gb_Module_add_targets,tools,\
     CustomTarget_reversemap \
     Library_tl \
     StaticLibrary_ooopathutils \
+    StaticLibrary_UseUnixWrappers \
 ))
 
 $(eval $(call gb_Module_add_targets_for_build,tools,\
diff --git a/tools/StaticLibrary_UseUnixWrappers.mk 
b/tools/StaticLibrary_UseUnixWrappers.mk
new file mode 100644
index 000000000000..a05f87d0fb16
--- /dev/null
+++ b/tools/StaticLibrary_UseUnixWrappers.mk
@@ -0,0 +1,23 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+
+# This is a dummy library with one C source that is compiled to make
+# sure that the <tools/UnixWrappers.h> stays compilable as plain C. If
+# you can think of a better way to ensure that (before we actually use
+# UnixWrappers.h in some (external) C file that is compiled in normal
+# Windows builds), go ahead.
+
+$(eval $(call gb_StaticLibrary_StaticLibrary,UseUnixWrappers))
+
+$(eval $(call gb_StaticLibrary_add_cobjects,UseUnixWrappers,\
+    tools/source/misc/UseUnixWrappers \
+))
+
+# vim: set noet sw=4 ts=4:
diff --git a/tools/source/misc/UseUnixWrappers.c 
b/tools/source/misc/UseUnixWrappers.c
new file mode 100644
index 000000000000..4f6193001be8
--- /dev/null
+++ b/tools/source/misc/UseUnixWrappers.c
@@ -0,0 +1,18 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; 
fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+/* Dummy C source file that is compiled to make sure that the
+ * <tools/UnixWrappers.h> stays compilable as plain C.
+ */
+
+#include <tools/UnixWrappers.h>
+
+/* Nothing else */
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */

Reply via email to