You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2024/07/19 04:12:03 UTC

(logging-log4cxx) branch master updated: Fix building with mingw-w64-gcc (#389)

This is an automated email from the ASF dual-hosted git repository.

swebb2066 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f0abc24 Fix building with mingw-w64-gcc (#389)
4f0abc24 is described below

commit 4f0abc24feff8f1925f5bbe2a8b4dc8b2c184291
Author: مهدي شينون (Mehdi Chinoune) <79...@users.noreply.github.com>
AuthorDate: Fri Jul 19 05:11:58 2024 +0100

    Fix building with mingw-w64-gcc (#389)
    
    * Generate pkgconfig files on Windows
    
    pkgconfig files could be used on Windows by both MSVC and MinGW compilers
    
    * Replace WIN32 with _WIN32
    
    WIN32 is not defined by default on MinGW. While _WIN32 is known be always defined on Windows
    
    reported before: https://issues.apache.org/jira/browse/LOGCXX-5
    
    * git-ignore build directory as It's usually used by CMake users for out-of-tree building.
    
    * Fix FindAPR.cmake on MinGW
    
    WIN32 compile definition is required.
    
    * Fix building with mingw-w64-gcc+winpthreads
    
    `!(defined(_WIN32) && defined(_LIBCPP_VERSION))` was added because In some platforms winpthreads exist but It's not used by libc++.
    
    * Silent a warning about Windows.h not being portable
    
    * Use backslash with MSVC only
---
 .gitignore                                         |  1 +
 CMakeLists.txt                                     | 54 +++++++++++-----------
 src/cmake/FindAPR.cmake                            |  6 +++
 src/examples/cpp/com/foo/config3.cpp               |  6 +--
 src/main/cpp/loggingevent.cpp                      | 22 ++++-----
 src/main/cpp/multiprocessrollingfileappender.cpp   |  2 +-
 src/main/cpp/nteventlogappender.cpp                |  2 +-
 src/main/cpp/threadutility.cpp                     |  6 +--
 src/main/cpp/timebasedrollingpolicy.cpp            |  2 +-
 src/main/include/CMakeLists.txt                    |  9 ++--
 .../include/log4cxx/spi/location/locationinfo.h    |  2 +-
 src/test/cpp/abts.h                                |  2 +-
 src/test/cpp/helpers/threadutilitytestcase.cpp     |  2 +-
 src/test/cpp/pattern/patternparsertestcase.cpp     |  8 ++--
 14 files changed, 66 insertions(+), 58 deletions(-)

diff --git a/.gitignore b/.gitignore
index fa67582c..4b18b8b3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,5 +32,6 @@ src/test/cpp/Testing/
 src/test/resources/org/
 src/test/resources/output/
 target/
+build/
 
 *.user
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 21a018bb..1bff64c9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -170,35 +170,33 @@ IF(MSVC AND BUILD_SHARED_LIBS AND LOG4CXX_INSTALL_PDB)
   )
 ENDIF()
 
-if(UNIX)
-  # Support for pkg-config in consuming projects
-  set(prefix "${CMAKE_INSTALL_PREFIX}")
-  set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
-  set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
-  set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
-  set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
-  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx.pc.in"
-    "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc"
-  )
+# Support for pkg-config in consuming projects
+set(prefix "${CMAKE_INSTALL_PREFIX}")
+set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
+set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
+set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
+set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
+configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx.pc.in"
+  "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc"
+)
+
+install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc" 
+  DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
+
+if(LOG4CXX_QT_SUPPORT)
+    set(prefix "${CMAKE_INSTALL_PREFIX}")
+    set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
+    set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
+    set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
+    set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
+    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx-qt.pc.in"
+      "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
+    )
 
-  install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc" 
-    DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
-
-  if(LOG4CXX_QT_SUPPORT)
-      set(prefix "${CMAKE_INSTALL_PREFIX}")
-      set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
-      set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
-      set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
-      set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
-      configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx-qt.pc.in"
-        "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
-      )
-
-      install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
-        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
-      )
-  endif(LOG4CXX_QT_SUPPORT)
-endif(UNIX)
+    install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
+      DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
+    )
+endif(LOG4CXX_QT_SUPPORT)
 
 # Support for find_package(log4cxx) in consuming CMake projects using
 # target_include_directories(myApplication PRIVATE $<TARGET_PROPERTY:log4cxx,INTERFACE_INCLUDE_DIRECTORIES>)
diff --git a/src/cmake/FindAPR.cmake b/src/cmake/FindAPR.cmake
index e3f4c98f..baf10277 100644
--- a/src/cmake/FindAPR.cmake
+++ b/src/cmake/FindAPR.cmake
@@ -46,6 +46,9 @@ if(APR_FOUND)
   else()
     set(APR_LIBRARIES ${APR_LINK_LIBRARIES})
   endif()
+  if(WIN32)
+    list(APPEND APR_COMPILE_DEFINITIONS WIN32)
+  endif()
 else()
   find_program(APR_CONFIG_EXECUTABLE
       apr-1-config
@@ -78,6 +81,9 @@ else()
         find_program(APR_DLL libapr-1.dll)
       endif()
   endif()
+  if(WIN32)
+    list(APPEND APR_COMPILE_DEFINITIONS WIN32)
+  endif()
 endif()
 
 find_package_handle_standard_args(APR
diff --git a/src/examples/cpp/com/foo/config3.cpp b/src/examples/cpp/com/foo/config3.cpp
index 8e686f68..33ee90d1 100644
--- a/src/examples/cpp/com/foo/config3.cpp
+++ b/src/examples/cpp/com/foo/config3.cpp
@@ -24,8 +24,8 @@
 #include <log4cxx/helpers/loglog.h>
 #include <log4cxx/helpers/transcoder.h>
 
-#ifdef WIN32
-#include <Windows.h>
+#ifdef _WIN32
+#include <windows.h>
 #elif __APPLE__
 #include <mach-o/dyld.h>
 #elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
@@ -48,7 +48,7 @@ auto DefaultConfigurationFileNames(std::string& altPrefix) -> std::vector<std::s
 	static const int bufSize = 4096;
 	char buf[bufSize+1] = {0}, pathSepar = '/';
 	uint32_t bufCount = 0;
-#if defined(WIN32)
+#if defined(_WIN32)
 	GetModuleFileName(NULL, buf, bufSize);
 	pathSepar = '\\';
 #elif defined(__APPLE__)
diff --git a/src/main/cpp/loggingevent.cpp b/src/main/cpp/loggingevent.cpp
index 2c67447a..69010202 100644
--- a/src/main/cpp/loggingevent.cpp
+++ b/src/main/cpp/loggingevent.cpp
@@ -333,12 +333,12 @@ LoggingEvent::KeySet LoggingEvent::getPropertyKeySet() const
 
 const LogString& LoggingEvent::getCurrentThreadName()
 {
-#if defined(_WIN32)
-	using ThreadIdType = DWORD;
-	ThreadIdType threadId = GetCurrentThreadId();
-#elif LOG4CXX_HAS_PTHREAD_SELF
+#if LOG4CXX_HAS_PTHREAD_SELF && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
 	using ThreadIdType = pthread_t;
 	ThreadIdType threadId = pthread_self();
+#elif defined(_WIN32)
+	using ThreadIdType = DWORD;
+	ThreadIdType threadId = GetCurrentThreadId();
 #else
 	using ThreadIdType = int;
 	ThreadIdType threadId = 0;
@@ -362,16 +362,16 @@ const LogString& LoggingEvent::getCurrentThreadName()
 		return thread_id_string;
 	}
 
-#if defined(_WIN32)
-	char result[20];
-	apr_snprintf(result, sizeof(result), LOG4CXX_WIN32_THREAD_FMTSPEC, threadId);
-	thread_id_string = Transcoder::decode(result);
-#elif LOG4CXX_HAS_PTHREAD_SELF
+#if LOG4CXX_HAS_PTHREAD_SELF && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
 	// pthread_t encoded in HEX takes needs as many characters
 	// as two times the size of the type, plus an additional null byte.
 	char result[sizeof(pthread_t) * 3 + 10];
 	apr_snprintf(result, sizeof(result), LOG4CXX_APR_THREAD_FMTSPEC, (void*) &threadId);
 	thread_id_string = Transcoder::decode(result);
+#elif defined(_WIN32)
+	char result[20];
+	apr_snprintf(result, sizeof(result), LOG4CXX_WIN32_THREAD_FMTSPEC, threadId);
+	thread_id_string = Transcoder::decode(result);
 #else
 	thread_id_string = LOG4CXX_STR("0x00000000");
 #endif
@@ -389,14 +389,14 @@ const LogString& LoggingEvent::getCurrentThreadUserName()
 		return thread_name;
 	}
 
-#if LOG4CXX_HAS_PTHREAD_GETNAME
+#if LOG4CXX_HAS_PTHREAD_GETNAME && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
 	char result[16];
 	pthread_t current_thread = pthread_self();
 	if (pthread_getname_np(current_thread, result, sizeof(result)) < 0 || 0 == result[0])
 		thread_name = getCurrentThreadName();
 	else
 		thread_name = Transcoder::decode(result);
-#elif WIN32
+#elif defined(_WIN32)
 	typedef HRESULT (WINAPI *TGetThreadDescription)(HANDLE, PWSTR*);
 	static struct initialiser
 	{
diff --git a/src/main/cpp/multiprocessrollingfileappender.cpp b/src/main/cpp/multiprocessrollingfileappender.cpp
index a29b0c6d..b20b8589 100644
--- a/src/main/cpp/multiprocessrollingfileappender.cpp
+++ b/src/main/cpp/multiprocessrollingfileappender.cpp
@@ -241,7 +241,7 @@ bool MultiprocessRollingFileAppender::rolloverInternal(Pool& p)
 
 			if (stat == APR_SUCCESS)
 			{
-#ifdef WIN32
+#ifdef _WIN32
 				snprintf(szUid, MAX_FILE_LEN, "%p", uid);
 #else
 				snprintf(szUid, MAX_FILE_LEN, "%u", (unsigned int)uid);
diff --git a/src/main/cpp/nteventlogappender.cpp b/src/main/cpp/nteventlogappender.cpp
index 424b575a..42dbbf2c 100644
--- a/src/main/cpp/nteventlogappender.cpp
+++ b/src/main/cpp/nteventlogappender.cpp
@@ -16,7 +16,7 @@
  */
 
 #if (defined(WIN32) || defined(_WIN32)) && !defined(_WIN32_WCE)
-#include <Windows.h>
+#include <windows.h>
 #include <Heapapi.h>
 #include <log4cxx/nt/nteventlogappender.h>
 #include <log4cxx/spi/loggingevent.h>
diff --git a/src/main/cpp/threadutility.cpp b/src/main/cpp/threadutility.cpp
index 321c3ee6..d4211ce5 100644
--- a/src/main/cpp/threadutility.cpp
+++ b/src/main/cpp/threadutility.cpp
@@ -26,7 +26,7 @@
 #include <signal.h>
 #include <mutex>
 
-#if WIN32
+#ifdef _WIN32
 	#include <windows.h>
 	#include <processthreadsapi.h>
 #endif
@@ -138,12 +138,12 @@ void ThreadUtility::threadStartedNameThread(LogString threadName,
 	std::thread::id /*threadId*/,
 	std::thread::native_handle_type nativeHandle)
 {
-#if LOG4CXX_HAS_PTHREAD_SETNAME
+#if LOG4CXX_HAS_PTHREAD_SETNAME && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
 	LOG4CXX_ENCODE_CHAR(sthreadName, threadName);
 	if (pthread_setname_np(static_cast<pthread_t>(nativeHandle), sthreadName.c_str()) < 0) {
 		LOGLOG_ERROR(LOG4CXX_STR("unable to set thread name"));
 	}
-#elif WIN32
+#elif defined(_WIN32)
 	typedef HRESULT (WINAPI *TSetThreadDescription)(HANDLE, PCWSTR);
 	static struct initialiser
 	{
diff --git a/src/main/cpp/timebasedrollingpolicy.cpp b/src/main/cpp/timebasedrollingpolicy.cpp
index e9dd5a2e..745cbbad 100644
--- a/src/main/cpp/timebasedrollingpolicy.cpp
+++ b/src/main/cpp/timebasedrollingpolicy.cpp
@@ -171,7 +171,7 @@ const std::string TimeBasedRollingPolicy::createFile(const std::string& fileName
 
 	if (stat == APR_SUCCESS)
 	{
-#ifdef WIN32
+#ifdef _WIN32
 		snprintf(szUid, MAX_FILE_LEN, "%p", uid);
 #else
 		snprintf(szUid, MAX_FILE_LEN, "%u", uid);
diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index e078865a..9eac0377 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -121,9 +121,12 @@ CHECK_SYMBOL_EXISTS(mbsrtowcs "cwchar" HAS_MBSRTOWCS)
 CHECK_SYMBOL_EXISTS(wcstombs "cstdlib" HAS_WCSTOMBS)
 CHECK_SYMBOL_EXISTS(fwide "cwchar" HAS_FWIDE )
 CHECK_SYMBOL_EXISTS(syslog "syslog.h" HAS_SYSLOG)
-if(UNIX)
+if(NOT MSVC)
     set(CMAKE_REQUIRED_LIBRARIES "pthread")
-    CHECK_SYMBOL_EXISTS(pthread_sigmask "signal.h" HAS_PTHREAD_SIGMASK)
+    # pthread_sigmask exists on MINGW but with no function
+    if(NOT MINGW)
+        CHECK_SYMBOL_EXISTS(pthread_sigmask "signal.h" HAS_PTHREAD_SIGMASK)
+    endif()
     CHECK_SYMBOL_EXISTS(pthread_self "pthread.h" HAS_PTHREAD_SELF)
 
     # Check for the (linux) pthread_setname_np.
@@ -138,7 +141,7 @@ if(UNIX)
     if(${PTHREAD_GETNAME_NP_FOUND})
         set(HAS_PTHREAD_GETNAME 1)
     endif()
-endif(UNIX)
+endif()
 
 foreach(varName
   HAS_THREAD_LOCAL
diff --git a/src/main/include/log4cxx/spi/location/locationinfo.h b/src/main/include/log4cxx/spi/location/locationinfo.h
index 3b68d531..c4af54e4 100644
--- a/src/main/include/log4cxx/spi/location/locationinfo.h
+++ b/src/main/include/log4cxx/spi/location/locationinfo.h
@@ -22,7 +22,7 @@
 #include <string>
 #include <string.h>
 
-#if defined(_WIN32)
+#if defined(_MSC_VER)
 #define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '\\'
 #else
 #define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '/'
diff --git a/src/test/cpp/abts.h b/src/test/cpp/abts.h
index 05e962d2..73c8e17a 100644
--- a/src/test/cpp/abts.h
+++ b/src/test/cpp/abts.h
@@ -20,7 +20,7 @@
 #include <vector>
 #include <string>
 
-#ifdef WIN32
+#ifdef _WIN32
 	#include <io.h>
 #else
 	#include <unistd.h>
diff --git a/src/test/cpp/helpers/threadutilitytestcase.cpp b/src/test/cpp/helpers/threadutilitytestcase.cpp
index 1304f896..c62d665b 100644
--- a/src/test/cpp/helpers/threadutilitytestcase.cpp
+++ b/src/test/cpp/helpers/threadutilitytestcase.cpp
@@ -33,7 +33,7 @@ LOGUNIT_CLASS(ThreadUtilityTest)
 	LOGUNIT_TEST(testNullFunctions);
 	LOGUNIT_TEST(testCustomFunctions);
 	LOGUNIT_TEST(testDefaultFunctions);
-#if LOG4CXX_HAS_PTHREAD_SETNAME || defined(WIN32)
+#if LOG4CXX_HAS_PTHREAD_SETNAME || defined(_WIN32)
 	LOGUNIT_TEST(testThreadNameLogging);
 #endif
 	LOGUNIT_TEST_SUITE_END();
diff --git a/src/test/cpp/pattern/patternparsertestcase.cpp b/src/test/cpp/pattern/patternparsertestcase.cpp
index bff9f830..7a653cb4 100644
--- a/src/test/cpp/pattern/patternparsertestcase.cpp
+++ b/src/test/cpp/pattern/patternparsertestcase.cpp
@@ -56,8 +56,8 @@
 #define LOG4CXX_TEST 1
 #include <log4cxx/private/log4cxx_private.h>
 #include <thread>
-#if WIN32
-#include <Windows.h>
+#ifdef _WIN32
+#include <windows.h>
 #endif
 
 using namespace log4cxx;
@@ -89,12 +89,12 @@ public:
 	{
 		LogString threadName = LOG4CXX_STR("log4cxx-thr");
 
-#if LOG4CXX_HAS_PTHREAD_SETNAME
+#if LOG4CXX_HAS_PTHREAD_SETNAME && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
 	LOG4CXX_ENCODE_CHAR(sthreadName, threadName);
 	if( pthread_setname_np( pthread_self(), sthreadName.c_str() ) < 0 ){
 		LOGLOG_ERROR( LOG4CXX_STR("unable to set thread name") );
 	}
-#elif WIN32
+#elif defined(_WIN32)
 	LOG4CXX_ENCODE_WCHAR(wthreadName, threadName);
 	HRESULT hr = SetThreadDescription(GetCurrentThread(), wthreadName.c_str());
 	if(FAILED(hr)){